MachineBlockFrequencyInfo.cpp 6.0 KB

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