[llvm] [StructurizeCFG] Fix incorrect zero-cost hoisting in nested control flow (PR #183792)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 11 19:45:55 PDT 2026


================
@@ -479,6 +479,16 @@ void StructurizeCFG::hoistZeroCostElseBlockPhiValues(BasicBlock *ElseBB,
 
   if (!ElseSucc || !CommonDominator)
     return;
+  // Only hoist when the merge phi has exactly 2 incoming values (a simple
+  // if-else). With 3+ incoming values, multiple control-flow paths merge at
+  // ElseSucc. After structurization the hoisted instruction ends up in a
+  // dominator block that feeds a Flow phi on every edge, including edges
+  // where the else block was never taken, causing the hoisted value to
+  // leak into unrelated paths.
+  for (PHINode &Phi : ElseSucc->phis()) {
----------------
ruiling wrote:

Debugging and fixing such issue is annoying. Thanks for doing this. My idea is this is mainly because of the fragile `simplifyHoistedPhis`. It only works in the exactly if/else pattern. So to fix this properly. I would rather we make sure the shape of the CFG is the simplest if/else pattern here. This would be stricter than what you are checking. Something like:

```
if (ThenBB->getSingleSuccessor() != ElseSucc || !ElseSucc->hasNPredecessors(2))
  return;
```

We really should add some assertion in the `simplifyHoistedPhis` to avoid silently doing wrong. That can be done separately.

https://github.com/llvm/llvm-project/pull/183792


More information about the llvm-commits mailing list