blob: 4a70642c4b56b39ea77953ce66d324bdf420ed90 [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"
18#include <fstream>
19using namespace llvm;
20
Chris Lattnere0646b82005-01-10 23:26:00 +000021namespace 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 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000035
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 }
Chris Lattnere0646b82005-01-10 23:26:00 +000041 };
42}
43
Chris Lattner66328482005-01-10 23:08:40 +000044/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
45/// rendered using 'dot'.
46///
47void 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}