blob: 2ca4d8fa6cfe018387adbb83ec87b61fe6a7659d [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"
Bill Wendling4de8de52008-07-03 22:53:42 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmane36d39c2008-07-17 21:12:16 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000023#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/GraphWriter.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Config/config.h"
28#include <fstream>
29#include <sstream>
30using namespace llvm;
31
32namespace llvm {
33 template<>
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35 static std::string getGraphName(const SelectionDAG *G) {
36 return G->getMachineFunction().getFunction()->getName();
37 }
38
39 static bool renderGraphFromBottomUp() {
40 return true;
41 }
42
43 static bool hasNodeAddressLabel(const SDNode *Node,
44 const SelectionDAG *Graph) {
45 return true;
46 }
47
48 /// If you want to override the dot attributes printed for a particular
49 /// edge, override this method.
50 template<typename EdgeIter>
51 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
52 SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
Duncan Sands92c43912008-06-06 12:08:01 +000053 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 if (VT == MVT::Flag)
55 return "color=red,style=bold";
56 else if (VT == MVT::Other)
57 return "color=blue,style=dashed";
58 return "";
59 }
60
61
62 static std::string getNodeLabel(const SDNode *Node,
63 const SelectionDAG *Graph);
64 static std::string getNodeAttributes(const SDNode *N,
65 const SelectionDAG *Graph) {
66#ifndef NDEBUG
67 const std::string &Attrs = Graph->getGraphAttrs(N);
68 if (!Attrs.empty()) {
69 if (Attrs.find("shape=") == std::string::npos)
70 return std::string("shape=Mrecord,") + Attrs;
71 else
72 return Attrs;
73 }
74#endif
75 return "shape=Mrecord";
76 }
77
78 static void addCustomGraphFeatures(SelectionDAG *G,
79 GraphWriter<SelectionDAG*> &GW) {
80 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
81 if (G->getRoot().Val)
82 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
83 }
84 };
85}
86
87std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
88 const SelectionDAG *G) {
89 std::string Op = Node->getOperationName(G);
90
91 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
92 if (Node->getValueType(i) == MVT::Other)
93 Op += ":ch";
94 else
Duncan Sands92c43912008-06-06 12:08:01 +000095 Op = Op + ":" + Node->getValueType(i).getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
98 Op += ": " + utostr(CSDN->getValue());
99 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000100 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 } else if (const GlobalAddressSDNode *GADN =
102 dyn_cast<GlobalAddressSDNode>(Node)) {
103 int offset = GADN->getOffset();
104 Op += ": " + GADN->getGlobal()->getName();
105 if (offset > 0)
106 Op += "+" + itostr(offset);
107 else
108 Op += itostr(offset);
109 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
110 Op += " " + itostr(FIDN->getIndex());
111 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
112 Op += " " + itostr(JTDN->getIndex());
113 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
114 if (CP->isMachineConstantPoolEntry()) {
115 std::ostringstream SS;
116 CP->getMachineCPVal()->print(SS);
117 Op += "<" + SS.str() + ">";
118 } else {
119 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000120 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
122 Op += "<" + utostr(CI->getZExtValue()) + ">";
123 else {
124 std::ostringstream SS;
125 WriteAsOperand(SS, CP->getConstVal(), false);
126 Op += "<" + SS.str() + ">";
127 }
128 }
129 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
130 Op = "BB: ";
131 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
132 if (LBB)
133 Op += LBB->getName();
134 //Op += " " + (const void*)BBDN->getBasicBlock();
135 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
136 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000137 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000138 Op = Op + " " +
Bill Wendling6c02cd22008-02-27 06:33:05 +0000139 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 } else {
141 Op += " #" + utostr(R->getReg());
142 }
Dan Gohman472d12c2008-06-30 20:59:49 +0000143 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
144 Op += ": " + D->getCompileUnit()->getFileName();
145 Op += ":" + utostr(D->getLine());
146 if (D->getColumn() != 0)
147 Op += ":" + utostr(D->getColumn());
Dan Gohmanfa607c92008-07-01 00:05:16 +0000148 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
149 Op += ": LabelID=" + utostr(L->getLabelID());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 } else if (const ExternalSymbolSDNode *ES =
151 dyn_cast<ExternalSymbolSDNode>(Node)) {
152 Op += "'" + std::string(ES->getSymbol()) + "'";
153 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
154 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000155 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000157 Op += "<null>";
158 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000159 const Value *V = M->MO.getValue();
160 Op += '<';
161 if (!V) {
162 Op += "(unknown)";
Duncan Sandscc8209c2008-07-18 21:07:41 +0000163 } else if (isa<PseudoSourceValue>(V)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000164 // PseudoSourceValues don't have names, so use their print method.
Dan Gohman6295ea22008-07-14 17:51:24 +0000165 std::ostringstream SS;
166 M->MO.getValue()->print(SS);
Dan Gohmane36d39c2008-07-17 21:12:16 +0000167 Op += SS.str();
Dan Gohman6295ea22008-07-14 17:51:24 +0000168 } else {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000169 Op += V->getName();
Dan Gohman6295ea22008-07-14 17:51:24 +0000170 }
Dan Gohmane36d39c2008-07-17 21:12:16 +0000171 Op += '+' + itostr(M->MO.getOffset()) + '>';
Duncan Sandsc93fae32008-03-21 09:14:45 +0000172 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
173 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000175 Op = Op + " VT=" + N->getVT().getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
177 bool doExt = true;
178 switch (LD->getExtensionType()) {
179 default: doExt = false; break;
180 case ISD::EXTLOAD:
181 Op = Op + "<anyext ";
182 break;
183 case ISD::SEXTLOAD:
184 Op = Op + " <sext ";
185 break;
186 case ISD::ZEXTLOAD:
187 Op = Op + " <zext ";
188 break;
189 }
190 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +0000191 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000192 if (LD->isVolatile())
193 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000195 if (LD->getAlignment() > 1)
196 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
198 if (ST->isTruncatingStore())
Duncan Sands92c43912008-06-06 12:08:01 +0000199 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000200 if (ST->isVolatile())
201 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000203 if (ST->getAlignment() > 1)
204 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000206
207#if 0
208 Op += " Id=" + itostr(Node->getNodeId());
209#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
211 return Op;
212}
213
214
215/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
216/// rendered using 'dot'.
217///
218void SelectionDAG::viewGraph() {
219// This code is only for debugging!
220#ifndef NDEBUG
221 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
222#else
223 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
224 << "systems with Graphviz or gv!\n";
225#endif // NDEBUG
226}
227
228
229/// clearGraphAttrs - Clear all previously defined node graph attributes.
230/// Intended to be used from a debugging tool (eg. gdb).
231void SelectionDAG::clearGraphAttrs() {
232#ifndef NDEBUG
233 NodeGraphAttrs.clear();
234#else
235 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
236 << " on systems with Graphviz or gv!\n";
237#endif
238}
239
240
241/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
242///
243void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
244#ifndef NDEBUG
245 NodeGraphAttrs[N] = Attrs;
246#else
247 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
248 << " on systems with Graphviz or gv!\n";
249#endif
250}
251
252
253/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
254/// Used from getNodeAttributes.
255const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
256#ifndef NDEBUG
257 std::map<const SDNode *, std::string>::const_iterator I =
258 NodeGraphAttrs.find(N);
259
260 if (I != NodeGraphAttrs.end())
261 return I->second;
262 else
263 return "";
264#else
265 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
266 << " on systems with Graphviz or gv!\n";
267 return std::string("");
268#endif
269}
270
271/// setGraphColor - Convenience for setting node color attribute.
272///
273void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
274#ifndef NDEBUG
275 NodeGraphAttrs[N] = std::string("color=") + Color;
276#else
277 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
278 << " on systems with Graphviz or gv!\n";
279#endif
280}
281
Dan Gohman134c5b62007-08-28 20:32:58 +0000282namespace llvm {
283 template<>
284 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
285 static std::string getGraphName(const ScheduleDAG *G) {
286 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
287 }
288
289 static bool renderGraphFromBottomUp() {
290 return true;
291 }
292
293 static bool hasNodeAddressLabel(const SUnit *Node,
294 const ScheduleDAG *Graph) {
295 return true;
296 }
297
298 /// If you want to override the dot attributes printed for a particular
299 /// edge, override this method.
300 template<typename EdgeIter>
301 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman75d89e22008-04-14 23:15:07 +0000302 if (EI.isSpecialDep())
303 return "color=cyan,style=dashed";
Evan Chenge7959472007-09-19 01:38:40 +0000304 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000305 return "color=blue,style=dashed";
306 return "";
307 }
308
309
310 static std::string getNodeLabel(const SUnit *Node,
311 const ScheduleDAG *Graph);
312 static std::string getNodeAttributes(const SUnit *N,
313 const ScheduleDAG *Graph) {
314 return "shape=Mrecord";
315 }
316
317 static void addCustomGraphFeatures(ScheduleDAG *G,
318 GraphWriter<ScheduleDAG*> &GW) {
319 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Dan Gohman018d7b72008-06-21 19:18:17 +0000320 const SDNode *N = G->DAG.getRoot().Val;
321 if (N && N->getNodeId() != -1)
322 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000323 }
324 };
325}
326
327std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
328 const ScheduleDAG *G) {
329 std::string Op;
330
331 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
332 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
333 &G->DAG) + "\n";
334 }
335
Dan Gohmand0627012008-03-21 22:51:06 +0000336 if (SU->Node)
337 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
338 else
339 Op += "<CROSS RC COPY>";
Dan Gohman134c5b62007-08-28 20:32:58 +0000340
341 return Op;
342}
343
344
345/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
346/// rendered using 'dot'.
347///
348void ScheduleDAG::viewGraph() {
349// This code is only for debugging!
350#ifndef NDEBUG
351 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
352#else
353 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
354 << "systems with Graphviz or gv!\n";
355#endif // NDEBUG
356}