blob: a64ccd3b84fe105ea1235510c7333f9db5301b53 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
21#include "llvm/Target/MRegisterInfo.h"
22#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 &&
135 MRegisterInfo::isPhysicalRegister(R->getReg())) {
136 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
137 } else {
138 Op += " #" + utostr(R->getReg());
139 }
140 } else if (const ExternalSymbolSDNode *ES =
141 dyn_cast<ExternalSymbolSDNode>(Node)) {
142 Op += "'" + std::string(ES->getSymbol()) + "'";
143 } 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()) + ">";
148 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
149 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
150 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
151 Op = Op + "\"" + N->getValue() + "\"";
152 } 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)
167 Op = Op + MVT::getValueTypeString(LD->getLoadedVT()) + ">";
168
169 Op += LD->getIndexedModeName(LD->getAddressingMode());
170 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
171 if (ST->isTruncatingStore())
172 Op = Op + "<trunc " + MVT::getValueTypeString(ST->getStoredVT()) + ">";
173 Op += ST->getIndexedModeName(ST->getAddressingMode());
174 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000175
176#if 0
177 Op += " Id=" + itostr(Node->getNodeId());
178#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
180 return Op;
181}
182
183
184/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
185/// rendered using 'dot'.
186///
187void SelectionDAG::viewGraph() {
188// This code is only for debugging!
189#ifndef NDEBUG
190 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
191#else
192 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
193 << "systems with Graphviz or gv!\n";
194#endif // NDEBUG
195}
196
197
198/// clearGraphAttrs - Clear all previously defined node graph attributes.
199/// Intended to be used from a debugging tool (eg. gdb).
200void SelectionDAG::clearGraphAttrs() {
201#ifndef NDEBUG
202 NodeGraphAttrs.clear();
203#else
204 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
205 << " on systems with Graphviz or gv!\n";
206#endif
207}
208
209
210/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
211///
212void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
213#ifndef NDEBUG
214 NodeGraphAttrs[N] = Attrs;
215#else
216 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
217 << " on systems with Graphviz or gv!\n";
218#endif
219}
220
221
222/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
223/// Used from getNodeAttributes.
224const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
225#ifndef NDEBUG
226 std::map<const SDNode *, std::string>::const_iterator I =
227 NodeGraphAttrs.find(N);
228
229 if (I != NodeGraphAttrs.end())
230 return I->second;
231 else
232 return "";
233#else
234 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
235 << " on systems with Graphviz or gv!\n";
236 return std::string("");
237#endif
238}
239
240/// setGraphColor - Convenience for setting node color attribute.
241///
242void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
243#ifndef NDEBUG
244 NodeGraphAttrs[N] = std::string("color=") + Color;
245#else
246 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
247 << " on systems with Graphviz or gv!\n";
248#endif
249}
250
Dan Gohman134c5b62007-08-28 20:32:58 +0000251namespace llvm {
252 template<>
253 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
254 static std::string getGraphName(const ScheduleDAG *G) {
255 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
256 }
257
258 static bool renderGraphFromBottomUp() {
259 return true;
260 }
261
262 static bool hasNodeAddressLabel(const SUnit *Node,
263 const ScheduleDAG *Graph) {
264 return true;
265 }
266
267 /// If you want to override the dot attributes printed for a particular
268 /// edge, override this method.
269 template<typename EdgeIter>
270 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Evan Chenge7959472007-09-19 01:38:40 +0000271 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000272 return "color=blue,style=dashed";
273 return "";
274 }
275
276
277 static std::string getNodeLabel(const SUnit *Node,
278 const ScheduleDAG *Graph);
279 static std::string getNodeAttributes(const SUnit *N,
280 const ScheduleDAG *Graph) {
281 return "shape=Mrecord";
282 }
283
284 static void addCustomGraphFeatures(ScheduleDAG *G,
285 GraphWriter<ScheduleDAG*> &GW) {
286 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
287 if (G->DAG.getRoot().Val)
Evan Cheng93f143e2007-09-25 01:54:36 +0000288 GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000289 }
290 };
291}
292
293std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
294 const ScheduleDAG *G) {
295 std::string Op;
296
297 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
298 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
299 &G->DAG) + "\n";
300 }
301
302 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
303
304 return Op;
305}
306
307
308/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
309/// rendered using 'dot'.
310///
311void ScheduleDAG::viewGraph() {
312// This code is only for debugging!
313#ifndef NDEBUG
314 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
315#else
316 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
317 << "systems with Graphviz or gv!\n";
318#endif // NDEBUG
319}