1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; Test merging of blocks with phi nodes.
4 ; RUN: opt < %s -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp -S | FileCheck %s
12 define i32 @test(i1 %a) {
15 ; CHECK-NEXT: [[R:%.*]] = add i32 2, 1
16 ; CHECK-NEXT: ret i32 [[R]]
19 br i1 %a, label %N, label %M
23 ; It's ok to merge N and M because the incoming values for W are the
24 ; same for both cases...
25 %W = phi i32 [ 2, %N ], [ 2, %Q ] ; <i32> [#uses=1]
26 %R = add i32 %W, 1 ; <i32> [#uses=1]
30 ; Test merging of blocks with phi nodes where at least one incoming value
31 ; in the successor is undef.
32 define i8 @testundef(i32 %u) {
33 ; CHECK-LABEL: @testundef(
35 ; CHECK-NEXT: [[U_OFF:%.*]] = add i32 [[U:%.*]], -1
36 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[U_OFF]], 2
37 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[SWITCH]], i8 1, i8 0
38 ; CHECK-NEXT: ret i8 [[SPEC_SELECT]]
41 switch i32 %u, label %U [
53 U: ; preds = %T, %S, %R
54 ; We should be able to merge either the S or T block into U by rewriting
55 ; R's incoming value with the incoming value of that predecessor since
56 ; R's incoming value is undef and both of those predecessors are simple
57 ; unconditional branches.
58 %val.0 = phi i8 [ undef, %R ], [ 1, %T ], [ 0, %S ]
62 ; Test merging of blocks with phi nodes where at least one incoming value
63 ; in the successor is undef.
64 define i8 @testundef2(i32 %u, i32* %A) {
65 ; CHECK-LABEL: @testundef2(
67 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[U:%.*]], 3
68 ; CHECK-NEXT: br i1 [[COND]], label [[Z:%.*]], label [[U:%.*]]
70 ; CHECK-NEXT: store i32 0, i32* [[A:%.*]], align 4
71 ; CHECK-NEXT: br label [[U]]
73 ; CHECK-NEXT: ret i8 1
76 switch i32 %u, label %U [
87 store i32 0, i32* %A, align 4
90 X: ; preds = %V, %V, %Z
93 U: ; preds = %X, %W, %V
94 ; We should be able to merge either the W or X block into U by rewriting
95 ; V's incoming value with the incoming value of that predecessor since
96 ; V's incoming value is undef and both of those predecessors are simple
97 ; unconditional branches. Note that X has predecessors beyond
98 ; the direct predecessors of U.
99 %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 1, %W ]
103 define i8 @testmergesome(i32 %u, i32* %A) {
104 ; CHECK-LABEL: @testmergesome(
106 ; CHECK-NEXT: switch i32 [[U:%.*]], label [[Y:%.*]] [
107 ; CHECK-NEXT: i32 0, label [[W:%.*]]
108 ; CHECK-NEXT: i32 3, label [[Z:%.*]]
111 ; CHECK-NEXT: store i32 1, i32* [[A:%.*]], align 4
112 ; CHECK-NEXT: br label [[Y]]
114 ; CHECK-NEXT: store i32 0, i32* [[A]], align 4
115 ; CHECK-NEXT: br label [[Y]]
117 ; CHECK-NEXT: [[VAL_0:%.*]] = phi i8 [ 2, [[W]] ], [ 1, [[Z]] ], [ 1, [[V:%.*]] ]
118 ; CHECK-NEXT: ret i8 [[VAL_0]]
121 switch i32 %u, label %Y [
129 store i32 1, i32* %A, align 4
133 store i32 0, i32* %A, align 4
139 Y: ; preds = %X, %W, %V
140 ; After merging X into Y, we should have 5 predecessors
141 ; and thus 5 incoming values to the phi.
142 %val.0 = phi i8 [ 1, %V ], [ 1, %X ], [ 2, %W ]
147 define i8 @testmergesome2(i32 %u, i32* %A) {
148 ; CHECK-LABEL: @testmergesome2(
150 ; CHECK-NEXT: switch i32 [[U:%.*]], label [[W:%.*]] [
151 ; CHECK-NEXT: i32 4, label [[Y:%.*]]
152 ; CHECK-NEXT: i32 1, label [[Y]]
153 ; CHECK-NEXT: i32 2, label [[Y]]
156 ; CHECK-NEXT: store i32 1, i32* [[A:%.*]], align 4
157 ; CHECK-NEXT: br label [[Y]]
159 ; CHECK-NEXT: [[VAL_0:%.*]] = phi i8 [ 1, [[V:%.*]] ], [ 2, [[W]] ], [ 1, [[V]] ], [ 1, [[V]] ]
160 ; CHECK-NEXT: ret i8 [[VAL_0]]
163 switch i32 %u, label %W [
171 store i32 1, i32* %A, align 4
177 Y: ; preds = %X, %W, %V
178 ; Ensure that we deal with both undef inputs for V when we merge in X.
179 %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 2, %W ], [ undef, %V ]
183 ; This function can't be merged
187 ; CHECK-NEXT: br label [[BB_NOMERGE:%.*]]
189 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ 0, [[COMMON:%.*]] ]
190 ; CHECK-NEXT: br label [[SUCC:%.*]]
192 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[A]], [[BB_NOMERGE]] ], [ 2, [[COMMON]] ]
193 ; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
194 ; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[EXIT:%.*]]
196 ; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
197 ; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
199 ; CHECK-NEXT: ret void
204 BB.nomerge: ; preds = %Common, %entry
205 ; This phi has a conflicting value (0) with below phi (2), so blocks
207 %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
210 Succ: ; preds = %Common, %BB.nomerge
211 %b = phi i32 [ %a, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
212 %conde = call i1 @foo( ) ; <i1> [#uses=1]
213 br i1 %conde, label %Common, label %Exit
215 Common: ; preds = %Succ
216 %cond = call i1 @foo( ) ; <i1> [#uses=1]
217 br i1 %cond, label %BB.nomerge, label %Succ
219 Exit: ; preds = %Succ
223 ; This function can't be merged
227 ; CHECK-NEXT: br label [[BB_NOMERGE:%.*]]
229 ; CHECK-NEXT: br label [[SUCC:%.*]]
231 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ 1, [[BB_NOMERGE]] ], [ 2, [[COMMON:%.*]] ]
232 ; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
233 ; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[EXIT:%.*]]
235 ; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
236 ; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
238 ; CHECK-NEXT: ret void
243 BB.nomerge: ; preds = %Common, %entry
246 Succ: ; preds = %Common, %BB.nomerge
247 ; This phi has confliction values for Common and (through BB) Common,
248 ; blocks can't be merged
249 %b = phi i32 [ 1, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
250 %conde = call i1 @foo( ) ; <i1> [#uses=1]
251 br i1 %conde, label %Common, label %Exit
253 Common: ; preds = %Succ
254 %cond = call i1 @foo( ) ; <i1> [#uses=1]
255 br i1 %cond, label %BB.nomerge, label %Succ
257 Exit: ; preds = %Succ
261 ; This function can't be merged (for keeping canonical loop structures)
265 ; CHECK-NEXT: br label [[BB_NOMERGE:%.*]]
267 ; CHECK-NEXT: br label [[SUCC:%.*]]
269 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ 1, [[BB_NOMERGE]] ], [ 1, [[COMMON:%.*]] ], [ 2, [[PRE_EXIT:%.*]] ]
270 ; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
271 ; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[PRE_EXIT]]
273 ; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
274 ; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
276 ; CHECK-NEXT: [[COND2:%.*]] = call i1 @foo()
277 ; CHECK-NEXT: br i1 [[COND2]], label [[SUCC]], label [[EXIT:%.*]]
279 ; CHECK-NEXT: ret void
284 BB.nomerge: ; preds = %Common, %entry
287 Succ: ; preds = %Common, %BB.tomerge, %Pre-Exit
288 ; This phi has identical values for Common and (through BB) Common,
289 ; blocks can't be merged
290 %b = phi i32 [ 1, %BB.nomerge ], [ 1, %Common ], [ 2, %Pre-Exit ]
291 %conde = call i1 @foo( ) ; <i1> [#uses=1]
292 br i1 %conde, label %Common, label %Pre-Exit
294 Common: ; preds = %Succ
295 %cond = call i1 @foo( ) ; <i1> [#uses=1]
296 br i1 %cond, label %BB.nomerge, label %Succ
298 Pre-Exit: ; preds = %Succ
299 ; This adds a backedge, so the %b phi node gets a third branch and is
300 ; not completely trivial
301 %cond2 = call i1 @foo( ) ; <i1> [#uses=1]
302 br i1 %cond2, label %Succ, label %Exit
304 Exit: ; preds = %Pre-Exit
308 ; This function can't be merged (for keeping canonical loop structures)
312 ; CHECK-NEXT: br label [[BB_NOMERGE:%.*]]
314 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ 0, [[COMMON:%.*]] ]
315 ; CHECK-NEXT: br label [[SUCC:%.*]]
317 ; CHECK-NEXT: [[B:%.*]] = phi i32 [ [[A]], [[BB_NOMERGE]] ], [ 0, [[COMMON]] ]
318 ; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
319 ; CHECK-NEXT: br i1 [[CONDE]], label [[COMMON]], label [[EXIT:%.*]]
321 ; CHECK-NEXT: [[COND:%.*]] = call i1 @foo()
322 ; CHECK-NEXT: br i1 [[COND]], label [[BB_NOMERGE]], label [[SUCC]]
324 ; CHECK-NEXT: ret void
329 BB.nomerge: ; preds = %Common, %entry
330 ; This phi has a matching value (0) with below phi (0), so blocks
332 %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
335 Succ: ; preds = %Common, %BB.tomerge
336 %b = phi i32 [ %a, %BB.nomerge ], [ 0, %Common ] ; <i32> [#uses=0]
337 %conde = call i1 @foo( ) ; <i1> [#uses=1]
338 br i1 %conde, label %Common, label %Exit
340 Common: ; preds = %Succ
341 %cond = call i1 @foo( ) ; <i1> [#uses=1]
342 br i1 %cond, label %BB.nomerge, label %Succ
344 Exit: ; preds = %Succ
348 ; This function can be merged
352 ; CHECK-NEXT: br label [[SUCC:%.*]]
354 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ 0, [[USE:%.*]] ]
355 ; CHECK-NEXT: [[CONDE:%.*]] = call i1 @foo()
356 ; CHECK-NEXT: br i1 [[CONDE]], label [[USE]], label [[EXIT:%.*]]
358 ; CHECK-NEXT: [[COND:%.*]] = call i1 @bar(i32 [[A]])
359 ; CHECK-NEXT: br i1 [[COND]], label [[SUCC]], label [[EXIT]]
361 ; CHECK-NEXT: ret void
366 Succ: ; preds = %Use, %entry
367 ; This phi is used somewhere else than Succ, but this should not prevent
369 %a = phi i32 [ 1, %entry ], [ 0, %Use ] ; <i32> [#uses=1]
372 BB.tomerge: ; preds = %Succ
373 %conde = call i1 @foo( ) ; <i1> [#uses=1]
374 br i1 %conde, label %Use, label %Exit
377 %cond = call i1 @bar( i32 %a ) ; <i1> [#uses=1]
378 br i1 %cond, label %Succ, label %Exit
380 Exit: ; preds = %Use, %Succ