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-p2:64:64:64-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-ni:2" -passes=instsimplify -S | FileCheck %s --check-prefixes=CHECK,LE
3; RUN: opt < %s -data-layout="E-p:64:64:64-p1:16:16:16-p2:64:64:64-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-ni:2" -passes=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 @test_i128() {
104; LE-LABEL: @test_i128(
105; LE-NEXT:    ret i128 2071796475790618158476296315
106;
107; BE-LABEL: @test_i128(
108; BE-NEXT:    ret i128 2268949521066387161080
109;
110  %r = load i128, i128* bitcast({i64, i64}* @g3 to i128*)
111  ret i128 %r
112}
113
114define fp128 @test_fp128() {
115; LE-LABEL: @test_fp128(
116; LE-NEXT:    ret fp128 0xL000000000000007B0000000006B1BFF8
117;
118; BE-LABEL: @test_fp128(
119; BE-NEXT:    ret fp128 0xL0000000006B1BFF8000000000000007B
120;
121  %r = load fp128, fp128* bitcast({i64, i64}* @g3 to fp128*)
122  ret fp128 %r
123}
124
125define ppc_fp128 @test_ppc_fp128() {
126; LE-LABEL: @test_ppc_fp128(
127; LE-NEXT:    ret ppc_fp128 bitcast (i128 2071796475790618158476296315 to ppc_fp128)
128;
129; BE-LABEL: @test_ppc_fp128(
130; BE-NEXT:    ret ppc_fp128 bitcast (i128 2268949521066387161080 to ppc_fp128)
131;
132  %r = load ppc_fp128, ppc_fp128* bitcast({i64, i64}* @g3 to ppc_fp128*)
133  ret ppc_fp128 %r
134}
135
136define x86_fp80 @test_x86_fp80() {
137; LE-LABEL: @test_x86_fp80(
138; LE-NEXT:    ret x86_fp80 0xKFFFF000000000000007B
139;
140; BE-LABEL: @test_x86_fp80(
141; BE-NEXT:    ret x86_fp80 0xK000000000000007B0000
142;
143  %r = load x86_fp80, x86_fp80* bitcast({i64, i64}* @g3 to x86_fp80*)
144  ret x86_fp80 %r
145}
146
147define bfloat @test_bfloat() {
148; LE-LABEL: @test_bfloat(
149; LE-NEXT:    ret bfloat 0xR007B
150;
151; BE-LABEL: @test_bfloat(
152; BE-NEXT:    ret bfloat 0xR0000
153;
154  %r = load bfloat, bfloat* bitcast({i64, i64}* @g3 to bfloat*)
155  ret bfloat %r
156}
157
158; vector load.
159define <2 x i64> @test10() {
160; CHECK-LABEL: @test10(
161; CHECK-NEXT:    ret <2 x i64> <i64 123, i64 112312312>
162;
163  %r = load <2 x i64>, <2 x i64>* bitcast({i64, i64}* @g3 to <2 x i64>*)
164  ret <2 x i64> %r
165}
166
167
168; PR5287
169; { 0xA1, 0x08 }
170@g4 = internal constant { i8, i8 } { i8 -95, i8 8 }
171
172define i16 @test11() nounwind {
173; LE-LABEL: @test11(
174; LE-NEXT:  entry:
175; LE-NEXT:    ret i16 2209
176;
177; BE-LABEL: @test11(
178; BE-NEXT:  entry:
179; BE-NEXT:    ret i16 -24312
180;
181entry:
182  %a = load i16, i16* bitcast ({ i8, i8 }* @g4 to i16*)
183  ret i16 %a
184}
185
186
187; PR5551
188@test12g = private constant [6 x i8] c"a\00b\00\00\00"
189
190define i16 @test12() {
191; LE-LABEL: @test12(
192; LE-NEXT:    ret i16 98
193;
194; BE-LABEL: @test12(
195; BE-NEXT:    ret i16 25088
196;
197  %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)
198  ret i16 %a
199}
200
201
202; PR5978
203@g5 = constant i8 4
204define i1 @test13() {
205; CHECK-LABEL: @test13(
206; CHECK-NEXT:    ret i1 false
207;
208  %A = load i1, i1* bitcast (i8* @g5 to i1*)
209  ret i1 %A
210}
211
212@g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)]
213define i64 @test14() nounwind {
214; CHECK-LABEL: @test14(
215; CHECK-NEXT:  entry:
216; CHECK-NEXT:    ret i64 1
217;
218entry:
219  %tmp = load i64, i64* bitcast ([2 x i8*]* @g6 to i64*)
220  ret i64 %tmp
221}
222
223; Check with address space pointers
224@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)*)]
225define i16 @test14_as1() nounwind {
226; CHECK-LABEL: @test14_as1(
227; CHECK-NEXT:  entry:
228; CHECK-NEXT:    ret i16 1
229;
230entry:
231  %tmp = load i16, i16* bitcast ([2 x i8 addrspace(1)*]* @g6_as1 to i16*)
232  ret i16 %tmp
233}
234
235define i64 @test15() nounwind {
236; CHECK-LABEL: @test15(
237; CHECK-NEXT:  entry:
238; CHECK-NEXT:    ret i64 2
239;
240entry:
241  %tmp = load i64, i64* bitcast (i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @g6, i32 0, i64 1) to i64*)
242  ret i64 %tmp
243}
244
245@gv7 = constant [4 x i8*] [i8* null, i8* inttoptr (i64 -14 to i8*), i8* null, i8* null]
246define i64 @test16.1() {
247; CHECK-LABEL: @test16.1(
248; CHECK-NEXT:    ret i64 0
249;
250  %v = load i64, i64* bitcast ([4 x i8*]* @gv7 to i64*), align 8
251  ret i64 %v
252}
253
254define i64 @test16.2() {
255; CHECK-LABEL: @test16.2(
256; CHECK-NEXT:    ret i64 -14
257;
258  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 1) to i64*), align 8
259  ret i64 %v
260}
261
262define i64 @test16.3() {
263; CHECK-LABEL: @test16.3(
264; CHECK-NEXT:    ret i64 0
265;
266  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 2) to i64*), align 8
267  ret i64 %v
268}
269
270@g7 = constant {[0 x i32], [0 x i8], {}*} { [0 x i32] undef, [0 x i8] undef, {}* null }
271
272define i64* @test_leading_zero_size_elems() {
273; CHECK-LABEL: @test_leading_zero_size_elems(
274; CHECK-NEXT:    ret i64* null
275;
276  %v = load i64*, i64** bitcast ({[0 x i32], [0 x i8], {}*}* @g7 to i64**)
277  ret i64* %v
278}
279
280@g8 = constant {[4294967295 x [0 x i32]], i64} { [4294967295 x [0 x i32]] undef, i64 123 }
281
282define i64 @test_leading_zero_size_elems_big() {
283; CHECK-LABEL: @test_leading_zero_size_elems_big(
284; CHECK-NEXT:    ret i64 123
285;
286  %v = load i64, i64* bitcast ({[4294967295 x [0 x i32]], i64}* @g8 to i64*)
287  ret i64 %v
288}
289
290@g9 = constant [4294967295 x [0 x i32]] zeroinitializer
291
292define i64 @test_array_of_zero_size_array() {
293; CHECK-LABEL: @test_array_of_zero_size_array(
294; CHECK-NEXT:    ret i64 undef
295;
296  %v = load i64, i64* bitcast ([4294967295 x [0 x i32]]* @g9 to i64*)
297  ret i64 %v
298}
299
300@g_undef = constant { i128 } undef
301
302define i32* @test_undef_aggregate() {
303; CHECK-LABEL: @test_undef_aggregate(
304; CHECK-NEXT:    ret i32* undef
305;
306  %v = load i32*, i32** bitcast ({i128}* @g_undef to i32**)
307  ret i32* %v
308}
309
310@g_poison = constant { i128 } poison
311
312define i32* @test_poison_aggregate() {
313; CHECK-LABEL: @test_poison_aggregate(
314; CHECK-NEXT:    ret i32* poison
315;
316  %v = load i32*, i32** bitcast ({i128}* @g_poison to i32**)
317  ret i32* %v
318}
319
320@g11 = constant <{ [8 x i8], [8 x i8] }> <{ [8 x i8] undef, [8 x i8] zeroinitializer }>, align 4
321
322define {}* @test_trailing_zero_gep_index() {
323; CHECK-LABEL: @test_trailing_zero_gep_index(
324; CHECK-NEXT:    ret {}* null
325;
326  %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
327  ret {}* %v
328}
329
330define { i64, i64 } @test_load_struct() {
331; CHECK-LABEL: @test_load_struct(
332; CHECK-NEXT:    ret { i64, i64 } { i64 123, i64 112312312 }
333;
334  %v = load { i64, i64 }, { i64, i64 }* @g3
335  ret { i64, i64 } %v
336}
337
338@m64 = internal constant [2 x i64] zeroinitializer
339@idx = external global i32
340
341; This should not try to create an x86_mmx null value.
342define x86_mmx @load_mmx() {
343; CHECK-LABEL: @load_mmx(
344; 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
345; CHECK-NEXT:    ret x86_mmx [[TEMP]]
346;
347  %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*)
348  ret x86_mmx %temp
349}
350
351@g_offset = external global i64
352
353@g_neg_one_vec = constant <4 x i8> <i8 -1, i8 -1, i8 -1, i8 -1>
354
355define i8 @load_neg_one_at_unknown_offset() {
356; CHECK-LABEL: @load_neg_one_at_unknown_offset(
357; CHECK-NEXT:    ret i8 -1
358;
359  %v = load i8, i8* getelementptr (<4 x i8>, <4 x i8>* @g_neg_one_vec, i64 0, i64 ptrtoint (i64* @g_offset to i64))
360  ret i8 %v
361}
362
363@g_with_padding = constant { i32, [4 x i8] } { i32 0, [4 x i8] undef }
364
365define i32 @load_padding() {
366; CHECK-LABEL: @load_padding(
367; CHECK-NEXT:    ret i32 undef
368;
369  %v = load i32, i32* getelementptr (i32, i32* bitcast ({ i32, [4 x i8] }* @g_with_padding to i32*), i64 1)
370  ret i32 %v
371}
372
373@g_all_undef = constant { i32, [4 x i8] } undef
374
375; Same as the previous case, but with an all-undef initializer.
376define i32 @load_all_undef() {
377; CHECK-LABEL: @load_all_undef(
378; CHECK-NEXT:    ret i32 undef
379;
380  %v = load i32, i32* getelementptr (i32, i32* bitcast ({ i32, [4 x i8] }* @g_all_undef to i32*), i64 1)
381  ret i32 %v
382}
383
384@g_i8_data = constant [16 x i8] c"\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
385
386define i64* @load_ptr_from_i8_data() {
387; LE-LABEL: @load_ptr_from_i8_data(
388; LE-NEXT:    ret i64* inttoptr (i64 1 to i64*)
389;
390; BE-LABEL: @load_ptr_from_i8_data(
391; BE-NEXT:    ret i64* inttoptr (i64 72057594037927936 to i64*)
392;
393  %v = load i64*, i64** bitcast ([16 x i8]* @g_i8_data to i64**)
394  ret i64* %v
395}
396
397define i64 addrspace(2)* @load_non_integral_ptr_from_i8_data() {
398; CHECK-LABEL: @load_non_integral_ptr_from_i8_data(
399; CHECK-NEXT:    [[V:%.*]] = load i64 addrspace(2)*, i64 addrspace(2)** bitcast ([16 x i8]* @g_i8_data to i64 addrspace(2)**), align 8
400; CHECK-NEXT:    ret i64 addrspace(2)* [[V]]
401;
402  %v = load i64 addrspace(2)*, i64 addrspace(2)** bitcast ([16 x i8]* @g_i8_data to i64 addrspace(2)**)
403  ret i64 addrspace(2)* %v
404}
405