[BOLT] Fix branch info stats after SCTC

Summary:
SCTC was incorrectly swapping BranchInfo when reversing the branch condition.  This was wrong because when we remove the successor BB later, it removes the BranchInfo for that BB.  In this case the successor would be the BB with the stats we had just swapped.

Instead leave BranchInfo as it is and read the branch count from the false or true branch depending on whether we reverse or replace the branch, respectively.  The call to removeSuccessor later will remove the unused BranchInfo we no longer care about.

(cherry picked from FBD6876799)
This commit is contained in:
Bill Nell 2018-02-01 14:24:26 -08:00 committed by Maksim Panchenko
parent 1207e1d229
commit 501601259b
1 changed files with 7 additions and 7 deletions

View File

@ -875,6 +875,7 @@ uint64_t SimplifyConditionalTailCalls::fixTailCalls(BinaryContext &BC,
// Record this block so that we don't try to optimize it twice.
BeenOptimized.insert(PredBB);
bool BranchForStats;
if (CondSucc != BB) {
// Patch the new target address into the conditional branch.
MIA->reverseBranchCondition(*CondBranch, CalleeSymbol, BC.Ctx.get());
@ -883,17 +884,16 @@ uint64_t SimplifyConditionalTailCalls::fixTailCalls(BinaryContext &BC,
// branch to the old target. This has to be done manually since
// fixupBranches is not called after SCTC.
NeedsUncondBranch.emplace_back(std::make_pair(PredBB, CondSucc));
// Swap branch statistics after swapping the branch targets.
auto BI = PredBB->branch_info_begin();
std::swap(*BI, *(BI + 1));
BranchForStats = false;
} else {
// Change destination of the conditional branch.
MIA->replaceBranchTarget(*CondBranch, CalleeSymbol, BC.Ctx.get());
BranchForStats = true;
}
const uint64_t CTCTakenFreq = PredBB->getBranchInfo(true).Count ==
BinaryBasicBlock::COUNT_NO_PROFILE
? 0
: PredBB->getBranchInfo(true).Count;
const auto Count = PredBB->getBranchInfo(BranchForStats).Count;
const uint64_t CTCTakenFreq =
Count == BinaryBasicBlock::COUNT_NO_PROFILE ? 0 : Count;
// Annotate it, so "isCall" returns true for this jcc
MIA->setConditionalTailCall(*CondBranch);
// Add info abount the conditional tail call frequency, otherwise this