mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-25 17:20:48 +00:00
PGO: preserve branch-weight metadata when simplifying two branches with a common
destination in SimplifyCondBranchToCondBranch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164054 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -2150,6 +2150,33 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
|
||||
PBI->setSuccessor(0, CommonDest);
|
||||
PBI->setSuccessor(1, OtherDest);
|
||||
|
||||
// Update branch weight for PBI.
|
||||
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
|
||||
bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
|
||||
PredFalseWeight);
|
||||
bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
|
||||
SuccFalseWeight);
|
||||
if (PredHasWeights && SuccHasWeights) {
|
||||
uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
|
||||
uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
|
||||
uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
|
||||
uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
|
||||
// The weight to CommonDest should be PredCommon * SuccTotal +
|
||||
// PredOther * SuccCommon.
|
||||
// The weight to OtherDest should be PredOther * SuccOther.
|
||||
SmallVector<uint64_t, 2> NewWeights;
|
||||
NewWeights.push_back(PredCommon * (SuccCommon + SuccOther) +
|
||||
PredOther * SuccCommon);
|
||||
NewWeights.push_back(PredOther * SuccOther);
|
||||
// Halve the weights if any of them cannot fit in an uint32_t
|
||||
FitWeights(NewWeights);
|
||||
|
||||
SmallVector<uint32_t, 2> MDWeights(NewWeights.begin(),NewWeights.end());
|
||||
PBI->setMetadata(LLVMContext::MD_prof,
|
||||
MDBuilder(BI->getContext()).
|
||||
createBranchWeights(MDWeights));
|
||||
}
|
||||
|
||||
// OtherDest may have phi nodes. If so, add an entry from PBI's
|
||||
// block that are identical to the entries for BI's block.
|
||||
AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
|
||||
|
Reference in New Issue
Block a user