1 ; This test makes sure that these instructions are properly eliminated.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128:n8:16:32:64"
7 define i32 @test1(i32 %A, i1 %b) {
9 br i1 %b, label %BB1, label %BB2
12 ; Combine away one argument PHI nodes
13 %B = phi i32 [ %A, %BB0 ]
20 ; CHECK-NEXT: ret i32 %A
23 define i32 @test2(i32 %A, i1 %b) {
25 br i1 %b, label %BB1, label %BB2
31 ; Combine away PHI nodes with same values
32 %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ]
36 ; CHECK-NEXT: ret i32 %A
39 define i32 @test3(i32 %A, i1 %b) {
44 ; PHI has same value always.
45 %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ]
46 br i1 %b, label %Loop, label %Exit
52 ; CHECK-NEXT: ret i32 %A
55 define i32 @test4(i1 %b) {
60 Loop: ; preds = %L2, %Loop
61 ; PHI has same value always.
62 %B = phi i32 [ %B, %L2 ], [ %B, %Loop ]
63 br i1 %b, label %L2, label %Loop
69 ; CHECK-NEXT: br i1 %b
72 define i32 @test5(i32 %A, i1 %b) {
76 Loop: ; preds = %Loop, %BB0
77 ; PHI has same value always.
78 %B = phi i32 [ %A, %BB0 ], [ undef, %Loop ]
79 br i1 %b, label %Loop, label %Exit
85 ; CHECK-NEXT: br i1 %b
87 ; CHECK-NEXT: ret i32 %A
90 define i32 @test6(i16 %A, i1 %b) {
92 %X = zext i16 %A to i32
93 br i1 %b, label %BB1, label %BB2
96 %Y = zext i16 %A to i32
100 ;; Suck casts into phi
101 %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
105 ; CHECK: zext i16 %A to i32
106 ; CHECK-NEXT: ret i32
109 define i32 @test7(i32 %A, i1 %b) {
113 Loop: ; preds = %Loop, %BB0
115 %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ]
117 br i1 %b, label %Loop, label %Exit
119 Exit: ; preds = %Loop
123 ; CHECK-NEXT: br i1 %b
126 define i32* @test8({ i32, i32 } *%A, i1 %b) {
128 %X = getelementptr { i32, i32 } *%A, i32 0, i32 1
129 br i1 %b, label %BB1, label %BB2
132 %Y = getelementptr { i32, i32 } *%A, i32 0, i32 1
136 ;; Suck GEPs into phi
137 %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ]
142 ; CHECK-NEXT: %B = getelementptr
143 ; CHECK-NEXT: ret i32* %B
146 define i32 @test9(i32* %A, i32* %B) {
148 %c = icmp eq i32* %A, null
149 br i1 %c, label %bb1, label %bb
152 %C = load i32* %B, align 1
156 %D = load i32* %A, align 1
160 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
164 ; CHECK-NEXT: phi i32* [ %B, %bb ], [ %A, %bb1 ]
165 ; CHECK-NEXT: %E = load i32* %{{[^,]*}}, align 1
166 ; CHECK-NEXT: ret i32 %E
170 define i32 @test10(i32* %A, i32* %B) {
172 %c = icmp eq i32* %A, null
173 br i1 %c, label %bb1, label %bb
176 %C = load i32* %B, align 16
180 %D = load i32* %A, align 32
184 %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
188 ; CHECK-NEXT: phi i32* [ %B, %bb ], [ %A, %bb1 ]
189 ; CHECK-NEXT: %E = load i32* %{{[^,]*}}, align 16
190 ; CHECK-NEXT: ret i32 %E
195 declare i1 @test11a()
197 define i1 @test11() {
200 %i = ptrtoint i32* %a to i32
201 %b = call i1 @test11a()
202 br i1 %b, label %one, label %two
205 %x = phi i32 [%i, %entry], [%y, %two]
206 %c = call i1 @test11a()
207 br i1 %c, label %two, label %end
210 %y = phi i32 [%i, %entry], [%x, %one]
211 %d = call i1 @test11a()
212 br i1 %d, label %one, label %end
215 %f = phi i32 [ %x, %one], [%y, %two]
216 ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
217 ; even though %f must equal %i at this point
218 %g = inttoptr i32 %f to i32*
219 store i32 10, i32* %g
220 %z = call i1 @test11a()
228 define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) {
230 %tmp41 = ptrtoint i8* %Ptr to i64
231 %tmp42 = zext i64 %tmp41 to i128
232 br i1 %cond, label %end, label %two
235 %tmp36 = zext i64 %Val to i128 ; <i128> [#uses=1]
236 %tmp37 = shl i128 %tmp36, 64 ; <i128> [#uses=1]
237 %ins39 = or i128 %tmp42, %tmp37 ; <i128> [#uses=1]
241 %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
242 %tmp32 = trunc i128 %tmp869.0 to i64 ; <i64> [#uses=1]
243 %tmp29 = lshr i128 %tmp869.0, 64 ; <i128> [#uses=1]
244 %tmp30 = trunc i128 %tmp29 to i64 ; <i64> [#uses=1]
246 %tmp2 = add i64 %tmp32, %tmp30
251 ; CHECK-NEXT: phi i64 [ 0, %entry ], [ %Val, %two ]
256 declare void @test13f(double, i32)
258 define void @test13(i1 %cond, i32 %V1, double %Vald) {
260 %tmp42 = zext i32 %V1 to i128
261 br i1 %cond, label %end, label %two
264 %Val = bitcast double %Vald to i64
265 %tmp36 = zext i64 %Val to i128 ; <i128> [#uses=1]
266 %tmp37 = shl i128 %tmp36, 64 ; <i128> [#uses=1]
267 %ins39 = or i128 %tmp42, %tmp37 ; <i128> [#uses=1]
271 %tmp869.0 = phi i128 [ %tmp42, %entry ], [ %ins39, %two ]
272 %tmp32 = trunc i128 %tmp869.0 to i32
273 %tmp29 = lshr i128 %tmp869.0, 64 ; <i128> [#uses=1]
274 %tmp30 = trunc i128 %tmp29 to i64 ; <i64> [#uses=1]
275 %tmp31 = bitcast i64 %tmp30 to double
277 call void @test13f(double %tmp31, i32 %tmp32)
282 ; CHECK-NEXT: phi double [ 0.000000e+00, %entry ], [ %Vald, %two ]
283 ; CHECK-NEXT: call void @test13f(double {{[^,]*}}, i32 %V1)
287 define i640 @test14a(i320 %A, i320 %B, i1 %b1) {
289 %a = zext i320 %A to i640
290 %b = zext i320 %B to i640
294 %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ]
295 br i1 %b1, label %Loop, label %Exit
297 Exit: ; preds = %Loop
301 ; CHECK-NEXT: phi i320
304 define i160 @test14b(i320 %A, i320 %B, i1 %b1) {
306 %a = trunc i320 %A to i160
307 %b = trunc i320 %B to i160
311 %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ]
312 br i1 %b1, label %Loop, label %Exit
314 Exit: ; preds = %Loop
318 ; CHECK-NEXT: phi i160
321 declare i64 @test15a(i64)
323 define i64 @test15b(i64 %A, i1 %b) {
326 %i0 = zext i64 %A to i128
327 %i1 = shl i128 %i0, 64
328 %i = or i128 %i1, %i0
329 br i1 %b, label %one, label %two
331 ; CHECK-NEXT: br i1 %b
334 %x = phi i128 [%i, %entry], [%y, %two]
335 %x1 = lshr i128 %x, 64
336 %x2 = trunc i128 %x1 to i64
337 %c = call i64 @test15a(i64 %x2)
338 %c1 = zext i64 %c to i128
342 ; CHECK-NEXT: phi i64
343 ; CHECK-NEXT: %c = call i64 @test15a
346 %y = phi i128 [%i, %entry], [%c1, %one]
347 %y1 = lshr i128 %y, 64
348 %y2 = trunc i128 %y1 to i64
349 %d = call i64 @test15a(i64 %y2)
350 %d1 = trunc i64 %d to i1
351 br i1 %d1, label %one, label %end
354 ; CHECK-NEXT: phi i64
355 ; CHECK-NEXT: phi i64
356 ; CHECK-NEXT: %d = call i64 @test15a
359 %g = trunc i128 %y to i64
362 ; CHECK-NEXT: ret i64
365 ; PR6512 - Shouldn't merge loads from different addr spaces.
366 define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2)
369 %retval = alloca i32, align 4 ; <i32*> [#uses=2]
370 %pointer1.addr = alloca i32 addrspace(1)*, align 4 ; <i32 addrspace(1)**>
371 %flag.addr = alloca i32, align 4 ; <i32*> [#uses=2]
372 %pointer2.addr = alloca i32*, align 4 ; <i32**> [#uses=2]
373 %res = alloca i32, align 4 ; <i32*> [#uses=4]
374 store i32 addrspace(1)* %pointer1, i32 addrspace(1)** %pointer1.addr
375 store i32 %flag, i32* %flag.addr
376 store i32* %pointer2, i32** %pointer2.addr
377 store i32 10, i32* %res
378 %tmp = load i32* %flag.addr ; <i32> [#uses=1]
379 %tobool = icmp ne i32 %tmp, 0 ; <i1> [#uses=1]
380 br i1 %tobool, label %if.then, label %if.else
382 return: ; preds = %if.end
383 %tmp7 = load i32* %retval ; <i32> [#uses=1]
386 if.end: ; preds = %if.else, %if.then
387 %tmp6 = load i32* %res ; <i32> [#uses=1]
388 store i32 %tmp6, i32* %retval
391 if.then: ; preds = %entry
392 %tmp1 = load i32 addrspace(1)** %pointer1.addr ; <i32 addrspace(1)*>
393 %arrayidx = getelementptr i32 addrspace(1)* %tmp1, i32 0 ; <i32 addrspace(1)*> [#uses=1]
394 %tmp2 = load i32 addrspace(1)* %arrayidx ; <i32> [#uses=1]
395 store i32 %tmp2, i32* %res
398 if.else: ; preds = %entry
399 %tmp3 = load i32** %pointer2.addr ; <i32*> [#uses=1]
400 %arrayidx4 = getelementptr i32* %tmp3, i32 0 ; <i32*> [#uses=1]
401 %tmp5 = load i32* %arrayidx4 ; <i32> [#uses=1]
402 store i32 %tmp5, i32* %res
409 define i32 @test17(i1 %a) {
411 br i1 %a, label %bb1, label %bb2
413 bb1: ; preds = %entry
414 %0 = tail call i32 @ext() ; <i32> [#uses=1]
417 bb2: ; preds = %bb1, %entry
418 %cond = phi i1 [ true, %bb1 ], [ false, %entry ] ; <i1> [#uses=1]
419 ; CHECK-NOT: %val = phi i32 [ %0, %bb1 ], [ 0, %entry ]
420 %val = phi i32 [ %0, %bb1 ], [ 0, %entry ] ; <i32> [#uses=1]
421 %res = select i1 %cond, i32 %val, i32 0 ; <i32> [#uses=1]
422 ; CHECK: ret i32 %cond
426 define i1 @test18(i1 %cond) {
429 br i1 %cond, label %true, label %false
435 %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
436 %isnull = icmp eq i32* %ptr, null
439 ; CHECK: ret i1 false
442 define i1 @test19(i1 %cond, double %x) {
443 br i1 %cond, label %true, label %false
449 %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
450 %cmp = fcmp ule double %x, %p
456 define i1 @test20(i1 %cond) {
460 br i1 %cond, label %true, label %false
466 %p = phi i32* [ %a, %true ], [ %b, %false ]
467 %r = icmp eq i32* %p, %c
470 ; CHECK: ret i1 false
473 define i1 @test21(i1 %c1, i1 %c2) {
477 br i1 %c1, label %true, label %false
483 %p = phi i32* [ %a, %true ], [ %b, %false ], [ %p, %loop ]
484 %r = icmp eq i32* %p, %c
485 br i1 %c2, label %ret, label %loop
489 ; CHECK: ret i1 false
492 define void @test22() {
497 %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
500 %e = icmp eq i32 %o, %y
501 br i1 %e, label %loop, label %ret