LLVM  6.0.0svn
MachineBlockFrequencyInfo.cpp
Go to the documentation of this file.
1 //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Loops should be simplified before this analysis.
11 //
12 //===----------------------------------------------------------------------===//
13 
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/Format.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "machine-block-freq"
30 
31 
33  "view-machine-block-freq-propagation-dags", cl::Hidden,
34  cl::desc("Pop up a window to show a dag displaying how machine block "
35  "frequencies propagate through the CFG."),
36  cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
37  clEnumValN(GVDT_Fraction, "fraction",
38  "display a graph using the "
39  "fractional block frequency representation."),
40  clEnumValN(GVDT_Integer, "integer",
41  "display a graph using the raw "
42  "integer fractional block frequency representation."),
43  clEnumValN(GVDT_Count, "count", "display a graph using the real "
44  "profile count if available.")));
45 // Similar option above, but used to control BFI display only after MBP pass
47  "view-block-layout-with-bfi", cl::Hidden,
48  cl::desc(
49  "Pop up a window to show a dag displaying MBP layout and associated "
50  "block frequencies of the CFG."),
51  cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
52  clEnumValN(GVDT_Fraction, "fraction",
53  "display a graph using the "
54  "fractional block frequency representation."),
55  clEnumValN(GVDT_Integer, "integer",
56  "display a graph using the raw "
57  "integer fractional block frequency representation."),
58  clEnumValN(GVDT_Count, "count",
59  "display a graph using the real "
60  "profile count if available.")));
61 
62 // Command line option to specify the name of the function for CFG dump
63 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
65 // Command line option to specify hot frequency threshold.
66 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=
68 
69 static GVDAGType getGVDT() {
72 
74 }
75 
76 namespace llvm {
77 
78 template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
79  typedef const MachineBasicBlock *NodeRef;
82 
83  static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
84  return &G->getFunction()->front();
85  }
86 
87  static ChildIteratorType child_begin(const NodeRef N) {
88  return N->succ_begin();
89  }
90 
91  static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
92 
93  static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
94  return nodes_iterator(G->getFunction()->begin());
95  }
96 
97  static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
98  return nodes_iterator(G->getFunction()->end());
99  }
100 };
101 
105 template <>
106 struct DOTGraphTraits<MachineBlockFrequencyInfo *>
107  : public MBFIDOTGraphTraitsBase {
108  explicit DOTGraphTraits(bool isSimple = false)
109  : MBFIDOTGraphTraitsBase(isSimple), CurFunc(nullptr), LayoutOrderMap() {}
110 
113 
114  std::string getNodeLabel(const MachineBasicBlock *Node,
115  const MachineBlockFrequencyInfo *Graph) {
116 
117  int layout_order = -1;
118  // Attach additional ordering information if 'isSimple' is false.
119  if (!isSimple()) {
120  const MachineFunction *F = Node->getParent();
121  if (!CurFunc || F != CurFunc) {
122  if (CurFunc)
123  LayoutOrderMap.clear();
124 
125  CurFunc = F;
126  int O = 0;
127  for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
128  LayoutOrderMap[&*MBI] = O;
129  }
130  }
131  layout_order = LayoutOrderMap[Node];
132  }
133  return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
134  layout_order);
135  }
136 
137  std::string getNodeAttributes(const MachineBasicBlock *Node,
138  const MachineBlockFrequencyInfo *Graph) {
140  ViewHotFreqPercent);
141  }
142 
143  std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
144  const MachineBlockFrequencyInfo *MBFI) {
146  Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
147  }
148 };
149 
150 } // end namespace llvm
151 
153  "Machine Block Frequency Analysis", true, true)
157  "Machine Block Frequency Analysis", true, true)
158 
159 char MachineBlockFrequencyInfo::ID = 0;
160 
161 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
162  : MachineFunctionPass(ID) {
164 }
165 
167 
171  AU.setPreservesAll();
173 }
174 
176  const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
177  const MachineLoopInfo &MLI) {
178  if (!MBFI)
179  MBFI.reset(new ImplType);
180  MBFI->calculate(F, MBPI, MLI);
182  (ViewBlockFreqFuncName.empty() ||
184  view("MachineBlockFrequencyDAGS." + F.getName());
185  }
186 }
187 
190  getAnalysis<MachineBranchProbabilityInfo>();
191  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
192  calculate(F, MBPI, MLI);
193  return false;
194 }
195 
197 
198 /// Pop up a ghostview window with the current block frequency propagation
199 /// rendered using dot.
201  // This code is only for debugging.
202  ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
203 }
204 
207  return MBFI ? MBFI->getBlockFreq(MBB) : 0;
208 }
209 
211  const MachineBasicBlock *MBB) const {
212  const Function *F = MBFI->getFunction()->getFunction();
213  return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
214 }
215 
218  const Function *F = MBFI->getFunction()->getFunction();
219  return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
220 }
221 
223  return MBFI ? MBFI->getFunction() : nullptr;
224 }
225 
227  return MBFI ? &MBFI->getBPI() : nullptr;
228 }
229 
230 raw_ostream &
232  const BlockFrequency Freq) const {
233  return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
234 }
235 
236 raw_ostream &
238  const MachineBasicBlock *MBB) const {
239  return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
240 }
241 
243  return MBFI ? MBFI->getEntryFreq() : 0;
244 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
std::string getNodeAttributes(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
cl::opt< unsigned > ViewHotFreqPercent
static ChildIteratorType child_end(const NodeRef N)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
static GVDAGType getGVDT()
static ChildIteratorType child_begin(const NodeRef N)
Machine Block Frequency Analysis
AnalysisUsage & addRequired()
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, "Machine Block Frequency Analysis", true, true) INITIALIZE_PASS_END(MachineBlockFrequencyInfo
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void view(const Twine &Name, bool isSimple=true) const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfoT *Graph, GVDAGType GType, int layout_order=-1)
pointer_iterator< MachineFunction::const_iterator > nodes_iterator
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 bool isSimple(Instruction *I)
BFIDOTGraphTraitsBase< MachineBlockFrequencyInfo, MachineBranchProbabilityInfo > MBFIDOTGraphTraitsBase
#define F(x, y, z)
Definition: MD5.cpp:55
Optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:323
std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, const MachineBlockFrequencyInfo *MBFI)
const char * Name
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G)
static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
#define DEBUG_TYPE
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:624
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
Represent the analysis usage information of a pass.
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
DenseMap< const MachineBasicBlock *, int > LayoutOrderMap
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 &#39;dot&#39;, run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:348
const MachineBranchProbabilityInfo * getMBPI() const
const MachineBasicBlock & front() const
const MachineFunction * getFunction() const
std::string getEdgeAttributes(NodeRef Node, EdgeIter EI, const BlockFrequencyInfoT *BFI, const BranchProbabilityInfoT *BPI, unsigned HotPercentThreshold=0)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void initializeMachineBlockFrequencyInfoPass(PassRegistry &)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to &#39;dot...
std::string getNodeAttributes(NodeRef Node, const BlockFrequencyInfoT *Graph, unsigned HotPercentThreshold=0)
MachineBasicBlock::const_succ_iterator ChildIteratorType
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
const DataFlowGraph & G
Definition: RDFGraph.cpp:206
const size_t N
void setPreservesAll()
Set by analyses that do not transform their input at all.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
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.")))
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:599
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
typename GTraits::ChildIteratorType EdgeIter
static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G)
cl::opt< std::string > ViewBlockFreqFuncName
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void calculate(const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI, const MachineLoopInfo &MLI)
calculate - compute block frequency info for the given function.
Shared implementation for block frequency analysis.
Machine Block Frequency true