blob: 73535809333f47e65b06594518efaf439301512e [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
Chris Lattner5839bf22005-08-26 17:15:30 +000014#include "llvm/Constants.h"
15#include "llvm/Function.h"
Chris Lattner66328482005-01-10 23:08:40 +000016#include "llvm/CodeGen/SelectionDAG.h"
17#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner7228aa72005-08-19 21:21:16 +000018#include "llvm/Target/MRegisterInfo.h"
19#include "llvm/Target/TargetMachine.h"
Chris Lattner66328482005-01-10 23:08:40 +000020#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000021#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000022#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000023#include <fstream>
24using namespace llvm;
25
Chris Lattnere0646b82005-01-10 23:26:00 +000026namespace llvm {
27 template<>
28 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
29 static std::string getGraphName(const SelectionDAG *G) {
30 return G->getMachineFunction().getFunction()->getName();
31 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000032
33 static bool renderGraphFromBottomUp() {
34 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000035 }
Chris Lattner37345fe2005-10-01 00:17:07 +000036
37 static bool hasNodeAddressLabel(const SDNode *Node,
38 const SelectionDAG *Graph) {
39 return true;
40 }
Chris Lattnere0646b82005-01-10 23:26:00 +000041
Chris Lattnere9c44cd2005-01-11 00:34:33 +000042 static std::string getNodeLabel(const SDNode *Node,
43 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000044 static std::string getNodeAttributes(const SDNode *N) {
45 return "shape=Mrecord";
46 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000047
48 static void addCustomGraphFeatures(SelectionDAG *G,
49 GraphWriter<SelectionDAG*> &GW) {
50 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
51 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
52 }
Chris Lattnere0646b82005-01-10 23:26:00 +000053 };
54}
55
Chris Lattnere9c44cd2005-01-11 00:34:33 +000056std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
57 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000058 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000059
Chris Lattnerad95d6a2005-08-16 18:31:23 +000060 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
61 if (Node->getValueType(i) == MVT::Other)
62 Op += ":ch";
63 else
64 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
65
Chris Lattnere9c44cd2005-01-11 00:34:33 +000066 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
67 Op += ": " + utostr(CSDN->getValue());
68 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
69 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000070 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000071 dyn_cast<GlobalAddressSDNode>(Node)) {
72 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000073 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000074 Op += " " + itostr(FIDN->getIndex());
75 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Chris Lattner5839bf22005-08-26 17:15:30 +000076 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
77 Op += "<" + ftostr(CFP->getValue()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000078 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000079 Op = "BB: ";
80 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
81 if (LBB)
82 Op += LBB->getName();
83 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +000084 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner44fa7642005-11-19 06:58:46 +000085 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +000086 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
87 } else {
88 Op += " #" + utostr(R->getReg());
89 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000090 } else if (const ExternalSymbolSDNode *ES =
91 dyn_cast<ExternalSymbolSDNode>(Node)) {
92 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000093 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
94 if (M->getValue())
95 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
96 else
97 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +000098 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +000099 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000100 }
101 return Op;
102}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000103
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000104
Chris Lattner66328482005-01-10 23:08:40 +0000105/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
106/// rendered using 'dot'.
107///
108void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000109// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000110#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000111 std::string Filename = "/tmp/dag." +
112 getMachineFunction().getFunction()->getName() + ".dot";
113 std::cerr << "Writing '" << Filename << "'... ";
114 std::ofstream F(Filename.c_str());
115
116 if (!F) {
117 std::cerr << " error opening file for writing!\n";
118 return;
119 }
120
121 WriteGraph(F, this);
122 F.close();
123 std::cerr << "\n";
124
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000125#ifdef HAVE_GRAPHVIZ
126 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000127 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000128 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
129 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000130 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000131 return;
132 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000133#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000134
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000135#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000136 std::cerr << "Running 'dot' program... " << std::flush;
137 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
138 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000139 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000140 } else {
141 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000142 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000143 }
144 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000145 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000146#endif // HAVE_GV
147#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000148 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
149 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000150
151#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000152 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000153#endif
Chris Lattner66328482005-01-10 23:08:40 +0000154}