[llvm] [StructurizeCFG] Hoist and simplify zero-cost incoming else phi values (PR #139605)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 21:23:44 PDT 2025


================
@@ -891,6 +969,39 @@ void StructurizeCFG::setPhiValues() {
   AffectedPhis.append(InsertedPhis.begin(), InsertedPhis.end());
 }
 
+/// Updates PHI nodes after hoisted zero cost instructions by replacing poison
+/// entries on Flow nodes with the appropriate hoisted values
+void StructurizeCFG::simplifyHoistedPhis() {
+  for (WeakVH VH : AffectedPhis) {
+    if (auto Phi = dyn_cast_or_null<PHINode>(VH)) {
+      if (Phi->getNumIncomingValues() != 2)
+        continue;
+      for (int i = 0; i < 2; i++) {
+        Value *V = Phi->getIncomingValue(i);
+        if (HoistedValues.count(V)) {
+          Value *OtherV = Phi->getIncomingValue(!i);
+          if (PHINode *OtherPhi = dyn_cast<PHINode>(OtherV)) {
+            int PoisonValBBIdx = -1;
+            for (size_t i = 0; i < OtherPhi->getNumIncomingValues(); i++) {
+              if (!isa<PoisonValue>(OtherPhi->getIncomingValue(i)))
+                continue;
+              PoisonValBBIdx = i;
+              break;
+            }
+
+            if (PoisonValBBIdx == -1 ||
+                !DT->dominates(HoistedValues[V],
+                               OtherPhi->getIncomingBlock(PoisonValBBIdx)))
+              continue;
+            OtherPhi->setIncomingValue(PoisonValBBIdx, V);
+            Phi->setIncomingValue(i, OtherV);
+          }
+        }
+      }
+    }
+  }
+}
----------------
shiltian wrote:

rewrite this entire code with early exit pls

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


More information about the llvm-commits mailing list