blob: cd1647b17b9b891a9d57598cbbe63a0d346680a8 [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
Dan Gohman0baf2a32009-12-01 19:16:15 +000038 explicit DOTGraphTraits(bool isSimple=false) :
39 DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +000040
Dan Gohman35803312008-07-21 21:06:55 +000041 static bool hasEdgeDestLabels() {
42 return true;
43 }
44
45 static unsigned numEdgeDestLabels(const void *Node) {
46 return ((const SDNode *) Node)->getNumValues();
47 }
48
49 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +000050 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohman35803312008-07-21 21:06:55 +000051 }
52
Dan Gohman5b69fe72009-12-01 19:20:00 +000053 template<typename EdgeIter>
54 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
55 return itostr(I - SDNodeIterator::begin((SDNode *) Node));
56 }
57
Dan Gohman35803312008-07-21 21:06:55 +000058 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossera10d5982009-11-30 12:38:13 +000059 /// should actually target another edge source, not a node. If this method
60 /// is implemented, getEdgeTarget should be implemented.
Dan Gohman35803312008-07-21 21:06:55 +000061 template<typename EdgeIter>
62 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
63 return true;
64 }
65
66 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
67 /// called to determine which outgoing edge of Node is the target of this
68 /// edge.
69 template<typename EdgeIter>
70 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
71 SDNode *TargetNode = *I;
72 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif99a6cb92008-08-26 22:36:50 +000073 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000074 return NI;
75 }
76
Chris Lattnere0646b82005-01-10 23:26:00 +000077 static std::string getGraphName(const SelectionDAG *G) {
78 return G->getMachineFunction().getFunction()->getName();
79 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000080
81 static bool renderGraphFromBottomUp() {
82 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000083 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000084
Chris Lattner37345fe2005-10-01 00:17:07 +000085 static bool hasNodeAddressLabel(const SDNode *Node,
86 const SelectionDAG *Graph) {
87 return true;
88 }
Dan Gohman0baf2a32009-12-01 19:16:15 +000089
Chris Lattner34ab4d42006-10-20 18:06:09 +000090 /// If you want to override the dot attributes printed for a particular
91 /// edge, override this method.
92 template<typename EdgeIter>
Tobias Grossera91f86c2011-02-27 04:11:03 +000093 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
94 const SelectionDAG *Graph) {
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000096 EVT VT = Op.getValueType();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000097 if (VT == MVT::Glue)
Chris Lattner34ab4d42006-10-20 18:06:09 +000098 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000099 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +0000100 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +0000101 return "";
102 }
Dan Gohman0baf2a32009-12-01 19:16:15 +0000103
Chris Lattnere0646b82005-01-10 23:26:00 +0000104
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000105 static std::string getSimpleNodeLabel(const SDNode *Node,
106 const SelectionDAG *G) {
107 std::string Result = Node->getOperationName(G);
108 {
109 raw_string_ostream OS(Result);
110 Node->print_details(OS, G);
111 }
112 return Result;
113 }
114 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +0000115 static std::string getNodeAttributes(const SDNode *N,
116 const SelectionDAG *Graph) {
117#ifndef NDEBUG
118 const std::string &Attrs = Graph->getGraphAttrs(N);
119 if (!Attrs.empty()) {
120 if (Attrs.find("shape=") == std::string::npos)
121 return std::string("shape=Mrecord,") + Attrs;
122 else
123 return Attrs;
124 }
125#endif
Chris Lattnere0646b82005-01-10 23:26:00 +0000126 return "shape=Mrecord";
127 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000128
129 static void addCustomGraphFeatures(SelectionDAG *G,
130 GraphWriter<SelectionDAG*> &GW) {
131 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000132 if (G->getRoot().getNode())
133 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000134 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000135 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000136 };
137}
138
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000139std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000140 const SelectionDAG *G) {
Dan Gohman0baf2a32009-12-01 19:16:15 +0000141 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000142}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000143
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000144
Chris Lattner66328482005-01-10 23:08:40 +0000145/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
146/// rendered using 'dot'.
147///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000148void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000149// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000150#ifndef NDEBUG
Dan Gohman0baf2a32009-12-01 19:16:15 +0000151 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000152 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000153#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000154 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
155 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000156#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000157}
Jim Laskeyec204022006-10-02 12:26:53 +0000158
Dan Gohman0b12aef2008-07-30 18:48:53 +0000159// This overload is defined out-of-line here instead of just using a
160// default parameter because this is easiest for gdb to call.
161void SelectionDAG::viewGraph() {
162 viewGraph("");
163}
Jim Laskeyec204022006-10-02 12:26:53 +0000164
165/// clearGraphAttrs - Clear all previously defined node graph attributes.
166/// Intended to be used from a debugging tool (eg. gdb).
167void SelectionDAG::clearGraphAttrs() {
168#ifndef NDEBUG
169 NodeGraphAttrs.clear();
170#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000171 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
172 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000173#endif
174}
175
176
177/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
178///
179void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
180#ifndef NDEBUG
181 NodeGraphAttrs[N] = Attrs;
182#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000183 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
184 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000185#endif
186}
187
188
189/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
190/// Used from getNodeAttributes.
191const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
192#ifndef NDEBUG
193 std::map<const SDNode *, std::string>::const_iterator I =
194 NodeGraphAttrs.find(N);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000195
Jim Laskeyec204022006-10-02 12:26:53 +0000196 if (I != NodeGraphAttrs.end())
197 return I->second;
198 else
199 return "";
200#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000201 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
202 << " on systems with Graphviz or gv!\n";
Dan Gohmand98af0a2010-08-04 01:39:08 +0000203 return std::string();
Jim Laskeyec204022006-10-02 12:26:53 +0000204#endif
205}
206
207/// setGraphColor - Convenience for setting node color attribute.
208///
209void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
210#ifndef NDEBUG
211 NodeGraphAttrs[N] = std::string("color=") + Color;
212#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000213 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
214 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000215#endif
216}
217
David Greenec5e7e8d2008-10-27 18:17:03 +0000218/// setSubgraphColorHelper - Implement setSubgraphColor. Return
219/// whether we truncated the search.
220///
221bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
222 int level, bool &printed) {
223 bool hit_limit = false;
224
225#ifndef NDEBUG
226 if (level >= 20) {
227 if (!printed) {
228 printed = true;
David Greene9abe0bb2010-01-05 01:24:45 +0000229 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-10-27 18:17:03 +0000230 }
231 return true;
232 }
233
234 unsigned oldSize = visited.size();
235 visited.insert(N);
236 if (visited.size() != oldSize) {
237 setGraphColor(N, Color);
238 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
239 i != iend;
240 ++i) {
241 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
242 }
243 }
244#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000245 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
246 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000247#endif
248 return hit_limit;
249}
250
251/// setSubgraphColor - Convenience for setting subgraph color attribute.
252///
253void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
254#ifndef NDEBUG
255 DenseSet<SDNode *> visited;
256 bool printed = false;
257 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
258 // Visually mark that we hit the limit
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000259 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000260 setSubgraphColorHelper(N, "blue", visited, 0, printed);
Dan Gohman0baf2a32009-12-01 19:16:15 +0000261 } else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000262 setSubgraphColorHelper(N, "green", visited, 0, printed);
263 }
264 }
265
266#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000267 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
268 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000269#endif
270}
271
Dan Gohman343f0c02008-11-19 23:18:57 +0000272std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000273 std::string s;
274 raw_string_ostream O(s);
275 O << "SU(" << SU->NodeNum << "): ";
276 if (SU->getNode()) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000277 SmallVector<SDNode *, 4> GluedNodes;
278 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
279 GluedNodes.push_back(N);
280 while (!GluedNodes.empty()) {
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000281 O << DOTGraphTraits<SelectionDAG*>
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000282 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
283 GluedNodes.pop_back();
284 if (!GluedNodes.empty())
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000285 O << "\n ";
286 }
287 } else {
288 O << "CROSS RC COPY";
289 }
290 return O.str();
291}
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000292
Dan Gohman343f0c02008-11-19 23:18:57 +0000293void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
294 if (DAG) {
295 // Draw a special "GraphRoot" node to indicate the root of the graph.
296 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
297 const SDNode *N = DAG->getRoot().getNode();
298 if (N && N->getNodeId() != -1)
299 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
300 "color=blue,style=dashed");
301 }
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000302}