blob: aca115fd84e735a27935f8575d149a34705890a4 [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"
Chris Lattner491f7832008-08-23 22:53:13 +000026#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/StringExtras.h"
28#include "llvm/Config/config.h"
29#include <fstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
32namespace llvm {
33 template<>
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Dan Gohmanb38a0052008-07-21 21:06:55 +000035 static bool hasEdgeDestLabels() {
36 return true;
37 }
38
39 static unsigned numEdgeDestLabels(const void *Node) {
40 return ((const SDNode *) Node)->getNumValues();
41 }
42
43 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
44 return ((const SDNode *) Node)->getValueType(i).getMVTString();
45 }
46
47 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
48 /// should actually target another edge source, not a node. If this method is
49 /// implemented, getEdgeTarget should be implemented.
50 template<typename EdgeIter>
51 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
52 return true;
53 }
54
55 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
56 /// called to determine which outgoing edge of Node is the target of this
57 /// edge.
58 template<typename EdgeIter>
59 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
60 SDNode *TargetNode = *I;
61 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif46bf5472008-08-26 22:36:50 +000062 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohmanb38a0052008-07-21 21:06:55 +000063 return NI;
64 }
65
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 static std::string getGraphName(const SelectionDAG *G) {
67 return G->getMachineFunction().getFunction()->getName();
68 }
69
70 static bool renderGraphFromBottomUp() {
71 return true;
72 }
73
74 static bool hasNodeAddressLabel(const SDNode *Node,
75 const SelectionDAG *Graph) {
76 return true;
77 }
78
79 /// If you want to override the dot attributes printed for a particular
80 /// edge, override this method.
81 template<typename EdgeIter>
82 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman8181bd12008-07-27 21:46:04 +000083 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Duncan Sands92c43912008-06-06 12:08:01 +000084 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 if (VT == MVT::Flag)
86 return "color=red,style=bold";
87 else if (VT == MVT::Other)
88 return "color=blue,style=dashed";
89 return "";
90 }
91
92
93 static std::string getNodeLabel(const SDNode *Node,
94 const SelectionDAG *Graph);
95 static std::string getNodeAttributes(const SDNode *N,
96 const SelectionDAG *Graph) {
97#ifndef NDEBUG
98 const std::string &Attrs = Graph->getGraphAttrs(N);
99 if (!Attrs.empty()) {
100 if (Attrs.find("shape=") == std::string::npos)
101 return std::string("shape=Mrecord,") + Attrs;
102 else
103 return Attrs;
104 }
105#endif
106 return "shape=Mrecord";
107 }
108
109 static void addCustomGraphFeatures(SelectionDAG *G,
110 GraphWriter<SelectionDAG*> &GW) {
111 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greif1c80d112008-08-28 21:40:38 +0000112 if (G->getRoot().getNode())
113 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman7951f802008-07-22 17:52:59 +0000114 "color=blue,style=dashed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 }
116 };
117}
118
119std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
120 const SelectionDAG *G) {
121 std::string Op = Node->getOperationName(G);
122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000124 Op += ": " + utostr(CSDN->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000126 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 } else if (const GlobalAddressSDNode *GADN =
128 dyn_cast<GlobalAddressSDNode>(Node)) {
129 int offset = GADN->getOffset();
130 Op += ": " + GADN->getGlobal()->getName();
131 if (offset > 0)
132 Op += "+" + itostr(offset);
133 else
134 Op += itostr(offset);
135 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
136 Op += " " + itostr(FIDN->getIndex());
137 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
138 Op += " " + itostr(JTDN->getIndex());
139 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
140 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner491f7832008-08-23 22:53:13 +0000141 Op += '<';
142 {
143 raw_string_ostream OSS(Op);
144 OSS << *CP->getMachineCPVal();
145 }
146 Op += '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 } else {
148 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000149 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
151 Op += "<" + utostr(CI->getZExtValue()) + ">";
152 else {
Chris Lattner491f7832008-08-23 22:53:13 +0000153 Op += '<';
154 {
155 raw_string_ostream OSS(Op);
156 WriteAsOperand(OSS, CP->getConstVal(), false);
157 }
158 Op += '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 }
160 }
161 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
162 Op = "BB: ";
163 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
164 if (LBB)
165 Op += LBB->getName();
166 //Op += " " + (const void*)BBDN->getBasicBlock();
167 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
168 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000169 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000170 Op = Op + " " +
Bill Wendling6c02cd22008-02-27 06:33:05 +0000171 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 } else {
173 Op += " #" + utostr(R->getReg());
174 }
Dan Gohman472d12c2008-06-30 20:59:49 +0000175 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
176 Op += ": " + D->getCompileUnit()->getFileName();
177 Op += ":" + utostr(D->getLine());
178 if (D->getColumn() != 0)
179 Op += ":" + utostr(D->getColumn());
Dan Gohmanfa607c92008-07-01 00:05:16 +0000180 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
181 Op += ": LabelID=" + utostr(L->getLabelID());
Dan Gohman705e3f72008-09-13 01:54:27 +0000182 } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) {
183 Op += ": CallingConv=" + utostr(C->getCallingConv());
184 if (C->isVarArg())
185 Op += ", isVarArg";
186 if (C->isTailCall())
187 Op += ", isTailCall";
Bill Wendlingbdad5cf2008-09-16 21:12:30 +0000188 } else if (const SymbolSDNode *S = dyn_cast<SymbolSDNode>(Node)) {
189 Op += "'" + std::string(S->getSymbol()) + "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
191 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000192 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000194 Op += "<null>";
195 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000196 const Value *V = M->MO.getValue();
197 Op += '<';
198 if (!V) {
199 Op += "(unknown)";
Duncan Sandscc8209c2008-07-18 21:07:41 +0000200 } else if (isa<PseudoSourceValue>(V)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000201 // PseudoSourceValues don't have names, so use their print method.
Chris Lattner491f7832008-08-23 22:53:13 +0000202 {
203 raw_string_ostream OSS(Op);
204 OSS << *M->MO.getValue();
205 }
Dan Gohman6295ea22008-07-14 17:51:24 +0000206 } else {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000207 Op += V->getName();
Dan Gohman6295ea22008-07-14 17:51:24 +0000208 }
Dan Gohmane36d39c2008-07-17 21:12:16 +0000209 Op += '+' + itostr(M->MO.getOffset()) + '>';
Duncan Sandsc93fae32008-03-21 09:14:45 +0000210 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
211 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000213 Op = Op + " VT=" + N->getVT().getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
215 bool doExt = true;
216 switch (LD->getExtensionType()) {
217 default: doExt = false; break;
218 case ISD::EXTLOAD:
219 Op = Op + "<anyext ";
220 break;
221 case ISD::SEXTLOAD:
222 Op = Op + " <sext ";
223 break;
224 case ISD::ZEXTLOAD:
225 Op = Op + " <zext ";
226 break;
227 }
228 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +0000229 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000230 if (LD->isVolatile())
231 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000233 if (LD->getAlignment() > 1)
234 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
236 if (ST->isTruncatingStore())
Duncan Sands92c43912008-06-06 12:08:01 +0000237 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000238 if (ST->isVolatile())
239 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000241 if (ST->getAlignment() > 1)
242 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000244
245#if 0
246 Op += " Id=" + itostr(Node->getNodeId());
247#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248
249 return Op;
250}
251
252
253/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
254/// rendered using 'dot'.
255///
Dan Gohmanb552df72008-07-21 20:00:07 +0000256void SelectionDAG::viewGraph(const std::string &Title) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257// This code is only for debugging!
258#ifndef NDEBUG
Dan Gohmanb552df72008-07-21 20:00:07 +0000259 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
260 Title);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261#else
262 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
263 << "systems with Graphviz or gv!\n";
264#endif // NDEBUG
265}
266
Dan Gohman8c892862008-07-30 18:48:53 +0000267// This overload is defined out-of-line here instead of just using a
268// default parameter because this is easiest for gdb to call.
269void SelectionDAG::viewGraph() {
270 viewGraph("");
271}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
273/// clearGraphAttrs - Clear all previously defined node graph attributes.
274/// Intended to be used from a debugging tool (eg. gdb).
275void SelectionDAG::clearGraphAttrs() {
276#ifndef NDEBUG
277 NodeGraphAttrs.clear();
278#else
279 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
280 << " on systems with Graphviz or gv!\n";
281#endif
282}
283
284
285/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
286///
287void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
288#ifndef NDEBUG
289 NodeGraphAttrs[N] = Attrs;
290#else
291 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
292 << " on systems with Graphviz or gv!\n";
293#endif
294}
295
296
297/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
298/// Used from getNodeAttributes.
299const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
300#ifndef NDEBUG
301 std::map<const SDNode *, std::string>::const_iterator I =
302 NodeGraphAttrs.find(N);
303
304 if (I != NodeGraphAttrs.end())
305 return I->second;
306 else
307 return "";
308#else
309 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
310 << " on systems with Graphviz or gv!\n";
311 return std::string("");
312#endif
313}
314
315/// setGraphColor - Convenience for setting node color attribute.
316///
317void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
318#ifndef NDEBUG
319 NodeGraphAttrs[N] = std::string("color=") + Color;
320#else
321 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
322 << " on systems with Graphviz or gv!\n";
323#endif
324}
325
Dan Gohman134c5b62007-08-28 20:32:58 +0000326namespace llvm {
327 template<>
328 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
329 static std::string getGraphName(const ScheduleDAG *G) {
330 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
331 }
332
333 static bool renderGraphFromBottomUp() {
334 return true;
335 }
336
337 static bool hasNodeAddressLabel(const SUnit *Node,
338 const ScheduleDAG *Graph) {
339 return true;
340 }
341
342 /// If you want to override the dot attributes printed for a particular
343 /// edge, override this method.
344 template<typename EdgeIter>
345 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman75d89e22008-04-14 23:15:07 +0000346 if (EI.isSpecialDep())
347 return "color=cyan,style=dashed";
Evan Chenge7959472007-09-19 01:38:40 +0000348 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000349 return "color=blue,style=dashed";
350 return "";
351 }
352
353
354 static std::string getNodeLabel(const SUnit *Node,
355 const ScheduleDAG *Graph);
356 static std::string getNodeAttributes(const SUnit *N,
357 const ScheduleDAG *Graph) {
358 return "shape=Mrecord";
359 }
360
361 static void addCustomGraphFeatures(ScheduleDAG *G,
362 GraphWriter<ScheduleDAG*> &GW) {
363 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greif1c80d112008-08-28 21:40:38 +0000364 const SDNode *N = G->DAG.getRoot().getNode();
Dan Gohman018d7b72008-06-21 19:18:17 +0000365 if (N && N->getNodeId() != -1)
Dan Gohmana7cc4ff2008-07-27 22:46:49 +0000366 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1,
367 "color=blue,style=dashed");
Dan Gohman134c5b62007-08-28 20:32:58 +0000368 }
369 };
370}
371
372std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
373 const ScheduleDAG *G) {
374 std::string Op;
375
376 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
377 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
378 &G->DAG) + "\n";
379 }
380
Dan Gohmand0627012008-03-21 22:51:06 +0000381 if (SU->Node)
382 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
383 else
384 Op += "<CROSS RC COPY>";
Dan Gohman134c5b62007-08-28 20:32:58 +0000385
386 return Op;
387}
388
389
390/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
391/// rendered using 'dot'.
392///
393void ScheduleDAG::viewGraph() {
394// This code is only for debugging!
395#ifndef NDEBUG
Dan Gohmanb552df72008-07-21 20:00:07 +0000396 ViewGraph(this, "dag." + MF->getFunction()->getName(),
397 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
398 BB->getBasicBlock()->getName());
Dan Gohman134c5b62007-08-28 20:32:58 +0000399#else
400 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
401 << "systems with Graphviz or gv!\n";
402#endif // NDEBUG
403}