Change these tests to feed the assembly files to opt directly, instead
[oota-llvm.git] / test / Transforms / InstCombine / getelementptr.ll
1 ; RUN: opt %s -instcombine | llvm-dis | FileCheck %s
2
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 }
8
9
10 @Global = constant [10 x i8] c"helloworld"
11
12 ; Test noop elimination
13 define i32* @test1(i32* %I) {
14         %A = getelementptr i32* %I, i64 0 
15         ret i32* %A
16 ; CHECK: @test1
17 ; CHECK: ret i32* %I
18 }
19
20 ; Test noop elimination
21 define i32* @test2(i32* %I) {
22         %A = getelementptr i32* %I
23         ret i32* %A
24 ; CHECK: @test2
25 ; CHECK: ret i32* %I
26 }
27
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
32         ret i32* %B
33 ; CHECK: @test3
34 ; CHECK: getelementptr i32* %I, i64 21
35 }
36
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
41         ret i32* %B
42 ; CHECK: @test4
43 ; CHECK: getelementptr %intstruct* %I, i64 1, i32 0
44 }
45
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 
49         store i8 %B, i8* %A
50         ret void
51 ; CHECK: @test5
52 ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8]* @Global, i64 0, i64 4)
53 }
54
55 define i32* @test6() {
56         %M = malloc [4 x i32] 
57         %A = getelementptr [4 x i32]* %M, i64 0, i64 0
58         %B = getelementptr i32* %A, i64 2             
59         ret i32* %B
60 ; CHECK: @test6
61 ; CHECK: getelementptr [4 x i32]* %M, i64 0, i64 2
62 }
63
64 define i32* @test7(i32* %I, i64 %C, i64 %D) {
65         %A = getelementptr i32* %I, i64 %C 
66         %B = getelementptr i32* %A, i64 %D 
67         ret i32* %B
68 ; CHECK: @test7
69 ; CHECK: %A.sum = add i64 %C, %D
70 ; CHECK: getelementptr i32* %I, i64 %A.sum
71 }
72
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*     
77         ret i8* %B
78 ; CHECK: @test8
79 ; CHECK: bitcast [10 x i32]* %X to i8*
80 }
81
82 define i32 @test9() {
83         %A = getelementptr { i32, double }* null, i32 0, i32 1
84         %B = ptrtoint double* %A to i32        
85         ret i32 %B
86 ; CHECK: @test9
87 ; CHECK: ret i32 8
88 }
89
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
93         ;; seteq x, y
94         %tmp.4 = icmp eq i32* %tmp.1, %tmp.3       
95         ret i1 %tmp.4
96 ; CHECK: @test10
97 ; CHECK: icmp eq %pair* %x, %y
98 }
99
100 define i1 @test11({ i32, i32 }* %X) {
101         %P = getelementptr { i32, i32 }* %X, i32 0, i32 0 
102         %Q = icmp eq i32* %P, null             
103         ret i1 %Q
104 ; CHECK: @test11
105 ; CHECK: icmp eq %pair* %X, null
106 }
107
108
109 ; PR4748
110 define i32 @test12(%struct.A* %a) {
111 entry:
112   %g3 = getelementptr %struct.A* %a, i32 0, i32 1
113   store i32 10, i32* %g3, align 4
114
115   %g4 = getelementptr %struct.A* %a, i32 0, i32 0
116   
117   %new_a = bitcast %struct.B* %g4 to %struct.A*
118
119   %g5 = getelementptr %struct.A* %new_a, i32 0, i32 1   
120   %a_a = load i32* %g5, align 4 
121   ret i32 %a_a
122 ; CHECK:      @test12
123 ; CHECK:      getelementptr %struct.A* %a, i64 0, i32 1
124 ; CHECK-NEXT: store i32 10, i32* %g3
125 ; CHECK-NEXT: ret i32 10
126 }
127
128
129 ; PR2235
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
135         ret i1 %C
136 ; CHECK: @test13
137 ; CHECK:    %C = icmp eq i64 %X, -1
138 }
139
140
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
145         ret i8* %tmp
146 ; CHECK: @test14
147 ; CHECK: getelementptr [3 x i8]* @G, i64 0, i64 %idx
148 }
149
150
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
155         ret i32* %A
156 ; CHECK: @test15
157 ; CHECK: getelementptr [40 x i32]* @Array, i64 0, i64 %X
158 }
159
160
161 define i32* @test16(i32* %X, i32 %Idx) {
162         %R = getelementptr i32* %X, i32 %Idx       
163         ret i32* %R
164 ; CHECK: @test16
165 ; CHECK: sext i32 %Idx to i64
166 }
167
168
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
173         ret i1 %C
174 ; CHECK: @test17
175 ; CHECK: %C = icmp slt i32 %I, %J 
176 }
177
178 define i1 @test18(i16* %P, i32 %I) {
179         %X = getelementptr inbounds i16* %P, i32 %I
180         %C = icmp ult i16* %X, %P
181         ret i1 %C
182 ; CHECK: @test18
183 ; CHECK: %C = icmp slt i32 %I, 0
184 }
185
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
191         ret i32 %tmp.11
192 ; CHECK: @test19
193 ; CHECK: icmp eq i32 %A, %B
194 }
195
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
200         ret i32 %tmp.7
201 ; CHECK: @test20
202 ; CHECK: icmp eq i32 %A, 0
203 }
204
205
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
211         ret i32 %rval
212 ; CHECK: @test21
213 ; CHECK: getelementptr %intstruct* %pbob1, i64 0, i32 0
214 }
215
216
217 @A = global i32 1               ; <i32*> [#uses=1]
218 @B = global i32 2               ; <i32*> [#uses=1]
219
220 define i1 @test22() {
221         %C = icmp ult i32* getelementptr (i32* @A, i64 1), 
222                            getelementptr (i32* @B, i64 2) 
223         ret i1 %C
224 ; CHECK: @test22
225 ; CHECK: icmp ult (i32* getelementptr (i32* @A, i64 1), i32* getelementptr (i32* @B, i64 2))
226 }
227
228
229 %X = type { [10 x i32], float }
230
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]
234         ret i1 %B
235 ; CHECK: @test23
236 ; CHECK: ret i1 false
237 }
238
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
242
243 define void @test24() {
244 bc0:
245         %tmp53 = getelementptr %"java/lang/StringBuffer"* null, i32 0, i32 1            ; <i32*> [#uses=1]
246         store i32 0, i32* %tmp53
247         ret void
248 ; CHECK: @test24
249 ; CHECK: store i32 0, i32* getelementptr (%"java/lang/StringBuffer"* null, i32 0, i32 1)
250 }
251
252 define void @test25() {
253 entry:
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 )
262         unreachable
263 ; CHECK: @test25
264 }
265
266 declare void @foo25(i32, i64)
267
268
269 ; PR1637
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
274         ret i1 %test
275 ; CHECK: @test26
276 ; CHECK: ret i1 true
277 }
278
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* }
283
284 define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
285 entry:
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 )
298         unreachable
299 ; CHECK: @test27
300 }
301
302 ; PR1978
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"        
306
307 define i32 @test28() nounwind  {
308 entry:
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
313         br label %bb10
314
315 bb10:
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
324
325 bb17:   
326         ret i32 0
327 ; CHECK: @test28
328 ; CHECK: icmp eq i32 %indvar, 0
329 }
330
331 declare i32 @puts(i8*)
332
333 declare i32 @printf(i8*, ...)
334
335
336
337
338 ; rdar://6762290
339         %T = type <{ i64, i64, i64 }>
340 define i32 @test29(i8* %start, i32 %X) nounwind {
341 entry:
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
350
351 if.then216:
352         ret i32 1
353
354 if.end363:
355         ret i32 0
356 ; CHECK: @test29
357 }
358
359
360 ; PR3694
361 define i32 @test30(i32 %m, i32 %n) nounwind {
362 entry:
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
368         ret i32 %3
369 ; CHECK: @test30
370 ; CHECK: getelementptr i32
371 }
372
373 declare void @test30f(i32*)
374
375
376
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 
381         ret i1 %V
382 ; CHECK: @test31
383 ; CHECK: ret i1 true
384 }
385
386
387 ; PR1345
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  
397         %G = load i8** %F
398         ret i8* %G
399 ; CHECK: @test32
400 ; CHECK: %D = getelementptr [4 x i8*]* %A, i64 0, i64 1
401 ; CHECK: %F = getelementptr [4 x i8*]* %A, i64 0, i64 2
402 }
403
404 ; PR3290
405 %struct.Key = type { { i32, i32 } }
406 %struct.anon = type <{ i8, [3 x i8], i32 }>
407
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 
411         ret i32 *%C
412 ; CHECK: @test33
413 ; CHECK: getelementptr %struct.Key* %A, i64 0, i32 0, i32 1
414 }
415
416
417
418         %T2 = type { i8*, i8 }
419 define i8* @test34(i8* %Val, i64 %V) nounwind {
420 entry:
421         %A = alloca %T2, align 8        
422         %mrv_gep = bitcast %T2* %A to i64*
423         %B = getelementptr %T2* %A, i64 0, i32 0
424         
425         store i64 %V, i64* %mrv_gep
426         %C = load i8** %B, align 8
427         ret i8* %C
428 ; CHECK: @test34
429 ; CHECK: %V.c = inttoptr i64 %V to i8*
430 ; CHECK: ret i8* %V.c
431 }
432
433 %t0 = type { i8*, [19 x i8] }
434 %t1 = type { i8*, [0 x i8] }
435
436 @array = external global [11 x i8]
437
438 @s = external global %t0
439 @"\01LC8" = external constant [17 x i8]
440
441 ; Instcombine should be able to fold this getelementptr.
442
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
446   ret i32 0
447 ; CHECK: @test35
448 ; CHECK: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([17 x i8]* @"\01LC8", i64 0, i64 0), i8* bitcast (i8** getelementptr (%t1* bitcast (%t0* @s to %t1*), i64 1, i32 0) to i8*)) nounwind
449 }
450
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).
455
456 define i8* @test36() nounwind {
457   ret i8* getelementptr ([11 x i8]* @array, i32 0, i64 -1)
458 ; CHECK: @test36
459 ; CHECK: ret i8* getelementptr ([11 x i8]* @array, i64 1676976733973595601, i64 4)
460 }
461