#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <optional>
#include <string>
usingnamespacellvm;
#define DEBUG_TYPE …
static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
"view-block-freq-propagation-dags", cl::Hidden,
cl::desc("Pop up a window to show a dag displaying how block "
"frequencies propagation through the CFG."),
cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
clEnumValN(GVDT_Fraction, "fraction",
"display a graph using the "
"fractional block frequency representation."),
clEnumValN(GVDT_Integer, "integer",
"display a graph using the raw "
"integer fractional block frequency representation."),
clEnumValN(GVDT_Count, "count", "display a graph using the real "
"profile count if available.")));
namespace llvm {
cl::opt<std::string>
ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
cl::desc("The option to specify "
"the name of the function "
"whose CFG will be displayed."));
cl::opt<unsigned>
ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
cl::desc("An integer in percent used to specify "
"the hot blocks/edges to be displayed "
"in red: a block or edge whose frequency "
"is no less than the max frequency of the "
"function multiplied by this percent."));
cl::opt<PGOViewCountsType> PGOViewCounts(
"pgo-view-counts", cl::Hidden,
cl::desc("A boolean option to show CFG dag or text with "
"block profile counts and branch probabilities "
"right after PGO profile annotation step. The "
"profile counts are computed using branch "
"probabilities from the runtime profile data and "
"block frequency propagation algorithm. To view "
"the raw counts from the profile, use option "
"-pgo-view-raw-counts instead. To limit graph "
"display to only one function, use filtering option "
"-view-bfi-func-name."),
cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
clEnumValN(PGOVCT_Text, "text", "show in text.")));
static cl::opt<bool> PrintBFI("print-bfi", cl::init(false), cl::Hidden,
cl::desc("Print the block frequency info."));
cl::opt<std::string>
PrintBFIFuncName("print-bfi-func-name", cl::Hidden,
cl::desc("The option to specify the name of the function "
"whose block frequency info is printed."));
}
namespace llvm {
static GVDAGType getGVDT() { … }
template <>
struct GraphTraits<BlockFrequencyInfo *> { … };
BFIDOTGTraitsBase;
template <>
struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase { … };
}
BlockFrequencyInfo::BlockFrequencyInfo() = default;
BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
const BranchProbabilityInfo &BPI,
const LoopInfo &LI) { … }
BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
: … { … }
BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) { … }
BlockFrequencyInfo::~BlockFrequencyInfo() = default;
bool BlockFrequencyInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) { … }
void BlockFrequencyInfo::calculate(const Function &F,
const BranchProbabilityInfo &BPI,
const LoopInfo &LI) { … }
BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const { … }
std::optional<uint64_t>
BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB,
bool AllowSynthetic) const { … }
std::optional<uint64_t>
BlockFrequencyInfo::getProfileCountFromFreq(BlockFrequency Freq) const { … }
bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock *BB) { … }
void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB,
BlockFrequency Freq) { … }
void BlockFrequencyInfo::setBlockFreqAndScale(
const BasicBlock *ReferenceBB, BlockFrequency Freq,
SmallPtrSetImpl<BasicBlock *> &BlocksToScale) { … }
void BlockFrequencyInfo::view(StringRef title) const { … }
const Function *BlockFrequencyInfo::getFunction() const { … }
const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const { … }
BlockFrequency BlockFrequencyInfo::getEntryFreq() const { … }
void BlockFrequencyInfo::releaseMemory() { … }
void BlockFrequencyInfo::print(raw_ostream &OS) const { … }
void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const { … }
Printable llvm::printBlockFreq(const BlockFrequencyInfo &BFI,
BlockFrequency Freq) { … }
Printable llvm::printBlockFreq(const BlockFrequencyInfo &BFI,
const BasicBlock &BB) { … }
INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
"Block Frequency Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
"Block Frequency Analysis", true, true)
char BlockFrequencyInfoWrapperPass::ID = …;
BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
: … { … }
BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
const Module *) const { … }
void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { … }
void BlockFrequencyInfoWrapperPass::releaseMemory() { … }
bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) { … }
AnalysisKey BlockFrequencyAnalysis::Key;
BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
FunctionAnalysisManager &AM) { … }
PreservedAnalyses
BlockFrequencyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { … }