Skip to content

Commit 0895b83

Browse files
committed
[SimplifyCFG] FoldBranchToCommonDest(): don't deal with unconditional branches
The case where BB ends with an unconditional branch, and has a single predecessor w/ conditional branch to BB and a single successor of BB is exactly the pattern SpeculativelyExecuteBB() transform deals with. (and in this case they both allow speculating only a single instruction) Well, or FoldTwoEntryPHINode(), if the final block has only those two predecessors. Here, in FoldBranchToCommonDest(), only a weird subset of that transform is supported, and it's glued on the side in a weird way. In particular, it took me a bit to understand that the Cond isn't actually a branch condition in that case, but just the value we allow to speculate (otherwise it reads as a miscompile to me). Additionally, this only supports for the speculated instruction to be an ICmp. So let's just unclutter FoldBranchToCommonDest(), and leave this transform up to SpeculativelyExecuteBB(). As far as i can tell, this shouldn't really impact optimization potential, but if it does, improving SpeculativelyExecuteBB() will be more beneficial anyways. Notably, this only affects a single test, but EarlyCSE should have run beforehand in the pipeline, and then FoldTwoEntryPHINode() would have caught it. This reverts commit rL158392 / commit d33f4ef.
1 parent 98a8344 commit 0895b83

File tree

3 files changed

+77
-274
lines changed

3 files changed

+77
-274
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 69 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -314,46 +314,6 @@ SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
314314
return !Fail;
315315
}
316316

317-
/// Return true if it is safe and profitable to merge these two terminator
318-
/// instructions together, where SI1 is an unconditional branch. PhiNodes will
319-
/// store all PHI nodes in common successors.
320-
static bool
321-
isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2,
322-
Instruction *Cond,
323-
SmallVectorImpl<PHINode *> &PhiNodes) {
324-
if (SI1 == SI2)
325-
return false; // Can't merge with self!
326-
assert(SI1->isUnconditional() && SI2->isConditional());
327-
328-
// We fold the unconditional branch if we can easily update all PHI nodes in
329-
// common successors:
330-
// 1> We have a constant incoming value for the conditional branch;
331-
// 2> We have "Cond" as the incoming value for the unconditional branch;
332-
// 3> SI2->getCondition() and Cond have same operands.
333-
CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
334-
if (!Ci2)
335-
return false;
336-
if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
337-
Cond->getOperand(1) == Ci2->getOperand(1)) &&
338-
!(Cond->getOperand(0) == Ci2->getOperand(1) &&
339-
Cond->getOperand(1) == Ci2->getOperand(0)))
340-
return false;
341-
342-
BasicBlock *SI1BB = SI1->getParent();
343-
BasicBlock *SI2BB = SI2->getParent();
344-
SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
345-
for (BasicBlock *Succ : successors(SI2BB))
346-
if (SI1Succs.count(Succ))
347-
for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
348-
PHINode *PN = cast<PHINode>(BBI);
349-
if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
350-
!isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
351-
return false;
352-
PhiNodes.push_back(PN);
353-
}
354-
return true;
355-
}
356-
357317
/// Update PHI nodes in Succ to indicate that there will now be entries in it
358318
/// from the 'NewPred' block. The values that will be flowing into the PHI nodes
359319
/// will be the same as those coming in from ExistPred, an existing predecessor
@@ -2783,23 +2743,6 @@ bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
27832743
return true;
27842744
}
27852745

