Skip to content

Commit 7b89efb

Browse files
committed
[NFC][SimplifyCFG] FoldBranchToCommonDest(): somewhat better structure weight updating code
Hoist the successor updating out of the code that deals with branch weight updating, and hoist the 'has weights' check from the latter, making code more consistent and easier to follow.
1 parent 256a035 commit 7b89efb

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,13 +3005,11 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30053005
PBI->setCondition(NewCond);
30063006

30073007
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3008-
bool HasWeights =
3009-
extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3010-
SuccTrueWeight, SuccFalseWeight);
3011-
SmallVector<uint64_t, 8> NewWeights;
3008+
if (extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3009+
SuccTrueWeight, SuccFalseWeight)) {
3010+
SmallVector<uint64_t, 8> NewWeights;
30123011

3013-
if (PBI->getSuccessor(0) == BB) {
3014-
if (HasWeights) {
3012+
if (PBI->getSuccessor(0) == BB) {
30153013
// PBI: br i1 %x, BB, FalseDest
30163014
// BI: br i1 %y, UniqueSucc, FalseDest
30173015
// TrueWeight is TrueWeight for PBI * TrueWeight for BI.
@@ -3023,11 +3021,7 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30233021
NewWeights.push_back(PredFalseWeight *
30243022
(SuccFalseWeight + SuccTrueWeight) +
30253023
PredTrueWeight * SuccFalseWeight);
3026-
}
3027-
PBI->setSuccessor(0, UniqueSucc);
3028-
}
3029-
if (PBI->getSuccessor(1) == BB) {
3030-
if (HasWeights) {
3024+
} else {
30313025
// PBI: br i1 %x, TrueDest, BB
30323026
// BI: br i1 %y, TrueDest, UniqueSucc
30333027
// TrueWeight is TrueWeight for PBI * TotalWeight for BI +
@@ -3038,17 +3032,20 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30383032
// FalseWeight is FalseWeight for PBI * FalseWeight for BI.
30393033
NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
30403034
}
3041-
PBI->setSuccessor(1, UniqueSucc);
3042-
}
3043-
if (NewWeights.size() == 2) {
3035+
30443036
// Halve the weights if any of them cannot fit in an uint32_t
30453037
FitWeights(NewWeights);
30463038

30473039
SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(), NewWeights.end());
30483040
setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
3041+
3042+
// TODO: If BB is reachable from all paths through PredBlock, then we
3043+
// could replace PBI's branch probabilities with BI's.
30493044
} else
30503045
PBI->setMetadata(LLVMContext::MD_prof, nullptr);
30513046

3047+
PBI->setSuccessor(PBI->getSuccessor(0) != BB, UniqueSucc);
3048+
30523049
if (DTU)
30533050
DTU->applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
30543051
{DominatorTree::Delete, PredBlock, BB}});
@@ -3058,9 +3055,6 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30583055
if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
30593056
PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
30603057

3061-
// TODO: If BB is reachable from all paths through PredBlock, then we
3062-
// could replace PBI's branch probabilities with BI's.
3063-
30643058
// Copy any debug value intrinsics into the end of PredBlock.
30653059
for (Instruction &I : *BB) {
30663060
if (isa<DbgInfoIntrinsic>(I)) {

0 commit comments

Comments
 (0)