blob: 1782587c3e40702518c4aa1fda1e79cde4dc34ea [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 Lattner3aa2c742005-11-19 07:44:09 +000021#include "llvm/System/Path.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000023#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000024#include <fstream>
25using namespace llvm;
26
Chris Lattnere0646b82005-01-10 23:26:00 +000027namespace llvm {
28 template<>
29 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
30 static std::string getGraphName(const SelectionDAG *G) {
31 return G->getMachineFunction().getFunction()->getName();
32 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000033
34 static bool renderGraphFromBottomUp() {
35 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000036 }
Chris Lattner37345fe2005-10-01 00:17:07 +000037
38 static bool hasNodeAddressLabel(const SDNode *Node,
39 const SelectionDAG *Graph) {
40 return true;
41 }
Chris Lattnere0646b82005-01-10 23:26:00 +000042
Chris Lattnere9c44cd2005-01-11 00:34:33 +000043 static std::string getNodeLabel(const SDNode *Node,
44 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000045 static std::string getNodeAttributes(const SDNode *N) {
46 return "shape=Mrecord";
47 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000048
49 static void addCustomGraphFeatures(SelectionDAG *G,
50 GraphWriter<SelectionDAG*> &GW) {
51 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
52 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
53 }
Chris Lattnere0646b82005-01-10 23:26:00 +000054 };
55}
56
Chris Lattnere9c44cd2005-01-11 00:34:33 +000057std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
58 const SelectionDAG *G) {
Chris Lattnerad95d6a2005-08-16 18:31:23 +000059 std::string Op = Node->getOperationName(G);
Chris Lattnerc871e1d2005-01-11 22:21:04 +000060
Chris Lattnerad95d6a2005-08-16 18:31:23 +000061 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
62 if (Node->getValueType(i) == MVT::Other)
63 Op += ":ch";
64 else
65 Op = Op + ":" + MVT::getValueTypeString(Node->getValueType(i));
66
Chris Lattnere9c44cd2005-01-11 00:34:33 +000067 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
68 Op += ": " + utostr(CSDN->getValue());
69 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
70 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000071 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000072 dyn_cast<GlobalAddressSDNode>(Node)) {
73 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000074 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000075 Op += " " + itostr(FIDN->getIndex());
76 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Chris Lattner5839bf22005-08-26 17:15:30 +000077 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
78 Op += "<" + ftostr(CFP->getValue()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000079 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000080 Op = "BB: ";
81 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
82 if (LBB)
83 Op += LBB->getName();
84 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +000085 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner44fa7642005-11-19 06:58:46 +000086 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +000087 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
88 } else {
89 Op += " #" + utostr(R->getReg());
90 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000091 } else if (const ExternalSymbolSDNode *ES =
92 dyn_cast<ExternalSymbolSDNode>(Node)) {
93 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000094 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
95 if (M->getValue())
96 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
97 else
98 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +000099 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +0000100 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000101 }
102 return Op;
103}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000104
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000105
Chris Lattner66328482005-01-10 23:08:40 +0000106/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
107/// rendered using 'dot'.
108///
109void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000110// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000111#ifndef NDEBUG
Chris Lattner3aa2c742005-11-19 07:44:09 +0000112 std::string TempDir = sys::Path::GetTemporaryDirectory().toString();
113 std::string Filename = TempDir + "dag." +
Chris Lattner66328482005-01-10 23:08:40 +0000114 getMachineFunction().getFunction()->getName() + ".dot";
115 std::cerr << "Writing '" << Filename << "'... ";
116 std::ofstream F(Filename.c_str());
117
118 if (!F) {
119 std::cerr << " error opening file for writing!\n";
120 return;
121 }
122
123 WriteGraph(F, this);
124 F.close();
125 std::cerr << "\n";
126
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000127#ifdef HAVE_GRAPHVIZ
128 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000129 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000130 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
131 } else {
Chris Lattner4c64dd72005-08-03 20:31:37 +0000132 system(("rm " + Filename).c_str());
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000133 return;
134 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000135#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000136
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000137#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000138 std::cerr << "Running 'dot' program... " << std::flush;
139 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
Chris Lattner3aa2c742005-11-19 07:44:09 +0000140 + " > " + TempDir +"dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000141 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000142 } else {
143 std::cerr << "\n";
Chris Lattner3aa2c742005-11-19 07:44:09 +0000144 system(LLVM_PATH_GV " " + TempDir + "dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000145 }
Chris Lattner3aa2c742005-11-19 07:44:09 +0000146 system(("rm " + Filename + " "+ TempDir + "dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000147 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000148#endif // HAVE_GV
149#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000150 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
151 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000152
153#ifndef NDEBUG
Chris Lattner4c64dd72005-08-03 20:31:37 +0000154 system(("rm " + Filename).c_str());
Misha Brukmancd33eef2005-08-04 14:22:41 +0000155#endif
Chris Lattner66328482005-01-10 23:08:40 +0000156}