CACAO
DomTreePrinterPass.cpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/DomTreePrinterPass.cpp - DomTreePrinterPass
2 
3  Copyright (C) 2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
31 
35 
36 #include "toolbox/GraphPrinter.hpp"
37 #include "vm/class.hpp"
38 #include "vm/jit/jit.hpp"
39 
40 #include <sstream>
41 
42 namespace cacao {
43 namespace jit {
44 namespace compiler2 {
45 
46 namespace {
47 
48 class DomTreeGraph : public PrintableGraph<Method*,BeginInst*> {
49 protected:
50  const Method &M;
51  bool verbose;
52  DominatorTree &DT;
53  const DFSTraversal<BeginInst> dfs;
54 
55 public:
56 
57  DomTreeGraph(const Method &M, DominatorTree &DT, bool verbose = false) :
58  M(M), verbose(verbose), DT(DT), dfs(M.get_init_bb()) {
59  for(Method::BBListTy::const_iterator i = M.bb_begin(),
60  e = M.bb_end(); i != e; ++i) {
61  BeginInst *BI = *i;
62  if (BI == NULL)
63  continue;
64  nodes.insert(BI);
65  BeginInst *idom = DT.get_idominator(BI);
66  if (idom) {
67  EdgeType edge = std::make_pair(idom,BI);
68  edges.insert(edge);
69  }
70  }
71  }
72 
73  virtual OStream& getGraphName(OStream& OS) const {
74  return OS << "DomTreeGraph";
75  }
76 
77  virtual OStream& getNodeLabel(OStream& OS, BeginInst *const &node) const {
78  return OS << *node;
79  }
80 
81 };
82 
83 } // end anonymous namespace
84 
87  return PU;
88 }
89 
90 Option<bool> DomTreePrinterPass::enabled("DomTreePrinterPass","compiler2: enable DomTreePrinterPass",false,::cacao::option::xx_root());
91 
92 // register pass
93 static PassRegistry<DomTreePrinterPass> X("DomTreePrinterPass");
94 
95 namespace {
96 std::string get_filename(methodinfo *m, jitdata *jd, std::string prefix = "cfg_", std::string suffix=".dot");
97 std::string get_filename(methodinfo *m, jitdata *jd, std::string prefix, std::string suffix)
98 {
99  std::string filename = prefix;
100  filename += Utf8String(m->clazz->name).begin();
101  filename += ".";
102  filename += Utf8String(m->name).begin();
103  filename += Utf8String(m->descriptor).begin();
104  filename += suffix;
105  /* replace unprintable chars */
106  for (size_t i = filename.find_first_of('/');
107  i != std::string::npos;
108  i = filename.find_first_of('/',i+1)) {
109  filename.replace(i,1,1,'.');
110  }
111  const char *unchar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.";
112  for (size_t i = filename.find_first_not_of(unchar);
113  i != std::string::npos;
114  i = filename.find_first_not_of(unchar,i+1)) {
115  filename.replace(i,1,1,'_');
116  }
117 
118  return filename;
119 }
120 } // end anonymous namespace
121 
122 // run pass
124  // get dominator tree
125  DominatorTree *DT = get_Pass<DominatorPass>();
126  assert(DT);
127  std::string name = get_filename(JD.get_jitdata()->m,JD.get_jitdata(),"domtree_");
128  GraphPrinter<DomTreeGraph>::print(name.c_str(), DomTreeGraph(*(JD.get_Method()),*DT));
129  return true;
130 }
131 
132 } // end namespace cacao
133 } // end namespace jit
134 } // end namespace compiler2
135 
136 
137 /*
138  * These are local overrides for various environment variables in Emacs.
139  * Please do not remove this and leave it at the end of the file, where
140  * Emacs will automagically detect them.
141  * ---------------------------------------------------------------------
142  * Local variables:
143  * mode: c++
144  * indent-tabs-mode: t
145  * c-basic-offset: 4
146  * tab-width: 4
147  * End:
148  * vim:noexpandtab:sw=4:ts=4:
149  */
Utf8String name
Definition: method.hpp:71
virtual PassUsage & get_PassUsage(PassUsage &PU) const
Set the requirements for the pass.
Definition: jit.hpp:126
DominatorTree & DT
static void print(const char *filename, const PrintableGraphTy &G)
BeginInst * BI
const DFSTraversal< BeginInst > dfs
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
jitdata * get_jitdata() const
Definition: JITData.hpp:51
std::set< EdgeType > & edges
Utf8String descriptor
Definition: method.hpp:72
classinfo * clazz
Definition: method.hpp:80
Utf8String name
Definition: class.hpp:91
Stores the interdependencies of a pass.
Definition: PassUsage.hpp:55
MIIterator i
OStream & OS
virtual bool run(JITData &JD)
Run the Pass.
MIIterator e
bool verbose
byte_iterator begin() const
Definition: utf8.hpp:106
methodinfo * m
Definition: jit.hpp:127
OptionPrefix & xx_root()
Definition: Option.cpp:39
const Method & M
static PassRegistry< BasicBlockSchedulingPass > X("BasicBlockSchedulingPass")
void add_requires()
PassName is required.
Definition: PassUsage.hpp:78
Calculate the Dominator Tree.