[BOLT] Sort basic block successors for printing

Summary:
For easier analysis of the hottest targets of jump tables it helps to
have basic block successors sorted based on the taken frequency.

(cherry picked from FBD14856640)
This commit is contained in:
Maksim Panchenko 2019-04-09 11:27:23 -07:00
parent a8e05d067d
commit 88375d311e
2 changed files with 23 additions and 9 deletions

View File

@ -49,6 +49,12 @@ public:
struct BinaryBranchInfo {
uint64_t Count;
uint64_t MispredictedCount; /// number of branches mispredicted
bool operator<(const BinaryBranchInfo &Other) const {
return (Count < Other.Count) ||
(Count == Other.Count &&
MispredictedCount < Other.MispredictedCount);
}
};
static constexpr uint32_t INVALID_OFFSET =

View File

@ -556,21 +556,29 @@ void BinaryFunction::print(raw_ostream &OS, std::string Annotation,
if (!BB->succ_empty()) {
OS << " Successors: ";
auto BI = BB->branch_info_begin();
// For more than 2 successors, sort them based on frequency.
std::vector<uint64_t> Indices(BB->succ_size());
std::iota(Indices.begin(), Indices.end(), 0);
if (BB->succ_size() > 2 && BB->getKnownExecutionCount()) {
std::stable_sort(Indices.begin(), Indices.end(),
[&](const uint64_t A, const uint64_t B) {
return BB->BranchInfo[B] < BB->BranchInfo[A];
});
}
auto Sep = "";
for (auto Succ : BB->successors()) {
assert(BI != BB->branch_info_end() && "missing BranchInfo entry");
for (unsigned I = 0; I < Indices.size(); ++I) {
auto *Succ = BB->Successors[Indices[I]];
auto &BI = BB->BranchInfo[Indices[I]];
OS << Sep << Succ->getName();
if (ExecutionCount != COUNT_NO_PROFILE &&
BI->MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
OS << " (mispreds: " << BI->MispredictedCount
<< ", count: " << BI->Count << ")";
BI.MispredictedCount != BinaryBasicBlock::COUNT_INFERRED) {
OS << " (mispreds: " << BI.MispredictedCount
<< ", count: " << BI.Count << ")";
} else if (ExecutionCount != COUNT_NO_PROFILE &&
BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
OS << " (inferred count: " << BI->Count << ")";
BI.Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
OS << " (inferred count: " << BI.Count << ")";
}
Sep = ", ";
++BI;
}
OS << '\n';
}