blob: 12b568262483e04a5537f1f13f1351af5b76d129 [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 Lattner34ab4d42006-10-20 18:06:09 +000044
45 /// If you want to override the dot attributes printed for a particular
46 /// edge, override this method.
47 template<typename EdgeIter>
48 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
49 SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
50 MVT::ValueType VT = Op.getValueType();
51 if (VT == MVT::Flag)
52 return "color=red,style=bold";
53 else if (VT == MVT::Other)
Dan Gohman7a0a4fc2007-06-18 15:30:16 +000054 return "color=blue,style=dashed";
Chris Lattner34ab4d42006-10-20 18:06:09 +000055 return "";
56 }
57
Chris Lattnere0646b82005-01-10 23:26:00 +000058
Chris Lattnere9c44cd2005-01-11 00:34:33 +000059 static std::string getNodeLabel(const SDNode *Node,
60 const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +000061 static std::string getNodeAttributes(const SDNode *N,
62 const SelectionDAG *Graph) {
63#ifndef NDEBUG
64 const std::string &Attrs = Graph->getGraphAttrs(N);
65 if (!Attrs.empty()) {
66 if (Attrs.find("shape=") == std::string::npos)
67 return std::string("shape=Mrecord,") + Attrs;
68 else
69 return Attrs;
70 }
71#endif
Chris Lattnere0646b82005-01-10 23:26:00 +000072 return "shape=Mrecord";
73 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000074
75 static void addCustomGraphFeatures(SelectionDAG *G,
76 GraphWriter<SelectionDAG*> &GW) {
77 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
Jim Laskey26f7fa72006-10-17 19:33:52 +000078 if (G->getRoot().Val)
79 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000080 }
Chris Lattnere0646b82005-01-10 23:26:00 +000081 };
82}
83
Chris Lattnere9c44cd2005-01-11 00:34:33 +000084std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
85 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000086 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000087
Chris Lattnerad95d6a2005-08-16 18:31:23 +000088 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
89 if (Node->getValueType(i) == MVT::Other)
90 Op += ":ch";
91 else
92 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
93
Chris Lattnere9c44cd2005-01-11 00:34:33 +000094 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
95 Op += ": " + utostr(CSDN->getValue());
96 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
97 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000098 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000099 dyn_cast<GlobalAddressSDNode>(Node)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000100 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000101 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +0000102 if (offset > 0)
103 Op += "+" + itostr(offset);
104 else
105 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000106 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000107 Op += " " + itostr(FIDN->getIndex());
Evan Cheng6cc31ae2006-11-01 04:48:30 +0000108 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
109 Op += " " + itostr(JTDN->getIndex());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000110 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Evan Chengd6594ae2006-09-12 21:00:35 +0000111 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner52676512006-03-05 09:38:03 +0000112 std::ostringstream SS;
Evan Chengd6594ae2006-09-12 21:00:35 +0000113 CP->getMachineCPVal()->print(SS);
Chris Lattner52676512006-03-05 09:38:03 +0000114 Op += "<" + SS.str() + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +0000115 } else {
116 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
117 Op += "<" + ftostr(CFP->getValue()) + ">";
118 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
119 Op += "<" + utostr(CI->getZExtValue()) + ">";
120 else {
121 std::ostringstream SS;
122 WriteAsOperand(SS, CP->getConstVal(), false);
123 Op += "<" + SS.str() + ">";
124 }
Chris Lattner52676512006-03-05 09:38:03 +0000125 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000126 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000127 Op = "BB: ";
128 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
129 if (LBB)
130 Op += LBB->getName();
131 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000132 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner34ab4d42006-10-20 18:06:09 +0000133 if (G && R->getReg() != 0 &&
134 MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +0000135 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
136 } else {
137 Op += " #" + utostr(R->getReg());
138 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000139 } else if (const ExternalSymbolSDNode *ES =
140 dyn_cast<ExternalSymbolSDNode>(Node)) {
141 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000142 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
143 if (M->getValue())
144 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
145 else
146 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +0000147 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Dan Gohmanb55757e2007-05-18 17:52:13 +0000148 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
Chris Lattner36ce6912005-11-29 06:21:05 +0000149 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
150 Op = Op + "\"" + N->getValue() + "\"";
Evan Cheng45aeccc2006-10-10 20:11:26 +0000151 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
152 bool doExt = true;
153 switch (LD->getExtensionType()) {
154 default: doExt = false; break;
155 case ISD::EXTLOAD:
156 Op = Op + "<anyext ";
157 break;
158 case ISD::SEXTLOAD:
159 Op = Op + " <sext ";
160 break;
161 case ISD::ZEXTLOAD:
162 Op = Op + " <zext ";
163 break;
164 }
165 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +0000166 Op = Op + MVT::getValueTypeString(LD->getLoadedVT()) + ">";
Evan Cheng45aeccc2006-10-10 20:11:26 +0000167
Evan Cheng00305822006-11-09 18:44:21 +0000168 Op += LD->getIndexedModeName(LD->getAddressingMode());
Evan Cheng649b7ef2006-10-17 21:18:26 +0000169 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
170 if (ST->isTruncatingStore())
171 Op = Op + "<trunc " + MVT::getValueTypeString(ST->getStoredVT()) + ">";
Evan Cheng00305822006-11-09 18:44:21 +0000172 Op += ST->getIndexedModeName(ST->getAddressingMode());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000173 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000174
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000175 return Op;
176}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000177
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000178
Chris Lattner66328482005-01-10 23:08:40 +0000179/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
180/// rendered using 'dot'.
181///
182void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000183// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000184#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000185 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
186#else
Bill Wendling832171c2006-12-07 20:04:42 +0000187 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
188 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000189#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000190}
Jim Laskeyec204022006-10-02 12:26:53 +0000191
192
193/// clearGraphAttrs - Clear all previously defined node graph attributes.
194/// Intended to be used from a debugging tool (eg. gdb).
195void SelectionDAG::clearGraphAttrs() {
196#ifndef NDEBUG
197 NodeGraphAttrs.clear();
198#else
Bill Wendling832171c2006-12-07 20:04:42 +0000199 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
200 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000201#endif
202}
203
204
205/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
206///
207void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
208#ifndef NDEBUG
209 NodeGraphAttrs[N] = Attrs;
210#else
Bill Wendling832171c2006-12-07 20:04:42 +0000211 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
212 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000213#endif
214}
215
216
217/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
218/// Used from getNodeAttributes.
219const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
220#ifndef NDEBUG
221 std::map<const SDNode *, std::string>::const_iterator I =
222 NodeGraphAttrs.find(N);
223
224 if (I != NodeGraphAttrs.end())
225 return I->second;
226 else
227 return "";
228#else
Bill Wendling832171c2006-12-07 20:04:42 +0000229 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
230 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000231 return std::string("");
232#endif
233}
234
235/// setGraphColor - Convenience for setting node color attribute.
236///
237void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
238#ifndef NDEBUG
239 NodeGraphAttrs[N] = std::string("color=") + Color;
240#else
Bill Wendling832171c2006-12-07 20:04:42 +0000241 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
242 << " on systems with Graphviz or gv!\n";
Jim Laskeyec204022006-10-02 12:26:53 +0000243#endif
244}
245