Add test for bitcast array ptrs with address spaces
[oota-llvm.git] / test / Transforms / InstCombine / getelementptr.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
3 target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32"
4
5 %intstruct = type { i32 }
6 %pair = type { i32, i32 }
7 %struct.B = type { double }
8 %struct.A = type { %struct.B, i32, i32 }
9
10
11 @Global = constant [10 x i8] c"helloworld"
12 @Global_as1 = addrspace(1) constant [10 x i8] c"helloworld"
13
14 ; Test noop elimination
15 define i32* @test1(i32* %I) {
16         %A = getelementptr i32* %I, i64 0
17         ret i32* %A
18 ; CHECK-LABEL: @test1(
19 ; CHECK: ret i32* %I
20 }
21
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
27 }
28
29 ; Test noop elimination
30 define i32* @test2(i32* %I) {
31         %A = getelementptr i32* %I
32         ret i32* %A
33 ; CHECK-LABEL: @test2(
34 ; CHECK: ret i32* %I
35 }
36
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
41         ret i32* %B
42 ; CHECK-LABEL: @test3(
43 ; CHECK: getelementptr i32* %I, i64 21
44 }
45
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
50         ret i32* %B
51 ; CHECK-LABEL: @test4(
52 ; CHECK: getelementptr { i32 }* %I, i64 1, i32 0
53 }
54
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
58         store i8 %B, i8* %A
59         ret void
60 ; CHECK-LABEL: @test5(
61 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
62 }
63
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
68         ret void
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)
71 }
72
73 %as1_ptr_struct = type { i32 addrspace(1)* }
74 %as2_ptr_struct = type { i32 addrspace(2)* }
75
76 @global_as2 = addrspace(2) global i32 zeroinitializer
77 @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
78
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
86   ret void
87 }
88
89 @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
90
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
94
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
98   ret void
99 }
100
101 define i32* @test7(i32* %I, i64 %C, i64 %D) {
102         %A = getelementptr i32* %I, i64 %C
103         %B = getelementptr i32* %A, i64 %D
104         ret i32* %B
105 ; CHECK-LABEL: @test7(
106 ; CHECK: %A.sum = add i64 %C, %D
107 ; CHECK: getelementptr i32* %I, i64 %A.sum
108 }
109
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*
114         ret i8* %B
115 ; CHECK-LABEL: @test8(
116 ; CHECK: bitcast [10 x i32]* %X to i8*
117 }
118
119 define i32 @test9() {
120         %A = getelementptr { i32, double }* null, i32 0, i32 1
121         %B = ptrtoint double* %A to i32
122         ret i32 %B
123 ; CHECK-LABEL: @test9(
124 ; CHECK: ret i32 8
125 }
126
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
130         ;; seteq x, y
131         %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
132         ret i1 %tmp.4
133 ; CHECK-LABEL: @test10(
134 ; CHECK: icmp eq { i32, i32 }* %x, %y
135 }
136
137 define i1 @test11({ i32, i32 }* %X) {
138         %P = getelementptr { i32, i32 }* %X, i32 0, i32 0
139         %Q = icmp eq i32* %P, null
140         ret i1 %Q
141 ; CHECK-LABEL: @test11(
142 ; CHECK: icmp eq { i32, i32 }* %X, null
143 }
144
145
146 ; PR4748
147 define i32 @test12(%struct.A* %a) {
148 entry:
149   %g3 = getelementptr %struct.A* %a, i32 0, i32 1
150   store i32 10, i32* %g3, align 4
151
152   %g4 = getelementptr %struct.A* %a, i32 0, i32 0
153
154   %new_a = bitcast %struct.B* %g4 to %struct.A*
155
156   %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1
157   %a_a = load i32* %g5, align 4
158   ret i32 %a_a
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
163 }
164
165
166 ; PR2235
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
172         ret i1 %C
173 ; CHECK-LABEL: @test13(
174 ; CHECK:    %C = icmp eq i64 %X, -1
175 }
176
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
185   ret <2 x i1> %C
186 }
187
188 define i1 @test13_i32(i32 %X, %S* %P) {
189 ; CHECK-LABEL: @test13_i32(
190 ; CHECK: %C = icmp eq i32 %X, -1
191   %A = getelementptr inbounds %S* %P, i32 0, i32 1, i32 %X
192   %B = getelementptr inbounds %S* %P, i32 0, i32 0
193   %C = icmp eq i32* %A, %B
194   ret i1 %C
195 }
196
197 define i1 @test13_i16(i16 %X, %S* %P) {
198 ; CHECK-LABEL: @test13_i16(
199 ; CHECK: %C = icmp eq i16 %X, -1
200   %A = getelementptr inbounds %S* %P, i16 0, i32 1, i16 %X
201   %B = getelementptr inbounds %S* %P, i16 0, i32 0
202   %C = icmp eq i32* %A, %B
203   ret i1 %C
204 }
205
206 define i1 @test13_i128(i128 %X, %S* %P) {
207 ; CHECK-LABEL: @test13_i128(
208 ; CHECK: %C = icmp eq i64 %1, -1
209   %A = getelementptr inbounds %S* %P, i128 0, i32 1, i128 %X
210   %B = getelementptr inbounds %S* %P, i128 0, i32 0
211   %C = icmp eq i32* %A, %B
212   ret i1 %C
213 }
214
215
216 @G = external global [3 x i8]
217 define i8* @test14(i32 %Idx) {
218         %idx = zext i32 %Idx to i64
219         %tmp = getelementptr i8* getelementptr ([3 x i8]* @G, i32 0, i32 0), i64 %idx
220         ret i8* %tmp
221 ; CHECK-LABEL: @test14(
222 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
223 }
224
225
226 ; Test folding of constantexpr geps into normal geps.
227 @Array = external global [40 x i32]
228 define i32 *@test15(i64 %X) {
229         %A = getelementptr i32* getelementptr ([40 x i32]* @Array, i64 0, i64 0), i64 %X
230         ret i32* %A
231 ; CHECK-LABEL: @test15(
232 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
233 }
234
235
236 define i32* @test16(i32* %X, i32 %Idx) {
237         %R = getelementptr i32* %X, i32 %Idx
238         ret i32* %R
239 ; CHECK-LABEL: @test16(
240 ; CHECK: sext i32 %Idx to i64
241 }
242
243
244 define i1 @test17(i16* %P, i32 %I, i32 %J) {
245         %X = getelementptr inbounds i16* %P, i32 %I
246         %Y = getelementptr inbounds i16* %P, i32 %J
247         %C = icmp ult i16* %X, %Y
248         ret i1 %C
249 ; CHECK-LABEL: @test17(
250 ; CHECK: %C = icmp slt i32 %I, %J
251 }
252
253 define i1 @test18(i16* %P, i32 %I) {
254         %X = getelementptr inbounds i16* %P, i32 %I
255         %C = icmp ult i16* %X, %P
256         ret i1 %C
257 ; CHECK-LABEL: @test18(
258 ; CHECK: %C = icmp slt i32 %I, 0
259 }
260
261 ; Smaller than pointer size
262 define i1 @test18_i16(i16* %P, i16 %I) {
263 ; CHECK-LABEL: @test18_i16(
264 ; CHECK: %C = icmp slt i16 %I, 0
265   %X = getelementptr inbounds i16* %P, i16 %I
266   %C = icmp ult i16* %X, %P
267   ret i1 %C
268 }
269
270 ; Same as pointer size
271 define i1 @test18_i64(i16* %P, i64 %I) {
272 ; CHECK-LABEL: @test18_i64(
273 ; CHECK: %C = icmp slt i64 %I, 0
274   %X = getelementptr inbounds i16* %P, i64 %I
275   %C = icmp ult i16* %X, %P
276   ret i1 %C
277 }
278
279 ; Larger than the pointer size
280 define i1 @test18_i128(i16* %P, i128 %I) {
281 ; CHECK-LABEL: @test18_i128(
282 ; CHECK: %C = icmp slt i64 %1, 0
283   %X = getelementptr inbounds i16* %P, i128 %I
284   %C = icmp ult i16* %X, %P
285   ret i1 %C
286 }
287
288 define i32 @test19(i32* %P, i32 %A, i32 %B) {
289         %tmp.4 = getelementptr inbounds i32* %P, i32 %A
290         %tmp.9 = getelementptr inbounds i32* %P, i32 %B
291         %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
292         %tmp.11 = zext i1 %tmp.10 to i32
293         ret i32 %tmp.11
294 ; CHECK-LABEL: @test19(
295 ; CHECK: icmp eq i32 %A, %B
296 }
297
298 define i32 @test20(i32* %P, i32 %A, i32 %B) {
299         %tmp.4 = getelementptr inbounds i32* %P, i32 %A
300         %tmp.6 = icmp eq i32* %tmp.4, %P
301         %tmp.7 = zext i1 %tmp.6 to i32
302         ret i32 %tmp.7
303 ; CHECK-LABEL: @test20(
304 ; CHECK: icmp eq i32 %A, 0
305 }
306
307 define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
308   %tmp.4 = getelementptr inbounds i32 addrspace(1)* %P, i32 %A
309   %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
310   %tmp.7 = zext i1 %tmp.6 to i32
311   ret i32 %tmp.7
312 ; CHECK-LABEL: @test20_as1(
313 ; CHECK: icmp eq i16 %1, 0
314 }
315
316
317 define i32 @test21() {
318         %pbob1 = alloca %intstruct
319         %pbob2 = getelementptr %intstruct* %pbob1
320         %pbobel = getelementptr %intstruct* %pbob2, i64 0, i32 0
321         %rval = load i32* %pbobel
322         ret i32 %rval
323 ; CHECK-LABEL: @test21(
324 ; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
325 }
326
327
328 @A = global i32 1               ; <i32*> [#uses=1]
329 @B = global i32 2               ; <i32*> [#uses=1]
330
331 define i1 @test22() {
332         %C = icmp ult i32* getelementptr (i32* @A, i64 1),
333                            getelementptr (i32* @B, i64 2)
334         ret i1 %C
335 ; CHECK-LABEL: @test22(
336 ; CHECK: icmp ult (i32* getelementptr inbounds (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
337 }
338
339
340 %X = type { [10 x i32], float }
341
342 define i1 @test23() {
343         %A = getelementptr %X* null, i64 0, i32 0, i64 0                ; <i32*> [#uses=1]
344         %B = icmp ne i32* %A, null              ; <i1> [#uses=1]
345         ret i1 %B
346 ; CHECK-LABEL: @test23(
347 ; CHECK: ret i1 false
348 }
349
350 define void @test25() {
351 entry:
352         %tmp = getelementptr { i64, i64, i64, i64 }* null, i32 0, i32 3         ; <i64*> [#uses=1]
353         %tmp.upgrd.1 = load i64* %tmp           ; <i64> [#uses=1]
354         %tmp8.ui = load i64* null               ; <i64> [#uses=1]
355         %tmp8 = bitcast i64 %tmp8.ui to i64             ; <i64> [#uses=1]
356         %tmp9 = and i64 %tmp8, %tmp.upgrd.1             ; <i64> [#uses=1]
357         %sext = trunc i64 %tmp9 to i32          ; <i32> [#uses=1]
358         %tmp27.i = sext i32 %sext to i64                ; <i64> [#uses=1]
359         tail call void @foo25( i32 0, i64 %tmp27.i )
360         unreachable
361 ; CHECK-LABEL: @test25(
362 }
363
364 declare void @foo25(i32, i64)
365
366
367 ; PR1637
368 define i1 @test26(i8* %arr) {
369         %X = getelementptr i8* %arr, i32 1
370         %Y = getelementptr i8* %arr, i32 1
371         %test = icmp uge i8* %X, %Y
372         ret i1 %test
373 ; CHECK-LABEL: @test26(
374 ; CHECK: ret i1 true
375 }
376
377         %struct.__large_struct = type { [100 x i64] }
378         %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
379         %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
380         %struct.sigval_t = type { i8* }
381
382 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
383 entry:
384         %from_addr = alloca %struct.siginfo_t*
385         %tmp344 = load %struct.siginfo_t** %from_addr, align 8
386         %tmp345 = getelementptr %struct.siginfo_t* %tmp344, i32 0, i32 3
387         %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
388         %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
389         %tmp348 = getelementptr { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
390         %tmp349 = getelementptr %struct.sigval_t* %tmp348, i32 0, i32 0
391         %tmp349350 = bitcast i8** %tmp349 to i32*
392         %tmp351 = load i32* %tmp349350, align 8
393         %tmp360 = call i32 asm sideeffect "...",
394         "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
395          %struct.__large_struct* null, i32 -14, i32 0 )
396         unreachable
397 ; CHECK-LABEL: @test27(
398 }
399
400 ; PR1978
401         %struct.x = type <{ i8 }>
402 @.str = internal constant [6 x i8] c"Main!\00"
403 @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
404
405 define i32 @test28() nounwind  {
406 entry:
407         %orientations = alloca [1 x [1 x %struct.x]]
408         %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8]* @.str, i32 0, i32 0) ) nounwind
409         %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
410         %orientations62 = getelementptr [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
411         br label %bb10
412
413 bb10:
414         %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
415         %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
416         %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
417         %tmp12 = getelementptr inbounds %struct.x* %tmp45, i32 %tmp12.rec
418         %tmp16 = call i32 (i8*, ...)* @printf( i8* getelementptr ([12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
419         %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
420         %indvar.next = add i32 %indvar, 1
421         br i1 %tmp84, label %bb17, label %bb10
422
423 bb17:
424         ret i32 0
425 ; CHECK-LABEL: @test28(
426 ; CHECK: icmp eq i32 %indvar, 0
427 }
428
429 declare i32 @puts(i8*)
430
431 declare i32 @printf(i8*, ...)
432
433
434
435
436 ; rdar://6762290
437         %T = type <{ i64, i64, i64 }>
438 define i32 @test29(i8* %start, i32 %X) nounwind {
439 entry:
440         %tmp3 = load i64* null
441         %add.ptr = getelementptr i8* %start, i64 %tmp3
442         %tmp158 = load i32* null
443         %add.ptr159 = getelementptr %T* null, i32 %tmp158
444         %add.ptr209 = getelementptr i8* %start, i64 0
445         %add.ptr212 = getelementptr i8* %add.ptr209, i32 %X
446         %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
447         br i1 %cmp214, label %if.then216, label %if.end363
448
449 if.then216:
450         ret i32 1
451
452 if.end363:
453         ret i32 0
454 ; CHECK-LABEL: @test29(
455 }
456
457
458 ; PR3694
459 define i32 @test30(i32 %m, i32 %n) nounwind {
460 entry:
461         %0 = alloca i32, i32 %n, align 4
462         %1 = bitcast i32* %0 to [0 x i32]*
463         call void @test30f(i32* %0) nounwind
464         %2 = getelementptr [0 x i32]* %1, i32 0, i32 %m
465         %3 = load i32* %2, align 4
466         ret i32 %3
467 ; CHECK-LABEL: @test30(
468 ; CHECK: getelementptr i32
469 }
470
471 declare void @test30f(i32*)
472
473
474
475 define i1 @test31(i32* %A) {
476         %B = getelementptr i32* %A, i32 1
477         %C = getelementptr i32* %A, i64 1
478         %V = icmp eq i32* %B, %C
479         ret i1 %V
480 ; CHECK-LABEL: @test31(
481 ; CHECK: ret i1 true
482 }
483
484
485 ; PR1345
486 define i8* @test32(i8* %v) {
487         %A = alloca [4 x i8*], align 16
488         %B = getelementptr [4 x i8*]* %A, i32 0, i32 0
489         store i8* null, i8** %B
490         %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
491         %D = getelementptr { [16 x i8] }* %C, i32 0, i32 0, i32 8
492         %E = bitcast i8* %D to i8**
493         store i8* %v, i8** %E
494         %F = getelementptr [4 x i8*]* %A, i32 0, i32 2
495         %G = load i8** %F
496         ret i8* %G
497 ; CHECK-LABEL: @test32(
498 ; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
499 ; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
500 }
501
502 ; PR3290
503 %struct.Key = type { { i32, i32 } }
504 %struct.anon = type <{ i8, [3 x i8], i32 }>
505
506 define i32 *@test33(%struct.Key *%A) {
507         %B = bitcast %struct.Key* %A to %struct.anon*
508         %C = getelementptr %struct.anon* %B, i32 0, i32 2
509         ret i32 *%C
510 ; CHECK-LABEL: @test33(
511 ; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
512 }
513
514
515
516         %T2 = type { i8*, i8 }
517 define i8* @test34(i8* %Val, i64 %V) nounwind {
518 entry:
519         %A = alloca %T2, align 8
520         %mrv_gep = bitcast %T2* %A to i64*
521         %B = getelementptr %T2* %A, i64 0, i32 0
522
523         store i64 %V, i64* %mrv_gep
524         %C = load i8** %B, align 8
525         ret i8* %C
526 ; CHECK-LABEL: @test34(
527 ; CHECK: %V.c = inttoptr i64 %V to i8*
528 ; CHECK: ret i8* %V.c
529 }
530
531 %t0 = type { i8*, [19 x i8] }
532 %t1 = type { i8*, [0 x i8] }
533
534 @array = external global [11 x i8]
535
536 @s = external global %t0
537 @"\01LC8" = external constant [17 x i8]
538
539 ; Instcombine should be able to fold this getelementptr.
540
541 define i32 @test35() nounwind {
542   call i32 (i8*, ...)* @printf(i8* getelementptr ([17 x i8]* @"\01LC8", i32 0, i32 0),
543              i8* getelementptr (%t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
544   ret i32 0
545 ; CHECK-LABEL: @test35(
546 ; 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]+]]
547 }
548
549 ; Instcombine should constant-fold the GEP so that indices that have
550 ; static array extents are within bounds of those array extents.
551 ; In the below, -1 is not in the range [0,11). After the transformation,
552 ; the same address is computed, but 3 is in the range of [0,11).
553
554 define i8* @test36() nounwind {
555   ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
556 ; CHECK-LABEL: @test36(
557 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
558 }
559
560 ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
561 @A37 = external constant [1 x i8]
562 define i1 @test37() nounwind {
563 ; CHECK-LABEL: @test37(
564 ; CHECK: ret i1 true
565   %t = icmp eq i8* getelementptr ([1 x i8]* @A37, i64 0, i64 1),
566                    getelementptr ([1 x i8]* @A37, i64 1, i64 0)
567   ret i1 %t
568 }
569
570 ; Test index promotion
571 define i32* @test38(i32* %I, i32 %n) {
572         %A = getelementptr i32* %I, i32 %n
573         ret i32* %A
574 ; CHECK-LABEL: @test38(
575 ; CHECK: = sext i32 %n to i64
576 ; CHECK: %A = getelementptr i32* %I, i64 %
577 }
578
579 ; Test that we don't duplicate work when the second gep is a "bitcast".
580 %pr10322_t = type { i8* }
581 declare void @pr10322_f2(%pr10322_t*)
582 declare void @pr10322_f3(i8**)
583 define void @pr10322_f1(%pr10322_t* %foo) {
584 entry:
585   %arrayidx8 = getelementptr inbounds %pr10322_t* %foo, i64 2
586   call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
587   %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
588   call void @pr10322_f3(i8** %tmp2) nounwind
589   ret void
590
591 ; CHECK-LABEL: @pr10322_f1(
592 ; CHECK: %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0
593 }
594
595 ; Test that we combine the last two geps in this sequence, before we
596 ; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
597 %three_gep_t = type {i32}
598 %three_gep_t2 = type {%three_gep_t}
599
600 define void @three_gep_f(%three_gep_t2* %x) {
601   %gep1 = getelementptr %three_gep_t2* %x, i64 2
602   call void @three_gep_h(%three_gep_t2* %gep1)
603   %gep2 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0
604   %gep3 = getelementptr %three_gep_t* %gep2, i64 0, i32 0
605   call void @three_gep_g(i32* %gep3)
606
607 ; CHECK-LABEL: @three_gep_f(
608 ; CHECK: %gep3 = getelementptr %three_gep_t2* %gep1, i64 0, i32 0, i32 0
609   ret void
610 }
611
612 declare void @three_gep_g(i32*)
613 declare void @three_gep_h(%three_gep_t2*)
614
615 %struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
616 %struct.zot = type { i64, i8 }
617
618 define void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
619   %tmp = getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
620   %tmp2 = load %struct.zot** %tmp, align 8
621   %tmp3 = bitcast %struct.zot* %tmp2 to i8*
622   %tmp4 = getelementptr inbounds i8* %tmp3, i64 -8
623   store i8 %arg1, i8* %tmp4, align 8
624   ret void
625
626 ; CHECK-LABEL: @test39(
627 ; CHECK: getelementptr inbounds %struct.ham* %arg, i64 0, i32 2
628 ; CHECK: getelementptr inbounds i8* %tmp3, i64 -8
629 }
630
631 define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
632   %c = getelementptr [1 x i8]* %a, i32 0, i32 0
633   %d = getelementptr [1 x i8]* %b, i32 0, i32 0
634   %cmp = icmp ult i8* %c, %d
635   ret i1 %cmp
636
637 ; CHECK-LABEL: @pr16483(
638 ; CHECK-NEXT: icmp ult  [1 x i8]* %a, %b
639 }
640
641 ; The element size of the array matches the element size of the pointer
642 define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
643 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
644 ; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %V
645 ; CHECK: bitcast
646   %cast = bitcast [100 x double]* %arr to i64*
647   %V = mul i64 %N, 8
648   %t = getelementptr i64* %cast, i64 %V
649   %x = load i64* %t
650   ret i64 %x
651 }
652
653 ; The element size of the array is different the element size of the pointer
654 define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
655 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
656 ; CHECK: getelementptr [100 x double]* %arr, i64 0, i64 %N
657 ; CHECK: bitcast
658   %cast = bitcast [100 x double]* %arr to i8*
659   %V = mul i64 %N, 8
660   %t = getelementptr i8* %cast, i64 %V
661   %x = load i8* %t
662   ret i8 %x
663 }
664
665 define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
666 ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
667 ; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %V
668 ; CHECK: bitcast
669   %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
670   %V = mul i16 %N, 8
671   %t = getelementptr i64 addrspace(1)* %cast, i16 %V
672   %x = load i64 addrspace(1)* %t
673   ret i64 %x
674 }
675
676 define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
677 ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
678 ; CHECK: getelementptr [100 x double] addrspace(1)* %arr, i16 0, i16 %N
679 ; CHECK: bitcast
680   %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
681   %V = mul i16 %N, 8
682   %t = getelementptr i8 addrspace(1)* %cast, i16 %V
683   %x = load i8 addrspace(1)* %t
684   ret i8 %x
685 }
686
687 ; CHECK: attributes [[NUW]] = { nounwind }