db094a0a1201097faf595352b39620ee22991a03
[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 }
37
38 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
39 /// rendered using 'dot'.
40 ///
41 void SelectionDAG::viewGraph() {
42   std::string Filename = "/tmp/dag." +
43     getMachineFunction().getFunction()->getName() + ".dot";
44   std::cerr << "Writing '" << Filename << "'... ";
45   std::ofstream F(Filename.c_str());
46
47   if (!F) {
48     std::cerr << "  error opening file for writing!\n";
49     return;
50   }
51
52   WriteGraph(F, this);
53   F.close();
54   std::cerr << "\n";
55
56   std::cerr << "Running 'dot' program... " << std::flush;
57   if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
58               + " > /tmp/dag.tempgraph.ps").c_str())) {
59     std::cerr << "Error running dot: 'dot' not in path?\n";
60   } else {
61     std::cerr << "\n";
62     system("gv /tmp/dag.tempgraph.ps");
63   }
64   system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
65 }