llvm.org GIT mirror llvm / eae8ef4
Added MachineBlockFrequencyInfo::view for displaying the block frequency propagation graph via graphviz. This is useful for debugging issues in the BlockFrequency implementation since one can easily visualize where probability mass and other errors occur in the propagation. This is the MI version of r194654. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@196183 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Gottesman 6 years ago
2 changed file(s) with 111 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
None //====----- MachineBlockFrequencyInfo.h - MachineBlock Frequency Analysis ----====//
0 //====-- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -*- C++ -*--====//
11 //
22 // The LLVM Compiler Infrastructure
33 //
4848 /// the other block frequencies. We do this to avoid using of floating points.
4949 ///
5050 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
51
52 MachineFunction *getFunction() const;
53 void view() const;
5154 };
5255
5356 }
1515 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
1616 #include "llvm/CodeGen/Passes.h"
1717 #include "llvm/InitializePasses.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/GraphWriter.h"
1821
1922 using namespace llvm;
23
24 #ifndef NDEBUG
25 enum GVDAGType {
26 GVDT_None,
27 GVDT_Fraction,
28 GVDT_Integer
29 };
30
31 static cl::opt
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 propgate 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
45 namespace llvm {
46
47 template <>
48 struct GraphTraits {
49 typedef const MachineBasicBlock NodeType;
50 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
51 typedef MachineFunction::const_iterator nodes_iterator;
52
53 static inline const NodeType *getEntryNode(const MachineBlockFrequencyInfo *G) {
54 return G->getFunction()->begin();
55 }
56 static ChildIteratorType child_begin(const NodeType *N) {
57 return N->succ_begin();
58 }
59 static ChildIteratorType child_end(const NodeType *N) {
60 return N->succ_end();
61 }
62 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
63 return G->getFunction()->begin();
64 }
65 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
66 return G->getFunction()->end();
67 }
68 };
69
70 template<>
71 struct DOTGraphTraits : public DefaultDOTGraphTraits {
72 explicit DOTGraphTraits(bool isSimple=false) :
73 DefaultDOTGraphTraits(isSimple) {}
74
75 static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
76 return G->getFunction()->getName();
77 }
78
79 std::string getNodeLabel(const MachineBasicBlock *Node,
80 const MachineBlockFrequencyInfo *Graph) {
81 std::string Result;
82 raw_string_ostream OS(Result);
83
84 OS << Node->getName().str() << ":";
85 switch (ViewMachineBlockFreqPropagationDAG) {
86 case GVDT_Fraction:
87 Graph->getBlockFreq(Node).print(OS);
88 break;
89 case GVDT_Integer:
90 OS << Graph->getBlockFreq(Node).getFrequency();
91 break;
92 case GVDT_None:
93 llvm_unreachable("If we are not supposed to render a graph we should "
94 "never reach this point.");
95 }
96
97 return Result;
98 }
99 };
100
101
102 } // end namespace llvm
103 #endif
20104
21105 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
22106 "Machine Block Frequency Analysis", true, true)
46130 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
47131 MachineBranchProbabilityInfo &MBPI = getAnalysis();
48132 MBFI->doFunction(&F, &MBPI);
133 #ifndef NDEBUG
134 if (ViewMachineBlockFreqPropagationDAG != GVDT_None) {
135 view();
136 }
137 #endif
49138 return false;
139 }
140
141 /// Pop up a ghostview window with the current block frequency propagation
142 /// rendered using dot.
143 void MachineBlockFrequencyInfo::view() const {
144 // This code is only for debugging.
145 #ifndef NDEBUG
146 ViewGraph(const_cast(this),
147 "MachineBlockFrequencyDAGs");
148 #else
149 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
150 "systems with Graphviz or gv!\n";
151 #endif // NDEBUG
50152 }
51153
52154 BlockFrequency MachineBlockFrequencyInfo::
53155 getBlockFreq(const MachineBasicBlock *MBB) const {
54156 return MBFI->getBlockFreq(MBB);
55157 }
158
159 MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
160 return MBFI->Fn;
161 }
162