blob: e3304f44beee01d292f82173b956a07cb3832850 [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
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/Function.h"
17#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner66328482005-01-10 23:08:40 +000019#include <fstream>
20using namespace llvm;
21
Chris Lattnere0646b82005-01-10 23:26:00 +000022namespace llvm {
23 template<>
24 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
25 static std::string getGraphName(const SelectionDAG *G) {
26 return G->getMachineFunction().getFunction()->getName();
27 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000028
29 static bool renderGraphFromBottomUp() {
30 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000031 }
32
Chris Lattnere9c44cd2005-01-11 00:34:33 +000033 static std::string getNodeLabel(const SDNode *Node,
34 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000035 static std::string getNodeAttributes(const SDNode *N) {
36 return "shape=Mrecord";
37 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000038
39 static void addCustomGraphFeatures(SelectionDAG *G,
40 GraphWriter<SelectionDAG*> &GW) {
41 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
42 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
43 }
Chris Lattnere0646b82005-01-10 23:26:00 +000044 };
45}
46
Chris Lattnere9c44cd2005-01-11 00:34:33 +000047std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
48 const SelectionDAG *G) {
49 std::string Op = Node->getOperationName();
Chris Lattnerc871e1d2005-01-11 22:21:04 +000050
51 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
52 switch (Node->getValueType(i)) {
53 default: Op += ":unknownvt!"; break;
54 case MVT::Other: Op += ":ch"; break;
55 case MVT::i1: Op += ":i1"; break;
56 case MVT::i8: Op += ":i8"; break;
57 case MVT::i16: Op += ":i16"; break;
58 case MVT::i32: Op += ":i32"; break;
59 case MVT::i64: Op += ":i64"; break;
60 case MVT::i128: Op += ":i128"; break;
61 case MVT::f32: Op += ":f32"; break;
62 case MVT::f64: Op += ":f64"; break;
63 case MVT::f80: Op += ":f80"; break;
64 case MVT::f128: Op += ":f128"; break;
65 case MVT::isVoid: Op += ":void"; break;
66 }
67 }
68
Chris Lattnere9c44cd2005-01-11 00:34:33 +000069 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
70 Op += ": " + utostr(CSDN->getValue());
71 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
72 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000073 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000074 dyn_cast<GlobalAddressSDNode>(Node)) {
75 Op += ": " + GADN->getGlobal()->getName();
76 } else if (const FrameIndexSDNode *FIDN =
77 dyn_cast<FrameIndexSDNode>(Node)) {
78 Op += " " + itostr(FIDN->getIndex());
79 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
80 Op += "<" + utostr(CP->getIndex()) + ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +000081 } else if (const BasicBlockSDNode *BBDN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000082 dyn_cast<BasicBlockSDNode>(Node)) {
83 Op = "BB: ";
84 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
85 if (LBB)
86 Op += LBB->getName();
87 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattner18c2f132005-01-13 20:50:02 +000088 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000089 Op += " #" + utostr(C2V->getReg());
90 } else if (const ExternalSymbolSDNode *ES =
91 dyn_cast<ExternalSymbolSDNode>(Node)) {
92 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +000093 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(Node)) {
Chris Lattner4b789932005-01-16 07:28:31 +000094 Op = Op + " ty=" + MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnere9c44cd2005-01-11 00:34:33 +000095 }
96 return Op;
97}
Misha Brukmanedf128a2005-04-21 22:36:52 +000098
Chris Lattnere9c44cd2005-01-11 00:34:33 +000099
Chris Lattner66328482005-01-10 23:08:40 +0000100/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
101/// rendered using 'dot'.
102///
103void SelectionDAG::viewGraph() {
104 std::string Filename = "/tmp/dag." +
105 getMachineFunction().getFunction()->getName() + ".dot";
106 std::cerr << "Writing '" << Filename << "'... ";
107 std::ofstream F(Filename.c_str());
108
109 if (!F) {
110 std::cerr << " error opening file for writing!\n";
111 return;
112 }
113
114 WriteGraph(F, this);
115 F.close();
116 std::cerr << "\n";
117
118 std::cerr << "Running 'dot' program... " << std::flush;
119 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
120 + " > /tmp/dag.tempgraph.ps").c_str())) {
121 std::cerr << "Error running dot: 'dot' not in path?\n";
122 } else {
123 std::cerr << "\n";
124 system("gv /tmp/dag.tempgraph.ps");
125 }
126 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
127}