[Layering] Move DebugInfo.h into the IR library where its implementation
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGPrinter.cpp
1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
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 // This implements the SelectionDAG::viewGraph method.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "ScheduleDAGSDNodes.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
28 using namespace llvm;
29
30 namespace llvm {
31   template<>
32   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33
34     explicit DOTGraphTraits(bool isSimple=false) :
35       DefaultDOTGraphTraits(isSimple) {}
36
37     static bool hasEdgeDestLabels() {
38       return true;
39     }
40
41     static unsigned numEdgeDestLabels(const void *Node) {
42       return ((const SDNode *) Node)->getNumValues();
43     }
44
45     static std::string getEdgeDestLabel(const void *Node, unsigned i) {
46       return ((const SDNode *) Node)->getValueType(i).getEVTString();
47     }
48
49     template<typename EdgeIter>
50     static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
51       return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
52     }
53
54     /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
55     /// should actually target another edge source, not a node.  If this method
56     /// is implemented, getEdgeTarget should be implemented.
57     template<typename EdgeIter>
58     static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
59       return true;
60     }
61
62     /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
63     /// called to determine which outgoing edge of Node is the target of this
64     /// edge.
65     template<typename EdgeIter>
66     static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
67       SDNode *TargetNode = *I;
68       SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
69       std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
70       return NI;
71     }
72
73     static std::string getGraphName(const SelectionDAG *G) {
74       return G->getMachineFunction().getName();
75     }
76
77     static bool renderGraphFromBottomUp() {
78       return true;
79     }
80
81     static bool hasNodeAddressLabel(const SDNode *Node,
82                                     const SelectionDAG *Graph) {
83       return true;
84     }
85
86     /// If you want to override the dot attributes printed for a particular
87     /// edge, override this method.
88     template<typename EdgeIter>
89     static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
90                                          const SelectionDAG *Graph) {
91       SDValue Op = EI.getNode()->getOperand(EI.getOperand());
92       EVT VT = Op.getValueType();
93       if (VT == MVT::Glue)
94         return "color=red,style=bold";
95       else if (VT == MVT::Other)
96         return "color=blue,style=dashed";
97       return "";
98     }
99
100
101     static std::string getSimpleNodeLabel(const SDNode *Node,
102                                           const SelectionDAG *G) {
103       std::string Result = Node->getOperationName(G);
104       {
105         raw_string_ostream OS(Result);
106         Node->print_details(OS, G);
107       }
108       return Result;
109     }
110     std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
111     static std::string getNodeAttributes(const SDNode *N,
112                                          const SelectionDAG *Graph) {
113 #ifndef NDEBUG
114       const std::string &Attrs = Graph->getGraphAttrs(N);
115       if (!Attrs.empty()) {
116         if (Attrs.find("shape=") == std::string::npos)
117           return std::string("shape=Mrecord,") + Attrs;
118         else
119           return Attrs;
120       }
121 #endif
122       return "shape=Mrecord";
123     }
124
125     static void addCustomGraphFeatures(SelectionDAG *G,
126                                        GraphWriter<SelectionDAG*> &GW) {
127       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
128       if (G->getRoot().getNode())
129         GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
130                     "color=blue,style=dashed");
131     }
132   };
133 }
134
135 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
136                                                         const SelectionDAG *G) {
137   return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
138 }
139
140
141 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
142 /// rendered using 'dot'.
143 ///
144 void SelectionDAG::viewGraph(const std::string &Title) {
145 // This code is only for debugging!
146 #ifndef NDEBUG
147   ViewGraph(this, "dag." + getMachineFunction().getName(),
148             false, Title);
149 #else
150   errs() << "SelectionDAG::viewGraph is only available in debug builds on "
151          << "systems with Graphviz or gv!\n";
152 #endif  // NDEBUG
153 }
154
155 // This overload is defined out-of-line here instead of just using a
156 // default parameter because this is easiest for gdb to call.
157 void SelectionDAG::viewGraph() {
158   viewGraph("");
159 }
160
161 /// clearGraphAttrs - Clear all previously defined node graph attributes.
162 /// Intended to be used from a debugging tool (eg. gdb).
163 void SelectionDAG::clearGraphAttrs() {
164 #ifndef NDEBUG
165   NodeGraphAttrs.clear();
166 #else
167   errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
168          << " on systems with Graphviz or gv!\n";
169 #endif
170 }
171
172
173 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
174 ///
175 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
176 #ifndef NDEBUG
177   NodeGraphAttrs[N] = Attrs;
178 #else
179   errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
180          << " on systems with Graphviz or gv!\n";
181 #endif
182 }
183
184
185 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
186 /// Used from getNodeAttributes.
187 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
188 #ifndef NDEBUG
189   std::map<const SDNode *, std::string>::const_iterator I =
190     NodeGraphAttrs.find(N);
191
192   if (I != NodeGraphAttrs.end())
193     return I->second;
194   else
195     return "";
196 #else
197   errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
198          << " on systems with Graphviz or gv!\n";
199   return std::string();
200 #endif
201 }
202
203 /// setGraphColor - Convenience for setting node color attribute.
204 ///
205 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
206 #ifndef NDEBUG
207   NodeGraphAttrs[N] = std::string("color=") + Color;
208 #else
209   errs() << "SelectionDAG::setGraphColor is only available in debug builds"
210          << " on systems with Graphviz or gv!\n";
211 #endif
212 }
213
214 /// setSubgraphColorHelper - Implement setSubgraphColor.  Return
215 /// whether we truncated the search.
216 ///
217 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
218                                           int level, bool &printed) {
219   bool hit_limit = false;
220
221 #ifndef NDEBUG
222   if (level >= 20) {
223     if (!printed) {
224       printed = true;
225       DEBUG(dbgs() << "setSubgraphColor hit max level\n");
226     }
227     return true;
228   }
229
230   unsigned oldSize = visited.size();
231   visited.insert(N);
232   if (visited.size() != oldSize) {
233     setGraphColor(N, Color);
234     for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
235         i != iend;
236         ++i) {
237       hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
238     }
239   }
240 #else
241   errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
242          << " on systems with Graphviz or gv!\n";
243 #endif
244   return hit_limit;
245 }
246
247 /// setSubgraphColor - Convenience for setting subgraph color attribute.
248 ///
249 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
250 #ifndef NDEBUG
251   DenseSet<SDNode *> visited;
252   bool printed = false;
253   if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
254     // Visually mark that we hit the limit
255     if (strcmp(Color, "red") == 0) {
256       setSubgraphColorHelper(N, "blue", visited, 0, printed);
257     } else if (strcmp(Color, "yellow") == 0) {
258       setSubgraphColorHelper(N, "green", visited, 0, printed);
259     }
260   }
261
262 #else
263   errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
264          << " on systems with Graphviz or gv!\n";
265 #endif
266 }
267
268 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
269   std::string s;
270   raw_string_ostream O(s);
271   O << "SU(" << SU->NodeNum << "): ";
272   if (SU->getNode()) {
273     SmallVector<SDNode *, 4> GluedNodes;
274     for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
275       GluedNodes.push_back(N);
276     while (!GluedNodes.empty()) {
277       O << DOTGraphTraits<SelectionDAG*>
278         ::getSimpleNodeLabel(GluedNodes.back(), DAG);
279       GluedNodes.pop_back();
280       if (!GluedNodes.empty())
281         O << "\n    ";
282     }
283   } else {
284     O << "CROSS RC COPY";
285   }
286   return O.str();
287 }
288
289 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
290   if (DAG) {
291     // Draw a special "GraphRoot" node to indicate the root of the graph.
292     GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
293     const SDNode *N = DAG->getRoot().getNode();
294     if (N && N->getNodeId() != -1)
295       GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
296                   "color=blue,style=dashed");
297   }
298 }