1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ScheduleDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/GraphWriter.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Config/config.h"
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33 static std::string getGraphName(const SelectionDAG *G) {
34 return G->getMachineFunction().getFunction()->getName();
37 static bool renderGraphFromBottomUp() {
41 static bool hasNodeAddressLabel(const SDNode *Node,
42 const SelectionDAG *Graph) {
46 /// If you want to override the dot attributes printed for a particular
47 /// edge, override this method.
48 template<typename EdgeIter>
49 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
50 SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
51 MVT::ValueType VT = Op.getValueType();
53 return "color=red,style=bold";
54 else if (VT == MVT::Other)
55 return "color=blue,style=dashed";
60 static std::string getNodeLabel(const SDNode *Node,
61 const SelectionDAG *Graph);
62 static std::string getNodeAttributes(const SDNode *N,
63 const SelectionDAG *Graph) {
65 const std::string &Attrs = Graph->getGraphAttrs(N);
67 if (Attrs.find("shape=") == std::string::npos)
68 return std::string("shape=Mrecord,") + Attrs;
73 return "shape=Mrecord";
76 static void addCustomGraphFeatures(SelectionDAG *G,
77 GraphWriter<SelectionDAG*> &GW) {
78 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
80 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
85 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
86 const SelectionDAG *G) {
87 std::string Op = Node->getOperationName(G);
89 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
90 if (Node->getValueType(i) == MVT::Other)
93 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
95 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
96 Op += ": " + utostr(CSDN->getValue());
97 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
98 Op += ": " + ftostr(CSDN->getValueAPF());
99 } else if (const GlobalAddressSDNode *GADN =
100 dyn_cast<GlobalAddressSDNode>(Node)) {
101 int offset = GADN->getOffset();
102 Op += ": " + GADN->getGlobal()->getName();
104 Op += "+" + itostr(offset);
106 Op += itostr(offset);
107 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
108 Op += " " + itostr(FIDN->getIndex());
109 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
110 Op += " " + itostr(JTDN->getIndex());
111 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
112 if (CP->isMachineConstantPoolEntry()) {
113 std::ostringstream SS;
114 CP->getMachineCPVal()->print(SS);
115 Op += "<" + SS.str() + ">";
117 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
118 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
119 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
120 Op += "<" + utostr(CI->getZExtValue()) + ">";
122 std::ostringstream SS;
123 WriteAsOperand(SS, CP->getConstVal(), false);
124 Op += "<" + SS.str() + ">";
127 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
129 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
131 Op += LBB->getName();
132 //Op += " " + (const void*)BBDN->getBasicBlock();
133 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
134 if (G && R->getReg() != 0 &&
135 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
136 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
138 Op += " #" + utostr(R->getReg());
140 } else if (const ExternalSymbolSDNode *ES =
141 dyn_cast<ExternalSymbolSDNode>(Node)) {
142 Op += "'" + std::string(ES->getSymbol()) + "'";
143 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
145 Op += "<" + M->getValue()->getName() + ">";
148 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
149 if (M->MO.getValue())
150 Op += "<" + M->MO.getValue()->getName() + ":" + itostr(M->MO.getOffset()) + ">";
152 Op += "<null:" + itostr(M->MO.getOffset()) + ">";
153 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
154 Op = Op + " VT=" + MVT::getValueTypeString(N->getVT());
155 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
156 Op = Op + "\"" + N->getValue() + "\"";
157 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
159 switch (LD->getExtensionType()) {
160 default: doExt = false; break;
162 Op = Op + "<anyext ";
172 Op += MVT::getValueTypeString(LD->getMemoryVT()) + ">";
173 if (LD->isVolatile())
175 Op += LD->getIndexedModeName(LD->getAddressingMode());
176 if (LD->getAlignment() > 1)
177 Op += " A=" + utostr(LD->getAlignment());
178 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
179 if (ST->isTruncatingStore())
180 Op += "<trunc " + MVT::getValueTypeString(ST->getMemoryVT()) + ">";
181 if (ST->isVolatile())
183 Op += ST->getIndexedModeName(ST->getAddressingMode());
184 if (ST->getAlignment() > 1)
185 Op += " A=" + utostr(ST->getAlignment());
189 Op += " Id=" + itostr(Node->getNodeId());
196 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
197 /// rendered using 'dot'.
199 void SelectionDAG::viewGraph() {
200 // This code is only for debugging!
202 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
204 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
205 << "systems with Graphviz or gv!\n";
210 /// clearGraphAttrs - Clear all previously defined node graph attributes.
211 /// Intended to be used from a debugging tool (eg. gdb).
212 void SelectionDAG::clearGraphAttrs() {
214 NodeGraphAttrs.clear();
216 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
217 << " on systems with Graphviz or gv!\n";
222 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
224 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
226 NodeGraphAttrs[N] = Attrs;
228 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
229 << " on systems with Graphviz or gv!\n";
234 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
235 /// Used from getNodeAttributes.
236 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
238 std::map<const SDNode *, std::string>::const_iterator I =
239 NodeGraphAttrs.find(N);
241 if (I != NodeGraphAttrs.end())
246 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
247 << " on systems with Graphviz or gv!\n";
248 return std::string("");
252 /// setGraphColor - Convenience for setting node color attribute.
254 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
256 NodeGraphAttrs[N] = std::string("color=") + Color;
258 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
259 << " on systems with Graphviz or gv!\n";
265 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
266 static std::string getGraphName(const ScheduleDAG *G) {
267 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
270 static bool renderGraphFromBottomUp() {
274 static bool hasNodeAddressLabel(const SUnit *Node,
275 const ScheduleDAG *Graph) {
279 /// If you want to override the dot attributes printed for a particular
280 /// edge, override this method.
281 template<typename EdgeIter>
282 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
284 return "color=blue,style=dashed";
289 static std::string getNodeLabel(const SUnit *Node,
290 const ScheduleDAG *Graph);
291 static std::string getNodeAttributes(const SUnit *N,
292 const ScheduleDAG *Graph) {
293 return "shape=Mrecord";
296 static void addCustomGraphFeatures(ScheduleDAG *G,
297 GraphWriter<ScheduleDAG*> &GW) {
298 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
299 if (G->DAG.getRoot().Val)
300 GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val].front(), -1, "");
305 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
306 const ScheduleDAG *G) {
309 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
310 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
314 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
320 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
321 /// rendered using 'dot'.
323 void ScheduleDAG::viewGraph() {
324 // This code is only for debugging!
326 ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
328 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
329 << "systems with Graphviz or gv!\n";