blob: 3786bd197b859716796d7fb2c5455519f10f2af7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::viewGraph method.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohman8afcc1d2009-02-06 17:22:58 +000014#include "ScheduleDAGSDNodes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Constants.h"
16#include "llvm/Function.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling4de8de52008-07-03 22:53:42 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmane36d39c2008-07-17 21:12:16 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Devang Patelfcf1c752009-01-13 00:35:13 +000023#include "llvm/Analysis/DebugInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetMachine.h"
David Greenef3614832008-10-27 18:17:03 +000026#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/GraphWriter.h"
Chris Lattner491f7832008-08-23 22:53:13 +000028#include "llvm/Support/raw_ostream.h"
David Greenef3614832008-10-27 18:17:03 +000029#include "llvm/ADT/DenseSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/Config/config.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032using namespace llvm;
33
34namespace llvm {
35 template<>
36 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +000037
Dan Gohman8cb37472009-12-01 19:16:15 +000038 explicit DOTGraphTraits(bool isSimple=false) :
39 DefaultDOTGraphTraits(isSimple) {}
Tobias Grossere2c3aec2009-11-30 12:38:13 +000040
Dan Gohmanb38a0052008-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 Andersonac9de032009-08-10 22:56:29 +000050 return ((const SDNode *) Node)->getValueType(i).getEVTString();
Dan Gohmanb38a0052008-07-21 21:06:55 +000051 }
52
Dan Gohman69be7872009-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 Gohmanb38a0052008-07-21 21:06:55 +000058 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossere2c3aec2009-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 Gohmanb38a0052008-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 Greif46bf5472008-08-26 22:36:50 +000073 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohmanb38a0052008-07-21 21:06:55 +000074 return NI;
75 }
76
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 static std::string getGraphName(const SelectionDAG *G) {
78 return G->getMachineFunction().getFunction()->getName();
79 }
80
81 static bool renderGraphFromBottomUp() {
82 return true;
83 }
Dan Gohman8cb37472009-12-01 19:16:15 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 static bool hasNodeAddressLabel(const SDNode *Node,
86 const SelectionDAG *Graph) {
87 return true;
88 }
Dan Gohman8cb37472009-12-01 19:16:15 +000089
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 /// If you want to override the dot attributes printed for a particular
91 /// edge, override this method.
92 template<typename EdgeIter>
93 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman8181bd12008-07-27 21:46:04 +000094 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersonac9de032009-08-10 22:56:29 +000095 EVT VT = Op.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +000096 if (VT == MVT::Flag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return "color=red,style=bold";
Owen Anderson36e3a6e2009-08-11 20:47:22 +000098 else if (VT == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return "color=blue,style=dashed";
100 return "";
101 }
Dan Gohman8cb37472009-12-01 19:16:15 +0000102
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Tobias Grosser810b18c2009-11-30 12:38:47 +0000104 static std::string getSimpleNodeLabel(const SDNode *Node,
105 const SelectionDAG *G) {
106 std::string Result = Node->getOperationName(G);
107 {
108 raw_string_ostream OS(Result);
109 Node->print_details(OS, G);
110 }
111 return Result;
112 }
113 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 static std::string getNodeAttributes(const SDNode *N,
115 const SelectionDAG *Graph) {
116#ifndef NDEBUG
117 const std::string &Attrs = Graph->getGraphAttrs(N);
118 if (!Attrs.empty()) {
119 if (Attrs.find("shape=") == std::string::npos)
120 return std::string("shape=Mrecord,") + Attrs;
121 else
122 return Attrs;
123 }
124#endif
125 return "shape=Mrecord";
126 }
127
128 static void addCustomGraphFeatures(SelectionDAG *G,
129 GraphWriter<SelectionDAG*> &GW) {
130 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greif1c80d112008-08-28 21:40:38 +0000131 if (G->getRoot().getNode())
132 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman7951f802008-07-22 17:52:59 +0000133 "color=blue,style=dashed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 }
135 };
136}
137
138std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Tobias Grosser810b18c2009-11-30 12:38:47 +0000139 const SelectionDAG *G) {
Dan Gohman8cb37472009-12-01 19:16:15 +0000140 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141}
142
143
144/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
145/// rendered using 'dot'.
146///
Dan Gohmanb552df72008-07-21 20:00:07 +0000147void SelectionDAG::viewGraph(const std::string &Title) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148// This code is only for debugging!
149#ifndef NDEBUG
Dan Gohman8cb37472009-12-01 19:16:15 +0000150 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000151 false, Title);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000153 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
154 << "systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155#endif // NDEBUG
156}
157
Dan Gohman8c892862008-07-30 18:48:53 +0000158// This overload is defined out-of-line here instead of just using a
159// default parameter because this is easiest for gdb to call.
160void SelectionDAG::viewGraph() {
161 viewGraph("");
162}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
164/// clearGraphAttrs - Clear all previously defined node graph attributes.
165/// Intended to be used from a debugging tool (eg. gdb).
166void SelectionDAG::clearGraphAttrs() {
167#ifndef NDEBUG
168 NodeGraphAttrs.clear();
169#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000170 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
171 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172#endif
173}
174
175
176/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
177///
178void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
179#ifndef NDEBUG
180 NodeGraphAttrs[N] = Attrs;
181#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000182 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
183 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184#endif
185}
186
187
188/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
189/// Used from getNodeAttributes.
190const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
191#ifndef NDEBUG
192 std::map<const SDNode *, std::string>::const_iterator I =
193 NodeGraphAttrs.find(N);
Dan Gohman8cb37472009-12-01 19:16:15 +0000194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 if (I != NodeGraphAttrs.end())
196 return I->second;
197 else
198 return "";
199#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000200 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
201 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 return std::string("");
203#endif
204}
205
206/// setGraphColor - Convenience for setting node color attribute.
207///
208void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
209#ifndef NDEBUG
210 NodeGraphAttrs[N] = std::string("color=") + Color;
211#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000212 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
213 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214#endif
215}
216
David Greenef3614832008-10-27 18:17:03 +0000217/// setSubgraphColorHelper - Implement setSubgraphColor. Return
218/// whether we truncated the search.
219///
220bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
221 int level, bool &printed) {
222 bool hit_limit = false;
223
224#ifndef NDEBUG
225 if (level >= 20) {
226 if (!printed) {
227 printed = true;
David Greeneb67509c2010-01-05 01:24:45 +0000228 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
David Greenef3614832008-10-27 18:17:03 +0000229 }
230 return true;
231 }
232
233 unsigned oldSize = visited.size();
234 visited.insert(N);
235 if (visited.size() != oldSize) {
236 setGraphColor(N, Color);
237 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
238 i != iend;
239 ++i) {
240 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
241 }
242 }
243#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000244 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
245 << " on systems with Graphviz or gv!\n";
David Greenef3614832008-10-27 18:17:03 +0000246#endif
247 return hit_limit;
248}
249
250/// setSubgraphColor - Convenience for setting subgraph color attribute.
251///
252void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
253#ifndef NDEBUG
254 DenseSet<SDNode *> visited;
255 bool printed = false;
256 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
257 // Visually mark that we hit the limit
Ted Kremenekb2b22922008-10-27 22:43:07 +0000258 if (strcmp(Color, "red") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000259 setSubgraphColorHelper(N, "blue", visited, 0, printed);
Dan Gohman8cb37472009-12-01 19:16:15 +0000260 } else if (strcmp(Color, "yellow") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000261 setSubgraphColorHelper(N, "green", visited, 0, printed);
262 }
263 }
264
265#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000266 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
267 << " on systems with Graphviz or gv!\n";
David Greenef3614832008-10-27 18:17:03 +0000268#endif
269}
270
Dan Gohmand27a0e02008-11-19 23:18:57 +0000271std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohmand7177102008-11-19 22:09:45 +0000272 std::string s;
273 raw_string_ostream O(s);
274 O << "SU(" << SU->NodeNum << "): ";
275 if (SU->getNode()) {
276 SmallVector<SDNode *, 4> FlaggedNodes;
277 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
278 FlaggedNodes.push_back(N);
279 while (!FlaggedNodes.empty()) {
Tobias Grosser810b18c2009-11-30 12:38:47 +0000280 O << DOTGraphTraits<SelectionDAG*>
281 ::getSimpleNodeLabel(FlaggedNodes.back(), DAG);
Dan Gohmand7177102008-11-19 22:09:45 +0000282 FlaggedNodes.pop_back();
283 if (!FlaggedNodes.empty())
284 O << "\n ";
285 }
286 } else {
287 O << "CROSS RC COPY";
288 }
289 return O.str();
290}
Dan Gohman134c5b62007-08-28 20:32:58 +0000291
Dan Gohmand27a0e02008-11-19 23:18:57 +0000292void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
293 if (DAG) {
294 // Draw a special "GraphRoot" node to indicate the root of the graph.
295 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
296 const SDNode *N = DAG->getRoot().getNode();
297 if (N && N->getNodeId() != -1)
298 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
299 "color=blue,style=dashed");
300 }
Dan Gohman134c5b62007-08-28 20:32:58 +0000301}