LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlockFrequencyInfo.cpp
Go to the documentation of this file.
1 //=======-------- BlockFrequencyInfo.cpp - Block 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 
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/Support/CFG.h"
22 #include "llvm/Support/Debug.h"
24 
25 using namespace llvm;
26 
27 #ifndef NDEBUG
28 enum GVDAGType {
32 };
33 
34 static cl::opt<GVDAGType>
35 ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
36  cl::desc("Pop up a window to show a dag displaying how block "
37  "frequencies propagation through the CFG."),
38  cl::values(
39  clEnumValN(GVDT_None, "none",
40  "do not display graphs."),
41  clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
42  "fractional block frequency representation."),
43  clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
44  "integer fractional block frequency representation."),
45  clEnumValEnd));
46 
47 namespace llvm {
48 
49 template <>
51  typedef const BasicBlock NodeType;
54 
55  static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
56  return G->getFunction()->begin();
57  }
58  static ChildIteratorType child_begin(const NodeType *N) {
59  return succ_begin(N);
60  }
61  static ChildIteratorType child_end(const NodeType *N) {
62  return succ_end(N);
63  }
64  static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
65  return G->getFunction()->begin();
66  }
67  static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
68  return G->getFunction()->end();
69  }
70 };
71 
72 template<>
74  explicit DOTGraphTraits(bool isSimple=false) :
75  DefaultDOTGraphTraits(isSimple) {}
76 
77  static std::string getGraphName(const BlockFrequencyInfo *G) {
78  return G->getFunction()->getName();
79  }
80 
81  std::string getNodeLabel(const BasicBlock *Node,
82  const BlockFrequencyInfo *Graph) {
83  std::string Result;
84  raw_string_ostream OS(Result);
85 
86  OS << Node->getName().str() << ":";
88  case GVDT_Fraction:
89  Graph->getBlockFreq(Node).print(OS);
90  break;
91  case GVDT_Integer:
92  OS << Graph->getBlockFreq(Node).getFrequency();
93  break;
94  case GVDT_None:
95  llvm_unreachable("If we are not supposed to render a graph we should "
96  "never reach this point.");
97  }
98 
99  return Result;
100  }
101 };
102 
103 } // end namespace llvm
104 #endif
105 
107  "Block Frequency Analysis", true, true)
110  "Block Frequency Analysis", true, true)
111 
112 char BlockFrequencyInfo::ID = 0;
113 
114 
115 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
118 }
119 
121  delete BFI;
122 }
123 
126  AU.setPreservesAll();
127 }
128 
130  BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
131  BFI->doFunction(&F, &BPI);
132 #ifndef NDEBUG
134  view();
135 #endif
136  return false;
137 }
138 
140  if (BFI) BFI->print(O);
141 }
142 
144  return BFI->getBlockFreq(BB);
145 }
146 
147 /// Pop up a ghostview window with the current block frequency propagation
148 /// rendered using dot.
150 // This code is only for debugging.
151 #ifndef NDEBUG
152  ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
153 #else
154  errs() << "BlockFrequencyInfo::view is only available in debug builds on "
155  "systems with Graphviz or gv!\n";
156 #endif // NDEBUG
157 }
158 
160  return BFI->Fn;
161 }
block Block Frequency true
std::string getNodeLabel(const BasicBlock *Node, const BlockFrequencyInfo *Graph)
raw_ostream & errs()
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
iterator end()
Definition: Function.h:397
#define clEnumValEnd
Definition: CommandLine.h:472
ValuesClass< DataType > END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
Definition: CommandLine.h:510
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
F(f)
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
StringRef getName() const
Definition: Value.cpp:167
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:167
void getAnalysisUsage(AnalysisUsage &AU) const
#define llvm_unreachable(msg)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
static ChildIteratorType child_begin(const NodeType *N)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
#define G(x, y, z)
Definition: MD5.cpp:52
Interval::succ_iterator succ_begin(Interval *I)
Definition: Interval.h:107
static nodes_iterator nodes_begin(const BlockFrequencyInfo *G)
iterator begin()
Definition: Function.h:395
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:110
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
block freq
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:346
static cl::opt< GVDAGType > ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how block ""frequencies propagation 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 initializeBlockFrequencyInfoPass(PassRegistry &)
void print(raw_ostream &OS) const
void print(raw_ostream &O, const Module *M) const
static ChildIteratorType child_end(const NodeType *N)
INITIALIZE_PASS_BEGIN(BlockFrequencyInfo,"block-freq","Block Frequency Analysis", true, true) INITIALIZE_PASS_END(BlockFrequencyInfo
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:471
Analysis pass providing branch probability information.
block Block Frequency Analysis
const Function * getFunction() const
#define N
static const NodeType * getEntryNode(const BlockFrequencyInfo *G)
static std::string getGraphName(const BlockFrequencyInfo *G)
BlockFrequency getBlockFreq(const BasicBlock *BB) const
static nodes_iterator nodes_end(const BlockFrequencyInfo *G)