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