blob: f68c227dc0ccfe7ef32f151b89f0c4d63120b67f [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Evan Chengd6594ae2006-09-12 21:00:35 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner66328482005-01-10 23:08:40 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner7228aa72005-08-19 21:21:16 +000020#include "llvm/Target/MRegisterInfo.h"
21#include "llvm/Target/TargetMachine.h"
Chris Lattner66328482005-01-10 23:08:40 +000022#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000024#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000025#include <fstream>
Chris Lattner52676512006-03-05 09:38:03 +000026#include <sstream>
Chris Lattner66328482005-01-10 23:08:40 +000027using namespace llvm;
28
Chris Lattnere0646b82005-01-10 23:26:00 +000029namespace llvm {
30 template<>
31 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
32 static std::string getGraphName(const SelectionDAG *G) {
33 return G->getMachineFunction().getFunction()->getName();
34 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000035
36 static bool renderGraphFromBottomUp() {
37 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000038 }
Chris Lattner37345fe2005-10-01 00:17:07 +000039
40 static bool hasNodeAddressLabel(const SDNode *Node,
41 const SelectionDAG *Graph) {
42 return true;
43 }
Chris Lattnere0646b82005-01-10 23:26:00 +000044
Chris Lattnere9c44cd2005-01-11 00:34:33 +000045 static std::string getNodeLabel(const SDNode *Node,
46 const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +000047 static std::string getNodeAttributes(const SDNode *N,
48 const SelectionDAG *Graph) {
49#ifndef NDEBUG
50 const std::string &Attrs = Graph->getGraphAttrs(N);
51 if (!Attrs.empty()) {
52 if (Attrs.find("shape=") == std::string::npos)
53 return std::string("shape=Mrecord,") + Attrs;
54 else
55 return Attrs;
56 }
57#endif
Chris Lattnere0646b82005-01-10 23:26:00 +000058 return "shape=Mrecord";
59 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000060
61 static void addCustomGraphFeatures(SelectionDAG *G,
62 GraphWriter<SelectionDAG*> &GW) {
63 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Jim Laskey26f7fa72006-10-17 19:33:52 +000064 if (G->getRoot().Val)
65 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000066 }
Chris Lattnere0646b82005-01-10 23:26:00 +000067 };
68}
69
Chris Lattnere9c44cd2005-01-11 00:34:33 +000070std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
71 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000072 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000073
Chris Lattnerad95d6a2005-08-16 18:31:23 +000074 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
75 if (Node->getValueType(i) == MVT::Other)
76 Op += ":ch";
77 else
78 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
79
Chris Lattnere9c44cd2005-01-11 00:34:33 +000080 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
81 Op += ": " + utostr(CSDN->getValue());
82 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
83 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000084 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000085 dyn_cast<GlobalAddressSDNode>(Node)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +000086 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +000087 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +000088 if (offset > 0)
89 Op += "+" + itostr(offset);
90 else
91 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +000092 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000093 Op += " " + itostr(FIDN->getIndex());
94 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Evan Chengd6594ae2006-09-12 21:00:35 +000095 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner52676512006-03-05 09:38:03 +000096 std::ostringstream SS;
Evan Chengd6594ae2006-09-12 21:00:35 +000097 CP->getMachineCPVal()->print(SS);
Chris Lattner52676512006-03-05 09:38:03 +000098 Op += "<" + SS.str() + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +000099 } else {
100 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
101 Op += "<" + ftostr(CFP->getValue()) + ">";
102 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
103 Op += "<" + utostr(CI->getZExtValue()) + ">";
104 else {
105 std::ostringstream SS;
106 WriteAsOperand(SS, CP->getConstVal(), false);
107 Op += "<" + SS.str() + ">";
108 }
Chris Lattner52676512006-03-05 09:38:03 +0000109 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000110 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000111 Op = "BB: ";
112 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
113 if (LBB)
114 Op += LBB->getName();
115 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000116 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner44fa7642005-11-19 06:58:46 +0000117 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +0000118 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
119 } else {
120 Op += " #" + utostr(R->getReg());
121 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000122 } else if (const ExternalSymbolSDNode *ES =
123 dyn_cast<ExternalSymbolSDNode>(Node)) {
124 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000125 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
126 if (M->getValue())
127 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
128 else
129 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +0000130 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +0000131 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattner36ce6912005-11-29 06:21:05 +0000132 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
133 Op = Op + "\"" + N->getValue() + "\"";
Evan Cheng45aeccc2006-10-10 20:11:26 +0000134 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
135 bool doExt = true;
136 switch (LD->getExtensionType()) {
137 default: doExt = false; break;
138 case ISD::EXTLOAD:
139 Op = Op + "<anyext ";
140 break;
141 case ISD::SEXTLOAD:
142 Op = Op + " <sext ";
143 break;
144 case ISD::ZEXTLOAD:
145 Op = Op + " <zext ";
146 break;
147 }
148 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +0000149 Op = Op + MVT::getValueTypeString(LD->getLoadedVT()) + ">";
Evan Cheng45aeccc2006-10-10 20:11:26 +0000150
Evan Cheng649b7ef2006-10-17 21:18:26 +0000151 Op += LD->getAddressingModeName(LD->getAddressingMode());
152 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
153 if (ST->isTruncatingStore())
154 Op = Op + "<trunc " + MVT::getValueTypeString(ST->getStoredVT()) + ">";
155 Op += ST->getAddressingModeName(ST->getAddressingMode());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000156 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000157
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000158 return Op;
159}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000160
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000161
Chris Lattner66328482005-01-10 23:08:40 +0000162/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
163/// rendered using 'dot'.
164///
165void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000166// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000167#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000168 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
169#else
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000170 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
171 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000172#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000173}
Jim Laskeyec204022006-10-02 12:26:53 +0000174
175
176/// clearGraphAttrs - Clear all previously defined node graph attributes.
177/// Intended to be used from a debugging tool (eg. gdb).
178void SelectionDAG::clearGraphAttrs() {
179#ifndef NDEBUG
180 NodeGraphAttrs.clear();
181#else
182 std::cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
183 << " on systems with Graphviz or gv!\n";
184#endif
185}
186
187
188/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
189///
190void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
191#ifndef NDEBUG
192 NodeGraphAttrs[N] = Attrs;
193#else
194 std::cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
195 << " on systems with Graphviz or gv!\n";
196#endif
197}
198
199
200/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
201/// Used from getNodeAttributes.
202const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
203#ifndef NDEBUG
204 std::map<const SDNode *, std::string>::const_iterator I =
205 NodeGraphAttrs.find(N);
206
207 if (I != NodeGraphAttrs.end())
208 return I->second;
209 else
210 return "";
211#else
212 std::cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
213 << " on systems with Graphviz or gv!\n";
214 return std::string("");
215#endif
216}
217
218/// setGraphColor - Convenience for setting node color attribute.
219///
220void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
221#ifndef NDEBUG
222 NodeGraphAttrs[N] = std::string("color=") + Color;
223#else
224 std::cerr << "SelectionDAG::setGraphColor is only available in debug builds"
225 << " on systems with Graphviz or gv!\n";
226#endif
227}
228