blob: c0311911d330f5afe7511070d3f4e3fb24f29462 [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 Gohman472d12c2008-06-30 20:59:49 +000021#include "llvm/CodeGen/MachineDebugInfoDesc.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/GraphWriter.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/Config/config.h"
27#include <fstream>
28#include <sstream>
29using namespace llvm;
30
31namespace llvm {
32 template<>
33 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
34 static std::string getGraphName(const SelectionDAG *G) {
35 return G->getMachineFunction().getFunction()->getName();
36 }
37
38 static bool renderGraphFromBottomUp() {
39 return true;
40 }
41
42 static bool hasNodeAddressLabel(const SDNode *Node,
43 const SelectionDAG *Graph) {
44 return true;
45 }
46
47 /// If you want to override the dot attributes printed for a particular
48 /// edge, override this method.
49 template<typename EdgeIter>
50 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
51 SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
Duncan Sands92c43912008-06-06 12:08:01 +000052 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (VT == MVT::Flag)
54 return "color=red,style=bold";
55 else if (VT == MVT::Other)
56 return "color=blue,style=dashed";
57 return "";
58 }
59
60
61 static std::string getNodeLabel(const SDNode *Node,
62 const SelectionDAG *Graph);
63 static std::string getNodeAttributes(const SDNode *N,
64 const SelectionDAG *Graph) {
65#ifndef NDEBUG
66 const std::string &Attrs = Graph->getGraphAttrs(N);
67 if (!Attrs.empty()) {
68 if (Attrs.find("shape=") == std::string::npos)
69 return std::string("shape=Mrecord,") + Attrs;
70 else
71 return Attrs;
72 }
73#endif
74 return "shape=Mrecord";
75 }
76
77 static void addCustomGraphFeatures(SelectionDAG *G,
78 GraphWriter<SelectionDAG*> &GW) {
79 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
80 if (G->getRoot().Val)
81 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
82 }
83 };
84}
85
86std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
87 const SelectionDAG *G) {
88 std::string Op = Node->getOperationName(G);
89
90 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
91 if (Node->getValueType(i) == MVT::Other)
92 Op += ":ch";
93 else
Duncan Sands92c43912008-06-06 12:08:01 +000094 Op = Op + ":" + Node->getValueType(i).getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
97 Op += ": " + utostr(CSDN->getValue());
98 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +000099 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 } else if (const GlobalAddressSDNode *GADN =
101 dyn_cast<GlobalAddressSDNode>(Node)) {
102 int offset = GADN->getOffset();
103 Op += ": " + GADN->getGlobal()->getName();
104 if (offset > 0)
105 Op += "+" + itostr(offset);
106 else
107 Op += itostr(offset);
108 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
109 Op += " " + itostr(FIDN->getIndex());
110 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
111 Op += " " + itostr(JTDN->getIndex());
112 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
113 if (CP->isMachineConstantPoolEntry()) {
114 std::ostringstream SS;
115 CP->getMachineCPVal()->print(SS);
116 Op += "<" + SS.str() + ">";
117 } else {
118 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000119 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
121 Op += "<" + utostr(CI->getZExtValue()) + ">";
122 else {
123 std::ostringstream SS;
124 WriteAsOperand(SS, CP->getConstVal(), false);
125 Op += "<" + SS.str() + ">";
126 }
127 }
128 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
129 Op = "BB: ";
130 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
131 if (LBB)
132 Op += LBB->getName();
133 //Op += " " + (const void*)BBDN->getBasicBlock();
134 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
135 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000136 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000137 Op = Op + " " +
Bill Wendling6c02cd22008-02-27 06:33:05 +0000138 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 } else {
140 Op += " #" + utostr(R->getReg());
141 }
Dan Gohman472d12c2008-06-30 20:59:49 +0000142 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
143 Op += ": " + D->getCompileUnit()->getFileName();
144 Op += ":" + utostr(D->getLine());
145 if (D->getColumn() != 0)
146 Op += ":" + utostr(D->getColumn());
Dan Gohmanfa607c92008-07-01 00:05:16 +0000147 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
148 Op += ": LabelID=" + utostr(L->getLabelID());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 } else if (const ExternalSymbolSDNode *ES =
150 dyn_cast<ExternalSymbolSDNode>(Node)) {
151 Op += "'" + std::string(ES->getSymbol()) + "'";
152 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
153 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000154 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000156 Op += "<null>";
157 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
158 if (M->MO.getValue())
159 Op += "<" + M->MO.getValue()->getName() + ":" + itostr(M->MO.getOffset()) + ">";
160 else
161 Op += "<null:" + itostr(M->MO.getOffset()) + ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +0000162 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
163 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000165 Op = Op + " VT=" + N->getVT().getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
167 bool doExt = true;
168 switch (LD->getExtensionType()) {
169 default: doExt = false; break;
170 case ISD::EXTLOAD:
171 Op = Op + "<anyext ";
172 break;
173 case ISD::SEXTLOAD:
174 Op = Op + " <sext ";
175 break;
176 case ISD::ZEXTLOAD:
177 Op = Op + " <zext ";
178 break;
179 }
180 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +0000181 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000182 if (LD->isVolatile())
183 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000185 if (LD->getAlignment() > 1)
186 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
188 if (ST->isTruncatingStore())
Duncan Sands92c43912008-06-06 12:08:01 +0000189 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000190 if (ST->isVolatile())
191 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000193 if (ST->getAlignment() > 1)
194 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000196
197#if 0
198 Op += " Id=" + itostr(Node->getNodeId());
199#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201 return Op;
202}
203
204
205/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
206/// rendered using 'dot'.
207///
208void SelectionDAG::viewGraph() {
209// This code is only for debugging!
210#ifndef NDEBUG
211 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
212#else
213 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
214 << "systems with Graphviz or gv!\n";
215#endif // NDEBUG
216}
217
218
219/// clearGraphAttrs - Clear all previously defined node graph attributes.
220/// Intended to be used from a debugging tool (eg. gdb).
221void SelectionDAG::clearGraphAttrs() {
222#ifndef NDEBUG
223 NodeGraphAttrs.clear();
224#else
225 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
226 << " on systems with Graphviz or gv!\n";
227#endif
228}
229
230
231/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
232///
233void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
234#ifndef NDEBUG
235 NodeGraphAttrs[N] = Attrs;
236#else
237 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
238 << " on systems with Graphviz or gv!\n";
239#endif
240}
241
242
243/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
244/// Used from getNodeAttributes.
245const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
246#ifndef NDEBUG
247 std::map<const SDNode *, std::string>::const_iterator I =
248 NodeGraphAttrs.find(N);
249
250 if (I != NodeGraphAttrs.end())
251 return I->second;
252 else
253 return "";
254#else
255 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
256 << " on systems with Graphviz or gv!\n";
257 return std::string("");
258#endif
259}
260
261/// setGraphColor - Convenience for setting node color attribute.
262///
263void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
264#ifndef NDEBUG
265 NodeGraphAttrs[N] = std::string("color=") + Color;
266#else
267 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
268 << " on systems with Graphviz or gv!\n";
269#endif
270}
271
Dan Gohman134c5b62007-08-28 20:32:58 +0000272namespace llvm {
273 template<>
274 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
275 static std::string getGraphName(const ScheduleDAG *G) {
276 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
277 }
278
279 static bool renderGraphFromBottomUp() {
280 return true;
281 }
282
283 static bool hasNodeAddressLabel(const SUnit *Node,
284 const ScheduleDAG *Graph) {
285 return true;
286 }
287
288 /// If you want to override the dot attributes printed for a particular
289 /// edge, override this method.
290 template<typename EdgeIter>
291 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman75d89e22008-04-14 23:15:07 +0000292 if (EI.isSpecialDep())
293 return "color=cyan,style=dashed";
Evan Chenge7959472007-09-19 01:38:40 +0000294 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000295 return "color=blue,style=dashed";
296 return "";
297 }
298
299
300 static std::string getNodeLabel(const SUnit *Node,
301 const ScheduleDAG *Graph);
302 static std::string getNodeAttributes(const SUnit *N,
303 const ScheduleDAG *Graph) {
304 return "shape=Mrecord";
305 }
306
307 static void addCustomGraphFeatures(ScheduleDAG *G,
308 GraphWriter<ScheduleDAG*> &GW) {
309 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Dan Gohman018d7b72008-06-21 19:18:17 +0000310 const SDNode *N = G->DAG.getRoot().Val;
311 if (N && N->getNodeId() != -1)
312 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000313 }
314 };
315}
316
317std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
318 const ScheduleDAG *G) {
319 std::string Op;
320
321 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
322 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
323 &G->DAG) + "\n";
324 }
325
Dan Gohmand0627012008-03-21 22:51:06 +0000326 if (SU->Node)
327 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
328 else
329 Op += "<CROSS RC COPY>";
Dan Gohman134c5b62007-08-28 20:32:58 +0000330
331 return Op;
332}
333
334
335/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
336/// rendered using 'dot'.
337///
338void ScheduleDAG::viewGraph() {
339// This code is only for debugging!
340#ifndef NDEBUG
341 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
342#else
343 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
344 << "systems with Graphviz or gv!\n";
345#endif // NDEBUG
346}