1 /* 2 * Copyright (c) 1997, 2017, 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_OOPS_GENERATEOOPMAP_HPP 26 #define SHARE_VM_OOPS_GENERATEOOPMAP_HPP 27 28 #include "interpreter/bytecodeStream.hpp" 29 #include "memory/allocation.hpp" 30 #include "memory/universe.inline.hpp" 31 #include "oops/method.hpp" 32 #include "oops/oopsHierarchy.hpp" 33 #include "runtime/signature.hpp" 34 #include "utilities/bitMap.hpp" 35 36 // Forward definition 37 class GenerateOopMap; 38 class BasicBlock; 39 class CellTypeState; 40 class StackMap; 41 42 // These two should be removed. But requires som code to be cleaned up 43 #define MAXARGSIZE 256 // This should be enough 44 #define MAX_LOCAL_VARS 65536 // 16-bit entry 45 46 typedef void (*jmpFct_t)(GenerateOopMap *c, int bcpDelta, int* data); 47 48 49 // RetTable 50 // 51 // Contains maping between jsr targets and there return addresses. One-to-many mapping 52 // 53 class RetTableEntry : public ResourceObj { 54 private: 55 static int _init_nof_jsrs; // Default size of jsrs list 56 int _target_bci; // Target PC address of jump (bytecode index) 57 GrowableArray<intptr_t> * _jsrs; // List of return addresses (bytecode index) 58 RetTableEntry *_next; // Link to next entry 59 public: 60 RetTableEntry(int target, RetTableEntry *next); 61 62 // Query 63 int target_bci() const { return _target_bci; } 64 int nof_jsrs() const { return _jsrs->length(); } 65 int jsrs(int i) const { assert(i>=0 && i<nof_jsrs(), "Index out of bounds"); return _jsrs->at(i); } 66 67 // Update entry 68 void add_jsr (int return_bci) { _jsrs->append(return_bci); } 69 void add_delta (int bci, int delta); 70 RetTableEntry * next() const { return _next; } 71 }; 72 73 74 class RetTable VALUE_OBJ_CLASS_SPEC { 75 private: 76 RetTableEntry *_first; 77 static int _init_nof_entries; 78 79 void add_jsr(int return_bci, int target_bci); // Adds entry to list 80 public: 81 RetTable() { _first = NULL; } 82 void compute_ret_table(const methodHandle& method); 83 void update_ret_table(int bci, int delta); 84 RetTableEntry* find_jsrs_for_target(int targBci); 85 }; 86 87 // 88 // CellTypeState 89 // 90 class CellTypeState VALUE_OBJ_CLASS_SPEC { 91 private: 92 unsigned int _state; 93 94 // Masks for separating the BITS and INFO portions of a CellTypeState 95 enum { info_mask = right_n_bits(27), 96 bits_mask = (int)(~info_mask) }; 97 98 // These constant are used for manipulating the BITS portion of a 99 // CellTypeState 100 enum { uninit_bit = (int)(nth_bit(31)), 101 ref_bit = nth_bit(30), 102 val_bit = nth_bit(29), 103 addr_bit = nth_bit(28), 104 live_bits_mask = (int)(bits_mask & ~uninit_bit) }; 105 106 // These constants are used for manipulating the INFO portion of a 107 // CellTypeState 108 enum { top_info_bit = nth_bit(26), 109 not_bottom_info_bit = nth_bit(25), 110 info_data_mask = right_n_bits(25), 111 info_conflict = info_mask }; 112 113 // Within the INFO data, these values are used to distinguish different 114 // kinds of references. 115 enum { ref_not_lock_bit = nth_bit(24), // 0 if this reference is locked as a monitor 116 ref_slot_bit = nth_bit(23), // 1 if this reference is a "slot" reference, 117 // 0 if it is a "line" reference. 118 ref_data_mask = right_n_bits(23) }; 119 120 // Within the INFO data, these values are used to distinguish different 121 // kinds of value types. 122 enum { valuetype_slot_bit = nth_bit(24), // 1 if this reference is a "slot" value type, 123 // 0 if it is a "line" value type. 124 valuetype_data_mask = right_n_bits(24) }; 125 126 // These values are used to initialize commonly used CellTypeState 127 // constants. 128 enum { bottom_value = 0, 129 uninit_value = (int)(uninit_bit | info_conflict), 130 ref_value = ref_bit, 131 ref_conflict = ref_bit | info_conflict, 132 val_value = val_bit | info_conflict, 133 addr_value = addr_bit, 134 addr_conflict = addr_bit | info_conflict }; 135 136 public: 137 138 // Since some C++ constructors generate poor code for declarations of the 139 // form... 140 // 141 // CellTypeState vector[length]; 142 // 143 // ...we avoid making a constructor for this class. CellTypeState values 144 // should be constructed using one of the make_* methods: 145 146 static CellTypeState make_any(int state) { 147 CellTypeState s; 148 s._state = state; 149 // Causes SS10 warning. 150 // assert(s.is_valid_state(), "check to see if CellTypeState is valid"); 151 return s; 152 } 153 154 static CellTypeState make_bottom() { 155 return make_any(0); 156 } 157 158 static CellTypeState make_top() { 159 return make_any(AllBits); 160 } 161 162 static CellTypeState make_addr(int bci) { 163 assert((bci >= 0) && (bci < info_data_mask), "check to see if ret addr is valid"); 164 return make_any(addr_bit | not_bottom_info_bit | (bci & info_data_mask)); 165 } 166 167 static CellTypeState make_slot_ref(int slot_num) { 168 assert(slot_num >= 0 && slot_num < ref_data_mask, "slot out of range"); 169 return make_any(ref_bit | not_bottom_info_bit | ref_not_lock_bit | ref_slot_bit | 170 (slot_num & ref_data_mask)); 171 } 172 173 static CellTypeState make_line_ref(int bci) { 174 assert(bci >= 0 && bci < ref_data_mask, "line out of range"); 175 return make_any(ref_bit | not_bottom_info_bit | ref_not_lock_bit | 176 (bci & ref_data_mask)); 177 } 178 179 static CellTypeState make_lock_ref(int bci) { 180 assert(bci >= 0 && bci < ref_data_mask, "line out of range"); 181 return make_any(ref_bit | not_bottom_info_bit | (bci & ref_data_mask)); 182 } 183 184 // Query methods: 185 bool is_bottom() const { return _state == 0; } 186 bool is_live() const { return ((_state & live_bits_mask) != 0); } 187 bool is_valid_state() const { 188 // Uninitialized and value cells must contain no data in their info field: 189 if ((can_be_uninit() || can_be_value()) && !is_info_top()) { 190 return false; 191 } 192 // The top bit is only set when all info bits are set: 193 if (is_info_top() && ((_state & info_mask) != info_mask)) { 194 return false; 195 } 196 // The not_bottom_bit must be set when any other info bit is set: 197 if (is_info_bottom() && ((_state & info_mask) != 0)) { 198 return false; 199 } 200 return true; 201 } 202 203 bool is_address() const { return ((_state & bits_mask) == addr_bit); } 204 bool is_reference() const { return ((_state & bits_mask) == ref_bit); } 205 bool is_value() const { return ((_state & bits_mask) == val_bit); } 206 bool is_uninit() const { return ((_state & bits_mask) == (uint)uninit_bit); } 207 208 bool can_be_address() const { return ((_state & addr_bit) != 0); } 209 bool can_be_reference() const { return ((_state & ref_bit) != 0); } 210 bool can_be_value() const { return ((_state & val_bit) != 0); } 211 bool can_be_uninit() const { return ((_state & uninit_bit) != 0); } 212 213 bool is_info_bottom() const { return ((_state & not_bottom_info_bit) == 0); } 214 bool is_info_top() const { return ((_state & top_info_bit) != 0); } 215 int get_info() const { 216 assert((!is_info_top() && !is_info_bottom()), 217 "check to make sure top/bottom info is not used"); 218 return (_state & info_data_mask); 219 } 220 221 bool is_good_address() const { return is_address() && !is_info_top(); } 222 bool is_lock_reference() const { 223 return ((_state & (bits_mask | top_info_bit | ref_not_lock_bit)) == ref_bit); 224 } 225 bool is_nonlock_reference() const { 226 return ((_state & (bits_mask | top_info_bit | ref_not_lock_bit)) == (ref_bit | ref_not_lock_bit)); 227 } 228 229 bool equal(CellTypeState a) const { return _state == a._state; } 230 bool equal_kind(CellTypeState a) const { 231 return (_state & bits_mask) == (a._state & bits_mask); 232 } 233 234 char to_char() const; 235 236 // Merge 237 CellTypeState merge (CellTypeState cts, int slot) const; 238 239 // Debugging output 240 void print(outputStream *os); 241 242 // Default values of common values 243 static CellTypeState bottom; 244 static CellTypeState uninit; 245 static CellTypeState ref; 246 static CellTypeState value; 247 static CellTypeState refUninit; 248 static CellTypeState varUninit; 249 static CellTypeState top; 250 static CellTypeState addr; 251 }; 252 253 254 // 255 // BasicBlockStruct 256 // 257 class BasicBlock: ResourceObj { 258 private: 259 bool _changed; // Reached a fixpoint or not 260 public: 261 enum Constants { 262 _dead_basic_block = -2, 263 _unreached = -1 // Alive but not yet reached by analysis 264 // >=0 // Alive and has a merged state 265 }; 266 267 int _bci; // Start of basic block 268 int _end_bci; // Bci of last instruction in basicblock 269 int _max_locals; // Determines split between vars and stack 270 int _max_stack; // Determines split between stack and monitors 271 CellTypeState* _state; // State (vars, stack) at entry. 272 int _stack_top; // -1 indicates bottom stack value. 273 int _monitor_top; // -1 indicates bottom monitor stack value. 274 275 CellTypeState* vars() { return _state; } 276 CellTypeState* stack() { return _state + _max_locals; } 277 278 bool changed() { return _changed; } 279 void set_changed(bool s) { _changed = s; } 280 281 bool is_reachable() const { return _stack_top >= 0; } // Analysis has reached this basicblock 282 283 // All basicblocks that are unreachable are going to have a _stack_top == _dead_basic_block. 284 // This info. is setup in a pre-parse before the real abstract interpretation starts. 285 bool is_dead() const { return _stack_top == _dead_basic_block; } 286 bool is_alive() const { return _stack_top != _dead_basic_block; } 287 void mark_as_alive() { assert(is_dead(), "must be dead"); _stack_top = _unreached; } 288 }; 289 290 291 // 292 // GenerateOopMap 293 // 294 // Main class used to compute the pointer-maps in a Method 295 // 296 class GenerateOopMap VALUE_OBJ_CLASS_SPEC { 297 protected: 298 299 // _monitor_top is set to this constant to indicate that a monitor matching 300 // problem was encountered prior to this point in control flow. 301 enum { bad_monitors = -1 }; 302 303 // Main variables 304 methodHandle _method; // The method we are examine 305 RetTable _rt; // Contains the return address mappings 306 int _max_locals; // Cached value of no. of locals 307 int _max_stack; // Cached value of max. stack depth 308 int _max_monitors; // Cached value of max. monitor stack depth 309 int _has_exceptions; // True, if exceptions exist for method 310 bool _got_error; // True, if an error occurred during interpretation. 311 Handle _exception; // Exception if got_error is true. 312 bool _did_rewriting; // was bytecodes rewritten 313 bool _did_relocation; // was relocation neccessary 314 bool _monitor_safe; // The monitors in this method have been determined 315 // to be safe. 316 317 // Working Cell type state 318 int _state_len; // Size of states 319 CellTypeState *_state; // list of states 320 char *_state_vec_buf; // Buffer used to print a readable version of a state 321 int _stack_top; 322 int _monitor_top; 323 324 // Timing and statistics 325 static elapsedTimer _total_oopmap_time; // Holds cumulative oopmap generation time 326 static long _total_byte_count; // Holds cumulative number of bytes inspected 327 328 // Cell type methods 329 void init_state(); 330 void make_context_uninitialized (); 331 int methodsig_to_effect (Symbol* signature, bool isStatic, CellTypeState* effect); 332 bool merge_local_state_vectors (CellTypeState* cts, CellTypeState* bbts); 333 bool merge_monitor_state_vectors(CellTypeState* cts, CellTypeState* bbts); 334 void copy_state (CellTypeState *dst, CellTypeState *src); 335 void merge_state_into_bb (BasicBlock *bb); 336 static void merge_state (GenerateOopMap *gom, int bcidelta, int* data); 337 void set_var (int localNo, CellTypeState cts); 338 CellTypeState get_var (int localNo); 339 CellTypeState pop (); 340 void push (CellTypeState cts); 341 CellTypeState monitor_pop (); 342 void monitor_push (CellTypeState cts); 343 CellTypeState * vars () { return _state; } 344 CellTypeState * stack () { return _state+_max_locals; } 345 CellTypeState * monitors () { return _state+_max_locals+_max_stack; } 346 347 void replace_all_CTS_matches (CellTypeState match, 348 CellTypeState replace); 349 void print_states (outputStream *os, CellTypeState *vector, int num); 350 void print_current_state (outputStream *os, 351 BytecodeStream *itr, 352 bool detailed); 353 void report_monitor_mismatch (const char *msg); 354 355 // Basicblock info 356 BasicBlock * _basic_blocks; // Array of basicblock info 357 int _gc_points; 358 int _bb_count; 359 ResourceBitMap _bb_hdr_bits; 360 361 // Basicblocks methods 362 void initialize_bb (); 363 void mark_bbheaders_and_count_gc_points(); 364 bool is_bb_header (int bci) const { 365 return _bb_hdr_bits.at(bci); 366 } 367 int gc_points () const { return _gc_points; } 368 int bb_count () const { return _bb_count; } 369 void set_bbmark_bit (int bci); 370 BasicBlock * get_basic_block_at (int bci) const; 371 BasicBlock * get_basic_block_containing (int bci) const; 372 void interp_bb (BasicBlock *bb); 373 void restore_state (BasicBlock *bb); 374 int next_bb_start_pc (BasicBlock *bb); 375 void update_basic_blocks (int bci, int delta, int new_method_size); 376 static void bb_mark_fct (GenerateOopMap *c, int deltaBci, int *data); 377 378 // Dead code detection 379 void mark_reachable_code(); 380 static void reachable_basicblock (GenerateOopMap *c, int deltaBci, int *data); 381 382 // Interpretation methods (primary) 383 void do_interpretation (); 384 void init_basic_blocks (); 385 void setup_method_entry_state (); 386 void interp_all (); 387 388 // Interpretation methods (secondary) 389 void interp1 (BytecodeStream *itr); 390 void do_exception_edge (BytecodeStream *itr); 391 void check_type (CellTypeState expected, CellTypeState actual); 392 void ppstore (CellTypeState *in, int loc_no); 393 void ppload (CellTypeState *out, int loc_no); 394 void ppush1 (CellTypeState in); 395 void ppush (CellTypeState *in); 396 void ppop1 (CellTypeState out); 397 void ppop (CellTypeState *out); 398 void ppop_any (int poplen); 399 void pp (CellTypeState *in, CellTypeState *out); 400 void pp_new_ref (CellTypeState *in, int bci); 401 void ppdupswap (int poplen, const char *out); 402 void do_ldc (int bci); 403 void do_astore (int idx); 404 void do_jsr (int delta); 405 void do_field (int is_get, int is_static, int idx, int bci); 406 void do_method (int is_static, int idx, int bci); 407 void do_withfield (int idx, int bci); 408 void do_multianewarray (int dims, int bci); 409 void do_monitorenter (int bci); 410 void do_monitorexit (int bci); 411 void do_return_monitor_check (); 412 void do_checkcast (); 413 CellTypeState *sigchar_to_effect (char sigch, int bci, CellTypeState *out); 414 int copy_cts (CellTypeState *dst, CellTypeState *src); 415 416 // Error handling 417 void error_work (const char *format, va_list ap) ATTRIBUTE_PRINTF(2, 0); 418 void report_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3); 419 void verify_error (const char *format, ...) ATTRIBUTE_PRINTF(2, 3); 420 bool got_error() { return _got_error; } 421 422 // Create result set 423 bool _report_result; 424 bool _report_result_for_send; // Unfortunatly, stackmaps for sends are special, so we need some extra 425 BytecodeStream *_itr_send; // variables to handle them properly. 426 427 void report_result (); 428 429 // Initvars 430 GrowableArray<intptr_t> * _init_vars; 431 432 void initialize_vars (); 433 void add_to_ref_init_set (int localNo); 434 435 // Conflicts rewrite logic 436 bool _conflict; // True, if a conflict occurred during interpretation 437 int _nof_refval_conflicts; // No. of conflicts that require rewrites 438 int * _new_var_map; 439 440 void record_refval_conflict (int varNo); 441 void rewrite_refval_conflicts (); 442 void rewrite_refval_conflict (int from, int to); 443 bool rewrite_refval_conflict_inst (BytecodeStream *i, int from, int to); 444 bool rewrite_load_or_store (BytecodeStream *i, Bytecodes::Code bc, Bytecodes::Code bc0, unsigned int varNo); 445 446 void expand_current_instr (int bci, int ilen, int newIlen, u_char inst_buffer[]); 447 bool is_astore (BytecodeStream *itr, int *index); 448 bool is_aload (BytecodeStream *itr, int *index); 449 450 // List of bci's where a return address is on top of the stack 451 GrowableArray<intptr_t> *_ret_adr_tos; 452 453 bool stack_top_holds_ret_addr (int bci); 454 void compute_ret_adr_at_TOS (); 455 void update_ret_adr_at_TOS (int bci, int delta); 456 457 int binsToHold (int no) { return ((no+(BitsPerWord-1))/BitsPerWord); } 458 char *state_vec_to_string (CellTypeState* vec, int len); 459 460 // Helper method. Can be used in subclasses to fx. calculate gc_points. If the current instuction 461 // is a control transfer, then calls the jmpFct all possible destinations. 462 void ret_jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int varNo,int *data); 463 bool jump_targets_do (BytecodeStream *bcs, jmpFct_t jmpFct, int *data); 464 465 friend class RelocCallback; 466 public: 467 GenerateOopMap(const methodHandle& method); 468 469 // Compute the map. 470 void compute_map(TRAPS); 471 void result_for_basicblock(int bci); // Do a callback on fill_stackmap_for_opcodes for basicblock containing bci 472 473 // Query 474 int max_locals() const { return _max_locals; } 475 Method* method() const { return _method(); } 476 methodHandle method_as_handle() const { return _method; } 477 478 bool did_rewriting() { return _did_rewriting; } 479 bool did_relocation() { return _did_relocation; } 480 481 static void print_time(); 482 483 // Monitor query 484 bool monitor_safe() { return _monitor_safe; } 485 486 // Specialization methods. Intended use: 487 // - possible_gc_point must return true for every bci for which the stackmaps must be returned 488 // - fill_stackmap_prolog is called just before the result is reported. The arguments tells the estimated 489 // number of gc points 490 // - fill_stackmap_for_opcodes is called once for each bytecode index in order (0...code_length-1) 491 // - fill_stackmap_epilog is called after all results has been reported. Note: Since the algorithm does not report 492 // stackmaps for deadcode, fewer gc_points might have been encounted than assumed during the epilog. It is the 493 // responsibility of the subclass to count the correct number. 494 // - fill_init_vars are called once with the result of the init_vars computation 495 // 496 // All these methods are used during a call to: compute_map. Note: Non of the return results are valid 497 // after compute_map returns, since all values are allocated as resource objects. 498 // 499 // All virtual method must be implemented in subclasses 500 virtual bool allow_rewrites () const { return false; } 501 virtual bool report_results () const { return true; } 502 virtual bool report_init_vars () const { return true; } 503 virtual bool possible_gc_point (BytecodeStream *bcs) { ShouldNotReachHere(); return false; } 504 virtual void fill_stackmap_prolog (int nof_gc_points) { ShouldNotReachHere(); } 505 virtual void fill_stackmap_epilog () { ShouldNotReachHere(); } 506 virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs, 507 CellTypeState* vars, 508 CellTypeState* stack, 509 int stackTop) { ShouldNotReachHere(); } 510 virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { ShouldNotReachHere();; } 511 }; 512 513 // 514 // Subclass of the GenerateOopMap Class that just do rewrites of the method, if needed. 515 // It does not store any oopmaps. 516 // 517 class ResolveOopMapConflicts: public GenerateOopMap { 518 private: 519 520 bool _must_clear_locals; 521 522 virtual bool report_results() const { return false; } 523 virtual bool report_init_vars() const { return true; } 524 virtual bool allow_rewrites() const { return true; } 525 virtual bool possible_gc_point (BytecodeStream *bcs) { return false; } 526 virtual void fill_stackmap_prolog (int nof_gc_points) {} 527 virtual void fill_stackmap_epilog () {} 528 virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs, 529 CellTypeState* vars, 530 CellTypeState* stack, 531 int stack_top) {} 532 virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) { _must_clear_locals = init_vars->length() > 0; } 533 534 #ifndef PRODUCT 535 // Statistics 536 static int _nof_invocations; 537 static int _nof_rewrites; 538 static int _nof_relocations; 539 #endif 540 541 public: 542 ResolveOopMapConflicts(const methodHandle& method) : GenerateOopMap(method) { _must_clear_locals = false; }; 543 544 methodHandle do_potential_rewrite(TRAPS); 545 bool must_clear_locals() const { return _must_clear_locals; } 546 }; 547 548 549 // 550 // Subclass used by the compiler to generate pairing infomation 551 // 552 class GeneratePairingInfo: public GenerateOopMap { 553 private: 554 555 virtual bool report_results() const { return false; } 556 virtual bool report_init_vars() const { return false; } 557 virtual bool allow_rewrites() const { return false; } 558 virtual bool possible_gc_point (BytecodeStream *bcs) { return false; } 559 virtual void fill_stackmap_prolog (int nof_gc_points) {} 560 virtual void fill_stackmap_epilog () {} 561 virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs, 562 CellTypeState* vars, 563 CellTypeState* stack, 564 int stack_top) {} 565 virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars) {} 566 public: 567 GeneratePairingInfo(const methodHandle& method) : GenerateOopMap(method) {}; 568 569 // Call compute_map(CHECK) to generate info. 570 }; 571 572 #endif // SHARE_VM_OOPS_GENERATEOOPMAP_HPP