4a70642c4b56b39ea77953ce66d324bdf420ed90
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGPrinter.cpp
1 //===-- 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"
18 #include <fstream>
19 using namespace llvm;
20
21 namespace llvm {
22   template<>
23   struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
24     static std::string getGraphName(const SelectionDAG *G) {
25       return G->getMachineFunction().getFunction()->getName();
26     }
27     static std::string getNodeLabel(const SDNode *Node,
28                                     const SelectionDAG *Graph) {
29       return Node->getOperationName();
30     }
31
32     static std::string getNodeAttributes(const SDNode *N) {
33       return "shape=Mrecord";
34     }
35
36     static void addCustomGraphFeatures(SelectionDAG *G,
37                                        GraphWriter<SelectionDAG*> &GW) {
38       GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
39       GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
40     }
41   };
42 }
43
44 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
45 /// rendered using 'dot'.
46 ///
47 void SelectionDAG::viewGraph() {
48   std::string Filename = "/tmp/dag." +
49     getMachineFunction().getFunction()->getName() + ".dot";
50   std::cerr << "Writing '" << Filename << "'... ";
51   std::ofstream F(Filename.c_str());
52
53   if (!F) {
54     std::cerr << "  error opening file for writing!\n";
55     return;
56   }
57
58   WriteGraph(F, this);
59   F.close();
60   std::cerr << "\n";
61
62   std::cerr << "Running 'dot' program... " << std::flush;
63   if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
64               + " > /tmp/dag.tempgraph.ps").c_str())) {
65     std::cerr << "Error running dot: 'dot' not in path?\n";
66   } else {
67     std::cerr << "\n";
68     system("gv /tmp/dag.tempgraph.ps");
69   }
70   system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
71 }