blob: d69004a557fa62cbad4992ecef8db9ba714ee2e3 [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 }
36
Chris Lattnere9c44cd2005-01-11 00:34:33 +000037 static std::string getNodeLabel(const SDNode *Node,
38 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000039 static std::string getNodeAttributes(const SDNode *N) {
40 return "shape=Mrecord";
41 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000042
43 static void addCustomGraphFeatures(SelectionDAG *G,
44 GraphWriter<SelectionDAG*> &GW) {
45 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
46 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
47 }
Chris Lattnere0646b82005-01-10 23:26:00 +000048 };
49}
50
Chris Lattnere9c44cd2005-01-11 00:34:33 +000051std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
52 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000053 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000054
Chris Lattnerad95d6a2005-08-16 18:31:23 +000055 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
56 if (Node->getValueType(i) == MVT::Other)
57 Op += ":ch";
58 else
59 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
60
Chris Lattnere9c44cd2005-01-11 00:34:33 +000061 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
62 Op += ": " + utostr(CSDN->getValue());
63 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
64 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000065 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000066 dyn_cast<GlobalAddressSDNode>(Node)) {
67 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000068 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000069 Op += " " + itostr(FIDN->getIndex());
70 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Chris Lattner5839bf22005-08-26 17:15:30 +000071 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
72 Op += "<" + ftostr(CFP->getValue()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000073 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000074 Op = "BB: ";
75 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
76 if (LBB)
77 Op += LBB->getName();
78 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +000079 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner7228aa72005-08-19 21:21:16 +000080 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
81 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
82 } else {
83 Op += " #" + utostr(R->getReg());
84 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000085 } else if (const ExternalSymbolSDNode *ES =
86 dyn_cast<ExternalSymbolSDNode>(Node)) {
87 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000088 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
89 if (M->getValue())
90 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
91 else
92 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +000093 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +000094 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattnere9c44cd2005-01-11 00:34:33 +000095 }
96 return Op;
97}
Misha Brukmanedf128a2005-04-21 22:36:52 +000098
Chris Lattnere9c44cd2005-01-11 00:34:33 +000099
Chris Lattner66328482005-01-10 23:08:40 +0000100/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
101/// rendered using 'dot'.
102///
103void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000104// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000105#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000106 std::string Filename = "/tmp/dag." +
107 getMachineFunction().getFunction()->getName() + ".dot";
108 std::cerr << "Writing '" << Filename << "'... ";
109 std::ofstream F(Filename.c_str());
110
111 if (!F) {
112 std::cerr << " error opening file for writing!\n";
113 return;
114 }
115
116 WriteGraph(F, this);
117 F.close();
118 std::cerr << "\n";
119
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000120#ifdef HAVE_GRAPHVIZ
121 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000122 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000123 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
124 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000125 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000126 return;
127 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000128#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000129
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000130#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000131 std::cerr << "Running 'dot' program... " << std::flush;
132 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
133 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000134 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000135 } else {
136 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000137 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000138 }
139 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000140 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000141#endif // HAVE_GV
142#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000143 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
144 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000145
146#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000147 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000148#endif
Chris Lattner66328482005-01-10 23:08:40 +0000149}