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 %intstruct* %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)
55 define i32* @test6() {
57 %A = getelementptr [4 x i32]* %M, i64 0, i64 0
58 %B = getelementptr i32* %A, i64 2
61 ; CHECK: getelementptr i8* %malloccall, i64 8
64 define i32* @test7(i32* %I, i64 %C, i64 %D) {
65 %A = getelementptr i32* %I, i64 %C
66 %B = getelementptr i32* %A, i64 %D
69 ; CHECK: %A.sum = add i64 %C, %D
70 ; CHECK: getelementptr i32* %I, i64 %A.sum
73 define i8* @test8([10 x i32]* %X) {
74 ;; Fold into the cast.
75 %A = getelementptr [10 x i32]* %X, i64 0, i64 0
76 %B = bitcast i32* %A to i8*
79 ; CHECK: bitcast [10 x i32]* %X to i8*
83 %A = getelementptr { i32, double }* null, i32 0, i32 1
84 %B = ptrtoint double* %A to i32
90 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
91 %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1
92 %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1
94 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
97 ; CHECK: icmp eq %pair* %x, %y
100 define i1 @test11({ i32, i32 }* %X) {
101 %P = getelementptr { i32, i32 }* %X, i32 0, i32 0
102 %Q = icmp eq i32* %P, null
105 ; CHECK: icmp eq %pair* %X, null
110 define i32 @test12(%struct.A* %a) {
112 %g3 = getelementptr %struct.A* %a, i32 0, i32 1
113 store i32 10, i32* %g3, align 4
115 %g4 = getelementptr %struct.A* %a, i32 0, i32 0
117 %new_a = bitcast %struct.B* %g4 to %struct.A*
119 %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
120 %a_a = load i32* %g5, align 4
123 ; CHECK: getelementptr %struct.A* %a, i64 0, i32 1
124 ; CHECK-NEXT: store i32 10, i32* %g3
125 ; CHECK-NEXT: ret i32 10
130 %S = type { i32, [ 100 x i32] }
131 define i1 @test13(i64 %X, %S* %P) {
132 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X
133 %B = getelementptr inbounds %S* %P, i32 0, i32 0
134 %C = icmp eq i32* %A, %B
137 ; CHECK: %C = icmp eq i64 %X, -1
141 @G = external global [3 x i8]
142 define i8* @test14(i32 %Idx) {
143 %idx = zext i32 %Idx to i64
144 %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
147 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
151 ; Test folding of constantexpr geps into normal geps.
152 @Array = external global [40 x i32]
153 define i32 *@test15(i64 %X) {
154 %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
157 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
161 define i32* @test16(i32* %X, i32 %Idx) {
162 %R = getelementptr i32* %X, i32 %Idx
165 ; CHECK: sext i32 %Idx to i64
169 define i1 @test17(i16* %P, i32 %I, i32 %J) {
170 %X = getelementptr inbounds i16* %P, i32 %I
171 %Y = getelementptr inbounds i16* %P, i32 %J
172 %C = icmp ult i16* %X, %Y
175 ; CHECK: %C = icmp slt i32 %I, %J
178 define i1 @test18(i16* %P, i32 %I) {
179 %X = getelementptr inbounds i16* %P, i32 %I
180 %C = icmp ult i16* %X, %P
183 ; CHECK: %C = icmp slt i32 %I, 0
186 define i32 @test19(i32* %P, i32 %A, i32 %B) {
187 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
188 %tmp.9 = getelementptr inbounds i32* %P, i32 %B
189 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
190 %tmp.11 = zext i1 %tmp.10 to i32
193 ; CHECK: icmp eq i32 %A, %B
196 define i32 @test20(i32* %P, i32 %A, i32 %B) {
197 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
198 %tmp.6 = icmp eq i32* %tmp.4, %P
199 %tmp.7 = zext i1 %tmp.6 to i32
202 ; CHECK: icmp eq i32 %A, 0
206 define i32 @test21() {
207 %pbob1 = alloca %intstruct
208 %pbob2 = getelementptr %intstruct* %pbob1
209 %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0
210 %rval = load i32* %pbobel
213 ; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
217 @A = global i32 1 ; <i32*> [#uses=1]
218 @B = global i32 2 ; <i32*> [#uses=1]
220 define i1 @test22() {
221 %C = icmp ult i32* getelementptr (i32* @A, i64 1),
222 getelementptr (i32* @B, i64 2)
225 ; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
229 %X = type { [10 x i32], float }
231 define i1 @test23() {
232 %A = getelementptr %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1]
233 %B = icmp ne i32* %A, null ; <i1> [#uses=1]
236 ; CHECK: ret i1 false
239 %"java/lang/Object" = type { %struct.llvm_java_object_base }
240 %"java/lang/StringBuffer" = type { %"java/lang/Object", i32, { %"java/lang/Object", i32, [0 x i16] }*, i1 }
241 %struct.llvm_java_object_base = type opaque
243 define void @test24() {
245 %tmp53 = getelementptr %"java/lang/StringBuffer"* null, i32 0, i32 1 ; <i32*> [#uses=1]
246 store i32 0, i32* %tmp53
249 ; CHECK: store i32 0, i32* getelementptr (%"java/lang/StringBuffer"* null, i64 0, i32 1)
252 define void @test25() {
254 %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1]
255 %tmp.upgrd.1 = load i64* %tmp ; <i64> [#uses=1]
256 %tmp8.ui = load i64* null ; <i64> [#uses=1]
257 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1]
258 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1]
259 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1]
260 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1]
261 tail call void @foo25( i32 0, i64 %tmp27.i )
266 declare void @foo25(i32, i64)
270 define i1 @test26(i8* %arr) {
271 %X = getelementptr i8* %arr, i32 1
272 %Y = getelementptr i8* %arr, i32 1
273 %test = icmp uge i8* %X, %Y
279 %struct.__large_struct = type { [100 x i64] }
280 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
281 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
282 %struct.sigval_t = type { i8* }
284 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
286 %from_addr = alloca %struct.siginfo_t*
287 %tmp344 = load %struct.siginfo_t** %from_addr, align 8
288 %tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3
289 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
290 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
291 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
292 %tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0
293 %tmp349350 = bitcast i8** %tmp349 to i32*
294 %tmp351 = load i32* %tmp349350, align 8
295 %tmp360 = call i32 asm sideeffect "...",
296 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
297 %struct.__large_struct* null, i32 -14, i32 0 )
303 %struct.x = type <{ i8 }>
304 @.str = internal constant [6 x i8] c"Main!\00"
305 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
307 define i32 @test28() nounwind {
309 %orientations = alloca [1 x [1 x %struct.x]]
310 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind
311 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
312 %orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
316 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
317 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
318 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
319 %tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec
320 %tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
321 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
322 %indvar.next = add i32 %indvar, 1
323 br i1 %tmp84, label %bb17, label %bb10
328 ; CHECK: icmp eq i32 %indvar, 0
331 declare i32 @puts(i8*)
333 declare i32 @printf(i8*, ...)
339 %T = type <{ i64, i64, i64 }>
340 define i32 @test29(i8* %start, i32 %X) nounwind {
342 %tmp3 = load i64* null
343 %add.ptr = getelementptr i8* %start, i64 %tmp3
344 %tmp158 = load i32* null
345 %add.ptr159 = getelementptr %T* null, i32 %tmp158
346 %add.ptr209 = getelementptr i8* %start, i64 0
347 %add.ptr212 = getelementptr i8* %add.ptr209, i32 %X
348 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
349 br i1 %cmp214, label %if.then216, label %if.end363
361 define i32 @test30(i32 %m, i32 %n) nounwind {
363 %0 = alloca i32, i32 %n, align 4
364 %1 = bitcast i32* %0 to [0 x i32]*
365 call void @test30f(i32* %0) nounwind
366 %2 = getelementptr [0 x i32]* %1, i32 0, i32 %m
367 %3 = load i32* %2, align 4
370 ; CHECK: getelementptr i32
373 declare void @test30f(i32*)
377 define i1 @test31(i32* %A) {
378 %B = getelementptr i32* %A, i32 1
379 %C = getelementptr i32* %A, i64 1
380 %V = icmp eq i32* %B, %C
388 define i8* @test32(i8* %v) {
389 %A = alloca [4 x i8*], align 16
390 %B = getelementptr [4 x i8*]* %A, i32 0, i32 0
391 store i8* null, i8** %B
392 %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
393 %D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8
394 %E = bitcast i8* %D to i8**
395 store i8* %v, i8** %E
396 %F = getelementptr [4 x i8*]* %A, i32 0, i32 2
400 ; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
401 ; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
405 %struct.Key = type { { i32, i32 } }
406 %struct.anon = type <{ i8, [3 x i8], i32 }>
408 define i32 *@test33(%struct.Key *%A) {
409 %B = bitcast %struct.Key* %A to %struct.anon*
410 %C = getelementptr %struct.anon* %B, i32 0, i32 2
413 ; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
418 %T2 = type { i8*, i8 }
419 define i8* @test34(i8* %Val, i64 %V) nounwind {
421 %A = alloca %T2, align 8
422 %mrv_gep = bitcast %T2* %A to i64*
423 %B = getelementptr %T2* %A, i64 0, i32 0
425 store i64 %V, i64* %mrv_gep
426 %C = load i8** %B, align 8
429 ; CHECK: %V.c = inttoptr i64 %V to i8*
430 ; CHECK: ret i8* %V.c
433 %t0 = type { i8*, [19 x i8] }
434 %t1 = type { i8*, [0 x i8] }
436 @array = external global [11 x i8]
438 @s = external global %t0
439 @"\01LC8" = external constant [17 x i8]
441 ; Instcombine should be able to fold this getelementptr.
443 define i32 @test35() nounwind {
444 call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0),
445 i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
448 ; 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
451 ; Instcombine should constant-fold the GEP so that indices that have
452 ; static array extents are within bounds of those array extents.
453 ; In the below, -1 is not in the range [0,11). After the transformation,
454 ; the same address is computed, but 3 is in the range of [0,11).
456 define i8* @test36() nounwind {
457 ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
459 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
462 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
463 @A37 = external constant [1 x i8]
464 define i1 @test37() nounwind {
467 %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1),
468 getelementptr ([1 x i8]* @A37, i64 1, i64 0)
472 ; Test index promotion
473 define i32* @test38(i32* %I, i32 %n) {
474 %A = getelementptr i32* %I, i32 %n
477 ; CHECK: = sext i32 %n to i64
478 ; CHECK: %A = getelementptr i32* %I, i64 %