blob: 3a509922abb1414e816d35bfa5a714cbc844359a [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 {
Tobias Grossera10d5982009-11-30 12:38:13 +000038
39 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
40
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
53 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
Tobias Grossera10d5982009-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 Gohman35803312008-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 Greif99a6cb92008-08-26 22:36:50 +000068 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohman35803312008-07-21 21:06:55 +000069 return NI;
70 }
71
Chris Lattnere0646b82005-01-10 23:26:00 +000072 static std::string getGraphName(const SelectionDAG *G) {
73 return G->getMachineFunction().getFunction()->getName();
74 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000075
76 static bool renderGraphFromBottomUp() {
77 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000078 }
Chris Lattner37345fe2005-10-01 00:17:07 +000079
80 static bool hasNodeAddressLabel(const SDNode *Node,
81 const SelectionDAG *Graph) {
82 return true;
83 }
Chris Lattner34ab4d42006-10-20 18:06:09 +000084
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 Gohman475871a2008-07-27 21:46:04 +000089 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Owen Andersone50ed302009-08-10 22:56:29 +000090 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +000091 if (VT == MVT::Flag)
Chris Lattner34ab4d42006-10-20 18:06:09 +000092 return "color=red,style=bold";
Owen Anderson825b72b2009-08-11 20:47:22 +000093 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000094 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000095 return "";
96 }
97
Chris Lattnere0646b82005-01-10 23:26:00 +000098
Chris Lattnere9c44cd2005-01-11 00:34:33 +000099 static std::string getNodeLabel(const SDNode *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000100 const SelectionDAG *Graph,
101 bool ShortNames);
Jim Laskeyec204022006-10-02 12:26:53 +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
Chris Lattnere0646b82005-01-10 23:26:00 +0000113 return "shape=Mrecord";
114 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000115
116 static void addCustomGraphFeatures(SelectionDAG *G,
117 GraphWriter<SelectionDAG*> &GW) {
118 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greifba36cb52008-08-28 21:40:38 +0000119 if (G->getRoot().getNode())
120 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman694caf52008-07-22 17:52:59 +0000121 "color=blue,style=dashed");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +0000122 }
Chris Lattnere0646b82005-01-10 23:26:00 +0000123 };
124}
125
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000126std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000127 const SelectionDAG *G,
128 bool ShortNames) {
Chris Lattnerd212bb82009-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);
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000133 }
Chris Lattnerd212bb82009-06-26 19:06:10 +0000134 return Result;
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000135}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000137
Chris Lattner66328482005-01-10 23:08:40 +0000138/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
139/// rendered using 'dot'.
140///
Dan Gohman462dc7f2008-07-21 20:00:07 +0000141void SelectionDAG::viewGraph(const std::string &Title) {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000142// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000143#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000144 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
145 false, Title);
Reid Spencer9d5b5322006-06-27 16:49:46 +0000146#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000147 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
148 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000149#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000150}
Jim Laskeyec204022006-10-02 12:26:53 +0000151
Dan Gohman0b12aef2008-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}
Jim Laskeyec204022006-10-02 12:26:53 +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 Dunbar43ed2672009-08-23 08:50:52 +0000164 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
165 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +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 Dunbar43ed2672009-08-23 08:50:52 +0000176 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
177 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +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 Dunbar43ed2672009-08-23 08:50:52 +0000194 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
195 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +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 Dunbar43ed2672009-08-23 08:50:52 +0000206 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
207 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000208#endif
209}
210
David Greenec5e7e8d2008-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 Lattnerbbbfa992009-08-23 06:35:02 +0000222 DEBUG(errs() << "setSubgraphColor hit max level\n");
David Greenec5e7e8d2008-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 Dunbar43ed2672009-08-23 08:50:52 +0000238 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
239 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-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 Kremenek8e7fa912008-10-27 22:43:07 +0000252 if (strcmp(Color, "red") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000253 setSubgraphColorHelper(N, "blue", visited, 0, printed);
254 }
Ted Kremenek8e7fa912008-10-27 22:43:07 +0000255 else if (strcmp(Color, "yellow") == 0) {
David Greenec5e7e8d2008-10-27 18:17:03 +0000256 setSubgraphColorHelper(N, "green", visited, 0, printed);
257 }
258 }
259
260#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000261 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
David Greenec5e7e8d2008-10-27 18:17:03 +0000263#endif
264}
265
Dan Gohman343f0c02008-11-19 23:18:57 +0000266std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohman7d1cd3f2008-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 Anderson8cbc94a2009-06-24 17:37:09 +0000275 O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(),
276 DAG, false);
Dan Gohman7d1cd3f2008-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 Gohman3e1a7ae2007-08-28 20:32:58 +0000286
Dan Gohman343f0c02008-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 Gohman3e1a7ae2007-08-28 20:32:58 +0000296}