blob: 61e4e0212420c1bebafa176cda64335ef0a0c2ac [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)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +000073 int offset = GADN->getOffset();
Chris Lattnere9c44cd2005-01-11 00:34:33 +000074 Op += ": " + GADN->getGlobal()->getName();
Evan Cheng61ca74b2005-11-30 02:04:11 +000075 if (offset > 0)
76 Op += "+" + itostr(offset);
77 else
78 Op += itostr(offset);
Misha Brukmandedf2bd2005-04-22 04:01:18 +000079 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000080 Op += " " + itostr(FIDN->getIndex());
81 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
Chris Lattner5839bf22005-08-26 17:15:30 +000082 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
83 Op += "<" + ftostr(CFP->getValue()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000084 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000085 Op = "BB: ";
86 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
87 if (LBB)
88 Op += LBB->getName();
89 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +000090 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
Chris Lattner44fa7642005-11-19 06:58:46 +000091 if (G && R->getReg() != 0 && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +000092 Op = Op + " " + G->getTarget().getRegisterInfo()->getName(R->getReg());
93 } else {
94 Op += " #" + utostr(R->getReg());
95 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000096 } else if (const ExternalSymbolSDNode *ES =
97 dyn_cast<ExternalSymbolSDNode>(Node)) {
98 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000099 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
100 if (M->getValue())
101 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
102 else
103 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnera23e8152005-08-18 03:31:02 +0000104 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
Chris Lattnere39db072005-08-24 18:30:00 +0000105 Op = Op + " VT=" + getValueTypeString(N->getVT());
Chris Lattner36ce6912005-11-29 06:21:05 +0000106 } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
107 Op = Op + "\"" + N->getValue() + "\"";
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000108 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000109
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000110 return Op;
111}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000112
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000113
Chris Lattner66328482005-01-10 23:08:40 +0000114/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
115/// rendered using 'dot'.
116///
117void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000118// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000119#ifndef NDEBUG
Chris Lattner6bf234c2005-11-20 03:45:52 +0000120 sys::Path TempDir = sys::Path::GetTemporaryDirectory();
121 sys::Path Filename = TempDir;
122 Filename.appendComponent("dag." + getMachineFunction().getFunction()->getName() + ".dot");
123 std::cerr << "Writing '" << Filename.toString() << "'... ";
124 std::ofstream F(Filename.toString().c_str());
Chris Lattner66328482005-01-10 23:08:40 +0000125
126 if (!F) {
127 std::cerr << " error opening file for writing!\n";
128 return;
129 }
130
131 WriteGraph(F, this);
132 F.close();
133 std::cerr << "\n";
134
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000135#ifdef HAVE_GRAPHVIZ
136 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattner6bf234c2005-11-20 03:45:52 +0000137 if (system((LLVM_PATH_GRAPHVIZ " " + Filename.toString()).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000138 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
139 } else {
Chris Lattner6bf234c2005-11-20 03:45:52 +0000140 Filename.eraseFromDisk();
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000141 return;
142 }
Misha Brukmancd33eef2005-08-04 14:22:41 +0000143#endif // HAVE_GRAPHVIZ
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000144
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000145#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000146 std::cerr << "Running 'dot' program... " << std::flush;
Chris Lattner6bf234c2005-11-20 03:45:52 +0000147 sys::Path PSFilename = TempDir;
148 PSFilename.appendComponent("dag.tempgraph.ps");
149 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename.toString()
150 + " > " + PSFilename.toString()).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000151 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000152 } else {
153 std::cerr << "\n";
Chris Lattner6bf234c2005-11-20 03:45:52 +0000154 system((LLVM_PATH_GV " " + PSFilename.toString()).c_str());
155 system((LLVM_PATH_GV " "+TempDir.toString()+ "dag.tempgraph.ps").c_str());
Chris Lattner66328482005-01-10 23:08:40 +0000156 }
Chris Lattner6bf234c2005-11-20 03:45:52 +0000157 Filename.eraseFromDisk();
158 PSFilename.eraseFromDisk();
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000159 return;
Misha Brukmancd33eef2005-08-04 14:22:41 +0000160#endif // HAVE_GV
161#endif // NDEBUG
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000162 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
163 << "systems with Graphviz or gv!\n";
Misha Brukmancd33eef2005-08-04 14:22:41 +0000164
165#ifndef NDEBUG
Chris Lattner6bf234c2005-11-20 03:45:52 +0000166 Filename.eraseFromDisk();
167 TempDir.eraseFromDisk(true);
Misha Brukmancd33eef2005-08-04 14:22:41 +0000168#endif
Chris Lattner66328482005-01-10 23:08:40 +0000169}