• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; This test makes sure that these instructions are properly eliminated.
2;
3; RUN: opt < %s -instcombine -S | FileCheck %s
4
5target 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"
6
7define i32 @test1(i32 %A, i1 %b) {
8BB0:
9        br i1 %b, label %BB1, label %BB2
10
11BB1:
12        ; Combine away one argument PHI nodes
13        %B = phi i32 [ %A, %BB0 ]
14        ret i32 %B
15
16BB2:
17        ret i32 %A
18; CHECK: @test1
19; CHECK: BB1:
20; CHECK-NEXT: ret i32 %A
21}
22
23define i32 @test2(i32 %A, i1 %b) {
24BB0:
25        br i1 %b, label %BB1, label %BB2
26
27BB1:
28        br label %BB2
29
30BB2:
31        ; Combine away PHI nodes with same values
32        %B = phi i32 [ %A, %BB0 ], [ %A, %BB1 ]
33        ret i32 %B
34; CHECK: @test2
35; CHECK: BB2:
36; CHECK-NEXT: ret i32 %A
37}
38
39define i32 @test3(i32 %A, i1 %b) {
40BB0:
41        br label %Loop
42
43Loop:
44        ; PHI has same value always.
45        %B = phi i32 [ %A, %BB0 ], [ %B, %Loop ]
46        br i1 %b, label %Loop, label %Exit
47
48Exit:
49        ret i32 %B
50; CHECK: @test3
51; CHECK: Exit:
52; CHECK-NEXT: ret i32 %A
53}
54
55define i32 @test4(i1 %b) {
56BB0:
57        ; Loop is unreachable
58        ret i32 7
59
60Loop:           ; 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
64
65L2:             ; preds = %Loop
66        br label %Loop
67; CHECK: @test4
68; CHECK: Loop:
69; CHECK-NEXT: br i1 %b
70}
71
72define i32 @test5(i32 %A, i1 %b) {
73BB0:
74        br label %Loop
75
76Loop:           ; 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
80
81Exit:           ; preds = %Loop
82        ret i32 %B
83; CHECK: @test5
84; CHECK: Loop:
85; CHECK-NEXT: br i1 %b
86; CHECK: Exit:
87; CHECK-NEXT: ret i32 %A
88}
89
90define i32 @test6(i16 %A, i1 %b) {
91BB0:
92        %X = zext i16 %A to i32
93        br i1 %b, label %BB1, label %BB2
94
95BB1:
96        %Y = zext i16 %A to i32
97        br label %BB2
98
99BB2:
100        ;; Suck casts into phi
101        %B = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
102        ret i32 %B
103; CHECK: @test6
104; CHECK: BB2:
105; CHECK: zext i16 %A to i32
106; CHECK-NEXT: ret i32
107}
108
109define i32 @test7(i32 %A, i1 %b) {
110BB0:
111        br label %Loop
112
113Loop:           ; preds = %Loop, %BB0
114        ; PHI is dead.
115        %B = phi i32 [ %A, %BB0 ], [ %C, %Loop ]
116        %C = add i32 %B, 123
117        br i1 %b, label %Loop, label %Exit
118
119Exit:           ; preds = %Loop
120        ret i32 0
121; CHECK: @test7
122; CHECK: Loop:
123; CHECK-NEXT: br i1 %b
124}
125
126define i32* @test8({ i32, i32 } *%A, i1 %b) {
127BB0:
128        %X = getelementptr inbounds { i32, i32 } *%A, i32 0, i32 1
129        br i1 %b, label %BB1, label %BB2
130
131BB1:
132        %Y = getelementptr { i32, i32 } *%A, i32 0, i32 1
133        br label %BB2
134
135BB2:
136        ;; Suck GEPs into phi
137        %B = phi i32* [ %X, %BB0 ], [ %Y, %BB1 ]
138        ret i32* %B
139; CHECK: @test8
140; CHECK-NOT: phi
141; CHECK: BB2:
142; CHECK-NEXT: %B = getelementptr { i32, i32 }* %A
143; CHECK-NEXT: ret i32* %B
144}
145
146define i32 @test9(i32* %A, i32* %B) {
147entry:
148  %c = icmp eq i32* %A, null
149  br i1 %c, label %bb1, label %bb
150
151bb:
152  %C = load i32* %B, align 1
153  br label %bb2
154
155bb1:
156  %D = load i32* %A, align 1
157  br label %bb2
158
159bb2:
160  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
161  ret i32 %E
162; CHECK: @test9
163; CHECK:       bb2:
164; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
165; CHECK-NEXT:   %E = load i32* %{{[^,]*}}, align 1
166; CHECK-NEXT:   ret i32 %E
167
168}
169
170define i32 @test10(i32* %A, i32* %B) {
171entry:
172  %c = icmp eq i32* %A, null
173  br i1 %c, label %bb1, label %bb
174
175bb:
176  %C = load i32* %B, align 16
177  br label %bb2
178
179bb1:
180  %D = load i32* %A, align 32
181  br label %bb2
182
183bb2:
184  %E = phi i32 [ %C, %bb ], [ %D, %bb1 ]
185  ret i32 %E
186; CHECK: @test10
187; CHECK:       bb2:
188; CHECK-NEXT:        phi i32* [ %B, %bb ], [ %A, %bb1 ]
189; CHECK-NEXT:   %E = load i32* %{{[^,]*}}, align 16
190; CHECK-NEXT:   ret i32 %E
191}
192
193
194; PR1777
195declare i1 @test11a()
196
197define i1 @test11() {
198entry:
199  %a = alloca i32
200  %i = ptrtoint i32* %a to i64
201  %b = call i1 @test11a()
202  br i1 %b, label %one, label %two
203
204one:
205  %x = phi i64 [%i, %entry], [%y, %two]
206  %c = call i1 @test11a()
207  br i1 %c, label %two, label %end
208
209two:
210  %y = phi i64 [%i, %entry], [%x, %one]
211  %d = call i1 @test11a()
212  br i1 %d, label %one, label %end
213
214end:
215  %f = phi i64 [ %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 i64 %f to i32*
219  store i32 10, i32* %g
220  %z = call i1 @test11a()
221  ret i1 %z
222; CHECK: @test11
223; CHECK-NOT: phi i32
224; CHECK: ret i1 %z
225}
226
227
228define i64 @test12(i1 %cond, i8* %Ptr, i64 %Val) {
229entry:
230  %tmp41 = ptrtoint i8* %Ptr to i64
231  %tmp42 = zext i64 %tmp41 to i128
232  br i1 %cond, label %end, label %two
233
234two:
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]
238  br label %end
239
240end:
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]
245
246  %tmp2 = add i64 %tmp32, %tmp30
247  ret i64 %tmp2
248; CHECK: @test12
249; CHECK-NOT: zext
250; CHECK: end:
251; CHECK-NEXT: phi i64 [ 0, %entry ], [ %Val, %two ]
252; CHECK-NOT: phi
253; CHECK: ret i64
254}
255
256declare void @test13f(double, i32)
257
258define void @test13(i1 %cond, i32 %V1, double %Vald) {
259entry:
260  %tmp42 = zext i32 %V1 to i128
261  br i1 %cond, label %end, label %two
262
263two:
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]
268  br label %end
269
270end:
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
276
277  call void @test13f(double %tmp31, i32 %tmp32)
278  ret void
279; CHECK: @test13
280; CHECK-NOT: zext
281; CHECK: end:
282; CHECK-NEXT: phi double [ 0.000000e+00, %entry ], [ %Vald, %two ]
283; CHECK-NEXT: call void @test13f(double {{[^,]*}}, i32 %V1)
284; CHECK: ret void
285}
286
287define i640 @test14a(i320 %A, i320 %B, i1 %b1) {
288BB0:
289        %a = zext i320 %A to i640
290        %b = zext i320 %B to i640
291        br label %Loop
292
293Loop:
294        %C = phi i640 [ %a, %BB0 ], [ %b, %Loop ]
295        br i1 %b1, label %Loop, label %Exit
296
297Exit:           ; preds = %Loop
298        ret i640 %C
299; CHECK: @test14a
300; CHECK: Loop:
301; CHECK-NEXT: phi i320
302}
303
304define i160 @test14b(i320 %A, i320 %B, i1 %b1) {
305BB0:
306        %a = trunc i320 %A to i160
307        %b = trunc i320 %B to i160
308        br label %Loop
309
310Loop:
311        %C = phi i160 [ %a, %BB0 ], [ %b, %Loop ]
312        br i1 %b1, label %Loop, label %Exit
313
314Exit:           ; preds = %Loop
315        ret i160 %C
316; CHECK: @test14b
317; CHECK: Loop:
318; CHECK-NEXT: phi i160
319}
320
321declare i64 @test15a(i64)
322
323define i64 @test15b(i64 %A, i1 %b) {
324; CHECK: @test15b
325entry:
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
330; CHECK: entry:
331; CHECK-NEXT: br i1 %b
332
333one:
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
339  br label %two
340
341; CHECK: one:
342; CHECK-NEXT: phi i64
343; CHECK-NEXT: %c = call i64 @test15a
344
345two:
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
352
353; CHECK: two:
354; CHECK-NEXT: phi i64
355; CHECK-NEXT: phi i64
356; CHECK-NEXT: %d = call i64 @test15a
357
358end:
359  %g = trunc i128 %y to i64
360  ret i64 %g
361; CHECK: end:
362; CHECK-NEXT: ret i64
363}
364
365; PR6512 - Shouldn't merge loads from different addr spaces.
366define i32 @test16(i32 addrspace(1)* %pointer1, i32 %flag, i32* %pointer2)
367nounwind {
368entry:
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
381
382return:                                           ; preds = %if.end
383  %tmp7 = load i32* %retval                       ; <i32> [#uses=1]
384  ret i32 %tmp7
385
386if.end:                                           ; preds = %if.else, %if.then
387  %tmp6 = load i32* %res                          ; <i32> [#uses=1]
388  store i32 %tmp6, i32* %retval
389  br label %return
390
391if.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
396  br label %if.end
397
398if.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
403  br label %if.end
404}
405
406; PR4413
407declare i32 @ext()
408; CHECK: @test17
409define i32 @test17(i1 %a) {
410entry:
411    br i1 %a, label %bb1, label %bb2
412
413bb1:        ; preds = %entry
414    %0 = tail call i32 @ext()        ; <i32> [#uses=1]
415    br label %bb2
416
417bb2:        ; 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
423    ret i32 %res
424}
425
426define i1 @test18(i1 %cond) {
427  %zero = alloca i32
428  %one = alloca i32
429  br i1 %cond, label %true, label %false
430true:
431  br label %ret
432false:
433  br label %ret
434ret:
435  %ptr = phi i32* [ %zero, %true ] , [ %one, %false ]
436  %isnull = icmp eq i32* %ptr, null
437  ret i1 %isnull
438; CHECK: @test18
439; CHECK: ret i1 false
440}
441
442define i1 @test19(i1 %cond, double %x) {
443  br i1 %cond, label %true, label %false
444true:
445  br label %ret
446false:
447  br label %ret
448ret:
449  %p = phi double [ %x, %true ], [ 0x7FF0000000000000, %false ]; RHS = +infty
450  %cmp = fcmp ule double %x, %p
451  ret i1 %cmp
452; CHECK: @test19
453; CHECK: ret i1 true
454}
455
456define i1 @test20(i1 %cond) {
457  %a = alloca i32
458  %b = alloca i32
459  %c = alloca i32
460  br i1 %cond, label %true, label %false
461true:
462  br label %ret
463false:
464  br label %ret
465ret:
466  %p = phi i32* [ %a, %true ], [ %b, %false ]
467  %r = icmp eq i32* %p, %c
468  ret i1 %r
469; CHECK: @test20
470; CHECK: ret i1 false
471}
472
473define i1 @test21(i1 %c1, i1 %c2) {
474  %a = alloca i32
475  %b = alloca i32
476  %c = alloca i32
477  br i1 %c1, label %true, label %false
478true:
479  br label %loop
480false:
481  br label %loop
482loop:
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
486ret:
487  ret i1 %r
488; CHECK: @test21
489; CHECK: ret i1 false
490}
491
492define void @test22() {
493; CHECK: @test22
494entry:
495  br label %loop
496loop:
497  %phi = phi i32 [ 0, %entry ], [ %y, %loop ]
498  %y = add i32 %phi, 1
499  %o = or i32 %y, %phi
500  %e = icmp eq i32 %o, %y
501  br i1 %e, label %loop, label %ret
502; CHECK: br i1 %e
503ret:
504  ret void
505}
506
507define i32 @test23(i32 %A, i1 %b, i32 * %P) {
508BB0:
509        br label %Loop
510
511Loop:           ; preds = %Loop, %BB0
512        ; PHI has same value always.
513        %B = phi i32 [ %A, %BB0 ], [ 42, %Loop ]
514        %D = add i32 %B, 19
515        store i32 %D, i32* %P
516        br i1 %b, label %Loop, label %Exit
517
518Exit:           ; preds = %Loop
519        %E = add i32 %B, 19
520        ret i32 %E
521; CHECK: @test23
522; CHECK: %phitmp = add i32 %A, 19
523; CHECK: Loop:
524; CHECK-NEXT: %B = phi i32 [ %phitmp, %BB0 ], [ 61, %Loop ]
525; CHECK: Exit:
526; CHECK-NEXT: ret i32 %B
527}
528
529define i32 @test24(i32 %A, i1 %cond) {
530BB0:
531        %X = add nuw nsw i32 %A, 1
532        br i1 %cond, label %BB1, label %BB2
533
534BB1:
535        %Y = add nuw i32 %A, 1
536        br label %BB2
537
538BB2:
539        %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
540        ret i32 %C
541; CHECK: @test24
542; CHECK-NOT: phi
543; CHECK: BB2:
544; CHECK-NEXT: %C = add nuw i32 %A, 1
545; CHECK-NEXT: ret i32 %C
546}
547
548; Same as test11, but used to be missed due to a bug.
549declare i1 @test25a()
550
551define i1 @test25() {
552entry:
553  %a = alloca i32
554  %i = ptrtoint i32* %a to i64
555  %b = call i1 @test25a()
556  br i1 %b, label %one, label %two
557
558one:
559  %x = phi i64 [%y, %two], [%i, %entry]
560  %c = call i1 @test25a()
561  br i1 %c, label %two, label %end
562
563two:
564  %y = phi i64 [%x, %one], [%i, %entry]
565  %d = call i1 @test25a()
566  br i1 %d, label %one, label %end
567
568end:
569  %f = phi i64 [ %x, %one], [%y, %two]
570  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
571  ; even though %f must equal %i at this point
572  %g = inttoptr i64 %f to i32*
573  store i32 10, i32* %g
574  %z = call i1 @test25a()
575  ret i1 %z
576; CHECK: @test25
577; CHECK-NOT: phi i32
578; CHECK: ret i1 %z
579}
580
581declare i1 @test26a()
582
583define i1 @test26(i32 %n) {
584entry:
585  %a = alloca i32
586  %i = ptrtoint i32* %a to i64
587  %b = call i1 @test26a()
588  br label %one
589
590one:
591  %x = phi i64 [%y, %two], [%w, %three], [%i, %entry]
592  %c = call i1 @test26a()
593  switch i32 %n, label %end [
594          i32 2, label %two
595          i32 3, label %three
596  ]
597
598two:
599  %y = phi i64 [%x, %one], [%w, %three]
600  %d = call i1 @test26a()
601  switch i32 %n, label %end [
602          i32 10, label %one
603          i32 30, label %three
604  ]
605
606three:
607  %w = phi i64 [%y, %two], [%x, %one]
608  %e = call i1 @test26a()
609  br i1 %e, label %one, label %two
610
611end:
612  %f = phi i64 [ %x, %one], [%y, %two]
613  ; Change the %f to %i, and the optimizer suddenly becomes a lot smarter
614  ; even though %f must equal %i at this point
615  %g = inttoptr i64 %f to i32*
616  store i32 10, i32* %g
617  %z = call i1 @test26a()
618  ret i1 %z
619; CHECK: @test26
620; CHECK-NOT: phi i32
621; CHECK: ret i1 %z
622}
623