blob: 9396f16a99e02c99be4835a4e66fd293f0bc7536 [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 Gohmand27a0e02008-11-19 23:18:57 +000018#include "llvm/CodeGen/ScheduleDAGSDNodes.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"
Devang Patelfcf1c752009-01-13 00:35:13 +000023#include "llvm/Analysis/DebugInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetMachine.h"
David Greenef3614832008-10-27 18:17:03 +000026#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/GraphWriter.h"
Chris Lattner491f7832008-08-23 22:53:13 +000028#include "llvm/Support/raw_ostream.h"
David Greenef3614832008-10-27 18:17:03 +000029#include "llvm/ADT/DenseSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/Config/config.h"
32#include <fstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35namespace llvm {
36 template<>
37 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
Dan Gohmanb38a0052008-07-21 21:06:55 +000038 static bool hasEdgeDestLabels() {
39 return true;
40 }
41
42 static unsigned numEdgeDestLabels(const void *Node) {
43 return ((const SDNode *) Node)->getNumValues();
44 }
45
46 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
47 return ((const SDNode *) Node)->getValueType(i).getMVTString();
48 }
49
50 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
51 /// should actually target another edge source, not a node. If this method is
52 /// implemented, getEdgeTarget should be implemented.
53 template<typename EdgeIter>
54 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
55 return true;
56 }
57
58 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
59 /// called to determine which outgoing edge of Node is the target of this
60 /// edge.
61 template<typename EdgeIter>
62 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
63 SDNode *TargetNode = *I;
64 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
Gabor Greif46bf5472008-08-26 22:36:50 +000065 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
Dan Gohmanb38a0052008-07-21 21:06:55 +000066 return NI;
67 }
68
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 static std::string getGraphName(const SelectionDAG *G) {
70 return G->getMachineFunction().getFunction()->getName();
71 }
72
73 static bool renderGraphFromBottomUp() {
74 return true;
75 }
76
77 static bool hasNodeAddressLabel(const SDNode *Node,
78 const SelectionDAG *Graph) {
79 return true;
80 }
81
82 /// If you want to override the dot attributes printed for a particular
83 /// edge, override this method.
84 template<typename EdgeIter>
85 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman8181bd12008-07-27 21:46:04 +000086 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
Duncan Sands92c43912008-06-06 12:08:01 +000087 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 if (VT == MVT::Flag)
89 return "color=red,style=bold";
90 else if (VT == MVT::Other)
91 return "color=blue,style=dashed";
92 return "";
93 }
94
95
96 static std::string getNodeLabel(const SDNode *Node,
97 const SelectionDAG *Graph);
98 static std::string getNodeAttributes(const SDNode *N,
99 const SelectionDAG *Graph) {
100#ifndef NDEBUG
101 const std::string &Attrs = Graph->getGraphAttrs(N);
102 if (!Attrs.empty()) {
103 if (Attrs.find("shape=") == std::string::npos)
104 return std::string("shape=Mrecord,") + Attrs;
105 else
106 return Attrs;
107 }
108#endif
109 return "shape=Mrecord";
110 }
111
112 static void addCustomGraphFeatures(SelectionDAG *G,
113 GraphWriter<SelectionDAG*> &GW) {
114 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Gabor Greif1c80d112008-08-28 21:40:38 +0000115 if (G->getRoot().getNode())
116 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
Dan Gohman7951f802008-07-22 17:52:59 +0000117 "color=blue,style=dashed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 }
119 };
120}
121
122std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
123 const SelectionDAG *G) {
124 std::string Op = Node->getOperationName(G);
125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000127 Op += ": " + utostr(CSDN->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000129 Op += ": " + ftostr(CSDN->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 } else if (const GlobalAddressSDNode *GADN =
131 dyn_cast<GlobalAddressSDNode>(Node)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 Op += ": " + GADN->getGlobal()->getName();
Dan Gohmancfe43232008-10-18 18:22:42 +0000133 if (int64_t Offset = GADN->getOffset()) {
Chris Lattner16594762008-09-21 18:38:31 +0000134 if (Offset > 0)
135 Op += "+" + itostr(Offset);
136 else
137 Op += itostr(Offset);
138 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
140 Op += " " + itostr(FIDN->getIndex());
141 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
142 Op += " " + itostr(JTDN->getIndex());
143 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
144 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner491f7832008-08-23 22:53:13 +0000145 Op += '<';
146 {
147 raw_string_ostream OSS(Op);
148 OSS << *CP->getMachineCPVal();
149 }
150 Op += '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 } else {
152 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesendf8a8312007-08-31 04:03:46 +0000153 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
155 Op += "<" + utostr(CI->getZExtValue()) + ">";
156 else {
Chris Lattner491f7832008-08-23 22:53:13 +0000157 Op += '<';
158 {
159 raw_string_ostream OSS(Op);
160 WriteAsOperand(OSS, CP->getConstVal(), false);
161 }
162 Op += '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 }
164 }
Dan Gohman27d53172008-09-16 21:18:22 +0000165 Op += " A=" + itostr(1 << CP->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
167 Op = "BB: ";
168 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
169 if (LBB)
170 Op += LBB->getName();
171 //Op += " " + (const void*)BBDN->getBasicBlock();
172 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
173 if (G && R->getReg() != 0 &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000174 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling8eeb9792008-02-26 21:11:01 +0000175 Op = Op + " " +
Bill Wendling6c02cd22008-02-27 06:33:05 +0000176 G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 } else {
178 Op += " #" + utostr(R->getReg());
179 }
Dan Gohman472d12c2008-06-30 20:59:49 +0000180 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
Devang Patelfcf1c752009-01-13 00:35:13 +0000181 DICompileUnit CU(cast<GlobalVariable>(D->getCompileUnit()));
182 Op += ": " + CU.getFilename();
Dan Gohman472d12c2008-06-30 20:59:49 +0000183 Op += ":" + utostr(D->getLine());
184 if (D->getColumn() != 0)
185 Op += ":" + utostr(D->getColumn());
Dan Gohmanfa607c92008-07-01 00:05:16 +0000186 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
187 Op += ": LabelID=" + utostr(L->getLabelID());
Dan Gohman705e3f72008-09-13 01:54:27 +0000188 } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) {
189 Op += ": CallingConv=" + utostr(C->getCallingConv());
190 if (C->isVarArg())
191 Op += ", isVarArg";
192 if (C->isTailCall())
193 Op += ", isTailCall";
Bill Wendlingfef06052008-09-16 21:48:12 +0000194 } else if (const ExternalSymbolSDNode *ES =
195 dyn_cast<ExternalSymbolSDNode>(Node)) {
196 Op += "'" + std::string(ES->getSymbol()) + "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
198 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +0000199 Op += "<" + M->getValue()->getName() + ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 else
Dan Gohman12a9c082008-02-06 22:27:42 +0000201 Op += "<null>";
202 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000203 const Value *V = M->MO.getValue();
204 Op += '<';
205 if (!V) {
206 Op += "(unknown)";
Dan Gohman3a2f5072008-12-15 17:28:10 +0000207 } else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000208 // PseudoSourceValues don't have names, so use their print method.
Dan Gohman3a2f5072008-12-15 17:28:10 +0000209 raw_string_ostream OSS(Op);
210 PSV->print(OSS);
Dan Gohman6295ea22008-07-14 17:51:24 +0000211 } else {
Dan Gohmane36d39c2008-07-17 21:12:16 +0000212 Op += V->getName();
Dan Gohman6295ea22008-07-14 17:51:24 +0000213 }
Dan Gohmane36d39c2008-07-17 21:12:16 +0000214 Op += '+' + itostr(M->MO.getOffset()) + '>';
Duncan Sandsc93fae32008-03-21 09:14:45 +0000215 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
216 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000218 Op = Op + " VT=" + N->getVT().getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
220 bool doExt = true;
221 switch (LD->getExtensionType()) {
222 default: doExt = false; break;
223 case ISD::EXTLOAD:
224 Op = Op + "<anyext ";
225 break;
226 case ISD::SEXTLOAD:
227 Op = Op + " <sext ";
228 break;
229 case ISD::ZEXTLOAD:
230 Op = Op + " <zext ";
231 break;
232 }
233 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +0000234 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000235 if (LD->isVolatile())
236 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000238 if (LD->getAlignment() > 1)
239 Op += " A=" + utostr(LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
241 if (ST->isTruncatingStore())
Duncan Sands92c43912008-06-06 12:08:01 +0000242 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner35165f12008-01-25 06:40:45 +0000243 if (ST->isVolatile())
244 Op += "<V>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner35165f12008-01-25 06:40:45 +0000246 if (ST->getAlignment() > 1)
247 Op += " A=" + utostr(ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 }
Chris Lattner7271a2d2007-10-15 05:32:43 +0000249
250#if 0
251 Op += " Id=" + itostr(Node->getNodeId());
252#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
254 return Op;
255}
256
257
258/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
259/// rendered using 'dot'.
260///
Dan Gohmanb552df72008-07-21 20:00:07 +0000261void SelectionDAG::viewGraph(const std::string &Title) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262// This code is only for debugging!
263#ifndef NDEBUG
Dan Gohmanb552df72008-07-21 20:00:07 +0000264 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
265 Title);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266#else
267 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
268 << "systems with Graphviz or gv!\n";
269#endif // NDEBUG
270}
271
Dan Gohman8c892862008-07-30 18:48:53 +0000272// This overload is defined out-of-line here instead of just using a
273// default parameter because this is easiest for gdb to call.
274void SelectionDAG::viewGraph() {
275 viewGraph("");
276}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
278/// clearGraphAttrs - Clear all previously defined node graph attributes.
279/// Intended to be used from a debugging tool (eg. gdb).
280void SelectionDAG::clearGraphAttrs() {
281#ifndef NDEBUG
282 NodeGraphAttrs.clear();
283#else
284 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
285 << " on systems with Graphviz or gv!\n";
286#endif
287}
288
289
290/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
291///
292void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
293#ifndef NDEBUG
294 NodeGraphAttrs[N] = Attrs;
295#else
296 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
297 << " on systems with Graphviz or gv!\n";
298#endif
299}
300
301
302/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
303/// Used from getNodeAttributes.
304const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
305#ifndef NDEBUG
306 std::map<const SDNode *, std::string>::const_iterator I =
307 NodeGraphAttrs.find(N);
308
309 if (I != NodeGraphAttrs.end())
310 return I->second;
311 else
312 return "";
313#else
314 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
315 << " on systems with Graphviz or gv!\n";
316 return std::string("");
317#endif
318}
319
320/// setGraphColor - Convenience for setting node color attribute.
321///
322void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
323#ifndef NDEBUG
324 NodeGraphAttrs[N] = std::string("color=") + Color;
325#else
326 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
327 << " on systems with Graphviz or gv!\n";
328#endif
329}
330
David Greenef3614832008-10-27 18:17:03 +0000331/// setSubgraphColorHelper - Implement setSubgraphColor. Return
332/// whether we truncated the search.
333///
334bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
335 int level, bool &printed) {
336 bool hit_limit = false;
337
338#ifndef NDEBUG
339 if (level >= 20) {
340 if (!printed) {
341 printed = true;
342 DOUT << "setSubgraphColor hit max level\n";
343 }
344 return true;
345 }
346
347 unsigned oldSize = visited.size();
348 visited.insert(N);
349 if (visited.size() != oldSize) {
350 setGraphColor(N, Color);
351 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
352 i != iend;
353 ++i) {
354 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
355 }
356 }
357#else
358 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
359 << " on systems with Graphviz or gv!\n";
360#endif
361 return hit_limit;
362}
363
364/// setSubgraphColor - Convenience for setting subgraph color attribute.
365///
366void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
367#ifndef NDEBUG
368 DenseSet<SDNode *> visited;
369 bool printed = false;
370 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
371 // Visually mark that we hit the limit
Ted Kremenekb2b22922008-10-27 22:43:07 +0000372 if (strcmp(Color, "red") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000373 setSubgraphColorHelper(N, "blue", visited, 0, printed);
374 }
Ted Kremenekb2b22922008-10-27 22:43:07 +0000375 else if (strcmp(Color, "yellow") == 0) {
David Greenef3614832008-10-27 18:17:03 +0000376 setSubgraphColorHelper(N, "green", visited, 0, printed);
377 }
378 }
379
380#else
381 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
382 << " on systems with Graphviz or gv!\n";
383#endif
384}
385
Dan Gohmand27a0e02008-11-19 23:18:57 +0000386std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
Dan Gohmand7177102008-11-19 22:09:45 +0000387 std::string s;
388 raw_string_ostream O(s);
389 O << "SU(" << SU->NodeNum << "): ";
390 if (SU->getNode()) {
391 SmallVector<SDNode *, 4> FlaggedNodes;
392 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
393 FlaggedNodes.push_back(N);
394 while (!FlaggedNodes.empty()) {
395 O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(), DAG);
396 FlaggedNodes.pop_back();
397 if (!FlaggedNodes.empty())
398 O << "\n ";
399 }
400 } else {
401 O << "CROSS RC COPY";
402 }
403 return O.str();
404}
Dan Gohman134c5b62007-08-28 20:32:58 +0000405
Dan Gohmand27a0e02008-11-19 23:18:57 +0000406void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
407 if (DAG) {
408 // Draw a special "GraphRoot" node to indicate the root of the graph.
409 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
410 const SDNode *N = DAG->getRoot().getNode();
411 if (N && N->getNodeId() != -1)
412 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
413 "color=blue,style=dashed");
414 }
Dan Gohman134c5b62007-08-28 20:32:58 +0000415}