2786-
/// Return true if the given instruction is available
2787-
/// in its predecessor block. If yes, the instruction will be removed.
2788-
static bool tryCSEWithPredecessor(Instruction *Inst, BasicBlock *PB) {
2789-
if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2790-
return false;
2791-
for (Instruction &I : *PB) {
2792-
Instruction *PBI = &I;
2793-
// Check whether Inst and PBI generate the same value.
2794-
if (Inst->isIdenticalTo(PBI)) {
2795-
Inst->replaceAllUsesWith(PBI);
2796-
Inst->eraseFromParent();
2797-
return true;
2798-
}
2799-
}
2800-
return false;
2801-
}
2802-
28032746
/// Return true if either PBI or BI has branch weight available, and store
28042747
/// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
28052748
/// not have branch weight, use 1:1 as its weight.
@@ -2830,6 +2773,11 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
28302773
MemorySSAUpdater *MSSAU,
28312774
const TargetTransformInfo *TTI,
28322775
unsigned BonusInstThreshold) {
2776+
// If this block ends with an unconditional branch,
2777+
// let SpeculativelyExecuteBB() deal with it.
2778+
if (!BI->isConditional())
2779+
return false;
2780+
28332781
BasicBlock *BB = BI->getParent();
28342782

28352783
const unsigned PredCount = pred_size(BB);
@@ -2845,37 +2793,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
28452793
BB->getParent()->hasMinSize() ? TargetTransformInfo::TCK_CodeSize
28462794
: TargetTransformInfo::TCK_SizeAndLatency;
28472795

2848-
Instruction *Cond = nullptr;
2849-
if (BI->isConditional())
2850-
Cond = dyn_cast<Instruction>(BI->getCondition());
2851-
else {
2852-
// For unconditional branch, check for a simple CFG pattern, where
2853-
// BB has a single predecessor and BB's successor is also its predecessor's
2854-
// successor. If such pattern exists, check for CSE between BB and its
2855-
// predecessor.
2856-
if (BasicBlock *PB = BB->getSinglePredecessor())
2857-
if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2858-
if (PBI->isConditional() &&
2859-
(BI->getSuccessor(0) == PBI->getSuccessor(0) ||
2860-
BI->getSuccessor(0) == PBI->getSuccessor(1))) {
2861-
for (auto I = BB->instructionsWithoutDebug().begin(),
2862-
E = BB->instructionsWithoutDebug().end();
2863-
I != E;) {
2864-
Instruction *Curr = &*I++;
2865-
if (isa<CmpInst>(Curr)) {
2866-
Cond = Curr;
2867-
break;
2868-
}
2869-
// Quit if we can't remove this instruction.
2870-
if (!tryCSEWithPredecessor(Curr, PB))
2871-
return Changed;
2872-
Changed = true;
2873-
}
2874-
}
2875-
2876-
if (!Cond)
2877-
return Changed;
2878-
}
2796+
Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
28792797

28802798
if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
28812799
Cond->getParent() != BB || !Cond->hasOneUse())
@@ -2934,7 +2852,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
29342852

29352853
// Finally, don't infinitely unroll conditional loops.
29362854
BasicBlock *TrueDest = BI->getSuccessor(0);
2937-
BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
2855+
BasicBlock *FalseDest = BI->getSuccessor(1);
29382856
if (TrueDest == BB || FalseDest == BB)
29392857
return Changed;
29402858

