xref: /aosp_15_r20/external/llvm/test/Transforms/InstCombine/getelementptr.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt < %s -instcombine -S | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker
3*9880d681SAndroid Build Coastguard Workertarget datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker%intstruct = type { i32 }
6*9880d681SAndroid Build Coastguard Worker%pair = type { i32, i32 }
7*9880d681SAndroid Build Coastguard Worker%struct.B = type { double }
8*9880d681SAndroid Build Coastguard Worker%struct.A = type { %struct.B, i32, i32 }
9*9880d681SAndroid Build Coastguard Worker%struct.C = type { [7 x i8] }
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Worker
12*9880d681SAndroid Build Coastguard Worker@Global = constant [10 x i8] c"helloworld"
13*9880d681SAndroid Build Coastguard Worker@Global_as1 = addrspace(1) constant [10 x i8] c"helloworld"
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker; Test noop elimination
16*9880d681SAndroid Build Coastguard Workerdefine i32* @test1(i32* %I) {
17*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* %I, i64 0
18*9880d681SAndroid Build Coastguard Worker        ret i32* %A
19*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test1(
20*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32* %I
21*9880d681SAndroid Build Coastguard Worker}
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) {
24*9880d681SAndroid Build Coastguard Worker  %A = getelementptr i32, i32 addrspace(1)* %I, i64 0
25*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %A
26*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test1_as1(
27*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 addrspace(1)* %I
28*9880d681SAndroid Build Coastguard Worker}
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker; Test noop elimination
31*9880d681SAndroid Build Coastguard Workerdefine i32* @test2(i32* %I) {
32*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* %I
33*9880d681SAndroid Build Coastguard Worker        ret i32* %A
34*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2(
35*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32* %I
36*9880d681SAndroid Build Coastguard Worker}
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker; Test that two array indexing geps fold
39*9880d681SAndroid Build Coastguard Workerdefine i32* @test3(i32* %I) {
40*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* %I, i64 17
41*9880d681SAndroid Build Coastguard Worker        %B = getelementptr i32, i32* %A, i64 4
42*9880d681SAndroid Build Coastguard Worker        ret i32* %B
43*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test3(
44*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr i32, i32* %I, i64 21
45*9880d681SAndroid Build Coastguard Worker}
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker; Test that two getelementptr insts fold
48*9880d681SAndroid Build Coastguard Workerdefine i32* @test4({ i32 }* %I) {
49*9880d681SAndroid Build Coastguard Worker        %A = getelementptr { i32 }, { i32 }* %I, i64 1
50*9880d681SAndroid Build Coastguard Worker        %B = getelementptr { i32 }, { i32 }* %A, i64 0, i32 0
51*9880d681SAndroid Build Coastguard Worker        ret i32* %B
52*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test4(
53*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr { i32 }, { i32 }* %I, i64 1, i32 0
54*9880d681SAndroid Build Coastguard Worker}
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Workerdefine void @test5(i8 %B) {
57*9880d681SAndroid Build Coastguard Worker        ; This should be turned into a constexpr instead of being an instruction
58*9880d681SAndroid Build Coastguard Worker        %A = getelementptr [10 x i8], [10 x i8]* @Global, i64 0, i64 4
59*9880d681SAndroid Build Coastguard Worker        store i8 %B, i8* %A
60*9880d681SAndroid Build Coastguard Worker        ret void
61*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test5(
62*9880d681SAndroid Build Coastguard Worker; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i64 0, i64 4)
63*9880d681SAndroid Build Coastguard Worker}
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Workerdefine void @test5_as1(i8 %B) {
66*9880d681SAndroid Build Coastguard Worker        ; This should be turned into a constexpr instead of being an instruction
67*9880d681SAndroid Build Coastguard Worker        %A = getelementptr [10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4
68*9880d681SAndroid Build Coastguard Worker        store i8 %B, i8 addrspace(1)* %A
69*9880d681SAndroid Build Coastguard Worker        ret void
70*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test5_as1(
71*9880d681SAndroid Build Coastguard Worker; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4)
72*9880d681SAndroid Build Coastguard Worker}
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker%as1_ptr_struct = type { i32 addrspace(1)* }
75*9880d681SAndroid Build Coastguard Worker%as2_ptr_struct = type { i32 addrspace(2)* }
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker@global_as2 = addrspace(2) global i32 zeroinitializer
78*9880d681SAndroid Build Coastguard Worker@global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker; This should be turned into a constexpr instead of being an instruction
81*9880d681SAndroid Build Coastguard Workerdefine void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
82*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
83*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8
84*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
85*9880d681SAndroid Build Coastguard Worker  %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0
86*9880d681SAndroid Build Coastguard Worker  store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
87*9880d681SAndroid Build Coastguard Worker  ret void
88*9880d681SAndroid Build Coastguard Worker}
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker@arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Workerdefine void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
93*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
94*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret void
97*9880d681SAndroid Build Coastguard Worker  %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
98*9880d681SAndroid Build Coastguard Worker  store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
99*9880d681SAndroid Build Coastguard Worker  ret void
100*9880d681SAndroid Build Coastguard Worker}
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Workerdefine i32* @test7(i32* %I, i64 %C, i64 %D) {
103*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* %I, i64 %C
104*9880d681SAndroid Build Coastguard Worker        %B = getelementptr i32, i32* %A, i64 %D
105*9880d681SAndroid Build Coastguard Worker        ret i32* %B
106*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test7(
107*9880d681SAndroid Build Coastguard Worker; CHECK: %A = getelementptr i32, i32* %I, i64 %C
108*9880d681SAndroid Build Coastguard Worker; CHECK: %B = getelementptr i32, i32* %A, i64 %D
109*9880d681SAndroid Build Coastguard Worker}
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Workerdefine i8* @test8([10 x i32]* %X) {
112*9880d681SAndroid Build Coastguard Worker        ;; Fold into the cast.
113*9880d681SAndroid Build Coastguard Worker        %A = getelementptr [10 x i32], [10 x i32]* %X, i64 0, i64 0
114*9880d681SAndroid Build Coastguard Worker        %B = bitcast i32* %A to i8*
115*9880d681SAndroid Build Coastguard Worker        ret i8* %B
116*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test8(
117*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast [10 x i32]* %X to i8*
118*9880d681SAndroid Build Coastguard Worker}
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Workerdefine i32 @test9() {
121*9880d681SAndroid Build Coastguard Worker        %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1
122*9880d681SAndroid Build Coastguard Worker        %B = ptrtoint double* %A to i32
123*9880d681SAndroid Build Coastguard Worker        ret i32 %B
124*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test9(
125*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32 8
126*9880d681SAndroid Build Coastguard Worker}
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Workerdefine i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
129*9880d681SAndroid Build Coastguard Worker        %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
130*9880d681SAndroid Build Coastguard Worker        %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1
131*9880d681SAndroid Build Coastguard Worker        ;; seteq x, y
132*9880d681SAndroid Build Coastguard Worker        %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
133*9880d681SAndroid Build Coastguard Worker        ret i1 %tmp.4
134*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test10(
135*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq { i32, i32 }* %x, %y
136*9880d681SAndroid Build Coastguard Worker}
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Workerdefine i1 @test11({ i32, i32 }* %X) {
139*9880d681SAndroid Build Coastguard Worker        %P = getelementptr { i32, i32 }, { i32, i32 }* %X, i32 0, i32 0
140*9880d681SAndroid Build Coastguard Worker        %Q = icmp eq i32* %P, null
141*9880d681SAndroid Build Coastguard Worker        ret i1 %Q
142*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test11(
143*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq { i32, i32 }* %X, null
144*9880d681SAndroid Build Coastguard Worker}
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker; PR4748
148*9880d681SAndroid Build Coastguard Workerdefine i32 @test12(%struct.A* %a) {
149*9880d681SAndroid Build Coastguard Workerentry:
150*9880d681SAndroid Build Coastguard Worker  %g3 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 1
151*9880d681SAndroid Build Coastguard Worker  store i32 10, i32* %g3, align 4
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker  %g4 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 0
154*9880d681SAndroid Build Coastguard Worker
155*9880d681SAndroid Build Coastguard Worker  %new_a = bitcast %struct.B* %g4 to %struct.A*
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker  %g5 = getelementptr %struct.A, %struct.A* %new_a, i32 0, i32 1
158*9880d681SAndroid Build Coastguard Worker  %a_a = load i32, i32* %g5, align 4
159*9880d681SAndroid Build Coastguard Worker  ret i32 %a_a
160*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL:      @test12(
161*9880d681SAndroid Build Coastguard Worker; CHECK:      getelementptr %struct.A, %struct.A* %a, i64 0, i32 1
162*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: store i32 10, i32* %g3
163*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32 10
164*9880d681SAndroid Build Coastguard Worker}
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker; PR2235
168*9880d681SAndroid Build Coastguard Worker%S = type { i32, [ 100 x i32] }
169*9880d681SAndroid Build Coastguard Workerdefine i1 @test13(i64 %X, %S* %P) {
170*9880d681SAndroid Build Coastguard Worker        %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i64 %X
171*9880d681SAndroid Build Coastguard Worker        %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
172*9880d681SAndroid Build Coastguard Worker	%C = icmp eq i32* %A, %B
173*9880d681SAndroid Build Coastguard Worker	ret i1 %C
174*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13(
175*9880d681SAndroid Build Coastguard Worker; CHECK:    %C = icmp eq i64 %X, -1
176*9880d681SAndroid Build Coastguard Worker}
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Workerdefine <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
179*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_vector(
180*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: shl nuw <2 x i64> %X, <i64 2, i64 2>
181*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: add <2 x i64> %A.idx, <i64 4, i64 4>
182*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: icmp eq <2 x i64> %A.offs, zeroinitializer
183*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X
184*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
185*9880d681SAndroid Build Coastguard Worker  %C = icmp eq <2 x i32*> %A, %B
186*9880d681SAndroid Build Coastguard Worker  ret <2 x i1> %C
187*9880d681SAndroid Build Coastguard Worker}
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Workerdefine i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
190*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_as1(
191*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  %C = icmp eq i16 %X, -1
192*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i1 %C
193*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 1, i16 %X
194*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 0
195*9880d681SAndroid Build Coastguard Worker  %C = icmp eq i32 addrspace(1)* %A, %B
196*9880d681SAndroid Build Coastguard Worker  ret i1 %C
197*9880d681SAndroid Build Coastguard Worker}
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Workerdefine <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) {
200*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_vector_as1(
201*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: shl nuw <2 x i16> %X, <i16 2, i16 2>
202*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: add <2 x i16> %A.idx, <i16 4, i16 4>
203*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: icmp eq <2 x i16> %A.offs, zeroinitializer
204*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret <2 x i1>
205*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X
206*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0>
207*9880d681SAndroid Build Coastguard Worker  %C = icmp eq <2 x i32 addrspace(1)*> %A, %B
208*9880d681SAndroid Build Coastguard Worker  ret <2 x i1> %C
209*9880d681SAndroid Build Coastguard Worker}
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Workerdefine i1 @test13_i32(i32 %X, %S* %P) {
212*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_i32(
213*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp eq i32 %X, -1
214*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i32 %X
215*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
216*9880d681SAndroid Build Coastguard Worker  %C = icmp eq i32* %A, %B
217*9880d681SAndroid Build Coastguard Worker  ret i1 %C
218*9880d681SAndroid Build Coastguard Worker}
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard Workerdefine i1 @test13_i16(i16 %X, %S* %P) {
221*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_i16(
222*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp eq i16 %X, -1
223*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, %S* %P, i16 0, i32 1, i16 %X
224*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, %S* %P, i16 0, i32 0
225*9880d681SAndroid Build Coastguard Worker  %C = icmp eq i32* %A, %B
226*9880d681SAndroid Build Coastguard Worker  ret i1 %C
227*9880d681SAndroid Build Coastguard Worker}
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Workerdefine i1 @test13_i128(i128 %X, %S* %P) {
230*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test13_i128(
231*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp eq i64 %1, -1
232*9880d681SAndroid Build Coastguard Worker  %A = getelementptr inbounds %S, %S* %P, i128 0, i32 1, i128 %X
233*9880d681SAndroid Build Coastguard Worker  %B = getelementptr inbounds %S, %S* %P, i128 0, i32 0
234*9880d681SAndroid Build Coastguard Worker  %C = icmp eq i32* %A, %B
235*9880d681SAndroid Build Coastguard Worker  ret i1 %C
236*9880d681SAndroid Build Coastguard Worker}
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker@G = external global [3 x i8]
240*9880d681SAndroid Build Coastguard Workerdefine i8* @test14(i32 %Idx) {
241*9880d681SAndroid Build Coastguard Worker        %idx = zext i32 %Idx to i64
242*9880d681SAndroid Build Coastguard Worker        %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i64 %idx
243*9880d681SAndroid Build Coastguard Worker        ret i8* %tmp
244*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test14(
245*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [3 x i8], [3 x i8]* @G, i64 0, i64 %idx
246*9880d681SAndroid Build Coastguard Worker}
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker; Test folding of constantexpr geps into normal geps.
250*9880d681SAndroid Build Coastguard Worker@Array = external global [40 x i32]
251*9880d681SAndroid Build Coastguard Workerdefine i32 *@test15(i64 %X) {
252*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i64 0, i64 0), i64 %X
253*9880d681SAndroid Build Coastguard Worker        ret i32* %A
254*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test15(
255*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [40 x i32], [40 x i32]* @Array, i64 0, i64 %X
256*9880d681SAndroid Build Coastguard Worker}
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard Workerdefine i32* @test16(i32* %X, i32 %Idx) {
260*9880d681SAndroid Build Coastguard Worker        %R = getelementptr i32, i32* %X, i32 %Idx
261*9880d681SAndroid Build Coastguard Worker        ret i32* %R
262*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test16(
263*9880d681SAndroid Build Coastguard Worker; CHECK: sext i32 %Idx to i64
264*9880d681SAndroid Build Coastguard Worker}
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Workerdefine i1 @test17(i16* %P, i32 %I, i32 %J) {
268*9880d681SAndroid Build Coastguard Worker        %X = getelementptr inbounds i16, i16* %P, i32 %I
269*9880d681SAndroid Build Coastguard Worker        %Y = getelementptr inbounds i16, i16* %P, i32 %J
270*9880d681SAndroid Build Coastguard Worker        %C = icmp ult i16* %X, %Y
271*9880d681SAndroid Build Coastguard Worker        ret i1 %C
272*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test17(
273*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp slt i32 %I, %J
274*9880d681SAndroid Build Coastguard Worker}
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Workerdefine i1 @test18(i16* %P, i32 %I) {
277*9880d681SAndroid Build Coastguard Worker        %X = getelementptr inbounds i16, i16* %P, i32 %I
278*9880d681SAndroid Build Coastguard Worker        %C = icmp ult i16* %X, %P
279*9880d681SAndroid Build Coastguard Worker        ret i1 %C
280*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18(
281*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp slt i32 %I, 0
282*9880d681SAndroid Build Coastguard Worker}
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker; Larger than the pointer size for a non-zero address space
285*9880d681SAndroid Build Coastguard Workerdefine i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) {
286*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18_as1(
287*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %1 = trunc i32 %I to i16
288*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %C = icmp slt i16 %1, 0
289*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i1 %C
290*9880d681SAndroid Build Coastguard Worker  %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
291*9880d681SAndroid Build Coastguard Worker  %C = icmp ult i16 addrspace(1)* %X, %P
292*9880d681SAndroid Build Coastguard Worker  ret i1 %C
293*9880d681SAndroid Build Coastguard Worker}
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker; Smaller than the pointer size for a non-zero address space
296*9880d681SAndroid Build Coastguard Workerdefine i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) {
297*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18_as1_i32(
298*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %1 = trunc i32 %I to i16
299*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %C = icmp slt i16 %1, 0
300*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i1 %C
301*9880d681SAndroid Build Coastguard Worker  %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
302*9880d681SAndroid Build Coastguard Worker  %C = icmp ult i16 addrspace(1)* %X, %P
303*9880d681SAndroid Build Coastguard Worker  ret i1 %C
304*9880d681SAndroid Build Coastguard Worker}
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker; Smaller than pointer size
307*9880d681SAndroid Build Coastguard Workerdefine i1 @test18_i16(i16* %P, i16 %I) {
308*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18_i16(
309*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp slt i16 %I, 0
310*9880d681SAndroid Build Coastguard Worker  %X = getelementptr inbounds i16, i16* %P, i16 %I
311*9880d681SAndroid Build Coastguard Worker  %C = icmp ult i16* %X, %P
312*9880d681SAndroid Build Coastguard Worker  ret i1 %C
313*9880d681SAndroid Build Coastguard Worker}
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard Worker; Same as pointer size
316*9880d681SAndroid Build Coastguard Workerdefine i1 @test18_i64(i16* %P, i64 %I) {
317*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18_i64(
318*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp slt i64 %I, 0
319*9880d681SAndroid Build Coastguard Worker  %X = getelementptr inbounds i16, i16* %P, i64 %I
320*9880d681SAndroid Build Coastguard Worker  %C = icmp ult i16* %X, %P
321*9880d681SAndroid Build Coastguard Worker  ret i1 %C
322*9880d681SAndroid Build Coastguard Worker}
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker; Larger than the pointer size
325*9880d681SAndroid Build Coastguard Workerdefine i1 @test18_i128(i16* %P, i128 %I) {
326*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test18_i128(
327*9880d681SAndroid Build Coastguard Worker; CHECK: %C = icmp slt i64 %1, 0
328*9880d681SAndroid Build Coastguard Worker  %X = getelementptr inbounds i16, i16* %P, i128 %I
329*9880d681SAndroid Build Coastguard Worker  %C = icmp ult i16* %X, %P
330*9880d681SAndroid Build Coastguard Worker  ret i1 %C
331*9880d681SAndroid Build Coastguard Worker}
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Workerdefine i32 @test19(i32* %P, i32 %A, i32 %B) {
334*9880d681SAndroid Build Coastguard Worker        %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
335*9880d681SAndroid Build Coastguard Worker        %tmp.9 = getelementptr inbounds i32, i32* %P, i32 %B
336*9880d681SAndroid Build Coastguard Worker        %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
337*9880d681SAndroid Build Coastguard Worker        %tmp.11 = zext i1 %tmp.10 to i32
338*9880d681SAndroid Build Coastguard Worker        ret i32 %tmp.11
339*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test19(
340*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq i32 %A, %B
341*9880d681SAndroid Build Coastguard Worker}
342*9880d681SAndroid Build Coastguard Worker
343*9880d681SAndroid Build Coastguard Workerdefine i32 @test20(i32* %P, i32 %A, i32 %B) {
344*9880d681SAndroid Build Coastguard Worker        %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
345*9880d681SAndroid Build Coastguard Worker        %tmp.6 = icmp eq i32* %tmp.4, %P
346*9880d681SAndroid Build Coastguard Worker        %tmp.7 = zext i1 %tmp.6 to i32
347*9880d681SAndroid Build Coastguard Worker        ret i32 %tmp.7
348*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test20(
349*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq i32 %A, 0
350*9880d681SAndroid Build Coastguard Worker}
351*9880d681SAndroid Build Coastguard Worker
352*9880d681SAndroid Build Coastguard Workerdefine i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
353*9880d681SAndroid Build Coastguard Worker  %tmp.4 = getelementptr inbounds i32, i32 addrspace(1)* %P, i32 %A
354*9880d681SAndroid Build Coastguard Worker  %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
355*9880d681SAndroid Build Coastguard Worker  %tmp.7 = zext i1 %tmp.6 to i32
356*9880d681SAndroid Build Coastguard Worker  ret i32 %tmp.7
357*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test20_as1(
358*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq i16 %1, 0
359*9880d681SAndroid Build Coastguard Worker}
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Workerdefine i32 @test21() {
363*9880d681SAndroid Build Coastguard Worker        %pbob1 = alloca %intstruct
364*9880d681SAndroid Build Coastguard Worker        %pbob2 = getelementptr %intstruct, %intstruct* %pbob1
365*9880d681SAndroid Build Coastguard Worker        %pbobel = getelementptr %intstruct, %intstruct* %pbob2, i64 0, i32 0
366*9880d681SAndroid Build Coastguard Worker        %rval = load i32, i32* %pbobel
367*9880d681SAndroid Build Coastguard Worker        ret i32 %rval
368*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test21(
369*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0
370*9880d681SAndroid Build Coastguard Worker}
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker@A = global i32 1               ; <i32*> [#uses=1]
374*9880d681SAndroid Build Coastguard Worker@B = global i32 2               ; <i32*> [#uses=1]
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Workerdefine i1 @test22() {
377*9880d681SAndroid Build Coastguard Worker        %C = icmp ult i32* getelementptr (i32, i32* @A, i64 1),
378*9880d681SAndroid Build Coastguard Worker                           getelementptr (i32, i32* @B, i64 2)
379*9880d681SAndroid Build Coastguard Worker        ret i1 %C
380*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test22(
381*9880d681SAndroid Build Coastguard Worker; CHECK: icmp ult (i32* getelementptr inbounds (i32, i32* @A, i64 1), i32* getelementptr (i32, i32* @B, i64 2))
382*9880d681SAndroid Build Coastguard Worker}
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker%X = type { [10 x i32], float }
386*9880d681SAndroid Build Coastguard Worker
387*9880d681SAndroid Build Coastguard Workerdefine i1 @test23() {
388*9880d681SAndroid Build Coastguard Worker        %A = getelementptr %X, %X* null, i64 0, i32 0, i64 0                ; <i32*> [#uses=1]
389*9880d681SAndroid Build Coastguard Worker        %B = icmp ne i32* %A, null              ; <i1> [#uses=1]
390*9880d681SAndroid Build Coastguard Worker        ret i1 %B
391*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test23(
392*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 false
393*9880d681SAndroid Build Coastguard Worker}
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Workerdefine void @test25() {
396*9880d681SAndroid Build Coastguard Workerentry:
397*9880d681SAndroid Build Coastguard Worker        %tmp = getelementptr { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* null, i32 0, i32 3         ; <i64*> [#uses=1]
398*9880d681SAndroid Build Coastguard Worker        %tmp.upgrd.1 = load i64, i64* %tmp           ; <i64> [#uses=1]
399*9880d681SAndroid Build Coastguard Worker        %tmp8.ui = load i64, i64* null               ; <i64> [#uses=1]
400*9880d681SAndroid Build Coastguard Worker        %tmp8 = bitcast i64 %tmp8.ui to i64             ; <i64> [#uses=1]
401*9880d681SAndroid Build Coastguard Worker        %tmp9 = and i64 %tmp8, %tmp.upgrd.1             ; <i64> [#uses=1]
402*9880d681SAndroid Build Coastguard Worker        %sext = trunc i64 %tmp9 to i32          ; <i32> [#uses=1]
403*9880d681SAndroid Build Coastguard Worker        %tmp27.i = sext i32 %sext to i64                ; <i64> [#uses=1]
404*9880d681SAndroid Build Coastguard Worker        tail call void @foo25( i32 0, i64 %tmp27.i )
405*9880d681SAndroid Build Coastguard Worker        unreachable
406*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test25(
407*9880d681SAndroid Build Coastguard Worker}
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Workerdeclare void @foo25(i32, i64)
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker; PR1637
413*9880d681SAndroid Build Coastguard Workerdefine i1 @test26(i8* %arr) {
414*9880d681SAndroid Build Coastguard Worker        %X = getelementptr i8, i8* %arr, i32 1
415*9880d681SAndroid Build Coastguard Worker        %Y = getelementptr i8, i8* %arr, i32 1
416*9880d681SAndroid Build Coastguard Worker        %test = icmp uge i8* %X, %Y
417*9880d681SAndroid Build Coastguard Worker        ret i1 %test
418*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test26(
419*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 true
420*9880d681SAndroid Build Coastguard Worker}
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker	%struct.__large_struct = type { [100 x i64] }
423*9880d681SAndroid Build Coastguard Worker	%struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
424*9880d681SAndroid Build Coastguard Worker	%struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
425*9880d681SAndroid Build Coastguard Worker	%struct.sigval_t = type { i8* }
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard Workerdefine i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
428*9880d681SAndroid Build Coastguard Workerentry:
429*9880d681SAndroid Build Coastguard Worker	%from_addr = alloca %struct.siginfo_t*
430*9880d681SAndroid Build Coastguard Worker	%tmp344 = load %struct.siginfo_t*, %struct.siginfo_t** %from_addr, align 8
431*9880d681SAndroid Build Coastguard Worker	%tmp345 = getelementptr %struct.siginfo_t, %struct.siginfo_t* %tmp344, i32 0, i32 3
432*9880d681SAndroid Build Coastguard Worker	%tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
433*9880d681SAndroid Build Coastguard Worker	%tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
434*9880d681SAndroid Build Coastguard Worker	%tmp348 = getelementptr { i32, i32, %struct.sigval_t }, { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
435*9880d681SAndroid Build Coastguard Worker	%tmp349 = getelementptr %struct.sigval_t, %struct.sigval_t* %tmp348, i32 0, i32 0
436*9880d681SAndroid Build Coastguard Worker	%tmp349350 = bitcast i8** %tmp349 to i32*
437*9880d681SAndroid Build Coastguard Worker	%tmp351 = load i32, i32* %tmp349350, align 8
438*9880d681SAndroid Build Coastguard Worker	%tmp360 = call i32 asm sideeffect "...",
439*9880d681SAndroid Build Coastguard Worker        "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
440*9880d681SAndroid Build Coastguard Worker         %struct.__large_struct* null, i32 -14, i32 0 )
441*9880d681SAndroid Build Coastguard Worker	unreachable
442*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test27(
443*9880d681SAndroid Build Coastguard Worker}
444*9880d681SAndroid Build Coastguard Worker
445*9880d681SAndroid Build Coastguard Worker; PR1978
446*9880d681SAndroid Build Coastguard Worker	%struct.x = type <{ i8 }>
447*9880d681SAndroid Build Coastguard Worker@.str = internal constant [6 x i8] c"Main!\00"
448*9880d681SAndroid Build Coastguard Worker@.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Workerdefine i32 @test28() nounwind  {
451*9880d681SAndroid Build Coastguard Workerentry:
452*9880d681SAndroid Build Coastguard Worker	%orientations = alloca [1 x [1 x %struct.x]]
453*9880d681SAndroid Build Coastguard Worker	%tmp3 = call i32 @puts( i8* getelementptr ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) ) nounwind
454*9880d681SAndroid Build Coastguard Worker	%tmp45 = getelementptr inbounds [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
455*9880d681SAndroid Build Coastguard Worker	%orientations62 = getelementptr [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
456*9880d681SAndroid Build Coastguard Worker	br label %bb10
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Workerbb10:
459*9880d681SAndroid Build Coastguard Worker	%indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
460*9880d681SAndroid Build Coastguard Worker	%tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
461*9880d681SAndroid Build Coastguard Worker	%tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
462*9880d681SAndroid Build Coastguard Worker	%tmp12 = getelementptr inbounds %struct.x, %struct.x* %tmp45, i32 %tmp12.rec
463*9880d681SAndroid Build Coastguard Worker	%tmp16 = call i32 (i8*, ...) @printf( i8* getelementptr ([12 x i8], [12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
464*9880d681SAndroid Build Coastguard Worker	%tmp84 = icmp eq %struct.x* %tmp12, %orientations62
465*9880d681SAndroid Build Coastguard Worker	%indvar.next = add i32 %indvar, 1
466*9880d681SAndroid Build Coastguard Worker	br i1 %tmp84, label %bb17, label %bb10
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Workerbb17:
469*9880d681SAndroid Build Coastguard Worker	ret i32 0
470*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test28(
471*9880d681SAndroid Build Coastguard Worker; CHECK: icmp eq i32 %indvar, 0
472*9880d681SAndroid Build Coastguard Worker}
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Workerdeclare i32 @puts(i8*)
475*9880d681SAndroid Build Coastguard Worker
476*9880d681SAndroid Build Coastguard Workerdeclare i32 @printf(i8*, ...)
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker
481*9880d681SAndroid Build Coastguard Worker; rdar://6762290
482*9880d681SAndroid Build Coastguard Worker	%T = type <{ i64, i64, i64 }>
483*9880d681SAndroid Build Coastguard Workerdefine i32 @test29(i8* %start, i32 %X) nounwind {
484*9880d681SAndroid Build Coastguard Workerentry:
485*9880d681SAndroid Build Coastguard Worker	%tmp3 = load i64, i64* null
486*9880d681SAndroid Build Coastguard Worker	%add.ptr = getelementptr i8, i8* %start, i64 %tmp3
487*9880d681SAndroid Build Coastguard Worker	%tmp158 = load i32, i32* null
488*9880d681SAndroid Build Coastguard Worker	%add.ptr159 = getelementptr %T, %T* null, i32 %tmp158
489*9880d681SAndroid Build Coastguard Worker	%add.ptr209 = getelementptr i8, i8* %start, i64 0
490*9880d681SAndroid Build Coastguard Worker	%add.ptr212 = getelementptr i8, i8* %add.ptr209, i32 %X
491*9880d681SAndroid Build Coastguard Worker	%cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
492*9880d681SAndroid Build Coastguard Worker	br i1 %cmp214, label %if.then216, label %if.end363
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Workerif.then216:
495*9880d681SAndroid Build Coastguard Worker	ret i32 1
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Workerif.end363:
498*9880d681SAndroid Build Coastguard Worker	ret i32 0
499*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test29(
500*9880d681SAndroid Build Coastguard Worker}
501*9880d681SAndroid Build Coastguard Worker
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker; PR3694
504*9880d681SAndroid Build Coastguard Workerdefine i32 @test30(i32 %m, i32 %n) nounwind {
505*9880d681SAndroid Build Coastguard Workerentry:
506*9880d681SAndroid Build Coastguard Worker	%0 = alloca i32, i32 %n, align 4
507*9880d681SAndroid Build Coastguard Worker	%1 = bitcast i32* %0 to [0 x i32]*
508*9880d681SAndroid Build Coastguard Worker	call void @test30f(i32* %0) nounwind
509*9880d681SAndroid Build Coastguard Worker	%2 = getelementptr [0 x i32], [0 x i32]* %1, i32 0, i32 %m
510*9880d681SAndroid Build Coastguard Worker	%3 = load i32, i32* %2, align 4
511*9880d681SAndroid Build Coastguard Worker	ret i32 %3
512*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test30(
513*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr i32
514*9880d681SAndroid Build Coastguard Worker}
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard Workerdeclare void @test30f(i32*)
517*9880d681SAndroid Build Coastguard Worker
518*9880d681SAndroid Build Coastguard Worker
519*9880d681SAndroid Build Coastguard Worker
520*9880d681SAndroid Build Coastguard Workerdefine i1 @test31(i32* %A) {
521*9880d681SAndroid Build Coastguard Worker        %B = getelementptr i32, i32* %A, i32 1
522*9880d681SAndroid Build Coastguard Worker        %C = getelementptr i32, i32* %A, i64 1
523*9880d681SAndroid Build Coastguard Worker        %V = icmp eq i32* %B, %C
524*9880d681SAndroid Build Coastguard Worker        ret i1 %V
525*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test31(
526*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 true
527*9880d681SAndroid Build Coastguard Worker}
528*9880d681SAndroid Build Coastguard Worker
529*9880d681SAndroid Build Coastguard Worker
530*9880d681SAndroid Build Coastguard Worker; PR1345
531*9880d681SAndroid Build Coastguard Workerdefine i8* @test32(i8* %v) {
532*9880d681SAndroid Build Coastguard Worker	%A = alloca [4 x i8*], align 16
533*9880d681SAndroid Build Coastguard Worker	%B = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 0
534*9880d681SAndroid Build Coastguard Worker	store i8* null, i8** %B
535*9880d681SAndroid Build Coastguard Worker	%C = bitcast [4 x i8*]* %A to { [16 x i8] }*
536*9880d681SAndroid Build Coastguard Worker	%D = getelementptr { [16 x i8] }, { [16 x i8] }* %C, i32 0, i32 0, i32 8
537*9880d681SAndroid Build Coastguard Worker	%E = bitcast i8* %D to i8**
538*9880d681SAndroid Build Coastguard Worker	store i8* %v, i8** %E
539*9880d681SAndroid Build Coastguard Worker	%F = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 2
540*9880d681SAndroid Build Coastguard Worker	%G = load i8*, i8** %F
541*9880d681SAndroid Build Coastguard Worker	ret i8* %G
542*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test32(
543*9880d681SAndroid Build Coastguard Worker; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
544*9880d681SAndroid Build Coastguard Worker; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
545*9880d681SAndroid Build Coastguard Worker}
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker; PR3290
548*9880d681SAndroid Build Coastguard Worker%struct.Key = type { { i32, i32 } }
549*9880d681SAndroid Build Coastguard Worker%struct.anon = type <{ i8, [3 x i8], i32 }>
550*9880d681SAndroid Build Coastguard Worker
551*9880d681SAndroid Build Coastguard Workerdefine i32* @test33(%struct.Key* %A) {
552*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test33(
553*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
554*9880d681SAndroid Build Coastguard Worker  %B = bitcast %struct.Key* %A to %struct.anon*
555*9880d681SAndroid Build Coastguard Worker  %C = getelementptr %struct.anon, %struct.anon* %B, i32 0, i32 2
556*9880d681SAndroid Build Coastguard Worker  ret i32* %C
557*9880d681SAndroid Build Coastguard Worker}
558*9880d681SAndroid Build Coastguard Worker
559*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) {
560*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test33_as1(
561*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr %struct.Key, %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1
562*9880d681SAndroid Build Coastguard Worker  %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)*
563*9880d681SAndroid Build Coastguard Worker  %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
564*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %C
565*9880d681SAndroid Build Coastguard Worker}
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) {
568*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test33_array_as1(
569*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [10 x i32], [10 x i32] addrspace(1)* %A, i16 0, i16 2
570*9880d681SAndroid Build Coastguard Worker  %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)*
571*9880d681SAndroid Build Coastguard Worker  %C = getelementptr [5 x i32], [5 x i32] addrspace(1)* %B, i32 0, i32 2
572*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %C
573*9880d681SAndroid Build Coastguard Worker}
574*9880d681SAndroid Build Coastguard Worker
575*9880d681SAndroid Build Coastguard Worker; Make sure the GEP indices use the right pointer sized integer
576*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) {
577*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test33_array_struct_as1(
578*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [10 x %struct.Key], [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0
579*9880d681SAndroid Build Coastguard Worker  %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)*
580*9880d681SAndroid Build Coastguard Worker  %C = getelementptr [20 x i32], [20 x i32] addrspace(1)* %B, i32 0, i32 2
581*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %C
582*9880d681SAndroid Build Coastguard Worker}
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @test33_addrspacecast(%struct.Key* %A) {
585*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test33_addrspacecast(
586*9880d681SAndroid Build Coastguard Worker; CHECK: %C = getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
587*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: addrspacecast i32* %C to i32 addrspace(1)*
588*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret
589*9880d681SAndroid Build Coastguard Worker  %B = addrspacecast %struct.Key* %A to %struct.anon addrspace(1)*
590*9880d681SAndroid Build Coastguard Worker  %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
591*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %C
592*9880d681SAndroid Build Coastguard Worker}
593*9880d681SAndroid Build Coastguard Worker
594*9880d681SAndroid Build Coastguard Worker	%T2 = type { i8*, i8 }
595*9880d681SAndroid Build Coastguard Workerdefine i8* @test34(i8* %Val, i64 %V) nounwind {
596*9880d681SAndroid Build Coastguard Workerentry:
597*9880d681SAndroid Build Coastguard Worker	%A = alloca %T2, align 8
598*9880d681SAndroid Build Coastguard Worker	%mrv_gep = bitcast %T2* %A to i64*
599*9880d681SAndroid Build Coastguard Worker	%B = getelementptr %T2, %T2* %A, i64 0, i32 0
600*9880d681SAndroid Build Coastguard Worker
601*9880d681SAndroid Build Coastguard Worker      	store i64 %V, i64* %mrv_gep
602*9880d681SAndroid Build Coastguard Worker	%C = load i8*, i8** %B, align 8
603*9880d681SAndroid Build Coastguard Worker	ret i8* %C
604*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test34(
605*9880d681SAndroid Build Coastguard Worker; CHECK: %[[C:.*]] = inttoptr i64 %V to i8*
606*9880d681SAndroid Build Coastguard Worker; CHECK: ret i8* %[[C]]
607*9880d681SAndroid Build Coastguard Worker}
608*9880d681SAndroid Build Coastguard Worker
609*9880d681SAndroid Build Coastguard Worker%t0 = type { i8*, [19 x i8] }
610*9880d681SAndroid Build Coastguard Worker%t1 = type { i8*, [0 x i8] }
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker@array = external global [11 x i8]
613*9880d681SAndroid Build Coastguard Worker
614*9880d681SAndroid Build Coastguard Worker@s = external global %t0
615*9880d681SAndroid Build Coastguard Worker@"\01LC8" = external constant [17 x i8]
616*9880d681SAndroid Build Coastguard Worker
617*9880d681SAndroid Build Coastguard Worker; Instcombine should be able to fold this getelementptr.
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Workerdefine i32 @test35() nounwind {
620*9880d681SAndroid Build Coastguard Worker  call i32 (i8*, ...) @printf(i8* getelementptr ([17 x i8], [17 x i8]* @"\01LC8", i32 0, i32 0),
621*9880d681SAndroid Build Coastguard Worker             i8* getelementptr (%t1, %t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
622*9880d681SAndroid Build Coastguard Worker  ret i32 0
623*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test35(
624*9880d681SAndroid Build Coastguard Worker; CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0, %t0* @s, i64 0, i32 1, i64 0)) [[NUW:#[0-9]+]]
625*9880d681SAndroid Build Coastguard Worker}
626*9880d681SAndroid Build Coastguard Worker
627*9880d681SAndroid Build Coastguard Worker; Don't treat signed offsets as unsigned.
628*9880d681SAndroid Build Coastguard Workerdefine i8* @test36() nounwind {
629*9880d681SAndroid Build Coastguard Worker  ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i32 0, i64 -1)
630*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test36(
631*9880d681SAndroid Build Coastguard Worker; CHECK: ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i64 0, i64 -1)
632*9880d681SAndroid Build Coastguard Worker}
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
635*9880d681SAndroid Build Coastguard Worker@A37 = external constant [1 x i8]
636*9880d681SAndroid Build Coastguard Workerdefine i1 @test37() nounwind {
637*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test37(
638*9880d681SAndroid Build Coastguard Worker; CHECK: ret i1 true
639*9880d681SAndroid Build Coastguard Worker  %t = icmp eq i8* getelementptr ([1 x i8], [1 x i8]* @A37, i64 0, i64 1),
640*9880d681SAndroid Build Coastguard Worker                   getelementptr ([1 x i8], [1 x i8]* @A37, i64 1, i64 0)
641*9880d681SAndroid Build Coastguard Worker  ret i1 %t
642*9880d681SAndroid Build Coastguard Worker}
643*9880d681SAndroid Build Coastguard Worker
644*9880d681SAndroid Build Coastguard Worker; Test index promotion
645*9880d681SAndroid Build Coastguard Workerdefine i32* @test38(i32* %I, i32 %n) {
646*9880d681SAndroid Build Coastguard Worker        %A = getelementptr i32, i32* %I, i32 %n
647*9880d681SAndroid Build Coastguard Worker        ret i32* %A
648*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test38(
649*9880d681SAndroid Build Coastguard Worker; CHECK: = sext i32 %n to i64
650*9880d681SAndroid Build Coastguard Worker; CHECK: %A = getelementptr i32, i32* %I, i64 %
651*9880d681SAndroid Build Coastguard Worker}
652*9880d681SAndroid Build Coastguard Worker
653*9880d681SAndroid Build Coastguard Worker; Test that we don't duplicate work when the second gep is a "bitcast".
654*9880d681SAndroid Build Coastguard Worker%pr10322_t = type { i8* }
655*9880d681SAndroid Build Coastguard Workerdeclare void @pr10322_f2(%pr10322_t*)
656*9880d681SAndroid Build Coastguard Workerdeclare void @pr10322_f3(i8**)
657*9880d681SAndroid Build Coastguard Workerdefine void @pr10322_f1(%pr10322_t* %foo) {
658*9880d681SAndroid Build Coastguard Workerentry:
659*9880d681SAndroid Build Coastguard Worker  %arrayidx8 = getelementptr inbounds %pr10322_t, %pr10322_t* %foo, i64 2
660*9880d681SAndroid Build Coastguard Worker  call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
661*9880d681SAndroid Build Coastguard Worker  %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
662*9880d681SAndroid Build Coastguard Worker  call void @pr10322_f3(i8** %tmp2) nounwind
663*9880d681SAndroid Build Coastguard Worker  ret void
664*9880d681SAndroid Build Coastguard Worker
665*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @pr10322_f1(
666*9880d681SAndroid Build Coastguard Worker; CHECK: %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
667*9880d681SAndroid Build Coastguard Worker}
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker; Test that we combine the last two geps in this sequence, before we
670*9880d681SAndroid Build Coastguard Worker; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
671*9880d681SAndroid Build Coastguard Worker%three_gep_t = type {i32}
672*9880d681SAndroid Build Coastguard Worker%three_gep_t2 = type {%three_gep_t}
673*9880d681SAndroid Build Coastguard Worker
674*9880d681SAndroid Build Coastguard Workerdefine void @three_gep_f(%three_gep_t2* %x) {
675*9880d681SAndroid Build Coastguard Worker  %gep1 = getelementptr %three_gep_t2, %three_gep_t2* %x, i64 2
676*9880d681SAndroid Build Coastguard Worker  call void @three_gep_h(%three_gep_t2* %gep1)
677*9880d681SAndroid Build Coastguard Worker  %gep2 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0
678*9880d681SAndroid Build Coastguard Worker  %gep3 = getelementptr %three_gep_t, %three_gep_t* %gep2, i64 0, i32 0
679*9880d681SAndroid Build Coastguard Worker  call void @three_gep_g(i32* %gep3)
680*9880d681SAndroid Build Coastguard Worker
681*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @three_gep_f(
682*9880d681SAndroid Build Coastguard Worker; CHECK: %gep3 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0, i32 0
683*9880d681SAndroid Build Coastguard Worker  ret void
684*9880d681SAndroid Build Coastguard Worker}
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Workerdeclare void @three_gep_g(i32*)
687*9880d681SAndroid Build Coastguard Workerdeclare void @three_gep_h(%three_gep_t2*)
688*9880d681SAndroid Build Coastguard Worker
689*9880d681SAndroid Build Coastguard Worker%struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
690*9880d681SAndroid Build Coastguard Worker%struct.zot = type { i64, i8 }
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard Workerdefine void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
693*9880d681SAndroid Build Coastguard Worker  %tmp = getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
694*9880d681SAndroid Build Coastguard Worker  %tmp2 = load %struct.zot*, %struct.zot** %tmp, align 8
695*9880d681SAndroid Build Coastguard Worker  %tmp3 = bitcast %struct.zot* %tmp2 to i8*
696*9880d681SAndroid Build Coastguard Worker  %tmp4 = getelementptr inbounds i8, i8* %tmp3, i64 -8
697*9880d681SAndroid Build Coastguard Worker  store i8 %arg1, i8* %tmp4, align 8
698*9880d681SAndroid Build Coastguard Worker  ret void
699*9880d681SAndroid Build Coastguard Worker
700*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test39(
701*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
702*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr inbounds i8, i8* %{{.+}}, i64 -8
703*9880d681SAndroid Build Coastguard Worker}
704*9880d681SAndroid Build Coastguard Worker
705*9880d681SAndroid Build Coastguard Workerdefine i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
706*9880d681SAndroid Build Coastguard Worker  %c = getelementptr [1 x i8], [1 x i8]* %a, i32 0, i32 0
707*9880d681SAndroid Build Coastguard Worker  %d = getelementptr [1 x i8], [1 x i8]* %b, i32 0, i32 0
708*9880d681SAndroid Build Coastguard Worker  %cmp = icmp ult i8* %c, %d
709*9880d681SAndroid Build Coastguard Worker  ret i1 %cmp
710*9880d681SAndroid Build Coastguard Worker
711*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @pr16483(
712*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: icmp ult  [1 x i8]* %a, %b
713*9880d681SAndroid Build Coastguard Worker}
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Workerdefine i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) {
716*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_as1(
717*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr i32, i32 addrspace(1)* %arr, i16 %N
718*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast
719*9880d681SAndroid Build Coastguard Worker  %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)*
720*9880d681SAndroid Build Coastguard Worker  %V = mul i16 %N, 4
721*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
722*9880d681SAndroid Build Coastguard Worker  %x = load i8, i8 addrspace(1)* %t
723*9880d681SAndroid Build Coastguard Worker  ret i8 %x
724*9880d681SAndroid Build Coastguard Worker}
725*9880d681SAndroid Build Coastguard Worker
726*9880d681SAndroid Build Coastguard Worker; The element size of the array matches the element size of the pointer
727*9880d681SAndroid Build Coastguard Workerdefine i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
728*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
729*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
730*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast
731*9880d681SAndroid Build Coastguard Worker  %cast = bitcast [100 x double]* %arr to i64*
732*9880d681SAndroid Build Coastguard Worker  %V = mul i64 %N, 8
733*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i64, i64* %cast, i64 %V
734*9880d681SAndroid Build Coastguard Worker  %x = load i64, i64* %t
735*9880d681SAndroid Build Coastguard Worker  ret i64 %x
736*9880d681SAndroid Build Coastguard Worker}
737*9880d681SAndroid Build Coastguard Worker
738*9880d681SAndroid Build Coastguard Worker; gep should be done in the original address space.
739*9880d681SAndroid Build Coastguard Workerdefine i64 @test_gep_bitcast_array_same_size_element_addrspacecast([100 x double]* %arr, i64 %N) {
740*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_addrspacecast(
741*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
742*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: bitcast double*
743*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: %t = addrspacecast i64*
744*9880d681SAndroid Build Coastguard Worker; CHECK: load i64, i64 addrspace(3)* %t
745*9880d681SAndroid Build Coastguard Worker  %cast = addrspacecast [100 x double]* %arr to i64 addrspace(3)*
746*9880d681SAndroid Build Coastguard Worker  %V = mul i64 %N, 8
747*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i64, i64 addrspace(3)* %cast, i64 %V
748*9880d681SAndroid Build Coastguard Worker  %x = load i64, i64 addrspace(3)* %t
749*9880d681SAndroid Build Coastguard Worker  ret i64 %x
750*9880d681SAndroid Build Coastguard Worker}
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard Worker; The element size of the array is different the element size of the pointer
753*9880d681SAndroid Build Coastguard Workerdefine i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
754*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
755*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %N
756*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast
757*9880d681SAndroid Build Coastguard Worker  %cast = bitcast [100 x double]* %arr to i8*
758*9880d681SAndroid Build Coastguard Worker  %V = mul i64 %N, 8
759*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i8, i8* %cast, i64 %V
760*9880d681SAndroid Build Coastguard Worker  %x = load i8, i8* %t
761*9880d681SAndroid Build Coastguard Worker  ret i8 %x
762*9880d681SAndroid Build Coastguard Worker}
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Workerdefine i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
765*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
766*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %V
767*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast
768*9880d681SAndroid Build Coastguard Worker  %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
769*9880d681SAndroid Build Coastguard Worker  %V = mul i16 %N, 8
770*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i64, i64 addrspace(1)* %cast, i16 %V
771*9880d681SAndroid Build Coastguard Worker  %x = load i64, i64 addrspace(1)* %t
772*9880d681SAndroid Build Coastguard Worker  ret i64 %x
773*9880d681SAndroid Build Coastguard Worker}
774*9880d681SAndroid Build Coastguard Worker
775*9880d681SAndroid Build Coastguard Workerdefine i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
776*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
777*9880d681SAndroid Build Coastguard Worker; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %N
778*9880d681SAndroid Build Coastguard Worker; CHECK: bitcast
779*9880d681SAndroid Build Coastguard Worker  %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
780*9880d681SAndroid Build Coastguard Worker  %V = mul i16 %N, 8
781*9880d681SAndroid Build Coastguard Worker  %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
782*9880d681SAndroid Build Coastguard Worker  %x = load i8, i8 addrspace(1)* %t
783*9880d681SAndroid Build Coastguard Worker  ret i8 %x
784*9880d681SAndroid Build Coastguard Worker}
785*9880d681SAndroid Build Coastguard Worker
786*9880d681SAndroid Build Coastguard Workerdefine i64 @test40() {
787*9880d681SAndroid Build Coastguard Worker  %array = alloca [3 x i32], align 4
788*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds [3 x i32], [3 x i32]* %array, i64 0, i64 2
789*9880d681SAndroid Build Coastguard Worker  %gepi8 = bitcast i32* %gep to i8*
790*9880d681SAndroid Build Coastguard Worker  %p = ptrtoint [3 x i32]* %array to i64
791*9880d681SAndroid Build Coastguard Worker  %np = sub i64 0, %p
792*9880d681SAndroid Build Coastguard Worker  %gep2 = getelementptr i8, i8* %gepi8, i64 %np
793*9880d681SAndroid Build Coastguard Worker  %ret = ptrtoint i8* %gep2 to i64
794*9880d681SAndroid Build Coastguard Worker  ret i64 %ret
795*9880d681SAndroid Build Coastguard Worker
796*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test40
797*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i64 8
798*9880d681SAndroid Build Coastguard Worker}
799*9880d681SAndroid Build Coastguard Worker
800*9880d681SAndroid Build Coastguard Workerdefine i16 @test41([3 x i32] addrspace(1)* %array) {
801*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds [3 x i32], [3 x i32] addrspace(1)* %array, i16 0, i16 2
802*9880d681SAndroid Build Coastguard Worker  %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)*
803*9880d681SAndroid Build Coastguard Worker  %p = ptrtoint [3 x i32] addrspace(1)* %array to i16
804*9880d681SAndroid Build Coastguard Worker  %np = sub i16 0, %p
805*9880d681SAndroid Build Coastguard Worker  %gep2 = getelementptr i8, i8 addrspace(1)* %gepi8, i16 %np
806*9880d681SAndroid Build Coastguard Worker  %ret = ptrtoint i8 addrspace(1)* %gep2 to i16
807*9880d681SAndroid Build Coastguard Worker  ret i16 %ret
808*9880d681SAndroid Build Coastguard Worker
809*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test41(
810*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i16 8
811*9880d681SAndroid Build Coastguard Worker}
812*9880d681SAndroid Build Coastguard Worker
813*9880d681SAndroid Build Coastguard Workerdefine i8* @test42(i8* %c1, i8* %c2) {
814*9880d681SAndroid Build Coastguard Worker  %ptrtoint = ptrtoint i8* %c1 to i64
815*9880d681SAndroid Build Coastguard Worker  %sub = sub i64 0, %ptrtoint
816*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds i8, i8* %c2, i64 %sub
817*9880d681SAndroid Build Coastguard Worker  ret i8* %gep
818*9880d681SAndroid Build Coastguard Worker
819*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test42(
820*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT1:%.*]] = ptrtoint i8* %c1 to i64
821*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT2:%.*]] = ptrtoint i8* %c2 to i64
822*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
823*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i8*
824*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  ret i8* [[INTTOPTR]]
825*9880d681SAndroid Build Coastguard Worker}
826*9880d681SAndroid Build Coastguard Worker
827*9880d681SAndroid Build Coastguard Workerdefine i16* @test43(i16* %c1, i16* %c2) {
828*9880d681SAndroid Build Coastguard Worker  %ptrtoint = ptrtoint i16* %c1 to i64
829*9880d681SAndroid Build Coastguard Worker  %sub = sub i64 0, %ptrtoint
830*9880d681SAndroid Build Coastguard Worker  %shr = ashr i64 %sub, 1
831*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds i16, i16* %c2, i64 %shr
832*9880d681SAndroid Build Coastguard Worker  ret i16* %gep
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test43(
835*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT1:%.*]] = ptrtoint i16* %c1 to i64
836*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT2:%.*]] = ptrtoint i16* %c2 to i64
837*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
838*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i16*
839*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  ret i16* [[INTTOPTR]]
840*9880d681SAndroid Build Coastguard Worker}
841*9880d681SAndroid Build Coastguard Worker
842*9880d681SAndroid Build Coastguard Workerdefine %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) {
843*9880d681SAndroid Build Coastguard Worker  %ptrtoint = ptrtoint %struct.C* %c1 to i64
844*9880d681SAndroid Build Coastguard Worker  %sub = sub i64 0, %ptrtoint
845*9880d681SAndroid Build Coastguard Worker  %shr = sdiv i64 %sub, 7
846*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %shr
847*9880d681SAndroid Build Coastguard Worker  ret %struct.C* %gep
848*9880d681SAndroid Build Coastguard Worker
849*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test44(
850*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT1:%.*]] = ptrtoint %struct.C* %c1 to i64
851*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT2:%.*]] = ptrtoint %struct.C* %c2 to i64
852*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
853*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to %struct.C*
854*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  ret %struct.C* [[INTTOPTR]]
855*9880d681SAndroid Build Coastguard Worker}
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard Workerdefine %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
858*9880d681SAndroid Build Coastguard Worker  %ptrtoint1 = ptrtoint %struct.C* %c1 to i64
859*9880d681SAndroid Build Coastguard Worker  %ptrtoint2 = ptrtoint %struct.C** %c2 to i64
860*9880d681SAndroid Build Coastguard Worker  %sub = sub i64 %ptrtoint2, %ptrtoint1 ; C2 - C1
861*9880d681SAndroid Build Coastguard Worker  %shr = sdiv i64 %sub, 7
862*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds %struct.C, %struct.C* %c1, i64 %shr ; C1 + (C2 - C1)
863*9880d681SAndroid Build Coastguard Worker  ret %struct.C* %gep
864*9880d681SAndroid Build Coastguard Worker
865*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test45(
866*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[BITCAST:%.*]] = bitcast %struct.C** %c2 to %struct.C*
867*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  ret %struct.C* [[BITCAST]]
868*9880d681SAndroid Build Coastguard Worker}
869*9880d681SAndroid Build Coastguard Worker
870*9880d681SAndroid Build Coastguard Workerdefine %struct.C* @test46(%struct.C* %c1, %struct.C* %c2, i64 %N) {
871*9880d681SAndroid Build Coastguard Worker  %ptrtoint = ptrtoint %struct.C* %c1 to i64
872*9880d681SAndroid Build Coastguard Worker  %sub = sub i64 0, %ptrtoint
873*9880d681SAndroid Build Coastguard Worker  %sdiv = sdiv i64 %sub, %N
874*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
875*9880d681SAndroid Build Coastguard Worker  ret %struct.C* %gep
876*9880d681SAndroid Build Coastguard Worker
877*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test46(
878*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[PTRTOINT:%.*]] = ptrtoint %struct.C* %c1 to i64
879*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[SUB:%.*]] = sub i64 0, [[PTRTOINT]]
880*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[SDIV:%.*]] = sdiv i64 [[SUB]], %N
881*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  [[GEP:%.*]] = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
882*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT:  ret %struct.C* [[GEP]]
883*9880d681SAndroid Build Coastguard Worker}
884*9880d681SAndroid Build Coastguard Worker
885*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
886*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @ascast_0_gep(
887*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: getelementptr
888*9880d681SAndroid Build Coastguard Worker; CHECK: ret
889*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr i32, i32* %p, i32 0
890*9880d681SAndroid Build Coastguard Worker  %x = addrspacecast i32* %gep to i32 addrspace(1)*
891*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %x
892*9880d681SAndroid Build Coastguard Worker}
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker; Do not merge the GEP and the addrspacecast, because it would undo the
895*9880d681SAndroid Build Coastguard Worker; addrspacecast canonicalization.
896*9880d681SAndroid Build Coastguard Workerdefine i32 addrspace(1)* @ascast_0_0_gep([128 x i32]* %p) nounwind {
897*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @ascast_0_0_gep(
898*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: getelementptr [128 x i32]
899*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: addrspacecast i32*
900*9880d681SAndroid Build Coastguard Worker; CHECK-NEXT: ret i32 addrspace(1)*
901*9880d681SAndroid Build Coastguard Worker  %gep = getelementptr [128 x i32], [128 x i32]* %p, i32 0, i32 0
902*9880d681SAndroid Build Coastguard Worker  %x = addrspacecast i32* %gep to i32 addrspace(1)*
903*9880d681SAndroid Build Coastguard Worker  ret i32 addrspace(1)* %x
904*9880d681SAndroid Build Coastguard Worker}
905*9880d681SAndroid Build Coastguard Worker
906*9880d681SAndroid Build Coastguard Worker; CHECK: attributes [[NUW]] = { nounwind }
907