0N/A/*
2027N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "ci/ciMethod.hpp"
1879N/A#include "ci/ciMethodBlocks.hpp"
1879N/A#include "ci/ciStreams.hpp"
1879N/A#include "compiler/methodLiveness.hpp"
1879N/A#include "interpreter/bytecode.hpp"
1879N/A#include "interpreter/bytecodes.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "utilities/bitMap.inline.hpp"
1879N/A
0N/A// The MethodLiveness class performs a simple liveness analysis on a method
0N/A// in order to decide which locals are live (that is, will be used again) at
0N/A// a particular bytecode index (bci).
0N/A//
0N/A// The algorithm goes:
0N/A//
0N/A// 1. Break the method into a set of basic blocks. For each basic block we
0N/A// also keep track of its set of predecessors through normal control flow
0N/A// and predecessors through exceptional control flow.
0N/A//
0N/A// 2. For each basic block, compute two sets, gen (the set of values used before
0N/A// they are defined) and kill (the set of values defined before they are used)
0N/A// in the basic block. A basic block "needs" the locals in its gen set to
0N/A// perform its computation. A basic block "provides" values for the locals in
0N/A// its kill set, allowing a need from a successor to be ignored.
0N/A//
0N/A// 3. Liveness information (the set of locals which are needed) is pushed backwards through
0N/A// the program, from blocks to their predecessors. We compute and store liveness
0N/A// information for the normal/exceptional exit paths for each basic block. When
0N/A// this process reaches a fixed point, we are done.
0N/A//
0N/A// 4. When we are asked about the liveness at a particular bci with a basic block, we
0N/A// compute gen/kill sets which represent execution from that bci to the exit of
0N/A// its blocks. We then compose this range gen/kill information with the normal
0N/A// and exceptional exit information for the block to produce liveness information
0N/A// at that bci.
0N/A//
0N/A// The algorithm is approximate in many respects. Notably:
0N/A//
0N/A// 1. We do not do the analysis necessary to match jsr's with the appropriate ret.
0N/A// Instead we make the conservative assumption that any ret can return to any
0N/A// jsr return site.
0N/A// 2. Instead of computing the effects of exceptions at every instruction, we
0N/A// summarize the effects of all exceptional continuations from the block as
0N/A// a single set (_exception_exit), losing some information but simplifying the
0N/A// analysis.
0N/A
0N/A
0N/A//--------------------------------------------------------------------------
0N/A// The BitCounter class is used for counting the number of bits set in
0N/A// some BitMap. It is only used when collecting liveness statistics.
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Aclass BitCounter: public BitMapClosure {
0N/A private:
0N/A int _count;
0N/A public:
0N/A BitCounter() : _count(0) {}
0N/A
0N/A // Callback when bit in map is set
342N/A virtual bool do_bit(size_t offset) {
0N/A _count++;
342N/A return true;
0N/A }
0N/A
0N/A int count() {
0N/A return _count;
0N/A }
0N/A};
0N/A
0N/A
0N/A//--------------------------------------------------------------------------
0N/A
0N/A
0N/A// Counts
0N/Along MethodLiveness::_total_bytes = 0;
0N/Aint MethodLiveness::_total_methods = 0;
0N/A
0N/Along MethodLiveness::_total_blocks = 0;
0N/Aint MethodLiveness::_max_method_blocks = 0;
0N/A
0N/Along MethodLiveness::_total_edges = 0;
0N/Aint MethodLiveness::_max_block_edges = 0;
0N/A
0N/Along MethodLiveness::_total_exc_edges = 0;
0N/Aint MethodLiveness::_max_block_exc_edges = 0;
0N/A
0N/Along MethodLiveness::_total_method_locals = 0;
0N/Aint MethodLiveness::_max_method_locals = 0;
0N/A
0N/Along MethodLiveness::_total_locals_queried = 0;
0N/Along MethodLiveness::_total_live_locals_queried = 0;
0N/A
0N/Along MethodLiveness::_total_visits = 0;
0N/A
0N/A#endif
0N/A
0N/A// Timers
0N/AelapsedTimer MethodLiveness::_time_build_graph;
0N/AelapsedTimer MethodLiveness::_time_gen_kill;
0N/AelapsedTimer MethodLiveness::_time_flow;
0N/AelapsedTimer MethodLiveness::_time_query;
0N/AelapsedTimer MethodLiveness::_time_total;
0N/A
0N/AMethodLiveness::MethodLiveness(Arena* arena, ciMethod* method)
0N/A#ifdef COMPILER1
0N/A : _bci_block_start((uintptr_t*)arena->Amalloc((method->code_size() >> LogBitsPerByte) + 1), method->code_size())
0N/A#endif
0N/A{
0N/A _arena = arena;
0N/A _method = method;
0N/A _bit_map_size_bits = method->max_locals();
0N/A _bit_map_size_words = (_bit_map_size_bits / sizeof(unsigned int)) + 1;
0N/A
0N/A#ifdef COMPILER1
0N/A _bci_block_start.clear();
0N/A#endif
0N/A}
0N/A
0N/Avoid MethodLiveness::compute_liveness() {
0N/A#ifndef PRODUCT
0N/A if (TraceLivenessGen) {
0N/A tty->print_cr("################################################################");
0N/A tty->print("# Computing liveness information for ");
0N/A method()->print_short_name();
0N/A }
0N/A
0N/A if (TimeLivenessAnalysis) _time_total.start();
0N/A#endif
0N/A
0N/A {
0N/A TraceTime buildGraph(NULL, &_time_build_graph, TimeLivenessAnalysis);
0N/A init_basic_blocks();
0N/A }
0N/A {
0N/A TraceTime genKill(NULL, &_time_gen_kill, TimeLivenessAnalysis);
0N/A init_gen_kill();
0N/A }
0N/A {
0N/A TraceTime flow(NULL, &_time_flow, TimeLivenessAnalysis);
0N/A propagate_liveness();
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TimeLivenessAnalysis) _time_total.stop();
0N/A
0N/A if (TimeLivenessAnalysis) {
0N/A // Collect statistics
0N/A _total_bytes += method()->code_size();
0N/A _total_methods++;
0N/A
0N/A int num_blocks = _block_count;
0N/A _total_blocks += num_blocks;
0N/A _max_method_blocks = MAX2(num_blocks,_max_method_blocks);
0N/A
0N/A for (int i=0; i<num_blocks; i++) {
0N/A BasicBlock *block = _block_list[i];
0N/A
0N/A int numEdges = block->_normal_predecessors->length();
0N/A int numExcEdges = block->_exception_predecessors->length();
0N/A
0N/A _total_edges += numEdges;
0N/A _total_exc_edges += numExcEdges;
0N/A _max_block_edges = MAX2(numEdges,_max_block_edges);
0N/A _max_block_exc_edges = MAX2(numExcEdges,_max_block_exc_edges);
0N/A }
0N/A
0N/A int numLocals = _bit_map_size_bits;
0N/A _total_method_locals += numLocals;
0N/A _max_method_locals = MAX2(numLocals,_max_method_locals);
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A
0N/Avoid MethodLiveness::init_basic_blocks() {
0N/A bool bailout = false;
0N/A
0N/A int method_len = method()->code_size();
0N/A ciMethodBlocks *mblocks = method()->get_method_blocks();
0N/A
0N/A // Create an array to store the bci->BasicBlock mapping.
0N/A _block_map = new (arena()) GrowableArray<BasicBlock*>(arena(), method_len, method_len, NULL);
0N/A
0N/A _block_count = mblocks->num_blocks();
0N/A _block_list = (BasicBlock **) arena()->Amalloc(sizeof(BasicBlock *) * _block_count);
0N/A
0N/A // Used for patching up jsr/ret control flow.
0N/A GrowableArray<BasicBlock*>* jsr_exit_list = new GrowableArray<BasicBlock*>(5);
0N/A GrowableArray<BasicBlock*>* ret_list = new GrowableArray<BasicBlock*>(5);
0N/A
0N/A // generate our block list from ciMethodBlocks
0N/A for (int blk = 0; blk < _block_count; blk++) {
0N/A ciBlock *cib = mblocks->block(blk);
0N/A int start_bci = cib->start_bci();
0N/A _block_list[blk] = new (arena()) BasicBlock(this, start_bci, cib->limit_bci());
0N/A _block_map->at_put(start_bci, _block_list[blk]);
0N/A#ifdef COMPILER1
0N/A // mark all bcis where a new basic block starts
0N/A _bci_block_start.set_bit(start_bci);
0N/A#endif // COMPILER1
0N/A }
0N/A // fill in the predecessors of blocks
0N/A ciBytecodeStream bytes(method());
0N/A
0N/A for (int blk = 0; blk < _block_count; blk++) {
0N/A BasicBlock *current_block = _block_list[blk];
0N/A int bci = mblocks->block(blk)->control_bci();
0N/A
0N/A if (bci == ciBlock::fall_through_bci) {
0N/A int limit = current_block->limit_bci();
0N/A if (limit < method_len) {
0N/A BasicBlock *next = _block_map->at(limit);
0N/A assert( next != NULL, "must be a block immediately following this one.");
0N/A next->add_normal_predecessor(current_block);
0N/A }
0N/A continue;
0N/A }
0N/A bytes.reset_to_bci(bci);
0N/A Bytecodes::Code code = bytes.next();
0N/A BasicBlock *dest;
0N/A
0N/A // Now we need to interpret the instruction's effect
0N/A // on control flow.
0N/A assert (current_block != NULL, "we must have a current block");
0N/A switch (code) {
0N/A case Bytecodes::_ifeq:
0N/A case Bytecodes::_ifne:
0N/A case Bytecodes::_iflt:
0N/A case Bytecodes::_ifge:
0N/A case Bytecodes::_ifgt:
0N/A case Bytecodes::_ifle:
0N/A case Bytecodes::_if_icmpeq:
0N/A case Bytecodes::_if_icmpne:
0N/A case Bytecodes::_if_icmplt:
0N/A case Bytecodes::_if_icmpge:
0N/A case Bytecodes::_if_icmpgt:
0N/A case Bytecodes::_if_icmple:
0N/A case Bytecodes::_if_acmpeq:
0N/A case Bytecodes::_if_acmpne:
0N/A case Bytecodes::_ifnull:
0N/A case Bytecodes::_ifnonnull:
0N/A // Two way branch. Set predecessors at each destination.
0N/A dest = _block_map->at(bytes.next_bci());
0N/A assert(dest != NULL, "must be a block immediately following this one.");
0N/A dest->add_normal_predecessor(current_block);
0N/A
0N/A dest = _block_map->at(bytes.get_dest());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A break;
0N/A case Bytecodes::_goto:
0N/A dest = _block_map->at(bytes.get_dest());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A break;
0N/A case Bytecodes::_goto_w:
0N/A dest = _block_map->at(bytes.get_far_dest());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A break;
0N/A case Bytecodes::_tableswitch:
0N/A {
2027N/A Bytecode_tableswitch tableswitch(&bytes);
0N/A
2027N/A int len = tableswitch.length();
0N/A
2027N/A dest = _block_map->at(bci + tableswitch.default_offset());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A while (--len >= 0) {
2027N/A dest = _block_map->at(bci + tableswitch.dest_offset_at(len));
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A }
0N/A break;
0N/A }
0N/A
0N/A case Bytecodes::_lookupswitch:
0N/A {
2027N/A Bytecode_lookupswitch lookupswitch(&bytes);
0N/A
2027N/A int npairs = lookupswitch.number_of_pairs();
0N/A
2027N/A dest = _block_map->at(bci + lookupswitch.default_offset());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A while(--npairs >= 0) {
2027N/A LookupswitchPair pair = lookupswitch.pair_at(npairs);
2027N/A dest = _block_map->at( bci + pair.offset());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A }
0N/A break;
0N/A }
0N/A
0N/A case Bytecodes::_jsr:
0N/A {
0N/A assert(bytes.is_wide()==false, "sanity check");
0N/A dest = _block_map->at(bytes.get_dest());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
0N/A assert(jsrExit != NULL, "jsr return bci must start a block.");
0N/A jsr_exit_list->append(jsrExit);
0N/A break;
0N/A }
0N/A case Bytecodes::_jsr_w:
0N/A {
0N/A dest = _block_map->at(bytes.get_far_dest());
0N/A assert(dest != NULL, "branch desination must start a block.");
0N/A dest->add_normal_predecessor(current_block);
0N/A BasicBlock *jsrExit = _block_map->at(current_block->limit_bci());
0N/A assert(jsrExit != NULL, "jsr return bci must start a block.");
0N/A jsr_exit_list->append(jsrExit);
0N/A break;
0N/A }
0N/A
0N/A case Bytecodes::_wide:
0N/A assert(false, "wide opcodes should not be seen here");
0N/A break;
0N/A case Bytecodes::_athrow:
0N/A case Bytecodes::_ireturn:
0N/A case Bytecodes::_lreturn:
0N/A case Bytecodes::_freturn:
0N/A case Bytecodes::_dreturn:
0N/A case Bytecodes::_areturn:
0N/A case Bytecodes::_return:
0N/A // These opcodes are not the normal predecessors of any other opcodes.
0N/A break;
0N/A case Bytecodes::_ret:
0N/A // We will patch up jsr/rets in a subsequent pass.
0N/A ret_list->append(current_block);
0N/A break;
0N/A case Bytecodes::_breakpoint:
0N/A // Bail out of there are breakpoints in here.
0N/A bailout = true;
0N/A break;
0N/A default:
0N/A // Do nothing.
0N/A break;
0N/A }
0N/A }
0N/A // Patch up the jsr/ret's. We conservatively assume that any ret
0N/A // can return to any jsr site.
0N/A int ret_list_len = ret_list->length();
0N/A int jsr_exit_list_len = jsr_exit_list->length();
0N/A if (ret_list_len > 0 && jsr_exit_list_len > 0) {
0N/A for (int i = jsr_exit_list_len - 1; i >= 0; i--) {
0N/A BasicBlock *jsrExit = jsr_exit_list->at(i);
0N/A for (int i = ret_list_len - 1; i >= 0; i--) {
0N/A jsrExit->add_normal_predecessor(ret_list->at(i));
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Compute exception edges.
0N/A for (int b=_block_count-1; b >= 0; b--) {
0N/A BasicBlock *block = _block_list[b];
0N/A int block_start = block->start_bci();
0N/A int block_limit = block->limit_bci();
0N/A ciExceptionHandlerStream handlers(method());
0N/A for (; !handlers.is_done(); handlers.next()) {
0N/A ciExceptionHandler* handler = handlers.handler();
0N/A int start = handler->start();
0N/A int limit = handler->limit();
0N/A int handler_bci = handler->handler_bci();
0N/A
0N/A int intersect_start = MAX2(block_start, start);
0N/A int intersect_limit = MIN2(block_limit, limit);
0N/A if (intersect_start < intersect_limit) {
0N/A // The catch range has a nonempty intersection with this
0N/A // basic block. That means this basic block can be an
0N/A // exceptional predecessor.
0N/A _block_map->at(handler_bci)->add_exception_predecessor(block);
0N/A
0N/A if (handler->is_catch_all()) {
0N/A // This is a catch-all block.
0N/A if (intersect_start == block_start && intersect_limit == block_limit) {
0N/A // The basic block is entirely contained in this catch-all block.
0N/A // Skip the rest of the exception handlers -- they can never be
0N/A // reached in execution.
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::init_gen_kill() {
0N/A for (int i=_block_count-1; i >= 0; i--) {
0N/A _block_list[i]->compute_gen_kill(method());
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::propagate_liveness() {
0N/A int num_blocks = _block_count;
0N/A BasicBlock *block;
0N/A
0N/A // We start our work list off with all blocks in it.
0N/A // Alternately, we could start off the work list with the list of all
0N/A // blocks which could exit the method directly, along with one block
0N/A // from any infinite loop. If this matters, it can be changed. It
0N/A // may not be clear from looking at the code, but the order of the
0N/A // workList will be the opposite of the creation order of the basic
0N/A // blocks, which should be decent for quick convergence (with the
0N/A // possible exception of exception handlers, which are all created
0N/A // early).
0N/A _work_list = NULL;
0N/A for (int i = 0; i < num_blocks; i++) {
0N/A block = _block_list[i];
0N/A block->set_next(_work_list);
0N/A block->set_on_work_list(true);
0N/A _work_list = block;
0N/A }
0N/A
0N/A
0N/A while ((block = work_list_get()) != NULL) {
0N/A block->propagate(this);
0N/A NOT_PRODUCT(_total_visits++;)
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::work_list_add(BasicBlock *block) {
0N/A if (!block->on_work_list()) {
0N/A block->set_next(_work_list);
0N/A block->set_on_work_list(true);
0N/A _work_list = block;
0N/A }
0N/A}
0N/A
0N/AMethodLiveness::BasicBlock *MethodLiveness::work_list_get() {
0N/A BasicBlock *block = _work_list;
0N/A if (block != NULL) {
0N/A block->set_on_work_list(false);
0N/A _work_list = block->next();
0N/A }
0N/A return block;
0N/A}
0N/A
0N/A
0N/AMethodLivenessResult MethodLiveness::get_liveness_at(int entry_bci) {
0N/A int bci = entry_bci;
0N/A bool is_entry = false;
0N/A if (entry_bci == InvocationEntryBci) {
0N/A is_entry = true;
0N/A bci = 0;
0N/A }
0N/A
342N/A MethodLivenessResult answer((uintptr_t*)NULL,0);
0N/A
0N/A if (_block_count > 0) {
0N/A if (TimeLivenessAnalysis) _time_total.start();
0N/A if (TimeLivenessAnalysis) _time_query.start();
0N/A
0N/A assert( 0 <= bci && bci < method()->code_size(), "bci out of range" );
0N/A BasicBlock *block = _block_map->at(bci);
0N/A // We may not be at the block start, so search backwards to find the block
0N/A // containing bci.
0N/A int t = bci;
0N/A while (block == NULL && t > 0) {
0N/A block = _block_map->at(--t);
0N/A }
0N/A assert( block != NULL, "invalid bytecode index; must be instruction index" );
0N/A assert(bci >= block->start_bci() && bci < block->limit_bci(), "block must contain bci.");
0N/A
0N/A answer = block->get_liveness_at(method(), bci);
0N/A
0N/A if (is_entry && method()->is_synchronized() && !method()->is_static()) {
0N/A // Synchronized methods use the receiver once on entry.
0N/A answer.at_put(0, true);
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TraceLivenessQuery) {
0N/A tty->print("Liveness query of ");
0N/A method()->print_short_name();
0N/A tty->print(" @ %d : result is ", bci);
0N/A answer.print_on(tty);
0N/A }
0N/A
0N/A if (TimeLivenessAnalysis) _time_query.stop();
0N/A if (TimeLivenessAnalysis) _time_total.stop();
0N/A#endif
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A if (TimeLivenessAnalysis) {
0N/A // Collect statistics.
0N/A _total_locals_queried += _bit_map_size_bits;
0N/A BitCounter counter;
0N/A answer.iterate(&counter);
0N/A _total_live_locals_queried += counter.count();
0N/A }
0N/A#endif
0N/A
0N/A return answer;
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid MethodLiveness::print_times() {
0N/A tty->print_cr ("Accumulated liveness analysis times/statistics:");
0N/A tty->print_cr ("-----------------------------------------------");
0N/A tty->print_cr (" Total : %3.3f sec.", _time_total.seconds());
0N/A tty->print_cr (" Build graph : %3.3f sec. (%2.2f%%)", _time_build_graph.seconds(),
0N/A _time_build_graph.seconds() * 100 / _time_total.seconds());
0N/A tty->print_cr (" Gen / Kill : %3.3f sec. (%2.2f%%)", _time_gen_kill.seconds(),
0N/A _time_gen_kill.seconds() * 100 / _time_total.seconds());
0N/A tty->print_cr (" Dataflow : %3.3f sec. (%2.2f%%)", _time_flow.seconds(),
0N/A _time_flow.seconds() * 100 / _time_total.seconds());
0N/A tty->print_cr (" Query : %3.3f sec. (%2.2f%%)", _time_query.seconds(),
0N/A _time_query.seconds() * 100 / _time_total.seconds());
0N/A tty->print_cr (" #bytes : %8d (%3.0f bytes per sec)",
0N/A _total_bytes,
0N/A _total_bytes / _time_total.seconds());
0N/A tty->print_cr (" #methods : %8d (%3.0f methods per sec)",
0N/A _total_methods,
0N/A _total_methods / _time_total.seconds());
0N/A tty->print_cr (" avg locals : %3.3f max locals : %3d",
0N/A (float)_total_method_locals / _total_methods,
0N/A _max_method_locals);
0N/A tty->print_cr (" avg blocks : %3.3f max blocks : %3d",
0N/A (float)_total_blocks / _total_methods,
0N/A _max_method_blocks);
0N/A tty->print_cr (" avg bytes : %3.3f",
0N/A (float)_total_bytes / _total_methods);
0N/A tty->print_cr (" #blocks : %8d",
0N/A _total_blocks);
0N/A tty->print_cr (" avg normal predecessors : %3.3f max normal predecessors : %3d",
0N/A (float)_total_edges / _total_blocks,
0N/A _max_block_edges);
0N/A tty->print_cr (" avg exception predecessors : %3.3f max exception predecessors : %3d",
0N/A (float)_total_exc_edges / _total_blocks,
0N/A _max_block_exc_edges);
0N/A tty->print_cr (" avg visits : %3.3f",
0N/A (float)_total_visits / _total_blocks);
0N/A tty->print_cr (" #locals queried : %8d #live : %8d %%live : %2.2f%%",
0N/A _total_locals_queried,
0N/A _total_live_locals_queried,
0N/A 100.0 * _total_live_locals_queried / _total_locals_queried);
0N/A}
0N/A
0N/A#endif
0N/A
0N/A
0N/AMethodLiveness::BasicBlock::BasicBlock(MethodLiveness *analyzer, int start, int limit) :
0N/A _gen((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
0N/A analyzer->bit_map_size_bits()),
0N/A _kill((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
0N/A analyzer->bit_map_size_bits()),
0N/A _entry((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
0N/A analyzer->bit_map_size_bits()),
0N/A _normal_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
0N/A analyzer->bit_map_size_bits()),
0N/A _exception_exit((uintptr_t*)analyzer->arena()->Amalloc(BytesPerWord * analyzer->bit_map_size_words()),
0N/A analyzer->bit_map_size_bits()),
0N/A _last_bci(-1) {
0N/A _analyzer = analyzer;
0N/A _start_bci = start;
0N/A _limit_bci = limit;
0N/A _normal_predecessors =
0N/A new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
0N/A _exception_predecessors =
0N/A new (analyzer->arena()) GrowableArray<MethodLiveness::BasicBlock*>(analyzer->arena(), 5, 0, NULL);
0N/A _normal_exit.clear();
0N/A _exception_exit.clear();
0N/A _entry.clear();
0N/A
0N/A // this initialization is not strictly necessary.
0N/A // _gen and _kill are cleared at the beginning of compute_gen_kill_range()
0N/A _gen.clear();
0N/A _kill.clear();
0N/A}
0N/A
0N/A
0N/A
0N/AMethodLiveness::BasicBlock *MethodLiveness::BasicBlock::split(int split_bci) {
0N/A int start = _start_bci;
0N/A int limit = _limit_bci;
0N/A
0N/A if (TraceLivenessGen) {
0N/A tty->print_cr(" ** Splitting block (%d,%d) at %d", start, limit, split_bci);
0N/A }
0N/A
0N/A GrowableArray<BasicBlock*>* save_predecessors = _normal_predecessors;
0N/A
0N/A assert (start < split_bci && split_bci < limit, "improper split");
0N/A
0N/A // Make a new block to cover the first half of the range.
0N/A BasicBlock *first_half = new (_analyzer->arena()) BasicBlock(_analyzer, start, split_bci);
0N/A
0N/A // Assign correct values to the second half (this)
0N/A _normal_predecessors = first_half->_normal_predecessors;
0N/A _start_bci = split_bci;
0N/A add_normal_predecessor(first_half);
0N/A
0N/A // Assign correct predecessors to the new first half
0N/A first_half->_normal_predecessors = save_predecessors;
0N/A
0N/A return first_half;
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::compute_gen_kill(ciMethod* method) {
0N/A ciBytecodeStream bytes(method);
0N/A bytes.reset_to_bci(start_bci());
0N/A bytes.set_max_bci(limit_bci());
0N/A compute_gen_kill_range(&bytes);
0N/A
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::compute_gen_kill_range(ciBytecodeStream *bytes) {
0N/A _gen.clear();
0N/A _kill.clear();
0N/A
0N/A while (bytes->next() != ciBytecodeStream::EOBC()) {
0N/A compute_gen_kill_single(bytes);
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::compute_gen_kill_single(ciBytecodeStream *instruction) {
0N/A int localNum;
0N/A
0N/A // We prohibit _gen and _kill from having locals in common. If we
0N/A // know that one is definitely going to be applied before the other,
0N/A // we could save some computation time by relaxing this prohibition.
0N/A
0N/A switch (instruction->cur_bc()) {
0N/A case Bytecodes::_nop:
0N/A case Bytecodes::_goto:
0N/A case Bytecodes::_goto_w:
0N/A case Bytecodes::_aconst_null:
0N/A case Bytecodes::_new:
0N/A case Bytecodes::_iconst_m1:
0N/A case Bytecodes::_iconst_0:
0N/A case Bytecodes::_iconst_1:
0N/A case Bytecodes::_iconst_2:
0N/A case Bytecodes::_iconst_3:
0N/A case Bytecodes::_iconst_4:
0N/A case Bytecodes::_iconst_5:
0N/A case Bytecodes::_fconst_0:
0N/A case Bytecodes::_fconst_1:
0N/A case Bytecodes::_fconst_2:
0N/A case Bytecodes::_bipush:
0N/A case Bytecodes::_sipush:
0N/A case Bytecodes::_lconst_0:
0N/A case Bytecodes::_lconst_1:
0N/A case Bytecodes::_dconst_0:
0N/A case Bytecodes::_dconst_1:
0N/A case Bytecodes::_ldc2_w:
0N/A case Bytecodes::_ldc:
0N/A case Bytecodes::_ldc_w:
0N/A case Bytecodes::_iaload:
0N/A case Bytecodes::_faload:
0N/A case Bytecodes::_baload:
0N/A case Bytecodes::_caload:
0N/A case Bytecodes::_saload:
0N/A case Bytecodes::_laload:
0N/A case Bytecodes::_daload:
0N/A case Bytecodes::_aaload:
0N/A case Bytecodes::_iastore:
0N/A case Bytecodes::_fastore:
0N/A case Bytecodes::_bastore:
0N/A case Bytecodes::_castore:
0N/A case Bytecodes::_sastore:
0N/A case Bytecodes::_lastore:
0N/A case Bytecodes::_dastore:
0N/A case Bytecodes::_aastore:
0N/A case Bytecodes::_pop:
0N/A case Bytecodes::_pop2:
0N/A case Bytecodes::_dup:
0N/A case Bytecodes::_dup_x1:
0N/A case Bytecodes::_dup_x2:
0N/A case Bytecodes::_dup2:
0N/A case Bytecodes::_dup2_x1:
0N/A case Bytecodes::_dup2_x2:
0N/A case Bytecodes::_swap:
0N/A case Bytecodes::_iadd:
0N/A case Bytecodes::_fadd:
0N/A case Bytecodes::_isub:
0N/A case Bytecodes::_fsub:
0N/A case Bytecodes::_imul:
0N/A case Bytecodes::_fmul:
0N/A case Bytecodes::_idiv:
0N/A case Bytecodes::_fdiv:
0N/A case Bytecodes::_irem:
0N/A case Bytecodes::_frem:
0N/A case Bytecodes::_ishl:
0N/A case Bytecodes::_ishr:
0N/A case Bytecodes::_iushr:
0N/A case Bytecodes::_iand:
0N/A case Bytecodes::_ior:
0N/A case Bytecodes::_ixor:
0N/A case Bytecodes::_l2f:
0N/A case Bytecodes::_l2i:
0N/A case Bytecodes::_d2f:
0N/A case Bytecodes::_d2i:
0N/A case Bytecodes::_fcmpl:
0N/A case Bytecodes::_fcmpg:
0N/A case Bytecodes::_ladd:
0N/A case Bytecodes::_dadd:
0N/A case Bytecodes::_lsub:
0N/A case Bytecodes::_dsub:
0N/A case Bytecodes::_lmul:
0N/A case Bytecodes::_dmul:
0N/A case Bytecodes::_ldiv:
0N/A case Bytecodes::_ddiv:
0N/A case Bytecodes::_lrem:
0N/A case Bytecodes::_drem:
0N/A case Bytecodes::_land:
0N/A case Bytecodes::_lor:
0N/A case Bytecodes::_lxor:
0N/A case Bytecodes::_ineg:
0N/A case Bytecodes::_fneg:
0N/A case Bytecodes::_i2f:
0N/A case Bytecodes::_f2i:
0N/A case Bytecodes::_i2c:
0N/A case Bytecodes::_i2s:
0N/A case Bytecodes::_i2b:
0N/A case Bytecodes::_lneg:
0N/A case Bytecodes::_dneg:
0N/A case Bytecodes::_l2d:
0N/A case Bytecodes::_d2l:
0N/A case Bytecodes::_lshl:
0N/A case Bytecodes::_lshr:
0N/A case Bytecodes::_lushr:
0N/A case Bytecodes::_i2l:
0N/A case Bytecodes::_i2d:
0N/A case Bytecodes::_f2l:
0N/A case Bytecodes::_f2d:
0N/A case Bytecodes::_lcmp:
0N/A case Bytecodes::_dcmpl:
0N/A case Bytecodes::_dcmpg:
0N/A case Bytecodes::_ifeq:
0N/A case Bytecodes::_ifne:
0N/A case Bytecodes::_iflt:
0N/A case Bytecodes::_ifge:
0N/A case Bytecodes::_ifgt:
0N/A case Bytecodes::_ifle:
0N/A case Bytecodes::_tableswitch:
0N/A case Bytecodes::_ireturn:
0N/A case Bytecodes::_freturn:
0N/A case Bytecodes::_if_icmpeq:
0N/A case Bytecodes::_if_icmpne:
0N/A case Bytecodes::_if_icmplt:
0N/A case Bytecodes::_if_icmpge:
0N/A case Bytecodes::_if_icmpgt:
0N/A case Bytecodes::_if_icmple:
0N/A case Bytecodes::_lreturn:
0N/A case Bytecodes::_dreturn:
0N/A case Bytecodes::_if_acmpeq:
0N/A case Bytecodes::_if_acmpne:
0N/A case Bytecodes::_jsr:
0N/A case Bytecodes::_jsr_w:
0N/A case Bytecodes::_getstatic:
0N/A case Bytecodes::_putstatic:
0N/A case Bytecodes::_getfield:
0N/A case Bytecodes::_putfield:
0N/A case Bytecodes::_invokevirtual:
0N/A case Bytecodes::_invokespecial:
0N/A case Bytecodes::_invokestatic:
0N/A case Bytecodes::_invokeinterface:
1135N/A case Bytecodes::_invokedynamic:
0N/A case Bytecodes::_newarray:
0N/A case Bytecodes::_anewarray:
0N/A case Bytecodes::_checkcast:
0N/A case Bytecodes::_arraylength:
0N/A case Bytecodes::_instanceof:
0N/A case Bytecodes::_athrow:
0N/A case Bytecodes::_areturn:
0N/A case Bytecodes::_monitorenter:
0N/A case Bytecodes::_monitorexit:
0N/A case Bytecodes::_ifnull:
0N/A case Bytecodes::_ifnonnull:
0N/A case Bytecodes::_multianewarray:
0N/A case Bytecodes::_lookupswitch:
0N/A // These bytecodes have no effect on the method's locals.
0N/A break;
0N/A
0N/A case Bytecodes::_return:
0N/A if (instruction->method()->intrinsic_id() == vmIntrinsics::_Object_init) {
0N/A // return from Object.init implicitly registers a finalizer
0N/A // for the receiver if needed, so keep it alive.
0N/A load_one(0);
0N/A }
0N/A break;
0N/A
0N/A
0N/A case Bytecodes::_lload:
0N/A case Bytecodes::_dload:
0N/A load_two(instruction->get_index());
0N/A break;
0N/A
0N/A case Bytecodes::_lload_0:
0N/A case Bytecodes::_dload_0:
0N/A load_two(0);
0N/A break;
0N/A
0N/A case Bytecodes::_lload_1:
0N/A case Bytecodes::_dload_1:
0N/A load_two(1);
0N/A break;
0N/A
0N/A case Bytecodes::_lload_2:
0N/A case Bytecodes::_dload_2:
0N/A load_two(2);
0N/A break;
0N/A
0N/A case Bytecodes::_lload_3:
0N/A case Bytecodes::_dload_3:
0N/A load_two(3);
0N/A break;
0N/A
0N/A case Bytecodes::_iload:
0N/A case Bytecodes::_iinc:
0N/A case Bytecodes::_fload:
0N/A case Bytecodes::_aload:
0N/A case Bytecodes::_ret:
0N/A load_one(instruction->get_index());
0N/A break;
0N/A
0N/A case Bytecodes::_iload_0:
0N/A case Bytecodes::_fload_0:
0N/A case Bytecodes::_aload_0:
0N/A load_one(0);
0N/A break;
0N/A
0N/A case Bytecodes::_iload_1:
0N/A case Bytecodes::_fload_1:
0N/A case Bytecodes::_aload_1:
0N/A load_one(1);
0N/A break;
0N/A
0N/A case Bytecodes::_iload_2:
0N/A case Bytecodes::_fload_2:
0N/A case Bytecodes::_aload_2:
0N/A load_one(2);
0N/A break;
0N/A
0N/A case Bytecodes::_iload_3:
0N/A case Bytecodes::_fload_3:
0N/A case Bytecodes::_aload_3:
0N/A load_one(3);
0N/A break;
0N/A
0N/A case Bytecodes::_lstore:
0N/A case Bytecodes::_dstore:
0N/A store_two(localNum = instruction->get_index());
0N/A break;
0N/A
0N/A case Bytecodes::_lstore_0:
0N/A case Bytecodes::_dstore_0:
0N/A store_two(0);
0N/A break;
0N/A
0N/A case Bytecodes::_lstore_1:
0N/A case Bytecodes::_dstore_1:
0N/A store_two(1);
0N/A break;
0N/A
0N/A case Bytecodes::_lstore_2:
0N/A case Bytecodes::_dstore_2:
0N/A store_two(2);
0N/A break;
0N/A
0N/A case Bytecodes::_lstore_3:
0N/A case Bytecodes::_dstore_3:
0N/A store_two(3);
0N/A break;
0N/A
0N/A case Bytecodes::_istore:
0N/A case Bytecodes::_fstore:
0N/A case Bytecodes::_astore:
0N/A store_one(instruction->get_index());
0N/A break;
0N/A
0N/A case Bytecodes::_istore_0:
0N/A case Bytecodes::_fstore_0:
0N/A case Bytecodes::_astore_0:
0N/A store_one(0);
0N/A break;
0N/A
0N/A case Bytecodes::_istore_1:
0N/A case Bytecodes::_fstore_1:
0N/A case Bytecodes::_astore_1:
0N/A store_one(1);
0N/A break;
0N/A
0N/A case Bytecodes::_istore_2:
0N/A case Bytecodes::_fstore_2:
0N/A case Bytecodes::_astore_2:
0N/A store_one(2);
0N/A break;
0N/A
0N/A case Bytecodes::_istore_3:
0N/A case Bytecodes::_fstore_3:
0N/A case Bytecodes::_astore_3:
0N/A store_one(3);
0N/A break;
0N/A
0N/A case Bytecodes::_wide:
0N/A fatal("Iterator should skip this bytecode");
0N/A break;
0N/A
0N/A default:
0N/A tty->print("unexpected opcode: %d\n", instruction->cur_bc());
0N/A ShouldNotReachHere();
0N/A break;
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::load_two(int local) {
0N/A load_one(local);
0N/A load_one(local+1);
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::load_one(int local) {
0N/A if (!_kill.at(local)) {
0N/A _gen.at_put(local, true);
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::store_two(int local) {
0N/A store_one(local);
0N/A store_one(local+1);
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::store_one(int local) {
0N/A if (!_gen.at(local)) {
0N/A _kill.at_put(local, true);
0N/A }
0N/A}
0N/A
0N/Avoid MethodLiveness::BasicBlock::propagate(MethodLiveness *ml) {
0N/A // These set operations could be combined for efficiency if the
0N/A // performance of this analysis becomes an issue.
0N/A _entry.set_union(_normal_exit);
0N/A _entry.set_difference(_kill);
0N/A _entry.set_union(_gen);
0N/A
0N/A // Note that we merge information from our exceptional successors
0N/A // just once, rather than at individual bytecodes.
0N/A _entry.set_union(_exception_exit);
0N/A
0N/A if (TraceLivenessGen) {
0N/A tty->print_cr(" ** Visiting block at %d **", start_bci());
0N/A print_on(tty);
0N/A }
0N/A
0N/A int i;
0N/A for (i=_normal_predecessors->length()-1; i>=0; i--) {
0N/A BasicBlock *block = _normal_predecessors->at(i);
0N/A if (block->merge_normal(_entry)) {
0N/A ml->work_list_add(block);
0N/A }
0N/A }
0N/A for (i=_exception_predecessors->length()-1; i>=0; i--) {
0N/A BasicBlock *block = _exception_predecessors->at(i);
0N/A if (block->merge_exception(_entry)) {
0N/A ml->work_list_add(block);
0N/A }
0N/A }
0N/A}
0N/A
0N/Abool MethodLiveness::BasicBlock::merge_normal(BitMap other) {
0N/A return _normal_exit.set_union_with_result(other);
0N/A}
0N/A
0N/Abool MethodLiveness::BasicBlock::merge_exception(BitMap other) {
0N/A return _exception_exit.set_union_with_result(other);
0N/A}
0N/A
0N/AMethodLivenessResult MethodLiveness::BasicBlock::get_liveness_at(ciMethod* method, int bci) {
0N/A MethodLivenessResult answer(NEW_RESOURCE_ARRAY(uintptr_t, _analyzer->bit_map_size_words()),
0N/A _analyzer->bit_map_size_bits());
0N/A answer.set_is_valid();
0N/A
0N/A#ifndef ASSERT
0N/A if (bci == start_bci()) {
0N/A answer.set_from(_entry);
0N/A return answer;
0N/A }
0N/A#endif
0N/A
0N/A#ifdef ASSERT
0N/A ResourceMark rm;
0N/A BitMap g(_gen.size()); g.set_from(_gen);
0N/A BitMap k(_kill.size()); k.set_from(_kill);
0N/A#endif
0N/A if (_last_bci != bci || trueInDebug) {
0N/A ciBytecodeStream bytes(method);
0N/A bytes.reset_to_bci(bci);
0N/A bytes.set_max_bci(limit_bci());
0N/A compute_gen_kill_range(&bytes);
0N/A assert(_last_bci != bci ||
0N/A (g.is_same(_gen) && k.is_same(_kill)), "cached computation is incorrect");
0N/A _last_bci = bci;
0N/A }
0N/A
0N/A answer.clear();
0N/A answer.set_union(_normal_exit);
0N/A answer.set_difference(_kill);
0N/A answer.set_union(_gen);
0N/A answer.set_union(_exception_exit);
0N/A
0N/A#ifdef ASSERT
0N/A if (bci == start_bci()) {
0N/A assert(answer.is_same(_entry), "optimized answer must be accurate");
0N/A }
0N/A#endif
0N/A
0N/A return answer;
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid MethodLiveness::BasicBlock::print_on(outputStream *os) const {
0N/A os->print_cr("===================================================================");
0N/A os->print_cr(" Block start: %4d, limit: %4d", _start_bci, _limit_bci);
0N/A os->print (" Normal predecessors (%2d) @", _normal_predecessors->length());
0N/A int i;
0N/A for (i=0; i < _normal_predecessors->length(); i++) {
0N/A os->print(" %4d", _normal_predecessors->at(i)->start_bci());
0N/A }
0N/A os->cr();
0N/A os->print (" Exceptional predecessors (%2d) @", _exception_predecessors->length());
0N/A for (i=0; i < _exception_predecessors->length(); i++) {
0N/A os->print(" %4d", _exception_predecessors->at(i)->start_bci());
0N/A }
0N/A os->cr();
0N/A os->print (" Normal Exit : ");
0N/A _normal_exit.print_on(os);
0N/A os->print (" Gen : ");
0N/A _gen.print_on(os);
0N/A os->print (" Kill : ");
0N/A _kill.print_on(os);
0N/A os->print (" Exception Exit: ");
0N/A _exception_exit.print_on(os);
0N/A os->print (" Entry : ");
0N/A _entry.print_on(os);
0N/A}
0N/A
0N/A#endif // PRODUCT