summaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll
blob: e43dad2ca08937960a19bf982f3099e0f5c55308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=aggressive-instcombine -S -data-layout="e" | FileCheck %s --check-prefixes=CHECK,LE
; RUN: opt < %s -passes=aggressive-instcombine -S -data-layout="E" | FileCheck %s --check-prefixes=CHECK,BE


@constarray1 = internal constant [8 x i8]  c"\01\00\01\00\01\00\01\00", align 4
@constarray2 = internal constant [8 x i8]  c"\FF\FF\01\00\01\00\01\00", align 4

@g = internal constant i32 42
@constptrarray = internal constant [4 x ptr]  [ptr @g, ptr @g, ptr @g, ptr @g], align 4

@constpackedstruct = internal constant <{[8 x i8]}>  <{[8 x i8] c"\01\00\01\00\01\00\01\00"}>, align 4
@conststruct = internal constant {i16, [8 x i8]}  {i16 1, [8 x i8] c"\01\00\01\00\01\00\01\00"}, align 4

%struct = type { i128 }
@global = internal constant %struct { i128 1 }
; TODO: this should be folded, but currently i128 is not folded.
define i32 @no-gep-128-struct(i64 %idx){
; CHECK-LABEL: @no-gep-128-struct(
; CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr @global, align 4
; CHECK-NEXT:    ret i32 [[TMP1]]
;
  %1 = load i32, ptr @global, align 4
  ret i32 %1
}

define i8 @inbounds_gep_load_i8_align2(i64 %idx){
; CHECK-LABEL: @inbounds_gep_load_i8_align2(
; CHECK-NEXT:    ret i8 1
;
  %1 = getelementptr inbounds i8, ptr @constarray1, i64 %idx
  %2 = load i8, ptr %1, align 2
  ret i8 %2
}

; can't be folded because access with i8 strides is not patterned.
define i8 @inbounds_gep_load_i8_align1(i64 %idx){
; CHECK-LABEL: @inbounds_gep_load_i8_align1(
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i8, ptr @constarray1, i64 [[IDX:%.*]]
; CHECK-NEXT:    [[TMP2:%.*]] = load i8, ptr [[TMP1]], align 1
; CHECK-NEXT:    ret i8 [[TMP2]]
;
  %1 = getelementptr inbounds i8, ptr @constarray1, i64 %idx
  %2 = load i8, ptr %1, align 1
  ret i8 %2
}

; can't be folded because volatile load cannot assure same results.
define i8 @inbounds_gep_load_i8_align2_volatile(i64 %idx){
; CHECK-LABEL: @inbounds_gep_load_i8_align2_volatile(
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i8, ptr @constarray1, i64 [[IDX:%.*]]
; CHECK-NEXT:    [[TMP2:%.*]] = load volatile i8, ptr [[TMP1]], align 2
; CHECK-NEXT:    ret i8 [[TMP2]]
;
  %1 = getelementptr inbounds i8, ptr @constarray1, i64 %idx
  %2 = load volatile i8, ptr %1, align 2
  ret i8 %2
}

declare ptr @llvm.ptrmask.p0.i64(ptr , i64)

; can't be folded because ptrmask can change ptr, while preserving provenance
; This invalidates GEP indices analysis
define i8 @inbounds_gep_load_i16_align1_ptrmasked(i64 %idx, i64 %mask){
; CHECK-LABEL: @inbounds_gep_load_i16_align1_ptrmasked(
; CHECK-NEXT:    [[TMP1:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr @constarray1, i64 [[MASK:%.*]])
; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds i16, ptr [[TMP1]], i64 [[IDX:%.*]]
; CHECK-NEXT:    [[TMP3:%.*]] = load i8, ptr [[TMP2]], align 1
; CHECK-NEXT:    ret i8 [[TMP3]]
;
  %1 = call ptr @llvm.ptrmask.p0.i64(ptr @constarray1, i64 %mask)
  %2 = getelementptr inbounds i16, ptr %1, i64 %idx
  %3 = load i8, ptr %2, align 1
  ret i8 %3
}

define i32 @inbounds_gep_i16_load_i32_align1(i64 %idx){
; LE-LABEL: @inbounds_gep_i16_load_i32_align1(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @inbounds_gep_i16_load_i32_align1(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr inbounds i16, ptr @constarray1, i64 %idx
  %2 = load i32, ptr %1, align 1
  ret i32 %2
}

define i32 @inbounds_gep_i32_load_i32_align8(i64 %idx){
; LE-LABEL: @inbounds_gep_i32_load_i32_align8(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @inbounds_gep_i32_load_i32_align8(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr inbounds i32, ptr @constarray1, i64 %idx
  %2 = load i32, ptr %1, align 8
  ret i32 %2
}

define i32 @inbounds_gep_i32_load_i32_const_offset(i64 %idx){
; LE-LABEL: @inbounds_gep_i32_load_i32_const_offset(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @inbounds_gep_i32_load_i32_const_offset(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr inbounds i16, ptr @constarray2, i64 1
  %2 = getelementptr inbounds i32, ptr %1, i64 %idx
  %3 = load i32, ptr %2, align 4
  ret i32 %3
}

define i32 @gep_load_i32_align2_const_offset(i64 %idx){
; LE-LABEL: @gep_load_i32_align2_const_offset(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @gep_load_i32_align2_const_offset(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr i16, ptr @constarray1, i64 -2
  %2 = getelementptr [3 x i16], ptr %1, i64 %idx
  %3 = load i32, ptr %2, align 2
  ret i32 %3
}

; can't be folded because if gep is non-inbounds,
; the offsets are silently-wrapped with two’s complement arithmetic(mod 2**64).
; So the load operand can be a base pointer of constarray2.
define i32 @gep_load_i32_align2_const_offset_wrap(i64 %idx){
; CHECK-LABEL: @gep_load_i32_align2_const_offset_wrap(
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr i16, ptr @constarray2, i64 -2
; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr [3 x i16], ptr [[TMP1]], i64 [[IDX:%.*]]
; CHECK-NEXT:    [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 2
; CHECK-NEXT:    ret i32 [[TMP3]]
;
  %1 = getelementptr i16, ptr @constarray2, i64 -2
  %2 = getelementptr [3 x i16], ptr %1, i64 %idx
  %3 = load i32, ptr %2, align 2
  ret i32 %3
}

define i32 @inbounds_gep_i32_load_i32_const_ptr_array(i64 %idx){
; CHECK-LABEL: @inbounds_gep_i32_load_i32_const_ptr_array(
; CHECK-NEXT:    ret i32 42
;
  %1 = getelementptr inbounds ptr, ptr @constptrarray, i64 %idx
  %2 = load ptr, ptr %1, align 4
  %3 = load i32, ptr %2, align 4
  ret i32 %3
}

define i32 @inbounds_gep_i32_load_i32_align4_packedstruct(i64 %idx){
; LE-LABEL: @inbounds_gep_i32_load_i32_align4_packedstruct(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @inbounds_gep_i32_load_i32_align4_packedstruct(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr inbounds i32, ptr @constpackedstruct, i64 %idx
  %2 = load i32, ptr %1, align 4
  ret i32 %2
}

; can't be folded because results are not equal
define i32 @inbounds_gep_i8_load_i32_align1_packedstruct(i64 %idx){
; CHECK-LABEL: @inbounds_gep_i8_load_i32_align1_packedstruct(
; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds i8, ptr @constpackedstruct, i64 [[IDX:%.*]]
; CHECK-NEXT:    [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 1
; CHECK-NEXT:    ret i32 [[TMP2]]
;
  %1 = getelementptr inbounds i8, ptr @constpackedstruct, i64 %idx
  %2 = load i32, ptr %1, align 1
  ret i32 %2
}

define i32 @inbounds_gep_i32_load_i32_align4_struct_with_const_offset(i64 %idx){
; LE-LABEL: @inbounds_gep_i32_load_i32_align4_struct_with_const_offset(
; LE-NEXT:    ret i32 65537
;
; BE-LABEL: @inbounds_gep_i32_load_i32_align4_struct_with_const_offset(
; BE-NEXT:    ret i32 16777472
;
  %1 = getelementptr inbounds i16, ptr @conststruct, i64 1
  %2 = getelementptr inbounds i32, ptr %1, i64 %idx
  %3 = load i32, ptr %2, align 4
  ret i32 %3
}