LLVM: lib/Analysis/BlockFrequencyInfo.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

27#include

28#include

29#include

30

31using namespace llvm;

32

33#define DEBUG_TYPE "block-freq"

34

36 "view-block-freq-propagation-dags", cl::Hidden,

37 cl::desc("Pop up a window to show a dag displaying how block "

38 "frequencies propagation through the CFG."),

41 "display a graph using the "

42 "fractional block frequency representation."),

44 "display a graph using the raw "

45 "integer fractional block frequency representation."),

47 "profile count if available.")));

48

49namespace llvm {

52 cl::desc("The option to specify "

53 "the name of the function "

54 "whose CFG will be displayed."));

55

58 cl::desc("An integer in percent used to specify "

59 "the hot blocks/edges to be displayed "

60 "in red: a block or edge whose frequency "

61 "is no less than the max frequency of the "

62 "function multiplied by this percent."));

63

64

67 cl::desc("A boolean option to show CFG dag or text with "

68 "block profile counts and branch probabilities "

69 "right after PGO profile annotation step. The "

70 "profile counts are computed using branch "

71 "probabilities from the runtime profile data and "

72 "block frequency propagation algorithm. To view "

73 "the raw counts from the profile, use option "

74 "-pgo-view-raw-counts instead. To limit graph "

75 "display to only one function, use filtering option "

76 "-view-bfi-func-name."),

80

82 cl::desc("Print the block frequency info."));

83

86 cl::desc("The option to specify the name of the function "

87 "whose block frequency info is printed."));

88}

89

90namespace llvm {

91

97

98template <>

103

105 return &G->getFunction()->front();

106 }

107

111

113

117

121};

122

125

126template <>

149

150}

151

153

159

162

165 BFI = std::move(RHS.BFI);

166 return *this;

167}

168

169

170

171

172

174

176 FunctionAnalysisManager::Invalidator &) {

177

178

182}

183

187 if (!BFI)

188 BFI.reset(new ImplType);

189 BFI->calculate(F, BPI, LI);

193 }

197 }

198}

199

203

204std::optional<uint64_t>

206 bool AllowSynthetic) const {

207 if (!BFI)

208 return std::nullopt;

209

210 return BFI->getBlockProfileCount(*getFunction(), BB, AllowSynthetic);

211}

212

213std::optional<uint64_t>

215 if (!BFI)

216 return std::nullopt;

217 return BFI->getProfileCountFromFreq(*getFunction(), Freq);

218}

219

221 assert(BFI && "Expected analysis to be available");

222 return BFI->isIrrLoopHeader(BB);

223}

224

227 assert(BFI && "Expected analysis to be available");

228 BFI->setBlockFreq(BB, Freq);

229}

230

234 assert(BFI && "Expected analysis to be available");

235

237 APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());

239 for (auto *BB : BlocksToScale) {

240 BBFreq = BFI->getBlockFreq(BB).getFrequency();

241

242

244

245

246

249 }

250 BFI->setBlockFreq(ReferenceBB, Freq);

251}

252

253

254

258

262

264 return BFI ? &BFI->getBPI() : nullptr;

265}

266

270

272

274 if (BFI)

275 BFI->print(OS);

276}

277

279 if (BFI)

280 BFI->verifyMatch(*Other.BFI);

281}

282

289

294

296 "Block Frequency Analysis", true, true)

301

303

306

308

310 const Module *) const {

311 BFI.print(OS);

312}

313

319

321

326 BFI.calculate(F, BPI, LI);

327 return false;

328}

329

339

342 OS << "Printing analysis results of BFI for function "

343 << "'" << F.getName() << "':"

344 << "\n";

347}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

This file implements a class to represent arbitrary precision integral constant values and operations...

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.")))

#define clEnumValN(ENUMVAL, FLAGNAME, DESC)

This file provides various utilities for inspecting and working with the control flow graph in LLVM I...

This header defines various interfaces for pass management in LLVM.

#define INITIALIZE_PASS_DEPENDENCY(depName)

#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)

#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)

Class for arbitrary precision integers.

This templated class represents "all analyses that operate over " (e....

PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)

Get the result of an analysis pass for a given IR unit.

Represent the analysis usage information of a pass.

AnalysisUsage & addRequired()

void setPreservesAll()

Set by analyses that do not transform their input at all.

LLVM Basic Block Representation.

Analysis pass which computes BlockFrequencyInfo.

LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)

Run the analysis pass over a function and produce BFI.

Definition BlockFrequencyInfo.cpp:331

Legacy analysis pass which computes BlockFrequencyInfo.

void print(raw_ostream &OS, const Module *M) const override

print - Print out the internal state of the pass.

Definition BlockFrequencyInfo.cpp:309

~BlockFrequencyInfoWrapperPass() override

void getAnalysisUsage(AnalysisUsage &AU) const override

getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...

Definition BlockFrequencyInfo.cpp:314

bool runOnFunction(Function &F) override

runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.

Definition BlockFrequencyInfo.cpp:322

BlockFrequencyInfoWrapperPass()

Definition BlockFrequencyInfo.cpp:304

void releaseMemory() override

releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...

Definition BlockFrequencyInfo.cpp:320

BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...

LLVM_ABI bool isIrrLoopHeader(const BasicBlock *BB)

Returns true if BB is an irreducible loop header block.

Definition BlockFrequencyInfo.cpp:220

LLVM_ABI void calculate(const Function &F, const BranchProbabilityInfo &BPI, const LoopInfo &LI)

calculate - compute block frequency info for the given function.

Definition BlockFrequencyInfo.cpp:184

LLVM_ABI std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const

Returns the estimated profile count of Freq.

