blob: 060102c8fcaf0747fd65993659a880dab7f0a1bc [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"
Chris Lattner7228aa72005-08-19 21:21:16 +000016#include "llvm/Target/MRegisterInfo.h"
17#include "llvm/Target/TargetMachine.h"
Chris Lattner66328482005-01-10 23:08:40 +000018#include "llvm/Function.h"
19#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000021#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000022#include <fstream>
23using namespace llvm;
24
Chris Lattnere0646b82005-01-10 23:26:00 +000025namespace llvm {
26 template<>
27 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
28 static std::string getGraphName(const SelectionDAG *G) {
29 return G->getMachineFunction().getFunction()->getName();
30 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000031
32 static bool renderGraphFromBottomUp() {
33 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000034 }
35
Chris Lattnere9c44cd2005-01-11 00:34:33 +000036 static std::string getNodeLabel(const SDNode *Node,
37 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000038 static std::string getNodeAttributes(const SDNode *N) {
39 return "shape=Mrecord";
40 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000041
42 static void addCustomGraphFeatures(SelectionDAG *G,
43 GraphWriter<SelectionDAG*> &GW) {
44 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
45 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
46 }
Chris Lattnere0646b82005-01-10 23:26:00 +000047 };
48}
49
Chris Lattnere9c44cd2005-01-11 00:34:33 +000050std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
51 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000052 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000053
Chris Lattnerad95d6a2005-08-16 18:31:23 +000054 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
55 if (Node->getValueType(i) == MVT::Other)
56 Op += ":ch";
57 else
58 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
59
Chris Lattnere9c44cd2005-01-11 00:34:33 +000060 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
61 Op += ": " + utostr(CSDN->getValue());
62 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
63 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000064 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000065 dyn_cast<GlobalAddressSDNode>(Node)) {
66 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000067 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000068 Op += " " + itostr(FIDN->getIndex());
69 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
70 Op += "<" + utostr(CP->getIndex()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000071 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000072 Op = "BB: ";
73 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
74 if (LBB)
75 Op += LBB->getName();
76 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +000077 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner7228aa72005-08-19 21:21:16 +000078 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
79 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
80 } else {
81 Op += " #" + utostr(R->getReg());
82 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000083 } else if (const ExternalSymbolSDNode *ES =
84 dyn_cast<ExternalSymbolSDNode>(Node)) {
85 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000086 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
87 if (M->getValue())
88 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
89 else
90 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +000091 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
92 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnere9c44cd2005-01-11 00:34:33 +000093 }
94 return Op;
95}
Misha Brukmanedf128a2005-04-21 22:36:52 +000096
Chris Lattnere9c44cd2005-01-11 00:34:33 +000097
Chris Lattner66328482005-01-10 23:08:40 +000098/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
99/// rendered using 'dot'.
100///
101void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000102// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000103#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000104 std::string Filename = "/tmp/dag." +
105 getMachineFunction().getFunction()->getName() + ".dot";
106 std::cerr << "Writing '" << Filename << "'... ";
107 std::ofstream F(Filename.c_str());
108
109 if (!F) {
110 std::cerr << " error opening file for writing!\n";
111 return;
112 }
113
114 WriteGraph(F, this);
115 F.close();
116 std::cerr << "\n";
117
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000118#ifdef HAVE_GRAPHVIZ
119 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000120 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000121 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
122 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000123 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000124 return;
125 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000126#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000127
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000128#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000129 std::cerr << "Running 'dot' program... " << std::flush;
130 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
131 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000132 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000133 } else {
134 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000135 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000136 }
137 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000138 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000139#endif // HAVE_GV
140#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000141 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
142 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000143
144#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000145 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000146#endif
Chris Lattner66328482005-01-10 23:08:40 +0000147}