blob: 430c326ba5649d9d27d021695c8116281749ca84 [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 {
Jim Laskeyec204022006-10-02 12:26:53 +000030#ifndef NDEBUG
31 std::map<const SDNode *, std::string> DagNodeColor;
32#endif
Chris Lattnere0646b82005-01-10 23:26:00 +000033 template<>
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
35 static std::string getGraphName(const SelectionDAG *G) {
36 return G->getMachineFunction().getFunction()->getName();
37 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000038
39 static bool renderGraphFromBottomUp() {
40 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000041 }
Chris Lattner37345fe2005-10-01 00:17:07 +000042
43 static bool hasNodeAddressLabel(const SDNode *Node,
44 const SelectionDAG *Graph) {
45 return true;
46 }
Chris Lattnere0646b82005-01-10 23:26:00 +000047
Chris Lattnere9c44cd2005-01-11 00:34:33 +000048 static std::string getNodeLabel(const SDNode *Node,
49 const SelectionDAG *Graph);
Jim Laskeyec204022006-10-02 12:26:53 +000050 static std::string getNodeAttributes(const SDNode *N,
51 const SelectionDAG *Graph) {
52#ifndef NDEBUG
53 const std::string &Attrs = Graph->getGraphAttrs(N);
54 if (!Attrs.empty()) {
55 if (Attrs.find("shape=") == std::string::npos)
56 return std::string("shape=Mrecord,") + Attrs;
57 else
58 return Attrs;
59 }
60#endif
Chris Lattnere0646b82005-01-10 23:26:00 +000061 return "shape=Mrecord";
62 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000063
64 static void addCustomGraphFeatures(SelectionDAG *G,
65 GraphWriter<SelectionDAG*> &GW) {
66 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
67 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
68 }
Chris Lattnere0646b82005-01-10 23:26:00 +000069 };
70}
71
Chris Lattnere9c44cd2005-01-11 00:34:33 +000072std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
73 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000074 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000075
Chris Lattnerad95d6a2005-08-16 18:31:23 +000076 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
77 if (Node->getValueType(i) == MVT::Other)
78 Op += ":ch";
79 else
80 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
81
Chris Lattnere9c44cd2005-01-11 00:34:33 +000082 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
83 Op += ": " + utostr(CSDN->getValue());
84 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
85 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000086 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000087 dyn_cast<GlobalAddressSDNode>(Node)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +000088 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +000089 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +000090 if (offset > 0)
91 Op += "+" + itostr(offset);
92 else
93 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +000094 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000095 Op += " " + itostr(FIDN->getIndex());
96 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Evan Chengd6594ae2006-09-12 21:00:35 +000097 if (CP->isMachineConstantPoolEntry()) {
Chris Lattner52676512006-03-05 09:38:03 +000098 std::ostringstream SS;
Evan Chengd6594ae2006-09-12 21:00:35 +000099 CP->getMachineCPVal()->print(SS);
Chris Lattner52676512006-03-05 09:38:03 +0000100 Op += "<" + SS.str() + ">";
Evan Chengd6594ae2006-09-12 21:00:35 +0000101 } else {
102 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
103 Op += "<" + ftostr(CFP->getValue()) + ">";
104 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
105 Op += "<" + utostr(CI->getZExtValue()) + ">";
106 else {
107 std::ostringstream SS;
108 WriteAsOperand(SS, CP->getConstVal(), false);
109 Op += "<" + SS.str() + ">";
110 }
Chris Lattner52676512006-03-05 09:38:03 +0000111 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000112 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000113 Op = "BB: ";
114 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
115 if (LBB)
116 Op += LBB->getName();
117 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000118 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner44fa7642005-11-19 06:58:46 +0000119 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +0000120 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
121 } else {
122 Op += " #" + utostr(R->getReg());
123 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000124 } else if (const ExternalSymbolSDNode *ES =
125 dyn_cast<ExternalSymbolSDNode>(Node)) {
126 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +0000127 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
128 if (M->getValue())
129 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
130 else
131 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +0000132 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +0000133 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattner36ce6912005-11-29 06:21:05 +0000134 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
135 Op = Op + "\"" + N->getValue() + "\"";
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000136 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000137
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000138 return Op;
139}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000140
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000141
Chris Lattner66328482005-01-10 23:08:40 +0000142/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
143/// rendered using 'dot'.
144///
145void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000146// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000147#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000148 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
149#else
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000150 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
151 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000152#endif // NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000153}
Jim Laskeyec204022006-10-02 12:26:53 +0000154
155
156/// clearGraphAttrs - Clear all previously defined node graph attributes.
157/// Intended to be used from a debugging tool (eg. gdb).
158void SelectionDAG::clearGraphAttrs() {
159#ifndef NDEBUG
160 NodeGraphAttrs.clear();
161#else
162 std::cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
163 << " on systems with Graphviz or gv!\n";
164#endif
165}
166
167
168/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
169///
170void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
171#ifndef NDEBUG
172 NodeGraphAttrs[N] = Attrs;
173#else
174 std::cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
175 << " on systems with Graphviz or gv!\n";
176#endif
177}
178
179
180/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
181/// Used from getNodeAttributes.
182const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
183#ifndef NDEBUG
184 std::map<const SDNode *, std::string>::const_iterator I =
185 NodeGraphAttrs.find(N);
186
187 if (I != NodeGraphAttrs.end())
188 return I->second;
189 else
190 return "";
191#else
192 std::cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
193 << " on systems with Graphviz or gv!\n";
194 return std::string("");
195#endif
196}
197
198/// setGraphColor - Convenience for setting node color attribute.
199///
200void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
201#ifndef NDEBUG
202 NodeGraphAttrs[N] = std::string("color=") + Color;
203#else
204 std::cerr << "SelectionDAG::setGraphColor is only available in debug builds"
205 << " on systems with Graphviz or gv!\n";
206#endif
207}
208