blob: 904638d4f7aa8fc88f09c775171eee1193556a89 [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 Wendling6c02cd22008-02-27 06:33:05 +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()) + ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +0000154 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
155 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
157 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
158 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
159 Op = Op + "\"" + N->getValue() + "\"";
160 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
161 bool doExt = true;
162 switch (LD->getExtensionType()) {
163 default: doExt = false; break;
164 case ISD::EXTLOAD:
165 Op = Op + "<anyext ";
166 break;
167 case ISD::SEXTLOAD:
168 Op = Op + " <sext ";
169 break;
170 case ISD::ZEXTLOAD:
171 Op = Op + " <zext ";
172 break;
173 }
174 if (doExt)
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000175 Op += MVT::getValueTypeString(LD->getMemoryVT()) + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000176 if (LD->isVolatile())
177 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000179 if (LD->getAlignment() > 1)
180 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
182 if (ST->isTruncatingStore())
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000183 Op += "<trunc " + MVT::getValueTypeString(ST->getMemoryVT()) + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000184 if (ST->isVolatile())
185 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000187 if (ST->getAlignment() > 1)
188 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000190
191#if 0
192 Op += " Id=" + itostr(Node->getNodeId());
193#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 return Op;
196}
197
198
199/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
200/// rendered using 'dot'.
201///
202void SelectionDAG::viewGraph() {
203// This code is only for debugging!
204#ifndef NDEBUG
205 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
206#else
207 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
208 << "systems with Graphviz or gv!\n";
209#endif // NDEBUG
210}
211
212
213/// clearGraphAttrs - Clear all previously defined node graph attributes.
214/// Intended to be used from a debugging tool (eg. gdb).
215void SelectionDAG::clearGraphAttrs() {
216#ifndef NDEBUG
217 NodeGraphAttrs.clear();
218#else
219 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
220 << " on systems with Graphviz or gv!\n";
221#endif
222}
223
224
225/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
226///
227void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
228#ifndef NDEBUG
229 NodeGraphAttrs[N] = Attrs;
230#else
231 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
232 << " on systems with Graphviz or gv!\n";
233#endif
234}
235
236
237/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
238/// Used from getNodeAttributes.
239const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
240#ifndef NDEBUG
241 std::map<const SDNode *, std::string>::const_iterator I =
242 NodeGraphAttrs.find(N);
243
244 if (I != NodeGraphAttrs.end())
245 return I->second;
246 else
247 return "";
248#else
249 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
250 << " on systems with Graphviz or gv!\n";
251 return std::string("");
252#endif
253}
254
255/// setGraphColor - Convenience for setting node color attribute.
256///
257void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
258#ifndef NDEBUG
259 NodeGraphAttrs[N] = std::string("color=") + Color;
260#else
261 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
263#endif
264}
265
Dan Gohman134c5b62007-08-28 20:32:58 +0000266namespace llvm {
267 template<>
268 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
269 static std::string getGraphName(const ScheduleDAG *G) {
270 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
271 }
272
273 static bool renderGraphFromBottomUp() {
274 return true;
275 }
276
277 static bool hasNodeAddressLabel(const SUnit *Node,
278 const ScheduleDAG *Graph) {
279 return true;
280 }
281
282 /// If you want to override the dot attributes printed for a particular
283 /// edge, override this method.
284 template<typename EdgeIter>
285 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman75d89e22008-04-14 23:15:07 +0000286 if (EI.isSpecialDep())
287 return "color=cyan,style=dashed";
Evan Chenge7959472007-09-19 01:38:40 +0000288 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000289 return "color=blue,style=dashed";
290 return "";
291 }
292
293
294 static std::string getNodeLabel(const SUnit *Node,
295 const ScheduleDAG *Graph);
296 static std::string getNodeAttributes(const SUnit *N,
297 const ScheduleDAG *Graph) {
298 return "shape=Mrecord";
299 }
300
301 static void addCustomGraphFeatures(ScheduleDAG *G,
302 GraphWriter<ScheduleDAG*> &GW) {
303 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Dan Gohman66d5eaf2008-04-21 20:07:30 +0000304 if (G->DAG.getRoot().Val &&
305 G->SUnitMap.find(G->DAG.getRoot().Val) != G->SUnitMap.end())
Evan Cheng93f143e2007-09-25 01:54:36 +0000306 GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000307 }
308 };
309}
310
311std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
312 const ScheduleDAG *G) {
313 std::string Op;
314
315 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
316 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
317 &G->DAG) + "\n";
318 }
319
Dan Gohmand0627012008-03-21 22:51:06 +0000320 if (SU->Node)
321 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
322 else
323 Op += "<CROSS RC COPY>";
Dan Gohman134c5b62007-08-28 20:32:58 +0000324
325 return Op;
326}
327
328
329/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
330/// rendered using 'dot'.
331///
332void ScheduleDAG::viewGraph() {
333// This code is only for debugging!
334#ifndef NDEBUG
335 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
336#else
337 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
338 << "systems with Graphviz or gv!\n";
339#endif // NDEBUG
340}