aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/BranchProbabilityInfo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Analysis/BranchProbabilityInfo.cpp')
-rw-r--r--llvm/lib/Analysis/BranchProbabilityInfo.cpp56
1 files changed, 23 insertions, 33 deletions
diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
index f45728768fcd..7931001d0a2b 100644
--- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -31,6 +31,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PassManager.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/InitializePasses.h"
@@ -379,21 +380,16 @@ bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
const Instruction *TI = BB->getTerminator();
assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI) ||
- isa<InvokeInst>(TI)))
+ isa<InvokeInst>(TI) || isa<CallBrInst>(TI)))
return false;
- MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
+ MDNode *WeightsNode = getValidBranchWeightMDNode(*TI);
if (!WeightsNode)
return false;
// Check that the number of successors is manageable.
assert(TI->getNumSuccessors() < UINT32_MAX && "Too many successors");
- // Ensure there are weights for all of the successors. Note that the first
- // operand to the metadata node is a name, not a weight.
- if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1)
- return false;
-
// Build up the final weights that will be used in a temporary buffer.
// Compute the sum of all weights to later decide whether they need to
// be scaled to fit in 32 bits.
@@ -401,24 +397,18 @@ bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
SmallVector<uint32_t, 2> Weights;
SmallVector<unsigned, 2> UnreachableIdxs;
SmallVector<unsigned, 2> ReachableIdxs;
- Weights.reserve(TI->getNumSuccessors());
- for (unsigned I = 1, E = WeightsNode->getNumOperands(); I != E; ++I) {
- ConstantInt *Weight =
- mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(I));
- if (!Weight)
- return false;
- assert(Weight->getValue().getActiveBits() <= 32 &&
- "Too many bits for uint32_t");
- Weights.push_back(Weight->getZExtValue());
- WeightSum += Weights.back();
+
+ extractBranchWeights(WeightsNode, Weights);
+ for (unsigned I = 0, E = Weights.size(); I != E; ++I) {
+ WeightSum += Weights[I];
const LoopBlock SrcLoopBB = getLoopBlock(BB);
- const LoopBlock DstLoopBB = getLoopBlock(TI->getSuccessor(I - 1));
+ const LoopBlock DstLoopBB = getLoopBlock(TI->getSuccessor(I));
auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
if (EstimatedWeight &&
*EstimatedWeight <= static_cast<uint32_t>(BlockExecWeight::UNREACHABLE))
- UnreachableIdxs.push_back(I - 1);
+ UnreachableIdxs.push_back(I);
else
- ReachableIdxs.push_back(I - 1);
+ ReachableIdxs.push_back(I);
}
assert(Weights.size() == TI->getNumSuccessors() && "Checked above");
@@ -652,23 +642,23 @@ computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
}
}
-Optional<uint32_t>
+std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedBlockWeight(const BasicBlock *BB) const {
auto WeightIt = EstimatedBlockWeight.find(BB);
if (WeightIt == EstimatedBlockWeight.end())
- return None;
+ return std::nullopt;
return WeightIt->second;
}
-Optional<uint32_t>
+std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedLoopWeight(const LoopData &L) const {
auto WeightIt = EstimatedLoopWeight.find(L);
if (WeightIt == EstimatedLoopWeight.end())
- return None;
+ return std::nullopt;
return WeightIt->second;
}
-Optional<uint32_t>
+std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const {
// For edges entering a loop take weight of a loop rather than an individual
// block in the loop.
@@ -678,16 +668,16 @@ BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const {
}
template <class IterT>
-Optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
+std::optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
const LoopBlock &SrcLoopBB, iterator_range<IterT> Successors) const {
SmallVector<uint32_t, 4> Weights;
- Optional<uint32_t> MaxWeight;
+ std::optional<uint32_t> MaxWeight;
for (const BasicBlock *DstBB : Successors) {
const LoopBlock DstLoopBB = getLoopBlock(DstBB);
auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
if (!Weight)
- return None;
+ return std::nullopt;
if (!MaxWeight || *MaxWeight < *Weight)
MaxWeight = Weight;
@@ -772,8 +762,8 @@ void BranchProbabilityInfo::propagateEstimatedBlockWeight(
}
}
-Optional<uint32_t> BranchProbabilityInfo::getInitialEstimatedBlockWeight(
- const BasicBlock *BB) {
+std::optional<uint32_t>
+BranchProbabilityInfo::getInitialEstimatedBlockWeight(const BasicBlock *BB) {
// Returns true if \p BB has call marked with "NoReturn" attribute.
auto hasNoReturn = [&](const BasicBlock *BB) {
for (const auto &I : reverse(*BB))
@@ -810,7 +800,7 @@ Optional<uint32_t> BranchProbabilityInfo::getInitialEstimatedBlockWeight(
if (CI->hasFnAttr(Attribute::Cold))
return static_cast<uint32_t>(BlockExecWeight::COLD);
- return None;
+ return std::nullopt;
}
// Does RPO traversal over all blocks in \p F and assigns weights to
@@ -828,7 +818,7 @@ void BranchProbabilityInfo::computeEestimateBlockWeight(
if (auto BBWeight = getInitialEstimatedBlockWeight(BB))
// If we were able to find estimated weight for the block set it to this
// block and propagate up the IR.
- propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT, BBWeight.value(),
+ propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT, *BBWeight,
BlockWorkList, LoopWorkList);
// BlockWorklist/LoopWorkList contains blocks/loops with at least one
@@ -900,7 +890,7 @@ bool BranchProbabilityInfo::calcEstimatedHeuristics(const BasicBlock *BB) {
uint64_t TotalWeight = 0;
// Go over all successors of BB and put their weights into SuccWeights.
for (const BasicBlock *SuccBB : successors(BB)) {
- Optional<uint32_t> Weight;
+ std::optional<uint32_t> Weight;
const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);
const LoopEdge Edge{LoopBB, SuccLoopBB};