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 */
24
25 #ifndef SHARE_C1_C1_GRAPHBUILDER_HPP
26 #define SHARE_C1_C1_GRAPHBUILDER_HPP
27
28 #include "c1/c1_IR.hpp"
29 #include "c1/c1_Instruction.hpp"
30 #include "c1/c1_ValueMap.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciMethodData.hpp"
33 #include "ci/ciStreams.hpp"
34 #include "compiler/compileLog.hpp"
35
36 class MemoryBuffer;
37
38 class GraphBuilder {
39 private:
40 // Per-scope data. These are pushed and popped as we descend into
41 // inlined methods. Currently in order to generate good code in the
42 // inliner we have to attempt to inline methods directly into the
43 // basic block we are parsing; this adds complexity.
44 class ScopeData: public CompilationResourceObj {
45 private:
46 ScopeData* _parent;
47 // bci-to-block mapping
48 BlockList* _bci2block;
49 // Scope
50 IRScope* _scope;
51 // Whether this scope or any parent scope has exception handlers
52 bool _has_handler;
53 // The bytecodes
54 ciBytecodeStream* _stream;
55
56 // Work list
57 BlockList* _work_list;
174 // for all GraphBuilders
175 static bool _can_trap[Bytecodes::number_of_java_codes];
176
177 // for each instance of GraphBuilder
178 ScopeData* _scope_data; // Per-scope data; used for inlining
179 Compilation* _compilation; // the current compilation
180 ValueMap* _vmap; // the map of values encountered (for CSE)
181 MemoryBuffer* _memory;
182 const char* _inline_bailout_msg; // non-null if most recent inline attempt failed
183 int _instruction_count; // for bailing out in pathological jsr/ret cases
184 BlockBegin* _start; // the start block
185 BlockBegin* _osr_entry; // the osr entry block block
186 ValueStack* _initial_state; // The state for the start block
187
188 // for each call to connect_to_end; can also be set by inliner
189 BlockBegin* _block; // the current block
190 ValueStack* _state; // the current execution state
191 Instruction* _last; // the last instruction added
192 bool _skip_block; // skip processing of the rest of this block
193
194 // accessors
195 ScopeData* scope_data() const { return _scope_data; }
196 Compilation* compilation() const { return _compilation; }
197 BlockList* bci2block() const { return scope_data()->bci2block(); }
198 ValueMap* vmap() const { assert(UseLocalValueNumbering, "should not access otherwise"); return _vmap; }
199 bool has_handler() const { return scope_data()->has_handler(); }
200
201 BlockBegin* block() const { return _block; }
202 ValueStack* state() const { return _state; }
203 void set_state(ValueStack* state) { _state = state; }
204 IRScope* scope() const { return scope_data()->scope(); }
205 ciMethod* method() const { return scope()->method(); }
206 ciBytecodeStream* stream() const { return scope_data()->stream(); }
207 Instruction* last() const { return _last; }
208 Bytecodes::Code code() const { return stream()->cur_bc(); }
209 int bci() const { return stream()->cur_bci(); }
210 int next_bci() const { return stream()->next_bci(); }
211
212 // unified bailout support
213 void bailout(const char* msg) const { compilation()->bailout(msg); }
214 bool bailed_out() const { return compilation()->bailed_out(); }
215
216 // stack manipulation helpers
217 void ipush(Value t) const { state()->ipush(t); }
218 void lpush(Value t) const { state()->lpush(t); }
219 void fpush(Value t) const { state()->fpush(t); }
220 void dpush(Value t) const { state()->dpush(t); }
221 void apush(Value t) const { state()->apush(t); }
222 void push(ValueType* type, Value t) const { state()-> push(type, t); }
223
224 Value ipop() { return state()->ipop(); }
225 Value lpop() { return state()->lpop(); }
226 Value fpop() { return state()->fpop(); }
227 Value dpop() { return state()->dpop(); }
228 Value apop() { return state()->apop(); }
229 Value pop(ValueType* type) { return state()-> pop(type); }
230
250 void if_same(ValueType* type, If::Condition cond);
251 void jsr(int dest);
252 void ret(int local_index);
253 void table_switch();
254 void lookup_switch();
255 void method_return(Value x, bool ignore_return = false);
256 void call_register_finalizer();
257 void access_field(Bytecodes::Code code);
258 void invoke(Bytecodes::Code code);
259 void new_instance(int klass_index);
260 void new_type_array();
261 void new_object_array();
262 void check_cast(int klass_index);
263 void instance_of(int klass_index);
264 void monitorenter(Value x, int bci);
265 void monitorexit(Value x, int bci);
266 void new_multi_array(int dimensions);
267 void throw_op(int bci);
268 Value round_fp(Value fp_value);
269
270 // stack/code manipulation helpers
271 Instruction* append_with_bci(Instruction* instr, int bci);
272 Instruction* append(Instruction* instr);
273 Instruction* append_split(StateSplit* instr);
274
275 // other helpers
276 BlockBegin* block_at(int bci) { return scope_data()->block_at(bci); }
277 XHandlers* handle_exception(Instruction* instruction);
278 void connect_to_end(BlockBegin* beg);
279 void null_check(Value value);
280 void eliminate_redundant_phis(BlockBegin* start);
281 BlockEnd* iterate_bytecodes_for_block(int bci);
282 void iterate_all_blocks(bool start_in_current_block_for_inlining = false);
283 Dependencies* dependency_recorder() const; // = compilation()->dependencies()
284 bool direct_compare(ciKlass* k);
285 Value make_constant(ciConstant value, ciField* field);
286
287 void kill_all();
288
289 // use of state copy routines (try to minimize unnecessary state
343 int recursive_inline_level(ciMethod* callee) const;
344
345 // inlining of synchronized methods
346 void inline_sync_entry(Value lock, BlockBegin* sync_handler);
347 void fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler = false);
348
349 void build_graph_for_intrinsic(ciMethod* callee, bool ignore_return);
350
351 // inliners
352 bool try_inline( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);
353 bool try_inline_intrinsics(ciMethod* callee, bool ignore_return = false);
354 bool try_inline_full( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);
355 bool try_inline_jsr(int jsr_dest_bci);
356
357 const char* check_can_parse(ciMethod* callee) const;
358 const char* should_not_inline(ciMethod* callee) const;
359
360 // JSR 292 support
361 bool try_method_handle_inline(ciMethod* callee, bool ignore_return);
362
363 // helpers
364 void inline_bailout(const char* msg);
365 BlockBegin* header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state);
366 BlockBegin* setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* init_state);
367 void setup_osr_entry_block();
368 void clear_inline_bailout();
369 ValueStack* state_at_entry();
370 void push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start);
371 void push_scope(ciMethod* callee, BlockBegin* continuation);
372 void push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci);
373 void pop_scope();
374 void pop_scope_for_jsr();
375
376 void append_unsafe_get(ciMethod* callee, BasicType t, bool is_volatile);
377 void append_unsafe_put(ciMethod* callee, BasicType t, bool is_volatile);
378 void append_unsafe_CAS(ciMethod* callee);
379 void append_unsafe_get_and_set(ciMethod* callee, bool is_add);
380 void append_char_access(ciMethod* callee, bool is_store);
381
382 void print_inlining(ciMethod* callee, const char* msg, bool success = true);
383
384 void profile_call(ciMethod* callee, Value recv, ciKlass* predicted_holder, Values* obj_args, bool inlined);
385 void profile_return_type(Value ret, ciMethod* callee, ciMethod* m = NULL, int bci = -1);
386 void profile_invocation(ciMethod* inlinee, ValueStack* state);
387
388 // Shortcuts to profiling control.
389 bool is_profiling() { return _compilation->is_profiling(); }
390 bool profile_branches() { return _compilation->profile_branches(); }
391 bool profile_calls() { return _compilation->profile_calls(); }
392 bool profile_inlined_calls() { return _compilation->profile_inlined_calls(); }
393 bool profile_checkcasts() { return _compilation->profile_checkcasts(); }
394 bool profile_parameters() { return _compilation->profile_parameters(); }
395 bool profile_arguments() { return _compilation->profile_arguments(); }
396 bool profile_return() { return _compilation->profile_return(); }
397
398 Values* args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver);
399 Values* collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver);
400 void check_args_for_profiling(Values* obj_args, int expected);
401
402 public:
403 NOT_PRODUCT(void print_stats();)
404
405 // initialization
406 static void initialize();
407
408 // public
409 static bool can_trap(ciMethod* method, Bytecodes::Code code) {
410 assert(0 <= code && code < Bytecodes::number_of_java_codes, "illegal bytecode");
411 if (_can_trap[code]) return true;
412 // special handling for finalizer registration
413 return code == Bytecodes::_return && method->intrinsic_id() == vmIntrinsics::_Object_init;
414 }
415
416 // creation
|
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 */
24
25 #ifndef SHARE_C1_C1_GRAPHBUILDER_HPP
26 #define SHARE_C1_C1_GRAPHBUILDER_HPP
27
28 #include "c1/c1_IR.hpp"
29 #include "c1/c1_Instruction.hpp"
30 #include "c1/c1_ValueMap.hpp"
31 #include "c1/c1_ValueStack.hpp"
32 #include "ci/ciMethodData.hpp"
33 #include "ci/ciStreams.hpp"
34 #include "compiler/compileLog.hpp"
35
36 class MemoryBuffer;
37
38 class DelayedFieldAccess : public CompilationResourceObj {
39 private:
40 Value _obj;
41 ciInstanceKlass* _holder;
42 int _offset;
43 public:
44 DelayedFieldAccess(Value obj, ciInstanceKlass* holder, int offset)
45 : _obj(obj), _holder(holder) , _offset(offset) { }
46
47 Value obj() const { return _obj; }
48 ciInstanceKlass* holder() const { return _holder; }
49 int offset() const { return _offset; }
50 void inc_offset(int offset) { _offset += offset; }
51 };
52
53 class GraphBuilder {
54 private:
55 // Per-scope data. These are pushed and popped as we descend into
56 // inlined methods. Currently in order to generate good code in the
57 // inliner we have to attempt to inline methods directly into the
58 // basic block we are parsing; this adds complexity.
59 class ScopeData: public CompilationResourceObj {
60 private:
61 ScopeData* _parent;
62 // bci-to-block mapping
63 BlockList* _bci2block;
64 // Scope
65 IRScope* _scope;
66 // Whether this scope or any parent scope has exception handlers
67 bool _has_handler;
68 // The bytecodes
69 ciBytecodeStream* _stream;
70
71 // Work list
72 BlockList* _work_list;
189 // for all GraphBuilders
190 static bool _can_trap[Bytecodes::number_of_java_codes];
191
192 // for each instance of GraphBuilder
193 ScopeData* _scope_data; // Per-scope data; used for inlining
194 Compilation* _compilation; // the current compilation
195 ValueMap* _vmap; // the map of values encountered (for CSE)
196 MemoryBuffer* _memory;
197 const char* _inline_bailout_msg; // non-null if most recent inline attempt failed
198 int _instruction_count; // for bailing out in pathological jsr/ret cases
199 BlockBegin* _start; // the start block
200 BlockBegin* _osr_entry; // the osr entry block block
201 ValueStack* _initial_state; // The state for the start block
202
203 // for each call to connect_to_end; can also be set by inliner
204 BlockBegin* _block; // the current block
205 ValueStack* _state; // the current execution state
206 Instruction* _last; // the last instruction added
207 bool _skip_block; // skip processing of the rest of this block
208
209 // support for optimization of accesses to flattened fields and arrays
210 DelayedFieldAccess* _pending_field_access;
211 DelayedLoadIndexed* _pending_load_indexed;
212
213 // accessors
214 ScopeData* scope_data() const { return _scope_data; }
215 Compilation* compilation() const { return _compilation; }
216 BlockList* bci2block() const { return scope_data()->bci2block(); }
217 ValueMap* vmap() const { assert(UseLocalValueNumbering, "should not access otherwise"); return _vmap; }
218 bool has_handler() const { return scope_data()->has_handler(); }
219
220 BlockBegin* block() const { return _block; }
221 ValueStack* state() const { return _state; }
222 void set_state(ValueStack* state) { _state = state; }
223 IRScope* scope() const { return scope_data()->scope(); }
224 ciMethod* method() const { return scope()->method(); }
225 ciBytecodeStream* stream() const { return scope_data()->stream(); }
226 Instruction* last() const { return _last; }
227 Bytecodes::Code code() const { return stream()->cur_bc(); }
228 int bci() const { return stream()->cur_bci(); }
229 int next_bci() const { return stream()->next_bci(); }
230 bool has_pending_field_access() { return _pending_field_access != NULL; }
231 DelayedFieldAccess* pending_field_access() { return _pending_field_access; }
232 void set_pending_field_access(DelayedFieldAccess* delayed) { _pending_field_access = delayed; }
233 bool has_pending_load_indexed() { return _pending_load_indexed != NULL; }
234 DelayedLoadIndexed* pending_load_indexed() { return _pending_load_indexed; }
235 void set_pending_load_indexed(DelayedLoadIndexed* delayed) { _pending_load_indexed = delayed; }
236
237 // unified bailout support
238 void bailout(const char* msg) const { compilation()->bailout(msg); }
239 bool bailed_out() const { return compilation()->bailed_out(); }
240
241 // stack manipulation helpers
242 void ipush(Value t) const { state()->ipush(t); }
243 void lpush(Value t) const { state()->lpush(t); }
244 void fpush(Value t) const { state()->fpush(t); }
245 void dpush(Value t) const { state()->dpush(t); }
246 void apush(Value t) const { state()->apush(t); }
247 void push(ValueType* type, Value t) const { state()-> push(type, t); }
248
249 Value ipop() { return state()->ipop(); }
250 Value lpop() { return state()->lpop(); }
251 Value fpop() { return state()->fpop(); }
252 Value dpop() { return state()->dpop(); }
253 Value apop() { return state()->apop(); }
254 Value pop(ValueType* type) { return state()-> pop(type); }
255
275 void if_same(ValueType* type, If::Condition cond);
276 void jsr(int dest);
277 void ret(int local_index);
278 void table_switch();
279 void lookup_switch();
280 void method_return(Value x, bool ignore_return = false);
281 void call_register_finalizer();
282 void access_field(Bytecodes::Code code);
283 void invoke(Bytecodes::Code code);
284 void new_instance(int klass_index);
285 void new_type_array();
286 void new_object_array();
287 void check_cast(int klass_index);
288 void instance_of(int klass_index);
289 void monitorenter(Value x, int bci);
290 void monitorexit(Value x, int bci);
291 void new_multi_array(int dimensions);
292 void throw_op(int bci);
293 Value round_fp(Value fp_value);
294
295 // inline types
296 void default_value(int klass_index);
297 void withfield(int field_index);
298 void copy_inline_content(ciInlineKlass* vk, Value src, int src_off, Value dest, int dest_off, ValueStack* state_before, ciField* encloding_field = NULL);
299
300 // stack/code manipulation helpers
301 Instruction* append_with_bci(Instruction* instr, int bci);
302 Instruction* append(Instruction* instr);
303 Instruction* append_split(StateSplit* instr);
304
305 // other helpers
306 BlockBegin* block_at(int bci) { return scope_data()->block_at(bci); }
307 XHandlers* handle_exception(Instruction* instruction);
308 void connect_to_end(BlockBegin* beg);
309 void null_check(Value value);
310 void eliminate_redundant_phis(BlockBegin* start);
311 BlockEnd* iterate_bytecodes_for_block(int bci);
312 void iterate_all_blocks(bool start_in_current_block_for_inlining = false);
313 Dependencies* dependency_recorder() const; // = compilation()->dependencies()
314 bool direct_compare(ciKlass* k);
315 Value make_constant(ciConstant value, ciField* field);
316
317 void kill_all();
318
319 // use of state copy routines (try to minimize unnecessary state
373 int recursive_inline_level(ciMethod* callee) const;
374
375 // inlining of synchronized methods
376 void inline_sync_entry(Value lock, BlockBegin* sync_handler);
377 void fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler = false);
378
379 void build_graph_for_intrinsic(ciMethod* callee, bool ignore_return);
380
381 // inliners
382 bool try_inline( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);
383 bool try_inline_intrinsics(ciMethod* callee, bool ignore_return = false);
384 bool try_inline_full( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);
385 bool try_inline_jsr(int jsr_dest_bci);
386
387 const char* check_can_parse(ciMethod* callee) const;
388 const char* should_not_inline(ciMethod* callee) const;
389
390 // JSR 292 support
391 bool try_method_handle_inline(ciMethod* callee, bool ignore_return);
392
393 // Inline type support
394 void update_larval_state(Value v) {
395 if (v != NULL && v->as_NewInlineTypeInstance() != NULL) {
396 v->as_NewInlineTypeInstance()->set_not_larva_anymore();
397 }
398 }
399 void update_larva_stack_count(Value v) {
400 if (v != NULL && v->as_NewInlineTypeInstance() != NULL &&
401 v->as_NewInlineTypeInstance()->in_larval_state()) {
402 v->as_NewInlineTypeInstance()->decrement_on_stack_count();
403 }
404 }
405
406 // helpers
407 void inline_bailout(const char* msg);
408 BlockBegin* header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state);
409 BlockBegin* setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* init_state);
410 void setup_osr_entry_block();
411 void clear_inline_bailout();
412 ValueStack* state_at_entry();
413 void push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start);
414 void push_scope(ciMethod* callee, BlockBegin* continuation);
415 void push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci);
416 void pop_scope();
417 void pop_scope_for_jsr();
418
419 void append_unsafe_get(ciMethod* callee, BasicType t, bool is_volatile);
420 void append_unsafe_put(ciMethod* callee, BasicType t, bool is_volatile);
421 void append_unsafe_CAS(ciMethod* callee);
422 void append_unsafe_get_and_set(ciMethod* callee, bool is_add);
423 void append_char_access(ciMethod* callee, bool is_store);
424
425 void print_inlining(ciMethod* callee, const char* msg, bool success = true);
426
427 void profile_call(ciMethod* callee, Value recv, ciKlass* predicted_holder, Values* obj_args, bool inlined);
428 void profile_return_type(Value ret, ciMethod* callee, ciMethod* m = NULL, int bci = -1);
429 void profile_invocation(ciMethod* inlinee, ValueStack* state);
430
431 // Shortcuts to profiling control.
432 bool is_profiling() { return _compilation->is_profiling(); }
433 bool profile_branches() { return _compilation->profile_branches(); }
434 bool profile_calls() { return _compilation->profile_calls(); }
435 bool profile_inlined_calls() { return _compilation->profile_inlined_calls(); }
436 bool profile_checkcasts() { return _compilation->profile_checkcasts(); }
437 bool profile_parameters() { return _compilation->profile_parameters(); }
438 bool profile_arguments() { return _compilation->profile_arguments(); }
439 bool profile_return() { return _compilation->profile_return(); }
440 bool profile_array_accesses(){ return _compilation->profile_array_accesses();}
441
442 Values* args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver);
443 Values* collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver);
444 void check_args_for_profiling(Values* obj_args, int expected);
445
446 public:
447 NOT_PRODUCT(void print_stats();)
448
449 // initialization
450 static void initialize();
451
452 // public
453 static bool can_trap(ciMethod* method, Bytecodes::Code code) {
454 assert(0 <= code && code < Bytecodes::number_of_java_codes, "illegal bytecode");
455 if (_can_trap[code]) return true;
456 // special handling for finalizer registration
457 return code == Bytecodes::_return && method->intrinsic_id() == vmIntrinsics::_Object_init;
458 }
459
460 // creation
|