blob: 4df5ede388fc6aed6269ad3d67fcf025da02bc62 [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
Stephen Hinesdce4a402014-05-29 02:49:00 -070030#define DEBUG_TYPE "dag-printer"
31
Chris Lattnere0646b82005-01-10 23:26:00 +000032namespace llvm {
33 template<>
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000035
Dan Gohman0baf2a32009-12-01 19:16:15 +000036 explicit DOTGraphTraits(bool isSimple=false) :
37 DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000038
Dan Gohman35803312008-07-21 21:06:55 +000039 static bool hasEdgeDestLabels() {
40 return true;
41 }
42
43 static unsigned numEdgeDestLabels(const void *Node) {
44 return ((const SDNode *) Node)->getNumValues();
45 }
46
47 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +000048 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohman35803312008-07-21 21:06:55 +000049 }
50
Dan Gohman5b69fe72009-12-01 19:20:00 +000051 template<typename EdgeIter>
52 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
Roman Divacky141e9972012-09-05 22:03:34 +000053 return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
Dan Gohman5b69fe72009-12-01 19:20:00 +000054 }
55
Dan Gohman35803312008-07-21 21:06:55 +000056 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossera10d5982009-11-30 12:38:13 +000057 /// should actually target another edge source, not a node. If this method
58 /// is implemented, getEdgeTarget should be implemented.
Dan Gohman35803312008-07-21 21:06:55 +000059 template<typename EdgeIter>
60 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
61 return true;
62 }
63
64 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
65 /// called to determine which outgoing edge of Node is the target of this
66 /// edge.
67 template<typename EdgeIter>
68 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
69 SDNode *TargetNode = *I;
70 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif99a6cb92008-08-26 22:36:50 +000071 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000072 return NI;
73 }
74
Chris Lattnere0646b82005-01-10 23:26:00 +000075 static std::string getGraphName(const SelectionDAG *G) {
Craig Topper96601ca2012-08-22 06:07:19 +000076 return G->getMachineFunction().getName();
Chris Lattnere0646b82005-01-10 23:26:00 +000077 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000078
79 static bool renderGraphFromBottomUp() {
80 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000081 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000082
Chris Lattner37345fe2005-10-01 00:17:07 +000083 static bool hasNodeAddressLabel(const SDNode *Node,
84 const SelectionDAG *Graph) {
85 return true;
86 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000087
Chris Lattner34ab4d42006-10-20 18:06:09 +000088 /// If you want to override the dot attributes printed for a particular
89 /// edge, override this method.
90 template<typename EdgeIter>
Tobias Grossera91f86c2011-02-27 04:11:03 +000091 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
92 const SelectionDAG *Graph) {
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000094 EVT VT = Op.getValueType();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000095 if (VT == MVT::Glue)
Chris Lattner34ab4d42006-10-20 18:06:09 +000096 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000097 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000098 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000099 return "";
100 }
Dan Gohman0baf2a32009-12-01 19:16:15 +0000101
Chris Lattnere0646b82005-01-10 23:26:00 +0000102
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000103 static std::string getSimpleNodeLabel(const SDNode *Node,
104 const SelectionDAG *G) {
105 std::string Result = Node->getOperationName(G);
106 {
107 raw_string_ostream OS(Result);
108 Node->print_details(OS, G);
109 }
110 return Result;
111 }
112 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +0000113 static std::string getNodeAttributes(const SDNode *N,
114 const SelectionDAG *Graph) {
115#ifndef NDEBUG
116 const std::string &Attrs = Graph->getGraphAttrs(N);
117 if (!Attrs.empty()) {
118 if (Attrs.find("shape=") == std::string::npos)
119 return std::string("shape=Mrecord,") + Attrs;
120 else
121 return Attrs;
122 }
123#endif
Chris Lattnere0646b82005-01-10 23:26:00 +0000124 return "shape=Mrecord";
125 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000126
127 static void addCustomGraphFeatures(SelectionDAG *G,
128 GraphWriter<SelectionDAG*> &GW) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700129 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000130 if (G->getRoot().getNode())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700131 GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000132 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000133 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000134 };
135}
136
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000137std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000138 const SelectionDAG *G) {
Dan Gohman0baf2a32009-12-01 19:16:15 +0000139 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000140}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000141
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000142
Chris Lattner66328482005-01-10 23:08:40 +0000143/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
144/// rendered using 'dot'.
145///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000146void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000147// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000148#ifndef NDEBUG
Craig Topper96601ca2012-08-22 06:07:19 +0000149 ViewGraph(this, "dag." + getMachineFunction().getName(),
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000150 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000151#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000152 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
153 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000154#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000155}
Jim Laskeyec204022006-10-02 12:26:53 +0000156
Dan Gohman0b12aef2008-07-30 18:48:53 +0000157// This overload is defined out-of-line here instead of just using a
158// default parameter because this is easiest for gdb to call.
159void SelectionDAG::viewGraph() {
160 viewGraph("");
161}
Jim Laskeyec204022006-10-02 12:26:53 +0000162
163/// clearGraphAttrs - Clear all previously defined node graph attributes.
164/// Intended to be used from a debugging tool (eg. gdb).
165void SelectionDAG::clearGraphAttrs() {
166#ifndef NDEBUG
167 NodeGraphAttrs.clear();
168#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000169 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
170 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000171#endif
172}
173
174
175/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
176///
177void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
178#ifndef NDEBUG
179 NodeGraphAttrs[N] = Attrs;
180#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000181 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
182 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000183#endif
184}
185
186
187/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
188/// Used from getNodeAttributes.
189const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
190#ifndef NDEBUG
191 std::map<const SDNode *, std::string>::const_iterator I =
192 NodeGraphAttrs.find(N);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000193
Jim Laskeyec204022006-10-02 12:26:53 +0000194 if (I != NodeGraphAttrs.end())
195 return I->second;
196 else
197 return "";
198#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000199 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
200 << " on systems with Graphviz or gv!\n";
Dan Gohmand98af0a2010-08-04 01:39:08 +0000201 return std::string();
Jim Laskeyec204022006-10-02 12:26:53 +0000202#endif
203}
204
205/// setGraphColor - Convenience for setting node color attribute.
206///
207void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
208#ifndef NDEBUG
209 NodeGraphAttrs[N] = std::string("color=") + Color;
210#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000211 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
212 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000213#endif
214}
215
David Greenec5e7e8d2008-10-27 18:17:03 +0000216/// setSubgraphColorHelper - Implement setSubgraphColor. Return
217/// whether we truncated the search.
218///
219bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
220 int level, bool &printed) {
221 bool hit_limit = false;
222
223#ifndef NDEBUG
224 if (level >= 20) {
225 if (!printed) {
226 printed = true;
David Greene9abe0bb2010-01-05 01:24:45 +0000227 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-10-27 18:17:03 +0000228 }
229 return true;
230 }
231
232 unsigned oldSize = visited.size();
233 visited.insert(N);
234 if (visited.size() != oldSize) {
235 setGraphColor(N, Color);
236 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
237 i != iend;
238 ++i) {
239 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
240 }
241 }
242#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000243 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
244 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000245#endif
246 return hit_limit;
247}
248
249/// setSubgraphColor - Convenience for setting subgraph color attribute.
250///
251void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
252#ifndef NDEBUG
253 DenseSet<SDNode *> visited;
254 bool printed = false;
255 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
256 // Visually mark that we hit the limit
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000257 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000258 setSubgraphColorHelper(N, "blue", visited, 0, printed);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000259 } else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000260 setSubgraphColorHelper(N, "green", visited, 0, printed);
261 }
262 }
263
264#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000265 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
266 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000267#endif
268}
269
Dan Gohman343f0c02008-11-19 23:18:57 +0000270std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000271 std::string s;
272 raw_string_ostream O(s);
273 O << "SU(" << SU->NodeNum << "): ";
274 if (SU->getNode()) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000275 SmallVector<SDNode *, 4> GluedNodes;
276 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
277 GluedNodes.push_back(N);
278 while (!GluedNodes.empty()) {
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000279 O << DOTGraphTraits<SelectionDAG*>
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000280 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
281 GluedNodes.pop_back();
282 if (!GluedNodes.empty())
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000283 O << "\n ";
284 }
285 } else {
286 O << "CROSS RC COPY";
287 }
288 return O.str();
289}
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000290
Dan Gohman343f0c02008-11-19 23:18:57 +0000291void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
292 if (DAG) {
293 // Draw a special "GraphRoot" node to indicate the root of the graph.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700294 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
Dan Gohman343f0c02008-11-19 23:18:57 +0000295 const SDNode *N = DAG->getRoot().getNode();
296 if (N && N->getNodeId() != -1)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700297 GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
Dan Gohman343f0c02008-11-19 23:18:57 +0000298 "color=blue,style=dashed");
299 }
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000300}