EqualPHIEdgeBlockMerge.ll 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ; Test merging of blocks with phi nodes.
  2. ;
  3. ; RUN: opt < %s -simplifycfg -S > %t
  4. ; RUN: not grep N: %t
  5. ; RUN: not grep X: %t
  6. ; RUN: not grep 'switch i32[^U]+%U' %t
  7. ; RUN: not grep "^BB.tomerge" %t
  8. ; RUN: grep "^BB.nomerge" %t | count 2
  9. ;
  10. ; ModuleID = '<stdin>'
  11. declare i1 @foo()
  12. declare i1 @bar(i32)
  13. define i32 @test(i1 %a) {
  14. Q:
  15. br i1 %a, label %N, label %M
  16. N: ; preds = %Q
  17. br label %M
  18. M: ; preds = %N, %Q
  19. ; It's ok to merge N and M because the incoming values for W are the
  20. ; same for both cases...
  21. %W = phi i32 [ 2, %N ], [ 2, %Q ] ; <i32> [#uses=1]
  22. %R = add i32 %W, 1 ; <i32> [#uses=1]
  23. ret i32 %R
  24. }
  25. ; Test merging of blocks with phi nodes where at least one incoming value
  26. ; in the successor is undef.
  27. define i8 @testundef(i32 %u) {
  28. R:
  29. switch i32 %u, label %U [
  30. i32 0, label %S
  31. i32 1, label %T
  32. i32 2, label %T
  33. ]
  34. S: ; preds = %R
  35. br label %U
  36. T: ; preds = %R, %R
  37. br label %U
  38. U: ; preds = %T, %S, %R
  39. ; We should be able to merge either the S or T block into U by rewriting
  40. ; R's incoming value with the incoming value of that predecessor since
  41. ; R's incoming value is undef and both of those predecessors are simple
  42. ; unconditional branches.
  43. %val.0 = phi i8 [ undef, %R ], [ 1, %T ], [ 0, %S ]
  44. ret i8 %val.0
  45. }
  46. ; Test merging of blocks with phi nodes where at least one incoming value
  47. ; in the successor is undef.
  48. define i8 @testundef2(i32 %u, i32* %A) {
  49. V:
  50. switch i32 %u, label %U [
  51. i32 0, label %W
  52. i32 1, label %X
  53. i32 2, label %X
  54. i32 3, label %Z
  55. ]
  56. W: ; preds = %V
  57. br label %U
  58. Z:
  59. store i32 0, i32* %A, align 4
  60. br label %X
  61. X: ; preds = %V, %V, %Z
  62. br label %U
  63. U: ; preds = %X, %W, %V
  64. ; We should be able to merge either the W or X block into U by rewriting
  65. ; V's incoming value with the incoming value of that predecessor since
  66. ; V's incoming value is undef and both of those predecessors are simple
  67. ; unconditional branches. Note that X has predecessors beyond
  68. ; the direct predecessors of U.
  69. %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 1, %W ]
  70. ret i8 %val.0
  71. }
  72. define i8 @testmergesome(i32 %u, i32* %A) {
  73. V:
  74. switch i32 %u, label %Y [
  75. i32 0, label %W
  76. i32 1, label %X
  77. i32 2, label %X
  78. i32 3, label %Z
  79. ]
  80. W: ; preds = %V
  81. store i32 1, i32* %A, align 4
  82. br label %Y
  83. Z:
  84. store i32 0, i32* %A, align 4
  85. br label %X
  86. X: ; preds = %V, %Z
  87. br label %Y
  88. Y: ; preds = %X, %W, %V
  89. ; After merging X into Y, we should have 5 predecessors
  90. ; and thus 5 incoming values to the phi.
  91. %val.0 = phi i8 [ 1, %V ], [ 1, %X ], [ 2, %W ]
  92. ret i8 %val.0
  93. }
  94. define i8 @testmergesome2(i32 %u, i32* %A) {
  95. V:
  96. switch i32 %u, label %W [
  97. i32 0, label %W
  98. i32 1, label %Y
  99. i32 2, label %X
  100. i32 4, label %Y
  101. ]
  102. W: ; preds = %V
  103. store i32 1, i32* %A, align 4
  104. br label %Y
  105. X: ; preds = %V, %Z
  106. br label %Y
  107. Y: ; preds = %X, %W, %V
  108. ; Ensure that we deal with both undef inputs for V when we merge in X.
  109. %val.0 = phi i8 [ undef, %V ], [ 1, %X ], [ 2, %W ], [ undef, %V ]
  110. ret i8 %val.0
  111. }
  112. ; This function can't be merged
  113. define void @a() {
  114. entry:
  115. br label %BB.nomerge
  116. BB.nomerge: ; preds = %Common, %entry
  117. ; This phi has a conflicting value (0) with below phi (2), so blocks
  118. ; can't be merged.
  119. %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
  120. br label %Succ
  121. Succ: ; preds = %Common, %BB.nomerge
  122. %b = phi i32 [ %a, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
  123. %conde = call i1 @foo( ) ; <i1> [#uses=1]
  124. br i1 %conde, label %Common, label %Exit
  125. Common: ; preds = %Succ
  126. %cond = call i1 @foo( ) ; <i1> [#uses=1]
  127. br i1 %cond, label %BB.nomerge, label %Succ
  128. Exit: ; preds = %Succ
  129. ret void
  130. }
  131. ; This function can't be merged
  132. define void @b() {
  133. entry:
  134. br label %BB.nomerge
  135. BB.nomerge: ; preds = %Common, %entry
  136. br label %Succ
  137. Succ: ; preds = %Common, %BB.nomerge
  138. ; This phi has confliction values for Common and (through BB) Common,
  139. ; blocks can't be merged
  140. %b = phi i32 [ 1, %BB.nomerge ], [ 2, %Common ] ; <i32> [#uses=0]
  141. %conde = call i1 @foo( ) ; <i1> [#uses=1]
  142. br i1 %conde, label %Common, label %Exit
  143. Common: ; preds = %Succ
  144. %cond = call i1 @foo( ) ; <i1> [#uses=1]
  145. br i1 %cond, label %BB.nomerge, label %Succ
  146. Exit: ; preds = %Succ
  147. ret void
  148. }
  149. ; This function can be merged
  150. define void @c() {
  151. entry:
  152. br label %BB.tomerge
  153. BB.tomerge: ; preds = %Common, %entry
  154. br label %Succ
  155. Succ: ; preds = %Common, %BB.tomerge, %Pre-Exit
  156. ; This phi has identical values for Common and (through BB) Common,
  157. ; blocks can't be merged
  158. %b = phi i32 [ 1, %BB.tomerge ], [ 1, %Common ], [ 2, %Pre-Exit ]
  159. %conde = call i1 @foo( ) ; <i1> [#uses=1]
  160. br i1 %conde, label %Common, label %Pre-Exit
  161. Common: ; preds = %Succ
  162. %cond = call i1 @foo( ) ; <i1> [#uses=1]
  163. br i1 %cond, label %BB.tomerge, label %Succ
  164. Pre-Exit: ; preds = %Succ
  165. ; This adds a backedge, so the %b phi node gets a third branch and is
  166. ; not completely trivial
  167. %cond2 = call i1 @foo( ) ; <i1> [#uses=1]
  168. br i1 %cond2, label %Succ, label %Exit
  169. Exit: ; preds = %Pre-Exit
  170. ret void
  171. }
  172. ; This function can be merged
  173. define void @d() {
  174. entry:
  175. br label %BB.tomerge
  176. BB.tomerge: ; preds = %Common, %entry
  177. ; This phi has a matching value (0) with below phi (0), so blocks
  178. ; can be merged.
  179. %a = phi i32 [ 1, %entry ], [ 0, %Common ] ; <i32> [#uses=1]
  180. br label %Succ
  181. Succ: ; preds = %Common, %BB.tomerge
  182. %b = phi i32 [ %a, %BB.tomerge ], [ 0, %Common ] ; <i32> [#uses=0]
  183. %conde = call i1 @foo( ) ; <i1> [#uses=1]
  184. br i1 %conde, label %Common, label %Exit
  185. Common: ; preds = %Succ
  186. %cond = call i1 @foo( ) ; <i1> [#uses=1]
  187. br i1 %cond, label %BB.tomerge, label %Succ
  188. Exit: ; preds = %Succ
  189. ret void
  190. }
  191. ; This function can be merged
  192. define void @e() {
  193. entry:
  194. br label %BB.tomerge
  195. BB.tomerge: ; preds = %Use, %entry
  196. ; This phi is used somewhere else than Succ, but this should not prevent
  197. ; merging this block
  198. %a = phi i32 [ 1, %entry ], [ 0, %Use ] ; <i32> [#uses=1]
  199. br label %Succ
  200. Succ: ; preds = %BB.tomerge
  201. %conde = call i1 @foo( ) ; <i1> [#uses=1]
  202. br i1 %conde, label %Use, label %Exit
  203. Use: ; preds = %Succ
  204. %cond = call i1 @bar( i32 %a ) ; <i1> [#uses=1]
  205. br i1 %cond, label %BB.tomerge, label %Exit
  206. Exit: ; preds = %Use, %Succ
  207. ret void
  208. }