Tue, 29 Dec 2009 19:08:54 +0100
6986046: C1 valuestack cleanup
Summary: fixes an historical oddity in C1 with inlining where all of the expression stacks are kept in the topmost ValueStack instead of being in their respective ValueStacks.
Reviewed-by: never
Contributed-by: Christian Wimmer <cwimmer@uci.edu>
1 /*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 // An XHandler is a C1 internal description for an exception handler
27 class XHandler: public CompilationResourceObj {
28 private:
29 ciExceptionHandler* _desc;
31 BlockBegin* _entry_block; // Entry block of xhandler
32 LIR_List* _entry_code; // LIR-operations that must be executed before jumping to entry_block
33 int _entry_pco; // pco where entry_code (or entry_block if no entry_code) starts
34 int _phi_operand; // For resolving of phi functions at begin of entry_block
35 int _scope_count; // for filling ExceptionRangeEntry::scope_count
37 #ifdef ASSERT
38 int _lir_op_id; // op_id of the LIR-operation throwing to this handler
39 #endif
41 public:
42 // creation
43 XHandler(ciExceptionHandler* desc)
44 : _desc(desc)
45 , _entry_block(NULL)
46 , _entry_code(NULL)
47 , _entry_pco(-1)
48 , _phi_operand(-1)
49 , _scope_count(-1)
50 #ifdef ASSERT
51 , _lir_op_id(-1)
52 #endif
53 { }
55 XHandler(XHandler* other)
56 : _desc(other->_desc)
57 , _entry_block(other->_entry_block)
58 , _entry_code(other->_entry_code)
59 , _entry_pco(other->_entry_pco)
60 , _phi_operand(other->_phi_operand)
61 , _scope_count(other->_scope_count)
62 #ifdef ASSERT
63 , _lir_op_id(other->_lir_op_id)
64 #endif
65 { }
67 // accessors for data of ciExceptionHandler
68 int beg_bci() const { return _desc->start(); }
69 int end_bci() const { return _desc->limit(); }
70 int handler_bci() const { return _desc->handler_bci(); }
71 bool is_catch_all() const { return _desc->is_catch_all(); }
72 int catch_type() const { return _desc->catch_klass_index(); }
73 ciInstanceKlass* catch_klass() const { return _desc->catch_klass(); }
74 bool covers(int bci) const { return beg_bci() <= bci && bci < end_bci(); }
76 // accessors for additional fields
77 BlockBegin* entry_block() const { return _entry_block; }
78 LIR_List* entry_code() const { return _entry_code; }
79 int entry_pco() const { return _entry_pco; }
80 int phi_operand() const { assert(_phi_operand != -1, "not set"); return _phi_operand; }
81 int scope_count() const { assert(_scope_count != -1, "not set"); return _scope_count; }
82 DEBUG_ONLY(int lir_op_id() const { return _lir_op_id; });
84 void set_entry_block(BlockBegin* entry_block) {
85 assert(entry_block->is_set(BlockBegin::exception_entry_flag), "must be an exception handler entry");
86 assert(entry_block->bci() == handler_bci(), "bci's must correspond");
87 _entry_block = entry_block;
88 }
89 void set_entry_code(LIR_List* entry_code) { _entry_code = entry_code; }
90 void set_entry_pco(int entry_pco) { _entry_pco = entry_pco; }
91 void set_phi_operand(int phi_operand) { _phi_operand = phi_operand; }
92 void set_scope_count(int scope_count) { _scope_count = scope_count; }
93 DEBUG_ONLY(void set_lir_op_id(int lir_op_id) { _lir_op_id = lir_op_id; });
95 bool equals(XHandler* other) const;
96 };
98 define_array(_XHandlerArray, XHandler*)
99 define_stack(_XHandlerList, _XHandlerArray)
102 // XHandlers is the C1 internal list of exception handlers for a method
103 class XHandlers: public CompilationResourceObj {
104 private:
105 _XHandlerList _list;
107 public:
108 // creation
109 XHandlers() : _list() { }
110 XHandlers(ciMethod* method);
111 XHandlers(XHandlers* other);
113 // accessors
114 int length() const { return _list.length(); }
115 XHandler* handler_at(int i) const { return _list.at(i); }
116 bool has_handlers() const { return _list.length() > 0; }
117 void append(XHandler* h) { _list.append(h); }
118 XHandler* remove_last() { return _list.pop(); }
120 bool could_catch(ciInstanceKlass* klass, bool type_is_exact) const;
121 bool equals(XHandlers* others) const;
122 };
125 class IRScope;
126 define_array(IRScopeArray, IRScope*)
127 define_stack(IRScopeList, IRScopeArray)
129 class Compilation;
130 class IRScope: public CompilationResourceObj {
131 private:
132 // hierarchy
133 Compilation* _compilation; // the current compilation
134 IRScope* _caller; // the caller scope, or NULL
135 int _level; // the inlining level
136 ciMethod* _method; // the corresponding method
137 IRScopeList _callees; // the inlined method scopes
139 // graph
140 XHandlers* _xhandlers; // the exception handlers
141 int _number_of_locks; // the number of monitor lock slots needed
142 bool _monitor_pairing_ok; // the monitor pairing info
143 BlockBegin* _start; // the start block, successsors are method entries
145 BitMap _requires_phi_function; // bit is set if phi functions at loop headers are necessary for a local variable
147 // helper functions
148 BlockBegin* build_graph(Compilation* compilation, int osr_bci);
150 public:
151 // creation
152 IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph = false);
154 // accessors
155 Compilation* compilation() const { return _compilation; }
156 IRScope* caller() const { return _caller; }
157 int level() const { return _level; }
158 ciMethod* method() const { return _method; }
159 int max_stack() const; // NOTE: expensive
160 BitMap& requires_phi_function() { return _requires_phi_function; }
162 // hierarchy
163 bool is_top_scope() const { return _caller == NULL; }
164 void add_callee(IRScope* callee) { _callees.append(callee); }
165 int number_of_callees() const { return _callees.length(); }
166 IRScope* callee_no(int i) const { return _callees.at(i); }
168 // accessors, graph
169 bool is_valid() const { return start() != NULL; }
170 XHandlers* xhandlers() const { return _xhandlers; }
171 int number_of_locks() const { return _number_of_locks; }
172 void set_min_number_of_locks(int n) { if (n > _number_of_locks) _number_of_locks = n; }
173 bool monitor_pairing_ok() const { return _monitor_pairing_ok; }
174 BlockBegin* start() const { return _start; }
175 };
178 //
179 // IRScopeDebugInfo records the debug information for a particular IRScope
180 // in a particular CodeEmitInfo. This allows the information to be computed
181 // once early enough for the OopMap to be available to the LIR and also to be
182 // reemited for different pcs using the same CodeEmitInfo without recomputing
183 // everything.
184 //
186 class IRScopeDebugInfo: public CompilationResourceObj {
187 private:
188 IRScope* _scope;
189 int _bci;
190 GrowableArray<ScopeValue*>* _locals;
191 GrowableArray<ScopeValue*>* _expressions;
192 GrowableArray<MonitorValue*>* _monitors;
193 IRScopeDebugInfo* _caller;
195 public:
196 IRScopeDebugInfo(IRScope* scope,
197 int bci,
198 GrowableArray<ScopeValue*>* locals,
199 GrowableArray<ScopeValue*>* expressions,
200 GrowableArray<MonitorValue*>* monitors,
201 IRScopeDebugInfo* caller):
202 _scope(scope)
203 , _locals(locals)
204 , _bci(bci)
205 , _expressions(expressions)
206 , _monitors(monitors)
207 , _caller(caller) {}
210 IRScope* scope() { return _scope; }
211 int bci() { return _bci; }
212 GrowableArray<ScopeValue*>* locals() { return _locals; }
213 GrowableArray<ScopeValue*>* expressions() { return _expressions; }
214 GrowableArray<MonitorValue*>* monitors() { return _monitors; }
215 IRScopeDebugInfo* caller() { return _caller; }
217 //Whether we should reexecute this bytecode for deopt
218 bool should_reexecute();
220 void record_debug_info(DebugInformationRecorder* recorder, int pc_offset, bool topmost, bool is_method_handle_invoke = false) {
221 if (caller() != NULL) {
222 // Order is significant: Must record caller first.
223 caller()->record_debug_info(recorder, pc_offset, false/*topmost*/);
224 }
225 DebugToken* locvals = recorder->create_scope_values(locals());
226 DebugToken* expvals = recorder->create_scope_values(expressions());
227 DebugToken* monvals = recorder->create_monitor_values(monitors());
228 // reexecute allowed only for the topmost frame
229 bool reexecute = topmost ? should_reexecute() : false;
230 bool return_oop = false; // This flag will be ignored since it used only for C2 with escape analysis.
231 recorder->describe_scope(pc_offset, scope()->method(), bci(), reexecute, is_method_handle_invoke, return_oop, locvals, expvals, monvals);
232 }
233 };
236 class CodeEmitInfo: public CompilationResourceObj {
237 friend class LinearScan;
238 private:
239 IRScopeDebugInfo* _scope_debug_info;
240 IRScope* _scope;
241 XHandlers* _exception_handlers;
242 OopMap* _oop_map;
243 ValueStack* _stack; // used by deoptimization (contains also monitors
244 bool _is_method_handle_invoke; // true if the associated call site is a MethodHandle call site.
246 FrameMap* frame_map() const { return scope()->compilation()->frame_map(); }
247 Compilation* compilation() const { return scope()->compilation(); }
249 public:
251 // use scope from ValueStack
252 CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers);
254 // make a copy
255 CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack = NULL);
257 // accessors
258 OopMap* oop_map() { return _oop_map; }
259 ciMethod* method() const { return _scope->method(); }
260 IRScope* scope() const { return _scope; }
261 XHandlers* exception_handlers() const { return _exception_handlers; }
262 ValueStack* stack() const { return _stack; }
264 void add_register_oop(LIR_Opr opr);
265 void record_debug_info(DebugInformationRecorder* recorder, int pc_offset);
267 bool is_method_handle_invoke() const { return _is_method_handle_invoke; }
268 void set_is_method_handle_invoke(bool x) { _is_method_handle_invoke = x; }
269 };
272 class IR: public CompilationResourceObj {
273 private:
274 Compilation* _compilation; // the current compilation
275 IRScope* _top_scope; // the root of the scope hierarchy
276 WordSize _locals_size; // the space required for all locals
277 int _num_loops; // Total number of loops
278 BlockList* _code; // the blocks in code generation order w/ use counts
280 public:
281 // creation
282 IR(Compilation* compilation, ciMethod* method, int osr_bci);
284 // accessors
285 bool is_valid() const { return top_scope()->is_valid(); }
286 Compilation* compilation() const { return _compilation; }
287 IRScope* top_scope() const { return _top_scope; }
288 int number_of_locks() const { return top_scope()->number_of_locks(); }
289 ciMethod* method() const { return top_scope()->method(); }
290 BlockBegin* start() const { return top_scope()->start(); }
291 BlockBegin* std_entry() const { return start()->end()->as_Base()->std_entry(); }
292 BlockBegin* osr_entry() const { return start()->end()->as_Base()->osr_entry(); }
293 WordSize locals_size() const { return _locals_size; }
294 int locals_size_in_words() const { return in_words(_locals_size); }
295 BlockList* code() const { return _code; }
296 int num_loops() const { return _num_loops; }
297 int max_stack() const { return top_scope()->max_stack(); } // expensive
299 // ir manipulation
300 void optimize();
301 void compute_predecessors();
302 void split_critical_edges();
303 void compute_code();
304 void compute_use_counts();
306 // The linear-scan order and the code emission order are equal, but
307 // this may change in future
308 BlockList* linear_scan_order() { assert(_code != NULL, "not computed"); return _code; }
310 // iteration
311 void iterate_preorder (BlockClosure* closure);
312 void iterate_postorder (BlockClosure* closure);
313 void iterate_linear_scan_order(BlockClosure* closure);
315 // debugging
316 static void print(BlockBegin* start, bool cfg_only, bool live_only = false) PRODUCT_RETURN;
317 void print(bool cfg_only, bool live_only = false) PRODUCT_RETURN;
318 void verify() PRODUCT_RETURN;
319 };
322 // Globally do instruction substitution and remove substituted
323 // instructions from the instruction list.
324 //
326 class SubstitutionResolver: public BlockClosure, ValueVisitor {
327 virtual void visit(Value* v);
329 public:
330 SubstitutionResolver(IR* hir) {
331 hir->iterate_preorder(this);
332 }
334 SubstitutionResolver(BlockBegin* block) {
335 block->iterate_preorder(this);
336 }
338 virtual void block_do(BlockBegin* block);
339 };