2
0

MachineBlockFrequencyInfo.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  14. #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
  15. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineLoopInfo.h"
  18. #include "llvm/CodeGen/Passes.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. ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
  33. 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(
  37. clEnumValN(GVDT_None, "none",
  38. "do not display graphs."),
  39. clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
  40. "fractional block frequency representation."),
  41. clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
  42. "integer fractional block frequency representation."),
  43. clEnumValEnd));
  44. namespace llvm {
  45. template <>
  46. struct GraphTraits<MachineBlockFrequencyInfo *> {
  47. typedef const MachineBasicBlock NodeType;
  48. typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
  49. typedef MachineFunction::const_iterator nodes_iterator;
  50. static inline
  51. const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
  52. return G->getFunction()->begin();
  53. }
  54. static ChildIteratorType child_begin(const NodeType *N) {
  55. return N->succ_begin();
  56. }
  57. static ChildIteratorType child_end(const NodeType *N) {
  58. return N->succ_end();
  59. }
  60. static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
  61. return G->getFunction()->begin();
  62. }
  63. static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
  64. return G->getFunction()->end();
  65. }
  66. };
  67. template<>
  68. struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
  69. public DefaultDOTGraphTraits {
  70. explicit DOTGraphTraits(bool isSimple=false) :
  71. DefaultDOTGraphTraits(isSimple) {}
  72. static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
  73. return G->getFunction()->getName();
  74. }
  75. std::string getNodeLabel(const MachineBasicBlock *Node,
  76. const MachineBlockFrequencyInfo *Graph) {
  77. std::string Result;
  78. raw_string_ostream OS(Result);
  79. OS << Node->getName().str() << ":";
  80. switch (ViewMachineBlockFreqPropagationDAG) {
  81. case GVDT_Fraction:
  82. Graph->printBlockFreq(OS, Node);
  83. break;
  84. case GVDT_Integer:
  85. OS << Graph->getBlockFreq(Node).getFrequency();
  86. break;
  87. case GVDT_None:
  88. llvm_unreachable("If we are not supposed to render a graph we should "
  89. "never reach this point.");
  90. }
  91. return Result;
  92. }
  93. };
  94. } // end namespace llvm
  95. #endif
  96. INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
  97. "Machine Block Frequency Analysis", true, true)
  98. INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
  99. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  100. INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
  101. "Machine Block Frequency Analysis", true, true)
  102. char MachineBlockFrequencyInfo::ID = 0;
  103. MachineBlockFrequencyInfo::
  104. MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
  105. initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
  106. }
  107. MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
  108. void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  109. AU.addRequired<MachineBranchProbabilityInfo>();
  110. AU.addRequired<MachineLoopInfo>();
  111. AU.setPreservesAll();
  112. MachineFunctionPass::getAnalysisUsage(AU);
  113. }
  114. bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
  115. MachineBranchProbabilityInfo &MBPI =
  116. getAnalysis<MachineBranchProbabilityInfo>();
  117. MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
  118. if (!MBFI)
  119. MBFI.reset(new ImplType);
  120. MBFI->doFunction(&F, &MBPI, &MLI);
  121. #ifndef NDEBUG
  122. if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
  123. view();
  124. }
  125. #endif
  126. return false;
  127. }
  128. void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
  129. /// Pop up a ghostview window with the current block frequency propagation
  130. /// rendered using dot.
  131. void MachineBlockFrequencyInfo::view() const {
  132. // This code is only for debugging.
  133. #ifndef NDEBUG
  134. ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
  135. "MachineBlockFrequencyDAGs");
  136. #else
  137. errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
  138. "on systems with Graphviz or gv!\n";
  139. #endif // NDEBUG
  140. }
  141. BlockFrequency MachineBlockFrequencyInfo::
  142. getBlockFreq(const MachineBasicBlock *MBB) const {
  143. return MBFI ? MBFI->getBlockFreq(MBB) : 0;
  144. }
  145. const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
  146. return MBFI ? MBFI->getFunction() : nullptr;
  147. }
  148. raw_ostream &
  149. MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  150. const BlockFrequency Freq) const {
  151. return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
  152. }
  153. raw_ostream &
  154. MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  155. const MachineBasicBlock *MBB) const {
  156. return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
  157. }
  158. uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
  159. return MBFI ? MBFI->getEntryFreq() : 0;
  160. }