@@ -2945,39 +2863,30 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
29452863
// Check that we have two conditional branches. If there is a PHI node in
29462864
// the common successor, verify that the same value flows in from both
29472865
// blocks.
2948-
SmallVector<PHINode *, 4> PHIs;
2949-
if (!PBI || PBI->isUnconditional() ||
2950-
(BI->isConditional() && !SafeToMergeTerminators(BI, PBI)) ||
2951-
(!BI->isConditional() &&
2952-
!isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
2866+
if (!PBI || PBI->isUnconditional() || !SafeToMergeTerminators(BI, PBI))
29532867
continue;
29542868

29552869
// Determine if the two branches share a common destination.
29562870
Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
29572871
bool InvertPredCond = false;
29582872

2959-
if (BI->isConditional()) {
2960-
if (PBI->getSuccessor(0) == TrueDest) {
2961-
Opc = Instruction::Or;
2962-
} else if (PBI->getSuccessor(1) == FalseDest) {
2963-
Opc = Instruction::And;
2964-
} else if (PBI->getSuccessor(0) == FalseDest) {
2965-
Opc = Instruction::And;
2966-
InvertPredCond = true;
2967-
} else if (PBI->getSuccessor(1) == TrueDest) {
2968-
Opc = Instruction::Or;
2969-
InvertPredCond = true;
2970-
} else {
2971-
continue;
2972-
}
2873+
if (PBI->getSuccessor(0) == TrueDest) {
2874+
Opc = Instruction::Or;
2875+
} else if (PBI->getSuccessor(1) == FalseDest) {
2876+
Opc = Instruction::And;
2877+
} else if (PBI->getSuccessor(0) == FalseDest) {
2878+
Opc = Instruction::And;
2879+
InvertPredCond = true;
2880+
} else if (PBI->getSuccessor(1) == TrueDest) {
2881+
Opc = Instruction::Or;
2882+
InvertPredCond = true;
29732883
} else {
2974-
if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
2975-
continue;
2884+
continue;
29762885
}
29772886

29782887
// Check the cost of inserting the necessary logic before performing the
29792888
// transformation.
2980-
if (TTI && Opc != Instruction::BinaryOpsEnd) {
2889+
if (TTI) {
29812890
Type *Ty = BI->getCondition()->getType();
29822891
unsigned Cost = TTI->getArithmeticInstrCost(Opc, Ty, CostKind);
29832892
if (InvertPredCond && (!PBI->getCondition()->hasOneUse() ||
@@ -2991,8 +2900,6 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
29912900
LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
29922901
Changed = true;
29932902

2994-
SmallVector<DominatorTree::UpdateType, 3> Updates;
2995-
29962903
IRBuilder<> Builder(PBI);
29972904
// The builder is used to create instructions to eliminate the branch in BB.
29982905
// If BB's terminator has !annotation metadata, add it to the new
@@ -3015,16 +2922,12 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30152922
PBI->swapSuccessors();
30162923
}
30172924

3018-
BasicBlock *UniqueSucc =
3019-
BI->isConditional()
3020-
? (PBI->getSuccessor(0) == BB ? TrueDest : FalseDest)
3021-
: TrueDest;
2925+
BasicBlock *UniqueSucc = PBI->getSuccessor(0) == BB ? TrueDest : FalseDest;
30222926

30232927
// Before cloning instructions, notify the successor basic block that it
30242928
// is about to have a new predecessor. This will update PHI nodes,
30252929
// which will allow us to update live-out uses of bonus instructions.
3026-
if (BI->isConditional())
3027-
AddPredecessorToBlock(UniqueSucc, PredBlock, BB, MSSAU);
2930+
AddPredecessorToBlock(UniqueSucc, PredBlock, BB, MSSAU);
30282931

30292932
// If we have bonus instructions, clone them into the predecessor block.
30302933
// Note that there may be multiple predecessor blocks, so we cannot move
@@ -3094,120 +2997,64 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30942997
if (User->getParent() != UniqueSucc)
30952998
return false;
30962999
// Update the incoming value for the new predecessor.
3097-
return PN->getIncomingBlock(U) ==
3098-
(BI->isConditional() ? PredBlock : BB);
3000+
return PN->getIncomingBlock(U) == PredBlock;
30993001
});
31003002
}
31013003

31023004
// Now that the Cond was cloned into the predecessor basic block,
31033005
// or/and the two conditions together.
3104-
if (BI->isConditional()) {
3105-
Instruction *NewCond = cast<Instruction>(
3106-
Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
3107-
PBI->setCondition(NewCond);
3108-
3109-
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3110-
bool HasWeights =
3111-
extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3112-
SuccTrueWeight, SuccFalseWeight);
3113-
SmallVector<uint64_t, 8> NewWeights;
3114-
3115-
if (PBI->getSuccessor(0) == BB) {
3116-
if (HasWeights) {
3117-
// PBI: br i1 %x, BB, FalseDest
3118-
// BI: br i1 %y, UniqueSucc, FalseDest
3119-
// TrueWeight is TrueWeight for PBI * TrueWeight for BI.
3120-
NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
3121-
// FalseWeight is FalseWeight for PBI * TotalWeight for BI +
3122-
// TrueWeight for PBI * FalseWeight for BI.
3123-
// We assume that total weights of a BranchInst can fit into 32 bits.
3124-
// Therefore, we will not have overflow using 64-bit arithmetic.
3125-
NewWeights.push_back(PredFalseWeight *
3126-
(SuccFalseWeight + SuccTrueWeight) +
3127-
PredTrueWeight * SuccFalseWeight);
3128-
}
3129-
PBI->setSuccessor(0, UniqueSucc);
3130-
}
3131-
if (PBI->getSuccessor(1) == BB) {
3132-
if (HasWeights) {
3133-
// PBI: br i1 %x, TrueDest, BB
3134-
// BI: br i1 %y, TrueDest, UniqueSucc
3135-
// TrueWeight is TrueWeight for PBI * TotalWeight for BI +
3136-
// FalseWeight for PBI * TrueWeight for BI.
3137-
NewWeights.push_back(PredTrueWeight *
3138-
(SuccFalseWeight + SuccTrueWeight) +
3139-
PredFalseWeight * SuccTrueWeight);
3140-
// FalseWeight is FalseWeight for PBI * FalseWeight for BI.
3141-
NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
3142-
}
3143-
PBI->setSuccessor(1, UniqueSucc);
3144-
}
3145-
if (NewWeights.size() == 2) {
3146-
// Halve the weights if any of them cannot fit in an uint32_t
3147-
FitWeights(NewWeights);
3006+
Instruction *NewCond = cast<Instruction>(
3007+
Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
3008+
PBI->setCondition(NewCond);
31483009

3149-
SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),
3150-
NewWeights.end());
3151-
setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
3152-
} else
3153-
PBI->setMetadata(LLVMContext::MD_prof, nullptr);
3010+
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3011+
bool HasWeights =
3012+
extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3013+
SuccTrueWeight, SuccFalseWeight);
3014+
SmallVector<uint64_t, 8> NewWeights;
31543015

3155-
Updates.push_back({DominatorTree::Insert, PredBlock, UniqueSucc});
3156-
Updates.push_back({DominatorTree::Delete, PredBlock, BB});
3157-
} else {
3158-
// Update PHI nodes in the common successors.
3159-
for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
3160-
ConstantInt *PBI_C = cast<ConstantInt>(
3161-
PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
3162-
assert(PBI_C->getType()->isIntegerTy(1));
3163-
Instruction *MergedCond = nullptr;
3164-
if (PBI->getSuccessor(0) == UniqueSucc) {
3165-
Updates.push_back(
3166-
{DominatorTree::Delete, PredBlock, PBI->getSuccessor(1)});
3167-
// Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
3168-
// PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
3169-
// is false: !PBI_Cond and BI_Value
3170-
Instruction *NotCond = cast<Instruction>(
3171-
Builder.CreateNot(PBI->getCondition(), "not.cond"));
3172-
MergedCond = cast<Instruction>(
3173-
Builder.CreateBinOp(Instruction::And, NotCond, CondInPred,
3174-
"and.cond"));
3175-
if (PBI_C->isOne())
3176-
MergedCond = cast<Instruction>(Builder.CreateBinOp(
3177-
Instruction::Or, PBI->getCondition(), MergedCond, "or.cond"));
3178-
} else {
3179-
assert(PBI->getSuccessor(1) == UniqueSucc && "Unexpected branch");
3180-
Updates.push_back(
3181-
{DominatorTree::Delete, PredBlock, PBI->getSuccessor(0)});
3182-
// Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
3183-
// PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
3184-
// is false: PBI_Cond and BI_Value
3185-
MergedCond = cast<Instruction>(Builder.CreateBinOp(
3186-
Instruction::And, PBI->getCondition(), CondInPred, "and.cond"));
3187-
if (PBI_C->isOne()) {
3188-
Instruction *NotCond = cast<Instruction>(
3189-
Builder.CreateNot(PBI->getCondition(), "not.cond"));
3190-
MergedCond = cast<Instruction>(Builder.CreateBinOp(
3191-
Instruction::Or, NotCond, MergedCond, "or.cond"));
3192-
}
3193-
}
3194-
// Update PHI Node.
3195-
PHIs[i]->setIncomingValueForBlock(PBI->getParent(), MergedCond);
3016+
if (PBI->getSuccessor(0) == BB) {
3017+
if (HasWeights) {
3018+
// PBI: br i1 %x, BB, FalseDest
3019+
// BI: br i1 %y, UniqueSucc, FalseDest
3020+
// TrueWeight is TrueWeight for PBI * TrueWeight for BI.
3021+
NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
3022+
// FalseWeight is FalseWeight for PBI * TotalWeight for BI +
3023+
// TrueWeight for PBI * FalseWeight for BI.
3024+
// We assume that total weights of a BranchInst can fit into 32 bits.
3025+
// Therefore, we will not have overflow using 64-bit arithmetic.
3026+
NewWeights.push_back(PredFalseWeight *
3027+
(SuccFalseWeight + SuccTrueWeight) +
3028+
PredTrueWeight * SuccFalseWeight);
31963029
}
3197-
3198-
// PBI is changed to branch to UniqueSucc below. Remove itself from
3199-
// potential phis from all other successors.
3200-
if (MSSAU)
3201-
MSSAU->changeCondBranchToUnconditionalTo(PBI, UniqueSucc);
3202-
3203-
// Change PBI from Conditional to Unconditional.
3204-
BranchInst *New_PBI = BranchInst::Create(UniqueSucc, PBI);
3205-
EraseTerminatorAndDCECond(PBI, MSSAU);
3206-
PBI = New_PBI;
3030+
PBI->setSuccessor(0, UniqueSucc);
3031+
}
3032+
if (PBI->getSuccessor(1) == BB) {
3033+
if (HasWeights) {
3034+
// PBI: br i1 %x, TrueDest, BB
3035+
// BI: br i1 %y, TrueDest, UniqueSucc
3036+
// TrueWeight is TrueWeight for PBI * TotalWeight for BI +
3037+
// FalseWeight for PBI * TrueWeight for BI.
3038+
NewWeights.push_back(PredTrueWeight *
3039+
(SuccFalseWeight + SuccTrueWeight) +
3040+
PredFalseWeight * SuccTrueWeight);
3041+
// FalseWeight is FalseWeight for PBI * FalseWeight for BI.
3042+
NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
3043+
}
3044+
PBI->setSuccessor(1, UniqueSucc);
32073045
}
3046+
if (NewWeights.size() == 2) {
3047+
// Halve the weights if any of them cannot fit in an uint32_t
3048+
FitWeights(NewWeights);
3049+
3050+
SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(), NewWeights.end());
3051+
setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
3052+
} else
3053+
PBI->setMetadata(LLVMContext::MD_prof, nullptr);
32083054

32093055
if (DTU)
3210-
DTU->applyUpdates(Updates);
3056+
DTU->applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
3057+
{DominatorTree::Delete, PredBlock, BB}});
32113058

32123059
// If BI was a loop latch, it may have had associated loop metadata.
32133060
// We need to copy it to the new latch, that is, PBI.

0 commit comments

Comments
 (0)