1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG::viewGraph method.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Constants.h"
15 #include "llvm/Function.h"
16 #include "llvm/Assembly/Writer.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/Target/MRegisterInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/GraphWriter.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Config/config.h"
31 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
32 static std::string getGraphName(const SelectionDAG *G) {
33 return G->getMachineFunction().getFunction()->getName();
36 static bool renderGraphFromBottomUp() {
40 static bool hasNodeAddressLabel(const SDNode *Node,
41 const SelectionDAG *Graph) {
45 static std::string getNodeLabel(const SDNode *Node,
46 const SelectionDAG *Graph);
47 static std::string getNodeAttributes(const SDNode *N,
48 const SelectionDAG *Graph) {
50 const std::string &Attrs = Graph->getGraphAttrs(N);
52 if (Attrs.find("shape=") == std::string::npos)
53 return std::string("shape=Mrecord,") + Attrs;
58 return "shape=Mrecord";
61 static void addCustomGraphFeatures(SelectionDAG *G,
62 GraphWriter<SelectionDAG*> &GW) {
63 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
64 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
69 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
70 const SelectionDAG *G) {
71 std::string Op = Node->getOperationName(G);
73 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
74 if (Node->getValueType(i) == MVT::Other)
77 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
79 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
80 Op += ": " + utostr(CSDN->getValue());
81 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
82 Op += ": " + ftostr(CSDN->getValue());
83 } else if (const GlobalAddressSDNode *GADN =
84 dyn_cast<GlobalAddressSDNode>(Node)) {
85 int offset = GADN->getOffset();
86 Op += ": " + GADN->getGlobal()->getName();
88 Op += "+" + itostr(offset);
91 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
92 Op += " " + itostr(FIDN->getIndex());
93 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
94 if (CP->isMachineConstantPoolEntry()) {
95 std::ostringstream SS;
96 CP->getMachineCPVal()->print(SS);
97 Op += "<" + SS.str() + ">";
99 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
100 Op += "<" + ftostr(CFP->getValue()) + ">";
101 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
102 Op += "<" + utostr(CI->getZExtValue()) + ">";
104 std::ostringstream SS;
105 WriteAsOperand(SS, CP->getConstVal(), false);
106 Op += "<" + SS.str() + ">";
109 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
111 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
113 Op += LBB->getName();
114 //Op += " " + (const void*)BBDN->getBasicBlock();
115 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
116 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
117 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
119 Op += " #" + utostr(R->getReg());
121 } else if (const ExternalSymbolSDNode *ES =
122 dyn_cast<ExternalSymbolSDNode>(Node)) {
123 Op += "'" + std::string(ES->getSymbol()) + "'";
124 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
126 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
128 Op += "<null:" + itostr(M->getOffset()) + ">";
129 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
130 Op = Op + " VT=" + getValueTypeString(N->getVT());
131 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
132 Op = Op + "\"" + N->getValue() + "\"";
133 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
135 switch (LD->getExtensionType()) {
136 default: doExt = false; break;
138 Op = Op + "<anyext ";
148 Op = Op + MVT::getValueTypeString(LD->getLoadVT()) + ">";
150 if (LD->getAddressingMode() == ISD::PRE_INDEXED)
152 else if (LD->getAddressingMode() == ISD::POST_INDEXED)
160 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
161 /// rendered using 'dot'.
163 void SelectionDAG::viewGraph() {
164 // This code is only for debugging!
166 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
168 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
169 << "systems with Graphviz or gv!\n";
174 /// clearGraphAttrs - Clear all previously defined node graph attributes.
175 /// Intended to be used from a debugging tool (eg. gdb).
176 void SelectionDAG::clearGraphAttrs() {
178 NodeGraphAttrs.clear();
180 std::cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
181 << " on systems with Graphviz or gv!\n";
186 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
188 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
190 NodeGraphAttrs[N] = Attrs;
192 std::cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
193 << " on systems with Graphviz or gv!\n";
198 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
199 /// Used from getNodeAttributes.
200 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
202 std::map<const SDNode *, std::string>::const_iterator I =
203 NodeGraphAttrs.find(N);
205 if (I != NodeGraphAttrs.end())
210 std::cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
211 << " on systems with Graphviz or gv!\n";
212 return std::string("");
216 /// setGraphColor - Convenience for setting node color attribute.
218 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
220 NodeGraphAttrs[N] = std::string("color=") + Color;
222 std::cerr << "SelectionDAG::setGraphColor is only available in debug builds"
223 << " on systems with Graphviz or gv!\n";