src/share/vm/ci/ciMethodBlocks.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 2462
8012aa3ccede
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "ci/ciMethodBlocks.hpp"
aoqi@0 27 #include "ci/ciStreams.hpp"
aoqi@0 28 #include "interpreter/bytecode.hpp"
aoqi@0 29 #include "utilities/copy.hpp"
aoqi@0 30
aoqi@0 31 // ciMethodBlocks
aoqi@0 32
aoqi@0 33
aoqi@0 34
aoqi@0 35 ciBlock *ciMethodBlocks::block_containing(int bci) {
aoqi@0 36 ciBlock *blk = _bci_to_block[bci];
aoqi@0 37 return blk;
aoqi@0 38 }
aoqi@0 39
aoqi@0 40 bool ciMethodBlocks::is_block_start(int bci) {
aoqi@0 41 assert(bci >=0 && bci < _code_size, "valid bytecode range");
aoqi@0 42 ciBlock *b = _bci_to_block[bci];
aoqi@0 43 assert(b != NULL, "must have block for bytecode");
aoqi@0 44 return b->start_bci() == bci;
aoqi@0 45 }
aoqi@0 46
aoqi@0 47 // ------------------------------------------------------------------
aoqi@0 48 // ciMethodBlocks::split_block_at
aoqi@0 49 //
aoqi@0 50 // Split the block spanning bci into two separate ranges. The former
aoqi@0 51 // block becomes the second half and a new range is created for the
aoqi@0 52 // first half. Returns the range beginning at bci.
aoqi@0 53 ciBlock *ciMethodBlocks::split_block_at(int bci) {
aoqi@0 54 ciBlock *former_block = block_containing(bci);
aoqi@0 55 ciBlock *new_block = new(_arena) ciBlock(_method, _num_blocks++, former_block->start_bci());
aoqi@0 56 _blocks->append(new_block);
aoqi@0 57 assert(former_block != NULL, "must not be NULL");
aoqi@0 58 new_block->set_limit_bci(bci);
aoqi@0 59 former_block->set_start_bci(bci);
aoqi@0 60 for (int pos=bci-1; pos >= 0; pos--) {
aoqi@0 61 ciBlock *current_block = block_containing(pos);
aoqi@0 62 if (current_block == former_block) {
aoqi@0 63 // Replace it.
aoqi@0 64 _bci_to_block[pos] = new_block;
aoqi@0 65 } else if (current_block == NULL) {
aoqi@0 66 // Non-bytecode start. Skip.
aoqi@0 67 continue;
aoqi@0 68 } else {
aoqi@0 69 // We are done with our backwards walk
aoqi@0 70 break;
aoqi@0 71 }
aoqi@0 72 }
aoqi@0 73 // Move an exception handler information if needed.
aoqi@0 74 if (former_block->is_handler()) {
aoqi@0 75 int ex_start = former_block->ex_start_bci();
aoqi@0 76 int ex_end = former_block->ex_limit_bci();
aoqi@0 77 new_block->set_exception_range(ex_start, ex_end);
aoqi@0 78 // Clear information in former_block.
aoqi@0 79 former_block->clear_exception_handler();
aoqi@0 80 }
aoqi@0 81 return former_block;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 ciBlock *ciMethodBlocks::make_block_at(int bci) {
aoqi@0 85 ciBlock *cb = block_containing(bci);
aoqi@0 86 if (cb == NULL ) {
aoqi@0 87 // This is our first time visiting this bytecode. Create
aoqi@0 88 // a fresh block and assign it this starting point.
aoqi@0 89 ciBlock *nb = new(_arena) ciBlock(_method, _num_blocks++, bci);
aoqi@0 90 _blocks->append(nb);
aoqi@0 91 _bci_to_block[bci] = nb;
aoqi@0 92 return nb;
aoqi@0 93 } else if (cb->start_bci() == bci) {
aoqi@0 94 // The block begins at bci. Simply return it.
aoqi@0 95 return cb;
aoqi@0 96 } else {
aoqi@0 97 // We have already created a block containing bci but
aoqi@0 98 // not starting at bci. This existing block needs to
aoqi@0 99 // be split into two.
aoqi@0 100 return split_block_at(bci);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 ciBlock *ciMethodBlocks::make_dummy_block() {
aoqi@0 105 ciBlock *dum = new(_arena) ciBlock(_method, -1, 0);
aoqi@0 106 return dum;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 void ciMethodBlocks::do_analysis() {
aoqi@0 110 ciBytecodeStream s(_method);
aoqi@0 111 ciBlock *cur_block = block_containing(0);
aoqi@0 112 int limit_bci = _method->code_size();
aoqi@0 113
aoqi@0 114 while (s.next() != ciBytecodeStream::EOBC()) {
aoqi@0 115 int bci = s.cur_bci();
aoqi@0 116 // Determine if a new block has been made at the current bci. If
aoqi@0 117 // this block differs from our current range, switch to the new
aoqi@0 118 // one and end the old one.
aoqi@0 119 assert(cur_block != NULL, "must always have a current block");
aoqi@0 120 ciBlock *new_block = block_containing(bci);
aoqi@0 121 if (new_block == NULL || new_block == cur_block) {
aoqi@0 122 // We have not marked this bci as the start of a new block.
aoqi@0 123 // Keep interpreting the current_range.
aoqi@0 124 _bci_to_block[bci] = cur_block;
aoqi@0 125 } else {
aoqi@0 126 cur_block->set_limit_bci(bci);
aoqi@0 127 cur_block = new_block;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 switch (s.cur_bc()) {
aoqi@0 131 case Bytecodes::_ifeq :
aoqi@0 132 case Bytecodes::_ifne :
aoqi@0 133 case Bytecodes::_iflt :
aoqi@0 134 case Bytecodes::_ifge :
aoqi@0 135 case Bytecodes::_ifgt :
aoqi@0 136 case Bytecodes::_ifle :
aoqi@0 137 case Bytecodes::_if_icmpeq :
aoqi@0 138 case Bytecodes::_if_icmpne :
aoqi@0 139 case Bytecodes::_if_icmplt :
aoqi@0 140 case Bytecodes::_if_icmpge :
aoqi@0 141 case Bytecodes::_if_icmpgt :
aoqi@0 142 case Bytecodes::_if_icmple :
aoqi@0 143 case Bytecodes::_if_acmpeq :
aoqi@0 144 case Bytecodes::_if_acmpne :
aoqi@0 145 case Bytecodes::_ifnull :
aoqi@0 146 case Bytecodes::_ifnonnull :
aoqi@0 147 {
aoqi@0 148 cur_block->set_control_bci(bci);
aoqi@0 149 ciBlock *fall_through = make_block_at(s.next_bci());
aoqi@0 150 int dest_bci = s.get_dest();
aoqi@0 151 ciBlock *dest = make_block_at(dest_bci);
aoqi@0 152 break;
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 case Bytecodes::_goto :
aoqi@0 156 {
aoqi@0 157 cur_block->set_control_bci(bci);
aoqi@0 158 if (s.next_bci() < limit_bci) {
aoqi@0 159 (void) make_block_at(s.next_bci());
aoqi@0 160 }
aoqi@0 161 int dest_bci = s.get_dest();
aoqi@0 162 ciBlock *dest = make_block_at(dest_bci);
aoqi@0 163 break;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 case Bytecodes::_jsr :
aoqi@0 167 {
aoqi@0 168 cur_block->set_control_bci(bci);
aoqi@0 169 ciBlock *ret = make_block_at(s.next_bci());
aoqi@0 170 int dest_bci = s.get_dest();
aoqi@0 171 ciBlock *dest = make_block_at(dest_bci);
aoqi@0 172 break;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 case Bytecodes::_tableswitch :
aoqi@0 176 {
aoqi@0 177 cur_block->set_control_bci(bci);
aoqi@0 178 Bytecode_tableswitch sw(&s);
aoqi@0 179 int len = sw.length();
aoqi@0 180 ciBlock *dest;
aoqi@0 181 int dest_bci;
aoqi@0 182 for (int i = 0; i < len; i++) {
aoqi@0 183 dest_bci = s.cur_bci() + sw.dest_offset_at(i);
aoqi@0 184 dest = make_block_at(dest_bci);
aoqi@0 185 }
aoqi@0 186 dest_bci = s.cur_bci() + sw.default_offset();
aoqi@0 187 make_block_at(dest_bci);
aoqi@0 188 if (s.next_bci() < limit_bci) {
aoqi@0 189 dest = make_block_at(s.next_bci());
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192 break;
aoqi@0 193
aoqi@0 194 case Bytecodes::_lookupswitch:
aoqi@0 195 {
aoqi@0 196 cur_block->set_control_bci(bci);
aoqi@0 197 Bytecode_lookupswitch sw(&s);
aoqi@0 198 int len = sw.number_of_pairs();
aoqi@0 199 ciBlock *dest;
aoqi@0 200 int dest_bci;
aoqi@0 201 for (int i = 0; i < len; i++) {
aoqi@0 202 dest_bci = s.cur_bci() + sw.pair_at(i).offset();
aoqi@0 203 dest = make_block_at(dest_bci);
aoqi@0 204 }
aoqi@0 205 dest_bci = s.cur_bci() + sw.default_offset();
aoqi@0 206 dest = make_block_at(dest_bci);
aoqi@0 207 if (s.next_bci() < limit_bci) {
aoqi@0 208 dest = make_block_at(s.next_bci());
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211 break;
aoqi@0 212
aoqi@0 213 case Bytecodes::_goto_w :
aoqi@0 214 {
aoqi@0 215 cur_block->set_control_bci(bci);
aoqi@0 216 if (s.next_bci() < limit_bci) {
aoqi@0 217 (void) make_block_at(s.next_bci());
aoqi@0 218 }
aoqi@0 219 int dest_bci = s.get_far_dest();
aoqi@0 220 ciBlock *dest = make_block_at(dest_bci);
aoqi@0 221 break;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 case Bytecodes::_jsr_w :
aoqi@0 225 {
aoqi@0 226 cur_block->set_control_bci(bci);
aoqi@0 227 ciBlock *ret = make_block_at(s.next_bci());
aoqi@0 228 int dest_bci = s.get_far_dest();
aoqi@0 229 ciBlock *dest = make_block_at(dest_bci);
aoqi@0 230 break;
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 case Bytecodes::_athrow :
aoqi@0 234 cur_block->set_may_throw();
aoqi@0 235 // fall-through
aoqi@0 236 case Bytecodes::_ret :
aoqi@0 237 case Bytecodes::_ireturn :
aoqi@0 238 case Bytecodes::_lreturn :
aoqi@0 239 case Bytecodes::_freturn :
aoqi@0 240 case Bytecodes::_dreturn :
aoqi@0 241 case Bytecodes::_areturn :
aoqi@0 242 case Bytecodes::_return :
aoqi@0 243 cur_block->set_control_bci(bci);
aoqi@0 244 if (s.next_bci() < limit_bci) {
aoqi@0 245 (void) make_block_at(s.next_bci());
aoqi@0 246 }
aoqi@0 247 break;
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250 // End the last block
aoqi@0 251 cur_block->set_limit_bci(limit_bci);
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 ciMethodBlocks::ciMethodBlocks(Arena *arena, ciMethod *meth): _method(meth),
aoqi@0 255 _arena(arena), _num_blocks(0), _code_size(meth->code_size()) {
aoqi@0 256 int block_estimate = _code_size / 8;
aoqi@0 257
aoqi@0 258 _blocks = new(_arena) GrowableArray<ciBlock *>(_arena, block_estimate, 0, NULL);
aoqi@0 259 int b2bsize = _code_size * sizeof(ciBlock **);
aoqi@0 260 _bci_to_block = (ciBlock **) arena->Amalloc(b2bsize);
aoqi@0 261 Copy::zero_to_words((HeapWord*) _bci_to_block, b2bsize / sizeof(HeapWord));
aoqi@0 262
aoqi@0 263 // create initial block covering the entire method
aoqi@0 264 ciBlock *b = new(arena) ciBlock(_method, _num_blocks++, 0);
aoqi@0 265 _blocks->append(b);
aoqi@0 266 _bci_to_block[0] = b;
aoqi@0 267
aoqi@0 268 // create blocks for exception handlers
aoqi@0 269 if (meth->has_exception_handlers()) {
aoqi@0 270 for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) {
aoqi@0 271 ciExceptionHandler* handler = str.handler();
aoqi@0 272 ciBlock *eb = make_block_at(handler->handler_bci());
aoqi@0 273 //
aoqi@0 274 // Several exception handlers can have the same handler_bci:
aoqi@0 275 //
aoqi@0 276 // try {
aoqi@0 277 // if (a.foo(b) < 0) {
aoqi@0 278 // return a.error();
aoqi@0 279 // }
aoqi@0 280 // return CoderResult.UNDERFLOW;
aoqi@0 281 // } finally {
aoqi@0 282 // a.position(b);
aoqi@0 283 // }
aoqi@0 284 //
aoqi@0 285 // The try block above is divided into 2 exception blocks
aoqi@0 286 // separated by 'areturn' bci.
aoqi@0 287 //
aoqi@0 288 int ex_start = handler->start();
aoqi@0 289 int ex_end = handler->limit();
aoqi@0 290 // ensure a block at the start of exception range and start of following code
aoqi@0 291 (void) make_block_at(ex_start);
aoqi@0 292 if (ex_end < _code_size)
aoqi@0 293 (void) make_block_at(ex_end);
aoqi@0 294
aoqi@0 295 if (eb->is_handler()) {
aoqi@0 296 // Extend old handler exception range to cover additional range.
aoqi@0 297 int old_ex_start = eb->ex_start_bci();
aoqi@0 298 int old_ex_end = eb->ex_limit_bci();
aoqi@0 299 if (ex_start > old_ex_start)
aoqi@0 300 ex_start = old_ex_start;
aoqi@0 301 if (ex_end < old_ex_end)
aoqi@0 302 ex_end = old_ex_end;
aoqi@0 303 eb->clear_exception_handler(); // Reset exception information
aoqi@0 304 }
aoqi@0 305 eb->set_exception_range(ex_start, ex_end);
aoqi@0 306 }
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 // scan the bytecodes and identify blocks
aoqi@0 310 do_analysis();
aoqi@0 311
aoqi@0 312 // mark blocks that have exception handlers
aoqi@0 313 if (meth->has_exception_handlers()) {
aoqi@0 314 for(ciExceptionHandlerStream str(meth); !str.is_done(); str.next()) {
aoqi@0 315 ciExceptionHandler* handler = str.handler();
aoqi@0 316 int ex_start = handler->start();
aoqi@0 317 int ex_end = handler->limit();
aoqi@0 318
aoqi@0 319 int bci = ex_start;
aoqi@0 320 while (bci < ex_end) {
aoqi@0 321 ciBlock *b = block_containing(bci);
aoqi@0 322 b->set_has_handler();
aoqi@0 323 bci = b->limit_bci();
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 }
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 void ciMethodBlocks::clear_processed() {
aoqi@0 330 for (int i = 0; i < _blocks->length(); i++)
aoqi@0 331 _blocks->at(i)->clear_processed();
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 #ifndef PRODUCT
aoqi@0 335 void ciMethodBlocks::dump() {
aoqi@0 336 tty->print("---- blocks for method: ");
aoqi@0 337 _method->print();
aoqi@0 338 tty->cr();
aoqi@0 339 for (int i = 0; i < _blocks->length(); i++) {
aoqi@0 340 tty->print(" B%d: ", i); _blocks->at(i)->dump();
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343 #endif
aoqi@0 344
aoqi@0 345
aoqi@0 346 ciBlock::ciBlock(ciMethod *method, int index, int start_bci) :
aoqi@0 347 #ifndef PRODUCT
aoqi@0 348 _method(method),
aoqi@0 349 #endif
aoqi@0 350 _idx(index), _flags(0), _start_bci(start_bci), _limit_bci(-1), _control_bci(fall_through_bci),
aoqi@0 351 _ex_start_bci(-1), _ex_limit_bci(-1) {
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 void ciBlock::set_exception_range(int start_bci, int limit_bci) {
aoqi@0 355 assert(limit_bci >= start_bci, "valid range");
aoqi@0 356 assert(!is_handler() && _ex_start_bci == -1 && _ex_limit_bci == -1, "must not be handler");
aoqi@0 357 _ex_start_bci = start_bci;
aoqi@0 358 _ex_limit_bci = limit_bci;
aoqi@0 359 set_handler();
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 #ifndef PRODUCT
aoqi@0 363 static const char *flagnames[] = {
aoqi@0 364 "Processed",
aoqi@0 365 "Handler",
aoqi@0 366 "MayThrow",
aoqi@0 367 "Jsr",
aoqi@0 368 "Ret",
aoqi@0 369 "RetTarget",
aoqi@0 370 "HasHandler",
aoqi@0 371 };
aoqi@0 372
aoqi@0 373 void ciBlock::dump() {
aoqi@0 374 tty->print(" [%d .. %d), {", _start_bci, _limit_bci);
aoqi@0 375 for (int i = 0; i < 8; i++) {
aoqi@0 376 if ((_flags & (1 << i)) != 0) {
aoqi@0 377 tty->print(" %s", flagnames[i]);
aoqi@0 378 }
aoqi@0 379 }
aoqi@0 380 tty->print(" ]");
aoqi@0 381 if (is_handler())
aoqi@0 382 tty->print(" handles(%d..%d)", _ex_start_bci, _ex_limit_bci);
aoqi@0 383 tty->cr();
aoqi@0 384 }
aoqi@0 385
aoqi@0 386 // ------------------------------------------------------------------
aoqi@0 387 // ciBlock::print_on
aoqi@0 388 void ciBlock::print_on(outputStream* st) const {
aoqi@0 389 st->print_cr("--------------------------------------------------------");
aoqi@0 390 st->print ("ciBlock [%d - %d) control : ", start_bci(), limit_bci());
aoqi@0 391 if (control_bci() == fall_through_bci) {
aoqi@0 392 st->print_cr("%d:fall through", limit_bci());
aoqi@0 393 } else {
aoqi@0 394 st->print_cr("%d:%s", control_bci(),
aoqi@0 395 Bytecodes::name(method()->java_code_at_bci(control_bci())));
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 if (Verbose || WizardMode) {
aoqi@0 399 method()->print_codes_on(start_bci(), limit_bci(), st);
aoqi@0 400 }
aoqi@0 401 }
aoqi@0 402 #endif

mercurial