1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "block-freq"
33 static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
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."),
37 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
38 clEnumValN(GVDT_Fraction, "fraction",
39 "display a graph using the "
40 "fractional block frequency representation."),
41 clEnumValN(GVDT_Integer, "integer",
42 "display a graph using the raw "
43 "integer fractional block frequency representation."),
44 clEnumValN(GVDT_Count, "count", "display a graph using the real "
45 "profile count if available.")));
47 extern cl::opt<std::string> ViewBlockFreqFuncName;
48 extern cl::opt<unsigned> ViewHotFreqPercent;
52 template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
53 typedef const MachineBasicBlock *NodeRef;
54 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
55 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
57 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
58 return &G->getFunction()->front();
61 static ChildIteratorType child_begin(const NodeRef N) {
62 return N->succ_begin();
65 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
67 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
68 return nodes_iterator(G->getFunction()->begin());
71 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
72 return nodes_iterator(G->getFunction()->end());
76 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
77 MachineBranchProbabilityInfo>
78 MBFIDOTGraphTraitsBase;
80 struct DOTGraphTraits<MachineBlockFrequencyInfo *>
81 : public MBFIDOTGraphTraitsBase {
82 explicit DOTGraphTraits(bool isSimple = false)
83 : MBFIDOTGraphTraitsBase(isSimple) {}
85 std::string getNodeLabel(const MachineBasicBlock *Node,
86 const MachineBlockFrequencyInfo *Graph) {
87 return MBFIDOTGraphTraitsBase::getNodeLabel(
88 Node, Graph, ViewMachineBlockFreqPropagationDAG);
91 std::string getNodeAttributes(const MachineBasicBlock *Node,
92 const MachineBlockFrequencyInfo *Graph) {
93 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
97 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
98 const MachineBlockFrequencyInfo *MBFI) {
99 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
100 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
104 } // end namespace llvm
107 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
108 "Machine Block Frequency Analysis", true, true)
109 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
110 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
111 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
112 "Machine Block Frequency Analysis", true, true)
114 char MachineBlockFrequencyInfo::ID = 0;
116 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
117 : MachineFunctionPass(ID) {
118 initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
121 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
123 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.addRequired<MachineBranchProbabilityInfo>();
125 AU.addRequired<MachineLoopInfo>();
126 AU.setPreservesAll();
127 MachineFunctionPass::getAnalysisUsage(AU);
130 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
131 MachineBranchProbabilityInfo &MBPI =
132 getAnalysis<MachineBranchProbabilityInfo>();
133 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
135 MBFI.reset(new ImplType);
136 MBFI->calculate(F, MBPI, MLI);
138 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
139 (ViewBlockFreqFuncName.empty() ||
140 F.getName().equals(ViewBlockFreqFuncName))) {
147 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
149 /// Pop up a ghostview window with the current block frequency propagation
150 /// rendered using dot.
151 void MachineBlockFrequencyInfo::view() const {
152 // This code is only for debugging.
154 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
155 "MachineBlockFrequencyDAGs");
157 errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
158 "on systems with Graphviz or gv!\n";
163 MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
164 return MBFI ? MBFI->getBlockFreq(MBB) : 0;
167 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
168 const MachineBasicBlock *MBB) const {
169 const Function *F = MBFI->getFunction()->getFunction();
170 return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
174 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
175 const Function *F = MBFI->getFunction()->getFunction();
176 return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
179 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
180 return MBFI ? MBFI->getFunction() : nullptr;
183 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
184 return MBFI ? &MBFI->getBPI() : nullptr;
188 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
189 const BlockFrequency Freq) const {
190 return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
194 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
195 const MachineBasicBlock *MBB) const {
196 return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
199 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
200 return MBFI ? MBFI->getEntryFreq() : 0;