Skip to content

Commit fc4a9aa

Browse files
nikicakyrtzi
authored andcommitted
Reapply [SimplifyCFG] Handle branch on same condition in pred more directly
Reapplying without changes, after a fix to a dependent patch. ----- Rather than creating a PHI node and then using the PHI threading code, directly handle this case in FoldCondBranchOnValueKnownInPredecessor(). This change is supposed to be NFC-ish, but may cause changes due to different transform order. (cherry picked from commit 993b166)
1 parent fcacff9 commit fc4a9aa

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

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

2978+
static ConstantInt *getKnownValueOnEdge(Value *V, BasicBlock *From,
2979+
BasicBlock *To) {
2980+
// Don't look past the block defining the value, we might get the value from
2981+
// a previous loop iteration.
2982+
auto *I = dyn_cast<Instruction>(V);
2983+
if (I && I->getParent() == To)
2984+
return nullptr;
2985+
2986+
// We know the value if the From block branches on it.
2987+
auto *BI = dyn_cast<BranchInst>(From->getTerminator());
2988+
if (BI && BI->isConditional() && BI->getCondition() == V &&
2989+
BI->getSuccessor(0) != BI->getSuccessor(1))
2990+
return BI->getSuccessor(0) == To ? ConstantInt::getTrue(BI->getContext())
2991+
: ConstantInt::getFalse(BI->getContext());
2992+
2993+
return nullptr;
2994+
}
2995+
29782996
/// If we have a conditional branch on something for which we know the constant
29792997
/// value in predecessors (e.g. a phi node in the current block), thread edges
29802998
/// from the predecessor to their ultimate destination.
@@ -2984,7 +3002,8 @@ FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
29843002
AssumptionCache *AC) {
29853003
SmallMapVector<BasicBlock *, ConstantInt *, 8> KnownValues;
29863004
BasicBlock *BB = BI->getParent();
2987-
PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
3005+
Value *Cond = BI->getCondition();
3006+
PHINode *PN = dyn_cast<PHINode>(Cond);
29883007
if (PN && PN->getParent() == BB) {
29893008
// Degenerate case of a single entry PHI.
29903009
if (PN->getNumIncomingValues() == 1) {
@@ -2995,6 +3014,11 @@ FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
29953014
for (Use &U : PN->incoming_values())
29963015
if (auto *CB = dyn_cast<ConstantInt>(U))
29973016
KnownValues.insert({PN->getIncomingBlock(U), CB});
3017+
} else {
3018+
for (BasicBlock *Pred : predecessors(BB)) {
3019+
if (ConstantInt *CB = getKnownValueOnEdge(Cond, Pred, BB))
3020+
KnownValues.insert({Pred, CB});
3021+
}
29983022
}
29993023

30003024
if (KnownValues.empty())
@@ -4044,43 +4068,15 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
40444068
if (PBI->getCondition() == BI->getCondition() &&
40454069
PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
40464070
// Okay, the outcome of this conditional branch is statically
4047-
// knowable. If this block had a single pred, handle specially.
4071+
// knowable. If this block had a single pred, handle specially, otherwise
4072+
// FoldCondBranchOnValueKnownInPredecessor() will handle it.
40484073
if (BB->getSinglePredecessor()) {
40494074
// Turn this into a branch on constant.
40504075
bool CondIsTrue = PBI->getSuccessor(0) == BB;
40514076
BI->setCondition(
40524077
ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue));
40534078
return true; // Nuke the branch on constant.
40544079
}
4055-
4056-
// Otherwise, if there are multiple predecessors, insert a PHI that merges
4057-
// in the constant and simplify the block result. Subsequent passes of
4058-
// simplifycfg will thread the block.
4059-
if (BlockIsSimpleEnoughToThreadThrough(BB)) {
4060-
pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
4061-
PHINode *NewPN = PHINode::Create(
4062-
Type::getInt1Ty(BB->getContext()), std::distance(PB, PE),
4063-
BI->getCondition()->getName() + ".pr", &BB->front());
4064-
// Okay, we're going to insert the PHI node. Since PBI is not the only
4065-
// predecessor, compute the PHI'd conditional value for all of the preds.
4066-
// Any predecessor where the condition is not computable we keep symbolic.
4067-
for (pred_iterator PI = PB; PI != PE; ++PI) {
4068-
BasicBlock *P = *PI;
4069-
if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && PBI != BI &&
4070-
PBI->isConditional() && PBI->getCondition() == BI->getCondition() &&
4071-
PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
4072-
bool CondIsTrue = PBI->getSuccessor(0) == BB;
4073-
NewPN->addIncoming(
4074-
ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue),
4075-
P);
4076-
} else {
4077-
NewPN->addIncoming(BI->getCondition(), P);
4078-
}
4079-
}
4080-
4081-
BI->setCondition(NewPN);
4082-
return true;
4083-
}
40844080
}
40854081

40864082
// If the previous block ended with a widenable branch, determine if reusing

0 commit comments

Comments
 (0)