blob: f342f04abc9a45cc92c5f0bbe061919f41b8f2d5 [file] [log] [blame]
Chris Lattner66328482005-01-10 23:08:40 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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();
50 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
51 Op += ": " + utostr(CSDN->getValue());
52 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
53 Op += ": " + ftostr(CSDN->getValue());
54 } else if (const GlobalAddressSDNode *GADN =
55 dyn_cast<GlobalAddressSDNode>(Node)) {
56 Op += ": " + GADN->getGlobal()->getName();
57 } else if (const FrameIndexSDNode *FIDN =
58 dyn_cast<FrameIndexSDNode>(Node)) {
59 Op += " " + itostr(FIDN->getIndex());
60 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
61 Op += "<" + utostr(CP->getIndex()) + ">";
62 } else if (const BasicBlockSDNode *BBDN =
63 dyn_cast<BasicBlockSDNode>(Node)) {
64 Op = "BB: ";
65 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
66 if (LBB)
67 Op += LBB->getName();
68 //Op += " " + (const void*)BBDN->getBasicBlock();
69 } else if (const CopyRegSDNode *C2V = dyn_cast<CopyRegSDNode>(Node)) {
70 Op += " #" + utostr(C2V->getReg());
71 } else if (const ExternalSymbolSDNode *ES =
72 dyn_cast<ExternalSymbolSDNode>(Node)) {
73 Op += "'" + std::string(ES->getSymbol()) + "'";
74 }
75 return Op;
76}
77
78
Chris Lattner66328482005-01-10 23:08:40 +000079/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
80/// rendered using 'dot'.
81///
82void SelectionDAG::viewGraph() {
83 std::string Filename = "/tmp/dag." +
84 getMachineFunction().getFunction()->getName() + ".dot";
85 std::cerr << "Writing '" << Filename << "'... ";
86 std::ofstream F(Filename.c_str());
87
88 if (!F) {
89 std::cerr << " error opening file for writing!\n";
90 return;
91 }
92
93 WriteGraph(F, this);
94 F.close();
95 std::cerr << "\n";
96
97 std::cerr << "Running 'dot' program... " << std::flush;
98 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
99 + " > /tmp/dag.tempgraph.ps").c_str())) {
100 std::cerr << "Error running dot: 'dot' not in path?\n";
101 } else {
102 std::cerr << "\n";
103 system("gv /tmp/dag.tempgraph.ps");
104 }
105 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
106}