blob: 50f2d07f8b2af7c67ac5f215104d8c75557eef0c [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 +000032#include <fstream>
33using namespace llvm;
34
Chris Lattnere0646b82005-01-10 23:26:00 +000035namespace llvm {
36 template<>
37 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Dan Gohman35803312008-07-21 21:06:55 +000038 static bool hasEdgeDestLabels() {
39 return true;
40 }
41
42 static unsigned numEdgeDestLabels(const void *Node) {
43 return ((const SDNode *) Node)->getNumValues();
44 }
45
46 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +000047 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohman35803312008-07-21 21:06:55 +000048 }
49
50 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
51 /// should actually target another edge source, not a node. If this method is
52 /// implemented, getEdgeTarget should be implemented.
53 template<typename EdgeIter>
54 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
55 return true;
56 }
57
58 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
59 /// called to determine which outgoing edge of Node is the target of this
60 /// edge.
61 template<typename EdgeIter>
62 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
63 SDNode *TargetNode = *I;
64 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif99a6cb92008-08-26 22:36:50 +000065 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000066 return NI;
67 }
68
Chris Lattnere0646b82005-01-10 23:26:00 +000069 static std::string getGraphName(const SelectionDAG *G) {
70 return G->getMachineFunction().getFunction()->getName();
71 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000072
73 static bool renderGraphFromBottomUp() {
74 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000075 }
Chris Lattner37345fe2005-10-01 00:17:07 +000076
77 static bool hasNodeAddressLabel(const SDNode *Node,
78 const SelectionDAG *Graph) {
79 return true;
80 }
Chris Lattner34ab4d42006-10-20 18:06:09 +000081
82 /// If you want to override the dot attributes printed for a particular
83 /// edge, override this method.
84 template<typename EdgeIter>
85 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman475871a2008-07-27 21:46:04 +000086 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000087 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (VT == MVT::Flag)
Chris Lattner34ab4d42006-10-20 18:06:09 +000089 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000090 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000091 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000092 return "";
93 }
94
Chris Lattnere0646b82005-01-10 23:26:00 +000095
Chris Lattnere9c44cd2005-01-11 00:34:33 +000096 static std::string getNodeLabel(const SDNode *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +000097 const SelectionDAG *Graph,
98 bool ShortNames);
Jim Laskeyec204022006-10-02 12:26:53 +000099 static std::string getNodeAttributes(const SDNode *N,
100 const SelectionDAG *Graph) {
101#ifndef NDEBUG
102 const std::string &Attrs = Graph->getGraphAttrs(N);
103 if (!Attrs.empty()) {
104 if (Attrs.find("shape=") == std::string::npos)
105 return std::string("shape=Mrecord,") + Attrs;
106 else
107 return Attrs;
108 }
109#endif
Chris Lattnere0646b82005-01-10 23:26:00 +0000110 return "shape=Mrecord";
111 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000112
113 static void addCustomGraphFeatures(SelectionDAG *G,
114 GraphWriter<SelectionDAG*> &GW) {
115 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000116 if (G->getRoot().getNode())
117 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000118 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000119 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000120 };
121}
122
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000123std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000124 const SelectionDAG *G,
125 bool ShortNames) {
Chris Lattnerd212bb82009-06-26 19:06:10 +0000126 std::string Result = Node->getOperationName(G);
127 {
128 raw_string_ostream OS(Result);
129 Node->print_details(OS, G);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000130 }
Chris Lattnerd212bb82009-06-26 19:06:10 +0000131 return Result;
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000132}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000134
Chris Lattner66328482005-01-10 23:08:40 +0000135/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
136/// rendered using 'dot'.
137///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000138void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000139// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000140#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000141 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
142 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000143#else
Bill Wendling832171c2006-12-07 20:04:42 +0000144 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
145 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000146#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000147}
Jim Laskeyec204022006-10-02 12:26:53 +0000148
Dan Gohman0b12aef2008-07-30 18:48:53 +0000149// This overload is defined out-of-line here instead of just using a
150// default parameter because this is easiest for gdb to call.
151void SelectionDAG::viewGraph() {
152 viewGraph("");
153}
Jim Laskeyec204022006-10-02 12:26:53 +0000154
155/// clearGraphAttrs - Clear all previously defined node graph attributes.
156/// Intended to be used from a debugging tool (eg. gdb).
157void SelectionDAG::clearGraphAttrs() {
158#ifndef NDEBUG
159 NodeGraphAttrs.clear();
160#else
Bill Wendling832171c2006-12-07 20:04:42 +0000161 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
162 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000163#endif
164}
165
166
167/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
168///
169void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
170#ifndef NDEBUG
171 NodeGraphAttrs[N] = Attrs;
172#else
Bill Wendling832171c2006-12-07 20:04:42 +0000173 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
174 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000175#endif
176}
177
178
179/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
180/// Used from getNodeAttributes.
181const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
182#ifndef NDEBUG
183 std::map<const SDNode *, std::string>::const_iterator I =
184 NodeGraphAttrs.find(N);
185
186 if (I != NodeGraphAttrs.end())
187 return I->second;
188 else
189 return "";
190#else
Bill Wendling832171c2006-12-07 20:04:42 +0000191 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
192 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000193 return std::string("");
194#endif
195}
196
197/// setGraphColor - Convenience for setting node color attribute.
198///
199void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
200#ifndef NDEBUG
201 NodeGraphAttrs[N] = std::string("color=") + Color;
202#else
Bill Wendling832171c2006-12-07 20:04:42 +0000203 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
204 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000205#endif
206}
207
David Greenec5e7e8d2008-10-27 18:17:03 +0000208/// setSubgraphColorHelper - Implement setSubgraphColor. Return
209/// whether we truncated the search.
210///
211bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
212 int level, bool &printed) {
213 bool hit_limit = false;
214
215#ifndef NDEBUG
216 if (level >= 20) {
217 if (!printed) {
218 printed = true;
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000219 DEBUG(errs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-10-27 18:17:03 +0000220 }
221 return true;
222 }
223
224 unsigned oldSize = visited.size();
225 visited.insert(N);
226 if (visited.size() != oldSize) {
227 setGraphColor(N, Color);
228 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
229 i != iend;
230 ++i) {
231 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
232 }
233 }
234#else
235 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
236 << " on systems with Graphviz or gv!\n";
237#endif
238 return hit_limit;
239}
240
241/// setSubgraphColor - Convenience for setting subgraph color attribute.
242///
243void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
244#ifndef NDEBUG
245 DenseSet<SDNode *> visited;
246 bool printed = false;
247 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
248 // Visually mark that we hit the limit
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000249 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000250 setSubgraphColorHelper(N, "blue", visited, 0, printed);
251 }
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000252 else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000253 setSubgraphColorHelper(N, "green", visited, 0, printed);
254 }
255 }
256
257#else
258 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
259 << " on systems with Graphviz or gv!\n";
260#endif
261}
262
Dan Gohman343f0c02008-11-19 23:18:57 +0000263std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000264 std::string s;
265 raw_string_ostream O(s);
266 O << "SU(" << SU->NodeNum << "): ";
267 if (SU->getNode()) {
268 SmallVector<SDNode *, 4> FlaggedNodes;
269 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
270 FlaggedNodes.push_back(N);
271 while (!FlaggedNodes.empty()) {
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000272 O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(),
273 DAG, false);
Dan Gohman7d1cd3f2008-11-19 22:09:45 +0000274 FlaggedNodes.pop_back();
275 if (!FlaggedNodes.empty())
276 O << "\n ";
277 }
278 } else {
279 O << "CROSS RC COPY";
280 }
281 return O.str();
282}
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000283
Dan Gohman343f0c02008-11-19 23:18:57 +0000284void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
285 if (DAG) {
286 // Draw a special "GraphRoot" node to indicate the root of the graph.
287 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
288 const SDNode *N = DAG->getRoot().getNode();
289 if (N && N->getNodeId() != -1)
290 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
291 "color=blue,style=dashed");
292 }
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000293}