• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -S -instcombine | FileCheck %s
2@A = extern_weak global i32, align 4
3@B = extern_weak global i32, align 4
4
5define i32 @foo(i1 %which) {
6entry:
7  br i1 %which, label %final, label %delay
8
9delay:
10  br label %final
11
12; CHECK-LABEL: @foo
13; CHECK-LABEL: final:
14; CHECK: phi i32 [ 1, %entry ], [ select (i1 icmp eq (i32* @A, i32* @B), i32 2, i32 1), %delay ]
15final:
16  %use2 = phi i1 [ false, %entry ], [ icmp eq (i32* @A, i32* @B), %delay ]
17  %value = select i1 %use2, i32 2, i32 1
18  ret i32 %value
19}
20
21
22; test folding of select into phi for vectors.
23define <4 x i64> @vec1(i1 %which) {
24entry:
25  br i1 %which, label %final, label %delay
26
27delay:
28 br label %final
29
30final:
31; CHECK-LABEL: @vec1
32; CHECK-LABEL: final:
33; CHECK: %phinode = phi <4 x i64> [ zeroinitializer, %entry ], [ <i64 0, i64 0, i64 126, i64 127>, %delay ]
34; CHECK-NOT: select
35; CHECK: ret <4 x i64> %phinode
36 %phinode =  phi <4 x i1> [ <i1 true, i1 true, i1 true, i1 true>, %entry ], [ <i1 true, i1 true, i1 false, i1 false>, %delay ]
37 %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127>
38 ret <4 x i64> %sel
39}
40
41define <4 x i64> @vec2(i1 %which) {
42entry:
43  br i1 %which, label %final, label %delay
44
45delay:
46 br label %final
47
48final:
49; CHECK-LABEL: @vec2
50; CHECK-LABEL: final:
51; CHECK: %phinode = phi <4 x i64> [ <i64 124, i64 125, i64 126, i64 127>, %entry ], [ <i64 0, i64 125, i64 0, i64 127>, %delay ]
52; CHECK-NOT: select
53; CHECK: ret <4 x i64> %phinode
54 %phinode =  phi <4 x i1> [ <i1 false, i1 false, i1 false, i1 false>, %entry ], [ <i1 true, i1 false, i1 true, i1 false>, %delay ]
55 %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127>
56 ret <4 x i64> %sel
57}
58
59; Test PR33364
60; Insert the generated select into the same block as the incoming phi value.
61; phi has constant vectors along with a single non-constant vector as operands.
62define <2 x i8> @vec3(i1 %cond1, i1 %cond2, <2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {
63; CHECK-LABEL: @vec3
64; CHECK: entry:
65; CHECK-NEXT: [[PHITMP1:%.*]] = shufflevector <2 x i8> %y, <2 x i8> %z, <2 x i32> <i32 2, i32 1>
66entry:
67  br i1 %cond1, label %if1, label %else
68
69; CHECK: if1:
70; CHECK-NEXT: [[PHITMP2:%.*]] = shufflevector <2 x i8> %y, <2 x i8> %z, <2 x i32> <i32 0, i32 3>
71if1:
72  br i1 %cond2, label %if2, label %else
73
74; CHECK: if2:
75; CHECK-NEXT: [[PHITMP3:%.*]] = select <2 x i1> %x, <2 x i8> %y, <2 x i8> %z
76if2:
77  br label %else
78
79; CHECK: else:
80; CHECK-NEXT: [[PHITMP4:%.*]] = phi <2 x i8> [ [[PHITMP3]], %if2 ], [ [[PHITMP1]], %entry ], [ [[PHITMP2]], %if1 ]
81; CHECK-NEXT: ret <2 x i8> [[PHITMP4]]
82else:
83  %phi = phi <2 x i1> [ %x, %if2 ], [ <i1 0, i1 1>, %entry ], [ <i1 1, i1 0>, %if1 ]
84  %sel = select <2 x i1> %phi, <2 x i8> %y, <2 x i8> %z
85  ret <2 x i8> %sel
86}
87