1 /* 2 * Copyright (c) 1998, 2025, 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 #include "ci/ciMethod.hpp" 26 #include "ci/ciMethodBlocks.hpp" 27 #include "ci/ciStreams.hpp" 28 #include "classfile/vmIntrinsics.hpp" 29 #include "compiler/methodLiveness.hpp" 30 #include "interpreter/bytecode.hpp" 31 #include "interpreter/bytecodes.hpp" 32 #include "memory/allocation.inline.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "utilities/bitMap.inline.hpp" 35 36 // The MethodLiveness class performs a simple liveness analysis on a method 37 // in order to decide which locals are live (that is, will be used again) at 38 // a particular bytecode index (bci). 39 // 40 // The algorithm goes: 41 // 42 // 1. Break the method into a set of basic blocks. For each basic block we 43 // also keep track of its set of predecessors through normal control flow 44 // and predecessors through exceptional control flow. 45 // 46 // 2. For each basic block, compute two sets, gen (the set of values used before 47 // they are defined) and kill (the set of values defined before they are used) 48 // in the basic block. A basic block "needs" the locals in its gen set to 49 // perform its computation. A basic block "provides" values for the locals in 50 // its kill set, allowing a need from a successor to be ignored. 51 // 52 // 3. Liveness information (the set of locals which are needed) is pushed backwards through 53 // the program, from blocks to their predecessors. We compute and store liveness 54 // information for the normal/exceptional exit paths for each basic block. When 55 // this process reaches a fixed point, we are done. 56 // 57 // 4. When we are asked about the liveness at a particular bci with a basic block, we 58 // compute gen/kill sets which represent execution from that bci to the exit of 59 // its blocks. We then compose this range gen/kill information with the normal 60 // and exceptional exit information for the block to produce liveness information 61 // at that bci. 62 // 63 // The algorithm is approximate in many respects. Notably: 64 // 65 // 1. We do not do the analysis necessary to match jsr's with the appropriate ret. 66 // Instead we make the conservative assumption that any ret can return to any 67 // jsr return site. 68 // 2. Instead of computing the effects of exceptions at every instruction, we 69 // summarize the effects of all exceptional continuations from the block as 70 // a single set (_exception_exit), losing some information but simplifying the 71 // analysis. 72 73 MethodLiveness::MethodLiveness(Arena* arena, ciMethod* method) 74 #ifdef COMPILER1 75 : _bci_block_start(arena, method->code_size()) 76 #endif 77 { 78 _arena = arena; 79 _method = method; 80 _bit_map_size_bits = method->max_locals(); 81 } 82 83 void MethodLiveness::compute_liveness() { 84 #ifndef PRODUCT 85 if (TraceLivenessGen) { 86 tty->print_cr("################################################################"); 87 tty->print("# Computing liveness information for "); 88 method()->print_short_name(); 89 } 90 #endif 91 92 init_basic_blocks(); 93 init_gen_kill(); 94 propagate_liveness(); 95 } 96 97 98 void MethodLiveness::init_basic_blocks() { 99 int method_len = method()->code_size(); 100 ciMethodBlocks *mblocks = method()->get_method_blocks(); 101 102 // Create an array to store the bci->BasicBlock mapping. 103 _block_map = new (arena()) GrowableArray<BasicBlock*>(arena(), method_len, method_len, nullptr); 104 105 _block_count = mblocks->num_blocks(); 106 _block_list = (BasicBlock **) arena()->Amalloc(sizeof(BasicBlock *) * _block_count); 107 108 // Used for patching up jsr/ret control flow. 109 GrowableArray<BasicBlock*>* jsr_exit_list = new GrowableArray<BasicBlock*>(5); 110 GrowableArray<BasicBlock*>* ret_list = new GrowableArray<BasicBlock*>(5); 111 112 // generate our block list from ciMethodBlocks 113 for (int blk = 0; blk < _block_count; blk++) { 114 ciBlock *cib = mblocks->block(blk); 115 int start_bci = cib->start_bci(); 116 _block_list[blk] = new (arena()) BasicBlock(this, start_bci, cib->limit_bci()); 117 _block_map->at_put(start_bci, _block_list[blk]); 118 #ifdef COMPILER1 119 // mark all bcis where a new basic block starts 120 _bci_block_start.set_bit(start_bci); 121 #endif // COMPILER1 122 } 123 // fill in the predecessors of blocks 124 ciBytecodeStream bytes(method()); 125 126 for (int blk = 0; blk < _block_count; blk++) { 127 BasicBlock *current_block = _block_list[blk]; 128 int bci = mblocks->block(blk)->control_bci(); 129 130 if (bci == ciBlock::fall_through_bci) { 131 int limit = current_block->limit_bci(); 132 if (limit < method_len) { 133 BasicBlock *next = _block_map->at(limit); 134 assert( next != nullptr, "must be a block immediately following this one."); 135 next->add_normal_predecessor(current_block); 136 } 137 continue; 138 } 139 bytes.reset_to_bci(bci); 140 Bytecodes::Code code = bytes.next(); 141 BasicBlock *dest; 142 143 // Now we need to interpret the instruction's effect 144 // on control flow. 145 assert (current_block != nullptr, "we must have a current block"); 146 switch (code) { 147 case Bytecodes::_ifeq: 148 case Bytecodes::_ifne: 149 case Bytecodes::_iflt: 150 case Bytecodes::_ifge: 151 case Bytecodes::_ifgt: 152 case Bytecodes::_ifle: 153 case Bytecodes::_if_icmpeq: 154 case Bytecodes::_if_icmpne: 155 case Bytecodes::_if_icmplt: 156 case Bytecodes::_if_icmpge: 157 case Bytecodes::_if_icmpgt: 158 case Bytecodes::_if_icmple: 159 case Bytecodes::_if_acmpeq: 160 case Bytecodes::_if_acmpne: 161 case Bytecodes::_ifnull: 162 case Bytecodes::_ifnonnull: 163 // Two way branch. Set predecessors at each destination. 164 if (bytes.next_bci() < method_len) { 165 dest = _block_map->at(bytes.next_bci()); 166 assert(dest != nullptr, "must be a block immediately following this one."); 167 dest->add_normal_predecessor(current_block); 168 } 169 dest = _block_map->at(bytes.get_dest()); 170 assert(dest != nullptr, "branch destination must start a block."); 171 dest->add_normal_predecessor(current_block); 172 break; 173 case Bytecodes::_goto: 174 dest = _block_map->at(bytes.get_dest()); 175 assert(dest != nullptr, "branch destination must start a block."); 176 dest->add_normal_predecessor(current_block); 177 break; 178 case Bytecodes::_goto_w: 179 dest = _block_map->at(bytes.get_far_dest()); 180 assert(dest != nullptr, "branch destination must start a block."); 181 dest->add_normal_predecessor(current_block); 182 break; 183 case Bytecodes::_tableswitch: 184 { 185 Bytecode_tableswitch tableswitch(&bytes); 186 187 int len = tableswitch.length(); 188 189 dest = _block_map->at(bci + tableswitch.default_offset()); 190 assert(dest != nullptr, "branch destination must start a block."); 191 dest->add_normal_predecessor(current_block); 192 while (--len >= 0) { 193 dest = _block_map->at(bci + tableswitch.dest_offset_at(len)); 194 assert(dest != nullptr, "branch destination must start a block."); 195 dest->add_normal_predecessor(current_block); 196 } 197 break; 198 } 199 200 case Bytecodes::_lookupswitch: 201 { 202 Bytecode_lookupswitch lookupswitch(&bytes); 203 204 int npairs = lookupswitch.number_of_pairs(); 205 206 dest = _block_map->at(bci + lookupswitch.default_offset()); 207 assert(dest != nullptr, "branch destination must start a block."); 208 dest->add_normal_predecessor(current_block); 209 while(--npairs >= 0) { 210 LookupswitchPair pair = lookupswitch.pair_at(npairs); 211 dest = _block_map->at( bci + pair.offset()); 212 assert(dest != nullptr, "branch destination must start a block."); 213 dest->add_normal_predecessor(current_block); 214 } 215 break; 216 } 217 218 case Bytecodes::_jsr: 219 { 220 assert(bytes.is_wide()==false, "sanity check"); 221 dest = _block_map->at(bytes.get_dest()); 222 assert(dest != nullptr, "branch destination must start a block."); 223 dest->add_normal_predecessor(current_block); 224 if (bci + Bytecodes::length_for(code) >= method_len) { 225 break; 226 } 227 BasicBlock *jsrExit = _block_map->at(current_block->limit_bci()); 228 assert(jsrExit != nullptr, "jsr return bci must start a block."); 229 jsr_exit_list->append(jsrExit); 230 break; 231 } 232 case Bytecodes::_jsr_w: 233 { 234 dest = _block_map->at(bytes.get_far_dest()); 235 assert(dest != nullptr, "branch destination must start a block."); 236 dest->add_normal_predecessor(current_block); 237 if (bci + Bytecodes::length_for(code) >= method_len) { 238 break; 239 } 240 BasicBlock *jsrExit = _block_map->at(current_block->limit_bci()); 241 assert(jsrExit != nullptr, "jsr return bci must start a block."); 242 jsr_exit_list->append(jsrExit); 243 break; 244 } 245 246 case Bytecodes::_wide: 247 assert(false, "wide opcodes should not be seen here"); 248 break; 249 case Bytecodes::_athrow: 250 case Bytecodes::_ireturn: 251 case Bytecodes::_lreturn: 252 case Bytecodes::_freturn: 253 case Bytecodes::_dreturn: 254 case Bytecodes::_areturn: 255 case Bytecodes::_return: 256 // These opcodes are not the normal predecessors of any other opcodes. 257 break; 258 case Bytecodes::_ret: 259 // We will patch up jsr/rets in a subsequent pass. 260 ret_list->append(current_block); 261 break; 262 default: 263 // Do nothing. 264 break; 265 } 266 } 267 // Patch up the jsr/ret's. We conservatively assume that any ret 268 // can return to any jsr site. 269 int ret_list_len = ret_list->length(); 270 int jsr_exit_list_len = jsr_exit_list->length(); 271 if (ret_list_len > 0 && jsr_exit_list_len > 0) { 272 for (int i = jsr_exit_list_len - 1; i >= 0; i--) { 273 BasicBlock *jsrExit = jsr_exit_list->at(i); 274 for (int i = ret_list_len - 1; i >= 0; i--) { 275 jsrExit->add_normal_predecessor(ret_list->at(i)); 276 } 277 } 278 } 279 280 // Compute exception edges. 281 for (int b=_block_count-1; b >= 0; b--) { 282 BasicBlock *block = _block_list[b]; 283 int block_start = block->start_bci(); 284 int block_limit = block->limit_bci(); 285 ciExceptionHandlerStream handlers(method()); 286 for (; !handlers.is_done(); handlers.next()) { 287 ciExceptionHandler* handler = handlers.handler(); 288 int start = handler->start(); 289 int limit = handler->limit(); 290 int handler_bci = handler->handler_bci(); 291 292 int intersect_start = MAX2(block_start, start); 293 int intersect_limit = MIN2(block_limit, limit); 294 if (intersect_start < intersect_limit) { 295 // The catch range has a nonempty intersection with this 296 // basic block. That means this basic block can be an 297 // exceptional predecessor. 298 _block_map->at(handler_bci)->add_exception_predecessor(block); 299 300 if (handler->is_catch_all()) { 301 // This is a catch-all block. 302 if (intersect_start == block_start && intersect_limit == block_limit) { 303 // The basic block is entirely contained in this catch-all block. 304 // Skip the rest of the exception handlers -- they can never be 305 // reached in execution. 306 break; 307 } 308 } 309 } 310 } 311 } 312 } 313 314 void MethodLiveness::init_gen_kill() { 315 for (int i=_block_count-1; i >= 0; i--) { 316 _block_list[i]->compute_gen_kill(method()); 317 } 318 } 319 320 void MethodLiveness::propagate_liveness() { 321 int num_blocks = _block_count; 322 BasicBlock *block; 323 324 // We start our work list off with all blocks in it. 325 // Alternately, we could start off the work list with the list of all 326 // blocks which could exit the method directly, along with one block 327 // from any infinite loop. If this matters, it can be changed. It 328 // may not be clear from looking at the code, but the order of the 329 // workList will be the opposite of the creation order of the basic 330 // blocks, which should be decent for quick convergence (with the 331 // possible exception of exception handlers, which are all created 332 // early). 333 _work_list = nullptr; 334 for (int i = 0; i < num_blocks; i++) { 335 block = _block_list[i]; 336 block->set_next(_work_list); 337 block->set_on_work_list(true); 338 _work_list = block; 339 } 340 341 342 while ((block = work_list_get()) != nullptr) { 343 block->propagate(this); 344 } 345 } 346 347 void MethodLiveness::work_list_add(BasicBlock *block) { 348 if (!block->on_work_list()) { 349 block->set_next(_work_list); 350 block->set_on_work_list(true); 351 _work_list = block; 352 } 353 } 354 355 MethodLiveness::BasicBlock *MethodLiveness::work_list_get() { 356 BasicBlock *block = _work_list; 357 if (block != nullptr) { 358 block->set_on_work_list(false); 359 _work_list = block->next(); 360 } 361 return block; 362 } 363 364 365 MethodLivenessResult MethodLiveness::get_liveness_at(int entry_bci) { 366 int bci = entry_bci; 367 bool is_entry = false; 368 if (entry_bci == InvocationEntryBci) { 369 is_entry = true; 370 bci = 0; 371 } 372 373 MethodLivenessResult answer; 374 375 if (_block_count > 0) { 376 377 assert( 0 <= bci && bci < method()->code_size(), "bci out of range" ); 378 BasicBlock *block = _block_map->at(bci); 379 // We may not be at the block start, so search backwards to find the block 380 // containing bci. 381 int t = bci; 382 while (block == nullptr && t > 0) { 383 block = _block_map->at(--t); 384 } 385 guarantee(block != nullptr, "invalid bytecode index; must be instruction index"); 386 assert(bci >= block->start_bci() && bci < block->limit_bci(), "block must contain bci."); 387 388 answer = block->get_liveness_at(method(), bci); 389 390 if (is_entry && method()->is_synchronized() && !method()->is_static()) { 391 // Synchronized methods use the receiver once on entry. 392 answer.at_put(0, true); 393 } 394 395 #ifndef PRODUCT 396 if (TraceLivenessQuery) { 397 tty->print("Liveness query of "); 398 method()->print_short_name(); 399 tty->print(" @ %d : result is ", bci); 400 answer.print_on(tty); 401 } 402 #endif 403 } 404 405 return answer; 406 } 407 408 MethodLiveness::BasicBlock::BasicBlock(MethodLiveness *analyzer, int start, int limit) : 409 _entry(analyzer->arena(), analyzer->bit_map_size_bits()), 410 _normal_exit(analyzer->arena(), analyzer->bit_map_size_bits()), 411 _exception_exit(analyzer->arena(), analyzer->bit_map_size_bits()), 412 _gen(analyzer->arena(), analyzer->bit_map_size_bits()), 413 _kill(analyzer->arena(), analyzer->bit_map_size_bits()), 414 _last_bci(-1) { 415 _analyzer = analyzer; 416 _start_bci = start; 417 _limit_bci = limit; 418 _normal_predecessors = 419 new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, nullptr); 420 _exception_predecessors = 421 new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, nullptr); 422 } 423 424 425 426 MethodLiveness::BasicBlock *MethodLiveness::BasicBlock::split(int split_bci) { 427 int start = _start_bci; 428 int limit = _limit_bci; 429 430 if (TraceLivenessGen) { 431 tty->print_cr(" ** Splitting block (%d,%d) at %d", start, limit, split_bci); 432 } 433 434 GrowableArray<BasicBlock*>* save_predecessors = _normal_predecessors; 435 436 assert (start < split_bci && split_bci < limit, "improper split"); 437 438 // Make a new block to cover the first half of the range. 439 BasicBlock *first_half = new (_analyzer->arena()) BasicBlock(_analyzer, start, split_bci); 440 441 // Assign correct values to the second half (this) 442 _normal_predecessors = first_half->_normal_predecessors; 443 _start_bci = split_bci; 444 add_normal_predecessor(first_half); 445 446 // Assign correct predecessors to the new first half 447 first_half->_normal_predecessors = save_predecessors; 448 449 return first_half; 450 } 451 452 void MethodLiveness::BasicBlock::compute_gen_kill(ciMethod* method) { 453 ciBytecodeStream bytes(method); 454 bytes.reset_to_bci(start_bci()); 455 bytes.set_max_bci(limit_bci()); 456 compute_gen_kill_range(&bytes); 457 458 } 459 460 void MethodLiveness::BasicBlock::compute_gen_kill_range(ciBytecodeStream *bytes) { 461 _gen.clear(); 462 _kill.clear(); 463 464 while (bytes->next() != ciBytecodeStream::EOBC()) { 465 compute_gen_kill_single(bytes); 466 } 467 } 468 469 void MethodLiveness::BasicBlock::compute_gen_kill_single(ciBytecodeStream *instruction) { 470 // We prohibit _gen and _kill from having locals in common. If we 471 // know that one is definitely going to be applied before the other, 472 // we could save some computation time by relaxing this prohibition. 473 474 switch (instruction->cur_bc()) { 475 case Bytecodes::_nop: 476 case Bytecodes::_goto: 477 case Bytecodes::_goto_w: 478 case Bytecodes::_aconst_null: 479 case Bytecodes::_new: 480 case Bytecodes::_iconst_m1: 481 case Bytecodes::_iconst_0: 482 case Bytecodes::_iconst_1: 483 case Bytecodes::_iconst_2: 484 case Bytecodes::_iconst_3: 485 case Bytecodes::_iconst_4: 486 case Bytecodes::_iconst_5: 487 case Bytecodes::_fconst_0: 488 case Bytecodes::_fconst_1: 489 case Bytecodes::_fconst_2: 490 case Bytecodes::_bipush: 491 case Bytecodes::_sipush: 492 case Bytecodes::_lconst_0: 493 case Bytecodes::_lconst_1: 494 case Bytecodes::_dconst_0: 495 case Bytecodes::_dconst_1: 496 case Bytecodes::_ldc2_w: 497 case Bytecodes::_ldc: 498 case Bytecodes::_ldc_w: 499 case Bytecodes::_iaload: 500 case Bytecodes::_faload: 501 case Bytecodes::_baload: 502 case Bytecodes::_caload: 503 case Bytecodes::_saload: 504 case Bytecodes::_laload: 505 case Bytecodes::_daload: 506 case Bytecodes::_aaload: 507 case Bytecodes::_iastore: 508 case Bytecodes::_fastore: 509 case Bytecodes::_bastore: 510 case Bytecodes::_castore: 511 case Bytecodes::_sastore: 512 case Bytecodes::_lastore: 513 case Bytecodes::_dastore: 514 case Bytecodes::_aastore: 515 case Bytecodes::_pop: 516 case Bytecodes::_pop2: 517 case Bytecodes::_dup: 518 case Bytecodes::_dup_x1: 519 case Bytecodes::_dup_x2: 520 case Bytecodes::_dup2: 521 case Bytecodes::_dup2_x1: 522 case Bytecodes::_dup2_x2: 523 case Bytecodes::_swap: 524 case Bytecodes::_iadd: 525 case Bytecodes::_fadd: 526 case Bytecodes::_isub: 527 case Bytecodes::_fsub: 528 case Bytecodes::_imul: 529 case Bytecodes::_fmul: 530 case Bytecodes::_idiv: 531 case Bytecodes::_fdiv: 532 case Bytecodes::_irem: 533 case Bytecodes::_frem: 534 case Bytecodes::_ishl: 535 case Bytecodes::_ishr: 536 case Bytecodes::_iushr: 537 case Bytecodes::_iand: 538 case Bytecodes::_ior: 539 case Bytecodes::_ixor: 540 case Bytecodes::_l2f: 541 case Bytecodes::_l2i: 542 case Bytecodes::_d2f: 543 case Bytecodes::_d2i: 544 case Bytecodes::_fcmpl: 545 case Bytecodes::_fcmpg: 546 case Bytecodes::_ladd: 547 case Bytecodes::_dadd: 548 case Bytecodes::_lsub: 549 case Bytecodes::_dsub: 550 case Bytecodes::_lmul: 551 case Bytecodes::_dmul: 552 case Bytecodes::_ldiv: 553 case Bytecodes::_ddiv: 554 case Bytecodes::_lrem: 555 case Bytecodes::_drem: 556 case Bytecodes::_land: 557 case Bytecodes::_lor: 558 case Bytecodes::_lxor: 559 case Bytecodes::_ineg: 560 case Bytecodes::_fneg: 561 case Bytecodes::_i2f: 562 case Bytecodes::_f2i: 563 case Bytecodes::_i2c: 564 case Bytecodes::_i2s: 565 case Bytecodes::_i2b: 566 case Bytecodes::_lneg: 567 case Bytecodes::_dneg: 568 case Bytecodes::_l2d: 569 case Bytecodes::_d2l: 570 case Bytecodes::_lshl: 571 case Bytecodes::_lshr: 572 case Bytecodes::_lushr: 573 case Bytecodes::_i2l: 574 case Bytecodes::_i2d: 575 case Bytecodes::_f2l: 576 case Bytecodes::_f2d: 577 case Bytecodes::_lcmp: 578 case Bytecodes::_dcmpl: 579 case Bytecodes::_dcmpg: 580 case Bytecodes::_ifeq: 581 case Bytecodes::_ifne: 582 case Bytecodes::_iflt: 583 case Bytecodes::_ifge: 584 case Bytecodes::_ifgt: 585 case Bytecodes::_ifle: 586 case Bytecodes::_tableswitch: 587 case Bytecodes::_ireturn: 588 case Bytecodes::_freturn: 589 case Bytecodes::_if_icmpeq: 590 case Bytecodes::_if_icmpne: 591 case Bytecodes::_if_icmplt: 592 case Bytecodes::_if_icmpge: 593 case Bytecodes::_if_icmpgt: 594 case Bytecodes::_if_icmple: 595 case Bytecodes::_lreturn: 596 case Bytecodes::_dreturn: 597 case Bytecodes::_if_acmpeq: 598 case Bytecodes::_if_acmpne: 599 case Bytecodes::_jsr: 600 case Bytecodes::_jsr_w: 601 case Bytecodes::_getstatic: 602 case Bytecodes::_putstatic: 603 case Bytecodes::_getfield: 604 case Bytecodes::_putfield: 605 case Bytecodes::_invokevirtual: 606 case Bytecodes::_invokespecial: 607 case Bytecodes::_invokestatic: 608 case Bytecodes::_invokeinterface: 609 case Bytecodes::_invokedynamic: 610 case Bytecodes::_newarray: 611 case Bytecodes::_anewarray: 612 case Bytecodes::_checkcast: 613 case Bytecodes::_arraylength: 614 case Bytecodes::_instanceof: 615 case Bytecodes::_athrow: 616 case Bytecodes::_areturn: 617 case Bytecodes::_monitorenter: 618 case Bytecodes::_monitorexit: 619 case Bytecodes::_ifnull: 620 case Bytecodes::_ifnonnull: 621 case Bytecodes::_multianewarray: 622 case Bytecodes::_lookupswitch: 623 // These bytecodes have no effect on the method's locals. 624 break; 625 626 case Bytecodes::_return: { 627 ciMethod* method = instruction->method(); 628 ciInstanceKlass* holder = method->holder(); 629 const bool abstract_klass = holder->is_abstract(); 630 const bool concrete_value_klass = !abstract_klass && holder->is_inlinetype(); 631 if (method->intrinsic_id() == vmIntrinsics::_Object_init || 632 (method->is_object_constructor() && (concrete_value_klass || abstract_klass))) { 633 // Returning from Object.<init> implicitly registers a finalizer for the receiver if needed, to keep it alive. 634 // Value class constructors update the scalarized receiver. We need to keep it live so that we can find it after 635 // (chained) constructor calls and propagate updates to the caller. If the holder of the constructor is abstract, 636 // we do not know if the constructor was called on a value class or not. We therefore keep the receiver of all 637 // abstract constructors live. 638 load_one(0); 639 } 640 break; 641 } 642 case Bytecodes::_lload: 643 case Bytecodes::_dload: 644 load_two(instruction->get_index()); 645 break; 646 647 case Bytecodes::_lload_0: 648 case Bytecodes::_dload_0: 649 load_two(0); 650 break; 651 652 case Bytecodes::_lload_1: 653 case Bytecodes::_dload_1: 654 load_two(1); 655 break; 656 657 case Bytecodes::_lload_2: 658 case Bytecodes::_dload_2: 659 load_two(2); 660 break; 661 662 case Bytecodes::_lload_3: 663 case Bytecodes::_dload_3: 664 load_two(3); 665 break; 666 667 case Bytecodes::_iload: 668 case Bytecodes::_iinc: 669 case Bytecodes::_fload: 670 case Bytecodes::_aload: 671 case Bytecodes::_ret: 672 load_one(instruction->get_index()); 673 break; 674 675 case Bytecodes::_iload_0: 676 case Bytecodes::_fload_0: 677 case Bytecodes::_aload_0: 678 load_one(0); 679 break; 680 681 case Bytecodes::_iload_1: 682 case Bytecodes::_fload_1: 683 case Bytecodes::_aload_1: 684 load_one(1); 685 break; 686 687 case Bytecodes::_iload_2: 688 case Bytecodes::_fload_2: 689 case Bytecodes::_aload_2: 690 load_one(2); 691 break; 692 693 case Bytecodes::_iload_3: 694 case Bytecodes::_fload_3: 695 case Bytecodes::_aload_3: 696 load_one(3); 697 break; 698 699 case Bytecodes::_lstore: 700 case Bytecodes::_dstore: 701 store_two(instruction->get_index()); 702 break; 703 704 case Bytecodes::_lstore_0: 705 case Bytecodes::_dstore_0: 706 store_two(0); 707 break; 708 709 case Bytecodes::_lstore_1: 710 case Bytecodes::_dstore_1: 711 store_two(1); 712 break; 713 714 case Bytecodes::_lstore_2: 715 case Bytecodes::_dstore_2: 716 store_two(2); 717 break; 718 719 case Bytecodes::_lstore_3: 720 case Bytecodes::_dstore_3: 721 store_two(3); 722 break; 723 724 case Bytecodes::_istore: 725 case Bytecodes::_fstore: 726 case Bytecodes::_astore: 727 store_one(instruction->get_index()); 728 break; 729 730 case Bytecodes::_istore_0: 731 case Bytecodes::_fstore_0: 732 case Bytecodes::_astore_0: 733 store_one(0); 734 break; 735 736 case Bytecodes::_istore_1: 737 case Bytecodes::_fstore_1: 738 case Bytecodes::_astore_1: 739 store_one(1); 740 break; 741 742 case Bytecodes::_istore_2: 743 case Bytecodes::_fstore_2: 744 case Bytecodes::_astore_2: 745 store_one(2); 746 break; 747 748 case Bytecodes::_istore_3: 749 case Bytecodes::_fstore_3: 750 case Bytecodes::_astore_3: 751 store_one(3); 752 break; 753 754 case Bytecodes::_wide: 755 fatal("Iterator should skip this bytecode"); 756 break; 757 758 default: 759 tty->print("unexpected opcode: %d\n", instruction->cur_bc()); 760 ShouldNotReachHere(); 761 break; 762 } 763 } 764 765 void MethodLiveness::BasicBlock::load_two(int local) { 766 load_one(local); 767 load_one(local+1); 768 } 769 770 void MethodLiveness::BasicBlock::load_one(int local) { 771 if (!_kill.at(local)) { 772 _gen.at_put(local, true); 773 } 774 } 775 776 void MethodLiveness::BasicBlock::store_two(int local) { 777 store_one(local); 778 store_one(local+1); 779 } 780 781 void MethodLiveness::BasicBlock::store_one(int local) { 782 if (!_gen.at(local)) { 783 _kill.at_put(local, true); 784 } 785 } 786 787 void MethodLiveness::BasicBlock::propagate(MethodLiveness *ml) { 788 // These set operations could be combined for efficiency if the 789 // performance of this analysis becomes an issue. 790 _entry.set_union(_normal_exit); 791 _entry.set_difference(_kill); 792 _entry.set_union(_gen); 793 794 // Note that we merge information from our exceptional successors 795 // just once, rather than at individual bytecodes. 796 _entry.set_union(_exception_exit); 797 798 if (TraceLivenessGen) { 799 tty->print_cr(" ** Visiting block at %d **", start_bci()); 800 print_on(tty); 801 } 802 803 int i; 804 for (i=_normal_predecessors->length()-1; i>=0; i--) { 805 BasicBlock *block = _normal_predecessors->at(i); 806 if (block->merge_normal(_entry)) { 807 ml->work_list_add(block); 808 } 809 } 810 for (i=_exception_predecessors->length()-1; i>=0; i--) { 811 BasicBlock *block = _exception_predecessors->at(i); 812 if (block->merge_exception(_entry)) { 813 ml->work_list_add(block); 814 } 815 } 816 } 817 818 bool MethodLiveness::BasicBlock::merge_normal(const BitMap& other) { 819 return _normal_exit.set_union_with_result(other); 820 } 821 822 bool MethodLiveness::BasicBlock::merge_exception(const BitMap& other) { 823 return _exception_exit.set_union_with_result(other); 824 } 825 826 MethodLivenessResult MethodLiveness::BasicBlock::get_liveness_at(ciMethod* method, int bci) { 827 MethodLivenessResult answer(_analyzer->bit_map_size_bits()); 828 answer.set_is_valid(); 829 830 #ifndef ASSERT 831 if (bci == start_bci()) { 832 answer.set_from(_entry); 833 return answer; 834 } 835 #endif 836 837 #ifdef ASSERT 838 ResourceMark rm; 839 ResourceBitMap g(_gen.size(), false); g.set_from(_gen); 840 ResourceBitMap k(_kill.size(), false); k.set_from(_kill); 841 #endif 842 if (_last_bci != bci || trueInDebug) { 843 ciBytecodeStream bytes(method); 844 bytes.reset_to_bci(bci); 845 bytes.set_max_bci(limit_bci()); 846 compute_gen_kill_range(&bytes); 847 assert(_last_bci != bci || 848 (g.is_same(_gen) && k.is_same(_kill)), "cached computation is incorrect"); 849 _last_bci = bci; 850 } 851 852 answer.set_union(_normal_exit); 853 answer.set_difference(_kill); 854 answer.set_union(_gen); 855 answer.set_union(_exception_exit); 856 857 #ifdef ASSERT 858 if (bci == start_bci()) { 859 assert(answer.is_same(_entry), "optimized answer must be accurate"); 860 } 861 #endif 862 863 return answer; 864 } 865 866 #ifndef PRODUCT 867 868 void MethodLiveness::BasicBlock::print_on(outputStream *os) const { 869 os->print_cr("==================================================================="); 870 os->print_cr(" Block start: %4d, limit: %4d", _start_bci, _limit_bci); 871 os->print (" Normal predecessors (%2d) @", _normal_predecessors->length()); 872 int i; 873 for (i=0; i < _normal_predecessors->length(); i++) { 874 os->print(" %4d", _normal_predecessors->at(i)->start_bci()); 875 } 876 os->cr(); 877 os->print (" Exceptional predecessors (%2d) @", _exception_predecessors->length()); 878 for (i=0; i < _exception_predecessors->length(); i++) { 879 os->print(" %4d", _exception_predecessors->at(i)->start_bci()); 880 } 881 os->cr(); 882 os->print (" Normal Exit : "); 883 _normal_exit.print_on(os); 884 os->print (" Gen : "); 885 _gen.print_on(os); 886 os->print (" Kill : "); 887 _kill.print_on(os); 888 os->print (" Exception Exit: "); 889 _exception_exit.print_on(os); 890 os->print (" Entry : "); 891 _entry.print_on(os); 892 } 893 894 #endif // PRODUCT