Skip to content

Commit fcacff9

Browse files
nikicakyrtzi
authored andcommitted
Reapply [SimplifyCFG] Make FoldCondBranchOnPHI more amenable to extension (NFCI)
Reapply with SmallMapVector instead of SmallDenseMap, which should address the non-determinism issue. ----- This general threading transform can be performed whenever we know a constant value for the condition in a predecessor, which would currently just be the case of a phi node with constant arguments. (cherry picked from commit df18e37)
1 parent ee76330 commit fcacff9

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,40 +2975,42 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
29752975
return true;
29762976
}
29772977

2978-
/// If we have a conditional branch on a PHI node value that is defined in the
2979-
/// same block as the branch and if any PHI entries are constants, thread edges
2980-
/// corresponding to that entry to be branches to their ultimate destination.
2981-
static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
2982-
DomTreeUpdater *DTU,
2983-
const DataLayout &DL,
2984-
AssumptionCache *AC) {
2978+
/// If we have a conditional branch on something for which we know the constant
2979+
/// value in predecessors (e.g. a phi node in the current block), thread edges
2980+
/// from the predecessor to their ultimate destination.
2981+
static Optional<bool>
2982+
FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
2983+
const DataLayout &DL,
2984+
AssumptionCache *AC) {
2985+
SmallMapVector<BasicBlock *, ConstantInt *, 8> KnownValues;
29852986
BasicBlock *BB = BI->getParent();
29862987
PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
2987-
if (!PN || PN->getParent() != BB)
2988-
return false;
2988+
if (PN && PN->getParent() == BB) {
2989+
// Degenerate case of a single entry PHI.
2990+
if (PN->getNumIncomingValues() == 1) {
2991+
FoldSingleEntryPHINodes(PN->getParent());
2992+
return true;
2993+
}
29892994

2990-
// Degenerate case of a single entry PHI.
2991-
if (PN->getNumIncomingValues() == 1) {
2992-
FoldSingleEntryPHINodes(PN->getParent());
2993-
return true;
2995+
for (Use &U : PN->incoming_values())
2996+
if (auto *CB = dyn_cast<ConstantInt>(U))
2997+
KnownValues.insert({PN->getIncomingBlock(U), CB});
29942998
}
29952999

3000+
if (KnownValues.empty())
3001+
return false;
3002+
29963003
// Now we know that this block has multiple preds and two succs.
29973004
// Check that the block is small enough and values defined in the block are
29983005
// not used outside of it.
29993006
if (!BlockIsSimpleEnoughToThreadThrough(BB))
30003007
return false;
30013008

3002-
// Okay, this is a simple enough basic block. See if any phi values are
3003-
// constants.
3004-
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
3005-
ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
3006-
if (!CB || !CB->getType()->isIntegerTy(1))
3007-
continue;
3008-
3009+
for (const auto &Pair : KnownValues) {
30093010
// Okay, we now know that all edges from PredBB should be revectored to
30103011
// branch to RealDest.
3011-
BasicBlock *PredBB = PN->getIncomingBlock(i);
3012+
ConstantInt *CB = Pair.second;
3013+
BasicBlock *PredBB = Pair.first;
30123014
BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
30133015

30143016
if (RealDest == BB)
@@ -3102,13 +3104,15 @@ static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
31023104
return false;
31033105
}
31043106

3105-
static bool FoldCondBranchOnPHI(BranchInst *BI, DomTreeUpdater *DTU,
3106-
const DataLayout &DL, AssumptionCache *AC) {
3107+
static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI,
3108+
DomTreeUpdater *DTU,
3109+
const DataLayout &DL,
3110+
AssumptionCache *AC) {
31073111
Optional<bool> Result;
31083112
bool EverChanged = false;
31093113
do {
31103114
// Note that None means "we changed things, but recurse further."
3111-
Result = FoldCondBranchOnPHIImpl(BI, DTU, DL, AC);
3115+
Result = FoldCondBranchOnValueKnownInPredecessorImpl(BI, DTU, DL, AC);
31123116
EverChanged |= Result == None || *Result;
31133117
} while (Result == None);
31143118
return EverChanged;
@@ -6903,12 +6907,11 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
69036907
return requestResimplify();
69046908
}
69056909

6906-
// If this is a branch on a phi node in the current block, thread control
6907-
// through this block if any PHI node entries are constants.
6908-
if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
6909-
if (PN->getParent() == BI->getParent())
6910-
if (FoldCondBranchOnPHI(BI, DTU, DL, Options.AC))
6911-
return requestResimplify();
6910+
// If this is a branch on something for which we know the constant value in
6911+
// predecessors (e.g. a phi node in the current block), thread control
6912+
// through this block.
6913+
if (FoldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, Options.AC))
6914+
return requestResimplify();
69126915

69136916
// Scan predecessor blocks for conditional branches.
69146917
for (BasicBlock *Pred : predecessors(BB))

0 commit comments

Comments
 (0)