#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#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"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cstdint>
#include <iterator>
#include <map>
#include <utility>
usingnamespacellvm;
#define DEBUG_TYPE …
static cl::opt<bool> PrintBranchProb(
"print-bpi", cl::init(false), cl::Hidden,
cl::desc("Print the branch probability info."));
cl::opt<std::string> PrintBranchProbFuncName(
"print-bpi-func-name", cl::Hidden,
cl::desc("The option to specify the name of the function "
"whose branch probability info is printed."));
INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",
"Branch Probability Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass, "branch-prob",
"Branch Probability Analysis", false, true)
BranchProbabilityInfoWrapperPass::BranchProbabilityInfoWrapperPass()
: … { … }
char BranchProbabilityInfoWrapperPass::ID = …;
static const uint32_t LBH_TAKEN_WEIGHT = …;
static const uint32_t LBH_NONTAKEN_WEIGHT = …;
static const BranchProbability UR_TAKEN_PROB = …;
static const uint32_t PH_TAKEN_WEIGHT = …;
static const uint32_t PH_NONTAKEN_WEIGHT = …;
static const BranchProbability
PtrTakenProb(PH_TAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
static const BranchProbability
PtrUntakenProb(PH_NONTAKEN_WEIGHT, PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
ProbabilityList;
ProbabilityTable;
static const ProbabilityTable PointerTable{ … };
static const uint32_t ZH_TAKEN_WEIGHT = …;
static const uint32_t ZH_NONTAKEN_WEIGHT = …;
static const BranchProbability
ZeroTakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
static const BranchProbability
ZeroUntakenProb(ZH_NONTAKEN_WEIGHT, ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
static const ProbabilityTable ICmpWithZeroTable{ … };
static const ProbabilityTable ICmpWithMinusOneTable{ … };
static const ProbabilityTable ICmpWithOneTable{ … };
static const ProbabilityTable ICmpWithLibCallTable{ … };
static const uint32_t FPH_TAKEN_WEIGHT = …;
static const uint32_t FPH_NONTAKEN_WEIGHT = …;
static const uint32_t FPH_ORD_WEIGHT = …;
static const uint32_t FPH_UNO_WEIGHT = …;
static const BranchProbability FPOrdTakenProb(FPH_ORD_WEIGHT,
FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);
static const BranchProbability
FPOrdUntakenProb(FPH_UNO_WEIGHT, FPH_ORD_WEIGHT + FPH_UNO_WEIGHT);
static const BranchProbability
FPTakenProb(FPH_TAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
static const BranchProbability
FPUntakenProb(FPH_NONTAKEN_WEIGHT, FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
static const ProbabilityTable FCmpTable{ … };
enum class BlockExecWeight : std::uint32_t { … };
BranchProbabilityInfo::SccInfo::SccInfo(const Function &F) { … }
int BranchProbabilityInfo::SccInfo::getSCCNum(const BasicBlock *BB) const { … }
void BranchProbabilityInfo::SccInfo::getSccEnterBlocks(
int SccNum, SmallVectorImpl<BasicBlock *> &Enters) const { … }
void BranchProbabilityInfo::SccInfo::getSccExitBlocks(
int SccNum, SmallVectorImpl<BasicBlock *> &Exits) const { … }
uint32_t BranchProbabilityInfo::SccInfo::getSccBlockType(const BasicBlock *BB,
int SccNum) const { … }
void BranchProbabilityInfo::SccInfo::calculateSccBlockType(const BasicBlock *BB,
int SccNum) { … }
BranchProbabilityInfo::LoopBlock::LoopBlock(const BasicBlock *BB,
const LoopInfo &LI,
const SccInfo &SccI)
: … { … }
bool BranchProbabilityInfo::isLoopEnteringEdge(const LoopEdge &Edge) const { … }
bool BranchProbabilityInfo::isLoopExitingEdge(const LoopEdge &Edge) const { … }
bool BranchProbabilityInfo::isLoopEnteringExitingEdge(
const LoopEdge &Edge) const { … }
bool BranchProbabilityInfo::isLoopBackEdge(const LoopEdge &Edge) const { … }
void BranchProbabilityInfo::getLoopEnterBlocks(
const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters) const { … }
void BranchProbabilityInfo::getLoopExitBlocks(
const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits) const { … }
bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) { … }
bool BranchProbabilityInfo::calcPointerHeuristics(const BasicBlock *BB) { … }
static void
computeUnlikelySuccessors(const BasicBlock *BB, Loop *L,
SmallPtrSetImpl<const BasicBlock*> &UnlikelyBlocks) { … }
std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedBlockWeight(const BasicBlock *BB) const { … }
std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedLoopWeight(const LoopData &L) const { … }
std::optional<uint32_t>
BranchProbabilityInfo::getEstimatedEdgeWeight(const LoopEdge &Edge) const { … }
template <class IterT>
std::optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
const LoopBlock &SrcLoopBB, iterator_range<IterT> Successors) const { … }
bool BranchProbabilityInfo::updateEstimatedBlockWeight(
LoopBlock &LoopBB, uint32_t BBWeight,
SmallVectorImpl<BasicBlock *> &BlockWorkList,
SmallVectorImpl<LoopBlock> &LoopWorkList) { … }
void BranchProbabilityInfo::propagateEstimatedBlockWeight(
const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,
uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,
SmallVectorImpl<LoopBlock> &LoopWorkList) { … }
std::optional<uint32_t>
BranchProbabilityInfo::getInitialEstimatedBlockWeight(const BasicBlock *BB) { … }
void BranchProbabilityInfo::computeEestimateBlockWeight(
const Function &F, DominatorTree *DT, PostDominatorTree *PDT) { … }
bool BranchProbabilityInfo::calcEstimatedHeuristics(const BasicBlock *BB) { … }
bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB,
const TargetLibraryInfo *TLI) { … }
bool BranchProbabilityInfo::calcFloatingPointHeuristics(const BasicBlock *BB) { … }
void BranchProbabilityInfo::releaseMemory() { … }
bool BranchProbabilityInfo::invalidate(Function &, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) { … }
void BranchProbabilityInfo::print(raw_ostream &OS) const { … }
bool BranchProbabilityInfo::
isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const { … }
BranchProbability
BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
unsigned IndexInSuccessors) const { … }
BranchProbability
BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
const_succ_iterator Dst) const { … }
BranchProbability
BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
const BasicBlock *Dst) const { … }
void BranchProbabilityInfo::setEdgeProbability(
const BasicBlock *Src, const SmallVectorImpl<BranchProbability> &Probs) { … }
void BranchProbabilityInfo::copyEdgeProbabilities(BasicBlock *Src,
BasicBlock *Dst) { … }
void BranchProbabilityInfo::swapSuccEdgesProbabilities(const BasicBlock *Src) { … }
raw_ostream &
BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
const BasicBlock *Src,
const BasicBlock *Dst) const { … }
void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) { … }
void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LoopI,
const TargetLibraryInfo *TLI,
DominatorTree *DT,
PostDominatorTree *PDT) { … }
void BranchProbabilityInfoWrapperPass::getAnalysisUsage(
AnalysisUsage &AU) const { … }
bool BranchProbabilityInfoWrapperPass::runOnFunction(Function &F) { … }
void BranchProbabilityInfoWrapperPass::releaseMemory() { … }
void BranchProbabilityInfoWrapperPass::print(raw_ostream &OS,
const Module *) const { … }
AnalysisKey BranchProbabilityAnalysis::Key;
BranchProbabilityInfo
BranchProbabilityAnalysis::run(Function &F, FunctionAnalysisManager &AM) { … }
PreservedAnalyses
BranchProbabilityPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { … }