blob: 1483fddfddc12d6e473f550d9dd62378b7a9d410 [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
14#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "ScheduleDAGSDNodes.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/StringExtras.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner66328482005-01-10 23:08:40 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling10fff602008-07-03 22:53:42 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Stephen Hines36b56882014-04-23 16:57:46 -070022#include "llvm/IR/DebugInfo.h"
David Greenec5e7e8d2008-10-27 18:17:03 +000023#include "llvm/Support/Debug.h"
Chris Lattner66328482005-01-10 23:08:40 +000024#include "llvm/Support/GraphWriter.h"
Chris Lattner62ca3252008-08-23 22:53:13 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner66328482005-01-10 23:08:40 +000028using namespace llvm;
29
Chris Lattnere0646b82005-01-10 23:26:00 +000030namespace llvm {
31 template<>
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000033
Dan Gohman0baf2a32009-12-01 19:16:15 +000034 explicit DOTGraphTraits(bool isSimple=false) :
35 DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000036
Dan Gohman35803312008-07-21 21:06:55 +000037 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) {
Owen Andersone50ed302009-08-10 22:56:29 +000046 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohman35803312008-07-21 21:06:55 +000047 }
48
Dan Gohman5b69fe72009-12-01 19:20:00 +000049 template<typename EdgeIter>
50 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
Roman Divacky141e9972012-09-05 22:03:34 +000051 return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
Dan Gohman5b69fe72009-12-01 19:20:00 +000052 }
53
Dan Gohman35803312008-07-21 21:06:55 +000054 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossera10d5982009-11-30 12:38:13 +000055 /// should actually target another edge source, not a node. If this method
56 /// is implemented, getEdgeTarget should be implemented.
Dan Gohman35803312008-07-21 21:06:55 +000057 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);
Gabor Greif99a6cb92008-08-26 22:36:50 +000069 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000070 return NI;
71 }
72
Chris Lattnere0646b82005-01-10 23:26:00 +000073 static std::string getGraphName(const SelectionDAG *G) {
Craig Topper96601ca2012-08-22 06:07:19 +000074 return G->getMachineFunction().getName();
Chris Lattnere0646b82005-01-10 23:26:00 +000075 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000076
77 static bool renderGraphFromBottomUp() {
78 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000079 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000080
Chris Lattner37345fe2005-10-01 00:17:07 +000081 static bool hasNodeAddressLabel(const SDNode *Node,
82 const SelectionDAG *Graph) {
83 return true;
84 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000085
Chris Lattner34ab4d42006-10-20 18:06:09 +000086 /// If you want to override the dot attributes printed for a particular
87 /// edge, override this method.
88 template<typename EdgeIter>
Tobias Grossera91f86c2011-02-27 04:11:03 +000089 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
90 const SelectionDAG *Graph) {
Dan Gohman475871a2008-07-27 21:46:04 +000091 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000092 EVT VT = Op.getValueType();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000093 if (VT == MVT::Glue)
Chris Lattner34ab4d42006-10-20 18:06:09 +000094 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000095 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000096 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000097 return "";
98 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000099
Chris Lattnere0646b82005-01-10 23:26:00 +0000100
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000101 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);
Jim Laskeyec204022006-10-02 12:26:53 +0000111 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
Chris Lattnere0646b82005-01-10 23:26:00 +0000122 return "shape=Mrecord";
123 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000124
125 static void addCustomGraphFeatures(SelectionDAG *G,
126 GraphWriter<SelectionDAG*> &GW) {
127 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000128 if (G->getRoot().getNode())
129 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000130 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000131 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000132 };
133}
134
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000135std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000136 const SelectionDAG *G) {
Dan Gohman0baf2a32009-12-01 19:16:15 +0000137 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000138}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000139
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000140
Chris Lattner66328482005-01-10 23:08:40 +0000141/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
142/// rendered using 'dot'.
143///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000144void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000145// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000146#ifndef NDEBUG
Craig Topper96601ca2012-08-22 06:07:19 +0000147 ViewGraph(this, "dag." + getMachineFunction().getName(),
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000148 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000149#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000150 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
151 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000152#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000153}
Jim Laskeyec204022006-10-02 12:26:53 +0000154
Dan Gohman0b12aef2008-07-30 18:48:53 +0000155// This overload is defined out-of-line here instead of just using a
156// default parameter because this is easiest for gdb to call.
157void SelectionDAG::viewGraph() {
158 viewGraph("");
159}
Jim Laskeyec204022006-10-02 12:26:53 +0000160
161/// clearGraphAttrs - Clear all previously defined node graph attributes.
162/// Intended to be used from a debugging tool (eg. gdb).
163void SelectionDAG::clearGraphAttrs() {
164#ifndef NDEBUG
165 NodeGraphAttrs.clear();
166#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000167 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
168 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000169#endif
170}
171
172
173/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
174///
175void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
176#ifndef NDEBUG
177 NodeGraphAttrs[N] = Attrs;
178#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000179 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
180 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000181#endif
182}
183
184
185/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
186/// Used from getNodeAttributes.
187const 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);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000191
Jim Laskeyec204022006-10-02 12:26:53 +0000192 if (I != NodeGraphAttrs.end())
193 return I->second;
194 else
195 return "";
196#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000197 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
198 << " on systems with Graphviz or gv!\n";
Dan Gohmand98af0a2010-08-04 01:39:08 +0000199 return std::string();
Jim Laskeyec204022006-10-02 12:26:53 +0000200#endif
201}
202
203/// setGraphColor - Convenience for setting node color attribute.
204///
205void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
206#ifndef NDEBUG
207 NodeGraphAttrs[N] = std::string("color=") + Color;
208#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000209 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
210 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000211#endif
212}
213
David Greenec5e7e8d2008-10-27 18:17:03 +0000214/// setSubgraphColorHelper - Implement setSubgraphColor. Return
215/// whether we truncated the search.
216///
217bool 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;
David Greene9abe0bb2010-01-05 01:24:45 +0000225 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-10-27 18:17:03 +0000226 }
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
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000241 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
242 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000243#endif
244 return hit_limit;
245}
246
247/// setSubgraphColor - Convenience for setting subgraph color attribute.
248///
249void 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
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000255 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000256 setSubgraphColorHelper(N, "blue", visited, 0, printed);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000257 } else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000258 setSubgraphColorHelper(N, "green", visited, 0, printed);
259 }
260 }
261
262#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000263 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
264 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000265#endif
266}
267
Dan Gohman343f0c02008-11-19 23:18:57 +0000268std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000269 std::string s;
270 raw_string_ostream O(s);
271 O << "SU(" << SU->NodeNum << "): ";
272 if (SU->getNode()) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000273 SmallVector<SDNode *, 4> GluedNodes;
274 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
275 GluedNodes.push_back(N);
276 while (!GluedNodes.empty()) {
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000277 O << DOTGraphTraits<SelectionDAG*>
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000278 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
279 GluedNodes.pop_back();
280 if (!GluedNodes.empty())
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000281 O << "\n ";
282 }
283 } else {
284 O << "CROSS RC COPY";
285 }
286 return O.str();
287}
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000288
Dan Gohman343f0c02008-11-19 23:18:57 +0000289void 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 }
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000298}