1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -passes=instcombine | FileCheck %s
3 @A = extern_weak global i32, align 4
4 @B = extern_weak global i32, align 4
6 define i32 @foo(i1 %which) {
9 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
11 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr @A, @B
12 ; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[CMP]], i32 2, i32 1
13 ; CHECK-NEXT: br label [[FINAL]]
15 ; CHECK-NEXT: [[USE2:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[TMP0]], [[DELAY]] ]
16 ; CHECK-NEXT: ret i32 [[USE2]]
19 br i1 %which, label %final, label %delay
22 %cmp = icmp eq ptr @A, @B
26 %use2 = phi i1 [ false, %entry ], [ %cmp, %delay ]
27 %value = select i1 %use2, i32 2, i32 1
32 ; test folding of select into phi for vectors.
33 define <4 x i64> @vec1(i1 %which) {
36 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
38 ; CHECK-NEXT: br label [[FINAL]]
40 ; CHECK-NEXT: [[PHINODE:%.*]] = phi <4 x i64> [ zeroinitializer, [[ENTRY:%.*]] ], [ <i64 0, i64 0, i64 126, i64 127>, [[DELAY]] ]
41 ; CHECK-NEXT: ret <4 x i64> [[PHINODE]]
44 br i1 %which, label %final, label %delay
50 %phinode = phi <4 x i1> [ <i1 true, i1 true, i1 true, i1 true>, %entry ], [ <i1 true, i1 true, i1 false, i1 false>, %delay ]
51 %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127>
55 define <4 x i64> @vec2(i1 %which) {
58 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
60 ; CHECK-NEXT: br label [[FINAL]]
62 ; CHECK-NEXT: [[PHINODE:%.*]] = phi <4 x i64> [ <i64 124, i64 125, i64 126, i64 127>, [[ENTRY:%.*]] ], [ <i64 0, i64 125, i64 0, i64 127>, [[DELAY]] ]
63 ; CHECK-NEXT: ret <4 x i64> [[PHINODE]]
66 br i1 %which, label %final, label %delay
72 %phinode = phi <4 x i1> [ <i1 false, i1 false, i1 false, i1 false>, %entry ], [ <i1 true, i1 false, i1 true, i1 false>, %delay ]
73 %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127>
78 ; Insert the generated select into the same block as the incoming phi value.
79 ; phi has constant vectors along with a single non-constant vector as operands.
80 define <2 x i8> @vec3(i1 %cond1, i1 %cond2, <2 x i1> %x, <2 x i8> %y, <2 x i8> %z) {
83 ; CHECK-NEXT: br i1 [[COND1:%.*]], label [[IF1:%.*]], label [[ELSE:%.*]]
85 ; CHECK-NEXT: br i1 [[COND2:%.*]], label [[IF2:%.*]], label [[ELSE]]
87 ; CHECK-NEXT: br label [[ELSE]]
89 ; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i1> [ [[X:%.*]], [[IF2]] ], [ <i1 false, i1 true>, [[ENTRY:%.*]] ], [ <i1 true, i1 false>, [[IF1]] ]
90 ; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[PHI]], <2 x i8> [[Y:%.*]], <2 x i8> [[Z:%.*]]
91 ; CHECK-NEXT: ret <2 x i8> [[SEL]]
94 br i1 %cond1, label %if1, label %else
97 br i1 %cond2, label %if2, label %else
103 %phi = phi <2 x i1> [ %x, %if2 ], [ <i1 0, i1 1>, %entry ], [ <i1 1, i1 0>, %if1 ]
104 %sel = select <2 x i1> %phi, <2 x i8> %y, <2 x i8> %z
108 ; Don't crash on unreachable IR.
110 define void @PR48369(i32 %a, ptr %p) {
111 ; CHECK-LABEL: @PR48369(
113 ; CHECK-NEXT: [[PHI_CMP:%.*]] = icmp sgt i32 [[A:%.*]], 0
114 ; CHECK-NEXT: br label [[BB1:%.*]]
116 ; CHECK-NEXT: [[CMP:%.*]] = phi i1 [ [[PHI_CMP]], [[DEADBB:%.*]] ], [ true, [[ENTRY:%.*]] ]
117 ; CHECK-NEXT: [[SHL:%.*]] = select i1 [[CMP]], i32 256, i32 0
118 ; CHECK-NEXT: store i32 [[SHL]], ptr [[P:%.*]], align 4
119 ; CHECK-NEXT: br label [[END:%.*]]
121 ; CHECK-NEXT: br label [[BB1]]
123 ; CHECK-NEXT: ret void
126 %phi.cmp = icmp sgt i32 %a, 0
130 %cmp = phi i1 [ %phi.cmp, %deadbb ], [ true, %entry ]
131 %shl = select i1 %cmp, i32 256, i32 0
132 store i32 %shl, ptr %p
142 define i16 @sink_to_unreachable_crash(i1 %a) {
143 ; CHECK-LABEL: @sink_to_unreachable_crash(
145 ; CHECK-NEXT: br label [[INF_LOOP:%.*]]
147 ; CHECK-NEXT: br label [[INF_LOOP]]
148 ; CHECK: unreachable:
149 ; CHECK-NEXT: ret i16 poison
152 %s = select i1 %a, i16 0, i16 5
158 unreachable: ; No predecessors!
162 define i32 @phi_trans(i1 %c, i1 %c2, i32 %v) {
163 ; CHECK-LABEL: @phi_trans(
165 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
167 ; CHECK-NEXT: [[V2:%.*]] = add i32 [[V:%.*]], 1
168 ; CHECK-NEXT: br label [[JOIN:%.*]]
170 ; CHECK-NEXT: [[V3:%.*]] = mul i32 [[V]], 3
171 ; CHECK-NEXT: [[V5:%.*]] = lshr i32 [[V]], 1
172 ; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[C2:%.*]], i32 [[V3]], i32 [[V5]]
173 ; CHECK-NEXT: br label [[JOIN]]
175 ; CHECK-NEXT: [[PHI1:%.*]] = phi i32 [ [[V2]], [[IF]] ], [ [[TMP0]], [[ELSE]] ]
176 ; CHECK-NEXT: ret i32 [[PHI1]]
179 br i1 %c, label %if, label %else
192 %phi1 = phi i1 [ true, %if ], [ %c2, %else ]
193 %phi2 = phi i32 [ %v2, %if ], [ %v3, %else ]
194 %phi3 = phi i32 [ %v4, %if ], [ %v5, %else ]
195 %sel = select i1 %phi1, i32 %phi2, i32 %phi3
199 define i32 @dominating_values_select_same_block(i1 %c1, i1 %c2, ptr %p, ptr %p2) {
200 ; CHECK-LABEL: @dominating_values_select_same_block(
202 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr [[P2:%.*]], align 4
203 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
205 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[P:%.*]], align 4
206 ; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[C2:%.*]], i32 [[A]], i32 [[B]]
207 ; CHECK-NEXT: br label [[FINAL]]
209 ; CHECK-NEXT: [[USE2:%.*]] = phi i32 [ [[B]], [[ENTRY:%.*]] ], [ [[TMP0]], [[DELAY]] ]
210 ; CHECK-NEXT: ret i32 [[USE2]]
213 %a = load i32, ptr %p
214 %b = load i32, ptr %p2
215 br i1 %c1, label %final, label %delay
221 %use2 = phi i1 [ false, %entry ], [ %c2, %delay ]
222 %value = select i1 %use2, i32 %a, i32 %b
226 define i32 @dominating_values_select_not_same_block(i1 %c1, i1 %c2, ptr %p, ptr %p2) {
227 ; CHECK-LABEL: @dominating_values_select_not_same_block(
229 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr [[P2:%.*]], align 4
230 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
232 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[P:%.*]], align 4
233 ; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[C2:%.*]], i32 [[A]], i32 [[B]]
234 ; CHECK-NEXT: br label [[FINAL]]
236 ; CHECK-NEXT: [[USE2:%.*]] = phi i32 [ [[B]], [[ENTRY:%.*]] ], [ [[TMP0]], [[DELAY]] ]
237 ; CHECK-NEXT: br label [[SPLIT:%.*]]
239 ; CHECK-NEXT: ret i32 [[USE2]]
242 %a = load i32, ptr %p
243 %b = load i32, ptr %p2
244 br i1 %c1, label %final, label %delay
250 %use2 = phi i1 [ false, %entry ], [ %c2, %delay ]
254 %value = select i1 %use2, i32 %a, i32 %b
258 define i32 @not_dominating_values(i1 %c1, i1 %c2, ptr %p, ptr %p2) {
259 ; CHECK-LABEL: @not_dominating_values(
261 ; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[P:%.*]], align 4
262 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
264 ; CHECK-NEXT: br label [[FINAL]]
266 ; CHECK-NEXT: [[USE2:%.*]] = phi i1 [ false, [[ENTRY:%.*]] ], [ [[C2:%.*]], [[DELAY]] ]
267 ; CHECK-NEXT: [[B:%.*]] = load i32, ptr [[P2:%.*]], align 4
268 ; CHECK-NEXT: [[VALUE:%.*]] = select i1 [[USE2]], i32 [[A]], i32 [[B]]
269 ; CHECK-NEXT: ret i32 [[VALUE]]
272 %a = load i32, ptr %p
273 br i1 %c1, label %final, label %delay
279 %use2 = phi i1 [ false, %entry ], [ %c2, %delay ]
280 %b = load i32, ptr %p2
281 %value = select i1 %use2, i32 %a, i32 %b