1 /* 2 * Copyright (c) 1997, 2011, 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 */ 24 25 #ifndef SHARE_VM_OPTO_PARSE_HPP 26 #define SHARE_VM_OPTO_PARSE_HPP 27 28 #include "ci/ciMethodData.hpp" 29 #include "ci/ciTypeFlow.hpp" 30 #include "compiler/methodLiveness.hpp" 31 #include "libadt/vectset.hpp" 32 #include "oops/generateOopMap.hpp" 33 #include "opto/graphKit.hpp" 34 #include "opto/subnode.hpp" 35 36 class BytecodeParseHistogram; 37 class InlineTree; 38 class Parse; 39 class SwitchRange; 40 41 42 //------------------------------InlineTree------------------------------------- 43 class InlineTree : public ResourceObj { 44 Compile* C; // cache 45 JVMState* _caller_jvms; // state of caller 46 ciMethod* _method; // method being called by the caller_jvms 47 InlineTree* _caller_tree; 48 uint _count_inline_bcs; // Accumulated count of inlined bytecodes 49 // Call-site count / interpreter invocation count, scaled recursively. 50 // Always between 0.0 and 1.0. Represents the percentage of the method's 51 // total execution time used at this call site. 52 const float _site_invoke_ratio; 53 const int _max_inline_level; // the maximum inline level for this sub-tree (may be adjusted) 54 float compute_callee_frequency( int caller_bci ) const; 55 56 GrowableArray<InlineTree*> _subtrees; 57 friend class Compile; 58 59 protected: 60 InlineTree(Compile* C, 61 const InlineTree* caller_tree, 62 ciMethod* callee_method, 63 JVMState* caller_jvms, 64 int caller_bci, 65 float site_invoke_ratio, 66 int max_inline_level); 67 InlineTree *build_inline_tree_for_callee(ciMethod* callee_method, 68 JVMState* caller_jvms, 69 int caller_bci); 70 const char* try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result); 71 const char* should_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const; 72 const char* should_not_inline(ciMethod* callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const; 73 void print_inlining(ciMethod *callee_method, int caller_bci, const char *failure_msg) const; 74 75 InlineTree *caller_tree() const { return _caller_tree; } 76 InlineTree* callee_at(int bci, ciMethod* m) const; 77 int inline_level() const { return stack_depth(); } 78 int stack_depth() const { return _caller_jvms ? _caller_jvms->depth() : 0; } 79 80 public: 81 static const char* check_can_parse(ciMethod* callee); 82 83 static InlineTree* build_inline_tree_root(); 84 static InlineTree* find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found = false); 85 86 // For temporary (stack-allocated, stateless) ilts: 87 InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms, float site_invoke_ratio, int max_inline_level); 88 89 // InlineTree enum 90 enum InlineStyle { 91 Inline_do_not_inline = 0, // 92 Inline_cha_is_monomorphic = 1, // 93 Inline_type_profile_monomorphic = 2 // 94 }; 95 96 // See if it is OK to inline. 97 // The receiver is the inline tree for the caller. 98 // 99 // The result is a temperature indication. If it is hot or cold, 100 // inlining is immediate or undesirable. Otherwise, the info block 101 // returned is newly allocated and may be enqueued. 102 // 103 // If the method is inlinable, a new inline subtree is created on the fly, 104 // and may be accessed by find_subtree_from_root. 105 // The call_method is the dest_method for a special or static invocation. 106 // The call_method is an optimized virtual method candidate otherwise. 107 WarmCallInfo* ok_to_inline(ciMethod *call_method, JVMState* caller_jvms, ciCallProfile& profile, WarmCallInfo* wci); 108 109 // Information about inlined method 110 JVMState* caller_jvms() const { return _caller_jvms; } 111 ciMethod *method() const { return _method; } 112 int caller_bci() const { return _caller_jvms ? _caller_jvms->bci() : InvocationEntryBci; } 113 uint count_inline_bcs() const { return _count_inline_bcs; } 114 float site_invoke_ratio() const { return _site_invoke_ratio; }; 115 116 #ifndef PRODUCT 117 private: 118 uint _count_inlines; // Count of inlined methods 119 public: 120 // Debug information collected during parse 121 uint count_inlines() const { return _count_inlines; }; 122 #endif 123 GrowableArray<InlineTree*> subtrees() { return _subtrees; } 124 }; 125 126 127 //----------------------------------------------------------------------------- 128 //------------------------------Parse------------------------------------------ 129 // Parse bytecodes, build a Graph 130 class Parse : public GraphKit { 131 public: 132 // Per-block information needed by the parser: 133 class Block { 134 private: 135 ciTypeFlow::Block* _flow; 136 int _pred_count; // how many predecessors in CFG? 137 int _preds_parsed; // how many of these have been parsed? 138 uint _count; // how many times executed? Currently only set by _goto's 139 bool _is_parsed; // has this block been parsed yet? 140 bool _is_handler; // is this block an exception handler? 141 bool _has_merged_backedge; // does this block have merged backedge? 142 SafePointNode* _start_map; // all values flowing into this block 143 MethodLivenessResult _live_locals; // lazily initialized liveness bitmap 144 145 int _num_successors; // Includes only normal control flow. 146 int _all_successors; // Include exception paths also. 147 Block** _successors; 148 149 // Use init_node/init_graph to initialize Blocks. 150 // Block() : _live_locals((uintptr_t*)NULL,0) { ShouldNotReachHere(); } 151 Block() : _live_locals(NULL,0) { ShouldNotReachHere(); } 152 153 public: 154 155 // Set up the block data structure itself. 156 void init_node(Parse* outer, int po); 157 // Set up the block's relations to other blocks. 158 void init_graph(Parse* outer); 159 160 ciTypeFlow::Block* flow() const { return _flow; } 161 int pred_count() const { return _pred_count; } 162 int preds_parsed() const { return _preds_parsed; } 163 bool is_parsed() const { return _is_parsed; } 164 bool is_handler() const { return _is_handler; } 165 void set_count( uint x ) { _count = x; } 166 uint count() const { return _count; } 167 168 SafePointNode* start_map() const { assert(is_merged(),""); return _start_map; } 169 void set_start_map(SafePointNode* m) { assert(!is_merged(), ""); _start_map = m; } 170 171 // True after any predecessor flows control into this block 172 bool is_merged() const { return _start_map != NULL; } 173 174 #ifdef ASSERT 175 // True after backedge predecessor flows control into this block 176 bool has_merged_backedge() const { return _has_merged_backedge; } 177 void mark_merged_backedge(Block* pred) { 178 assert(is_SEL_head(), "should be loop head"); 179 if (pred != NULL && is_SEL_backedge(pred)) { 180 assert(is_parsed(), "block should be parsed before merging backedges"); 181 _has_merged_backedge = true; 182 } 183 } 184 #endif 185 186 // True when all non-exception predecessors have been parsed. 187 bool is_ready() const { return preds_parsed() == pred_count(); } 188 189 int num_successors() const { return _num_successors; } 190 int all_successors() const { return _all_successors; } 191 Block* successor_at(int i) const { 192 assert((uint)i < (uint)all_successors(), ""); 193 return _successors[i]; 194 } 195 Block* successor_for_bci(int bci); 196 197 int start() const { return flow()->start(); } 198 int limit() const { return flow()->limit(); } 199 int rpo() const { return flow()->rpo(); } 200 int start_sp() const { return flow()->stack_size(); } 201 202 bool is_loop_head() const { return flow()->is_loop_head(); } 203 bool is_SEL_head() const { return flow()->is_single_entry_loop_head(); } 204 bool is_SEL_backedge(Block* pred) const{ return is_SEL_head() && pred->rpo() >= rpo(); } 205 bool is_invariant_local(uint i) const { 206 const JVMState* jvms = start_map()->jvms(); 207 if (!jvms->is_loc(i) || flow()->outer()->has_irreducible_entry()) return false; 208 return flow()->is_invariant_local(i - jvms->locoff()); 209 } 210 bool can_elide_SEL_phi(uint i) const { assert(is_SEL_head(),""); return is_invariant_local(i); } 211 212 const Type* peek(int off=0) const { return stack_type_at(start_sp() - (off+1)); } 213 214 const Type* stack_type_at(int i) const; 215 const Type* local_type_at(int i) const; 216 static const Type* get_type(ciType* t) { return Type::get_typeflow_type(t); } 217 218 bool has_trap_at(int bci) const { return flow()->has_trap() && flow()->trap_bci() == bci; } 219 220 // Call this just before parsing a block. 221 void mark_parsed() { 222 assert(!_is_parsed, "must parse each block exactly once"); 223 _is_parsed = true; 224 } 225 226 // Return the phi/region input index for the "current" pred, 227 // and bump the pred number. For historical reasons these index 228 // numbers are handed out in descending order. The last index is 229 // always PhiNode::Input (i.e., 1). The value returned is known 230 // as a "path number" because it distinguishes by which path we are 231 // entering the block. 232 int next_path_num() { 233 assert(preds_parsed() < pred_count(), "too many preds?"); 234 return pred_count() - _preds_parsed++; 235 } 236 237 // Add a previously unaccounted predecessor to this block. 238 // This operates by increasing the size of the block's region 239 // and all its phi nodes (if any). The value returned is a 240 // path number ("pnum"). 241 int add_new_path(); 242 243 // Initialize me by recording the parser's map. My own map must be NULL. 244 void record_state(Parse* outer); 245 }; 246 247 #ifndef PRODUCT 248 // BytecodeParseHistogram collects number of bytecodes parsed, nodes constructed, and transformations. 249 class BytecodeParseHistogram : public ResourceObj { 250 private: 251 enum BPHType { 252 BPH_transforms, 253 BPH_values 254 }; 255 static bool _initialized; 256 static uint _bytecodes_parsed [Bytecodes::number_of_codes]; 257 static uint _nodes_constructed[Bytecodes::number_of_codes]; 258 static uint _nodes_transformed[Bytecodes::number_of_codes]; 259 static uint _new_values [Bytecodes::number_of_codes]; 260 261 Bytecodes::Code _initial_bytecode; 262 int _initial_node_count; 263 int _initial_transforms; 264 int _initial_values; 265 266 Parse *_parser; 267 Compile *_compiler; 268 269 // Initialization 270 static void reset(); 271 272 // Return info being collected, select with global flag 'BytecodeParseInfo' 273 int current_count(BPHType info_selector); 274 275 public: 276 BytecodeParseHistogram(Parse *p, Compile *c); 277 static bool initialized(); 278 279 // Record info when starting to parse one bytecode 280 void set_initial_state( Bytecodes::Code bc ); 281 // Record results of parsing one bytecode 282 void record_change(); 283 284 // Profile printing 285 static void print(float cutoff = 0.01F); // cutoff in percent 286 }; 287 288 public: 289 // Record work done during parsing 290 BytecodeParseHistogram* _parse_histogram; 291 void set_parse_histogram(BytecodeParseHistogram *bph) { _parse_histogram = bph; } 292 BytecodeParseHistogram* parse_histogram() { return _parse_histogram; } 293 #endif 294 295 private: 296 friend class Block; 297 298 // Variables which characterize this compilation as a whole: 299 300 JVMState* _caller; // JVMS which carries incoming args & state. 301 float _expected_uses; // expected number of calls to this code 302 float _prof_factor; // discount applied to my profile counts 303 int _depth; // Inline tree depth, for debug printouts 304 const TypeFunc*_tf; // My kind of function type 305 int _entry_bci; // the osr bci or InvocationEntryBci 306 307 ciTypeFlow* _flow; // Results of previous flow pass. 308 Block* _blocks; // Array of basic-block structs. 309 int _block_count; // Number of elements in _blocks. 310 311 GraphKit _exits; // Record all normal returns and throws here. 312 bool _wrote_final; // Did we write a final field? 313 bool _count_invocations; // update and test invocation counter 314 bool _method_data_update; // update method data oop 315 316 // Variables which track Java semantics during bytecode parsing: 317 318 Block* _block; // block currently getting parsed 319 ciBytecodeStream _iter; // stream of this method's bytecodes 320 321 int _blocks_merged; // Progress meter: state merges from BB preds 322 int _blocks_parsed; // Progress meter: BBs actually parsed 323 324 const FastLockNode* _synch_lock; // FastLockNode for synchronized method 325 326 #ifndef PRODUCT 327 int _max_switch_depth; // Debugging SwitchRanges. 328 int _est_switch_depth; // Debugging SwitchRanges. 329 #endif 330 331 public: 332 // Constructor 333 Parse(JVMState* caller, ciMethod* parse_method, float expected_uses); 334 335 virtual Parse* is_Parse() const { return (Parse*)this; } 336 337 public: 338 // Accessors. 339 JVMState* caller() const { return _caller; } 340 float expected_uses() const { return _expected_uses; } 341 float prof_factor() const { return _prof_factor; } 342 int depth() const { return _depth; } 343 const TypeFunc* tf() const { return _tf; } 344 // entry_bci() -- see osr_bci, etc. 345 346 ciTypeFlow* flow() const { return _flow; } 347 // blocks() -- see rpo_at, start_block, etc. 348 int block_count() const { return _block_count; } 349 350 GraphKit& exits() { return _exits; } 351 bool wrote_final() const { return _wrote_final; } 352 void set_wrote_final(bool z) { _wrote_final = z; } 353 bool count_invocations() const { return _count_invocations; } 354 bool method_data_update() const { return _method_data_update; } 355 356 Block* block() const { return _block; } 357 ciBytecodeStream& iter() { return _iter; } 358 Bytecodes::Code bc() const { return _iter.cur_bc(); } 359 360 void set_block(Block* b) { _block = b; } 361 362 // Derived accessors: 363 bool is_normal_parse() const { return _entry_bci == InvocationEntryBci; } 364 bool is_osr_parse() const { return _entry_bci != InvocationEntryBci; } 365 int osr_bci() const { assert(is_osr_parse(),""); return _entry_bci; } 366 367 void set_parse_bci(int bci); 368 369 // Must this parse be aborted? 370 bool failing() { return C->failing(); } 371 372 Block* rpo_at(int rpo) { 373 assert(0 <= rpo && rpo < _block_count, "oob"); 374 return &_blocks[rpo]; 375 } 376 Block* start_block() { 377 return rpo_at(flow()->start_block()->rpo()); 378 } 379 // Can return NULL if the flow pass did not complete a block. 380 Block* successor_for_bci(int bci) { 381 return block()->successor_for_bci(bci); 382 } 383 384 private: 385 // Create a JVMS & map for the initial state of this method. 386 SafePointNode* create_entry_map(); 387 388 // OSR helpers 389 Node *fetch_interpreter_state(int index, BasicType bt, Node *local_addrs, Node *local_addrs_base); 390 Node* check_interpreter_type(Node* l, const Type* type, SafePointNode* &bad_type_exit); 391 void load_interpreter_state(Node* osr_buf); 392 393 // Functions for managing basic blocks: 394 void init_blocks(); 395 void load_state_from(Block* b); 396 void store_state_to(Block* b) { b->record_state(this); } 397 398 // Parse all the basic blocks. 399 void do_all_blocks(); 400 401 // Parse the current basic block 402 void do_one_block(); 403 404 // Raise an error if we get a bad ciTypeFlow CFG. 405 void handle_missing_successor(int bci); 406 407 // first actions (before BCI 0) 408 void do_method_entry(); 409 410 // implementation of monitorenter/monitorexit 411 void do_monitor_enter(); 412 void do_monitor_exit(); 413 414 // Eagerly create phie throughout the state, to cope with back edges. 415 void ensure_phis_everywhere(); 416 417 // Merge the current mapping into the basic block starting at bci 418 void merge( int target_bci); 419 // Same as plain merge, except that it allocates a new path number. 420 void merge_new_path( int target_bci); 421 // Merge the current mapping into an exception handler. 422 void merge_exception(int target_bci); 423 // Helper: Merge the current mapping into the given basic block 424 void merge_common(Block* target, int pnum); 425 // Helper functions for merging individual cells. 426 PhiNode *ensure_phi( int idx, bool nocreate = false); 427 PhiNode *ensure_memory_phi(int idx, bool nocreate = false); 428 // Helper to merge the current memory state into the given basic block 429 void merge_memory_edges(MergeMemNode* n, int pnum, bool nophi); 430 431 // Parse this bytecode, and alter the Parsers JVM->Node mapping 432 void do_one_bytecode(); 433 434 // helper function to generate array store check 435 void array_store_check(); 436 // Helper function to generate array load 437 void array_load(BasicType etype); 438 // Helper function to generate array store 439 void array_store(BasicType etype); 440 // Helper function to compute array addressing 441 Node* array_addressing(BasicType type, int vals, const Type* *result2=NULL); 442 443 // Pass current map to exits 444 void return_current(Node* value); 445 446 // Register finalizers on return from Object.<init> 447 void call_register_finalizer(); 448 449 // Insert a compiler safepoint into the graph 450 void add_safepoint(); 451 452 // Insert a compiler safepoint into the graph, if there is a back-branch. 453 void maybe_add_safepoint(int target_bci) { 454 if (UseLoopSafepoints && target_bci <= bci()) { 455 add_safepoint(); 456 } 457 } 458 459 // Note: Intrinsic generation routines may be found in library_call.cpp. 460 461 // Helper function to setup Ideal Call nodes 462 void do_call(); 463 464 // Helper function to uncommon-trap or bailout for non-compilable call-sites 465 bool can_not_compile_call_site(ciMethod *dest_method, ciInstanceKlass *klass); 466 467 // Helper function to identify inlining potential at call-site 468 ciMethod* optimize_inlining(ciMethod* caller, int bci, ciInstanceKlass* klass, 469 ciMethod *dest_method, const TypeOopPtr* receiver_type); 470 471 // Helper function to setup for type-profile based inlining 472 bool prepare_type_profile_inline(ciInstanceKlass* prof_klass, ciMethod* prof_method); 473 474 // Helper functions for type checking bytecodes: 475 void do_checkcast(); 476 void do_instanceof(); 477 478 // Helper functions for shifting & arithmetic 479 void modf(); 480 void modd(); 481 void l2f(); 482 483 void do_irem(); 484 485 // implementation of _get* and _put* bytecodes 486 void do_getstatic() { do_field_access(true, false); } 487 void do_getfield () { do_field_access(true, true); } 488 void do_putstatic() { do_field_access(false, false); } 489 void do_putfield () { do_field_access(false, true); } 490 491 // common code for making initial checks and forming addresses 492 void do_field_access(bool is_get, bool is_field); 493 bool static_field_ok_in_clinit(ciField *field, ciMethod *method); 494 495 // common code for actually performing the load or store 496 void do_get_xxx(Node* obj, ciField* field, bool is_field); 497 void do_put_xxx(Node* obj, ciField* field, bool is_field); 498 499 // loading from a constant field or the constant pool 500 // returns false if push failed (non-perm field constants only, not ldcs) 501 bool push_constant(ciConstant con, bool require_constant = false); 502 503 // implementation of object creation bytecodes 504 void emit_guard_for_new(ciInstanceKlass* klass); 505 void do_new(); 506 void do_newarray(BasicType elemtype); 507 void do_anewarray(); 508 void do_multianewarray(); 509 Node* expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs); 510 511 // implementation of jsr/ret 512 void do_jsr(); 513 void do_ret(); 514 515 float dynamic_branch_prediction(float &cnt); 516 float branch_prediction(float &cnt, BoolTest::mask btest, int target_bci); 517 bool seems_never_taken(float prob); 518 bool seems_stable_comparison(BoolTest::mask btest, Node* c); 519 520 void do_ifnull(BoolTest::mask btest, Node* c); 521 void do_if(BoolTest::mask btest, Node* c); 522 int repush_if_args(); 523 void adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, 524 Block* path, Block* other_path); 525 IfNode* jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask); 526 Node* jump_if_join(Node* iffalse, Node* iftrue); 527 void jump_if_true_fork(IfNode *ifNode, int dest_bci_if_true, int prof_table_index); 528 void jump_if_false_fork(IfNode *ifNode, int dest_bci_if_false, int prof_table_index); 529 void jump_if_always_fork(int dest_bci_if_true, int prof_table_index); 530 531 friend class SwitchRange; 532 void do_tableswitch(); 533 void do_lookupswitch(); 534 void jump_switch_ranges(Node* a, SwitchRange* lo, SwitchRange* hi, int depth = 0); 535 bool create_jump_tables(Node* a, SwitchRange* lo, SwitchRange* hi); 536 537 // helper functions for methodData style profiling 538 void test_counter_against_threshold(Node* cnt, int limit); 539 void increment_and_test_invocation_counter(int limit); 540 void test_for_osr_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize offset, int limit); 541 Node* method_data_addressing(ciMethodData* md, ciProfileData* data, ByteSize offset, Node* idx = NULL, uint stride = 0); 542 void increment_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize offset, Node* idx = NULL, uint stride = 0); 543 void set_md_flag_at(ciMethodData* md, ciProfileData* data, int flag_constant); 544 545 void profile_method_entry(); 546 void profile_taken_branch(int target_bci, bool force_update = false); 547 void profile_not_taken_branch(bool force_update = false); 548 void profile_call(Node* receiver); 549 void profile_generic_call(); 550 void profile_receiver_type(Node* receiver); 551 void profile_ret(int target_bci); 552 void profile_null_checkcast(); 553 void profile_switch_case(int table_index); 554 555 // helper function for call statistics 556 void count_compiled_calls(bool at_method_entry, bool is_inline) PRODUCT_RETURN; 557 558 Node_Notes* make_node_notes(Node_Notes* caller_nn); 559 560 // Helper functions for handling normal and abnormal exits. 561 void build_exits(); 562 563 // Fix up all exceptional control flow exiting a single bytecode. 564 void do_exceptions(); 565 566 // Fix up all exiting control flow at the end of the parse. 567 void do_exits(); 568 569 // Add Catch/CatchProjs 570 // The call is either a Java call or the VM's rethrow stub 571 void catch_call_exceptions(ciExceptionHandlerStream&); 572 573 // Handle all exceptions thrown by the inlined method. 574 // Also handles exceptions for individual bytecodes. 575 void catch_inline_exceptions(SafePointNode* ex_map); 576 577 // Merge the given map into correct exceptional exit state. 578 // Assumes that there is no applicable local handler. 579 void throw_to_exit(SafePointNode* ex_map); 580 581 public: 582 #ifndef PRODUCT 583 // Handle PrintOpto, etc. 584 void show_parse_info(); 585 void dump_map_adr_mem() const; 586 static void print_statistics(); // Print some performance counters 587 void dump(); 588 void dump_bci(int bci); 589 #endif 590 }; 591 592 #endif // SHARE_VM_OPTO_PARSE_HPP