LLVM  3.7.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"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "block-freq"
28 
29 #ifndef NDEBUG
30 enum GVDAGType {
34 };
35 
36 static cl::opt<GVDAGType>
37 ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
38  cl::Hidden,
39  cl::desc("Pop up a window to show a dag displaying how machine block "
40  "frequencies propagate through the CFG."),
41  cl::values(
42  clEnumValN(GVDT_None, "none",
43  "do not display graphs."),
44  clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
45  "fractional block frequency representation."),
46  clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
47  "integer fractional block frequency representation."),
48  clEnumValEnd));
49 
50 namespace llvm {
51 
52 template <>
54  typedef const MachineBasicBlock NodeType;
57 
58  static inline
59  const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
60  return G->getFunction()->begin();
61  }
62 
63  static ChildIteratorType child_begin(const NodeType *N) {
64  return N->succ_begin();
65  }
66 
67  static ChildIteratorType child_end(const NodeType *N) {
68  return N->succ_end();
69  }
70 
71  static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
72  return G->getFunction()->begin();
73  }
74 
75  static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
76  return G->getFunction()->end();
77  }
78 };
79 
80 template<>
82  public DefaultDOTGraphTraits {
83  explicit DOTGraphTraits(bool isSimple=false) :
84  DefaultDOTGraphTraits(isSimple) {}
85 
86  static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
87  return G->getFunction()->getName();
88  }
89 
90  std::string getNodeLabel(const MachineBasicBlock *Node,
91  const MachineBlockFrequencyInfo *Graph) {
92  std::string Result;
93  raw_string_ostream OS(Result);
94 
95  OS << Node->getName().str() << ":";
97  case GVDT_Fraction:
98  Graph->printBlockFreq(OS, Node);
99  break;
100  case GVDT_Integer:
101  OS << Graph->getBlockFreq(Node).getFrequency();
102  break;
103  case GVDT_None:
104  llvm_unreachable("If we are not supposed to render a graph we should "
105  "never reach this point.");
106  }
107 
108  return Result;
109  }
110 };
111 
112 
113 } // end namespace llvm
114 #endif
115 
117  "Machine Block Frequency Analysis", true, true)
121  "Machine Block Frequency Analysis", true, true)
122 
123 char MachineBlockFrequencyInfo::ID = 0;
124 
125 
126 MachineBlockFrequencyInfo::
127 MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
129 }
130 
132 
136  AU.setPreservesAll();
138 }
139 
142  getAnalysis<MachineBranchProbabilityInfo>();
143  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
144  if (!MBFI)
145  MBFI.reset(new ImplType);
146  MBFI->doFunction(&F, &MBPI, &MLI);
147 #ifndef NDEBUG
149  view();
150  }
151 #endif
152  return false;
153 }
154 
156 
157 /// Pop up a ghostview window with the current block frequency propagation
158 /// rendered using dot.
160 // This code is only for debugging.
161 #ifndef NDEBUG
162  ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
163  "MachineBlockFrequencyDAGs");
164 #else
165  errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
166  "on systems with Graphviz or gv!\n";
167 #endif // NDEBUG
168 }
169 
171 getBlockFreq(const MachineBasicBlock *MBB) const {
172  return MBFI ? MBFI->getBlockFreq(MBB) : 0;
173 }
174 
176  return MBFI ? MBFI->getFunction() : nullptr;
177 }
178 
179 raw_ostream &
181  const BlockFrequency Freq) const {
182  return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
183 }
184 
185 raw_ostream &
187  const MachineBasicBlock *MBB) const {
188  return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
189 }
190 
192  return MBFI ? MBFI->getEntryFreq() : 0;
193 }
ValuesClass< DataType > LLVM_END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
Definition: CommandLine.h:536
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...
machine block Machine Block Frequency true
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
#define clEnumValEnd
Definition: CommandLine.h:498
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...
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:188
static ChildIteratorType child_end(const NodeType *N)
F(f)
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph)
machine block Machine Block Frequency Analysis
#define G(x, y, z)
Definition: MD5.cpp:52
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:301
static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G)
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."), clEnumValEnd))
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.
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:348
machine block freq
void initializeMachineBlockFrequencyInfoPass(PassRegistry &)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
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...
StringRef getName() const
getName - Return the name of the corresponding LLVM basic block, or "(null)".
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:497
static const NodeType * getEntryNode(const MachineBlockFrequencyInfo *G)
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)
static ChildIteratorType child_begin(const NodeType *N)
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo,"machine-block-freq","Machine Block Frequency Analysis", true, true) INITIALIZE_PASS_END(MachineBlockFrequencyInfo
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
Shared implementation for block frequency analysis.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static std::string getGraphName(const MachineBlockFrequencyInfo *G)
const MachineFunction * getFunction() const