MachineBlockFrequencyInfo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/BlockFrequencyImpl.h"
  15. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/GraphWriter.h"
  21. using namespace llvm;
  22. #ifndef NDEBUG
  23. enum GVDAGType {
  24. GVDT_None,
  25. GVDT_Fraction,
  26. GVDT_Integer
  27. };
  28. static cl::opt<GVDAGType>
  29. ViewMachineBlockFreqPropagationDAG("view-machine-block-freq-propagation-dags",
  30. cl::Hidden,
  31. cl::desc("Pop up a window to show a dag displaying how machine block "
  32. "frequencies propagate through the CFG."),
  33. cl::values(
  34. clEnumValN(GVDT_None, "none",
  35. "do not display graphs."),
  36. clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
  37. "fractional block frequency representation."),
  38. clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
  39. "integer fractional block frequency representation."),
  40. clEnumValEnd));
  41. namespace llvm {
  42. template <>
  43. struct GraphTraits<MachineBlockFrequencyInfo *> {
  44. typedef const MachineBasicBlock NodeType;
  45. typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
  46. typedef MachineFunction::const_iterator nodes_iterator;
  47. static inline
  48. const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
  49. return G->getFunction()->begin();
  50. }
  51. static ChildIteratorType child_begin(const NodeType *N) {
  52. return N->succ_begin();
  53. }
  54. static ChildIteratorType child_end(const NodeType *N) {
  55. return N->succ_end();
  56. }
  57. static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
  58. return G->getFunction()->begin();
  59. }
  60. static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
  61. return G->getFunction()->end();
  62. }
  63. };
  64. template<>
  65. struct DOTGraphTraits<MachineBlockFrequencyInfo*> :
  66. public DefaultDOTGraphTraits {
  67. explicit DOTGraphTraits(bool isSimple=false) :
  68. DefaultDOTGraphTraits(isSimple) {}
  69. static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
  70. return G->getFunction()->getName();
  71. }
  72. std::string getNodeLabel(const MachineBasicBlock *Node,
  73. const MachineBlockFrequencyInfo *Graph) {
  74. std::string Result;
  75. raw_string_ostream OS(Result);
  76. OS << Node->getName().str() << ":";
  77. switch (ViewMachineBlockFreqPropagationDAG) {
  78. case GVDT_Fraction:
  79. Graph->getBlockFreq(Node).print(OS);
  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(MachineBlockFrequencyInfo, "machine-block-freq",
  94. "Machine Block Frequency Analysis", true, true)
  95. INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
  96. INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
  97. "Machine Block Frequency Analysis", true, true)
  98. char MachineBlockFrequencyInfo::ID = 0;
  99. MachineBlockFrequencyInfo::
  100. MachineBlockFrequencyInfo() :MachineFunctionPass(ID) {
  101. initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
  102. MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
  103. MachineBranchProbabilityInfo>();
  104. }
  105. MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {
  106. delete MBFI;
  107. }
  108. void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  109. AU.addRequired<MachineBranchProbabilityInfo>();
  110. AU.setPreservesAll();
  111. MachineFunctionPass::getAnalysisUsage(AU);
  112. }
  113. bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
  114. MachineBranchProbabilityInfo &MBPI =
  115. getAnalysis<MachineBranchProbabilityInfo>();
  116. MBFI->doFunction(&F, &MBPI);
  117. #ifndef NDEBUG
  118. if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
  119. view();
  120. }
  121. #endif
  122. return false;
  123. }
  124. /// Pop up a ghostview window with the current block frequency propagation
  125. /// rendered using dot.
  126. void MachineBlockFrequencyInfo::view() const {
  127. // This code is only for debugging.
  128. #ifndef NDEBUG
  129. ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
  130. "MachineBlockFrequencyDAGs");
  131. #else
  132. errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
  133. "on systems with Graphviz or gv!\n";
  134. #endif // NDEBUG
  135. }
  136. BlockFrequency MachineBlockFrequencyInfo::
  137. getBlockFreq(const MachineBasicBlock *MBB) const {
  138. return MBFI->getBlockFreq(MBB);
  139. }
  140. MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
  141. return MBFI->Fn;
  142. }
  143. raw_ostream &
  144. MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  145. const BlockFrequency Freq) const {
  146. return MBFI->printBlockFreq(OS, Freq);
  147. }
  148. raw_ostream &
  149. MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
  150. const MachineBasicBlock *MBB) const {
  151. return MBFI->printBlockFreq(OS, MBB);
  152. }
  153. uint64_t MachineBlockFrequencyInfo::getEntryFrequency() const {
  154. return MBFI->getEntryFrequency();
  155. }