[SimpifyCFG] Remove recursion from FoldCondBranchOnPHI. NFCI.

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.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D107803
This commit is contained in:
Carl Ritson 2021-08-10 19:14:23 +09:00
parent aef3992521
commit a1783b54e8
1 changed files with 18 additions and 4 deletions

View File

@ -2606,8 +2606,10 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
/// If we have a conditional branch on a PHI node value that is defined in the /// 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 /// 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. /// corresponding to that entry to be branches to their ultimate destination.
static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU, static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
const DataLayout &DL, AssumptionCache *AC) { DomTreeUpdater *DTU,
const DataLayout &DL,
AssumptionCache *AC) {
BasicBlock *BB = BI->getParent(); BasicBlock *BB = BI->getParent();
PHINode *PN = dyn_cast<PHINode>(BI->getCondition()); PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
// NOTE: we currently cannot transform this case if the PHI node is used // NOTE: we currently cannot transform this case if the PHI node is used
@ -2721,13 +2723,25 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
DTU->applyUpdates(Updates); DTU->applyUpdates(Updates);
} }
// Recurse, simplifying any other constants. // Signal repeat, simplifying any other constants.
return FoldCondBranchOnPHI(BI, DTU, DL, AC) || true; return None;
} }
return false; return false;
} }
static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
const DataLayout &DL, AssumptionCache *AC) {
Optional<bool> Result;
bool EverChanged = false;
do {
// Note that None means "we changed things, but recurse further."
Result = FoldCondBranchOnPHIImpl(BI, DTU, DL, AC);
EverChanged |= Result == None || *Result;
} while (Result == None);
return EverChanged;
}
/// Given a BB that starts with the specified two-entry PHI node, /// Given a BB that starts with the specified two-entry PHI node,
/// see if we can eliminate it. /// see if we can eliminate it.
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,