Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 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 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | fc08d9c | 2005-01-10 23:52:04 +0000 | [diff] [blame^] | 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 | } |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 41 | }; |
| 42 | } |
| 43 | |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 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 | } |