@@ -2975,6 +2975,24 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
2975
2975
return true ;
2976
2976
}
2977
2977
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
+
2978
2996
// / If we have a conditional branch on something for which we know the constant
2979
2997
// / value in predecessors (e.g. a phi node in the current block), thread edges
2980
2998
// / from the predecessor to their ultimate destination.
@@ -2984,7 +3002,8 @@ FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
2984
3002
AssumptionCache *AC) {
2985
3003
SmallMapVector<BasicBlock *, ConstantInt *, 8 > KnownValues;
2986
3004
BasicBlock *BB = BI->getParent ();
2987
- PHINode *PN = dyn_cast<PHINode>(BI->getCondition ());
3005
+ Value *Cond = BI->getCondition ();
3006
+ PHINode *PN = dyn_cast<PHINode>(Cond);
2988
3007
if (PN && PN->getParent () == BB) {
2989
3008
// Degenerate case of a single entry PHI.
2990
3009
if (PN->getNumIncomingValues () == 1 ) {
@@ -2995,6 +3014,11 @@ FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
2995
3014
for (Use &U : PN->incoming_values ())
2996
3015
if (auto *CB = dyn_cast<ConstantInt>(U))
2997
3016
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
+ }
2998
3022
}
2999
3023
3000
3024
if (KnownValues.empty ())
@@ -4044,43 +4068,15 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
4044
4068
if (PBI->getCondition () == BI->getCondition () &&
4045
4069
PBI->getSuccessor (0 ) != PBI->getSuccessor (1 )) {
4046
4070
// 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.
4048
4073
if (BB->getSinglePredecessor ()) {
4049
4074
// Turn this into a branch on constant.
4050
4075
bool CondIsTrue = PBI->getSuccessor (0 ) == BB;
4051
4076
BI->setCondition (
4052
4077
ConstantInt::get (Type::getInt1Ty (BB->getContext ()), CondIsTrue));
4053
4078
return true ; // Nuke the branch on constant.
4054
4079
}
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
- }
4084
4080
}
4085
4081
4086
4082
// If the previous block ended with a widenable branch, determine if reusing
0 commit comments