Definition BlockFrequencyInfo.cpp:214

LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)

Definition BlockFrequencyInfo.cpp:225

LLVM_ABI ~BlockFrequencyInfo()

LLVM_ABI const Function * getFunction() const

Definition BlockFrequencyInfo.cpp:259

LLVM_ABI std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const

Returns the estimated profile count of BB.

Definition BlockFrequencyInfo.cpp:205

BlockFrequencyInfo & operator=(const BlockFrequencyInfo &)=delete

LLVM_ABI BlockFrequencyInfo()

LLVM_ABI void view(StringRef="BlockFrequencyDAGs") const

Pop up a ghostview window with the current block frequency propagation rendered using dot.

Definition BlockFrequencyInfo.cpp:255

LLVM_ABI void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq, SmallPtrSetImpl< BasicBlock * > &BlocksToScale)

Set the frequency of ReferenceBB to Freq and scale the frequencies of the blocks in BlocksToScale suc...

Definition BlockFrequencyInfo.cpp:231

LLVM_ABI const BranchProbabilityInfo * getBPI() const

Definition BlockFrequencyInfo.cpp:263

LLVM_ABI BlockFrequency getEntryFreq() const

Definition BlockFrequencyInfo.cpp:267

LLVM_ABI BlockFrequency getBlockFreq(const BasicBlock *BB) const

getblockFreq - Return block frequency.

Definition BlockFrequencyInfo.cpp:200

LLVM_ABI void print(raw_ostream &OS) const

Definition BlockFrequencyInfo.cpp:273

LLVM_ABI void releaseMemory()

Definition BlockFrequencyInfo.cpp:271

LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)

Handle invalidation explicitly.

Definition BlockFrequencyInfo.cpp:175

LLVM_ABI void verifyMatch(BlockFrequencyInfo &Other) const

Definition BlockFrequencyInfo.cpp:278

LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)

Definition BlockFrequencyInfo.cpp:341

uint64_t getFrequency() const

Returns the frequency as a fixpoint number scaled by the entry frequency.

Analysis pass which computes BranchProbabilityInfo.

Legacy analysis pass which computes BranchProbabilityInfo.

Analysis providing branch probability information.

Represents analyses that only rely on functions' control flow.

const Function & getFunction() const

Analysis pass that exposes the LoopInfo for a function.

The legacy pass manager's analysis pass to compute loop information.

A Module instance is used to store all the information related to an LLVM module.

AnalysisType & getAnalysis() const

getAnalysis() - This function is used by subclasses to get to the analysis information ...

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

PreservedAnalysisChecker getChecker() const

Build a checker for this PreservedAnalyses and the specified analysis type.

Simple wrapper around std::function<void(raw_ostream&)>.

A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...

StringRef - Represent a constant reference to a string, i.e.

This class implements an extremely fast bulk output stream that can only output to a stream.

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

ValuesClass values(OptsTy... Options)

Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...

initializer< Ty > init(const Ty &Val)

This is an optimization pass for GlobalISel generic memory operations.

static GVDAGType getGVDT()

Definition BlockFrequencyInfo.cpp:92

Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)

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."))

static cl::opt< bool > PrintBFI("print-bfi", cl::init(false), cl::Hidden, cl::desc("Print the block frequency info."))

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< 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."))

BFIDOTGraphTraitsBase< BlockFrequencyInfo, BranchProbabilityInfo > BFIDOTGTraitsBase

Definition BlockFrequencyInfo.cpp:123

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)

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.")))

RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)

void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)

ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

LLVM_ABI void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq, BlockFrequency Freq)

LLVM_ABI Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)

Print the block frequency Freq relative to the current functions entry frequency.

Definition BlockFrequencyInfo.cpp:283

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

SuccIterator< const Instruction, const BasicBlock > const_succ_iterator

Implement std::hash so that hash_code can be used in STL containers.

A special type used by analysis passes to provide an address that identifies that particular analysis...

std::string getNodeAttributes(NodeRef Node, const BlockFrequencyInfo *Graph, unsigned HotPercentThreshold=0)

typename GTraits::ChildIteratorType EdgeIter

std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfo *Graph, GVDAGType GType, int layout_order=-1)

std::string getEdgeAttributes(NodeRef Node, EdgeIter EI, const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI, unsigned HotPercentThreshold=0)

std::string getNodeAttributes(const BasicBlock *Node, const BlockFrequencyInfo *Graph)

Definition BlockFrequencyInfo.cpp:137

DOTGraphTraits(bool isSimple=false)

Definition BlockFrequencyInfo.cpp:128

std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI, const BlockFrequencyInfo *BFI)

Definition BlockFrequencyInfo.cpp:143

std::string getNodeLabel(const BasicBlock *Node, const BlockFrequencyInfo *Graph)

Definition BlockFrequencyInfo.cpp:131

static ChildIteratorType child_begin(const NodeRef N)

Definition BlockFrequencyInfo.cpp:108

static NodeRef getEntryNode(const BlockFrequencyInfo *G)

Definition BlockFrequencyInfo.cpp:104

const_succ_iterator ChildIteratorType

Definition BlockFrequencyInfo.cpp:101

static ChildIteratorType child_end(const NodeRef N)

Definition BlockFrequencyInfo.cpp:112

static nodes_iterator nodes_end(const BlockFrequencyInfo *G)

Definition BlockFrequencyInfo.cpp:118

const BasicBlock * NodeRef

Definition BlockFrequencyInfo.cpp:100

pointer_iterator< Function::const_iterator > nodes_iterator

Definition BlockFrequencyInfo.cpp:102

static nodes_iterator nodes_begin(const BlockFrequencyInfo *G)

Definition BlockFrequencyInfo.cpp:114