blob: a15d75bfaa243a53adf0a831c724ecfffaba8af1 [file] [log] [blame]
Chris Lattner66328482005-01-10 23:08:40 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner66328482005-01-10 23:08:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner66328482005-01-10 23:08:40 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::viewGraph method.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohman84fbac52009-02-06 17:22:58 +000014#include "ScheduleDAGSDNodes.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000015#include "llvm/Constants.h"
16#include "llvm/Function.h"
Chris Lattner52676512006-03-05 09:38:03 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner66328482005-01-10 23:08:40 +000018#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner66328482005-01-10 23:08:40 +000020#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling10fff602008-07-03 22:53:42 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmandc5f9362008-07-17 21:12:16 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Devang Patel83489bb2009-01-13 00:35:13 +000023#include "llvm/Analysis/DebugInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7228aa72005-08-19 21:21:16 +000025#include "llvm/Target/TargetMachine.h"
David Greenec5e7e8d2008-10-27 18:17:03 +000026#include "llvm/Support/Debug.h"
Chris Lattner66328482005-01-10 23:08:40 +000027#include "llvm/Support/GraphWriter.h"
Chris Lattner62ca3252008-08-23 22:53:13 +000028#include "llvm/Support/raw_ostream.h"
David Greenec5e7e8d2008-10-27 18:17:03 +000029#include "llvm/ADT/DenseSet.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000031#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000032using namespace llvm;
33
Chris Lattnere0646b82005-01-10 23:26:00 +000034namespace llvm {
35 template<>
36 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000037
38 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
39
Dan Gohman35803312008-07-21 21:06:55 +000040 static bool hasEdgeDestLabels() {
41 return true;
42 }
43
44 static unsigned numEdgeDestLabels(const void *Node) {
45 return ((const SDNode *) Node)->getNumValues();
46 }
47
48 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +000049 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohman35803312008-07-21 21:06:55 +000050 }
51
52 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossera10d5982009-11-30 12:38:13 +000053 /// should actually target another edge source, not a node. If this method
54 /// is implemented, getEdgeTarget should be implemented.
Dan Gohman35803312008-07-21 21:06:55 +000055 template<typename EdgeIter>
56 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
57 return true;
58 }
59
60 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
61 /// called to determine which outgoing edge of Node is the target of this
62 /// edge.
63 template<typename EdgeIter>
64 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
65 SDNode *TargetNode = *I;
66 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif99a6cb92008-08-26 22:36:50 +000067 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000068 return NI;
69 }
70
Chris Lattnere0646b82005-01-10 23:26:00 +000071 static std::string getGraphName(const SelectionDAG *G) {
72 return G->getMachineFunction().getFunction()->getName();
73 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000074
75 static bool renderGraphFromBottomUp() {
76 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000077 }
Chris Lattner37345fe2005-10-01 00:17:07 +000078
79 static bool hasNodeAddressLabel(const SDNode *Node,
80 const SelectionDAG *Graph) {
81 return true;
82 }
Chris Lattner34ab4d42006-10-20 18:06:09 +000083
84 /// If you want to override the dot attributes printed for a particular
85 /// edge, override this method.
86 template<typename EdgeIter>
87 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman475871a2008-07-27 21:46:04 +000088 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000089 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +000090 if (VT == MVT::Flag)
Chris Lattner34ab4d42006-10-20 18:06:09 +000091 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000092 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000093 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000094 return "";
95 }
96
Chris Lattnere0646b82005-01-10 23:26:00 +000097
Tobias Grosser56f4ef32009-11-30 12:38:47 +000098 static std::string getSimpleNodeLabel(const SDNode *Node,
99 const SelectionDAG *G) {
100 std::string Result = Node->getOperationName(G);
101 {
102 raw_string_ostream OS(Result);
103 Node->print_details(OS, G);
104 }
105 return Result;
106 }
107 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +0000108 static std::string getNodeAttributes(const SDNode *N,
109 const SelectionDAG *Graph) {
110#ifndef NDEBUG
111 const std::string &Attrs = Graph->getGraphAttrs(N);
112 if (!Attrs.empty()) {
113 if (Attrs.find("shape=") == std::string::npos)
114 return std::string("shape=Mrecord,") + Attrs;
115 else
116 return Attrs;
117 }
118#endif
Chris Lattnere0646b82005-01-10 23:26:00 +0000119 return "shape=Mrecord";
120 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000121
122 static void addCustomGraphFeatures(SelectionDAG *G,
123 GraphWriter<SelectionDAG*> &GW) {
124 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000125 if (G->getRoot().getNode())
126 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000127 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000128 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000129 };
130}
131
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000132std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000133 const SelectionDAG *G) {
Tobias Grosserab010692009-11-30 13:34:51 +0000134 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel (Node, G);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000135}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000137
Chris Lattner66328482005-01-10 23:08:40 +0000138/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
139/// rendered using 'dot'.
140///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000141void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000142// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000143#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000144 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
145 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000146#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000147 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
148 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000149#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000150}
Jim Laskeyec204022006-10-02 12:26:53 +0000151
Dan Gohman0b12aef2008-07-30 18:48:53 +0000152// This overload is defined out-of-line here instead of just using a
153// default parameter because this is easiest for gdb to call.
154void SelectionDAG::viewGraph() {
155 viewGraph("");
156}
Jim Laskeyec204022006-10-02 12:26:53 +0000157
158/// clearGraphAttrs - Clear all previously defined node graph attributes.
159/// Intended to be used from a debugging tool (eg. gdb).
160void SelectionDAG::clearGraphAttrs() {
161#ifndef NDEBUG
162 NodeGraphAttrs.clear();
163#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000164 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
165 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000166#endif
167}
168
169
170/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
171///
172void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
173#ifndef NDEBUG
174 NodeGraphAttrs[N] = Attrs;
175#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000176 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
177 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000178#endif
179}
180
181
182/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
183/// Used from getNodeAttributes.
184const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
185#ifndef NDEBUG
186 std::map<const SDNode *, std::string>::const_iterator I =
187 NodeGraphAttrs.find(N);
188
189 if (I != NodeGraphAttrs.end())
190 return I->second;
191 else
192 return "";
193#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000194 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
195 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000196 return std::string("");
197#endif
198}
199
200/// setGraphColor - Convenience for setting node color attribute.
201///
202void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
203#ifndef NDEBUG
204 NodeGraphAttrs[N] = std::string("color=") + Color;
205#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000206 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
207 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000208#endif
209}
210
David Greenec5e7e8d2008-10-27 18:17:03 +0000211/// setSubgraphColorHelper - Implement setSubgraphColor. Return
212/// whether we truncated the search.
213///
214bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
215 int level, bool &printed) {
216 bool hit_limit = false;
217
218#ifndef NDEBUG
219 if (level >= 20) {
220 if (!printed) {
221 printed = true;
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000222 DEBUG(errs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-10-27 18:17:03 +0000223 }
224 return true;
225 }
226
227 unsigned oldSize = visited.size();
228 visited.insert(N);
229 if (visited.size() != oldSize) {
230 setGraphColor(N, Color);
231 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
232 i != iend;
233 ++i) {
234 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
235 }
236 }
237#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000238 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
239 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000240#endif
241 return hit_limit;
242}
243
244/// setSubgraphColor - Convenience for setting subgraph color attribute.
245///
246void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
247#ifndef NDEBUG
248 DenseSet<SDNode *> visited;
249 bool printed = false;
250 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
251 // Visually mark that we hit the limit
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000252 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000253 setSubgraphColorHelper(N, "blue", visited, 0, printed);
254 }
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000255 else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000256 setSubgraphColorHelper(N, "green", visited, 0, printed);
257 }
258 }
259
260#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000261 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000263#endif
264}
265
Dan Gohman343f0c02008-11-19 23:18:57 +0000266std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000267 std::string s;
268 raw_string_ostream O(s);
269 O << "SU(" << SU->NodeNum << "): ";
270 if (SU->getNode()) {
271 SmallVector<SDNode *, 4> FlaggedNodes;
272 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
273 FlaggedNodes.push_back(N);
274 while (!FlaggedNodes.empty()) {
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000275 O << DOTGraphTraits<SelectionDAG*>
276 ::getSimpleNodeLabel(FlaggedNodes.back(), DAG);
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000277 FlaggedNodes.pop_back();
278 if (!FlaggedNodes.empty())
279 O << "\n ";
280 }
281 } else {
282 O << "CROSS RC COPY";
283 }
284 return O.str();
285}
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000286
Dan Gohman343f0c02008-11-19 23:18:57 +0000287void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
288 if (DAG) {
289 // Draw a special "GraphRoot" node to indicate the root of the graph.
290 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
291 const SDNode *N = DAG->getRoot().getNode();
292 if (N && N->getNodeId() != -1)
293 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
294 "color=blue,style=dashed");
295 }
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000296}