1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32"
5 %intstruct = type { i32 }
6 %pair = type { i32, i32 }
7 %struct.B = type { double }
8 %struct.A = type { %struct.B, i32, i32 }
11 @Global = constant [10 x i8] c"helloworld"
12 @Global_as1 = addrspace(1) constant [10 x i8] c"helloworld"
14 ; Test noop elimination
15 define i32* @test1(i32* %I) {
16 %A = getelementptr i32* %I, i64 0
18 ; CHECK-LABEL: @test1(
22 define i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) {
23 %A = getelementptr i32 addrspace(1)* %I, i64 0
24 ret i32 addrspace(1)* %A
25 ; CHECK-LABEL: @test1_as1(
26 ; CHECK: ret i32 addrspace(1)* %I
29 ; Test noop elimination
30 define i32* @test2(i32* %I) {
31 %A = getelementptr i32* %I
33 ; CHECK-LABEL: @test2(
37 ; Test that two array indexing geps fold
38 define i32* @test3(i32* %I) {
39 %A = getelementptr i32* %I, i64 17
40 %B = getelementptr i32* %A, i64 4
42 ; CHECK-LABEL: @test3(
43 ; CHECK: getelementptr i32* %I, i64 21
46 ; Test that two getelementptr insts fold
47 define i32* @test4({ i32 }* %I) {
48 %A = getelementptr { i32 }* %I, i64 1
49 %B = getelementptr { i32 }* %A, i64 0, i32 0
51 ; CHECK-LABEL: @test4(
52 ; CHECK: getelementptr { i32 }* %I, i64 1, i32 0
55 define void @test5(i8 %B) {
56 ; This should be turned into a constexpr instead of being an instruction
57 %A = getelementptr [10 x i8]* @Global, i64 0, i64 4
60 ; CHECK-LABEL: @test5(
61 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
64 define void @test5_as1(i8 %B) {
65 ; This should be turned into a constexpr instead of being an instruction
66 %A = getelementptr [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4
67 store i8 %B, i8 addrspace(1)* %A
69 ; CHECK-LABEL: @test5_as1(
70 ; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4)
73 %as1_ptr_struct = type { i32 addrspace(1)* }
74 %as2_ptr_struct = type { i32 addrspace(2)* }
76 @global_as2 = addrspace(2) global i32 zeroinitializer
77 @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
79 ; This should be turned into a constexpr instead of being an instruction
80 define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
81 ; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
82 ; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8
83 ; CHECK-NEXT: ret void
84 %A = getelementptr %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0
85 store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
89 @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
91 define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
92 ; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
93 ; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4
95 ; CHECK-NEXT: ret void
96 %A = getelementptr [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
97 store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
101 define i32* @test7(i32* %I, i64 %C, i64 %D) {
102 %A = getelementptr i32* %I, i64 %C
103 %B = getelementptr i32* %A, i64 %D
105 ; CHECK-LABEL: @test7(
106 ; CHECK: %A.sum = add i64 %C, %D
107 ; CHECK: getelementptr i32* %I, i64 %A.sum
110 define i8* @test8([10 x i32]* %X) {
111 ;; Fold into the cast.
112 %A = getelementptr [10 x i32]* %X, i64 0, i64 0
113 %B = bitcast i32* %A to i8*
115 ; CHECK-LABEL: @test8(
116 ; CHECK: bitcast [10 x i32]* %X to i8*
119 define i32 @test9() {
120 %A = getelementptr { i32, double }* null, i32 0, i32 1
121 %B = ptrtoint double* %A to i32
123 ; CHECK-LABEL: @test9(
127 define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
128 %tmp.1 = getelementptr { i32, i32 }* %x, i32 0, i32 1
129 %tmp.3 = getelementptr { i32, i32 }* %y, i32 0, i32 1
131 %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
133 ; CHECK-LABEL: @test10(
134 ; CHECK: icmp eq { i32, i32 }* %x, %y
137 define i1 @test11({ i32, i32 }* %X) {
138 %P = getelementptr { i32, i32 }* %X, i32 0, i32 0
139 %Q = icmp eq i32* %P, null
141 ; CHECK-LABEL: @test11(
142 ; CHECK: icmp eq { i32, i32 }* %X, null
147 define i32 @test12(%struct.A* %a) {
149 %g3 = getelementptr %struct.A* %a, i32 0, i32 1
150 store i32 10, i32* %g3, align 4
152 %g4 = getelementptr %struct.A* %a, i32 0, i32 0
154 %new_a = bitcast %struct.B* %g4 to %struct.A*
156 %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
157 %a_a = load i32* %g5, align 4
159 ; CHECK-LABEL: @test12(
160 ; CHECK: getelementptr %struct.A* %a, i64 0, i32 1
161 ; CHECK-NEXT: store i32 10, i32* %g3
162 ; CHECK-NEXT: ret i32 10
167 %S = type { i32, [ 100 x i32] }
168 define i1 @test13(i64 %X, %S* %P) {
169 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i64 %X
170 %B = getelementptr inbounds %S* %P, i32 0, i32 0
171 %C = icmp eq i32* %A, %B
173 ; CHECK-LABEL: @test13(
174 ; CHECK: %C = icmp eq i64 %X, -1
177 define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
178 ; CHECK-LABEL: @test13_vector(
179 ; CHECK-NEXT: shl nuw <2 x i64> %X, <i64 2, i64 2>
180 ; CHECK-NEXT: add <2 x i64> %A.idx, <i64 4, i64 4>
181 ; CHECK-NEXT: icmp eq <2 x i64> %A.offs, zeroinitializer
182 %A = getelementptr inbounds <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X
183 %B = getelementptr inbounds <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
184 %C = icmp eq <2 x i32*> %A, %B
188 define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
189 ; CHECK-LABEL: @test13_as1(
190 ; CHECK-NEXT: %C = icmp eq i16 %X, -1
191 ; CHECK-NEXT: ret i1 %C
192 %A = getelementptr inbounds %S addrspace(1)* %P, i16 0, i32 1, i16 %X
193 %B = getelementptr inbounds %S addrspace(1)* %P, i16 0, i32 0
194 %C = icmp eq i32 addrspace(1)* %A, %B
198 define <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) {
199 ; CHECK-LABEL: @test13_vector_as1(
200 ; CHECK-NEXT: shl nuw <2 x i16> %X, <i16 2, i16 2>
201 ; CHECK-NEXT: add <2 x i16> %A.idx, <i16 4, i16 4>
202 ; CHECK-NEXT: icmp eq <2 x i16> %A.offs, zeroinitializer
203 ; CHECK-NEXT: ret <2 x i1>
204 %A = getelementptr inbounds <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X
205 %B = getelementptr inbounds <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0>
206 %C = icmp eq <2 x i32 addrspace(1)*> %A, %B
210 define i1 @test13_i32(i32 %X, %S* %P) {
211 ; CHECK-LABEL: @test13_i32(
212 ; CHECK: %C = icmp eq i32 %X, -1
213 %A = getelementptr inbounds %S* %P, i32 0, i32 1, i32 %X
214 %B = getelementptr inbounds %S* %P, i32 0, i32 0
215 %C = icmp eq i32* %A, %B
219 define i1 @test13_i16(i16 %X, %S* %P) {
220 ; CHECK-LABEL: @test13_i16(
221 ; CHECK: %C = icmp eq i16 %X, -1
222 %A = getelementptr inbounds %S* %P, i16 0, i32 1, i16 %X
223 %B = getelementptr inbounds %S* %P, i16 0, i32 0
224 %C = icmp eq i32* %A, %B
228 define i1 @test13_i128(i128 %X, %S* %P) {
229 ; CHECK-LABEL: @test13_i128(
230 ; CHECK: %C = icmp eq i64 %1, -1
231 %A = getelementptr inbounds %S* %P, i128 0, i32 1, i128 %X
232 %B = getelementptr inbounds %S* %P, i128 0, i32 0
233 %C = icmp eq i32* %A, %B
238 @G = external global [3 x i8]
239 define i8* @test14(i32 %Idx) {
240 %idx = zext i32 %Idx to i64
241 %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
243 ; CHECK-LABEL: @test14(
244 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
248 ; Test folding of constantexpr geps into normal geps.
249 @Array = external global [40 x i32]
250 define i32 *@test15(i64 %X) {
251 %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
253 ; CHECK-LABEL: @test15(
254 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
258 define i32* @test16(i32* %X, i32 %Idx) {
259 %R = getelementptr i32* %X, i32 %Idx
261 ; CHECK-LABEL: @test16(
262 ; CHECK: sext i32 %Idx to i64
266 define i1 @test17(i16* %P, i32 %I, i32 %J) {
267 %X = getelementptr inbounds i16* %P, i32 %I
268 %Y = getelementptr inbounds i16* %P, i32 %J
269 %C = icmp ult i16* %X, %Y
271 ; CHECK-LABEL: @test17(
272 ; CHECK: %C = icmp slt i32 %I, %J
275 define i1 @test18(i16* %P, i32 %I) {
276 %X = getelementptr inbounds i16* %P, i32 %I
277 %C = icmp ult i16* %X, %P
279 ; CHECK-LABEL: @test18(
280 ; CHECK: %C = icmp slt i32 %I, 0
283 ; Larger than the pointer size for a non-zero address space
284 define i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) {
285 ; CHECK-LABEL: @test18_as1(
286 ; CHECK-NEXT: %1 = trunc i32 %I to i16
287 ; CHECK-NEXT: %C = icmp slt i16 %1, 0
288 ; CHECK-NEXT: ret i1 %C
289 %X = getelementptr inbounds i16 addrspace(1)* %P, i32 %I
290 %C = icmp ult i16 addrspace(1)* %X, %P
294 ; Smaller than the pointer size for a non-zero address space
295 define i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) {
296 ; CHECK-LABEL: @test18_as1_i32(
297 ; CHECK-NEXT: %1 = trunc i32 %I to i16
298 ; CHECK-NEXT: %C = icmp slt i16 %1, 0
299 ; CHECK-NEXT: ret i1 %C
300 %X = getelementptr inbounds i16 addrspace(1)* %P, i32 %I
301 %C = icmp ult i16 addrspace(1)* %X, %P
305 ; Smaller than pointer size
306 define i1 @test18_i16(i16* %P, i16 %I) {
307 ; CHECK-LABEL: @test18_i16(
308 ; CHECK: %C = icmp slt i16 %I, 0
309 %X = getelementptr inbounds i16* %P, i16 %I
310 %C = icmp ult i16* %X, %P
314 ; Same as pointer size
315 define i1 @test18_i64(i16* %P, i64 %I) {
316 ; CHECK-LABEL: @test18_i64(
317 ; CHECK: %C = icmp slt i64 %I, 0
318 %X = getelementptr inbounds i16* %P, i64 %I
319 %C = icmp ult i16* %X, %P
323 ; Larger than the pointer size
324 define i1 @test18_i128(i16* %P, i128 %I) {
325 ; CHECK-LABEL: @test18_i128(
326 ; CHECK: %C = icmp slt i64 %1, 0
327 %X = getelementptr inbounds i16* %P, i128 %I
328 %C = icmp ult i16* %X, %P
332 define i32 @test19(i32* %P, i32 %A, i32 %B) {
333 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
334 %tmp.9 = getelementptr inbounds i32* %P, i32 %B
335 %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
336 %tmp.11 = zext i1 %tmp.10 to i32
338 ; CHECK-LABEL: @test19(
339 ; CHECK: icmp eq i32 %A, %B
342 define i32 @test20(i32* %P, i32 %A, i32 %B) {
343 %tmp.4 = getelementptr inbounds i32* %P, i32 %A
344 %tmp.6 = icmp eq i32* %tmp.4, %P
345 %tmp.7 = zext i1 %tmp.6 to i32
347 ; CHECK-LABEL: @test20(
348 ; CHECK: icmp eq i32 %A, 0
351 define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
352 %tmp.4 = getelementptr inbounds i32 addrspace(1)* %P, i32 %A
353 %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
354 %tmp.7 = zext i1 %tmp.6 to i32
356 ; CHECK-LABEL: @test20_as1(
357 ; CHECK: icmp eq i16 %1, 0
361 define i32 @test21() {
362 %pbob1 = alloca %intstruct
363 %pbob2 = getelementptr %intstruct* %pbob1
364 %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0
365 %rval = load i32* %pbobel
367 ; CHECK-LABEL: @test21(
368 ; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
372 @A = global i32 1 ; <i32*> [#uses=1]
373 @B = global i32 2 ; <i32*> [#uses=1]
375 define i1 @test22() {
376 %C = icmp ult i32* getelementptr (i32* @A, i64 1),
377 getelementptr (i32* @B, i64 2)
379 ; CHECK-LABEL: @test22(
380 ; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
384 %X = type { [10 x i32], float }
386 define i1 @test23() {
387 %A = getelementptr %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1]
388 %B = icmp ne i32* %A, null ; <i1> [#uses=1]
390 ; CHECK-LABEL: @test23(
391 ; CHECK: ret i1 false
394 define void @test25() {
396 %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1]
397 %tmp.upgrd.1 = load i64* %tmp ; <i64> [#uses=1]
398 %tmp8.ui = load i64* null ; <i64> [#uses=1]
399 %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1]
400 %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1]
401 %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1]
402 %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1]
403 tail call void @foo25( i32 0, i64 %tmp27.i )
405 ; CHECK-LABEL: @test25(
408 declare void @foo25(i32, i64)
412 define i1 @test26(i8* %arr) {
413 %X = getelementptr i8* %arr, i32 1
414 %Y = getelementptr i8* %arr, i32 1
415 %test = icmp uge i8* %X, %Y
417 ; CHECK-LABEL: @test26(
421 %struct.__large_struct = type { [100 x i64] }
422 %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
423 %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
424 %struct.sigval_t = type { i8* }
426 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
428 %from_addr = alloca %struct.siginfo_t*
429 %tmp344 = load %struct.siginfo_t** %from_addr, align 8
430 %tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3
431 %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
432 %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
433 %tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
434 %tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0
435 %tmp349350 = bitcast i8** %tmp349 to i32*
436 %tmp351 = load i32* %tmp349350, align 8
437 %tmp360 = call i32 asm sideeffect "...",
438 "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
439 %struct.__large_struct* null, i32 -14, i32 0 )
441 ; CHECK-LABEL: @test27(
445 %struct.x = type <{ i8 }>
446 @.str = internal constant [6 x i8] c"Main!\00"
447 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
449 define i32 @test28() nounwind {
451 %orientations = alloca [1 x [1 x %struct.x]]
452 %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind
453 %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
454 %orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
458 %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
459 %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
460 %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
461 %tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec
462 %tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
463 %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
464 %indvar.next = add i32 %indvar, 1
465 br i1 %tmp84, label %bb17, label %bb10
469 ; CHECK-LABEL: @test28(
470 ; CHECK: icmp eq i32 %indvar, 0
473 declare i32 @puts(i8*)
475 declare i32 @printf(i8*, ...)
481 %T = type <{ i64, i64, i64 }>
482 define i32 @test29(i8* %start, i32 %X) nounwind {
484 %tmp3 = load i64* null
485 %add.ptr = getelementptr i8* %start, i64 %tmp3
486 %tmp158 = load i32* null
487 %add.ptr159 = getelementptr %T* null, i32 %tmp158
488 %add.ptr209 = getelementptr i8* %start, i64 0
489 %add.ptr212 = getelementptr i8* %add.ptr209, i32 %X
490 %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
491 br i1 %cmp214, label %if.then216, label %if.end363
498 ; CHECK-LABEL: @test29(
503 define i32 @test30(i32 %m, i32 %n) nounwind {
505 %0 = alloca i32, i32 %n, align 4
506 %1 = bitcast i32* %0 to [0 x i32]*
507 call void @test30f(i32* %0) nounwind
508 %2 = getelementptr [0 x i32]* %1, i32 0, i32 %m
509 %3 = load i32* %2, align 4
511 ; CHECK-LABEL: @test30(
512 ; CHECK: getelementptr i32
515 declare void @test30f(i32*)
519 define i1 @test31(i32* %A) {
520 %B = getelementptr i32* %A, i32 1
521 %C = getelementptr i32* %A, i64 1
522 %V = icmp eq i32* %B, %C
524 ; CHECK-LABEL: @test31(
530 define i8* @test32(i8* %v) {
531 %A = alloca [4 x i8*], align 16
532 %B = getelementptr [4 x i8*]* %A, i32 0, i32 0
533 store i8* null, i8** %B
534 %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
535 %D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8
536 %E = bitcast i8* %D to i8**
537 store i8* %v, i8** %E
538 %F = getelementptr [4 x i8*]* %A, i32 0, i32 2
541 ; CHECK-LABEL: @test32(
542 ; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
543 ; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
547 %struct.Key = type { { i32, i32 } }
548 %struct.anon = type <{ i8, [3 x i8], i32 }>
550 define i32* @test33(%struct.Key* %A) {
551 ; CHECK-LABEL: @test33(
552 ; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
553 %B = bitcast %struct.Key* %A to %struct.anon*
554 %C = getelementptr %struct.anon* %B, i32 0, i32 2
558 define i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) {
559 ; CHECK-LABEL: @test33_as1(
560 ; CHECK: getelementptr %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1
561 %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)*
562 %C = getelementptr %struct.anon addrspace(1)* %B, i32 0, i32 2
563 ret i32 addrspace(1)* %C
566 define i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) {
567 ; CHECK-LABEL: @test33_array_as1(
568 ; CHECK: getelementptr [10 x i32] addrspace(1)* %A, i16 0, i16 2
569 %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)*
570 %C = getelementptr [5 x i32] addrspace(1)* %B, i32 0, i32 2
571 ret i32 addrspace(1)* %C
574 ; Make sure the GEP indices use the right pointer sized integer
575 define i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) {
576 ; CHECK-LABEL: @test33_array_struct_as1(
577 ; CHECK: getelementptr [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0
578 %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)*
579 %C = getelementptr [20 x i32] addrspace(1)* %B, i32 0, i32 2
580 ret i32 addrspace(1)* %C
583 %T2 = type { i8*, i8 }
584 define i8* @test34(i8* %Val, i64 %V) nounwind {
586 %A = alloca %T2, align 8
587 %mrv_gep = bitcast %T2* %A to i64*
588 %B = getelementptr %T2* %A, i64 0, i32 0
590 store i64 %V, i64* %mrv_gep
591 %C = load i8** %B, align 8
593 ; CHECK-LABEL: @test34(
594 ; CHECK: %V.c = inttoptr i64 %V to i8*
595 ; CHECK: ret i8* %V.c
598 %t0 = type { i8*, [19 x i8] }
599 %t1 = type { i8*, [0 x i8] }
601 @array = external global [11 x i8]
603 @s = external global %t0
604 @"\01LC8" = external constant [17 x i8]
606 ; Instcombine should be able to fold this getelementptr.
608 define i32 @test35() nounwind {
609 call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0),
610 i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
612 ; CHECK-LABEL: @test35(
613 ; 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)) [[NUW:#[0-9]+]]
616 ; Instcombine should constant-fold the GEP so that indices that have
617 ; static array extents are within bounds of those array extents.
618 ; In the below, -1 is not in the range [0,11). After the transformation,
619 ; the same address is computed, but 3 is in the range of [0,11).
621 define i8* @test36() nounwind {
622 ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
623 ; CHECK-LABEL: @test36(
624 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
627 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
628 @A37 = external constant [1 x i8]
629 define i1 @test37() nounwind {
630 ; CHECK-LABEL: @test37(
632 %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1),
633 getelementptr ([1 x i8]* @A37, i64 1, i64 0)
637 ; Test index promotion
638 define i32* @test38(i32* %I, i32 %n) {
639 %A = getelementptr i32* %I, i32 %n
641 ; CHECK-LABEL: @test38(
642 ; CHECK: = sext i32 %n to i64
643 ; CHECK: %A = getelementptr i32* %I, i64 %
646 ; Test that we don't duplicate work when the second gep is a "bitcast".
647 %pr10322_t = type { i8* }
648 declare void @pr10322_f2(%pr10322_t*)
649 declare void @pr10322_f3(i8**)
650 define void @pr10322_f1(%pr10322_t* %foo) {
652 %arrayidx8 = getelementptr inbounds %pr10322_t* %foo, i64 2
653 call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
654 %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
655 call void @pr10322_f3(i8** %tmp2) nounwind
658 ; CHECK-LABEL: @pr10322_f1(
659 ; CHECK: %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
662 ; Test that we combine the last two geps in this sequence, before we
663 ; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
664 %three_gep_t = type {i32}
665 %three_gep_t2 = type {%three_gep_t}
667 define void @three_gep_f(%three_gep_t2* %x) {
668 %gep1 = getelementptr %three_gep_t2* %x, i64 2
669 call void @three_gep_h(%three_gep_t2* %gep1)
670 %gep2 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0
671 %gep3 = getelementptr %three_gep_t* %gep2, i64 0, i32 0
672 call void @three_gep_g(i32* %gep3)
674 ; CHECK-LABEL: @three_gep_f(
675 ; CHECK: %gep3 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0, i32 0
679 declare void @three_gep_g(i32*)
680 declare void @three_gep_h(%three_gep_t2*)
682 %struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
683 %struct.zot = type { i64, i8 }
685 define void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
686 %tmp = getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
687 %tmp2 = load %struct.zot** %tmp, align 8
688 %tmp3 = bitcast %struct.zot* %tmp2 to i8*
689 %tmp4 = getelementptr inbounds i8* %tmp3, i64 -8
690 store i8 %arg1, i8* %tmp4, align 8
693 ; CHECK-LABEL: @test39(
694 ; CHECK: getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
695 ; CHECK: getelementptr inbounds i8* %tmp3, i64 -8
698 define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
699 %c = getelementptr [1 x i8]* %a, i32 0, i32 0
700 %d = getelementptr [1 x i8]* %b, i32 0, i32 0
701 %cmp = icmp ult i8* %c, %d
704 ; CHECK-LABEL: @pr16483(
705 ; CHECK-NEXT: icmp ult [1 x i8]* %a, %b
708 define i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) {
709 ; CHECK-LABEL: @test_gep_bitcast_as1(
710 ; CHECK: getelementptr i32 addrspace(1)* %arr, i16 %N
712 %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)*
714 %t = getelementptr i8 addrspace(1)* %cast, i16 %V
715 %x = load i8 addrspace(1)* %t
719 ; The element size of the array matches the element size of the pointer
720 define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
721 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
722 ; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %V
724 %cast = bitcast [100 x double]* %arr to i64*
726 %t = getelementptr i64* %cast, i64 %V
731 ; The element size of the array is different the element size of the pointer
732 define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
733 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
734 ; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %N
736 %cast = bitcast [100 x double]* %arr to i8*
738 %t = getelementptr i8* %cast, i64 %V
743 define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
744 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
745 ; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %V
747 %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
749 %t = getelementptr i64 addrspace(1)* %cast, i16 %V
750 %x = load i64 addrspace(1)* %t
754 define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
755 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
756 ; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %N
758 %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
760 %t = getelementptr i8 addrspace(1)* %cast, i16 %V
761 %x = load i8 addrspace(1)* %t
765 ; CHECK: attributes [[NUW]] = { nounwind }