blob: a60acc12080ba3af458dad47b2c3107432b4b4f3 [file] [log] [blame]
Chris Lattner66328482005-01-10 23:08:40 +00001//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner66328482005-01-10 23:08:40 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner66328482005-01-10 23:08:40 +00008//===----------------------------------------------------------------------===//
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 Lattner6e741f82005-07-15 22:48:31 +000019#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000020#include <fstream>
21using namespace llvm;
22
Chris Lattnere0646b82005-01-10 23:26:00 +000023namespace llvm {
24 template<>
25 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
26 static std::string getGraphName(const SelectionDAG *G) {
27 return G->getMachineFunction().getFunction()->getName();
28 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000029
30 static bool renderGraphFromBottomUp() {
31 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000032 }
33
Chris Lattnere9c44cd2005-01-11 00:34:33 +000034 static std::string getNodeLabel(const SDNode *Node,
35 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000036 static std::string getNodeAttributes(const SDNode *N) {
37 return "shape=Mrecord";
38 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000039
40 static void addCustomGraphFeatures(SelectionDAG *G,
41 GraphWriter<SelectionDAG*> &GW) {
42 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
43 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
44 }
Chris Lattnere0646b82005-01-10 23:26:00 +000045 };
46}
47
Chris Lattnere9c44cd2005-01-11 00:34:33 +000048std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
49 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000050 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000051
Chris Lattnerad95d6a2005-08-16 18:31:23 +000052 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
53 if (Node->getValueType(i) == MVT::Other)
54 Op += ":ch";
55 else
56 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
57
Chris Lattnere9c44cd2005-01-11 00:34:33 +000058 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
59 Op += ": " + utostr(CSDN->getValue());
60 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
61 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000062 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000063 dyn_cast<GlobalAddressSDNode>(Node)) {
64 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000065 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000066 Op += " " + itostr(FIDN->getIndex());
67 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
68 Op += "<" + utostr(CP->getIndex()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000069 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000070 Op = "BB: ";
71 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
72 if (LBB)
73 Op += LBB->getName();
74 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattner18c2f132005-01-13 20:50:02 +000075 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000076 Op += " #" + utostr(C2V->getReg());
77 } else if (const ExternalSymbolSDNode *ES =
78 dyn_cast<ExternalSymbolSDNode>(Node)) {
79 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000080 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
81 if (M->getValue())
82 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
83 else
84 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnere9c44cd2005-01-11 00:34:33 +000085 }
86 return Op;
87}
Misha Brukmanedf128a2005-04-21 22:36:52 +000088
Chris Lattnere9c44cd2005-01-11 00:34:33 +000089
Chris Lattner66328482005-01-10 23:08:40 +000090/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
91/// rendered using 'dot'.
92///
93void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +000094// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +000095#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +000096 std::string Filename = "/tmp/dag." +
97 getMachineFunction().getFunction()->getName() + ".dot";
98 std::cerr << "Writing '" << Filename << "'... ";
99 std::ofstream F(Filename.c_str());
100
101 if (!F) {
102 std::cerr << " error opening file for writing!\n";
103 return;
104 }
105
106 WriteGraph(F, this);
107 F.close();
108 std::cerr << "\n";
109
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000110#ifdef HAVE_GRAPHVIZ
111 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000112 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000113 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
114 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000115 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000116 return;
117 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000118#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000119
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000120#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000121 std::cerr << "Running 'dot' program... " << std::flush;
122 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
123 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000124 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000125 } else {
126 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000127 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000128 }
129 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000130 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000131#endif // HAVE_GV
132#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000133 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
134 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000135
136#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000137 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000138#endif
Chris Lattner66328482005-01-10 23:08:40 +0000139}