1 ; Test merging of blocks with phi nodes.
3 ; RUN: opt < %s -simplifycfg -S > %t
6 ; RUN: not grep 'switch i32[^U]+%U' %t
7 ; RUN: not grep "^BB.tomerge" %t
8 ; RUN: grep "^BB.nomerge" %t | count 4
11 ; ModuleID = '<stdin>'
16 define i32 @test(i1 %a) {
18 br i1 %a, label %N, label %M
22 ; It's ok to merge N and M because the incoming values for W are the
23 ; same for both cases...
24 %W = phi i32 [ 2, %N ], [ 2, %Q ] ; <i32> [#uses=1]
25 %R = add i32 %W, 1 ; <i32> [#uses=1]
29 ; Test merging of blocks with phi nodes where at least one incoming value
30 ; in the successor is undef.
31 define i8 @testundef(i32 %u) {
33 switch i32 %u, label %U [
45 U: ; preds = %T, %S, %R
46 ; We should be able to merge either the S or T block into U by rewriting
47 ; R's incoming value with the incoming value of that predecessor since
48 ; R's incoming value is undef and both of those predecessors are simple
49 ; unconditional branches.
50 %val.0 = phi i8 [ undef, %R ], [ 1, %T ], [ 0, %S ]
54 ; Test merging of blocks with phi nodes where at least one incoming value
55 ; in the successor is undef.
56 define i8 @testundef2(i32 %u, i32* %A) {
58 switch i32 %u, label %U [
69 store i32 0, i32* %A, align 4
72 X: ; preds = %V, %V, %Z
75 U: ; preds = %X, %W, %V
76 ; We should be able to merge either the W or X block into U by rewriting
77 ; V's incoming value with the incoming value of that predecessor since
78 ; V's incoming value is undef and both of those predecessors are simple
79 ; unconditional branches. Note that X has predecessors beyond
80 ; the direct predecessors of U.
81 %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 1, %W ]
85 define i8 @testmergesome(i32 %u, i32* %A) {
87 switch i32 %u, label %Y [
95 store i32 1, i32* %A, align 4
99 store i32 0, i32* %A, align 4
105 Y: ; preds = %X, %W, %V
106 ; After merging X into Y, we should have 5 predecessors
107 ; and thus 5 incoming values to the phi.
108 %val.0 = phi i8 [ 1, %V ], [ 1, %X ], [ 2, %W ]
113 define i8 @testmergesome2(i32 %u, i32* %A) {
115 switch i32 %u, label %W [
123 store i32 1, i32* %A, align 4
129 Y: ; preds = %X, %W, %V
130 ; Ensure that we deal with both undef inputs for V when we merge in X.
131 %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 2, %W ], [ undef, %V ]
135 ; This function can't be merged
140 BB.nomerge: ; preds = %Common, %entry
141 ; This phi has a conflicting value (0) with below phi (2), so blocks
143 %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
146 Succ: ; preds = %Common, %BB.nomerge
147 %b = phi i32 [ %a, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
148 %conde = call i1 @foo( ) ; <i1> [#uses=1]
149 br i1 %conde, label %Common, label %Exit
151 Common: ; preds = %Succ
152 %cond = call i1 @foo( ) ; <i1> [#uses=1]
153 br i1 %cond, label %BB.nomerge, label %Succ
155 Exit: ; preds = %Succ
159 ; This function can't be merged
164 BB.nomerge: ; preds = %Common, %entry
167 Succ: ; preds = %Common, %BB.nomerge
168 ; This phi has confliction values for Common and (through BB) Common,
169 ; blocks can't be merged
170 %b = phi i32 [ 1, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
171 %conde = call i1 @foo( ) ; <i1> [#uses=1]
172 br i1 %conde, label %Common, label %Exit
174 Common: ; preds = %Succ
175 %cond = call i1 @foo( ) ; <i1> [#uses=1]
176 br i1 %cond, label %BB.nomerge, label %Succ
178 Exit: ; preds = %Succ
182 ; This function can't be merged (for keeping canonical loop structures)
187 BB.nomerge: ; preds = %Common, %entry
190 Succ: ; preds = %Common, %BB.tomerge, %Pre-Exit
191 ; This phi has identical values for Common and (through BB) Common,
192 ; blocks can't be merged
193 %b = phi i32 [ 1, %BB.nomerge ], [ 1, %Common ], [ 2, %Pre-Exit ]
194 %conde = call i1 @foo( ) ; <i1> [#uses=1]
195 br i1 %conde, label %Common, label %Pre-Exit
197 Common: ; preds = %Succ
198 %cond = call i1 @foo( ) ; <i1> [#uses=1]
199 br i1 %cond, label %BB.nomerge, label %Succ
201 Pre-Exit: ; preds = %Succ
202 ; This adds a backedge, so the %b phi node gets a third branch and is
203 ; not completely trivial
204 %cond2 = call i1 @foo( ) ; <i1> [#uses=1]
205 br i1 %cond2, label %Succ, label %Exit
207 Exit: ; preds = %Pre-Exit
211 ; This function can't be merged (for keeping canonical loop structures)
216 BB.nomerge: ; preds = %Common, %entry
217 ; This phi has a matching value (0) with below phi (0), so blocks
219 %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
222 Succ: ; preds = %Common, %BB.tomerge
223 %b = phi i32 [ %a, %BB.nomerge ], [ 0, %Common ] ; <i32> [#uses=0]
224 %conde = call i1 @foo( ) ; <i1> [#uses=1]
225 br i1 %conde, label %Common, label %Exit
227 Common: ; preds = %Succ
228 %cond = call i1 @foo( ) ; <i1> [#uses=1]
229 br i1 %cond, label %BB.nomerge, label %Succ
231 Exit: ; preds = %Succ
235 ; This function can be merged
240 Succ: ; preds = %Use, %entry
241 ; This phi is used somewhere else than Succ, but this should not prevent
243 %a = phi i32 [ 1, %entry ], [ 0, %Use ] ; <i32> [#uses=1]
246 BB.tomerge: ; preds = %Succ
247 %conde = call i1 @foo( ) ; <i1> [#uses=1]
248 br i1 %conde, label %Use, label %Exit
251 %cond = call i1 @bar( i32 %a ) ; <i1> [#uses=1]
252 br i1 %cond, label %Succ, label %Exit
254 Exit: ; preds = %Use, %Succ