1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -data-layout="e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64" -instsimplify -S | FileCheck %s --check-prefixes=CHECK,LE
3; RUN: opt < %s -data-layout="E-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64" -instsimplify -S | FileCheck %s --check-prefixes=CHECK,BE
4
5; {{ 0xDEADBEEF, 0xBA }, 0xCAFEBABE}
6@g1 = constant {{i32,i8},i32} {{i32,i8} { i32 -559038737, i8 186 }, i32 -889275714 }
7@g2 = constant double 1.0
8; { 0x7B, 0x06B1BFF8 }
9@g3 = constant {i64, i64} { i64 123, i64 112312312 }
10
11; Simple load
12define i32 @test1() {
13; CHECK-LABEL: @test1(
14; CHECK-NEXT:    ret i32 -559038737
15;
16  %r = load i32, i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0)
17  ret i32 %r
18}
19
20; PR3152
21; Load of first 16 bits of 32-bit value.
22define i16 @test2() {
23; LE-LABEL: @test2(
24; LE-NEXT:    ret i16 -16657
25;
26; BE-LABEL: @test2(
27; BE-NEXT:    ret i16 -8531
28;
29  %r = load i16, i16* bitcast(i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*)
30  ret i16 %r
31}
32
33define i16 @test2_addrspacecast() {
34; LE-LABEL: @test2_addrspacecast(
35; LE-NEXT:    ret i16 -16657
36;
37; BE-LABEL: @test2_addrspacecast(
38; BE-NEXT:    ret i16 -8531
39;
40  %r = load i16, i16 addrspace(1)* addrspacecast(i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16 addrspace(1)*)
41  ret i16 %r
42}
43
44; Load of second 16 bits of 32-bit value.
45define i16 @test3() {
46; LE-LABEL: @test3(
47; LE-NEXT:    ret i16 -8531
48;
49; BE-LABEL: @test3(
50; BE-NEXT:    ret i16 -16657
51;
52  %r = load i16, i16* getelementptr(i16, i16* bitcast(i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 1)
53  ret i16 %r
54}
55
56; Load of 8 bit field + tail padding.
57define i16 @test4() {
58; LE-LABEL: @test4(
59; LE-NEXT:    ret i16 186
60;
61; BE-LABEL: @test4(
62; BE-NEXT:    ret i16 -17920
63;
64  %r = load i16, i16* getelementptr(i16, i16* bitcast(i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*), i32 2)
65  ret i16 %r
66}
67
68; Load of double bits.
69define i64 @test6() {
70; CHECK-LABEL: @test6(
71; CHECK-NEXT:    ret i64 4607182418800017408
72;
73  %r = load i64, i64* bitcast(double* @g2 to i64*)
74  ret i64 %r
75}
76
77; Load of double bits.
78define i16 @test7() {
79; LE-LABEL: @test7(
80; LE-NEXT:    ret i16 0
81;
82; BE-LABEL: @test7(
83; BE-NEXT:    ret i16 16368
84;
85  %r = load i16, i16* bitcast(double* @g2 to i16*)
86  ret i16 %r
87}
88
89; Double load.
90define double @test8() {
91; LE-LABEL: @test8(
92; LE-NEXT:    ret double 0xBADEADBEEF
93;
94; BE-LABEL: @test8(
95; BE-NEXT:    ret double 0xDEADBEEFBA000000
96;
97  %r = load double, double* bitcast({{i32,i8},i32}* @g1 to double*)
98  ret double %r
99}
100
101
102; i128 load.
103define i128 @test9() {
104; LE-LABEL: @test9(
105; LE-NEXT:    ret i128 2071796475790618158476296315
106;
107; BE-LABEL: @test9(
108; BE-NEXT:    ret i128 2268949521066387161080
109;
110  %r = load i128, i128* bitcast({i64, i64}* @g3 to i128*)
111  ret i128 %r
112}
113
114; vector load.
115define <2 x i64> @test10() {
116; CHECK-LABEL: @test10(
117; CHECK-NEXT:    ret <2 x i64> <i64 123, i64 112312312>
118;
119  %r = load <2 x i64>, <2 x i64>* bitcast({i64, i64}* @g3 to <2 x i64>*)
120  ret <2 x i64> %r
121}
122
123
124; PR5287
125; { 0xA1, 0x08 }
126@g4 = internal constant { i8, i8 } { i8 -95, i8 8 }
127
128define i16 @test11() nounwind {
129; LE-LABEL: @test11(
130; LE-NEXT:  entry:
131; LE-NEXT:    ret i16 2209
132;
133; BE-LABEL: @test11(
134; BE-NEXT:  entry:
135; BE-NEXT:    ret i16 -24312
136;
137entry:
138  %a = load i16, i16* bitcast ({ i8, i8 }* @g4 to i16*)
139  ret i16 %a
140}
141
142
143; PR5551
144@test12g = private constant [6 x i8] c"a\00b\00\00\00"
145
146define i16 @test12() {
147; LE-LABEL: @test12(
148; LE-NEXT:    ret i16 98
149;
150; BE-LABEL: @test12(
151; BE-NEXT:    ret i16 25088
152;
153  %a = load i16, i16* getelementptr inbounds ([3 x i16], [3 x i16]* bitcast ([6 x i8]* @test12g to [3 x i16]*), i32 0, i64 1)
154  ret i16 %a
155}
156
157
158; PR5978
159@g5 = constant i8 4
160define i1 @test13() {
161; CHECK-LABEL: @test13(
162; CHECK-NEXT:    ret i1 false
163;
164  %A = load i1, i1* bitcast (i8* @g5 to i1*)
165  ret i1 %A
166}
167
168@g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)]
169define i64 @test14() nounwind {
170; CHECK-LABEL: @test14(
171; CHECK-NEXT:  entry:
172; CHECK-NEXT:    ret i64 1
173;
174entry:
175  %tmp = load i64, i64* bitcast ([2 x i8*]* @g6 to i64*)
176  ret i64 %tmp
177}
178
179; Check with address space pointers
180@g6_as1 = constant [2 x i8 addrspace(1)*] [i8 addrspace(1)* inttoptr (i16 1 to i8 addrspace(1)*), i8 addrspace(1)* inttoptr (i16 2 to i8 addrspace(1)*)]
181define i16 @test14_as1() nounwind {
182; CHECK-LABEL: @test14_as1(
183; CHECK-NEXT:  entry:
184; CHECK-NEXT:    ret i16 1
185;
186entry:
187  %tmp = load i16, i16* bitcast ([2 x i8 addrspace(1)*]* @g6_as1 to i16*)
188  ret i16 %tmp
189}
190
191define i64 @test15() nounwind {
192; CHECK-LABEL: @test15(
193; CHECK-NEXT:  entry:
194; CHECK-NEXT:    ret i64 2
195;
196entry:
197  %tmp = load i64, i64* bitcast (i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @g6, i32 0, i64 1) to i64*)
198  ret i64 %tmp
199}
200
201@gv7 = constant [4 x i8*] [i8* null, i8* inttoptr (i64 -14 to i8*), i8* null, i8* null]
202define i64 @test16.1() {
203; CHECK-LABEL: @test16.1(
204; CHECK-NEXT:    ret i64 0
205;
206  %v = load i64, i64* bitcast ([4 x i8*]* @gv7 to i64*), align 8
207  ret i64 %v
208}
209
210define i64 @test16.2() {
211; CHECK-LABEL: @test16.2(
212; CHECK-NEXT:    ret i64 -14
213;
214  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 1) to i64*), align 8
215  ret i64 %v
216}
217
218define i64 @test16.3() {
219; CHECK-LABEL: @test16.3(
220; CHECK-NEXT:    ret i64 0
221;
222  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 2) to i64*), align 8
223  ret i64 %v
224}
225
226@g7 = constant {[0 x i32], [0 x i8], {}*} { [0 x i32] undef, [0 x i8] undef, {}* null }
227
228define i64* @test_leading_zero_size_elems() {
229; CHECK-LABEL: @test_leading_zero_size_elems(
230; CHECK-NEXT:    ret i64* null
231;
232  %v = load i64*, i64** bitcast ({[0 x i32], [0 x i8], {}*}* @g7 to i64**)
233  ret i64* %v
234}
235
236@g8 = constant {[4294967295 x [0 x i32]], i64} { [4294967295 x [0 x i32]] undef, i64 123 }
237
238define i64 @test_leading_zero_size_elems_big() {
239; CHECK-LABEL: @test_leading_zero_size_elems_big(
240; CHECK-NEXT:    ret i64 123
241;
242  %v = load i64, i64* bitcast ({[4294967295 x [0 x i32]], i64}* @g8 to i64*)
243  ret i64 %v
244}
245
246@g9 = constant [4294967295 x [0 x i32]] zeroinitializer
247
248define i64 @test_array_of_zero_size_array() {
249; CHECK-LABEL: @test_array_of_zero_size_array(
250; CHECK-NEXT:    ret i64 undef
251;
252  %v = load i64, i64* bitcast ([4294967295 x [0 x i32]]* @g9 to i64*)
253  ret i64 %v
254}
255
256@g_undef = constant { i128 } undef
257
258define i32* @test_undef_aggregate() {
259; CHECK-LABEL: @test_undef_aggregate(
260; CHECK-NEXT:    ret i32* undef
261;
262  %v = load i32*, i32** bitcast ({i128}* @g_undef to i32**)
263  ret i32* %v
264}
265
266@g_poison = constant { i128 } poison
267
268define i32* @test_poison_aggregate() {
269; CHECK-LABEL: @test_poison_aggregate(
270; CHECK-NEXT:    ret i32* poison
271;
272  %v = load i32*, i32** bitcast ({i128}* @g_poison to i32**)
273  ret i32* %v
274}
275
276@g11 = constant <{ [8 x i8], [8 x i8] }> <{ [8 x i8] undef, [8 x i8] zeroinitializer }>, align 4
277
278define {}* @test_trailing_zero_gep_index() {
279; CHECK-LABEL: @test_trailing_zero_gep_index(
280; CHECK-NEXT:    ret {}* null
281;
282  %v = load {}*, {}** bitcast (i8* getelementptr inbounds (<{ [8 x i8], [8 x i8] }>, <{ [8 x i8], [8 x i8] }>* @g11, i32 0, i32 1, i32 0) to {}**), align 4
283  ret {}* %v
284}
285
286define { i64, i64 } @test_load_struct() {
287; CHECK-LABEL: @test_load_struct(
288; CHECK-NEXT:    ret { i64, i64 } { i64 123, i64 112312312 }
289;
290  %v = load { i64, i64 }, { i64, i64 }* @g3
291  ret { i64, i64 } %v
292}
293
294@m64 = internal constant [2 x i64] zeroinitializer
295@idx = external global i32
296
297; This should not try to create an x86_mmx null value.
298define x86_mmx @load_mmx() {
299; CHECK-LABEL: @load_mmx(
300; CHECK-NEXT:    [[TEMP:%.*]] = load x86_mmx, x86_mmx* bitcast (i64* getelementptr ([2 x i64], [2 x i64]* @m64, i64 0, i64 ptrtoint (i32* @idx to i64)) to x86_mmx*), align 8
301; CHECK-NEXT:    ret x86_mmx [[TEMP]]
302;
303  %temp = load x86_mmx, x86_mmx* bitcast (i64* getelementptr ([2 x i64], [2 x i64]* @m64, i64 0, i64 ptrtoint (i32* @idx to i64)) to x86_mmx*)
304  ret x86_mmx %temp
305}
306
307@g_offset = external global i64
308
309@g_neg_one_vec = constant <4 x i8> <i8 -1, i8 -1, i8 -1, i8 -1>
310
311define i8 @load_neg_one_at_unknown_offset() {
312; CHECK-LABEL: @load_neg_one_at_unknown_offset(
313; CHECK-NEXT:    ret i8 -1
314;
315  %v = load i8, i8* getelementptr (<4 x i8>, <4 x i8>* @g_neg_one_vec, i64 0, i64 ptrtoint (i64* @g_offset to i64))
316  ret i8 %v
317}
318
319@g_with_padding = constant { i32, [4 x i8] } { i32 0, [4 x i8] undef }
320
321define i32 @load_padding() {
322; CHECK-LABEL: @load_padding(
323; CHECK-NEXT:    ret i32 undef
324;
325  %v = load i32, i32* getelementptr (i32, i32* bitcast ({ i32, [4 x i8] }* @g_with_padding to i32*), i64 1)
326  ret i32 %v
327}
328
329@g_all_undef = constant { i32, [4 x i8] } undef
330
331; Same as the previous case, but with an all-undef initializer.
332define i32 @load_all_undef() {
333; CHECK-LABEL: @load_all_undef(
334; CHECK-NEXT:    ret i32 undef
335;
336  %v = load i32, i32* getelementptr (i32, i32* bitcast ({ i32, [4 x i8] }* @g_all_undef to i32*), i64 1)
337  ret i32 %v
338}
339