Path: blob/master/src/hotspot/share/compiler/methodLiveness.cpp
64440 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"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"3536// The MethodLiveness class performs a simple liveness analysis on a method37// in order to decide which locals are live (that is, will be used again) at38// 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 we43// also keep track of its set of predecessors through normal control flow44// and predecessors through exceptional control flow.45//46// 2. For each basic block, compute two sets, gen (the set of values used before47// 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 to49// perform its computation. A basic block "provides" values for the locals in50// 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 through53// the program, from blocks to their predecessors. We compute and store liveness54// information for the normal/exceptional exit paths for each basic block. When55// 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, we58// compute gen/kill sets which represent execution from that bci to the exit of59// its blocks. We then compose this range gen/kill information with the normal60// and exceptional exit information for the block to produce liveness information61// 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 any67// jsr return site.68// 2. Instead of computing the effects of exceptions at every instruction, we69// summarize the effects of all exceptional continuations from the block as70// a single set (_exception_exit), losing some information but simplifying the71// analysis.7273MethodLiveness::MethodLiveness(Arena* arena, ciMethod* method)74#ifdef COMPILER175: _bci_block_start(arena, method->code_size())76#endif77{78_arena = arena;79_method = method;80_bit_map_size_bits = method->max_locals();81}8283void MethodLiveness::compute_liveness() {84#ifndef PRODUCT85if (TraceLivenessGen) {86tty->print_cr("################################################################");87tty->print("# Computing liveness information for ");88method()->print_short_name();89}90#endif9192init_basic_blocks();93init_gen_kill();94propagate_liveness();95}969798void MethodLiveness::init_basic_blocks() {99bool bailout = false;100101int method_len = method()->code_size();102ciMethodBlocks *mblocks = method()->get_method_blocks();103104// Create an array to store the bci->BasicBlock mapping.105_block_map = new (arena()) GrowableArray<BasicBlock*>(arena(), method_len, method_len, NULL);106107_block_count = mblocks->num_blocks();108_block_list = (BasicBlock **) arena()->Amalloc(sizeof(BasicBlock *) * _block_count);109110// Used for patching up jsr/ret control flow.111GrowableArray<BasicBlock*>* jsr_exit_list = new GrowableArray<BasicBlock*>(5);112GrowableArray<BasicBlock*>* ret_list = new GrowableArray<BasicBlock*>(5);113114// generate our block list from ciMethodBlocks115for (int blk = 0; blk < _block_count; blk++) {116ciBlock *cib = mblocks->block(blk);117int start_bci = cib->start_bci();118_block_list[blk] = new (arena()) BasicBlock(this, start_bci, cib->limit_bci());119_block_map->at_put(start_bci, _block_list[blk]);120#ifdef COMPILER1121// mark all bcis where a new basic block starts122_bci_block_start.set_bit(start_bci);123#endif // COMPILER1124}125// fill in the predecessors of blocks126ciBytecodeStream bytes(method());127128for (int blk = 0; blk < _block_count; blk++) {129BasicBlock *current_block = _block_list[blk];130int bci = mblocks->block(blk)->control_bci();131132if (bci == ciBlock::fall_through_bci) {133int limit = current_block->limit_bci();134if (limit < method_len) {135BasicBlock *next = _block_map->at(limit);136assert( next != NULL, "must be a block immediately following this one.");137next->add_normal_predecessor(current_block);138}139continue;140}141bytes.reset_to_bci(bci);142Bytecodes::Code code = bytes.next();143BasicBlock *dest;144145// Now we need to interpret the instruction's effect146// on control flow.147assert (current_block != NULL, "we must have a current block");148switch (code) {149case Bytecodes::_ifeq:150case Bytecodes::_ifne:151case Bytecodes::_iflt:152case Bytecodes::_ifge:153case Bytecodes::_ifgt:154case Bytecodes::_ifle:155case Bytecodes::_if_icmpeq:156case Bytecodes::_if_icmpne:157case Bytecodes::_if_icmplt:158case Bytecodes::_if_icmpge:159case Bytecodes::_if_icmpgt:160case Bytecodes::_if_icmple:161case Bytecodes::_if_acmpeq:162case Bytecodes::_if_acmpne:163case Bytecodes::_ifnull:164case Bytecodes::_ifnonnull:165// Two way branch. Set predecessors at each destination.166if (bytes.next_bci() < method_len) {167dest = _block_map->at(bytes.next_bci());168assert(dest != NULL, "must be a block immediately following this one.");169dest->add_normal_predecessor(current_block);170}171dest = _block_map->at(bytes.get_dest());172assert(dest != NULL, "branch desination must start a block.");173dest->add_normal_predecessor(current_block);174break;175case Bytecodes::_goto:176dest = _block_map->at(bytes.get_dest());177assert(dest != NULL, "branch desination must start a block.");178dest->add_normal_predecessor(current_block);179break;180case Bytecodes::_goto_w:181dest = _block_map->at(bytes.get_far_dest());182assert(dest != NULL, "branch desination must start a block.");183dest->add_normal_predecessor(current_block);184break;185case Bytecodes::_tableswitch:186{187Bytecode_tableswitch tableswitch(&bytes);188189int len = tableswitch.length();190191dest = _block_map->at(bci + tableswitch.default_offset());192assert(dest != NULL, "branch desination must start a block.");193dest->add_normal_predecessor(current_block);194while (--len >= 0) {195dest = _block_map->at(bci + tableswitch.dest_offset_at(len));196assert(dest != NULL, "branch desination must start a block.");197dest->add_normal_predecessor(current_block);198}199break;200}201202case Bytecodes::_lookupswitch:203{204Bytecode_lookupswitch lookupswitch(&bytes);205206int npairs = lookupswitch.number_of_pairs();207208dest = _block_map->at(bci + lookupswitch.default_offset());209assert(dest != NULL, "branch desination must start a block.");210dest->add_normal_predecessor(current_block);211while(--npairs >= 0) {212LookupswitchPair pair = lookupswitch.pair_at(npairs);213dest = _block_map->at( bci + pair.offset());214assert(dest != NULL, "branch desination must start a block.");215dest->add_normal_predecessor(current_block);216}217break;218}219220case Bytecodes::_jsr:221{222assert(bytes.is_wide()==false, "sanity check");223dest = _block_map->at(bytes.get_dest());224assert(dest != NULL, "branch desination must start a block.");225dest->add_normal_predecessor(current_block);226BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());227assert(jsrExit != NULL, "jsr return bci must start a block.");228jsr_exit_list->append(jsrExit);229break;230}231case Bytecodes::_jsr_w:232{233dest = _block_map->at(bytes.get_far_dest());234assert(dest != NULL, "branch desination must start a block.");235dest->add_normal_predecessor(current_block);236BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());237assert(jsrExit != NULL, "jsr return bci must start a block.");238jsr_exit_list->append(jsrExit);239break;240}241242case Bytecodes::_wide:243assert(false, "wide opcodes should not be seen here");244break;245case Bytecodes::_athrow:246case Bytecodes::_ireturn:247case Bytecodes::_lreturn:248case Bytecodes::_freturn:249case Bytecodes::_dreturn:250case Bytecodes::_areturn:251case Bytecodes::_return:252// These opcodes are not the normal predecessors of any other opcodes.253break;254case Bytecodes::_ret:255// We will patch up jsr/rets in a subsequent pass.256ret_list->append(current_block);257break;258case Bytecodes::_breakpoint:259// Bail out of there are breakpoints in here.260bailout = true;261break;262default:263// Do nothing.264break;265}266}267// Patch up the jsr/ret's. We conservatively assume that any ret268// can return to any jsr site.269int ret_list_len = ret_list->length();270int jsr_exit_list_len = jsr_exit_list->length();271if (ret_list_len > 0 && jsr_exit_list_len > 0) {272for (int i = jsr_exit_list_len - 1; i >= 0; i--) {273BasicBlock *jsrExit = jsr_exit_list->at(i);274for (int i = ret_list_len - 1; i >= 0; i--) {275jsrExit->add_normal_predecessor(ret_list->at(i));276}277}278}279280// Compute exception edges.281for (int b=_block_count-1; b >= 0; b--) {282BasicBlock *block = _block_list[b];283int block_start = block->start_bci();284int block_limit = block->limit_bci();285ciExceptionHandlerStream handlers(method());286for (; !handlers.is_done(); handlers.next()) {287ciExceptionHandler* handler = handlers.handler();288int start = handler->start();289int limit = handler->limit();290int handler_bci = handler->handler_bci();291292int intersect_start = MAX2(block_start, start);293int intersect_limit = MIN2(block_limit, limit);294if (intersect_start < intersect_limit) {295// The catch range has a nonempty intersection with this296// basic block. That means this basic block can be an297// exceptional predecessor.298_block_map->at(handler_bci)->add_exception_predecessor(block);299300if (handler->is_catch_all()) {301// This is a catch-all block.302if (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 be305// reached in execution.306break;307}308}309}310}311}312}313314void MethodLiveness::init_gen_kill() {315for (int i=_block_count-1; i >= 0; i--) {316_block_list[i]->compute_gen_kill(method());317}318}319320void MethodLiveness::propagate_liveness() {321int num_blocks = _block_count;322BasicBlock *block;323324// We start our work list off with all blocks in it.325// Alternately, we could start off the work list with the list of all326// blocks which could exit the method directly, along with one block327// from any infinite loop. If this matters, it can be changed. It328// may not be clear from looking at the code, but the order of the329// workList will be the opposite of the creation order of the basic330// blocks, which should be decent for quick convergence (with the331// possible exception of exception handlers, which are all created332// early).333_work_list = NULL;334for (int i = 0; i < num_blocks; i++) {335block = _block_list[i];336block->set_next(_work_list);337block->set_on_work_list(true);338_work_list = block;339}340341342while ((block = work_list_get()) != NULL) {343block->propagate(this);344}345}346347void MethodLiveness::work_list_add(BasicBlock *block) {348if (!block->on_work_list()) {349block->set_next(_work_list);350block->set_on_work_list(true);351_work_list = block;352}353}354355MethodLiveness::BasicBlock *MethodLiveness::work_list_get() {356BasicBlock *block = _work_list;357if (block != NULL) {358block->set_on_work_list(false);359_work_list = block->next();360}361return block;362}363364365MethodLivenessResult MethodLiveness::get_liveness_at(int entry_bci) {366int bci = entry_bci;367bool is_entry = false;368if (entry_bci == InvocationEntryBci) {369is_entry = true;370bci = 0;371}372373MethodLivenessResult answer;374375if (_block_count > 0) {376377assert( 0 <= bci && bci < method()->code_size(), "bci out of range" );378BasicBlock *block = _block_map->at(bci);379// We may not be at the block start, so search backwards to find the block380// containing bci.381int t = bci;382while (block == NULL && t > 0) {383block = _block_map->at(--t);384}385guarantee(block != NULL, "invalid bytecode index; must be instruction index");386assert(bci >= block->start_bci() && bci < block->limit_bci(), "block must contain bci.");387388answer = block->get_liveness_at(method(), bci);389390if (is_entry && method()->is_synchronized() && !method()->is_static()) {391// Synchronized methods use the receiver once on entry.392answer.at_put(0, true);393}394395#ifndef PRODUCT396if (TraceLivenessQuery) {397tty->print("Liveness query of ");398method()->print_short_name();399tty->print(" @ %d : result is ", bci);400answer.print_on(tty);401}402#endif403}404405return answer;406}407408MethodLiveness::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 =419new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);420_exception_predecessors =421new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);422}423424425426MethodLiveness::BasicBlock *MethodLiveness::BasicBlock::split(int split_bci) {427int start = _start_bci;428int limit = _limit_bci;429430if (TraceLivenessGen) {431tty->print_cr(" ** Splitting block (%d,%d) at %d", start, limit, split_bci);432}433434GrowableArray<BasicBlock*>* save_predecessors = _normal_predecessors;435436assert (start < split_bci && split_bci < limit, "improper split");437438// Make a new block to cover the first half of the range.439BasicBlock *first_half = new (_analyzer->arena()) BasicBlock(_analyzer, start, split_bci);440441// Assign correct values to the second half (this)442_normal_predecessors = first_half->_normal_predecessors;443_start_bci = split_bci;444add_normal_predecessor(first_half);445446// Assign correct predecessors to the new first half447first_half->_normal_predecessors = save_predecessors;448449return first_half;450}451452void MethodLiveness::BasicBlock::compute_gen_kill(ciMethod* method) {453ciBytecodeStream bytes(method);454bytes.reset_to_bci(start_bci());455bytes.set_max_bci(limit_bci());456compute_gen_kill_range(&bytes);457458}459460void MethodLiveness::BasicBlock::compute_gen_kill_range(ciBytecodeStream *bytes) {461_gen.clear();462_kill.clear();463464while (bytes->next() != ciBytecodeStream::EOBC()) {465compute_gen_kill_single(bytes);466}467}468469void MethodLiveness::BasicBlock::compute_gen_kill_single(ciBytecodeStream *instruction) {470// We prohibit _gen and _kill from having locals in common. If we471// know that one is definitely going to be applied before the other,472// we could save some computation time by relaxing this prohibition.473474switch (instruction->cur_bc()) {475case Bytecodes::_nop:476case Bytecodes::_goto:477case Bytecodes::_goto_w:478case Bytecodes::_aconst_null:479case Bytecodes::_new:480case Bytecodes::_iconst_m1:481case Bytecodes::_iconst_0:482case Bytecodes::_iconst_1:483case Bytecodes::_iconst_2:484case Bytecodes::_iconst_3:485case Bytecodes::_iconst_4:486case Bytecodes::_iconst_5:487case Bytecodes::_fconst_0:488case Bytecodes::_fconst_1:489case Bytecodes::_fconst_2:490case Bytecodes::_bipush:491case Bytecodes::_sipush:492case Bytecodes::_lconst_0:493case Bytecodes::_lconst_1:494case Bytecodes::_dconst_0:495case Bytecodes::_dconst_1:496case Bytecodes::_ldc2_w:497case Bytecodes::_ldc:498case Bytecodes::_ldc_w:499case Bytecodes::_iaload:500case Bytecodes::_faload:501case Bytecodes::_baload:502case Bytecodes::_caload:503case Bytecodes::_saload:504case Bytecodes::_laload:505case Bytecodes::_daload:506case Bytecodes::_aaload:507case Bytecodes::_iastore:508case Bytecodes::_fastore:509case Bytecodes::_bastore:510case Bytecodes::_castore:511case Bytecodes::_sastore:512case Bytecodes::_lastore:513case Bytecodes::_dastore:514case Bytecodes::_aastore:515case Bytecodes::_pop:516case Bytecodes::_pop2:517case Bytecodes::_dup:518case Bytecodes::_dup_x1:519case Bytecodes::_dup_x2:520case Bytecodes::_dup2:521case Bytecodes::_dup2_x1:522case Bytecodes::_dup2_x2:523case Bytecodes::_swap:524case Bytecodes::_iadd:525case Bytecodes::_fadd:526case Bytecodes::_isub:527case Bytecodes::_fsub:528case Bytecodes::_imul:529case Bytecodes::_fmul:530case Bytecodes::_idiv:531case Bytecodes::_fdiv:532case Bytecodes::_irem:533case Bytecodes::_frem:534case Bytecodes::_ishl:535case Bytecodes::_ishr:536case Bytecodes::_iushr:537case Bytecodes::_iand:538case Bytecodes::_ior:539case Bytecodes::_ixor:540case Bytecodes::_l2f:541case Bytecodes::_l2i:542case Bytecodes::_d2f:543case Bytecodes::_d2i:544case Bytecodes::_fcmpl:545case Bytecodes::_fcmpg:546case Bytecodes::_ladd:547case Bytecodes::_dadd:548case Bytecodes::_lsub:549case Bytecodes::_dsub:550case Bytecodes::_lmul:551case Bytecodes::_dmul:552case Bytecodes::_ldiv:553case Bytecodes::_ddiv:554case Bytecodes::_lrem:555case Bytecodes::_drem:556case Bytecodes::_land:557case Bytecodes::_lor:558case Bytecodes::_lxor:559case Bytecodes::_ineg:560case Bytecodes::_fneg:561case Bytecodes::_i2f:562case Bytecodes::_f2i:563case Bytecodes::_i2c:564case Bytecodes::_i2s:565case Bytecodes::_i2b:566case Bytecodes::_lneg:567case Bytecodes::_dneg:568case Bytecodes::_l2d:569case Bytecodes::_d2l:570case Bytecodes::_lshl:571case Bytecodes::_lshr:572case Bytecodes::_lushr:573case Bytecodes::_i2l:574case Bytecodes::_i2d:575case Bytecodes::_f2l:576case Bytecodes::_f2d:577case Bytecodes::_lcmp:578case Bytecodes::_dcmpl:579case Bytecodes::_dcmpg:580case Bytecodes::_ifeq:581case Bytecodes::_ifne:582case Bytecodes::_iflt:583case Bytecodes::_ifge:584case Bytecodes::_ifgt:585case Bytecodes::_ifle:586case Bytecodes::_tableswitch:587case Bytecodes::_ireturn:588case Bytecodes::_freturn:589case Bytecodes::_if_icmpeq:590case Bytecodes::_if_icmpne:591case Bytecodes::_if_icmplt:592case Bytecodes::_if_icmpge:593case Bytecodes::_if_icmpgt:594case Bytecodes::_if_icmple:595case Bytecodes::_lreturn:596case Bytecodes::_dreturn:597case Bytecodes::_if_acmpeq:598case Bytecodes::_if_acmpne:599case Bytecodes::_jsr:600case Bytecodes::_jsr_w:601case Bytecodes::_getstatic:602case Bytecodes::_putstatic:603case Bytecodes::_getfield:604case Bytecodes::_putfield:605case Bytecodes::_invokevirtual:606case Bytecodes::_invokespecial:607case Bytecodes::_invokestatic:608case Bytecodes::_invokeinterface:609case Bytecodes::_invokedynamic:610case Bytecodes::_newarray:611case Bytecodes::_anewarray:612case Bytecodes::_checkcast:613case Bytecodes::_arraylength:614case Bytecodes::_instanceof:615case Bytecodes::_athrow:616case Bytecodes::_areturn:617case Bytecodes::_monitorenter:618case Bytecodes::_monitorexit:619case Bytecodes::_ifnull:620case Bytecodes::_ifnonnull:621case Bytecodes::_multianewarray:622case Bytecodes::_lookupswitch:623// These bytecodes have no effect on the method's locals.624break;625626case Bytecodes::_return:627if (instruction->method()->intrinsic_id() == vmIntrinsics::_Object_init) {628// return from Object.init implicitly registers a finalizer629// for the receiver if needed, so keep it alive.630load_one(0);631}632break;633634635case Bytecodes::_lload:636case Bytecodes::_dload:637load_two(instruction->get_index());638break;639640case Bytecodes::_lload_0:641case Bytecodes::_dload_0:642load_two(0);643break;644645case Bytecodes::_lload_1:646case Bytecodes::_dload_1:647load_two(1);648break;649650case Bytecodes::_lload_2:651case Bytecodes::_dload_2:652load_two(2);653break;654655case Bytecodes::_lload_3:656case Bytecodes::_dload_3:657load_two(3);658break;659660case Bytecodes::_iload:661case Bytecodes::_iinc:662case Bytecodes::_fload:663case Bytecodes::_aload:664case Bytecodes::_ret:665load_one(instruction->get_index());666break;667668case Bytecodes::_iload_0:669case Bytecodes::_fload_0:670case Bytecodes::_aload_0:671load_one(0);672break;673674case Bytecodes::_iload_1:675case Bytecodes::_fload_1:676case Bytecodes::_aload_1:677load_one(1);678break;679680case Bytecodes::_iload_2:681case Bytecodes::_fload_2:682case Bytecodes::_aload_2:683load_one(2);684break;685686case Bytecodes::_iload_3:687case Bytecodes::_fload_3:688case Bytecodes::_aload_3:689load_one(3);690break;691692case Bytecodes::_lstore:693case Bytecodes::_dstore:694store_two(instruction->get_index());695break;696697case Bytecodes::_lstore_0:698case Bytecodes::_dstore_0:699store_two(0);700break;701702case Bytecodes::_lstore_1:703case Bytecodes::_dstore_1:704store_two(1);705break;706707case Bytecodes::_lstore_2:708case Bytecodes::_dstore_2:709store_two(2);710break;711712case Bytecodes::_lstore_3:713case Bytecodes::_dstore_3:714store_two(3);715break;716717case Bytecodes::_istore:718case Bytecodes::_fstore:719case Bytecodes::_astore:720store_one(instruction->get_index());721break;722723case Bytecodes::_istore_0:724case Bytecodes::_fstore_0:725case Bytecodes::_astore_0:726store_one(0);727break;728729case Bytecodes::_istore_1:730case Bytecodes::_fstore_1:731case Bytecodes::_astore_1:732store_one(1);733break;734735case Bytecodes::_istore_2:736case Bytecodes::_fstore_2:737case Bytecodes::_astore_2:738store_one(2);739break;740741case Bytecodes::_istore_3:742case Bytecodes::_fstore_3:743case Bytecodes::_astore_3:744store_one(3);745break;746747case Bytecodes::_wide:748fatal("Iterator should skip this bytecode");749break;750751default:752tty->print("unexpected opcode: %d\n", instruction->cur_bc());753ShouldNotReachHere();754break;755}756}757758void MethodLiveness::BasicBlock::load_two(int local) {759load_one(local);760load_one(local+1);761}762763void MethodLiveness::BasicBlock::load_one(int local) {764if (!_kill.at(local)) {765_gen.at_put(local, true);766}767}768769void MethodLiveness::BasicBlock::store_two(int local) {770store_one(local);771store_one(local+1);772}773774void MethodLiveness::BasicBlock::store_one(int local) {775if (!_gen.at(local)) {776_kill.at_put(local, true);777}778}779780void MethodLiveness::BasicBlock::propagate(MethodLiveness *ml) {781// These set operations could be combined for efficiency if the782// performance of this analysis becomes an issue.783_entry.set_union(_normal_exit);784_entry.set_difference(_kill);785_entry.set_union(_gen);786787// Note that we merge information from our exceptional successors788// just once, rather than at individual bytecodes.789_entry.set_union(_exception_exit);790791if (TraceLivenessGen) {792tty->print_cr(" ** Visiting block at %d **", start_bci());793print_on(tty);794}795796int i;797for (i=_normal_predecessors->length()-1; i>=0; i--) {798BasicBlock *block = _normal_predecessors->at(i);799if (block->merge_normal(_entry)) {800ml->work_list_add(block);801}802}803for (i=_exception_predecessors->length()-1; i>=0; i--) {804BasicBlock *block = _exception_predecessors->at(i);805if (block->merge_exception(_entry)) {806ml->work_list_add(block);807}808}809}810811bool MethodLiveness::BasicBlock::merge_normal(const BitMap& other) {812return _normal_exit.set_union_with_result(other);813}814815bool MethodLiveness::BasicBlock::merge_exception(const BitMap& other) {816return _exception_exit.set_union_with_result(other);817}818819MethodLivenessResult MethodLiveness::BasicBlock::get_liveness_at(ciMethod* method, int bci) {820MethodLivenessResult answer(_analyzer->bit_map_size_bits());821answer.set_is_valid();822823#ifndef ASSERT824if (bci == start_bci()) {825answer.set_from(_entry);826return answer;827}828#endif829830#ifdef ASSERT831ResourceMark rm;832ResourceBitMap g(_gen.size(), false); g.set_from(_gen);833ResourceBitMap k(_kill.size(), false); k.set_from(_kill);834#endif835if (_last_bci != bci || trueInDebug) {836ciBytecodeStream bytes(method);837bytes.reset_to_bci(bci);838bytes.set_max_bci(limit_bci());839compute_gen_kill_range(&bytes);840assert(_last_bci != bci ||841(g.is_same(_gen) && k.is_same(_kill)), "cached computation is incorrect");842_last_bci = bci;843}844845answer.set_union(_normal_exit);846answer.set_difference(_kill);847answer.set_union(_gen);848answer.set_union(_exception_exit);849850#ifdef ASSERT851if (bci == start_bci()) {852assert(answer.is_same(_entry), "optimized answer must be accurate");853}854#endif855856return answer;857}858859#ifndef PRODUCT860861void MethodLiveness::BasicBlock::print_on(outputStream *os) const {862os->print_cr("===================================================================");863os->print_cr(" Block start: %4d, limit: %4d", _start_bci, _limit_bci);864os->print (" Normal predecessors (%2d) @", _normal_predecessors->length());865int i;866for (i=0; i < _normal_predecessors->length(); i++) {867os->print(" %4d", _normal_predecessors->at(i)->start_bci());868}869os->cr();870os->print (" Exceptional predecessors (%2d) @", _exception_predecessors->length());871for (i=0; i < _exception_predecessors->length(); i++) {872os->print(" %4d", _exception_predecessors->at(i)->start_bci());873}874os->cr();875os->print (" Normal Exit : ");876_normal_exit.print_on(os);877os->print (" Gen : ");878_gen.print_on(os);879os->print (" Kill : ");880_kill.print_on(os);881os->print (" Exception Exit: ");882_exception_exit.print_on(os);883os->print (" Entry : ");884_entry.print_on(os);885}886887#endif // PRODUCT888889890