LLVM: lib/CodeGen/MachineBlockFrequencyInfo.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

25#include

26#include

27

28using namespace llvm;

29

30#define DEBUG_TYPE "machine-block-freq"

31

32namespace llvm {

34 "view-machine-block-freq-propagation-dags", cl::Hidden,

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

36 "frequencies propagate through the CFG."),

39 "display a graph using the "

40 "fractional block frequency representation."),

42 "display a graph using the raw "

43 "integer fractional block frequency representation."),

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

46

47

49 "view-block-layout-with-bfi", cl::Hidden,

51 "Pop up a window to show a dag displaying MBP layout and associated "

52 "block frequencies of the CFG."),

55 "display a graph using the "

56 "fractional block frequency representation."),

58 "display a graph using the raw "

59 "integer fractional block frequency representation."),

61 "display a graph using the real "

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

63

64

65

67

68

69

71

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

75

76

77

79}

80

84

86}

87

88namespace llvm {

89

94

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

97 }

98

100 return N->succ_begin();

101 }

102

104

107 }

108

111 }

112};

113

117

118template <>

123

126

129 int layout_order = -1;

130

133 if (!CurFunc || F != CurFunc) {

134 if (CurFunc)

135 LayoutOrderMap.clear();

136

137 CurFunc = F;

138 int O = 0;

139 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {

140 LayoutOrderMap[&*MBI] = O;

141 }

142 }

143 layout_order = LayoutOrderMap[Node];

144 }

146 layout_order);

147 }

148

153 }

154

159 }

160};

161

162}

163

164AnalysisKey MachineBlockFrequencyAnalysis::Key;

165

171 return Result(MF, MBPI, MLI);

172}

173

178 OS << "Machine block frequency for machine function: " << MF.getName()

179 << '\n';

180 MBFI.print(OS);

182}

183

185 "Machine Block Frequency Analysis", true, true)

190

192

197}

198

200

203

208}

209

211

215

216

218 return !PAC.preserved() &&

221}

222

229}

230

234 if (!MBFI)

236 MBFI->calculate(F, MBPI, MLI);

239 view("MachineBlockFrequencyDAGS." + F.getName());

240 }

243 MBFI->print(dbgs());

244 }

245}

246

250 getAnalysis().getMBPI();

251 MachineLoopInfo &MLI = getAnalysis().getLI();

253 return false;

254}

255

257

259

260

261

263

265}

266

270}

271

274 if (!MBFI)

275 return std::nullopt;

276

277 const Function &F = MBFI->getFunction()->getFunction();

278 return MBFI->getBlockProfileCount(F, MBB);

279}

280

281std::optional<uint64_t>

283 if (!MBFI)

284 return std::nullopt;

285

286 const Function &F = MBFI->getFunction()->getFunction();

287 return MBFI->getProfileCountFromFreq(F, Freq);

288}

289

292 assert(MBFI && "Expected analysis to be available");

293 return MBFI->isIrrLoopHeader(MBB);

294}

295

300 assert(MBFI && "Expected analysis to be available");

301 auto NewSuccFreq = MBFI->getBlockFreq(&NewPredecessor) *

303

304 MBFI->setBlockFreq(&NewSuccessor, NewSuccFreq);

305}

306

308 return MBFI ? MBFI->getFunction() : nullptr;

309}

310

312 return MBFI ? &MBFI->getBPI() : nullptr;

313}

314

316 return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);

317}

318

323 });

324}

325

329}

block Block Frequency Analysis

COFF::MachineTypes Machine

#define clEnumValN(ENUMVAL, FLAGNAME, DESC)

This file defines the DenseMap class.

static GVDAGType getGVDT()

#define INITIALIZE_PASS_DEPENDENCY(depName)

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

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

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

static bool isSimple(Instruction *I)

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

API to communicate dependencies between analyses during invalidation.

A container for analyses that lazily runs them and caches their results.

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.

Shared implementation for block frequency analysis.

Represents analyses that only rely on functions' control flow.

SmallVectorImpl< MachineBasicBlock * >::const_iterator const_succ_iterator

MachineBlockFrequencyInfo Result

Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)

void getAnalysisUsage(AnalysisUsage &AU) const override

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

bool runOnMachineFunction(MachineFunction &F) override

runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...

MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...

void view(const Twine &Name, bool isSimple=true) const

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

void print(raw_ostream &OS)

bool isIrrLoopHeader(const MachineBasicBlock *MBB) const

const MachineBranchProbabilityInfo * getMBPI() const

BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const

getblockFreq - Return block frequency.

void onEdgeSplit(const MachineBasicBlock &NewPredecessor, const MachineBasicBlock &NewSuccessor, const MachineBranchProbabilityInfo &MBPI)

incrementally calculate block frequencies when we split edges, to avoid full CFG traversal.

void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)

calculate - compute block frequency info for the given function.

const MachineFunction * getFunction() const

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

bool invalidate(MachineFunction &F, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)

Handle invalidation explicitly.

BlockFrequency getEntryFreq() const

Divide a block's BlockFrequency::getFrequency() value by this value to obtain the entry block - relat...

~MachineBlockFrequencyInfo()

std::optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const

MachineBlockFrequencyInfo()

PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)

BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const

MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...

void getAnalysisUsage(AnalysisUsage &AU) const override

getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.

StringRef getName() const

getName - Return the name of the corresponding LLVM function.

Analysis pass that exposes the MachineLoopInfo for a machine function.

static PassRegistry * getPassRegistry()

getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...

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

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

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

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()

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

void initializeMachineBlockFrequencyInfoWrapperPassPass(PassRegistry &)

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

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

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

raw_ostream & dbgs()

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

cl::opt< GVDAGType > ViewBlockLayoutWithBFI("view-block-layout-with-bfi", cl::Hidden, cl::desc("Pop up a window to show a dag displaying MBP layout and associated " "block frequencies of 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.")))

static cl::opt< GVDAGType > ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how machine block " "frequencies propagate 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.")))

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.

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

Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)

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

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

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

typename GTraits::ChildIteratorType EdgeIter

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

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

std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, const MachineBlockFrequencyInfo *MBFI)

DOTGraphTraits(bool isSimple=false)

std::string getNodeAttributes(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)

DenseMap< const MachineBasicBlock *, int > LayoutOrderMap

std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)

DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...

static ChildIteratorType child_begin(const NodeRef N)

static ChildIteratorType child_end(const NodeRef N)

static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G)

static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G)

MachineBasicBlock::const_succ_iterator ChildIteratorType

static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G)