1 /*
   2  * Copyright (c) 1998, 2016, 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 "precompiled.hpp"
  26 #include "ci/ciMethod.hpp"
  27 #include "ci/ciMethodBlocks.hpp"
  28 #include "ci/ciStreams.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 
  74 //--------------------------------------------------------------------------
  75 // The BitCounter class is used for counting the number of bits set in
  76 // some BitMap.  It is only used when collecting liveness statistics.
  77 
  78 #ifndef PRODUCT
  79 
  80 class BitCounter: public BitMapClosure {
  81  private:
  82   int _count;
  83  public:
  84   BitCounter() : _count(0) {}
  85 
  86   // Callback when bit in map is set
  87   virtual bool do_bit(size_t offset) {
  88     _count++;
  89     return true;
  90   }
  91 
  92   int count() {
  93     return _count;
  94   }
  95 };
  96 
  97 
  98 //--------------------------------------------------------------------------
  99 
 100 
 101 // Counts
 102 long MethodLiveness::_total_bytes = 0;
 103 int  MethodLiveness::_total_methods = 0;
 104 
 105 long MethodLiveness::_total_blocks = 0;
 106 int  MethodLiveness::_max_method_blocks = 0;
 107 
 108 long MethodLiveness::_total_edges = 0;
 109 int  MethodLiveness::_max_block_edges = 0;
 110 
 111 long MethodLiveness::_total_exc_edges = 0;
 112 int  MethodLiveness::_max_block_exc_edges = 0;
 113 
 114 long MethodLiveness::_total_method_locals = 0;
 115 int  MethodLiveness::_max_method_locals = 0;
 116 
 117 long MethodLiveness::_total_locals_queried = 0;
 118 long MethodLiveness::_total_live_locals_queried = 0;
 119 
 120 long MethodLiveness::_total_visits = 0;
 121 
 122 #endif
 123 
 124 // Timers
 125 elapsedTimer MethodLiveness::_time_build_graph;
 126 elapsedTimer MethodLiveness::_time_gen_kill;
 127 elapsedTimer MethodLiveness::_time_flow;
 128 elapsedTimer MethodLiveness::_time_query;
 129 elapsedTimer MethodLiveness::_time_total;
 130 
 131 MethodLiveness::MethodLiveness(Arena* arena, ciMethod* method)
 132 #ifdef COMPILER1
 133   : _bci_block_start((uintptr_t*)arena->Amalloc((method->code_size() >> LogBitsPerByte) + 1), method->code_size())
 134 #endif
 135 {
 136   _arena = arena;
 137   _method = method;
 138   _bit_map_size_bits = method->max_locals();
 139   _bit_map_size_words = (_bit_map_size_bits / sizeof(unsigned int)) + 1;
 140 
 141 #ifdef COMPILER1
 142   _bci_block_start.clear();
 143 #endif
 144 }
 145 
 146 void MethodLiveness::compute_liveness() {
 147 #ifndef PRODUCT
 148   if (TraceLivenessGen) {
 149     tty->print_cr("################################################################");
 150     tty->print("# Computing liveness information for ");
 151     method()->print_short_name();
 152   }
 153 
 154   if (TimeLivenessAnalysis) _time_total.start();
 155 #endif
 156 
 157   {
 158     TraceTime buildGraph(NULL, &_time_build_graph, TimeLivenessAnalysis);
 159     init_basic_blocks();
 160   }
 161   {
 162     TraceTime genKill(NULL, &_time_gen_kill, TimeLivenessAnalysis);
 163     init_gen_kill();
 164   }
 165   {
 166     TraceTime flow(NULL, &_time_flow, TimeLivenessAnalysis);
 167     propagate_liveness();
 168   }
 169 
 170 #ifndef PRODUCT
 171   if (TimeLivenessAnalysis) _time_total.stop();
 172 
 173   if (TimeLivenessAnalysis) {
 174     // Collect statistics
 175     _total_bytes += method()->code_size();
 176     _total_methods++;
 177 
 178     int num_blocks = _block_count;
 179     _total_blocks += num_blocks;
 180     _max_method_blocks = MAX2(num_blocks,_max_method_blocks);
 181 
 182     for (int i=0; i<num_blocks; i++) {
 183       BasicBlock *block = _block_list[i];
 184 
 185       int numEdges = block->_normal_predecessors->length();
 186       int numExcEdges = block->_exception_predecessors->length();
 187 
 188       _total_edges += numEdges;
 189       _total_exc_edges += numExcEdges;
 190       _max_block_edges = MAX2(numEdges,_max_block_edges);
 191       _max_block_exc_edges = MAX2(numExcEdges,_max_block_exc_edges);
 192     }
 193 
 194     int numLocals = _bit_map_size_bits;
 195     _total_method_locals += numLocals;
 196     _max_method_locals = MAX2(numLocals,_max_method_locals);
 197   }
 198 #endif
 199 }
 200 
 201 
 202 void MethodLiveness::init_basic_blocks() {
 203   bool bailout = false;
 204 
 205   int method_len = method()->code_size();
 206   ciMethodBlocks *mblocks = method()->get_method_blocks();
 207 
 208   // Create an array to store the bci->BasicBlock mapping.
 209   _block_map = new (arena()) GrowableArray<BasicBlock*>(arena(), method_len, method_len, NULL);
 210 
 211   _block_count = mblocks->num_blocks();
 212   _block_list = (BasicBlock **) arena()->Amalloc(sizeof(BasicBlock *) * _block_count);
 213 
 214   // Used for patching up jsr/ret control flow.
 215   GrowableArray<BasicBlock*>* jsr_exit_list = new GrowableArray<BasicBlock*>(5);
 216   GrowableArray<BasicBlock*>* ret_list = new GrowableArray<BasicBlock*>(5);
 217 
 218   // generate our block list from ciMethodBlocks
 219   for (int blk = 0; blk < _block_count; blk++) {
 220     ciBlock *cib = mblocks->block(blk);
 221      int start_bci = cib->start_bci();
 222     _block_list[blk] = new (arena()) BasicBlock(this, start_bci, cib->limit_bci());
 223     _block_map->at_put(start_bci, _block_list[blk]);
 224 #ifdef COMPILER1
 225     // mark all bcis where a new basic block starts
 226     _bci_block_start.set_bit(start_bci);
 227 #endif // COMPILER1
 228   }
 229   // fill in the predecessors of blocks
 230   ciBytecodeStream bytes(method());
 231 
 232   for (int blk = 0; blk < _block_count; blk++) {
 233     BasicBlock *current_block = _block_list[blk];
 234     int bci =  mblocks->block(blk)->control_bci();
 235 
 236     if (bci == ciBlock::fall_through_bci) {
 237       int limit = current_block->limit_bci();
 238       if (limit < method_len) {
 239         BasicBlock *next = _block_map->at(limit);
 240         assert( next != NULL, "must be a block immediately following this one.");
 241         next->add_normal_predecessor(current_block);
 242       }
 243       continue;
 244     }
 245     bytes.reset_to_bci(bci);
 246     Bytecodes::Code code = bytes.next();
 247     BasicBlock *dest;
 248 
 249     // Now we need to interpret the instruction's effect
 250     // on control flow.
 251     assert (current_block != NULL, "we must have a current block");
 252     switch (code) {
 253       case Bytecodes::_ifeq:
 254       case Bytecodes::_ifne:
 255       case Bytecodes::_iflt:
 256       case Bytecodes::_ifge:
 257       case Bytecodes::_ifgt:
 258       case Bytecodes::_ifle:
 259       case Bytecodes::_if_icmpeq:
 260       case Bytecodes::_if_icmpne:
 261       case Bytecodes::_if_icmplt:
 262       case Bytecodes::_if_icmpge:
 263       case Bytecodes::_if_icmpgt:
 264       case Bytecodes::_if_icmple:
 265       case Bytecodes::_if_acmpeq:
 266       case Bytecodes::_if_acmpne:
 267       case Bytecodes::_ifnull:
 268       case Bytecodes::_ifnonnull:
 269         // Two way branch.  Set predecessors at each destination.
 270         dest = _block_map->at(bytes.next_bci());
 271         assert(dest != NULL, "must be a block immediately following this one.");
 272         dest->add_normal_predecessor(current_block);
 273 
 274         dest = _block_map->at(bytes.get_dest());
 275         assert(dest != NULL, "branch desination must start a block.");
 276         dest->add_normal_predecessor(current_block);
 277         break;
 278       case Bytecodes::_goto:
 279         dest = _block_map->at(bytes.get_dest());
 280         assert(dest != NULL, "branch desination must start a block.");
 281         dest->add_normal_predecessor(current_block);
 282         break;
 283       case Bytecodes::_goto_w:
 284         dest = _block_map->at(bytes.get_far_dest());
 285         assert(dest != NULL, "branch desination must start a block.");
 286         dest->add_normal_predecessor(current_block);
 287         break;
 288       case Bytecodes::_tableswitch:
 289         {
 290           Bytecode_tableswitch tableswitch(&bytes);
 291 
 292           int len = tableswitch.length();
 293 
 294           dest = _block_map->at(bci + tableswitch.default_offset());
 295           assert(dest != NULL, "branch desination must start a block.");
 296           dest->add_normal_predecessor(current_block);
 297           while (--len >= 0) {
 298             dest = _block_map->at(bci + tableswitch.dest_offset_at(len));
 299             assert(dest != NULL, "branch desination must start a block.");
 300             dest->add_normal_predecessor(current_block);
 301           }
 302           break;
 303         }
 304 
 305       case Bytecodes::_lookupswitch:
 306         {
 307           Bytecode_lookupswitch lookupswitch(&bytes);
 308 
 309           int npairs = lookupswitch.number_of_pairs();
 310 
 311           dest = _block_map->at(bci + lookupswitch.default_offset());
 312           assert(dest != NULL, "branch desination must start a block.");
 313           dest->add_normal_predecessor(current_block);
 314           while(--npairs >= 0) {
 315             LookupswitchPair pair = lookupswitch.pair_at(npairs);
 316             dest = _block_map->at( bci + pair.offset());
 317             assert(dest != NULL, "branch desination must start a block.");
 318             dest->add_normal_predecessor(current_block);
 319           }
 320           break;
 321         }
 322 
 323       case Bytecodes::_jsr:
 324         {
 325           assert(bytes.is_wide()==false, "sanity check");
 326           dest = _block_map->at(bytes.get_dest());
 327           assert(dest != NULL, "branch desination must start a block.");
 328           dest->add_normal_predecessor(current_block);
 329           BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
 330           assert(jsrExit != NULL, "jsr return bci must start a block.");
 331           jsr_exit_list->append(jsrExit);
 332           break;
 333         }
 334       case Bytecodes::_jsr_w:
 335         {
 336           dest = _block_map->at(bytes.get_far_dest());
 337           assert(dest != NULL, "branch desination must start a block.");
 338           dest->add_normal_predecessor(current_block);
 339           BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
 340           assert(jsrExit != NULL, "jsr return bci must start a block.");
 341           jsr_exit_list->append(jsrExit);
 342           break;
 343         }
 344 
 345       case Bytecodes::_wide:
 346         assert(false, "wide opcodes should not be seen here");
 347         break;
 348       case Bytecodes::_athrow:
 349       case Bytecodes::_ireturn:
 350       case Bytecodes::_lreturn:
 351       case Bytecodes::_freturn:
 352       case Bytecodes::_dreturn:
 353       case Bytecodes::_areturn:
 354       case Bytecodes::_return:
 355         // These opcodes are  not the normal predecessors of any other opcodes.
 356         break;
 357       case Bytecodes::_ret:
 358         // We will patch up jsr/rets in a subsequent pass.
 359         ret_list->append(current_block);
 360         break;
 361       case Bytecodes::_breakpoint:
 362         // Bail out of there are breakpoints in here.
 363         bailout = true;
 364         break;
 365       default:
 366         // Do nothing.
 367         break;
 368     }
 369   }
 370   // Patch up the jsr/ret's.  We conservatively assume that any ret
 371   // can return to any jsr site.
 372   int ret_list_len = ret_list->length();
 373   int jsr_exit_list_len = jsr_exit_list->length();
 374   if (ret_list_len > 0 && jsr_exit_list_len > 0) {
 375     for (int i = jsr_exit_list_len - 1; i >= 0; i--) {
 376       BasicBlock *jsrExit = jsr_exit_list->at(i);
 377       for (int i = ret_list_len - 1; i >= 0; i--) {
 378         jsrExit->add_normal_predecessor(ret_list->at(i));
 379       }
 380     }
 381   }
 382 
 383   // Compute exception edges.
 384   for (int b=_block_count-1; b >= 0; b--) {
 385     BasicBlock *block = _block_list[b];
 386     int block_start = block->start_bci();
 387     int block_limit = block->limit_bci();
 388     ciExceptionHandlerStream handlers(method());
 389     for (; !handlers.is_done(); handlers.next()) {
 390       ciExceptionHandler* handler = handlers.handler();
 391       int start       = handler->start();
 392       int limit       = handler->limit();
 393       int handler_bci = handler->handler_bci();
 394 
 395       int intersect_start = MAX2(block_start, start);
 396       int intersect_limit = MIN2(block_limit, limit);
 397       if (intersect_start < intersect_limit) {
 398         // The catch range has a nonempty intersection with this
 399         // basic block.  That means this basic block can be an
 400         // exceptional predecessor.
 401         _block_map->at(handler_bci)->add_exception_predecessor(block);
 402 
 403         if (handler->is_catch_all()) {
 404           // This is a catch-all block.
 405           if (intersect_start == block_start && intersect_limit == block_limit) {
 406             // The basic block is entirely contained in this catch-all block.
 407             // Skip the rest of the exception handlers -- they can never be
 408             // reached in execution.
 409             break;
 410           }
 411         }
 412       }
 413     }
 414   }
 415 }
 416 
 417 void MethodLiveness::init_gen_kill() {
 418   for (int i=_block_count-1; i >= 0; i--) {
 419     _block_list[i]->compute_gen_kill(method());
 420   }
 421 }
 422 
 423 void MethodLiveness::propagate_liveness() {
 424   int num_blocks = _block_count;
 425   BasicBlock *block;
 426 
 427   // We start our work list off with all blocks in it.
 428   // Alternately, we could start off the work list with the list of all
 429   // blocks which could exit the method directly, along with one block
 430   // from any infinite loop.  If this matters, it can be changed.  It
 431   // may not be clear from looking at the code, but the order of the
 432   // workList will be the opposite of the creation order of the basic
 433   // blocks, which should be decent for quick convergence (with the
 434   // possible exception of exception handlers, which are all created
 435   // early).
 436   _work_list = NULL;
 437   for (int i = 0; i < num_blocks; i++) {
 438     block = _block_list[i];
 439     block->set_next(_work_list);
 440     block->set_on_work_list(true);
 441     _work_list = block;
 442   }
 443 
 444 
 445   while ((block = work_list_get()) != NULL) {
 446     block->propagate(this);
 447     NOT_PRODUCT(_total_visits++;)
 448   }
 449 }
 450 
 451 void MethodLiveness::work_list_add(BasicBlock *block) {
 452   if (!block->on_work_list()) {
 453     block->set_next(_work_list);
 454     block->set_on_work_list(true);
 455     _work_list = block;
 456   }
 457 }
 458 
 459 MethodLiveness::BasicBlock *MethodLiveness::work_list_get() {
 460   BasicBlock *block = _work_list;
 461   if (block != NULL) {
 462     block->set_on_work_list(false);
 463     _work_list = block->next();
 464   }
 465   return block;
 466 }
 467 
 468 
 469 MethodLivenessResult MethodLiveness::get_liveness_at(int entry_bci) {
 470   int bci = entry_bci;
 471   bool is_entry = false;
 472   if (entry_bci == InvocationEntryBci) {
 473     is_entry = true;
 474     bci = 0;
 475   }
 476 
 477   MethodLivenessResult answer((BitMap::bm_word_t*)NULL,0);
 478 
 479   if (_block_count > 0) {
 480     if (TimeLivenessAnalysis) _time_total.start();
 481     if (TimeLivenessAnalysis) _time_query.start();
 482 
 483     assert( 0 <= bci && bci < method()->code_size(), "bci out of range" );
 484     BasicBlock *block = _block_map->at(bci);
 485     // We may not be at the block start, so search backwards to find the block
 486     // containing bci.
 487     int t = bci;
 488     while (block == NULL && t > 0) {
 489      block = _block_map->at(--t);
 490     }
 491     assert( block != NULL, "invalid bytecode index; must be instruction index" );
 492     assert(bci >= block->start_bci() && bci < block->limit_bci(), "block must contain bci.");
 493 
 494     answer = block->get_liveness_at(method(), bci);
 495 
 496     if (is_entry && method()->is_synchronized() && !method()->is_static()) {
 497       // Synchronized methods use the receiver once on entry.
 498       answer.at_put(0, true);
 499     }
 500 
 501 #ifndef PRODUCT
 502     if (TraceLivenessQuery) {
 503       tty->print("Liveness query of ");
 504       method()->print_short_name();
 505       tty->print(" @ %d : result is ", bci);
 506       answer.print_on(tty);
 507     }
 508 
 509     if (TimeLivenessAnalysis) _time_query.stop();
 510     if (TimeLivenessAnalysis) _time_total.stop();
 511 #endif
 512   }
 513 
 514 #ifndef PRODUCT
 515   if (TimeLivenessAnalysis) {
 516     // Collect statistics.
 517     _total_locals_queried += _bit_map_size_bits;
 518     BitCounter counter;
 519     answer.iterate(&counter);
 520     _total_live_locals_queried += counter.count();
 521   }
 522 #endif
 523 
 524   return answer;
 525 }
 526 
 527 
 528 #ifndef PRODUCT
 529 
 530 void MethodLiveness::print_times() {
 531   tty->print_cr ("Accumulated liveness analysis times/statistics:");
 532   tty->print_cr ("-----------------------------------------------");
 533   tty->print_cr ("  Total         : %3.3f sec.", _time_total.seconds());
 534   tty->print_cr ("    Build graph : %3.3f sec. (%2.2f%%)", _time_build_graph.seconds(),
 535                  _time_build_graph.seconds() * 100 / _time_total.seconds());
 536   tty->print_cr ("    Gen / Kill  : %3.3f sec. (%2.2f%%)", _time_gen_kill.seconds(),
 537                  _time_gen_kill.seconds() * 100 / _time_total.seconds());
 538   tty->print_cr ("    Dataflow    : %3.3f sec. (%2.2f%%)", _time_flow.seconds(),
 539                  _time_flow.seconds() * 100 / _time_total.seconds());
 540   tty->print_cr ("    Query       : %3.3f sec. (%2.2f%%)", _time_query.seconds(),
 541                  _time_query.seconds() * 100 / _time_total.seconds());
 542   tty->print_cr ("  #bytes   : %8ld (%3.0f bytes per sec)",
 543                  _total_bytes,
 544                  _total_bytes / _time_total.seconds());
 545   tty->print_cr ("  #methods : %8d (%3.0f methods per sec)",
 546                  _total_methods,
 547                  _total_methods / _time_total.seconds());
 548   tty->print_cr ("    avg locals : %3.3f    max locals : %3d",
 549                  (float)_total_method_locals / _total_methods,
 550                  _max_method_locals);
 551   tty->print_cr ("    avg blocks : %3.3f    max blocks : %3d",
 552                  (float)_total_blocks / _total_methods,
 553                  _max_method_blocks);
 554   tty->print_cr ("    avg bytes  : %3.3f",
 555                  (float)_total_bytes / _total_methods);
 556   tty->print_cr ("  #blocks  : %8ld",
 557                  _total_blocks);
 558   tty->print_cr ("    avg normal predecessors    : %3.3f  max normal predecessors    : %3d",
 559                  (float)_total_edges / _total_blocks,
 560                  _max_block_edges);
 561   tty->print_cr ("    avg exception predecessors : %3.3f  max exception predecessors : %3d",
 562                  (float)_total_exc_edges / _total_blocks,
 563                  _max_block_exc_edges);
 564   tty->print_cr ("    avg visits                 : %3.3f",
 565                  (float)_total_visits / _total_blocks);
 566   tty->print_cr ("  #locals queried : %8ld    #live : %8ld   %%live : %2.2f%%",
 567                  _total_locals_queried,
 568                  _total_live_locals_queried,
 569                  100.0 * _total_live_locals_queried / _total_locals_queried);
 570 }
 571 
 572 #endif
 573 
 574 
 575 MethodLiveness::BasicBlock::BasicBlock(MethodLiveness *analyzer, int start, int limit) :
 576          _gen((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
 577                          analyzer->bit_map_size_bits()),
 578          _kill((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
 579                          analyzer->bit_map_size_bits()),
 580          _entry((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
 581                          analyzer->bit_map_size_bits()),
 582          _normal_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
 583                          analyzer->bit_map_size_bits()),
 584          _exception_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
 585                          analyzer->bit_map_size_bits()),
 586          _last_bci(-1) {
 587   _analyzer = analyzer;
 588   _start_bci = start;
 589   _limit_bci = limit;
 590   _normal_predecessors =
 591     new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
 592   _exception_predecessors =
 593     new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
 594   _normal_exit.clear();
 595   _exception_exit.clear();
 596   _entry.clear();
 597 
 598   // this initialization is not strictly necessary.
 599   // _gen and _kill are cleared at the beginning of compute_gen_kill_range()
 600   _gen.clear();
 601   _kill.clear();
 602 }
 603 
 604 
 605 
 606 MethodLiveness::BasicBlock *MethodLiveness::BasicBlock::split(int split_bci) {
 607   int start = _start_bci;
 608   int limit = _limit_bci;
 609 
 610   if (TraceLivenessGen) {
 611     tty->print_cr(" ** Splitting block (%d,%d) at %d", start, limit, split_bci);
 612   }
 613 
 614   GrowableArray<BasicBlock*>* save_predecessors = _normal_predecessors;
 615 
 616   assert (start < split_bci && split_bci < limit, "improper split");
 617 
 618   // Make a new block to cover the first half of the range.
 619   BasicBlock *first_half = new (_analyzer->arena()) BasicBlock(_analyzer, start, split_bci);
 620 
 621   // Assign correct values to the second half (this)
 622   _normal_predecessors = first_half->_normal_predecessors;
 623   _start_bci = split_bci;
 624   add_normal_predecessor(first_half);
 625 
 626   // Assign correct predecessors to the new first half
 627   first_half->_normal_predecessors = save_predecessors;
 628 
 629   return first_half;
 630 }
 631 
 632 void MethodLiveness::BasicBlock::compute_gen_kill(ciMethod* method) {
 633   ciBytecodeStream bytes(method);
 634   bytes.reset_to_bci(start_bci());
 635   bytes.set_max_bci(limit_bci());
 636   compute_gen_kill_range(&bytes);
 637 
 638 }
 639 
 640 void MethodLiveness::BasicBlock::compute_gen_kill_range(ciBytecodeStream *bytes) {
 641   _gen.clear();
 642   _kill.clear();
 643 
 644   while (bytes->next() != ciBytecodeStream::EOBC()) {
 645     compute_gen_kill_single(bytes);
 646   }
 647 }
 648 
 649 void MethodLiveness::BasicBlock::compute_gen_kill_single(ciBytecodeStream *instruction) {
 650   int localNum;
 651 
 652   // We prohibit _gen and _kill from having locals in common.  If we
 653   // know that one is definitely going to be applied before the other,
 654   // we could save some computation time by relaxing this prohibition.
 655 
 656   switch (instruction->cur_bc()) {
 657     case Bytecodes::_nop:
 658     case Bytecodes::_goto:
 659     case Bytecodes::_goto_w:
 660     case Bytecodes::_aconst_null:
 661     case Bytecodes::_new:
 662     case Bytecodes::_iconst_m1:
 663     case Bytecodes::_iconst_0:
 664     case Bytecodes::_iconst_1:
 665     case Bytecodes::_iconst_2:
 666     case Bytecodes::_iconst_3:
 667     case Bytecodes::_iconst_4:
 668     case Bytecodes::_iconst_5:
 669     case Bytecodes::_fconst_0:
 670     case Bytecodes::_fconst_1:
 671     case Bytecodes::_fconst_2:
 672     case Bytecodes::_bipush:
 673     case Bytecodes::_sipush:
 674     case Bytecodes::_lconst_0:
 675     case Bytecodes::_lconst_1:
 676     case Bytecodes::_dconst_0:
 677     case Bytecodes::_dconst_1:
 678     case Bytecodes::_ldc2_w:
 679     case Bytecodes::_ldc:
 680     case Bytecodes::_ldc_w:
 681     case Bytecodes::_iaload:
 682     case Bytecodes::_faload:
 683     case Bytecodes::_baload:
 684     case Bytecodes::_caload:
 685     case Bytecodes::_saload:
 686     case Bytecodes::_laload:
 687     case Bytecodes::_daload:
 688     case Bytecodes::_aaload:
 689     case Bytecodes::_iastore:
 690     case Bytecodes::_fastore:
 691     case Bytecodes::_bastore:
 692     case Bytecodes::_castore:
 693     case Bytecodes::_sastore:
 694     case Bytecodes::_lastore:
 695     case Bytecodes::_dastore:
 696     case Bytecodes::_aastore:
 697     case Bytecodes::_pop:
 698     case Bytecodes::_pop2:
 699     case Bytecodes::_dup:
 700     case Bytecodes::_dup_x1:
 701     case Bytecodes::_dup_x2:
 702     case Bytecodes::_dup2:
 703     case Bytecodes::_dup2_x1:
 704     case Bytecodes::_dup2_x2:
 705     case Bytecodes::_swap:
 706     case Bytecodes::_iadd:
 707     case Bytecodes::_fadd:
 708     case Bytecodes::_isub:
 709     case Bytecodes::_fsub:
 710     case Bytecodes::_imul:
 711     case Bytecodes::_fmul:
 712     case Bytecodes::_idiv:
 713     case Bytecodes::_fdiv:
 714     case Bytecodes::_irem:
 715     case Bytecodes::_frem:
 716     case Bytecodes::_ishl:
 717     case Bytecodes::_ishr:
 718     case Bytecodes::_iushr:
 719     case Bytecodes::_iand:
 720     case Bytecodes::_ior:
 721     case Bytecodes::_ixor:
 722     case Bytecodes::_l2f:
 723     case Bytecodes::_l2i:
 724     case Bytecodes::_d2f:
 725     case Bytecodes::_d2i:
 726     case Bytecodes::_fcmpl:
 727     case Bytecodes::_fcmpg:
 728     case Bytecodes::_ladd:
 729     case Bytecodes::_dadd:
 730     case Bytecodes::_lsub:
 731     case Bytecodes::_dsub:
 732     case Bytecodes::_lmul:
 733     case Bytecodes::_dmul:
 734     case Bytecodes::_ldiv:
 735     case Bytecodes::_ddiv:
 736     case Bytecodes::_lrem:
 737     case Bytecodes::_drem:
 738     case Bytecodes::_land:
 739     case Bytecodes::_lor:
 740     case Bytecodes::_lxor:
 741     case Bytecodes::_ineg:
 742     case Bytecodes::_fneg:
 743     case Bytecodes::_i2f:
 744     case Bytecodes::_f2i:
 745     case Bytecodes::_i2c:
 746     case Bytecodes::_i2s:
 747     case Bytecodes::_i2b:
 748     case Bytecodes::_lneg:
 749     case Bytecodes::_dneg:
 750     case Bytecodes::_l2d:
 751     case Bytecodes::_d2l:
 752     case Bytecodes::_lshl:
 753     case Bytecodes::_lshr:
 754     case Bytecodes::_lushr:
 755     case Bytecodes::_i2l:
 756     case Bytecodes::_i2d:
 757     case Bytecodes::_f2l:
 758     case Bytecodes::_f2d:
 759     case Bytecodes::_lcmp:
 760     case Bytecodes::_dcmpl:
 761     case Bytecodes::_dcmpg:
 762     case Bytecodes::_ifeq:
 763     case Bytecodes::_ifne:
 764     case Bytecodes::_iflt:
 765     case Bytecodes::_ifge:
 766     case Bytecodes::_ifgt:
 767     case Bytecodes::_ifle:
 768     case Bytecodes::_tableswitch:
 769     case Bytecodes::_ireturn:
 770     case Bytecodes::_freturn:
 771     case Bytecodes::_if_icmpeq:
 772     case Bytecodes::_if_icmpne:
 773     case Bytecodes::_if_icmplt:
 774     case Bytecodes::_if_icmpge:
 775     case Bytecodes::_if_icmpgt:
 776     case Bytecodes::_if_icmple:
 777     case Bytecodes::_lreturn:
 778     case Bytecodes::_dreturn:
 779     case Bytecodes::_if_acmpeq:
 780     case Bytecodes::_if_acmpne:
 781     case Bytecodes::_jsr:
 782     case Bytecodes::_jsr_w:
 783     case Bytecodes::_getstatic:
 784     case Bytecodes::_putstatic:
 785     case Bytecodes::_getfield:
 786     case Bytecodes::_putfield:
 787     case Bytecodes::_invokevirtual:
 788     case Bytecodes::_invokespecial:
 789     case Bytecodes::_invokestatic:
 790     case Bytecodes::_invokeinterface:
 791     case Bytecodes::_invokedynamic:
 792     case Bytecodes::_newarray:
 793     case Bytecodes::_anewarray:
 794     case Bytecodes::_checkcast:
 795     case Bytecodes::_arraylength:
 796     case Bytecodes::_instanceof:
 797     case Bytecodes::_athrow:
 798     case Bytecodes::_areturn:
 799     case Bytecodes::_monitorenter:
 800     case Bytecodes::_monitorexit:
 801     case Bytecodes::_ifnull:
 802     case Bytecodes::_ifnonnull:
 803     case Bytecodes::_multianewarray:
 804     case Bytecodes::_lookupswitch:
 805       // These bytecodes have no effect on the method's locals.
 806       break;
 807 
 808     case Bytecodes::_return:
 809       if (instruction->method()->intrinsic_id() == vmIntrinsics::_Object_init) {
 810         // return from Object.init implicitly registers a finalizer
 811         // for the receiver if needed, so keep it alive.
 812         load_one(0);
 813       }
 814       break;
 815 
 816 
 817     case Bytecodes::_lload:
 818     case Bytecodes::_dload:
 819       load_two(instruction->get_index());
 820       break;
 821 
 822     case Bytecodes::_lload_0:
 823     case Bytecodes::_dload_0:
 824       load_two(0);
 825       break;
 826 
 827     case Bytecodes::_lload_1:
 828     case Bytecodes::_dload_1:
 829       load_two(1);
 830       break;
 831 
 832     case Bytecodes::_lload_2:
 833     case Bytecodes::_dload_2:
 834       load_two(2);
 835       break;
 836 
 837     case Bytecodes::_lload_3:
 838     case Bytecodes::_dload_3:
 839       load_two(3);
 840       break;
 841 
 842     case Bytecodes::_iload:
 843     case Bytecodes::_iinc:
 844     case Bytecodes::_fload:
 845     case Bytecodes::_aload:
 846     case Bytecodes::_ret:
 847       load_one(instruction->get_index());
 848       break;
 849 
 850     case Bytecodes::_iload_0:
 851     case Bytecodes::_fload_0:
 852     case Bytecodes::_aload_0:
 853       load_one(0);
 854       break;
 855 
 856     case Bytecodes::_iload_1:
 857     case Bytecodes::_fload_1:
 858     case Bytecodes::_aload_1:
 859       load_one(1);
 860       break;
 861 
 862     case Bytecodes::_iload_2:
 863     case Bytecodes::_fload_2:
 864     case Bytecodes::_aload_2:
 865       load_one(2);
 866       break;
 867 
 868     case Bytecodes::_iload_3:
 869     case Bytecodes::_fload_3:
 870     case Bytecodes::_aload_3:
 871       load_one(3);
 872       break;
 873 
 874     case Bytecodes::_lstore:
 875     case Bytecodes::_dstore:
 876       store_two(localNum = instruction->get_index());
 877       break;
 878 
 879     case Bytecodes::_lstore_0:
 880     case Bytecodes::_dstore_0:
 881       store_two(0);
 882       break;
 883 
 884     case Bytecodes::_lstore_1:
 885     case Bytecodes::_dstore_1:
 886       store_two(1);
 887       break;
 888 
 889     case Bytecodes::_lstore_2:
 890     case Bytecodes::_dstore_2:
 891       store_two(2);
 892       break;
 893 
 894     case Bytecodes::_lstore_3:
 895     case Bytecodes::_dstore_3:
 896       store_two(3);
 897       break;
 898 
 899     case Bytecodes::_istore:
 900     case Bytecodes::_fstore:
 901     case Bytecodes::_astore:
 902       store_one(instruction->get_index());
 903       break;
 904 
 905     case Bytecodes::_istore_0:
 906     case Bytecodes::_fstore_0:
 907     case Bytecodes::_astore_0:
 908       store_one(0);
 909       break;
 910 
 911     case Bytecodes::_istore_1:
 912     case Bytecodes::_fstore_1:
 913     case Bytecodes::_astore_1:
 914       store_one(1);
 915       break;
 916 
 917     case Bytecodes::_istore_2:
 918     case Bytecodes::_fstore_2:
 919     case Bytecodes::_astore_2:
 920       store_one(2);
 921       break;
 922 
 923     case Bytecodes::_istore_3:
 924     case Bytecodes::_fstore_3:
 925     case Bytecodes::_astore_3:
 926       store_one(3);
 927       break;
 928 
 929     case Bytecodes::_wide:
 930       fatal("Iterator should skip this bytecode");
 931       break;
 932 
 933     default:
 934       tty->print("unexpected opcode: %d\n", instruction->cur_bc());
 935       ShouldNotReachHere();
 936       break;
 937   }
 938 }
 939 
 940 void MethodLiveness::BasicBlock::load_two(int local) {
 941   load_one(local);
 942   load_one(local+1);
 943 }
 944 
 945 void MethodLiveness::BasicBlock::load_one(int local) {
 946   if (!_kill.at(local)) {
 947     _gen.at_put(local, true);
 948   }
 949 }
 950 
 951 void MethodLiveness::BasicBlock::store_two(int local) {
 952   store_one(local);
 953   store_one(local+1);
 954 }
 955 
 956 void MethodLiveness::BasicBlock::store_one(int local) {
 957   if (!_gen.at(local)) {
 958     _kill.at_put(local, true);
 959   }
 960 }
 961 
 962 void MethodLiveness::BasicBlock::propagate(MethodLiveness *ml) {
 963   // These set operations could be combined for efficiency if the
 964   // performance of this analysis becomes an issue.
 965   _entry.set_union(_normal_exit);
 966   _entry.set_difference(_kill);
 967   _entry.set_union(_gen);
 968 
 969   // Note that we merge information from our exceptional successors
 970   // just once, rather than at individual bytecodes.
 971   _entry.set_union(_exception_exit);
 972 
 973   if (TraceLivenessGen) {
 974     tty->print_cr(" ** Visiting block at %d **", start_bci());
 975     print_on(tty);
 976   }
 977 
 978   int i;
 979   for (i=_normal_predecessors->length()-1; i>=0; i--) {
 980     BasicBlock *block = _normal_predecessors->at(i);
 981     if (block->merge_normal(_entry)) {
 982       ml->work_list_add(block);
 983     }
 984   }
 985   for (i=_exception_predecessors->length()-1; i>=0; i--) {
 986     BasicBlock *block = _exception_predecessors->at(i);
 987     if (block->merge_exception(_entry)) {
 988       ml->work_list_add(block);
 989     }
 990   }
 991 }
 992 
 993 bool MethodLiveness::BasicBlock::merge_normal(BitMap other) {
 994   return _normal_exit.set_union_with_result(other);
 995 }
 996 
 997 bool MethodLiveness::BasicBlock::merge_exception(BitMap other) {
 998   return _exception_exit.set_union_with_result(other);
 999 }
1000 
1001 MethodLivenessResult MethodLiveness::BasicBlock::get_liveness_at(ciMethod* method, int bci) {
1002   MethodLivenessResult answer(NEW_RESOURCE_ARRAY(BitMap::bm_word_t, _analyzer->bit_map_size_words()),
1003                 _analyzer->bit_map_size_bits());
1004   answer.set_is_valid();
1005 
1006 #ifndef ASSERT
1007   if (bci == start_bci()) {
1008     answer.set_from(_entry);
1009     return answer;
1010   }
1011 #endif
1012 
1013 #ifdef ASSERT
1014   ResourceMark rm;
1015   BitMap g(_gen.size()); g.set_from(_gen);
1016   BitMap k(_kill.size()); k.set_from(_kill);
1017 #endif
1018   if (_last_bci != bci || trueInDebug) {
1019     ciBytecodeStream bytes(method);
1020     bytes.reset_to_bci(bci);
1021     bytes.set_max_bci(limit_bci());
1022     compute_gen_kill_range(&bytes);
1023     assert(_last_bci != bci ||
1024            (g.is_same(_gen) && k.is_same(_kill)), "cached computation is incorrect");
1025     _last_bci = bci;
1026   }
1027 
1028   answer.clear();
1029   answer.set_union(_normal_exit);
1030   answer.set_difference(_kill);
1031   answer.set_union(_gen);
1032   answer.set_union(_exception_exit);
1033 
1034 #ifdef ASSERT
1035   if (bci == start_bci()) {
1036     assert(answer.is_same(_entry), "optimized answer must be accurate");
1037   }
1038 #endif
1039 
1040   return answer;
1041 }
1042 
1043 #ifndef PRODUCT
1044 
1045 void MethodLiveness::BasicBlock::print_on(outputStream *os) const {
1046   os->print_cr("===================================================================");
1047   os->print_cr("    Block start: %4d, limit: %4d", _start_bci, _limit_bci);
1048   os->print   ("    Normal predecessors (%2d)      @", _normal_predecessors->length());
1049   int i;
1050   for (i=0; i < _normal_predecessors->length(); i++) {
1051     os->print(" %4d", _normal_predecessors->at(i)->start_bci());
1052   }
1053   os->cr();
1054   os->print   ("    Exceptional predecessors (%2d) @", _exception_predecessors->length());
1055   for (i=0; i < _exception_predecessors->length(); i++) {
1056     os->print(" %4d", _exception_predecessors->at(i)->start_bci());
1057   }
1058   os->cr();
1059   os->print ("    Normal Exit   : ");
1060   _normal_exit.print_on(os);
1061   os->print ("    Gen           : ");
1062   _gen.print_on(os);
1063   os->print ("    Kill          : ");
1064   _kill.print_on(os);
1065   os->print ("    Exception Exit: ");
1066   _exception_exit.print_on(os);
1067   os->print ("    Entry         : ");
1068   _entry.print_on(os);
1069 }
1070 
1071 #endif // PRODUCT