[PATCH] D107803: [SimpifyCFG] Remove recursion from FoldCondBranchOnPHI. NFCI.

Carl Ritson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 10 01:16:05 PDT 2021


critson created this revision.
critson added reviewers: lebedev.ri, fhahn, nikic, spatel.
Herald added a subscriber: hiraditya.
critson requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Avoid stack overflow errors on systems with small stack sizes
by removing recursion in FoldCondBranchOnPHI.

This is a simple change as the recursion was only iteratively
calling the function again on the same arguments.
Ideally this would be compiled to a tail call, but there is
no guarantee.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107803

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp


Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2606,8 +2606,10 @@
 /// If we have a conditional branch on a PHI node value that is defined in the
 /// same block as the branch and if any PHI entries are constants, thread edges
 /// corresponding to that entry to be branches to their ultimate destination.
-static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
-                                const DataLayout &DL, AssumptionCache *AC) {
+static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
+                                              DomTreeUpdater *DTU,
+                                              const DataLayout &DL,
+                                              AssumptionCache *AC) {
   BasicBlock *BB = BI->getParent();
   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
   // NOTE: we currently cannot transform this case if the PHI node is used
@@ -2721,13 +2723,24 @@
       DTU->applyUpdates(Updates);
     }
 
-    // Recurse, simplifying any other constants.
-    return FoldCondBranchOnPHI(BI, DTU, DL, AC) || true;
+    // Signal repeat, simplifying any other constants.
+    return Optional<bool>();
   }
 
   return false;
 }
 
+static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
+                                const DataLayout &DL, AssumptionCache *AC) {
+  Optional<bool> Result;
+  unsigned Iterations = 0;
+  do {
+    Result = FoldCondBranchOnPHIImpl(BI, DTU, DL, AC);
+    Iterations++;
+  } while (!Result);
+  return *Result || Iterations > 1;
+}
+
 /// Given a BB that starts with the specified two-entry PHI node,
 /// see if we can eliminate it.
 static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107803.365379.patch
Type: text/x-patch
Size: 1877 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210810/cdafc2ba/attachment.bin>


More information about the llvm-commits mailing list