Skip to content

Commit

Permalink
[BPI] Replace weights by probabilities in BPI.
Browse files Browse the repository at this point in the history
This patch removes all weight-related interfaces from BPI and replace
them by probability versions. With this patch, we won't use edge weight
anymore in either IR or MC passes. Edge probabilitiy is a better
representation in terms of CFG update and validation.


Differential revision: http://reviews.llvm.org/D15519 

llvm-svn: 256263
  • Loading branch information
Cong Hou committed Dec 22, 2015
1 parent 4e4f60d commit e93b8e1
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 280 deletions.
39 changes: 7 additions & 32 deletions llvm/include/llvm/Analysis/BranchProbabilityInfo.h
Expand Up @@ -84,36 +84,14 @@ class BranchProbabilityInfo {
raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
const BasicBlock *Dst) const;

/// \brief Get the raw edge weight calculated for the edge.
/// \brief Set the raw edge probability for the given edge.
///
/// This returns the raw edge weight. It is guaranteed to fall between 1 and
/// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.
/// This interface should be very carefully, and primarily by routines that
/// are updating the analysis by later calling setEdgeWeight.
uint32_t getEdgeWeight(const BasicBlock *Src,
unsigned IndexInSuccessors) const;

/// \brief Get the raw edge weight calculated for the block pair.
///
/// This returns the sum of all raw edge weights from Src to Dst.
/// It is guaranteed to fall between 1 and UINT32_MAX.
uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;

uint32_t getEdgeWeight(const BasicBlock *Src,
succ_const_iterator Dst) const;

/// \brief Set the raw edge weight for a given edge.
///
/// This allows a pass to explicitly set the edge weight for an edge. It can
/// be used when updating the CFG to update and preserve the branch
/// This allows a pass to explicitly set the edge probability for an edge. It
/// can be used when updating the CFG to update and preserve the branch
/// probability information. Read the implementation of how these edge
/// weights are calculated carefully before using!
void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
uint32_t Weight);

static uint32_t getBranchWeightStackProtector(bool IsLikely) {
return IsLikely ? (1u << 20) - 1 : 1;
}
/// probabilities are calculated carefully before using!
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors,
BranchProbability Prob);

static BranchProbability getBranchProbStackProtector(bool IsLikely) {
static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20);
Expand All @@ -135,7 +113,7 @@ class BranchProbabilityInfo {
// weight to just "inherit" the non-zero weight of an adjacent successor.
static const uint32_t DEFAULT_WEIGHT = 16;

DenseMap<Edge, uint32_t> Weights;
DenseMap<Edge, BranchProbability> Probs;

/// \brief Track the last function we run over for printing.
Function *LastF;
Expand All @@ -146,9 +124,6 @@ class BranchProbabilityInfo {
/// \brief Track the set of blocks that always lead to a cold call.
SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;

/// \brief Get sum of the block successors' weights.
uint32_t getSumForBlock(const BasicBlock *BB) const;

bool calcUnreachableHeuristics(BasicBlock *BB);
bool calcMetadataWeights(BasicBlock *BB);
bool calcColdCallHeuristics(BasicBlock *BB);
Expand Down
48 changes: 0 additions & 48 deletions llvm/include/llvm/Support/BranchProbability.h
Expand Up @@ -63,11 +63,6 @@ class BranchProbability {
static void normalizeProbabilities(ProbabilityIter Begin,
ProbabilityIter End);

// Normalize a list of weights by scaling them down so that the sum of them
// doesn't exceed UINT32_MAX.
template <class WeightListIter>
static void normalizeEdgeWeights(WeightListIter Begin, WeightListIter End);

uint32_t getNumerator() const { return N; }
static uint32_t getDenominator() { return D; }

Expand Down Expand Up @@ -219,49 +214,6 @@ void BranchProbability::normalizeProbabilities(ProbabilityIter Begin,
I->N = (I->N * uint64_t(D) + Sum / 2) / Sum;
}

template <class WeightListIter>
void BranchProbability::normalizeEdgeWeights(WeightListIter Begin,
WeightListIter End) {
// First we compute the sum with 64-bits of precision.
uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));

if (Sum > UINT32_MAX) {
// Compute the scale necessary to cause the weights to fit, and re-sum with
// that scale applied.
assert(Sum / UINT32_MAX < UINT32_MAX &&
"The sum of weights exceeds UINT32_MAX^2!");
uint32_t Scale = Sum / UINT32_MAX + 1;
for (auto I = Begin; I != End; ++I)
*I /= Scale;
Sum = std::accumulate(Begin, End, uint64_t(0));
}

// Eliminate zero weights.
auto ZeroWeightNum = std::count(Begin, End, 0u);
if (ZeroWeightNum > 0) {
// If all weights are zeros, replace them by 1.
if (Sum == 0)
std::fill(Begin, End, 1u);
else {
// We are converting zeros into ones, and here we need to make sure that
// after this the sum won't exceed UINT32_MAX.
if (Sum + ZeroWeightNum > UINT32_MAX) {
for (auto I = Begin; I != End; ++I)
*I /= 2;
ZeroWeightNum = std::count(Begin, End, 0u);
Sum = std::accumulate(Begin, End, uint64_t(0));
}
// Scale up non-zero weights and turn zero weights into ones.
uint64_t ScalingFactor = (UINT32_MAX - ZeroWeightNum) / Sum;
assert(ScalingFactor >= 1);
if (ScalingFactor > 1)
for (auto I = Begin; I != End; ++I)
*I *= ScalingFactor;
std::replace(Begin, End, 0u, 1u);
}
}
}

}

#endif

0 comments on commit e93b8e1

Please sign in to comment.