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 /// 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();
52 return "color=red,style=bold";
53 else if (VT == MVT::Other)
54 return "style=dashed";
59 static std::string getNodeLabel(const SDNode *Node,
60 const SelectionDAG *Graph);
61 static std::string getNodeAttributes(const SDNode *N,
62 const SelectionDAG *Graph) {
64 const std::string &Attrs = Graph->getGraphAttrs(N);
66 if (Attrs.find("shape=") == std::string::npos)
67 return std::string("shape=Mrecord,") + Attrs;
72 return "shape=Mrecord";
75 static void addCustomGraphFeatures(SelectionDAG *G,
76 GraphWriter<SelectionDAG*> &GW) {
77 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
79 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
84 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
85 const SelectionDAG *G) {
86 std::string Op = Node->getOperationName(G);
88 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
89 if (Node->getValueType(i) == MVT::Other)
92 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
94 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());
98 } else if (const GlobalAddressSDNode *GADN =
99 dyn_cast<GlobalAddressSDNode>(Node)) {
100 int offset = GADN->getOffset();
101 Op += ": " + GADN->getGlobal()->getName();
103 Op += "+" + itostr(offset);
105 Op += itostr(offset);
106 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
107 Op += " " + itostr(FIDN->getIndex());
108 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
109 Op += " " + itostr(JTDN->getIndex());
110 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
111 if (CP->isMachineConstantPoolEntry()) {
112 std::ostringstream SS;
113 CP->getMachineCPVal()->print(SS);
114 Op += "<" + SS.str() + ">";
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()) + ">";
121 std::ostringstream SS;
122 WriteAsOperand(SS, CP->getConstVal(), false);
123 Op += "<" + SS.str() + ">";
126 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
128 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
130 Op += LBB->getName();
131 //Op += " " + (const void*)BBDN->getBasicBlock();
132 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
133 if (G && R->getReg() != 0 &&
134 MRegisterInfo::isPhysicalRegister(R->getReg())) {
135 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
137 Op += " #" + utostr(R->getReg());
139 } else if (const ExternalSymbolSDNode *ES =
140 dyn_cast<ExternalSymbolSDNode>(Node)) {
141 Op += "'" + std::string(ES->getSymbol()) + "'";
142 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
144 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
146 Op += "<null:" + itostr(M->getOffset()) + ">";
147 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
148 Op = Op + " VT=" + getValueTypeString(N->getVT());
149 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
150 Op = Op + "\"" + N->getValue() + "\"";
151 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
153 switch (LD->getExtensionType()) {
154 default: doExt = false; break;
156 Op = Op + "<anyext ";
166 Op = Op + MVT::getValueTypeString(LD->getLoadedVT()) + ">";
168 Op += LD->getIndexedModeName(LD->getAddressingMode());
169 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
170 if (ST->isTruncatingStore())
171 Op = Op + "<trunc " + MVT::getValueTypeString(ST->getStoredVT()) + ">";
172 Op += ST->getIndexedModeName(ST->getAddressingMode());
179 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
180 /// rendered using 'dot'.
182 void SelectionDAG::viewGraph() {
183 // This code is only for debugging!
185 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
187 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
188 << "systems with Graphviz or gv!\n";
193 /// clearGraphAttrs - Clear all previously defined node graph attributes.
194 /// Intended to be used from a debugging tool (eg. gdb).
195 void SelectionDAG::clearGraphAttrs() {
197 NodeGraphAttrs.clear();
199 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
200 << " on systems with Graphviz or gv!\n";
205 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
207 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
209 NodeGraphAttrs[N] = Attrs;
211 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
212 << " on systems with Graphviz or gv!\n";
217 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
218 /// Used from getNodeAttributes.
219 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
221 std::map<const SDNode *, std::string>::const_iterator I =
222 NodeGraphAttrs.find(N);
224 if (I != NodeGraphAttrs.end())
229 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
230 << " on systems with Graphviz or gv!\n";
231 return std::string("");
235 /// setGraphColor - Convenience for setting node color attribute.
237 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
239 NodeGraphAttrs[N] = std::string("color=") + Color;
241 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
242 << " on systems with Graphviz or gv!\n";