blob: 184c50e9037d245c871fdba395c157338cedb6a6 [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 {
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);
62 std::advance(NI, I.getNode()->getOperand(I.getOperand()).ResNo);
63 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) {
83 SDOperand 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");
112 if (G->getRoot().Val)
113 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
114 }
115 };
116}
117
118std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
119 const SelectionDAG *G) {
120 std::string Op = Node->getOperationName(G);
121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
123 Op += ": " + utostr(CSDN->getValue());
124 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000125 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 } else if (const GlobalAddressSDNode *GADN =
127 dyn_cast<GlobalAddressSDNode>(Node)) {
128 int offset = GADN->getOffset();
129 Op += ": " + GADN->getGlobal()->getName();
130 if (offset > 0)
131 Op += "+" + itostr(offset);
132 else
133 Op += itostr(offset);
134 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
135 Op += " " + itostr(FIDN->getIndex());
136 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
137 Op += " " + itostr(JTDN->getIndex());
138 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
139 if (CP->isMachineConstantPoolEntry()) {
140 std::ostringstream SS;
141 CP->getMachineCPVal()->print(SS);
142 Op += "<" + SS.str() + ">";
143 } else {
144 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000145 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
147 Op += "<" + utostr(CI->getZExtValue()) + ">";
148 else {
149 std::ostringstream SS;
150 WriteAsOperand(SS, CP->getConstVal(), false);
151 Op += "<" + SS.str() + ">";
152 }
153 }
154 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
155 Op = "BB: ";
156 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
157 if (LBB)
158 Op += LBB->getName();
159 //Op += " " + (const void*)BBDN->getBasicBlock();
160 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
161 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000162 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000163 Op = Op + " " +
Bill Wendling6c02cd22008-02-27 06:33:05 +0000164 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 } else {
166 Op += " #" + utostr(R->getReg());
167 }
Dan Gohman472d12c2008-06-30 20:59:49 +0000168 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
169 Op += ": " + D->getCompileUnit()->getFileName();
170 Op += ":" + utostr(D->getLine());
171 if (D->getColumn() != 0)
172 Op += ":" + utostr(D->getColumn());
Dan Gohmanfa607c92008-07-01 00:05:16 +0000173 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
174 Op += ": LabelID=" + utostr(L->getLabelID());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 } else if (const ExternalSymbolSDNode *ES =
176 dyn_cast<ExternalSymbolSDNode>(Node)) {
177 Op += "'" + std::string(ES->getSymbol()) + "'";
178 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
179 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000180 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000182 Op += "<null>";
183 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000184 const Value *V = M->MO.getValue();
185 Op += '<';
186 if (!V) {
187 Op += "(unknown)";
Duncan Sandscc8209c2008-07-18 21:07:41 +0000188 } else if (isa<PseudoSourceValue>(V)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000189 // PseudoSourceValues don't have names, so use their print method.
Dan Gohman6295ea22008-07-14 17:51:24 +0000190 std::ostringstream SS;
191 M->MO.getValue()->print(SS);
Dan Gohmane36d39c2008-07-17 21:12:16 +0000192 Op += SS.str();
Dan Gohman6295ea22008-07-14 17:51:24 +0000193 } else {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000194 Op += V->getName();
Dan Gohman6295ea22008-07-14 17:51:24 +0000195 }
Dan Gohmane36d39c2008-07-17 21:12:16 +0000196 Op += '+' + itostr(M->MO.getOffset()) + '>';
Duncan Sandsc93fae32008-03-21 09:14:45 +0000197 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
198 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000200 Op = Op + " VT=" + N->getVT().getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
202 bool doExt = true;
203 switch (LD->getExtensionType()) {
204 default: doExt = false; break;
205 case ISD::EXTLOAD:
206 Op = Op + "<anyext ";
207 break;
208 case ISD::SEXTLOAD:
209 Op = Op + " <sext ";
210 break;
211 case ISD::ZEXTLOAD:
212 Op = Op + " <zext ";
213 break;
214 }
215 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +0000216 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000217 if (LD->isVolatile())
218 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000220 if (LD->getAlignment() > 1)
221 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
223 if (ST->isTruncatingStore())
Duncan Sands92c43912008-06-06 12:08:01 +0000224 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000225 if (ST->isVolatile())
226 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000228 if (ST->getAlignment() > 1)
229 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000231
232#if 0
233 Op += " Id=" + itostr(Node->getNodeId());
234#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235
236 return Op;
237}
238
239
240/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
241/// rendered using 'dot'.
242///
Dan Gohmanb552df72008-07-21 20:00:07 +0000243void SelectionDAG::viewGraph(const std::string &Title) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244// This code is only for debugging!
245#ifndef NDEBUG
Dan Gohmanb552df72008-07-21 20:00:07 +0000246 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
247 Title);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248#else
249 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
250 << "systems with Graphviz or gv!\n";
251#endif // NDEBUG
252}
253
254
255/// clearGraphAttrs - Clear all previously defined node graph attributes.
256/// Intended to be used from a debugging tool (eg. gdb).
257void SelectionDAG::clearGraphAttrs() {
258#ifndef NDEBUG
259 NodeGraphAttrs.clear();
260#else
261 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
262 << " on systems with Graphviz or gv!\n";
263#endif
264}
265
266
267/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
268///
269void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
270#ifndef NDEBUG
271 NodeGraphAttrs[N] = Attrs;
272#else
273 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
274 << " on systems with Graphviz or gv!\n";
275#endif
276}
277
278
279/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
280/// Used from getNodeAttributes.
281const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
282#ifndef NDEBUG
283 std::map<const SDNode *, std::string>::const_iterator I =
284 NodeGraphAttrs.find(N);
285
286 if (I != NodeGraphAttrs.end())
287 return I->second;
288 else
289 return "";
290#else
291 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
292 << " on systems with Graphviz or gv!\n";
293 return std::string("");
294#endif
295}
296
297/// setGraphColor - Convenience for setting node color attribute.
298///
299void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
300#ifndef NDEBUG
301 NodeGraphAttrs[N] = std::string("color=") + Color;
302#else
303 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
304 << " on systems with Graphviz or gv!\n";
305#endif
306}
307
Dan Gohman134c5b62007-08-28 20:32:58 +0000308namespace llvm {
309 template<>
310 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
311 static std::string getGraphName(const ScheduleDAG *G) {
312 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
313 }
314
315 static bool renderGraphFromBottomUp() {
316 return true;
317 }
318
319 static bool hasNodeAddressLabel(const SUnit *Node,
320 const ScheduleDAG *Graph) {
321 return true;
322 }
323
324 /// If you want to override the dot attributes printed for a particular
325 /// edge, override this method.
326 template<typename EdgeIter>
327 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman75d89e22008-04-14 23:15:07 +0000328 if (EI.isSpecialDep())
329 return "color=cyan,style=dashed";
Evan Chenge7959472007-09-19 01:38:40 +0000330 if (EI.isCtrlDep())
Dan Gohman134c5b62007-08-28 20:32:58 +0000331 return "color=blue,style=dashed";
332 return "";
333 }
334
335
336 static std::string getNodeLabel(const SUnit *Node,
337 const ScheduleDAG *Graph);
338 static std::string getNodeAttributes(const SUnit *N,
339 const ScheduleDAG *Graph) {
340 return "shape=Mrecord";
341 }
342
343 static void addCustomGraphFeatures(ScheduleDAG *G,
344 GraphWriter<ScheduleDAG*> &GW) {
345 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Dan Gohman018d7b72008-06-21 19:18:17 +0000346 const SDNode *N = G->DAG.getRoot().Val;
347 if (N && N->getNodeId() != -1)
348 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
Dan Gohman134c5b62007-08-28 20:32:58 +0000349 }
350 };
351}
352
353std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
354 const ScheduleDAG *G) {
355 std::string Op;
356
357 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
358 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
359 &G->DAG) + "\n";
360 }
361
Dan Gohmand0627012008-03-21 22:51:06 +0000362 if (SU->Node)
363 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
364 else
365 Op += "<CROSS RC COPY>";
Dan Gohman134c5b62007-08-28 20:32:58 +0000366
367 return Op;
368}
369
370
371/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
372/// rendered using 'dot'.
373///
374void ScheduleDAG::viewGraph() {
375// This code is only for debugging!
376#ifndef NDEBUG
Dan Gohmanb552df72008-07-21 20:00:07 +0000377 ViewGraph(this, "dag." + MF->getFunction()->getName(),
378 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
379 BB->getBasicBlock()->getName());
Dan Gohman134c5b62007-08-28 20:32:58 +0000380#else
381 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
382 << "systems with Graphviz or gv!\n";
383#endif // NDEBUG
384}