1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 target datalayout = "e-p:64:64"
4 %intstruct = type { i32 }
5 %pair = type { i32, i32 }
6 %struct.B = type { double }
7 %struct.A = type { %struct.B, i32, i32 }
10 @Global = constant [10 x i8] c"helloworld"
12 ; Test noop elimination
13 define i32* @test1(i32* %I) {
14 %A = getelementptr i32* %I, i64 0
20 ; Test noop elimination
21 define i32* @test2(i32* %I) {
22 %A = getelementptr i32* %I
28 ; Test that two array indexing geps fold
29 define i32* @test3(i32* %I) {
30 %A = getelementptr i32* %I, i64 17
31 %B = getelementptr i32* %A, i64 4
34 ; CHECK: getelementptr i32* %I, i64 21
37 ; Test that two getelementptr insts fold
38 define i32* @test4({ i32 }* %I) {
39 %A = getelementptr { i32 }* %I, i64 1
40 %B = getelementptr { i32 }* %A, i64 0, i32 0
43 ; CHECK: getelementptr { i32 }* %I, i64 1, i32 0
46 define void @test5(i8 %B) {
47 ; This should be turned into a constexpr instead of being an instruction
48 %A = getelementptr [10 x i8]* @Global, i64 0, i64 4
52 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
56 define i32* @test7(i32* %I, i64 %C, i64 %D) {
57 %A = getelementptr i32* %I, i64 %C
58 %B = getelementptr i32* %A, i64 %D
61 ; CHECK: %A.sum = add i64 %C, %D
62 ; CHECK: getelementptr i32* %I, i64 %A.sum
65 define i8* @test8([10 x i32]* %X) {
66 ;; Fold into the cast.
67 %A = getelementptr [10 x i32]* %X, i64 0, i64 0
68 %B = bitcast i32* %A to i8*
71 ; CHECK: bitcast [10 x i32]* %X to i8*
75 %A = getelementptr { i32, double }* null, i32 0, i32 1
76 %B = ptrtoint double* %A to i32
82 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
83 %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1
84 %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1
86 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
89 ; CHECK: icmp eq { i32, i32 }* %x, %y
92 define i1 @test11({ i32, i32 }* %X) {
93 %P = getelementptr { i32, i32 }* %X, i32 0, i32 0
94 %Q = icmp eq i32* %P, null
97 ; CHECK: icmp eq { i32, i32 }* %X, null
102 define i32 @test12(%struct.A* %a) {
104 %g3 = getelementptr %struct.A* %a, i32 0, i32 1
105 store i32 10, i32* %g3, align 4
107 %g4 = getelementptr %struct.A* %a, i32 0, i32 0
109 %new_a = bitcast %struct.B* %g4 to %struct.A*
111 %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
112 %a_a = load i32* %g5, align 4
115 ; CHECK: getelementptr %struct.A* %a, i64 0, i32 1
116 ; CHECK-NEXT: store i32 10, i32* %g3
117 ; CHECK-NEXT: ret i32 10
122 %S = type { i32, [ 100 x i32] }
123 define i1 @test13(i64 %X, %S* %P) {
124 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X
125 %B = getelementptr inbounds %S* %P, i32 0, i32 0
126 %C = icmp eq i32* %A, %B
129 ; CHECK: %C = icmp eq i64 %X, -1
133 @G = external global [3 x i8]
134 define i8* @test14(i32 %Idx) {
135 %idx = zext i32 %Idx to i64
136 %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
139 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
143 ; Test folding of constantexpr geps into normal geps.
144 @Array = external global [40 x i32]
145 define i32 *@test15(i64 %X) {
146 %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
149 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
153 define i32* @test16(i32* %X, i32 %Idx) {
154 %R = getelementptr i32* %X, i32 %Idx
157 ; CHECK: sext i32 %Idx to i64
161 define i1 @test17(i16* %P, i32 %I, i32 %J) {
162 %X = getelementptr inbounds i16* %P, i32 %I
163 %Y = getelementptr inbounds i16* %P, i32 %J
164 %C = icmp ult i16* %X, %Y
167 ; CHECK: %C = icmp slt i32 %I, %J
170 define i1 @test18(i16* %P, i32 %I) {
171 %X = getelementptr inbounds i16* %P, i32 %I
172 %C = icmp ult i16* %X, %P
175 ; CHECK: %C = icmp slt i32 %I, 0
178 define i32 @test19(i32* %P, i32 %A, i32 %B) {
179 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
180 %tmp.9 = getelementptr inbounds i32* %P, i32 %B
181 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
182 %tmp.11 = zext i1 %tmp.10 to i32
185 ; CHECK: icmp eq i32 %A, %B
188 define i32 @test20(i32* %P, i32 %A, i32 %B) {
189 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
190 %tmp.6 = icmp eq i32* %tmp.4, %P
191 %tmp.7 = zext i1 %tmp.6 to i32
194 ; CHECK: icmp eq i32 %A, 0
198 define i32 @test21() {
199 %pbob1 = alloca %intstruct
200 %pbob2 = getelementptr %intstruct* %pbob1
201 %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0
202 %rval = load i32* %pbobel
205 ; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
209 @A = global i32 1 ; <i32*> [#uses=1]
210 @B = global i32 2 ; <i32*> [#uses=1]
212 define i1 @test22() {
213 %C = icmp ult i32* getelementptr (i32* @A, i64 1),
214 getelementptr (i32* @B, i64 2)
217 ; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
221 %X = type { [10 x i32], float }
223 define i1 @test23() {
224 %A = getelementptr %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1]
225 %B = icmp ne i32* %A, null ; <i1> [#uses=1]
228 ; CHECK: ret i1 false
231 define void @test25() {
233 %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1]
234 %tmp.upgrd.1 = load i64* %tmp ; <i64> [#uses=1]
235 %tmp8.ui = load i64* null ; <i64> [#uses=1]
236 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1]
237 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1]
238 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1]
239 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1]
240 tail call void @foo25( i32 0, i64 %tmp27.i )
245 declare void @foo25(i32, i64)
249 define i1 @test26(i8* %arr) {
250 %X = getelementptr i8* %arr, i32 1
251 %Y = getelementptr i8* %arr, i32 1
252 %test = icmp uge i8* %X, %Y
258 %struct.__large_struct = type { [100 x i64] }
259 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
260 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
261 %struct.sigval_t = type { i8* }
263 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
265 %from_addr = alloca %struct.siginfo_t*
266 %tmp344 = load %struct.siginfo_t** %from_addr, align 8
267 %tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3
268 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
269 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
270 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
271 %tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0
272 %tmp349350 = bitcast i8** %tmp349 to i32*
273 %tmp351 = load i32* %tmp349350, align 8
274 %tmp360 = call i32 asm sideeffect "...",
275 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
276 %struct.__large_struct* null, i32 -14, i32 0 )
282 %struct.x = type <{ i8 }>
283 @.str = internal constant [6 x i8] c"Main!\00"
284 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
286 define i32 @test28() nounwind {
288 %orientations = alloca [1 x [1 x %struct.x]]
289 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind
290 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
291 %orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
295 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
296 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
297 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
298 %tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec
299 %tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
300 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
301 %indvar.next = add i32 %indvar, 1
302 br i1 %tmp84, label %bb17, label %bb10
307 ; CHECK: icmp eq i32 %indvar, 0
310 declare i32 @puts(i8*)
312 declare i32 @printf(i8*, ...)
318 %T = type <{ i64, i64, i64 }>
319 define i32 @test29(i8* %start, i32 %X) nounwind {
321 %tmp3 = load i64* null
322 %add.ptr = getelementptr i8* %start, i64 %tmp3
323 %tmp158 = load i32* null
324 %add.ptr159 = getelementptr %T* null, i32 %tmp158
325 %add.ptr209 = getelementptr i8* %start, i64 0
326 %add.ptr212 = getelementptr i8* %add.ptr209, i32 %X
327 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
328 br i1 %cmp214, label %if.then216, label %if.end363
340 define i32 @test30(i32 %m, i32 %n) nounwind {
342 %0 = alloca i32, i32 %n, align 4
343 %1 = bitcast i32* %0 to [0 x i32]*
344 call void @test30f(i32* %0) nounwind
345 %2 = getelementptr [0 x i32]* %1, i32 0, i32 %m
346 %3 = load i32* %2, align 4
349 ; CHECK: getelementptr i32
352 declare void @test30f(i32*)
356 define i1 @test31(i32* %A) {
357 %B = getelementptr i32* %A, i32 1
358 %C = getelementptr i32* %A, i64 1
359 %V = icmp eq i32* %B, %C
367 define i8* @test32(i8* %v) {
368 %A = alloca [4 x i8*], align 16
369 %B = getelementptr [4 x i8*]* %A, i32 0, i32 0
370 store i8* null, i8** %B
371 %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
372 %D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8
373 %E = bitcast i8* %D to i8**
374 store i8* %v, i8** %E
375 %F = getelementptr [4 x i8*]* %A, i32 0, i32 2
379 ; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
380 ; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
384 %struct.Key = type { { i32, i32 } }
385 %struct.anon = type <{ i8, [3 x i8], i32 }>
387 define i32 *@test33(%struct.Key *%A) {
388 %B = bitcast %struct.Key* %A to %struct.anon*
389 %C = getelementptr %struct.anon* %B, i32 0, i32 2
392 ; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
397 %T2 = type { i8*, i8 }
398 define i8* @test34(i8* %Val, i64 %V) nounwind {
400 %A = alloca %T2, align 8
401 %mrv_gep = bitcast %T2* %A to i64*
402 %B = getelementptr %T2* %A, i64 0, i32 0
404 store i64 %V, i64* %mrv_gep
405 %C = load i8** %B, align 8
408 ; CHECK: %V.c = inttoptr i64 %V to i8*
409 ; CHECK: ret i8* %V.c
412 %t0 = type { i8*, [19 x i8] }
413 %t1 = type { i8*, [0 x i8] }
415 @array = external global [11 x i8]
417 @s = external global %t0
418 @"\01LC8" = external constant [17 x i8]
420 ; Instcombine should be able to fold this getelementptr.
422 define i32 @test35() nounwind {
423 call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0),
424 i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
427 ; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0* @s, i64 0, i32 1, i64 0)) nounwind
430 ; Instcombine should constant-fold the GEP so that indices that have
431 ; static array extents are within bounds of those array extents.
432 ; In the below, -1 is not in the range [0,11). After the transformation,
433 ; the same address is computed, but 3 is in the range of [0,11).
435 define i8* @test36() nounwind {
436 ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
438 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
441 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
442 @A37 = external constant [1 x i8]
443 define i1 @test37() nounwind {
446 %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1),
447 getelementptr ([1 x i8]* @A37, i64 1, i64 0)
451 ; Test index promotion
452 define i32* @test38(i32* %I, i32 %n) {
453 %A = getelementptr i32* %I, i32 %n
456 ; CHECK: = sext i32 %n to i64
457 ; CHECK: %A = getelementptr i32* %I, i64 %