CACAO
PassManager.hpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/PassManager.hpp - PassManager
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 
25 #ifndef _JIT_COMPILER2_PASSMANAGER
26 #define _JIT_COMPILER2_PASSMANAGER
27 
28 #include <memory>
29 #include <unordered_map>
30 
34 
35 #include "toolbox/logging.hpp"
36 
37 // NOTE: the register stuff can not use LOG* macros because comman line
38 // arguments are not parsed at registration
39 #if 0
40 #define MYLOG(EXPR) do { dbg() << EXPR; } while(0)
41 #else
42 #define MYLOG(EXPR)
43 #endif
44 
45 namespace cacao {
46 namespace jit {
47 namespace compiler2 {
48 
49 class JITData;
50 class Pass;
51 class PassRunner;
52 
53 using PassUPtrTy = std::unique_ptr<Pass>;
54 
55 class PassInfo {
56 public:
57  using IDTy = uint32_t;
58  using ConstructorTy = Pass* (*)();
59 private:
60  const char *const name;
61  /// Constructor function pointer
63 public:
65  PassInfo(const char* name, PassInfo::IDTy ID, ConstructorTy ctor) : name(name), ctor(ctor), ID(ID) {}
66  const char* get_name() const {
67  return name;
68  }
69  Pass* create_Pass() const {
70  return ctor();
71  }
72 };
73 
74 
75 /**
76  * Manages pass registry and scheduling.
77  *
78  * PassManager is implemented as a singleton. It holds a list of registered passes
79  * and knows how to construct them.
80  *
81  * Passes are scheduled the first time
82  * the schedule is accessed and the schedule does not change after that.
83  *
84  * Running passes and propagating results between passes
85  * is handled by the PassRunner.
86  */
87 class PassManager {
88 public:
91 private:
92  /**
93  * This is the pass schedule. A pass may occur more than once.
94  */
96 
98 
101  return registered_passes;
102  }
103 
104  void schedulePasses();
106 
107  explicit PassManager() : passes_are_scheduled(false) {}
108 
109 public:
110  static PassManager& get() {
111  // C++11 ensures that the initialization for local static variables is thread-safe
112  static PassManager instance;
113  return instance;
114  }
115 
116  /**
117  * DO NOT CALL THIS MANUALLY. ONLY INVOKE VIA RegisterPass.
118  */
119  static void register_Pass(PassInfo *PI) {
120  MYLOG("PassManager::register_Pass: " << PI->get_name() << nl);
121  registered_passes().insert(std::make_pair(PI->ID,PI));
122  }
123 
124  const char * get_Pass_name(PassInfo::IDTy ID) {
125  PassInfo *PI = registered_passes()[ID];
126  assert(PI && "Pass not registered");
127  return PI->get_name();
128  }
129 
130  ScheduleListTy::const_iterator schedule_begin() {
131  if (!passes_are_scheduled) {
132  schedulePasses();
133  passes_are_scheduled = true;
134  }
135  return schedule.begin();
136  }
137  ScheduleListTy::const_iterator schedule_end() const { return schedule.end(); }
138 
139  PassInfoMapTy::const_iterator registered_begin() const { return registered_passes().begin(); }
140  PassInfoMapTy::const_iterator registered_end() const { return registered_passes().end(); }
141 
142  friend class PassRunner;
143 };
144 
145 
146 /**
147  * Each instance of PassRunner represents a single run of the compiler2.
148  *
149  * The PassRunner owns all the pass instances for its corresponding run.
150  */
151 class PassRunner {
152 public:
153  using PassMapTy = std::unordered_map<PassInfo::IDTy,PassUPtrTy>;
155 private:
156  /**
157  * Stores pass instances so other passes can retrieve
158  * their results. This map owns all contained passes.
159  */
161 
162  /**
163  * Map of ready results
164  */
166 
168 
169  template<class _PassClass>
170  _PassClass* get_Pass_result() {
171  auto pass_id = _PassClass::template ID<_PassClass>();
172 
173  assert_msg(result_ready[pass_id], "result for "
174  << PassManager::get().get_Pass_name(pass_id) << " is not ready!");
175  return (_PassClass*) passes[pass_id].get();
176  }
177 
178 public:
179  /**
180  * run passes
181  */
182  void runPasses(JITData &JD);
183 
184  friend class Pass;
185 };
186 
187 template<class _PassClass>
188 Pass *call_ctor() { return new _PassClass(); }
189 
190 template <class _PassClass>
191 struct PassRegistry : public PassInfo {
192  PassRegistry(const char * name) : PassInfo(name, _PassClass::template ID<_PassClass>(), (PassInfo::ConstructorTy)call_ctor<_PassClass>) {
194  }
195 };
196 
197 #undef MYLOG
198 
199 } // end namespace cacao
200 } // end namespace jit
201 } // end namespace compiler2
202 
203 #endif /* _JIT_COMPILER2_PASSMANAGER */
204 
205 
206 /*
207  * These are local overrides for various environment variables in Emacs.
208  * Please do not remove this and leave it at the end of the file, where
209  * Emacs will automagically detect them.
210  * ---------------------------------------------------------------------
211  * Local variables:
212  * mode: c++
213  * indent-tabs-mode: t
214  * c-basic-offset: 4
215  * tab-width: 4
216  * End:
217  * vim:noexpandtab:sw=4:ts=4:
218  */
Pass superclass All compiler passes should inheritate this class.
Definition: Pass.hpp:47
PassUPtrTy & get_Pass(PassInfo::IDTy ID)
Definition: PassManager.cpp:80
Each instance of PassRunner represents a single run of the compiler2.
std::unordered_map< PassInfo::IDTy, PassUPtrTy > PassMapTy
PassInfo(const char *name, PassInfo::IDTy ID, ConstructorTy ctor)
Definition: PassManager.hpp:65
std::unique_ptr< Pass > PassUPtrTy
Definition: PassManager.hpp:53
ResultReadyMapTy result_ready
Map of ready results.
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
alloc::vector< PassInfo::IDTy >::type ScheduleListTy
Definition: PassManager.hpp:89
PassInfoMapTy::const_iterator registered_begin() const
PassUPtrTy create_Pass(PassInfo::IDTy ID) const
Definition: PassManager.cpp:63
ScheduleListTy::const_iterator schedule_begin()
std::vector< T, Allocator< T > > type
Definition: vector.hpp:38
PassMapTy passes
Stores pass instances so other passes can retrieve their results.
static PassInfoMapTy & registered_passes()
Definition: PassManager.hpp:99
PassInfoMapTy::const_iterator registered_end() const
Manages pass registry and scheduling.
Definition: PassManager.hpp:87
#define MYLOG(EXPR)
Definition: PassManager.hpp:42
const char * get_Pass_name(PassInfo::IDTy ID)
alloc::unordered_map< PassInfo::IDTy, bool >::type ResultReadyMapTy
static void register_Pass(PassInfo *PI)
DO NOT CALL THIS MANUALLY.
const char * get_name() const
Definition: PassManager.hpp:66
alloc::unordered_map< PassInfo::IDTy, PassInfo * >::type PassInfoMapTy
Definition: PassManager.hpp:90
#define assert_msg(COND, EXPR)
Definition: logging.hpp:107
ScheduleListTy::const_iterator schedule_end() const
ConstructorTy ctor
Constructor function pointer.
Definition: PassManager.hpp:62
Nl nl
Definition: OStream.cpp:56
void runPasses(JITData &JD)
run passes
Definition: PassManager.cpp:91
ScheduleListTy schedule
This is the pass schedule.
Definition: PassManager.hpp:95
std::unordered_map< Key, T, Hash, KeyEqual, Allocator< std::pair< const Key, T > > > type