blob: a554b625f281153a0a04f1794ee688abe38d528f [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"
16#include "llvm/Function.h"
17#include "llvm/Support/GraphWriter.h"
Chris Lattnere9c44cd2005-01-11 00:34:33 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner6e741f82005-07-15 22:48:31 +000019#include "llvm/Config/config.h"
Chris Lattner66328482005-01-10 23:08:40 +000020#include <fstream>
21using namespace llvm;
22
Chris Lattnere0646b82005-01-10 23:26:00 +000023namespace llvm {
24 template<>
25 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
26 static std::string getGraphName(const SelectionDAG *G) {
27 return G->getMachineFunction().getFunction()->getName();
28 }
Chris Lattnere9c44cd2005-01-11 00:34:33 +000029
30 static bool renderGraphFromBottomUp() {
31 return true;
Chris Lattnere0646b82005-01-10 23:26:00 +000032 }
33
Chris Lattnere9c44cd2005-01-11 00:34:33 +000034 static std::string getNodeLabel(const SDNode *Node,
35 const SelectionDAG *Graph);
Chris Lattnere0646b82005-01-10 23:26:00 +000036 static std::string getNodeAttributes(const SDNode *N) {
37 return "shape=Mrecord";
38 }
Chris Lattnerfc08d9c2005-01-10 23:52:04 +000039
40 static void addCustomGraphFeatures(SelectionDAG *G,
41 GraphWriter<SelectionDAG*> &GW) {
42 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
43 GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
44 }
Chris Lattnere0646b82005-01-10 23:26:00 +000045 };
46}
47
Chris Lattnere9c44cd2005-01-11 00:34:33 +000048std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
49 const SelectionDAG *G) {
50 std::string Op = Node->getOperationName();
Chris Lattnerc871e1d2005-01-11 22:21:04 +000051
52 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
53 switch (Node->getValueType(i)) {
54 default: Op += ":unknownvt!"; break;
55 case MVT::Other: Op += ":ch"; break;
56 case MVT::i1: Op += ":i1"; break;
57 case MVT::i8: Op += ":i8"; break;
58 case MVT::i16: Op += ":i16"; break;
59 case MVT::i32: Op += ":i32"; break;
60 case MVT::i64: Op += ":i64"; break;
61 case MVT::i128: Op += ":i128"; break;
62 case MVT::f32: Op += ":f32"; break;
63 case MVT::f64: Op += ":f64"; break;
64 case MVT::f80: Op += ":f80"; break;
65 case MVT::f128: Op += ":f128"; break;
66 case MVT::isVoid: Op += ":void"; break;
67 }
68 }
69
Chris Lattnere9c44cd2005-01-11 00:34:33 +000070 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
71 Op += ": " + utostr(CSDN->getValue());
72 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
73 Op += ": " + ftostr(CSDN->getValue());
Misha Brukmanedf128a2005-04-21 22:36:52 +000074 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnere9c44cd2005-01-11 00:34:33 +000075 dyn_cast<GlobalAddressSDNode>(Node)) {
76 Op += ": " + GADN->getGlobal()->getName();
Misha Brukmandedf2bd2005-04-22 04:01:18 +000077 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000078 Op += " " + itostr(FIDN->getIndex());
79 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
80 Op += "<" + utostr(CP->getIndex()) + ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +000081 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000082 Op = "BB: ";
83 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
84 if (LBB)
85 Op += LBB->getName();
86 //Op += " " + (const void*)BBDN->getBasicBlock();
Chris Lattner18c2f132005-01-13 20:50:02 +000087 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(Node)) {
Chris Lattnere9c44cd2005-01-11 00:34:33 +000088 Op += " #" + utostr(C2V->getReg());
89 } else if (const ExternalSymbolSDNode *ES =
90 dyn_cast<ExternalSymbolSDNode>(Node)) {
91 Op += "'" + std::string(ES->getSymbol()) + "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +000092 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
93 if (M->getValue())
94 Op += "<" + M->getValue()->getName() + ":" + itostr(M->getOffset()) + ">";
95 else
96 Op += "<null:" + itostr(M->getOffset()) + ">";
Chris Lattnere9c44cd2005-01-11 00:34:33 +000097 }
98 return Op;
99}
Misha Brukmanedf128a2005-04-21 22:36:52 +0000100
Chris Lattnere9c44cd2005-01-11 00:34:33 +0000101
Chris Lattner66328482005-01-10 23:08:40 +0000102/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
103/// rendered using 'dot'.
104///
105void SelectionDAG::viewGraph() {
Chris Lattnere388b5e2005-07-14 05:17:43 +0000106// This code is only for debugging!
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000107#ifndef NDEBUG
Chris Lattner66328482005-01-10 23:08:40 +0000108 std::string Filename = "/tmp/dag." +
109 getMachineFunction().getFunction()->getName() + ".dot";
110 std::cerr << "Writing '" << Filename << "'... ";
111 std::ofstream F(Filename.c_str());
112
113 if (!F) {
114 std::cerr << " error opening file for writing!\n";
115 return;
116 }
117
118 WriteGraph(F, this);
119 F.close();
120 std::cerr << "\n";
121
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000122#ifdef HAVE_GRAPHVIZ
123 std::cerr << "Running 'Graphviz' program... " << std::flush;
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000124 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000125 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
126 } else {
127 return;
128 }
129#endif
130
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000131#ifdef HAVE_GV
Chris Lattner66328482005-01-10 23:08:40 +0000132 std::cerr << "Running 'dot' program... " << std::flush;
133 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
134 + " > /tmp/dag.tempgraph.ps").c_str())) {
Chris Lattnerf1a2f152005-07-14 01:10:55 +0000135 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Chris Lattner66328482005-01-10 23:08:40 +0000136 } else {
137 std::cerr << "\n";
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000138 system(LLVM_PATH_GV " /tmp/dag.tempgraph.ps");
Chris Lattner66328482005-01-10 23:08:40 +0000139 }
140 system(("rm " + Filename + " /tmp/dag.tempgraph.ps").c_str());
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000141 return;
Chris Lattnere388b5e2005-07-14 05:17:43 +0000142#endif
Chris Lattnerc5f44ad2005-07-14 05:33:13 +0000143#endif
144 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
145 << "systems with Graphviz or gv!\n";
Chris Lattner66328482005-01-10 23:08:40 +0000146}