1; RUN: opt < %s -default-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" -instcombine -S | FileCheck %s --check-prefix=LE
2; RUN: opt < %s -default-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" -instcombine -S | FileCheck %s --check-prefix=BE
3
4; {{ 0xDEADBEEF, 0xBA }, 0xCAFEBABE}
5@g1 = constant {{i32,i8},i32} {{i32,i8} { i32 -559038737, i8 186 }, i32 -889275714 }
6@g2 = constant double 1.0
7; { 0x7B, 0x06B1BFF8 }
8@g3 = constant {i64, i64} { i64 123, i64 112312312 }
9
10; Simple load
11define i32 @test1() {
12  %r = load i32, i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0)
13  ret i32 %r
14
15; 0xDEADBEEF
16; LE-LABEL: @test1(
17; LE: ret i32 -559038737
18
19; 0xDEADBEEF
20; BE-LABEL: @test1(
21; BE: ret i32 -559038737
22}
23
24; PR3152
25; Load of first 16 bits of 32-bit value.
26define i16 @test2() {
27  %r = load i16, i16* bitcast(i32* getelementptr ({{i32,i8},i32}, {{i32,i8},i32}* @g1, i32 0, i32 0, i32 0) to i16*)
28  ret i16 %r
29
30; 0xBEEF
31; LE-LABEL: @test2(
32; LE: ret i16 -16657
33
34; 0xDEAD
35; BE-LABEL: @test2(
36; BE: ret i16 -8531
37}
38
39define i16 @test2_addrspacecast() {
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; 0xBEEF
44; LE-LABEL: @test2_addrspacecast(
45; LE: ret i16 -16657
46
47; 0xDEAD
48; BE-LABEL: @test2_addrspacecast(
49; BE: ret i16 -8531
50}
51
52; Load of second 16 bits of 32-bit value.
53define i16 @test3() {
54  %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)
55  ret i16 %r
56
57; 0xDEAD
58; LE-LABEL: @test3(
59; LE: ret i16 -8531
60
61; 0xBEEF
62; BE-LABEL: @test3(
63; BE: ret i16 -16657
64}
65
66; Load of 8 bit field + tail padding.
67define i16 @test4() {
68  %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)
69  ret i16 %r
70
71; 0x00BA
72; LE-LABEL: @test4(
73; LE: ret i16 186
74
75; 0xBA00
76; BE-LABEL: @test4(
77; BE: ret i16 -17920
78}
79
80; Load of double bits.
81define i64 @test6() {
82  %r = load i64, i64* bitcast(double* @g2 to i64*)
83  ret i64 %r
84
85; 0x3FF_0000000000000
86; LE-LABEL: @test6(
87; LE: ret i64 4607182418800017408
88
89; 0x3FF_0000000000000
90; BE-LABEL: @test6(
91; BE: ret i64 4607182418800017408
92}
93
94; Load of double bits.
95define i16 @test7() {
96  %r = load i16, i16* bitcast(double* @g2 to i16*)
97  ret i16 %r
98
99; 0x0000
100; LE-LABEL: @test7(
101; LE: ret i16 0
102
103; 0x3FF0
104; BE-LABEL: @test7(
105; BE: ret i16 16368
106}
107
108; Double load.
109define double @test8() {
110  %r = load double, double* bitcast({{i32,i8},i32}* @g1 to double*)
111  ret double %r
112
113; LE-LABEL: @test8(
114; LE: ret double 0xBADEADBEEF
115
116; BE-LABEL: @test8(
117; BE: ret double 0xDEADBEEFBA000000
118}
119
120
121; i128 load.
122define i128 @test9() {
123  %r = load i128, i128* bitcast({i64, i64}* @g3 to i128*)
124  ret i128 %r
125
126; 0x00000000_06B1BFF8_00000000_0000007B
127; LE-LABEL: @test9(
128; LE: ret i128 2071796475790618158476296315
129
130; 0x00000000_0000007B_00000000_06B1BFF8
131; BE-LABEL: @test9(
132; BE: ret i128 2268949521066387161080
133}
134
135; vector load.
136define <2 x i64> @test10() {
137  %r = load <2 x i64>, <2 x i64>* bitcast({i64, i64}* @g3 to <2 x i64>*)
138  ret <2 x i64> %r
139
140; LE-LABEL: @test10(
141; LE: ret <2 x i64> <i64 123, i64 112312312>
142
143; BE-LABEL: @test10(
144; BE: ret <2 x i64> <i64 123, i64 112312312>
145}
146
147
148; PR5287
149; { 0xA1, 0x08 }
150@g4 = internal constant { i8, i8 } { i8 -95, i8 8 }
151
152define i16 @test11() nounwind {
153entry:
154  %a = load i16, i16* bitcast ({ i8, i8 }* @g4 to i16*)
155  ret i16 %a
156
157; 0x08A1
158; LE-LABEL: @test11(
159; LE: ret i16 2209
160
161; 0xA108
162; BE-LABEL: @test11(
163; BE: ret i16 -24312
164}
165
166
167; PR5551
168@test12g = private constant [6 x i8] c"a\00b\00\00\00"
169
170define i16 @test12() {
171  %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)
172  ret i16 %a
173
174; 0x0062
175; LE-LABEL: @test12(
176; LE: ret i16 98
177
178; 0x6200
179; BE-LABEL: @test12(
180; BE: ret i16 25088
181}
182
183
184; PR5978
185@g5 = constant i8 4
186define i1 @test13() {
187  %A = load i1, i1* bitcast (i8* @g5 to i1*)
188  ret i1 %A
189
190; LE-LABEL: @test13(
191; LE: ret i1 false
192
193; BE-LABEL: @test13(
194; BE: ret i1 false
195}
196
197@g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)]
198define i64 @test14() nounwind {
199entry:
200  %tmp = load i64, i64* bitcast ([2 x i8*]* @g6 to i64*)
201  ret i64 %tmp
202
203; LE-LABEL: @test14(
204; LE: ret i64 1
205
206; BE-LABEL: @test14(
207; BE: ret i64 1
208}
209
210; Check with address space pointers
211@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)*)]
212define i16 @test14_as1() nounwind {
213entry:
214  %tmp = load i16, i16* bitcast ([2 x i8 addrspace(1)*]* @g6_as1 to i16*)
215  ret i16 %tmp
216
217; LE: @test14_as1
218; LE: ret i16 1
219
220; BE: @test14_as1
221; BE: ret i16 1
222}
223
224define i64 @test15() nounwind {
225entry:
226  %tmp = load i64, i64* bitcast (i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @g6, i32 0, i64 1) to i64*)
227  ret i64 %tmp
228
229; LE-LABEL: @test15(
230; LE: ret i64 2
231
232; BE-LABEL: @test15(
233; BE: ret i64 2
234}
235
236@gv7 = constant [4 x i8*] [i8* null, i8* inttoptr (i64 -14 to i8*), i8* null, i8* null]
237define i64 @test16.1() {
238  %v = load i64, i64* bitcast ([4 x i8*]* @gv7 to i64*), align 8
239  ret i64 %v
240
241; LE-LABEL: @test16.1(
242; LE: ret i64 0
243
244; BE-LABEL: @test16.1(
245; BE: ret i64 0
246}
247
248define i64 @test16.2() {
249  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 1) to i64*), align 8
250  ret i64 %v
251
252; LE-LABEL: @test16.2(
253; LE: ret i64 -14
254
255; BE-LABEL: @test16.2(
256; BE: ret i64 -14
257}
258
259define i64 @test16.3() {
260  %v = load i64, i64* bitcast (i8** getelementptr inbounds ([4 x i8*], [4 x i8*]* @gv7, i64 0, i64 2) to i64*), align 8
261  ret i64 %v
262
263; LE-LABEL: @test16.3(
264; LE: ret i64 0
265
266; BE-LABEL: @test16.3(
267; BE: ret i64 0
268}
269