1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -forward-switch-cond=false -switch-range-to-icmp -S | FileCheck %s --check-prefix=NO_FWD
3 ; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -forward-switch-cond=true -switch-range-to-icmp -S | FileCheck %s --check-prefix=FWD
7 define i32 @forward_multiple(i32 %m) {
8 ; NO_FWD-LABEL: @forward_multiple(
10 ; NO_FWD-NEXT: switch i32 [[M:%.*]], label [[SW_BB4:%.*]] [
11 ; NO_FWD-NEXT: i32 0, label [[RETURN:%.*]]
12 ; NO_FWD-NEXT: i32 1, label [[SW_BB1:%.*]]
13 ; NO_FWD-NEXT: i32 2, label [[SW_BB2:%.*]]
14 ; NO_FWD-NEXT: i32 3, label [[SW_BB3:%.*]]
17 ; NO_FWD-NEXT: br label [[RETURN]]
19 ; NO_FWD-NEXT: br label [[RETURN]]
21 ; NO_FWD-NEXT: br label [[RETURN]]
23 ; NO_FWD-NEXT: br label [[RETURN]]
25 ; NO_FWD-NEXT: [[RETVAL_0:%.*]] = phi i32 [ 4, [[SW_BB4]] ], [ 3, [[SW_BB3]] ], [ 2, [[SW_BB2]] ], [ 1, [[SW_BB1]] ], [ 0, [[ENTRY:%.*]] ]
26 ; NO_FWD-NEXT: ret i32 [[RETVAL_0]]
28 ; FWD-LABEL: @forward_multiple(
30 ; FWD-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[M:%.*]], 4
31 ; FWD-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[SWITCH]], i32 [[M]], i32 4
32 ; FWD-NEXT: ret i32 [[SPEC_SELECT]]
35 switch i32 %m, label %sw.bb4 [
42 sw.bb0: ; preds = %entry
45 sw.bb1: ; preds = %entry
48 sw.bb2: ; preds = %entry
51 sw.bb3: ; preds = %entry
54 sw.bb4: ; preds = %entry
57 return: ; preds = %entry, %sw.bb4, %sw.bb3, %sw.bb2, %sw.bb1
58 %retval.0 = phi i32 [ 4, %sw.bb4 ], [ 3, %sw.bb3 ], [ 2, %sw.bb2 ], [ 1, %sw.bb1 ], [ 0, %sw.bb0 ]
62 ; We should not forward `%m` to 1, as this does not simplify the CFG.
63 define i32 @forward_one(i32 %m) {
64 ; NO_FWD-LABEL: @forward_one(
66 ; NO_FWD-NEXT: switch i32 [[M:%.*]], label [[SW_BB4:%.*]] [
67 ; NO_FWD-NEXT: i32 0, label [[RETURN:%.*]]
68 ; NO_FWD-NEXT: i32 1, label [[SW_BB1:%.*]]
69 ; NO_FWD-NEXT: i32 2, label [[SW_BB2:%.*]]
70 ; NO_FWD-NEXT: i32 3, label [[SW_BB3:%.*]]
73 ; NO_FWD-NEXT: br label [[RETURN]]
75 ; NO_FWD-NEXT: br label [[RETURN]]
77 ; NO_FWD-NEXT: br label [[RETURN]]
79 ; NO_FWD-NEXT: br label [[RETURN]]
81 ; NO_FWD-NEXT: [[RETVAL_0:%.*]] = phi i32 [ 4, [[SW_BB4]] ], [ 5, [[SW_BB3]] ], [ 6, [[SW_BB2]] ], [ 1, [[SW_BB1]] ], [ 8, [[ENTRY:%.*]] ]
82 ; NO_FWD-NEXT: ret i32 [[RETVAL_0]]
84 ; FWD-LABEL: @forward_one(
86 ; FWD-NEXT: switch i32 [[M:%.*]], label [[SW_BB4:%.*]] [
87 ; FWD-NEXT: i32 0, label [[RETURN:%.*]]
88 ; FWD-NEXT: i32 1, label [[SW_BB1:%.*]]
89 ; FWD-NEXT: i32 2, label [[SW_BB2:%.*]]
90 ; FWD-NEXT: i32 3, label [[SW_BB3:%.*]]
93 ; FWD-NEXT: br label [[RETURN]]
95 ; FWD-NEXT: br label [[RETURN]]
97 ; FWD-NEXT: br label [[RETURN]]
99 ; FWD-NEXT: br label [[RETURN]]
101 ; FWD-NEXT: [[RETVAL_0:%.*]] = phi i32 [ 4, [[SW_BB4]] ], [ 5, [[SW_BB3]] ], [ 6, [[SW_BB2]] ], [ 1, [[SW_BB1]] ], [ 8, [[ENTRY:%.*]] ]
102 ; FWD-NEXT: ret i32 [[RETVAL_0]]
105 switch i32 %m, label %sw.bb4 [
112 sw.bb0: ; preds = %entry
115 sw.bb1: ; preds = %entry
118 sw.bb2: ; preds = %entry
121 sw.bb3: ; preds = %entry
124 sw.bb4: ; preds = %entry
127 return: ; preds = %entry, %sw.bb4, %sw.bb3, %sw.bb2, %sw.bb1
128 %retval.0 = phi i32 [ 4, %sw.bb4 ], [ 5, %sw.bb3 ], [ 6, %sw.bb2 ], [ 1, %sw.bb1 ], [ 8, %sw.bb0 ]
132 ; If 1 incoming phi value is a case constant of a switch, convert it to the switch condition:
133 ; https://bugs.llvm.org/show_bug.cgi?id=34471
134 ; This then subsequently should allow squashing of the other trivial case blocks.
136 define i32 @PR34471(i32 %x) {
137 ; NO_FWD-LABEL: @PR34471(
138 ; NO_FWD-NEXT: entry:
139 ; NO_FWD-NEXT: switch i32 [[X:%.*]], label [[ELSE3:%.*]] [
140 ; NO_FWD-NEXT: i32 17, label [[RETURN:%.*]]
141 ; NO_FWD-NEXT: i32 19, label [[IF19:%.*]]
142 ; NO_FWD-NEXT: i32 42, label [[IF42:%.*]]
145 ; NO_FWD-NEXT: br label [[RETURN]]
147 ; NO_FWD-NEXT: br label [[RETURN]]
149 ; NO_FWD-NEXT: br label [[RETURN]]
151 ; NO_FWD-NEXT: [[R:%.*]] = phi i32 [ [[X]], [[IF19]] ], [ [[X]], [[IF42]] ], [ 0, [[ELSE3]] ], [ 17, [[ENTRY:%.*]] ]
152 ; NO_FWD-NEXT: ret i32 [[R]]
154 ; FWD-LABEL: @PR34471(
156 ; FWD-NEXT: switch i32 [[X:%.*]], label [[ELSE3:%.*]] [
157 ; FWD-NEXT: i32 17, label [[RETURN:%.*]]
158 ; FWD-NEXT: i32 19, label [[RETURN]]
159 ; FWD-NEXT: i32 42, label [[RETURN]]
162 ; FWD-NEXT: br label [[RETURN]]
164 ; FWD-NEXT: [[R:%.*]] = phi i32 [ 0, [[ELSE3]] ], [ [[X]], [[ENTRY:%.*]] ], [ [[X]], [[ENTRY]] ], [ [[X]], [[ENTRY]] ]
165 ; FWD-NEXT: ret i32 [[R]]
168 switch i32 %x, label %else3 [
169 i32 17, label %return
184 %r = phi i32 [ %x, %if19 ], [ %x, %if42 ], [ 0, %else3 ], [ 17, %entry ]
188 ; We can replace `[ 1, %bb2 ]` with `[ %arg1, %bb2 ]`.
189 define { i64, i64 } @PR95919(i64 noundef %arg, i64 noundef %arg1) {
190 ; NO_FWD-LABEL: @PR95919(
192 ; NO_FWD-NEXT: switch i64 [[ARG1:%.*]], label [[BB3:%.*]] [
193 ; NO_FWD-NEXT: i64 0, label [[BB5:%.*]]
194 ; NO_FWD-NEXT: i64 1, label [[BB2:%.*]]
197 ; NO_FWD-NEXT: br label [[BB5]]
199 ; NO_FWD-NEXT: [[I:%.*]] = udiv i64 [[ARG:%.*]], [[ARG1]]
200 ; NO_FWD-NEXT: [[I4:%.*]] = shl nuw i64 [[I]], 1
201 ; NO_FWD-NEXT: br label [[BB5]]
203 ; NO_FWD-NEXT: [[I6:%.*]] = phi i64 [ [[I4]], [[BB3]] ], [ [[ARG]], [[BB2]] ], [ undef, [[BB:%.*]] ]
204 ; NO_FWD-NEXT: [[I7:%.*]] = phi i64 [ 1, [[BB3]] ], [ 1, [[BB2]] ], [ [[ARG1]], [[BB]] ]
205 ; NO_FWD-NEXT: [[I8:%.*]] = insertvalue { i64, i64 } poison, i64 [[I7]], 0
206 ; NO_FWD-NEXT: [[I9:%.*]] = insertvalue { i64, i64 } [[I8]], i64 [[I6]], 1
207 ; NO_FWD-NEXT: ret { i64, i64 } [[I9]]
209 ; FWD-LABEL: @PR95919(
211 ; FWD-NEXT: [[SWITCH:%.*]] = icmp ult i64 [[ARG1:%.*]], 2
212 ; FWD-NEXT: br i1 [[SWITCH]], label [[BB5:%.*]], label [[BB3:%.*]]
214 ; FWD-NEXT: [[I:%.*]] = udiv i64 [[ARG:%.*]], [[ARG1]]
215 ; FWD-NEXT: [[I4:%.*]] = shl nuw i64 [[I]], 1
216 ; FWD-NEXT: br label [[BB5]]
218 ; FWD-NEXT: [[I6:%.*]] = phi i64 [ [[I4]], [[BB3]] ], [ [[ARG]], [[BB:%.*]] ]
219 ; FWD-NEXT: [[I7:%.*]] = phi i64 [ 1, [[BB3]] ], [ [[ARG1]], [[BB]] ]
220 ; FWD-NEXT: [[I8:%.*]] = insertvalue { i64, i64 } poison, i64 [[I7]], 0
221 ; FWD-NEXT: [[I9:%.*]] = insertvalue { i64, i64 } [[I8]], i64 [[I6]], 1
222 ; FWD-NEXT: ret { i64, i64 } [[I9]]
225 switch i64 %arg1, label %bb3 [
234 %i = udiv i64 %arg, %arg1
235 %i4 = shl nuw i64 %i, 1
238 bb5: ; preds = %bb3, %bb2, %bb
239 %i6 = phi i64 [ %i4, %bb3 ], [ %arg, %bb2 ], [ undef, %bb ]
240 %i7 = phi i64 [ 1, %bb3 ], [ 1, %bb2 ], [ %arg1, %bb ]
241 %i8 = insertvalue { i64, i64 } poison, i64 %i7, 0
242 %i9 = insertvalue { i64, i64 } %i8, i64 %i6, 1