blob: 3a509922abb1414e816d35bfa5a714cbc844359a [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"
32#include <fstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35namespace llvm {
36 template<>
37 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +000038
39 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
40
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
53 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossere2c3aec2009-11-30 12:38:13 +000054 /// should actually target another edge source, not a node. If this method
55 /// is implemented, getEdgeTarget should be implemented.
Dan Gohmanb38a0052008-07-21 21:06:55 +000056 template<typename EdgeIter>
57 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
58 return true;
59 }
60
61 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
62 /// called to determine which outgoing edge of Node is the target of this
63 /// edge.
64 template<typename EdgeIter>
65 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
66 SDNode *TargetNode = *I;
67 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif46bf5472008-08-26 22:36:50 +000068 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohmanb38a0052008-07-21 21:06:55 +000069 return NI;
70 }
71
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 static std::string getGraphName(const SelectionDAG *G) {
73 return G->getMachineFunction().getFunction()->getName();
74 }
75
76 static bool renderGraphFromBottomUp() {
77 return true;
78 }
79
80 static bool hasNodeAddressLabel(const SDNode *Node,
81 const SelectionDAG *Graph) {
82 return true;
83 }
84
85 /// If you want to override the dot attributes printed for a particular
86 /// edge, override this method.
87 template<typename EdgeIter>
88 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman8181bd12008-07-27 21:46:04 +000089 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersonac9de032009-08-10 22:56:29 +000090 EVT VT = Op.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +000091 if (VT == MVT::Flag)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 return "color=red,style=bold";
Owen Anderson36e3a6e2009-08-11 20:47:22 +000093 else if (VT == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 return "color=blue,style=dashed";
95 return "";
96 }
97
98
99 static std::string getNodeLabel(const SDNode *Node,
Owen Andersonf4a15462009-06-24 17:37:09 +0000100 const SelectionDAG *Graph,
101 bool ShortNames);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 static std::string getNodeAttributes(const SDNode *N,
103 const SelectionDAG *Graph) {
104#ifndef NDEBUG
105 const std::string &Attrs = Graph->getGraphAttrs(N);
106 if (!Attrs.empty()) {
107 if (Attrs.find("shape=") == std::string::npos)
108 return std::string("shape=Mrecord,") + Attrs;
109 else
110 return Attrs;
111 }
112#endif
113 return "shape=Mrecord";
114 }
115
116 static void addCustomGraphFeatures(SelectionDAG *G,
117 GraphWriter<SelectionDAG*> &GW) {
118 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greif1c80d112008-08-28 21:40:38 +0000119 if (G->getRoot().getNode())
120 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman7951f802008-07-22 17:52:59 +0000121 "color=blue,style=dashed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 }
123 };
124}
125
126std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Owen Andersonf4a15462009-06-24 17:37:09 +0000127 const SelectionDAG *G,
128 bool ShortNames) {
Chris Lattner367c8512009-06-26 19:06:10 +0000129 std::string Result = Node->getOperationName(G);
130 {
131 raw_string_ostream OS(Result);
132 Node->print_details(OS, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 }
Chris Lattner367c8512009-06-26 19:06:10 +0000134 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135}
136
137
138/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
139/// rendered using 'dot'.
140///
Dan Gohmanb552df72008-07-21 20:00:07 +0000141void SelectionDAG::viewGraph(const std::string &Title) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142// This code is only for debugging!
143#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000144 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
145 false, Title);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000147 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
148 << "systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149#endif // NDEBUG
150}
151
Dan Gohman8c892862008-07-30 18:48:53 +0000152// This overload is defined out-of-line here instead of just using a
153// default parameter because this is easiest for gdb to call.
154void SelectionDAG::viewGraph() {
155 viewGraph("");
156}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
158/// clearGraphAttrs - Clear all previously defined node graph attributes.
159/// Intended to be used from a debugging tool (eg. gdb).
160void SelectionDAG::clearGraphAttrs() {
161#ifndef NDEBUG
162 NodeGraphAttrs.clear();
163#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000164 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
165 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166#endif
167}
168
169
170/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
171///
172void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
173#ifndef NDEBUG
174 NodeGraphAttrs[N] = Attrs;
175#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000176 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
177 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178#endif
179}
180
181
182/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
183/// Used from getNodeAttributes.
184const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
185#ifndef NDEBUG
186 std::map<const SDNode *, std::string>::const_iterator I =
187 NodeGraphAttrs.find(N);
188
189 if (I != NodeGraphAttrs.end())
190 return I->second;
191 else
192 return "";
193#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000194 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
195 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 return std::string("");
197#endif
198}
199
200/// setGraphColor - Convenience for setting node color attribute.
201///
202void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
203#ifndef NDEBUG
204 NodeGraphAttrs[N] = std::string("color=") + Color;
205#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000206 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
207 << " on systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208#endif
209}
210
David Greenef3614832008-10-27 18:17:03 +0000211/// setSubgraphColorHelper - Implement setSubgraphColor. Return
212/// whether we truncated the search.
213///
214bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
215 int level, bool &printed) {
216 bool hit_limit = false;
217
218#ifndef NDEBUG
219 if (level >= 20) {
220 if (!printed) {
221 printed = true;
Chris Lattner2b40c562009-08-23 06:35:02 +0000222 DEBUG(errs() << "setSubgraphColor hit max level\n");
David Greenef3614832008-10-27 18:17:03 +0000223 }
224 return true;
225 }
226
227 unsigned oldSize = visited.size();
228 visited.insert(N);
229 if (visited.size() != oldSize) {
230 setGraphColor(N, Color);
231 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
232 i != iend;
233 ++i) {
234 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
235 }
236 }
237#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000238 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
239 << " on systems with Graphviz or gv!\n";
David Greenef3614832008-10-27 18:17:03 +0000240#endif
241 return hit_limit;
242}
243
244/// setSubgraphColor - Convenience for setting subgraph color attribute.
245///
246void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
247#ifndef NDEBUG
248 DenseSet<SDNode *> visited;
249 bool printed = false;
250 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
251 // Visually mark that we hit the limit
Ted Kremenekb2b22922008-10-27 22:43:07 +0000252 if (strcmp(Color, "red") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000253 setSubgraphColorHelper(N, "blue", visited, 0, printed);
254 }
Ted Kremenekb2b22922008-10-27 22:43:07 +0000255 else if (strcmp(Color, "yellow") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000256 setSubgraphColorHelper(N, "green", visited, 0, printed);
257 }
258 }
259
260#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000261 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
David Greenef3614832008-10-27 18:17:03 +0000263#endif
264}
265
Dan Gohmand27a0e02008-11-19 23:18:57 +0000266std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohmand7177102008-11-19 22:09:45 +0000267 std::string s;
268 raw_string_ostream O(s);
269 O << "SU(" << SU->NodeNum << "): ";
270 if (SU->getNode()) {
271 SmallVector<SDNode *, 4> FlaggedNodes;
272 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
273 FlaggedNodes.push_back(N);
274 while (!FlaggedNodes.empty()) {
Owen Andersonf4a15462009-06-24 17:37:09 +0000275 O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(),
276 DAG, false);
Dan Gohmand7177102008-11-19 22:09:45 +0000277 FlaggedNodes.pop_back();
278 if (!FlaggedNodes.empty())
279 O << "\n ";
280 }
281 } else {
282 O << "CROSS RC COPY";
283 }
284 return O.str();
285}
Dan Gohman134c5b62007-08-28 20:32:58 +0000286
Dan Gohmand27a0e02008-11-19 23:18:57 +0000287void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
288 if (DAG) {
289 // Draw a special "GraphRoot" node to indicate the root of the graph.
290 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
291 const SDNode *N = DAG->getRoot().getNode();
292 if (N && N->getNodeId() != -1)
293 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
294 "color=blue,style=dashed");
295 }
Dan Gohman134c5b62007-08-28 20:32:58 +0000296}