@@ -2975,40 +2975,42 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
2975
2975
return true ;
2976
2976
}
2977
2977
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;
2985
2986
BasicBlock *BB = BI->getParent ();
2986
2987
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
+ }
2989
2994
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});
2994
2998
}
2995
2999
3000
+ if (KnownValues.empty ())
3001
+ return false ;
3002
+
2996
3003
// Now we know that this block has multiple preds and two succs.
2997
3004
// Check that the block is small enough and values defined in the block are
2998
3005
// not used outside of it.
2999
3006
if (!BlockIsSimpleEnoughToThreadThrough (BB))
3000
3007
return false ;
3001
3008
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) {
3009
3010
// Okay, we now know that all edges from PredBB should be revectored to
3010
3011
// branch to RealDest.
3011
- BasicBlock *PredBB = PN->getIncomingBlock (i);
3012
+ ConstantInt *CB = Pair.second ;
3013
+ BasicBlock *PredBB = Pair.first ;
3012
3014
BasicBlock *RealDest = BI->getSuccessor (!CB->getZExtValue ());
3013
3015
3014
3016
if (RealDest == BB)
@@ -3102,13 +3104,15 @@ static Optional<bool> FoldCondBranchOnPHIImpl(BranchInst *BI,
3102
3104
return false ;
3103
3105
}
3104
3106
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) {
3107
3111
Optional<bool > Result;
3108
3112
bool EverChanged = false ;
3109
3113
do {
3110
3114
// Note that None means "we changed things, but recurse further."
3111
- Result = FoldCondBranchOnPHIImpl (BI, DTU, DL, AC);
3115
+ Result = FoldCondBranchOnValueKnownInPredecessorImpl (BI, DTU, DL, AC);
3112
3116
EverChanged |= Result == None || *Result;
3113
3117
} while (Result == None);
3114
3118
return EverChanged;
@@ -6903,12 +6907,11 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
6903
6907
return requestResimplify ();
6904
6908
}
6905
6909
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 ();
6912
6915
6913
6916
// Scan predecessor blocks for conditional branches.
6914
6917
for (BasicBlock *Pred : predecessors (BB))
0 commit comments