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