blob: cdfdf6f9a222391fa3e9cdc8c50f5f4e0e2c09f6 [file] [log] [blame]
Chris Lattner66328482005-01-10 23:08:40 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner66328482005-01-10 23:08:40 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner66328482005-01-10 23:08:40 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::viewGraph method.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner5839bf22005-08-26 17:15:30 +000014#include "llvm/Constants.h"
15#include "llvm/Function.h"
Chris Lattner52676512006-03-05 09:38:03 +000016#include "llvm/Assembly/Writer.h"
Chris Lattner66328482005-01-10 23:08:40 +000017#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman3e1a7ae2007-08-28 20:32:58 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner66328482005-01-10 23:08:40 +000020#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling10fff602008-07-03 22:53:42 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7228aa72005-08-19 21:21:16 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner66328482005-01-10 23:08:40 +000024#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000026#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000027#include <fstream>
Chris Lattner52676512006-03-05 09:38:03 +000028#include <sstream>
Chris Lattner66328482005-01-10 23:08:40 +000029using namespace llvm;
30
Chris Lattnere0646b82005-01-10 23:26:00 +000031namespace llvm {
32 template<>
33 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
34 static std::string getGraphName(const SelectionDAG *G) {
35 return G->getMachineFunction().getFunction()->getName();
36 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000037
38 static bool renderGraphFromBottomUp() {
39 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000040 }
Chris Lattner37345fe2005-10-01 00:17:07 +000041
42 static bool hasNodeAddressLabel(const SDNode *Node,
43 const SelectionDAG *Graph) {
44 return true;
45 }
Chris Lattner34ab4d42006-10-20 18:06:09 +000046
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 Sands83ec4b62008-06-06 12:08:01 +000052 MVT VT = Op.getValueType();
Chris Lattner34ab4d42006-10-20 18:06:09 +000053 if (VT == MVT::Flag)
54 return "color=red,style=bold";
55 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000056 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000057 return "";
58 }
59
Chris Lattnere0646b82005-01-10 23:26:00 +000060
Chris Lattnere9c44cd2005-01-11 00:34:33 +000061 static std::string getNodeLabel(const SDNode *Node,
62 const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +000063 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
Chris Lattnere0646b82005-01-10 23:26:00 +000074 return "shape=Mrecord";
75 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000076
77 static void addCustomGraphFeatures(SelectionDAG *G,
78 GraphWriter<SelectionDAG*> &GW) {
79 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Jim Laskey26f7fa72006-10-17 19:33:52 +000080 if (G->getRoot().Val)
81 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000082 }
Chris Lattnere0646b82005-01-10 23:26:00 +000083 };
84}
85
Chris Lattnere9c44cd2005-01-11 00:34:33 +000086std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
87 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000088 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000089
Chris Lattnerad95d6a2005-08-16 18:31:23 +000090 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
91 if (Node->getValueType(i) == MVT::Other)
92 Op += ":ch";
93 else
Duncan Sands83ec4b62008-06-06 12:08:01 +000094 Op = Op + ":" + Node->getValueType(i).getMVTString();
Chris Lattnerad95d6a2005-08-16 18:31:23 +000095
Chris Lattnere9c44cd2005-01-11 00:34:33 +000096 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
97 Op += ": " + utostr(CSDN->getValue());
98 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +000099 Op += ": " + ftostr(CSDN->getValueAPF());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000100 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000101 dyn_cast<GlobalAddressSDNode>(Node)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000102 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000103 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +0000104 if (offset > 0)
105 Op += "+" + itostr(offset);
106 else
107 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000108 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000109 Op += " " + itostr(FIDN->getIndex());
Evan Cheng6cc31ae2006-11-01 04:48:30 +0000110 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
111 Op += " " + itostr(JTDN->getIndex());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000112 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Evan Chengd6594ae2006-09-12 21:00:35 +0000113 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner52676512006-03-05 09:38:03 +0000114 std::ostringstream SS;
Evan Chengd6594ae2006-09-12 21:00:35 +0000115 CP->getMachineCPVal()->print(SS);
Chris Lattner52676512006-03-05 09:38:03 +0000116 Op += "<" + SS.str() + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +0000117 } else {
118 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000119 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +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 }
Chris Lattner52676512006-03-05 09:38:03 +0000127 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000128 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000129 Op = "BB: ";
130 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
131 if (LBB)
132 Op += LBB->getName();
133 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000134 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner34ab4d42006-10-20 18:06:09 +0000135 if (G && R->getReg() != 0 &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000136 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling74ab84c2008-02-26 21:11:01 +0000137 Op = Op + " " +
Bill Wendling6ef781f2008-02-27 06:33:05 +0000138 G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +0000139 } else {
140 Op += " #" + utostr(R->getReg());
141 }
Dan Gohman7f460202008-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 Gohman44066042008-07-01 00:05:16 +0000147 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
148 Op += ": LabelID=" + utostr(L->getLabelID());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000149 } else if (const ExternalSymbolSDNode *ES =
150 dyn_cast<ExternalSymbolSDNode>(Node)) {
151 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000152 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
153 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +0000154 Op += "<" + M->getValue()->getName() + ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000155 else
Dan Gohman69de1932008-02-06 22:27:42 +0000156 Op += "<null>";
157 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
Dan Gohman91d49f52008-07-14 17:51:24 +0000158 if (M->MO.getValue()) {
159 std::ostringstream SS;
160 M->MO.getValue()->print(SS);
161 Op += "<" + SS.str() + "+" + itostr(M->MO.getOffset()) + ">";
162 } else {
163 Op += "<(unknown)+" + itostr(M->MO.getOffset()) + ">";
164 }
Duncan Sands276dcbd2008-03-21 09:14:45 +0000165 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
166 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +0000167 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000168 Op = Op + " VT=" + N->getVT().getMVTString();
Evan Cheng45aeccc2006-10-10 20:11:26 +0000169 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
170 bool doExt = true;
171 switch (LD->getExtensionType()) {
172 default: doExt = false; break;
173 case ISD::EXTLOAD:
174 Op = Op + "<anyext ";
175 break;
176 case ISD::SEXTLOAD:
177 Op = Op + " <sext ";
178 break;
179 case ISD::ZEXTLOAD:
180 Op = Op + " <zext ";
181 break;
182 }
183 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +0000184 Op += LD->getMemoryVT().getMVTString() + ">";
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000185 if (LD->isVolatile())
186 Op += "<V>";
Evan Cheng00305822006-11-09 18:44:21 +0000187 Op += LD->getIndexedModeName(LD->getAddressingMode());
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000188 if (LD->getAlignment() > 1)
189 Op += " A=" + utostr(LD->getAlignment());
Evan Cheng649b7ef2006-10-17 21:18:26 +0000190 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
191 if (ST->isTruncatingStore())
Duncan Sands83ec4b62008-06-06 12:08:01 +0000192 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000193 if (ST->isVolatile())
194 Op += "<V>";
Evan Cheng00305822006-11-09 18:44:21 +0000195 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattner94ffc7e2008-01-25 06:40:45 +0000196 if (ST->getAlignment() > 1)
197 Op += " A=" + utostr(ST->getAlignment());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000198 }
Chris Lattner59afba02007-10-15 05:32:43 +0000199
200#if 0
201 Op += " Id=" + itostr(Node->getNodeId());
202#endif
Chris Lattner36ce6912005-11-29 06:21:05 +0000203
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000204 return Op;
205}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000206
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000207
Chris Lattner66328482005-01-10 23:08:40 +0000208/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
209/// rendered using 'dot'.
210///
211void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000212// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000213#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000214 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
215#else
Bill Wendling832171c2006-12-07 20:04:42 +0000216 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
217 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000218#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000219}
Jim Laskeyec204022006-10-02 12:26:53 +0000220
221
222/// clearGraphAttrs - Clear all previously defined node graph attributes.
223/// Intended to be used from a debugging tool (eg. gdb).
224void SelectionDAG::clearGraphAttrs() {
225#ifndef NDEBUG
226 NodeGraphAttrs.clear();
227#else
Bill Wendling832171c2006-12-07 20:04:42 +0000228 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
229 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000230#endif
231}
232
233
234/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
235///
236void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
237#ifndef NDEBUG
238 NodeGraphAttrs[N] = Attrs;
239#else
Bill Wendling832171c2006-12-07 20:04:42 +0000240 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
241 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000242#endif
243}
244
245
246/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
247/// Used from getNodeAttributes.
248const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
249#ifndef NDEBUG
250 std::map<const SDNode *, std::string>::const_iterator I =
251 NodeGraphAttrs.find(N);
252
253 if (I != NodeGraphAttrs.end())
254 return I->second;
255 else
256 return "";
257#else
Bill Wendling832171c2006-12-07 20:04:42 +0000258 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
259 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000260 return std::string("");
261#endif
262}
263
264/// setGraphColor - Convenience for setting node color attribute.
265///
266void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
267#ifndef NDEBUG
268 NodeGraphAttrs[N] = std::string("color=") + Color;
269#else
Bill Wendling832171c2006-12-07 20:04:42 +0000270 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
271 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000272#endif
273}
274
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000275namespace llvm {
276 template<>
277 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
278 static std::string getGraphName(const ScheduleDAG *G) {
279 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
280 }
281
282 static bool renderGraphFromBottomUp() {
283 return true;
284 }
285
286 static bool hasNodeAddressLabel(const SUnit *Node,
287 const ScheduleDAG *Graph) {
288 return true;
289 }
290
291 /// If you want to override the dot attributes printed for a particular
292 /// edge, override this method.
293 template<typename EdgeIter>
294 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
Dan Gohman89bf0a62008-04-14 23:15:07 +0000295 if (EI.isSpecialDep())
296 return "color=cyan,style=dashed";
Evan Cheng713a98d2007-09-19 01:38:40 +0000297 if (EI.isCtrlDep())
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000298 return "color=blue,style=dashed";
299 return "";
300 }
301
302
303 static std::string getNodeLabel(const SUnit *Node,
304 const ScheduleDAG *Graph);
305 static std::string getNodeAttributes(const SUnit *N,
306 const ScheduleDAG *Graph) {
307 return "shape=Mrecord";
308 }
309
310 static void addCustomGraphFeatures(ScheduleDAG *G,
311 GraphWriter<ScheduleDAG*> &GW) {
312 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000313 const SDNode *N = G->DAG.getRoot().Val;
314 if (N && N->getNodeId() != -1)
315 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, "");
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000316 }
317 };
318}
319
320std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
321 const ScheduleDAG *G) {
322 std::string Op;
323
324 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
325 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
326 &G->DAG) + "\n";
327 }
328
Dan Gohman4145bd52008-03-21 22:51:06 +0000329 if (SU->Node)
330 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
331 else
332 Op += "<CROSS RC COPY>";
Dan Gohman3e1a7ae2007-08-28 20:32:58 +0000333
334 return Op;
335}
336
337
338/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
339/// rendered using 'dot'.
340///
341void ScheduleDAG::viewGraph() {
342// This code is only for debugging!
343#ifndef NDEBUG
344 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
345#else
346 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
347 << "systems with Graphviz or gv!\n";
348#endif // NDEBUG
349}