BlockFrequencyInfo.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "llvm/Analysis/BlockFrequencyInfo.h"
  14. #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
  15. #include "llvm/Analysis/BranchProbabilityInfo.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/Analysis/Passes.h"
  18. #include "llvm/IR/CFG.h"
  19. #include "llvm/InitializePasses.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/GraphWriter.h"
  23. using namespace llvm;
  24. #define DEBUG_TYPE "block-freq"
  25. #ifndef NDEBUG
  26. enum GVDAGType {
  27. GVDT_None,
  28. GVDT_Fraction,
  29. GVDT_Integer
  30. };
  31. static cl::opt<GVDAGType>
  32. ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
  33. cl::desc("Pop up a window to show a dag displaying how block "
  34. "frequencies propagation through the CFG."),
  35. cl::values(
  36. clEnumValN(GVDT_None, "none",
  37. "do not display graphs."),
  38. clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
  39. "fractional block frequency representation."),
  40. clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
  41. "integer fractional block frequency representation."),
  42. clEnumValEnd));
  43. namespace llvm {
  44. template <>
  45. struct GraphTraits<BlockFrequencyInfo *> {
  46. typedef const BasicBlock NodeType;
  47. typedef succ_const_iterator ChildIteratorType;
  48. typedef Function::const_iterator nodes_iterator;
  49. static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
  50. return G->getFunction()->begin();
  51. }
  52. static ChildIteratorType child_begin(const NodeType *N) {
  53. return succ_begin(N);
  54. }
  55. static ChildIteratorType child_end(const NodeType *N) {
  56. return succ_end(N);
  57. }
  58. static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
  59. return G->getFunction()->begin();
  60. }
  61. static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
  62. return G->getFunction()->end();
  63. }
  64. };
  65. template<>
  66. struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
  67. explicit DOTGraphTraits(bool isSimple=false) :
  68. DefaultDOTGraphTraits(isSimple) {}
  69. static std::string getGraphName(const BlockFrequencyInfo *G) {
  70. return G->getFunction()->getName();
  71. }
  72. std::string getNodeLabel(const BasicBlock *Node,
  73. const BlockFrequencyInfo *Graph) {
  74. std::string Result;
  75. raw_string_ostream OS(Result);
  76. OS << Node->getName() << ":";
  77. switch (ViewBlockFreqPropagationDAG) {
  78. case GVDT_Fraction:
  79. Graph->printBlockFreq(OS, Node);
  80. break;
  81. case GVDT_Integer:
  82. OS << Graph->getBlockFreq(Node).getFrequency();
  83. break;
  84. case GVDT_None:
  85. llvm_unreachable("If we are not supposed to render a graph we should "
  86. "never reach this point.");
  87. }
  88. return Result;
  89. }
  90. };
  91. } // end namespace llvm
  92. #endif
  93. INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq",
  94. "Block Frequency Analysis", true, true)
  95. INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
  96. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  97. INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq",
  98. "Block Frequency Analysis", true, true)
  99. char BlockFrequencyInfo::ID = 0;
  100. BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
  101. initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
  102. }
  103. BlockFrequencyInfo::~BlockFrequencyInfo() {}
  104. void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  105. AU.addRequired<BranchProbabilityInfo>();
  106. AU.addRequired<LoopInfoWrapperPass>();
  107. AU.setPreservesAll();
  108. }
  109. bool BlockFrequencyInfo::runOnFunction(Function &F) {
  110. BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
  111. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  112. if (!BFI)
  113. BFI.reset(new ImplType);
  114. BFI->doFunction(&F, &BPI, &LI);
  115. #ifndef NDEBUG
  116. if (ViewBlockFreqPropagationDAG != GVDT_None)
  117. view();
  118. #endif
  119. return false;
  120. }
  121. void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
  122. void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
  123. if (BFI) BFI->print(O);
  124. }
  125. BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
  126. return BFI ? BFI->getBlockFreq(BB) : 0;
  127. }
  128. /// Pop up a ghostview window with the current block frequency propagation
  129. /// rendered using dot.
  130. void BlockFrequencyInfo::view() const {
  131. // This code is only for debugging.
  132. #ifndef NDEBUG
  133. ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
  134. #else
  135. errs() << "BlockFrequencyInfo::view is only available in debug builds on "
  136. "systems with Graphviz or gv!\n";
  137. #endif // NDEBUG
  138. }
  139. const Function *BlockFrequencyInfo::getFunction() const {
  140. return BFI ? BFI->getFunction() : nullptr;
  141. }
  142. raw_ostream &BlockFrequencyInfo::
  143. printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
  144. return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
  145. }
  146. raw_ostream &
  147. BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  148. const BasicBlock *BB) const {
  149. return BFI ? BFI->printBlockFreq(OS, BB) : OS;
  150. }
  151. uint64_t BlockFrequencyInfo::getEntryFreq() const {
  152. return BFI ? BFI->getEntryFreq() : 0;
  153. }