LLVM  4.0.0
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 "block-freq"
30 
31 #ifndef NDEBUG
32 
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.")));
46 
49 
50 namespace llvm {
51 
52 template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
53  typedef const MachineBasicBlock *NodeRef;
56 
57  static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
58  return &G->getFunction()->front();
59  }
60 
61  static ChildIteratorType child_begin(const NodeRef N) {
62  return N->succ_begin();
63  }
64 
65  static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
66 
67  static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
68  return nodes_iterator(G->getFunction()->begin());
69  }
70 
71  static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
72  return nodes_iterator(G->getFunction()->end());
73  }
74 };
75 
79 template <>
81  : public MBFIDOTGraphTraitsBase {
82  explicit DOTGraphTraits(bool isSimple = false)
84 
85  std::string getNodeLabel(const MachineBasicBlock *Node,
86  const MachineBlockFrequencyInfo *Graph) {
89  }
90 
91  std::string getNodeAttributes(const MachineBasicBlock *Node,
92  const MachineBlockFrequencyInfo *Graph) {
95  }
96 
97  std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
98  const MachineBlockFrequencyInfo *MBFI) {
100  Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
101  }
102 };
103 
104 } // end namespace llvm
105 #endif
106 
108  "Machine Block Frequency Analysis", true, true)
112  "Machine Block Frequency Analysis", true, true)
113 
114 char MachineBlockFrequencyInfo::ID = 0;
115 
116 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
117  : MachineFunctionPass(ID) {
119 }
120 
122 
126  AU.setPreservesAll();
128 }
129 
132  getAnalysis<MachineBranchProbabilityInfo>();
133  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
134  if (!MBFI)
135  MBFI.reset(new ImplType);
136  MBFI->calculate(F, MBPI, MLI);
137 #ifndef NDEBUG
139  (ViewBlockFreqFuncName.empty() ||
141  view();
142  }
143 #endif
144  return false;
145 }
146 
148 
149 /// Pop up a ghostview window with the current block frequency propagation
150 /// rendered using dot.
152 // This code is only for debugging.
153 #ifndef NDEBUG
154  ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
155  "MachineBlockFrequencyDAGs");
156 #else
157  errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
158  "on systems with Graphviz or gv!\n";
159 #endif // NDEBUG
160 }
161 
164  return MBFI ? MBFI->getBlockFreq(MBB) : 0;
165 }
166 
168  const MachineBasicBlock *MBB) const {
169  const Function *F = MBFI->getFunction()->getFunction();
170  return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
171 }
172 
175  const Function *F = MBFI->getFunction()->getFunction();
176  return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
177 }
178 
180  return MBFI ? MBFI->getFunction() : nullptr;
181 }
182 
184  return MBFI ? &MBFI->getBPI() : nullptr;
185 }
186 
187 raw_ostream &
189  const BlockFrequency Freq) const {
190  return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
191 }
192 
193 raw_ostream &
195  const MachineBasicBlock *MBB) const {
196  return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
197 }
198 
200  return MBFI ? MBFI->getEntryFreq() : 0;
201 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
std::string getNodeAttributes(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
machine block Machine Block Frequency true
cl::opt< unsigned > ViewHotFreqPercent
static ChildIteratorType child_end(const NodeRef N)
void view() const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
static ChildIteratorType child_begin(const NodeRef N)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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:166
std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
pointer_iterator< MachineFunction::const_iterator > nodes_iterator
machine block Machine Block Frequency Analysis
static bool isSimple(Instruction *I)
const MachineBasicBlock & front() const
BFIDOTGraphTraitsBase< MachineBlockFrequencyInfo, MachineBranchProbabilityInfo > MBFIDOTGraphTraitsBase
#define F(x, y, z)
Definition: MD5.cpp:51
MachineBasicBlock * MBB
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:303
std::string getNodeLabel(NodeRef Node, const BlockFrequencyInfoT *Graph, GVDAGType GType)
std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI, const MachineBlockFrequencyInfo *MBFI)
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G)
static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G)
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:615
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
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.
Definition: GraphWriter.h:339
Optional< uint64_t > getProfileCountFromFreq(uint64_t Freq) const
machine block freq
std::string getEdgeAttributes(NodeRef Node, EdgeIter EI, const BlockFrequencyInfoT *BFI, const BranchProbabilityInfoT *BPI, unsigned HotPercentThreshold=0)
void initializeMachineBlockFrequencyInfoPass(PassRegistry &)
Optional< uint64_t > getBlockProfileCount(const MachineBasicBlock *MBB) const
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to '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 MachineBranchProbabilityInfo * getMBPI() const
GTraits::ChildIteratorType EdgeIter
const DataFlowGraph & G
Definition: RDFGraph.cpp:206
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 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...
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:590
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
#define N
static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G)
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo,"machine-block-freq","Machine Block Frequency Analysis", true, true) INITIALIZE_PASS_END(MachineBlockFrequencyInfo
cl::opt< std::string > ViewBlockFreqFuncName
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Shared implementation for block frequency analysis.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineFunction * getFunction() const