blob: 95c791b43eb7aa120379c9d33a7b15a6e779dfe3 [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
Chris Lattner5839bf22005-08-26 17:15:30 +000014#include "llvm/Constants.h"
15#include "llvm/Function.h"
Chris Lattner52676512006-03-05 09:38:03 +000016#include "llvm/Assembly/Writer.h"
Chris Lattner66328482005-01-10 23:08:40 +000017#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman3e1a7ae2007-08-28 20:32:58 +000018#include "llvm/CodeGen/ScheduleDAG.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"
Chris Lattner7228aa72005-08-19 21:21:16 +000021#include "llvm/Target/MRegisterInfo.h"
22#include "llvm/Target/TargetMachine.h"
Chris Lattner66328482005-01-10 23:08:40 +000023#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000025#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000026#include <fstream>
Chris Lattner52676512006-03-05 09:38:03 +000027#include <sstream>
Chris Lattner66328482005-01-10 23:08:40 +000028using namespace llvm;
29
Chris Lattnere0646b82005-01-10 23:26:00 +000030namespace llvm {
31 template<>
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33 static std::string getGraphName(const SelectionDAG *G) {
34 return G->getMachineFunction().getFunction()->getName();
35 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000036
37 static bool renderGraphFromBottomUp() {
38 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000039 }
Chris Lattner37345fe2005-10-01 00:17:07 +000040
41 static bool hasNodeAddressLabel(const SDNode *Node,
42 const SelectionDAG *Graph) {
43 return true;
44 }
Chris Lattner34ab4d42006-10-20 18:06:09 +000045
46 /// If you want to override the dot attributes printed for a particular
47 /// edge, override this method.
48 template<typename EdgeIter>
49 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
50 SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
51 MVT::ValueType VT = Op.getValueType();
52 if (VT == MVT::Flag)
53 return "color=red,style=bold";
54 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000055 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000056 return "";
57 }
58
Chris Lattnere0646b82005-01-10 23:26:00 +000059
Chris Lattnere9c44cd2005-01-11 00:34:33 +000060 static std::string getNodeLabel(const SDNode *Node,
61 const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +000062 static std::string getNodeAttributes(const SDNode *N,
63 const SelectionDAG *Graph) {
64#ifndef NDEBUG
65 const std::string &Attrs = Graph->getGraphAttrs(N);
66 if (!Attrs.empty()) {
67 if (Attrs.find("shape=") == std::string::npos)
68 return std::string("shape=Mrecord,") + Attrs;
69 else
70 return Attrs;
71 }
72#endif
Chris Lattnere0646b82005-01-10 23:26:00 +000073 return "shape=Mrecord";
74 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000075
76 static void addCustomGraphFeatures(SelectionDAG *G,
77 GraphWriter<SelectionDAG*> &GW) {
78 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Jim Laskey26f7fa72006-10-17 19:33:52 +000079 if (G->getRoot().Val)
80 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000081 }
Chris Lattnere0646b82005-01-10 23:26:00 +000082 };
83}
84
Chris Lattnere9c44cd2005-01-11 00:34:33 +000085std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
86 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000087 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000088
Chris Lattnerad95d6a2005-08-16 18:31:23 +000089 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
90 if (Node->getValueType(i) == MVT::Other)
91 Op += ":ch";
92 else
93 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
94
Chris Lattnere9c44cd2005-01-11 00:34:33 +000095 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
96 Op += ": " + utostr(CSDN->getValue());
97 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +000098 Op += ": " + ftostr(CSDN->getValueAPF());
Misha Brukmanedf128a2005-04-21 22:36:52 +000099 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000100 dyn_cast<GlobalAddressSDNode>(Node)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000101 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000102 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +0000103 if (offset > 0)
104 Op += "+" + itostr(offset);
105 else
106 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000107 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000108 Op += " " + itostr(FIDN->getIndex());
Evan Cheng6cc31ae2006-11-01 04:48:30 +0000109 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
110 Op += " " + itostr(JTDN->getIndex());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000111 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Evan Chengd6594ae2006-09-12 21:00:35 +0000112 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner52676512006-03-05 09:38:03 +0000113 std::ostringstream SS;
Evan Chengd6594ae2006-09-12 21:00:35 +0000114 CP->getMachineCPVal()->print(SS);
Chris Lattner52676512006-03-05 09:38:03 +0000115 Op += "<" + SS.str() + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +0000116 } else {
117 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000118 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +0000119 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
120 Op += "<" + utostr(CI->getZExtValue()) + ">";
121 else {
122 std::ostringstream SS;
123 WriteAsOperand(SS, CP->getConstVal(), false);
124 Op += "<" + SS.str() + ">";
125 }
Chris Lattner52676512006-03-05 09:38:03 +0000126 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000127 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000128 Op = "BB: ";
129 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
130 if (LBB)
131 Op += LBB->getName();
132 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000133 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner34ab4d42006-10-20 18:06:09 +0000134 if (G && R->getReg() != 0 &&
135 MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +0000136 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
137 } else {
138 Op += " #" + utostr(R->getReg());
139 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000140 } else if (const ExternalSymbolSDNode *ES =
141 dyn_cast<ExternalSymbolSDNode>(Node)) {
142 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000143 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
144 if (M->getValue())
145 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
146 else
147 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +0000148 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Dan Gohmanb55757e2007-05-18 17:52:13 +0000149 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
Chris Lattner36ce6912005-11-29 06:21:05 +0000150 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
151 Op = Op + "\"" + N->getValue() + "\"";
Evan Cheng45aeccc2006-10-10 20:11:26 +0000152 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
153 bool doExt = true;
154 switch (LD->getExtensionType()) {
155 default: doExt = false; break;
156 case ISD::EXTLOAD:
157 Op = Op + "<anyext ";
158 break;
159 case ISD::SEXTLOAD:
160 Op = Op + " <sext ";
161 break;
162 case ISD::ZEXTLOAD:
163 Op = Op + " <zext ";
164 break;
165 }
166 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000167 Op += MVT::getValueTypeString(LD->getMemoryVT()) + ">";
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000168 if (LD->isVolatile())
169 Op += "<V>";
Evan Cheng00305822006-11-09 18:44:21 +0000170 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000171 if (LD->getAlignment() > 1)
172 Op += " A=" + utostr(LD->getAlignment());
Evan Cheng649b7ef2006-10-17 21:18:26 +0000173 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
174 if (ST->isTruncatingStore())
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000175 Op += "<trunc " + MVT::getValueTypeString(ST->getMemoryVT()) + ">";
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000176 if (ST->isVolatile())
177 Op += "<V>";
Evan Cheng00305822006-11-09 18:44:21 +0000178 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000179 if (ST->getAlignment() > 1)
180 Op += " A=" + utostr(ST->getAlignment());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000181 }
Chris Lattner59afba02007-10-15 05:32:43 +0000182
183#if 0
184 Op += " Id=" + itostr(Node->getNodeId());
185#endif
Chris Lattner36ce6912005-11-29 06:21:05 +0000186
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000187 return Op;
188}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000189
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000190
Chris Lattner66328482005-01-10 23:08:40 +0000191/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
192/// rendered using 'dot'.
193///
194void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000195// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000196#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000197 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
198#else
Bill Wendling832171c2006-12-07 20:04:42 +0000199 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
200 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000201#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000202}
Jim Laskeyec204022006-10-02 12:26:53 +0000203
204
205/// clearGraphAttrs - Clear all previously defined node graph attributes.
206/// Intended to be used from a debugging tool (eg. gdb).
207void SelectionDAG::clearGraphAttrs() {
208#ifndef NDEBUG
209 NodeGraphAttrs.clear();
210#else
Bill Wendling832171c2006-12-07 20:04:42 +0000211 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
212 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000213#endif
214}
215
216
217/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
218///
219void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
220#ifndef NDEBUG
221 NodeGraphAttrs[N] = Attrs;
222#else
Bill Wendling832171c2006-12-07 20:04:42 +0000223 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
224 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000225#endif
226}
227
228
229/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
230/// Used from getNodeAttributes.
231const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
232#ifndef NDEBUG
233 std::map<const SDNode *, std::string>::const_iterator I =
234 NodeGraphAttrs.find(N);
235
236 if (I != NodeGraphAttrs.end())
237 return I->second;
238 else
239 return "";
240#else
Bill Wendling832171c2006-12-07 20:04:42 +0000241 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
242 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000243 return std::string("");
244#endif
245}
246
247/// setGraphColor - Convenience for setting node color attribute.
248///
249void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
250#ifndef NDEBUG
251 NodeGraphAttrs[N] = std::string("color=") + Color;
252#else
Bill Wendling832171c2006-12-07 20:04:42 +0000253 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
254 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000255#endif
256}
257
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000258namespace llvm {
259 template<>
260 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
261 static std::string getGraphName(const ScheduleDAG *G) {
262 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
263 }
264
265 static bool renderGraphFromBottomUp() {
266 return true;
267 }
268
269 static bool hasNodeAddressLabel(const SUnit *Node,
270 const ScheduleDAG *Graph) {
271 return true;
272 }
273
274 /// If you want to override the dot attributes printed for a particular
275 /// edge, override this method.
276 template<typename EdgeIter>
277 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000278 if (EI.isCtrlDep())
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000279 return "color=blue,style=dashed";
280 return "";
281 }
282
283
284 static std::string getNodeLabel(const SUnit *Node,
285 const ScheduleDAG *Graph);
286 static std::string getNodeAttributes(const SUnit *N,
287 const ScheduleDAG *Graph) {
288 return "shape=Mrecord";
289 }
290
291 static void addCustomGraphFeatures(ScheduleDAG *G,
292 GraphWriter<ScheduleDAG*> &GW) {
293 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
294 if (G->DAG.getRoot().Val)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000295 GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000296 }
297 };
298}
299
300std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
301 const ScheduleDAG *G) {
302 std::string Op;
303
304 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
305 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
306 &G->DAG) + "\n";
307 }
308
309 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
310
311 return Op;
312}
313
314
315/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
316/// rendered using 'dot'.
317///
318void ScheduleDAG::viewGraph() {
319// This code is only for debugging!
320#ifndef NDEBUG
321 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
322#else
323 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
324 << "systems with Graphviz or gv!\n";
325#endif // NDEBUG
326}