blob: 2a51d203fe1fd50d73d09754168ae9d13e1f1db2 [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 Lattnerd5d0f9b2005-08-16 21:55:35 +000075 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
76 Op += " #" + utostr(R->getReg());
Chris Lattnere9c44cd2005-01-11 00:34:33 +000077 } 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 Lattnera23e8152005-08-18 03:31:02 +000085 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
86 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnere9c44cd2005-01-11 00:34:33 +000087 }
88 return Op;
89}
Misha Brukmanedf128a2005-04-21 22:36:52 +000090
Chris Lattnere9c44cd2005-01-11 00:34:33 +000091
Chris Lattner66328482005-01-10 23:08:40 +000092/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
93/// rendered using 'dot'.
94///
95void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +000096// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +000097#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +000098 std::string Filename = "/tmp/dag." +
99 getMachineFunction().getFunction()->getName() + ".dot";
100 std::cerr << "Writing '" << Filename << "'... ";
101 std::ofstream F(Filename.c_str());
102
103 if (!F) {
104 std::cerr << " error opening file for writing!\n";
105 return;
106 }
107
108 WriteGraph(F, this);
109 F.close();
110 std::cerr << "\n";
111
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000112#ifdef HAVE_GRAPHVIZ
113 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000114 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000115 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
116 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000117 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000118 return;
119 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000120#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000121
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000122#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000123 std::cerr << "Running 'dot' program... " << std::flush;
124 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
125 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000126 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000127 } else {
128 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000129 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000130 }
131 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000132 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000133#endif // HAVE_GV
134#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000135 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
136 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000137
138#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000139 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000140#endif
Chris Lattner66328482005-01-10 23:08:40 +0000141}