blob: d8230be3c343ad1a7c25857fe3c7e6d1af0c284c [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
14#include "llvm/Constants.h"
15#include "llvm/Function.h"
16#include "llvm/Assembly/Writer.h"
17#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman134c5b62007-08-28 20:32:58 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000021#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Support/GraphWriter.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/Config/config.h"
26#include <fstream>
27#include <sstream>
28using namespace llvm;
29
30namespace llvm {
31 template<>
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33 static std::string getGraphName(const SelectionDAG *G) {
34 return G->getMachineFunction().getFunction()->getName();
35 }
36
37 static bool renderGraphFromBottomUp() {
38 return true;
39 }
40
41 static bool hasNodeAddressLabel(const SDNode *Node,
42 const SelectionDAG *Graph) {
43 return true;
44 }
45
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)
55 return "color=blue,style=dashed";
56 return "";
57 }
58
59
60 static std::string getNodeLabel(const SDNode *Node,
61 const SelectionDAG *Graph);
62 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
73 return "shape=Mrecord";
74 }
75
76 static void addCustomGraphFeatures(SelectionDAG *G,
77 GraphWriter<SelectionDAG*> &GW) {
78 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
79 if (G->getRoot().Val)
80 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
81 }
82 };
83}
84
85std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
86 const SelectionDAG *G) {
87 std::string Op = Node->getOperationName(G);
88
89 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
95 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
96 Op += ": " + utostr(CSDN->getValue());
97 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +000098 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 } else if (const GlobalAddressSDNode *GADN =
100 dyn_cast<GlobalAddressSDNode>(Node)) {
101 int offset = GADN->getOffset();
102 Op += ": " + GADN->getGlobal()->getName();
103 if (offset > 0)
104 Op += "+" + itostr(offset);
105 else
106 Op += itostr(offset);
107 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
108 Op += " " + itostr(FIDN->getIndex());
109 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
110 Op += " " + itostr(JTDN->getIndex());
111 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
112 if (CP->isMachineConstantPoolEntry()) {
113 std::ostringstream SS;
114 CP->getMachineCPVal()->print(SS);
115 Op += "<" + SS.str() + ">";
116 } else {
117 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000118 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
126 }
127 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
128 Op = "BB: ";
129 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
130 if (LBB)
131 Op += LBB->getName();
132 //Op += " " + (const void*)BBDN->getBasicBlock();
133 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
134 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000135 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000136 Op = Op + " " +
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000137 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 } else {
139 Op += " #" + utostr(R->getReg());
140 }
141 } else if (const ExternalSymbolSDNode *ES =
142 dyn_cast<ExternalSymbolSDNode>(Node)) {
143 Op += "'" + std::string(ES->getSymbol()) + "'";
144 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
145 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000146 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000148 Op += "<null>";
149 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
150 if (M->MO.getValue())
151 Op += "<" + M->MO.getValue()->getName() + ":" + itostr(M->MO.getOffset()) + ">";
152 else
153 Op += "<null:" + itostr(M->MO.getOffset()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
155 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
156 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
157 Op = Op + "\"" + N->getValue() + "\"";
158 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
159 bool doExt = true;
160 switch (LD->getExtensionType()) {
161 default: doExt = false; break;
162 case ISD::EXTLOAD:
163 Op = Op + "<anyext ";
164 break;
165 case ISD::SEXTLOAD:
166 Op = Op + " <sext ";
167 break;
168 case ISD::ZEXTLOAD:
169 Op = Op + " <zext ";
170 break;
171 }
172 if (doExt)
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000173 Op += MVT::getValueTypeString(LD->getMemoryVT()) + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000174 if (LD->isVolatile())
175 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000177 if (LD->getAlignment() > 1)
178 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
180 if (ST->isTruncatingStore())
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000181 Op += "<trunc " + MVT::getValueTypeString(ST->getMemoryVT()) + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000182 if (ST->isVolatile())
183 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000185 if (ST->getAlignment() > 1)
186 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000188
189#if 0
190 Op += " Id=" + itostr(Node->getNodeId());
191#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192
193 return Op;
194}
195
196
197/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
198/// rendered using 'dot'.
199///
200void SelectionDAG::viewGraph() {
201// This code is only for debugging!
202#ifndef NDEBUG
203 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
204#else
205 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
206 << "systems with Graphviz or gv!\n";
207#endif // NDEBUG
208}
209
210
211/// clearGraphAttrs - Clear all previously defined node graph attributes.
212/// Intended to be used from a debugging tool (eg. gdb).
213void SelectionDAG::clearGraphAttrs() {
214#ifndef NDEBUG
215 NodeGraphAttrs.clear();
216#else
217 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
218 << " on systems with Graphviz or gv!\n";
219#endif
220}
221
222
223/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
224///
225void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
226#ifndef NDEBUG
227 NodeGraphAttrs[N] = Attrs;
228#else
229 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
230 << " on systems with Graphviz or gv!\n";
231#endif
232}
233
234
235/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
236/// Used from getNodeAttributes.
237const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
238#ifndef NDEBUG
239 std::map<const SDNode *, std::string>::const_iterator I =
240 NodeGraphAttrs.find(N);
241
242 if (I != NodeGraphAttrs.end())
243 return I->second;
244 else
245 return "";
246#else
247 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
248 << " on systems with Graphviz or gv!\n";
249 return std::string("");
250#endif
251}
252
253/// setGraphColor - Convenience for setting node color attribute.
254///
255void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
256#ifndef NDEBUG
257 NodeGraphAttrs[N] = std::string("color=") + Color;
258#else
259 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
260 << " on systems with Graphviz or gv!\n";
261#endif
262}
263
Dan Gohman134c5b62007-08-28 20:32:58 +0000264namespace llvm {
265 template<>
266 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
267 static std::string getGraphName(const ScheduleDAG *G) {
268 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
269 }
270
271 static bool renderGraphFromBottomUp() {
272 return true;
273 }
274
275 static bool hasNodeAddressLabel(const SUnit *Node,
276 const ScheduleDAG *Graph) {
277 return true;
278 }
279
280 /// If you want to override the dot attributes printed for a particular
281 /// edge, override this method.
282 template<typename EdgeIter>
283 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Evan Chenge7959472007-09-19 01:38:40 +0000284 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000285 return "color=blue,style=dashed";
286 return "";
287 }
288
289
290 static std::string getNodeLabel(const SUnit *Node,
291 const ScheduleDAG *Graph);
292 static std::string getNodeAttributes(const SUnit *N,
293 const ScheduleDAG *Graph) {
294 return "shape=Mrecord";
295 }
296
297 static void addCustomGraphFeatures(ScheduleDAG *G,
298 GraphWriter<ScheduleDAG*> &GW) {
299 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
300 if (G->DAG.getRoot().Val)
Evan Cheng93f143e2007-09-25 01:54:36 +0000301 GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000302 }
303 };
304}
305
306std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
307 const ScheduleDAG *G) {
308 std::string Op;
309
310 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
311 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
312 &G->DAG) + "\n";
313 }
314
315 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
316
317 return Op;
318}
319
320
321/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
322/// rendered using 'dot'.
323///
324void ScheduleDAG::viewGraph() {
325// This code is only for debugging!
326#ifndef NDEBUG
327 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
328#else
329 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
330 << "systems with Graphviz or gv!\n";
331#endif // NDEBUG
332}