1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
3 ; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -keep-loops=false -S < %s | FileCheck %s
4 ; RUN: opt -passes='simplifycfg<no-keep-loops>' -S < %s | FileCheck %s
6 ; It's not worthwhile to if-convert one of the phi nodes and leave
7 ; the other behind, because that still requires a branch. If
8 ; SimplifyCFG if-converts one of the phis, it should do both.
10 define i32 @upper_bound(ptr %r, i32 %high, i32 %k) nounwind {
11 ; CHECK-LABEL: define i32 @upper_bound
12 ; CHECK-SAME: (ptr [[R:%.*]], i32 [[HIGH:%.*]], i32 [[K:%.*]]) #[[ATTR0:[0-9]+]] {
14 ; CHECK-NEXT: br label [[WHILE_COND:%.*]]
16 ; CHECK-NEXT: [[HIGH_ADDR_0:%.*]] = phi i32 [ [[HIGH]], [[ENTRY:%.*]] ], [ [[SPEC_SELECT:%.*]], [[WHILE_BODY:%.*]] ]
17 ; CHECK-NEXT: [[LOW_0:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[SPEC_SELECT1:%.*]], [[WHILE_BODY]] ]
18 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[LOW_0]], [[HIGH_ADDR_0]]
19 ; CHECK-NEXT: br i1 [[CMP]], label [[WHILE_BODY]], label [[WHILE_END:%.*]]
21 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LOW_0]], [[HIGH_ADDR_0]]
22 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[ADD]], 2
23 ; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[DIV]] to i64
24 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[R]], i64 [[IDXPROM]]
25 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
26 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[K]], [[TMP0]]
27 ; CHECK-NEXT: [[ADD2:%.*]] = add i32 [[DIV]], 1
28 ; CHECK-NEXT: [[SPEC_SELECT]] = select i1 [[CMP1]], i32 [[DIV]], i32 [[HIGH_ADDR_0]]
29 ; CHECK-NEXT: [[SPEC_SELECT1]] = select i1 [[CMP1]], i32 [[LOW_0]], i32 [[ADD2]]
30 ; CHECK-NEXT: br label [[WHILE_COND]]
32 ; CHECK-NEXT: ret i32 [[LOW_0]]
37 while.cond: ; preds = %if.then, %if.else, %entry
38 %high.addr.0 = phi i32 [ %high, %entry ], [ %div, %if.then ], [ %high.addr.0, %if.else ]
39 %low.0 = phi i32 [ 0, %entry ], [ %low.0, %if.then ], [ %add2, %if.else ]
40 %cmp = icmp ult i32 %low.0, %high.addr.0
41 br i1 %cmp, label %while.body, label %while.end
43 while.body: ; preds = %while.cond
44 %add = add i32 %low.0, %high.addr.0
45 %div = udiv i32 %add, 2
46 %idxprom = zext i32 %div to i64
47 %arrayidx = getelementptr inbounds i32, ptr %r, i64 %idxprom
48 %0 = load i32, ptr %arrayidx
49 %cmp1 = icmp ult i32 %k, %0
50 br i1 %cmp1, label %if.then, label %if.else
52 if.then: ; preds = %while.body
55 if.else: ; preds = %while.body
56 %add2 = add i32 %div, 1
59 while.end: ; preds = %while.cond
63 define i32 @merge0(i1 %c1, i1 %c2, i1 %c3) {
64 ; CHECK-LABEL: define i32 @merge0
65 ; CHECK-SAME: (i1 [[C1:%.*]], i1 [[C2:%.*]], i1 [[C3:%.*]]) {
67 ; CHECK-NEXT: [[DOT:%.*]] = select i1 [[C2]], i32 0, i32 1
68 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[C3]], i32 2, i32 3
69 ; CHECK-NEXT: [[PHI2:%.*]] = select i1 [[C1]], i32 [[DOT]], i32 [[SPEC_SELECT]]
70 ; CHECK-NEXT: ret i32 [[PHI2]]
72 br i1 %c1, label %if1, label %else1
75 br i1 %c2, label %if2, label %else2
82 br i1 %c3, label %j1, label %j2
85 %phi1 = phi i32 [ 0, %if2 ], [ 1, %else2 ], [ 2, %else1 ]
89 %phi2 = phi i32 [ %phi1, %j1 ], [ 3, %else1 ]
93 define i8 @merge1(i8 noundef %arg, i1 %c1, i1 %c2) {
94 ; CHECK-LABEL: define i8 @merge1
95 ; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) {
97 ; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [
98 ; CHECK-NEXT: i8 -123, label [[CASE0:%.*]]
99 ; CHECK-NEXT: i8 66, label [[SUCC:%.*]]
100 ; CHECK-NEXT: i8 123, label [[CASE2:%.*]]
102 ; CHECK: unreachable:
103 ; CHECK-NEXT: unreachable
105 ; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true
106 ; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true
107 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]]
108 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3
109 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[BRMERGE]], i8 [[DOTMUX]], i8 4
110 ; CHECK-NEXT: br label [[SUCC]]
112 ; CHECK-NEXT: br label [[SUCC]]
114 ; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 2, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ], [ [[SPEC_SELECT]], [[CASE0]] ]
115 ; CHECK-NEXT: ret i8 [[PHI2]]
118 switch i8 %arg, label %unreachable [
119 i8 -123, label %case0
128 br i1 %c1, label %CommonPred, label %BB
137 br i1 %c2, label %Succ, label %BB
140 %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred]
144 %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
148 define i8 @merge1_unfoldable_one_block(i8 noundef %arg, i1 %c1, i1 %c2) {
149 ; CHECK-LABEL: define i8 @merge1_unfoldable_one_block
150 ; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) {
152 ; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [
153 ; CHECK-NEXT: i8 -123, label [[CASE0:%.*]]
154 ; CHECK-NEXT: i8 66, label [[SUCC:%.*]]
155 ; CHECK-NEXT: i8 123, label [[CASE2:%.*]]
157 ; CHECK: unreachable:
158 ; CHECK-NEXT: unreachable
160 ; CHECK-NEXT: call void @dummy()
161 ; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true
162 ; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true
163 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]]
164 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3
165 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[BRMERGE]], i8 [[DOTMUX]], i8 4
166 ; CHECK-NEXT: br label [[SUCC]]
168 ; CHECK-NEXT: br label [[SUCC]]
170 ; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 2, [[CASE2]] ], [ 1, [[ENTRY:%.*]] ], [ [[SPEC_SELECT]], [[CASE0]] ]
171 ; CHECK-NEXT: ret i8 [[PHI2]]
174 switch i8 %arg, label %unreachable [
175 i8 -123, label %case0
185 br i1 %c1, label %CommonPred, label %BB
194 br i1 %c2, label %Succ, label %BB
197 %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred]
201 %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
205 define i8 @merge1_unfoldable_two_block(i8 noundef %arg, i1 %c1, i1 %c2) {
206 ; CHECK-LABEL: define i8 @merge1_unfoldable_two_block
207 ; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) {
209 ; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [
210 ; CHECK-NEXT: i8 -123, label [[CASE0:%.*]]
211 ; CHECK-NEXT: i8 66, label [[CASE1:%.*]]
212 ; CHECK-NEXT: i8 123, label [[SUCC:%.*]]
214 ; CHECK: unreachable:
215 ; CHECK-NEXT: unreachable
217 ; CHECK-NEXT: call void @dummy()
218 ; CHECK-NEXT: [[C1_NOT:%.*]] = xor i1 [[C1]], true
219 ; CHECK-NEXT: [[C2_NOT:%.*]] = xor i1 [[C2]], true
220 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[C1_NOT]], i1 true, i1 [[C2_NOT]]
221 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[C1_NOT]], i8 0, i8 3
222 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[BRMERGE]], i8 [[DOTMUX]], i8 4
223 ; CHECK-NEXT: br label [[SUCC]]
225 ; CHECK-NEXT: call void @dummy()
226 ; CHECK-NEXT: br label [[SUCC]]
228 ; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 1, [[CASE1]] ], [ 2, [[ENTRY:%.*]] ], [ [[SPEC_SELECT]], [[CASE0]] ]
229 ; CHECK-NEXT: ret i8 [[PHI2]]
232 switch i8 %arg, label %unreachable [
233 i8 -123, label %case0
243 br i1 %c1, label %CommonPred, label %BB
253 br i1 %c2, label %Succ, label %BB
256 %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred]
260 %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
264 define i8 @merge1_unfoldable_all_block(i8 noundef %arg, i1 %c1, i1 %c2) {
265 ; CHECK-LABEL: define i8 @merge1_unfoldable_all_block
266 ; CHECK-SAME: (i8 noundef [[ARG:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]]) {
268 ; CHECK-NEXT: switch i8 [[ARG]], label [[UNREACHABLE:%.*]] [
269 ; CHECK-NEXT: i8 -123, label [[CASE0:%.*]]
270 ; CHECK-NEXT: i8 66, label [[CASE1:%.*]]
271 ; CHECK-NEXT: i8 123, label [[CASE2:%.*]]
273 ; CHECK: unreachable:
274 ; CHECK-NEXT: unreachable
276 ; CHECK-NEXT: call void @dummy()
277 ; CHECK-NEXT: br i1 [[C1]], label [[COMMONPRED:%.*]], label [[SUCC:%.*]]
279 ; CHECK-NEXT: call void @dummy()
280 ; CHECK-NEXT: br label [[SUCC]]
282 ; CHECK-NEXT: call void @dummy()
283 ; CHECK-NEXT: br label [[SUCC]]
285 ; CHECK-NEXT: call void @dummy()
286 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[C2]], i8 4, i8 3
287 ; CHECK-NEXT: br label [[SUCC]]
289 ; CHECK-NEXT: [[PHI2:%.*]] = phi i8 [ 0, [[CASE0]] ], [ 1, [[CASE1]] ], [ 2, [[CASE2]] ], [ [[SPEC_SELECT]], [[COMMONPRED]] ]
290 ; CHECK-NEXT: ret i8 [[PHI2]]
293 switch i8 %arg, label %unreachable [
294 i8 -123, label %case0
304 br i1 %c1, label %CommonPred, label %BB
316 br i1 %c2, label %Succ, label %BB
319 %phi1 = phi i8 [ 0, %case0 ], [1, %case1],[2, %case2],[3,%CommonPred]
323 %phi2 = phi i8 [ %phi1, %BB ], [ 4, %CommonPred ]
327 declare void @dummy()