blob: 2764688518c2b00a94fbc83eba88fa452afbd039 [file] [log] [blame]
Chris Lattner7f650752005-01-10 23:08:40 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattner7f650752005-01-10 23:08:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattner7f650752005-01-10 23:08:40 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::viewGraph method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "ScheduleDAGSDNodes.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/StringExtras.h"
Evan Cheng45fe3bc2006-09-12 21:00:35 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner7f650752005-01-10 23:08:40 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2e5068942008-07-03 22:53:42 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000022#include "llvm/IR/DebugInfo.h"
David Greeneb04e7c32008-10-27 18:17:03 +000023#include "llvm/Support/Debug.h"
Chris Lattner7f650752005-01-10 23:08:40 +000024#include "llvm/Support/GraphWriter.h"
Chris Lattner838aff32008-08-23 22:53:13 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7f650752005-01-10 23:08:40 +000028using namespace llvm;
29
Chandler Carruthe96dd892014-04-21 22:55:11 +000030#define DEBUG_TYPE "dag-printer"
31
Chris Lattner12be0272005-01-10 23:26:00 +000032namespace llvm {
33 template<>
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grosser90d33402009-11-30 12:38:13 +000035
Dan Gohman8def6e32009-12-01 19:16:15 +000036 explicit DOTGraphTraits(bool isSimple=false) :
37 DefaultDOTGraphTraits(isSimple) {}
Tobias Grosser90d33402009-11-30 12:38:13 +000038
Dan Gohmanf1dc3622008-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 Anderson53aa7a92009-08-10 22:56:29 +000048 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohmanf1dc3622008-07-21 21:06:55 +000049 }
50
Dan Gohmanb2ae0292009-12-01 19:20:00 +000051 template<typename EdgeIter>
52 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
Roman Divacky66526022012-09-05 22:03:34 +000053 return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
Dan Gohmanb2ae0292009-12-01 19:20:00 +000054 }
55
Dan Gohmanf1dc3622008-07-21 21:06:55 +000056 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grosser90d33402009-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 Gohmanf1dc3622008-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 Greifabfdf922008-08-26 22:36:50 +000071 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohmanf1dc3622008-07-21 21:06:55 +000072 return NI;
73 }
74
Chris Lattner12be0272005-01-10 23:26:00 +000075 static std::string getGraphName(const SelectionDAG *G) {
Craig Toppera538d832012-08-22 06:07:19 +000076 return G->getMachineFunction().getName();
Chris Lattner12be0272005-01-10 23:26:00 +000077 }
Chris Lattner1308b482005-01-11 00:34:33 +000078
79 static bool renderGraphFromBottomUp() {
80 return true;
Chris Lattner12be0272005-01-10 23:26:00 +000081 }
Dan Gohman8def6e32009-12-01 19:16:15 +000082
James Y Knight14eedd12015-10-27 23:09:03 +000083 static std::string getNodeIdentifierLabel(const SDNode *Node,
84 const SelectionDAG *Graph) {
85 std::string R;
86 raw_string_ostream OS(R);
87#ifndef NDEBUG
88 OS << 't' << Node->PersistentId;
89#else
90 OS << static_cast<const void *>(Node);
91#endif
92 return R;
Chris Lattnerfda69442005-10-01 00:17:07 +000093 }
Dan Gohman8def6e32009-12-01 19:16:15 +000094
Chris Lattnerc5ab6ce2006-10-20 18:06:09 +000095 /// If you want to override the dot attributes printed for a particular
96 /// edge, override this method.
97 template<typename EdgeIter>
Tobias Grosser3ac86892011-02-27 04:11:03 +000098 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
99 const SelectionDAG *Graph) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000100 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Anderson53aa7a92009-08-10 22:56:29 +0000101 EVT VT = Op.getValueType();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000102 if (VT == MVT::Glue)
Chris Lattnerc5ab6ce2006-10-20 18:06:09 +0000103 return "color=red,style=bold";
Owen Anderson9f944592009-08-11 20:47:22 +0000104 else if (VT == MVT::Other)
Dan Gohman8c733322007-06-18 15:30:16 +0000105 return "color=blue,style=dashed";
Chris Lattnerc5ab6ce2006-10-20 18:06:09 +0000106 return "";
107 }
Dan Gohman8def6e32009-12-01 19:16:15 +0000108
Chris Lattner12be0272005-01-10 23:26:00 +0000109
Tobias Grosserdd7f2e72009-11-30 12:38:47 +0000110 static std::string getSimpleNodeLabel(const SDNode *Node,
111 const SelectionDAG *G) {
112 std::string Result = Node->getOperationName(G);
113 {
114 raw_string_ostream OS(Result);
115 Node->print_details(OS, G);
116 }
117 return Result;
118 }
119 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
Jim Laskey1368c262006-10-02 12:26:53 +0000120 static std::string getNodeAttributes(const SDNode *N,
121 const SelectionDAG *Graph) {
122#ifndef NDEBUG
123 const std::string &Attrs = Graph->getGraphAttrs(N);
124 if (!Attrs.empty()) {
125 if (Attrs.find("shape=") == std::string::npos)
126 return std::string("shape=Mrecord,") + Attrs;
127 else
128 return Attrs;
129 }
130#endif
Chris Lattner12be0272005-01-10 23:26:00 +0000131 return "shape=Mrecord";
132 }
Chris Lattnerb241b442005-01-10 23:52:04 +0000133
134 static void addCustomGraphFeatures(SelectionDAG *G,
135 GraphWriter<SelectionDAG*> &GW) {
Craig Topperc0196b12014-04-14 00:51:57 +0000136 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
Gabor Greiff304a7a2008-08-28 21:40:38 +0000137 if (G->getRoot().getNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000138 GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman57c74922008-07-22 17:52:59 +0000139 "color=blue,style=dashed");
Chris Lattnerb241b442005-01-10 23:52:04 +0000140 }
Chris Lattner12be0272005-01-10 23:26:00 +0000141 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000142}
Chris Lattner12be0272005-01-10 23:26:00 +0000143
Chris Lattner1308b482005-01-11 00:34:33 +0000144std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosserdd7f2e72009-11-30 12:38:47 +0000145 const SelectionDAG *G) {
Dan Gohman8def6e32009-12-01 19:16:15 +0000146 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
Chris Lattner1308b482005-01-11 00:34:33 +0000147}
Misha Brukman835702a2005-04-21 22:36:52 +0000148
Chris Lattner1308b482005-01-11 00:34:33 +0000149
Chris Lattner7f650752005-01-10 23:08:40 +0000150/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
151/// rendered using 'dot'.
152///
Dan Gohman581cc872008-07-21 20:00:07 +0000153void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnerfcc53ad2005-07-14 05:17:43 +0000154// This code is only for debugging!
Chris Lattner46524e22005-07-14 05:33:13 +0000155#ifndef NDEBUG
Craig Toppera538d832012-08-22 06:07:19 +0000156 ViewGraph(this, "dag." + getMachineFunction().getName(),
Daniel Dunbar123686852009-07-24 08:24:36 +0000157 false, Title);
Reid Spenceree7eaa22006-06-27 16:49:46 +0000158#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000159 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
160 << "systems with Graphviz or gv!\n";
Reid Spenceree7eaa22006-06-27 16:49:46 +0000161#endif // NDEBUG
Chris Lattner7f650752005-01-10 23:08:40 +0000162}
Jim Laskey1368c262006-10-02 12:26:53 +0000163
Dan Gohman88e0df02008-07-30 18:48:53 +0000164// This overload is defined out-of-line here instead of just using a
165// default parameter because this is easiest for gdb to call.
166void SelectionDAG::viewGraph() {
167 viewGraph("");
168}
Jim Laskey1368c262006-10-02 12:26:53 +0000169
170/// clearGraphAttrs - Clear all previously defined node graph attributes.
171/// Intended to be used from a debugging tool (eg. gdb).
172void SelectionDAG::clearGraphAttrs() {
173#ifndef NDEBUG
174 NodeGraphAttrs.clear();
175#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000176 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
177 << " on systems with Graphviz or gv!\n";
Jim Laskey1368c262006-10-02 12:26:53 +0000178#endif
179}
180
181
182/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
183///
184void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
185#ifndef NDEBUG
186 NodeGraphAttrs[N] = Attrs;
187#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000188 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
189 << " on systems with Graphviz or gv!\n";
Jim Laskey1368c262006-10-02 12:26:53 +0000190#endif
191}
192
193
194/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
195/// Used from getNodeAttributes.
196const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
197#ifndef NDEBUG
198 std::map<const SDNode *, std::string>::const_iterator I =
199 NodeGraphAttrs.find(N);
Dan Gohman8def6e32009-12-01 19:16:15 +0000200
Jim Laskey1368c262006-10-02 12:26:53 +0000201 if (I != NodeGraphAttrs.end())
202 return I->second;
203 else
204 return "";
205#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000206 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
207 << " on systems with Graphviz or gv!\n";
Dan Gohman5cae1032010-08-04 01:39:08 +0000208 return std::string();
Jim Laskey1368c262006-10-02 12:26:53 +0000209#endif
210}
211
212/// setGraphColor - Convenience for setting node color attribute.
213///
214void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
215#ifndef NDEBUG
216 NodeGraphAttrs[N] = std::string("color=") + Color;
217#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000218 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
219 << " on systems with Graphviz or gv!\n";
Jim Laskey1368c262006-10-02 12:26:53 +0000220#endif
221}
222
David Greeneb04e7c32008-10-27 18:17:03 +0000223/// setSubgraphColorHelper - Implement setSubgraphColor. Return
224/// whether we truncated the search.
225///
226bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
227 int level, bool &printed) {
228 bool hit_limit = false;
229
230#ifndef NDEBUG
231 if (level >= 20) {
232 if (!printed) {
233 printed = true;
David Greene40deefd2010-01-05 01:24:45 +0000234 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
David Greeneb04e7c32008-10-27 18:17:03 +0000235 }
236 return true;
237 }
238
239 unsigned oldSize = visited.size();
240 visited.insert(N);
241 if (visited.size() != oldSize) {
242 setGraphColor(N, Color);
243 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
244 i != iend;
245 ++i) {
246 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
247 }
248 }
249#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000250 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
251 << " on systems with Graphviz or gv!\n";
David Greeneb04e7c32008-10-27 18:17:03 +0000252#endif
253 return hit_limit;
254}
255
256/// setSubgraphColor - Convenience for setting subgraph color attribute.
257///
258void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
259#ifndef NDEBUG
260 DenseSet<SDNode *> visited;
261 bool printed = false;
262 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
263 // Visually mark that we hit the limit
Ted Kremenek8fcff4d2008-10-27 22:43:07 +0000264 if (strcmp(Color, "red") == 0) {
David Greeneb04e7c32008-10-27 18:17:03 +0000265 setSubgraphColorHelper(N, "blue", visited, 0, printed);
Dan Gohman8def6e32009-12-01 19:16:15 +0000266 } else if (strcmp(Color, "yellow") == 0) {
David Greeneb04e7c32008-10-27 18:17:03 +0000267 setSubgraphColorHelper(N, "green", visited, 0, printed);
268 }
269 }
270
271#else
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000272 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
273 << " on systems with Graphviz or gv!\n";
David Greeneb04e7c32008-10-27 18:17:03 +0000274#endif
275}
276
Dan Gohman60cb69e2008-11-19 23:18:57 +0000277std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Alp Tokere69170a2014-06-26 22:52:05 +0000278 std::string s;
279 raw_string_ostream O(s);
Dan Gohmanf4d95fd2008-11-19 22:09:45 +0000280 O << "SU(" << SU->NodeNum << "): ";
281 if (SU->getNode()) {
Chris Lattner11a33812010-12-23 17:24:32 +0000282 SmallVector<SDNode *, 4> GluedNodes;
283 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
284 GluedNodes.push_back(N);
285 while (!GluedNodes.empty()) {
Tobias Grosserdd7f2e72009-11-30 12:38:47 +0000286 O << DOTGraphTraits<SelectionDAG*>
Chris Lattner11a33812010-12-23 17:24:32 +0000287 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
288 GluedNodes.pop_back();
289 if (!GluedNodes.empty())
Dan Gohmanf4d95fd2008-11-19 22:09:45 +0000290 O << "\n ";
291 }
292 } else {
293 O << "CROSS RC COPY";
294 }
295 return O.str();
296}
Dan Gohman81b62e12007-08-28 20:32:58 +0000297
Dan Gohman60cb69e2008-11-19 23:18:57 +0000298void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
299 if (DAG) {
300 // Draw a special "GraphRoot" node to indicate the root of the graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000301 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
Dan Gohman60cb69e2008-11-19 23:18:57 +0000302 const SDNode *N = DAG->getRoot().getNode();
303 if (N && N->getNodeId() != -1)
Craig Topperc0196b12014-04-14 00:51:57 +0000304 GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
Dan Gohman60cb69e2008-11-19 23:18:57 +0000305 "color=blue,style=dashed");
306 }
Dan Gohman81b62e12007-08-28 20:32:58 +0000307}