1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
6 ; Check that we fold the condition of branches of the
7 ; form: br <condition> dest1, dest2, where dest1 == dest2.
8 define i32 @test(i32 %x) {
11 ; CHECK-NEXT: br i1 false, label [[MERGE:%.*]], label [[MERGE]]
13 ; CHECK-NEXT: ret i32 [[X:%.*]]
16 %cmp = icmp ult i32 %x, 7
17 br i1 %cmp, label %merge, label %merge
24 define i32 @pat(i32 %x) {
26 ; CHECK-NEXT: br i1 false, label [[PATATINO:%.*]], label [[PATATINO]]
28 ; CHECK-NEXT: ret i32 [[X:%.*]]
30 %y = icmp eq i32 27, ptrtoint(ptr @global to i32)
31 br i1 %y, label %patatino, label %patatino
36 define i1 @test01(i1 %cond) {
37 ; CHECK-LABEL: @test01(
39 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_FALSE_1:%.*]]
41 ; CHECK-NEXT: br label [[MERGE_1:%.*]]
43 ; CHECK-NEXT: br label [[MERGE_1]]
45 ; CHECK-NEXT: br i1 [[COND]], label [[IF_TRUE_2:%.*]], label [[IF_FALSE_2:%.*]]
47 ; CHECK-NEXT: br label [[MERGE_2:%.*]]
49 ; CHECK-NEXT: br label [[MERGE_2]]
51 ; CHECK-NEXT: ret i1 [[COND]]
54 br i1 %cond, label %if.true.1, label %if.false.1
63 %merge.cond.1 = phi i1 [true, %if.true.1], [false, %if.false.1]
64 br i1 %merge.cond.1, label %if.true.2, label %if.false.2
73 %merge.cond.2 = phi i1 [true, %if.true.2], [false, %if.false.2]
77 define i1 @test02(i1 %cond) {
78 ; CHECK-LABEL: @test02(
80 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_FALSE_1:%.*]]
82 ; CHECK-NEXT: br label [[MERGE_1:%.*]]
84 ; CHECK-NEXT: br label [[MERGE_1]]
86 ; CHECK-NEXT: br i1 [[COND]], label [[IF_FALSE_2:%.*]], label [[IF_TRUE_2:%.*]]
88 ; CHECK-NEXT: br label [[MERGE_2:%.*]]
90 ; CHECK-NEXT: br label [[MERGE_2]]
92 ; CHECK-NEXT: ret i1 [[COND]]
95 br i1 %cond, label %if.true.1, label %if.false.1
104 %merge.cond.1 = phi i1 [false, %if.true.1], [true, %if.false.1]
105 br i1 %merge.cond.1, label %if.true.2, label %if.false.2
114 %merge.cond.2 = phi i1 [false, %if.true.2], [true, %if.false.2]
118 ; if (x && !y) ret 42; ret 3 --> if (!x || y) ret 3; ret 42
120 define i32 @logical_and_not(i1 %x, i1 %y) {
121 ; CHECK-LABEL: @logical_and_not(
123 ; CHECK-NEXT: [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
124 ; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]]
125 ; CHECK-NEXT: br i1 [[TMP0]], label [[F:%.*]], label [[T:%.*]]
127 ; CHECK-NEXT: ret i32 42
129 ; CHECK-NEXT: ret i32 3
132 %noty = xor i1 %y, true
133 %and = select i1 %x, i1 %noty, i1 false
134 br i1 %and, label %t, label %f
143 ; if (x && y || !x) ret 3; ret 42 --> if (!x || y) ret 3; ret 42
145 define i32 @logical_and_or(i1 %x, i1 %y) {
146 ; CHECK-LABEL: @logical_and_or(
148 ; CHECK-NEXT: [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
149 ; CHECK-NEXT: [[AND:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]]
150 ; CHECK-NEXT: br i1 [[AND]], label [[F:%.*]], label [[T:%.*]]
152 ; CHECK-NEXT: ret i32 42
154 ; CHECK-NEXT: ret i32 3
157 %and = select i1 %x, i1 %y, i1 true
158 br i1 %and, label %f, label %t
167 ; if (!x || y) ret 3; ret 42
169 define i32 @logical_or_not(i1 %x, i1 %y) {
170 ; CHECK-LABEL: @logical_or_not(
172 ; CHECK-NEXT: [[NOTX:%.*]] = xor i1 [[X:%.*]], true
173 ; CHECK-NEXT: [[AND:%.*]] = select i1 [[NOTX]], i1 true, i1 [[Y:%.*]]
174 ; CHECK-NEXT: br i1 [[AND]], label [[F:%.*]], label [[T:%.*]]
176 ; CHECK-NEXT: ret i32 42
178 ; CHECK-NEXT: ret i32 3
181 %notx = xor i1 %x, true
182 %and = select i1 %notx, i1 true, i1 %y
183 br i1 %and, label %f, label %t
194 define i32 @logical_and_not_use1(i1 %x, i1 %y) {
195 ; CHECK-LABEL: @logical_and_not_use1(
197 ; CHECK-NEXT: [[NOTY:%.*]] = xor i1 [[Y:%.*]], true
198 ; CHECK-NEXT: call void @use(i1 [[NOTY]])
199 ; CHECK-NEXT: [[AND:%.*]] = select i1 [[X:%.*]], i1 [[NOTY]], i1 false
200 ; CHECK-NEXT: br i1 [[AND]], label [[T:%.*]], label [[F:%.*]]
202 ; CHECK-NEXT: ret i32 42
204 ; CHECK-NEXT: ret i32 3
207 %noty = xor i1 %y, true
208 call void @use(i1 %noty)
209 %and = select i1 %x, i1 %noty, i1 false
210 br i1 %and, label %t, label %f
221 define i32 @logical_and_not_use2(i1 %x, i1 %y) {
222 ; CHECK-LABEL: @logical_and_not_use2(
224 ; CHECK-NEXT: [[NOTY:%.*]] = xor i1 [[Y:%.*]], true
225 ; CHECK-NEXT: [[AND:%.*]] = select i1 [[X:%.*]], i1 [[NOTY]], i1 false
226 ; CHECK-NEXT: call void @use(i1 [[AND]])
227 ; CHECK-NEXT: br i1 [[AND]], label [[T:%.*]], label [[F:%.*]]
229 ; CHECK-NEXT: ret i32 42
231 ; CHECK-NEXT: ret i32 3
234 %noty = xor i1 %y, true
235 %and = select i1 %x, i1 %noty, i1 false
236 call void @use(i1 %and)
237 br i1 %and, label %t, label %f
246 define i32 @dom_true(i1 %cmp) {
247 ; CHECK-LABEL: @dom_true(
248 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
250 ; CHECK-NEXT: ret i32 1
252 ; CHECK-NEXT: ret i32 0
254 br i1 %cmp, label %if.then, label %if.else
257 %zext = zext i1 %cmp to i32
264 define i32 @dom_false(i1 %cmp) {
265 ; CHECK-LABEL: @dom_false(
266 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
268 ; CHECK-NEXT: ret i32 0
270 ; CHECK-NEXT: ret i32 0
272 br i1 %cmp, label %if.else, label %if.then
275 %zext = zext i1 %cmp to i32
282 define i32 @dom_true_phi(i1 %cmp) {
283 ; CHECK-LABEL: @dom_true_phi(
284 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
286 ; CHECK-NEXT: br label [[IF_END:%.*]]
288 ; CHECK-NEXT: br label [[IF_END]]
290 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i32
291 ; CHECK-NEXT: ret i32 [[ZEXT]]
293 br i1 %cmp, label %if.then, label %if.else
302 %phi = phi i1 [ true, %if.then ], [ %cmp, %if.else ]
303 %zext = zext i1 %phi to i32
309 define i32 @same_dest(i1 %cmp) {
310 ; CHECK-LABEL: @same_dest(
311 ; CHECK-NEXT: br i1 false, label [[IF_THEN:%.*]], label [[IF_THEN]]
313 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP:%.*]] to i32
314 ; CHECK-NEXT: ret i32 [[ZEXT]]
316 br i1 %cmp, label %if.then, label %if.then
319 %zext = zext i1 %cmp to i32
323 define i32 @not_dom(i1 %cmp) {
324 ; CHECK-LABEL: @not_dom(
325 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
327 ; CHECK-NEXT: br label [[IF_ELSE]]
329 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 [[CMP]] to i32
330 ; CHECK-NEXT: ret i32 [[ZEXT]]
332 br i1 %cmp, label %if.then, label %if.else
338 %zext = zext i1 %cmp to i32