aoqi@0: /* aoqi@0: * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "ci/ciMethodBlocks.hpp" aoqi@0: #include "ci/ciStreams.hpp" aoqi@0: #include "interpreter/bytecode.hpp" aoqi@0: #include "utilities/copy.hpp" aoqi@0: aoqi@0: // ciMethodBlocks aoqi@0: aoqi@0: aoqi@0: aoqi@0: ciBlock *ciMethodBlocks::block_containing(int bci) { aoqi@0: ciBlock *blk = _bci_to_block[bci]; aoqi@0: return blk; aoqi@0: } aoqi@0: aoqi@0: bool ciMethodBlocks::is_block_start(int bci) { aoqi@0: assert(bci >=0 && bci < _code_size, "valid bytecode range"); aoqi@0: ciBlock *b = _bci_to_block[bci]; aoqi@0: assert(b != NULL, "must have block for bytecode"); aoqi@0: return b->start_bci() == bci; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // ciMethodBlocks::split_block_at aoqi@0: // aoqi@0: // Split the block spanning bci into two separate ranges. The former aoqi@0: // block becomes the second half and a new range is created for the aoqi@0: // first half. Returns the range beginning at bci. aoqi@0: ciBlock *ciMethodBlocks::split_block_at(int bci) { aoqi@0: ciBlock *former_block = block_containing(bci); aoqi@0: ciBlock *new_block = new(_arena) ciBlock(_method, _num_blocks++, former_block->start_bci()); aoqi@0: _blocks->append(new_block); aoqi@0: assert(former_block != NULL, "must not be NULL"); aoqi@0: new_block->set_limit_bci(bci); aoqi@0: former_block->set_start_bci(bci); aoqi@0: for (int pos=bci-1; pos >= 0; pos--) { aoqi@0: ciBlock *current_block = block_containing(pos); aoqi@0: if (current_block == former_block) { aoqi@0: // Replace it. aoqi@0: _bci_to_block[pos] = new_block; aoqi@0: } else if (current_block == NULL) { aoqi@0: // Non-bytecode start. Skip. aoqi@0: continue; aoqi@0: } else { aoqi@0: // We are done with our backwards walk aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: // Move an exception handler information if needed. aoqi@0: if (former_block->is_handler()) { aoqi@0: int ex_start = former_block->ex_start_bci(); aoqi@0: int ex_end = former_block->ex_limit_bci(); aoqi@0: new_block->set_exception_range(ex_start, ex_end); aoqi@0: // Clear information in former_block. aoqi@0: former_block->clear_exception_handler(); aoqi@0: } aoqi@0: return former_block; aoqi@0: } aoqi@0: aoqi@0: ciBlock *ciMethodBlocks::make_block_at(int bci) { aoqi@0: ciBlock *cb = block_containing(bci); aoqi@0: if (cb == NULL ) { aoqi@0: // This is our first time visiting this bytecode. Create aoqi@0: // a fresh block and assign it this starting point. aoqi@0: ciBlock *nb = new(_arena) ciBlock(_method, _num_blocks++, bci); aoqi@0: _blocks->append(nb); aoqi@0: _bci_to_block[bci] = nb; aoqi@0: return nb; aoqi@0: } else if (cb->start_bci() == bci) { aoqi@0: // The block begins at bci. Simply return it. aoqi@0: return cb; aoqi@0: } else { aoqi@0: // We have already created a block containing bci but aoqi@0: // not starting at bci. This existing block needs to aoqi@0: // be split into two. aoqi@0: return split_block_at(bci); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ciBlock *ciMethodBlocks::make_dummy_block() { aoqi@0: ciBlock *dum = new(_arena) ciBlock(_method, -1, 0); aoqi@0: return dum; aoqi@0: } aoqi@0: aoqi@0: void ciMethodBlocks::do_analysis() { aoqi@0: ciBytecodeStream s(_method); aoqi@0: ciBlock *cur_block = block_containing(0); aoqi@0: int limit_bci = _method->code_size(); aoqi@0: aoqi@0: while (s.next() != ciBytecodeStream::EOBC()) { aoqi@0: int bci = s.cur_bci(); aoqi@0: // Determine if a new block has been made at the current bci. If aoqi@0: // this block differs from our current range, switch to the new aoqi@0: // one and end the old one. aoqi@0: assert(cur_block != NULL, "must always have a current block"); aoqi@0: ciBlock *new_block = block_containing(bci); aoqi@0: if (new_block == NULL || new_block == cur_block) { aoqi@0: // We have not marked this bci as the start of a new block. aoqi@0: // Keep interpreting the current_range. aoqi@0: _bci_to_block[bci] = cur_block; aoqi@0: } else { aoqi@0: cur_block->set_limit_bci(bci); aoqi@0: cur_block = new_block; aoqi@0: } aoqi@0: aoqi@0: switch (s.cur_bc()) { aoqi@0: case Bytecodes::_ifeq : aoqi@0: case Bytecodes::_ifne : aoqi@0: case Bytecodes::_iflt : aoqi@0: case Bytecodes::_ifge : aoqi@0: case Bytecodes::_ifgt : aoqi@0: case Bytecodes::_ifle : aoqi@0: case Bytecodes::_if_icmpeq : aoqi@0: case Bytecodes::_if_icmpne : aoqi@0: case Bytecodes::_if_icmplt : aoqi@0: case Bytecodes::_if_icmpge : aoqi@0: case Bytecodes::_if_icmpgt : aoqi@0: case Bytecodes::_if_icmple : aoqi@0: case Bytecodes::_if_acmpeq : aoqi@0: case Bytecodes::_if_acmpne : aoqi@0: case Bytecodes::_ifnull : aoqi@0: case Bytecodes::_ifnonnull : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: ciBlock *fall_through = make_block_at(s.next_bci()); aoqi@0: int dest_bci = s.get_dest(); aoqi@0: ciBlock *dest = make_block_at(dest_bci); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_goto : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: if (s.next_bci() < limit_bci) { aoqi@0: (void) make_block_at(s.next_bci()); aoqi@0: } aoqi@0: int dest_bci = s.get_dest(); aoqi@0: ciBlock *dest = make_block_at(dest_bci); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_jsr : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: ciBlock *ret = make_block_at(s.next_bci()); aoqi@0: int dest_bci = s.get_dest(); aoqi@0: ciBlock *dest = make_block_at(dest_bci); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_tableswitch : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: Bytecode_tableswitch sw(&s); aoqi@0: int len = sw.length(); aoqi@0: ciBlock *dest; aoqi@0: int dest_bci; aoqi@0: for (int i = 0; i < len; i++) { aoqi@0: dest_bci = s.cur_bci() + sw.dest_offset_at(i); aoqi@0: dest = make_block_at(dest_bci); aoqi@0: } aoqi@0: dest_bci = s.cur_bci() + sw.default_offset(); aoqi@0: make_block_at(dest_bci); aoqi@0: if (s.next_bci() < limit_bci) { aoqi@0: dest = make_block_at(s.next_bci()); aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: Bytecode_lookupswitch sw(&s); aoqi@0: int len = sw.number_of_pairs(); aoqi@0: ciBlock *dest; aoqi@0: int dest_bci; aoqi@0: for (int i = 0; i < len; i++) { aoqi@0: dest_bci = s.cur_bci() + sw.pair_at(i).offset(); aoqi@0: dest = make_block_at(dest_bci); aoqi@0: } aoqi@0: dest_bci = s.cur_bci() + sw.default_offset(); aoqi@0: dest = make_block_at(dest_bci); aoqi@0: if (s.next_bci() < limit_bci) { aoqi@0: dest = make_block_at(s.next_bci()); aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_goto_w : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: if (s.next_bci() < limit_bci) { aoqi@0: (void) make_block_at(s.next_bci()); aoqi@0: } aoqi@0: int dest_bci = s.get_far_dest(); aoqi@0: ciBlock *dest = make_block_at(dest_bci); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_jsr_w : aoqi@0: { aoqi@0: cur_block->set_control_bci(bci); aoqi@0: ciBlock *ret = make_block_at(s.next_bci()); aoqi@0: int dest_bci = s.get_far_dest(); aoqi@0: ciBlock *dest = make_block_at(dest_bci); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_athrow : aoqi@0: cur_block->set_may_throw(); aoqi@0: // fall-through aoqi@0: case Bytecodes::_ret : aoqi@0: case Bytecodes::_ireturn : aoqi@0: case Bytecodes::_lreturn : aoqi@0: case Bytecodes::_freturn : aoqi@0: case Bytecodes::_dreturn : aoqi@0: case Bytecodes::_areturn : aoqi@0: case Bytecodes::_return : aoqi@0: cur_block->set_control_bci(bci); aoqi@0: if (s.next_bci() < limit_bci) { aoqi@0: (void) make_block_at(s.next_bci()); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: // End the last block aoqi@0: cur_block->set_limit_bci(limit_bci); aoqi@0: } aoqi@0: aoqi@0: ciMethodBlocks::ciMethodBlocks(Arena *arena, ciMethod *meth): _method(meth), aoqi@0: _arena(arena), _num_blocks(0), _code_size(meth->code_size()) { aoqi@0: int block_estimate = _code_size / 8; aoqi@0: aoqi@0: _blocks = new(_arena) GrowableArray(_arena, block_estimate, 0, NULL); aoqi@0: int b2bsize = _code_size * sizeof(ciBlock **); aoqi@0: _bci_to_block = (ciBlock **) arena->Amalloc(b2bsize); aoqi@0: Copy::zero_to_words((HeapWord*) _bci_to_block, b2bsize / sizeof(HeapWord)); aoqi@0: aoqi@0: // create initial block covering the entire method aoqi@0: ciBlock *b = new(arena) ciBlock(_method, _num_blocks++, 0); aoqi@0: _blocks->append(b); aoqi@0: _bci_to_block[0] = b; aoqi@0: aoqi@0: // create blocks for exception handlers aoqi@0: if (meth->has_exception_handlers()) { aoqi@0: for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) { aoqi@0: ciExceptionHandler* handler = str.handler(); aoqi@0: ciBlock *eb = make_block_at(handler->handler_bci()); aoqi@0: // aoqi@0: // Several exception handlers can have the same handler_bci: aoqi@0: // aoqi@0: // try { aoqi@0: // if (a.foo(b) < 0) { aoqi@0: // return a.error(); aoqi@0: // } aoqi@0: // return CoderResult.UNDERFLOW; aoqi@0: // } finally { aoqi@0: // a.position(b); aoqi@0: // } aoqi@0: // aoqi@0: // The try block above is divided into 2 exception blocks aoqi@0: // separated by 'areturn' bci. aoqi@0: // aoqi@0: int ex_start = handler->start(); aoqi@0: int ex_end = handler->limit(); aoqi@0: // ensure a block at the start of exception range and start of following code aoqi@0: (void) make_block_at(ex_start); aoqi@0: if (ex_end < _code_size) aoqi@0: (void) make_block_at(ex_end); aoqi@0: aoqi@0: if (eb->is_handler()) { aoqi@0: // Extend old handler exception range to cover additional range. aoqi@0: int old_ex_start = eb->ex_start_bci(); aoqi@0: int old_ex_end = eb->ex_limit_bci(); aoqi@0: if (ex_start > old_ex_start) aoqi@0: ex_start = old_ex_start; aoqi@0: if (ex_end < old_ex_end) aoqi@0: ex_end = old_ex_end; aoqi@0: eb->clear_exception_handler(); // Reset exception information aoqi@0: } aoqi@0: eb->set_exception_range(ex_start, ex_end); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // scan the bytecodes and identify blocks aoqi@0: do_analysis(); aoqi@0: aoqi@0: // mark blocks that have exception handlers aoqi@0: if (meth->has_exception_handlers()) { aoqi@0: for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) { aoqi@0: ciExceptionHandler* handler = str.handler(); aoqi@0: int ex_start = handler->start(); aoqi@0: int ex_end = handler->limit(); aoqi@0: aoqi@0: int bci = ex_start; aoqi@0: while (bci < ex_end) { aoqi@0: ciBlock *b = block_containing(bci); aoqi@0: b->set_has_handler(); aoqi@0: bci = b->limit_bci(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ciMethodBlocks::clear_processed() { aoqi@0: for (int i = 0; i < _blocks->length(); i++) aoqi@0: _blocks->at(i)->clear_processed(); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void ciMethodBlocks::dump() { aoqi@0: tty->print("---- blocks for method: "); aoqi@0: _method->print(); aoqi@0: tty->cr(); aoqi@0: for (int i = 0; i < _blocks->length(); i++) { aoqi@0: tty->print(" B%d: ", i); _blocks->at(i)->dump(); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: ciBlock::ciBlock(ciMethod *method, int index, int start_bci) : aoqi@0: #ifndef PRODUCT aoqi@0: _method(method), aoqi@0: #endif aoqi@0: _idx(index), _flags(0), _start_bci(start_bci), _limit_bci(-1), _control_bci(fall_through_bci), aoqi@0: _ex_start_bci(-1), _ex_limit_bci(-1) { aoqi@0: } aoqi@0: aoqi@0: void ciBlock::set_exception_range(int start_bci, int limit_bci) { aoqi@0: assert(limit_bci >= start_bci, "valid range"); aoqi@0: assert(!is_handler() && _ex_start_bci == -1 && _ex_limit_bci == -1, "must not be handler"); aoqi@0: _ex_start_bci = start_bci; aoqi@0: _ex_limit_bci = limit_bci; aoqi@0: set_handler(); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: static const char *flagnames[] = { aoqi@0: "Processed", aoqi@0: "Handler", aoqi@0: "MayThrow", aoqi@0: "Jsr", aoqi@0: "Ret", aoqi@0: "RetTarget", aoqi@0: "HasHandler", aoqi@0: }; aoqi@0: aoqi@0: void ciBlock::dump() { aoqi@0: tty->print(" [%d .. %d), {", _start_bci, _limit_bci); aoqi@0: for (int i = 0; i < 8; i++) { aoqi@0: if ((_flags & (1 << i)) != 0) { aoqi@0: tty->print(" %s", flagnames[i]); aoqi@0: } aoqi@0: } aoqi@0: tty->print(" ]"); aoqi@0: if (is_handler()) aoqi@0: tty->print(" handles(%d..%d)", _ex_start_bci, _ex_limit_bci); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // ciBlock::print_on aoqi@0: void ciBlock::print_on(outputStream* st) const { aoqi@0: st->print_cr("--------------------------------------------------------"); aoqi@0: st->print ("ciBlock [%d - %d) control : ", start_bci(), limit_bci()); aoqi@0: if (control_bci() == fall_through_bci) { aoqi@0: st->print_cr("%d:fall through", limit_bci()); aoqi@0: } else { aoqi@0: st->print_cr("%d:%s", control_bci(), aoqi@0: Bytecodes::name(method()->java_code_at_bci(control_bci()))); aoqi@0: } aoqi@0: aoqi@0: if (Verbose || WizardMode) { aoqi@0: method()->print_codes_on(start_bci(), limit_bci(), st); aoqi@0: } aoqi@0: } aoqi@0: #endif