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/CodeGen/SelectionDAG.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/GraphWriter.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Config/config.h"
28 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
29 static std::string getGraphName(const SelectionDAG *G) {
30 return G->getMachineFunction().getFunction()->getName();
33 static bool renderGraphFromBottomUp() {
37 static bool hasNodeAddressLabel(const SDNode *Node,
38 const SelectionDAG *Graph) {
42 static std::string getNodeLabel(const SDNode *Node,
43 const SelectionDAG *Graph);
44 static std::string getNodeAttributes(const SDNode *N) {
45 return "shape=Mrecord";
48 static void addCustomGraphFeatures(SelectionDAG *G,
49 GraphWriter<SelectionDAG*> &GW) {
50 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
51 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
56 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
57 const SelectionDAG *G) {
58 std::string Op = Node->getOperationName(G);
60 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
61 if (Node->getValueType(i) == MVT::Other)
64 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
66 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
67 Op += ": " + utostr(CSDN->getValue());
68 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
69 Op += ": " + ftostr(CSDN->getValue());
70 } else if (const GlobalAddressSDNode *GADN =
71 dyn_cast<GlobalAddressSDNode>(Node)) {
72 Op += ": " + GADN->getGlobal()->getName();
73 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
74 Op += " " + itostr(FIDN->getIndex());
75 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
76 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
77 Op += "<" + ftostr(CFP->getValue()) + ">";
78 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
80 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
83 //Op += " " + (const void*)BBDN->getBasicBlock();
84 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
85 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
86 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
88 Op += " #" + utostr(R->getReg());
90 } else if (const ExternalSymbolSDNode *ES =
91 dyn_cast<ExternalSymbolSDNode>(Node)) {
92 Op += "'" + std::string(ES->getSymbol()) + "'";
93 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
95 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
97 Op += "<null:" + itostr(M->getOffset()) + ">";
98 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
99 Op = Op + " VT=" + getValueTypeString(N->getVT());
105 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
106 /// rendered using 'dot'.
108 void SelectionDAG::viewGraph() {
109 // This code is only for debugging!
111 std::string Filename = "/tmp/dag." +
112 getMachineFunction().getFunction()->getName() + ".dot";
113 std::cerr << "Writing '" << Filename << "'... ";
114 std::ofstream F(Filename.c_str());
117 std::cerr << " error opening file for writing!\n";
126 std::cerr << "Running 'Graphviz' program... " << std::flush;
127 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
128 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
130 system(("rm " + Filename).c_str());
133 #endif // HAVE_GRAPHVIZ
136 std::cerr << "Running 'dot' program... " << std::flush;
137 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
138 + " > /tmp/dag.tempgraph.ps").c_str())) {
139 std::cerr << "Error viewing graph: 'dot' not in path?\n";
142 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
144 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
148 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
149 << "systems with Graphviz or gv!\n";
152 system(("rm " + Filename).c_str());