Skip to content

Commit 4931158

Browse files
author
Yevgeny Rouban
committed
[BranchProbabilityInfo] Get rid of MaxSuccIdx. NFC
This refactoring allows to eliminate the MaxSuccIdx map proposed in the commit a7b662d. The idea is to remove probabilities for a block BB for all its successors one by one from first, second, ... till N-th until they are defined in Probs. This works because probabilities for the block are set at once for all its successors from number 0 to N-1 and the rest are removed if there were stale probs. The protected method setEdgeProbability(), which set probabilities for individual successor, is removed. This makes it clear that the probabilities are set in bulk by the public method with the same name. Reviewed By: kazu, MaskRay Differential Revision: https://reviews.llvm.org/D90837
1 parent 2af37cf commit 4931158

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

llvm/include/llvm/Analysis/BranchProbabilityInfo.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class BranchProbabilityInfo {
6262
}
6363

6464
BranchProbabilityInfo(BranchProbabilityInfo &&Arg)
65-
: Probs(std::move(Arg.Probs)), MaxSuccIdx(std::move(Arg.MaxSuccIdx)),
66-
LastF(Arg.LastF),
65+
: Probs(std::move(Arg.Probs)), LastF(Arg.LastF),
6766
PostDominatedByUnreachable(std::move(Arg.PostDominatedByUnreachable)),
6867
PostDominatedByColdCall(std::move(Arg.PostDominatedByColdCall)) {}
6968

@@ -73,7 +72,6 @@ class BranchProbabilityInfo {
7372
BranchProbabilityInfo &operator=(BranchProbabilityInfo &&RHS) {
7473
releaseMemory();
7574
Probs = std::move(RHS.Probs);
76-
MaxSuccIdx = std::move(RHS.MaxSuccIdx);
7775
PostDominatedByColdCall = std::move(RHS.PostDominatedByColdCall);
7876
PostDominatedByUnreachable = std::move(RHS.PostDominatedByUnreachable);
7977
return *this;
@@ -124,16 +122,6 @@ class BranchProbabilityInfo {
124122
raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
125123
const BasicBlock *Dst) const;
126124

127-
protected:
128-
/// Set the raw edge probability for the given edge.
129-
///
130-
/// This allows a pass to explicitly set the edge probability for an edge. It
131-
/// can be used when updating the CFG to update and preserve the branch
132-
/// probability information. Read the implementation of how these edge
133-
/// probabilities are calculated carefully before using!
134-
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors,
135-
BranchProbability Prob);
136-
137125
public:
138126
/// Set the raw probabilities for all edges from the given block.
139127
///
@@ -275,9 +263,6 @@ class BranchProbabilityInfo {
275263

276264
DenseMap<Edge, BranchProbability> Probs;
277265

278-
// The maximum successor index ever entered for a given basic block.
279-
DenseMap<const BasicBlock *, unsigned> MaxSuccIdx;
280-
281266
/// Track the last function we run over for printing.
282267
const Function *LastF = nullptr;
283268

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,6 @@ bool BranchProbabilityInfo::calcInvokeHeuristics(const BasicBlock *BB) {
10311031

10321032
void BranchProbabilityInfo::releaseMemory() {
10331033
Probs.clear();
1034-
MaxSuccIdx.clear();
10351034
Handles.clear();
10361035
}
10371036

@@ -1093,6 +1092,10 @@ BranchProbability
10931092
BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
10941093
unsigned IndexInSuccessors) const {
10951094
auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
1095+
assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==
1096+
(Probs.end() == I) &&
1097+
"Probability for I-th successor must always be defined along with the "
1098+
"probability for the first successor");
10961099

10971100
if (I != Probs.end())
10981101
return I->second;
@@ -1127,31 +1130,21 @@ BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
11271130
return FoundProb ? Prob : BranchProbability(EdgeCount, succ_num);
11281131
}
11291132

1130-
/// Set the edge probability for a given edge specified by PredBlock and an
1131-
/// index to the successors.
1132-
void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src,
1133-
unsigned IndexInSuccessors,
1134-
BranchProbability Prob) {
1135-
Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;
1136-
Handles.insert(BasicBlockCallbackVH(Src, this));
1137-
LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
1138-
<< IndexInSuccessors << " successor probability to " << Prob
1139-
<< "\n");
1140-
1141-
unsigned &SuccIdx = MaxSuccIdx[Src];
1142-
SuccIdx = std::max(SuccIdx, IndexInSuccessors);
1143-
}
1144-
11451133
/// Set the edge probability for all edges at once.
11461134
void BranchProbabilityInfo::setEdgeProbability(
11471135
const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) {
11481136
assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
1137+
eraseBlock(Src); // Erase stale data if any.
11491138
if (Probs.size() == 0)
11501139
return; // Nothing to set.
11511140

11521141
uint64_t TotalNumerator = 0;
11531142
for (unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {
1154-
setEdgeProbability(Src, SuccIdx, Probs[SuccIdx]);
1143+
this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];
1144+
Handles.insert(BasicBlockCallbackVH(Src, this));
1145+
LLVM_DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << SuccIdx
1146+
<< " successor probability to " << Probs[SuccIdx]
1147+
<< "\n");
11551148
TotalNumerator += Probs[SuccIdx].getNumerator();
11561149
}
11571150

@@ -1179,16 +1172,20 @@ BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
11791172
void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {
11801173
// Note that we cannot use successors of BB because the terminator of BB may
11811174
// have changed when eraseBlock is called as a BasicBlockCallbackVH callback.
1182-
auto It = MaxSuccIdx.find(BB);
1183-
if (It == MaxSuccIdx.end())
1184-
return;
1185-
1186-
for (unsigned I = 0, E = It->second; I <= E; ++I) {
1175+
// Instead we remove prob data for the block by iterating successors by their
1176+
// indices from 0 till the last which exists. There could not be prob data for
1177+
// a pair (BB, N) if there is no data for (BB, N-1) because the data is always
1178+
// set for all successors from 0 to M at once by the method
1179+
// setEdgeProbability().
1180+
for (unsigned I = 0;; ++I) {
11871181
auto MapI = Probs.find(std::make_pair(BB, I));
1188-
if (MapI != Probs.end())
1189-
Probs.erase(MapI);
1182+
if (MapI == Probs.end()) {
1183+
assert(Probs.count(std::make_pair(BB, I + 1)) == 0 &&
1184+
"Must be no more successors");
1185+
return;
1186+
}
1187+
Probs.erase(MapI);
11901188
}
1191-
MaxSuccIdx.erase(BB);
11921189
}
11931190

11941191
void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI,

0 commit comments

Comments
 (0)