1 #ifdef USE_PRAGMA_IDENT_SRC 2 #pragma ident "@(#)bcEscapeAnalyzer.cpp 1.7 07/05/17 15:49:50 JVM" 3 #endif 4 /* 5 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7 * 8 * This code is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License version 2 only, as 10 * published by the Free Software Foundation. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 23 * CA 95054 USA or visit www.sun.com if you need additional information or 24 * have any questions. 25 * 26 */ 27 28 29 #include "incls/_precompiled.incl" 30 #include "incls/_bcEscapeAnalyzer.cpp.incl" 31 32 33 #ifndef PRODUCT 34 #define TRACE_BCEA(level, code) \ 35 if (EstimateArgEscape && BCEATraceLevel >= level) { \ 36 code; \ 37 } 38 #else 39 #define TRACE_BCEA(level, code) 40 #endif 41 42 // Maintain a map of which aguments a local variable or 43 // stack slot may contain. In addition to tracking 44 // arguments, it tracks two special values, "allocated" 45 // which represents any object allocated in the current 46 // method, and "unknown" which is any other object. 47 // Up to 30 arguments are handled, with the last one 48 // representing summary information for any extra arguments 49 class BCEscapeAnalyzer::ArgumentMap { 50 uint _bits; 51 enum {MAXBIT = 29, 52 ALLOCATED = 1, 53 UNKNOWN = 2}; 54 55 uint int_to_bit(uint e) const { 56 if (e > MAXBIT) 57 e = MAXBIT; 58 return (1 << (e + 2)); 59 } 60 61 public: 62 ArgumentMap() { _bits = 0;} 63 void set_bits(uint bits) { _bits = bits;} 64 uint get_bits() const { return _bits;} 65 void clear() { _bits = 0;} 66 void set_all() { _bits = ~0u; } 67 bool is_empty() const { return _bits == 0; } 68 bool contains(uint var) const { return (_bits & int_to_bit(var)) != 0; } 69 bool is_singleton(uint var) const { return (_bits == int_to_bit(var)); } 70 bool contains_unknown() const { return (_bits & UNKNOWN) != 0; } 71 bool contains_allocated() const { return (_bits & ALLOCATED) != 0; } 72 bool contains_vars() const { return (_bits & (((1 << MAXBIT) -1) << 2)) != 0; } 73 void set(uint var) { _bits = int_to_bit(var); } 74 void add(uint var) { _bits |= int_to_bit(var); } 75 void add_unknown() { _bits = UNKNOWN; } 76 void add_allocated() { _bits = ALLOCATED; } 77 void set_union(const ArgumentMap &am) { _bits |= am._bits; } 78 void set_intersect(const ArgumentMap &am) { _bits |= am._bits; } 79 void set_difference(const ArgumentMap &am) { _bits &= ~am._bits; } 80 void operator=(const ArgumentMap &am) { _bits = am._bits; } 81 bool operator==(const ArgumentMap &am) { return _bits == am._bits; } 82 bool operator!=(const ArgumentMap &am) { return _bits != am._bits; } 83 }; 84 85 class BCEscapeAnalyzer::StateInfo { 86 public: 87 ArgumentMap *_vars; 88 ArgumentMap *_stack; 89 short _stack_height; 90 short _max_stack; 91 bool _initialized; 92 ArgumentMap empty_map; 93 94 StateInfo() { 95 empty_map.clear(); 96 } 97 98 ArgumentMap raw_pop() { assert(_stack_height > 0, "stack underflow"); return _stack[--_stack_height]; } 99 ArgumentMap apop() { return raw_pop(); } 100 void spop() { raw_pop(); } 101 void lpop() { spop(); spop(); } 102 void raw_push(ArgumentMap i) { assert(_stack_height < _max_stack, "stack overflow"); _stack[_stack_height++] = i; } 103 void apush(ArgumentMap i) { raw_push(i); } 104 void spush() { raw_push(empty_map); } 105 void lpush() { spush(); spush(); } 106 107 }; 108 109 void BCEscapeAnalyzer::set_returned(ArgumentMap vars) { 110 for (int i = 0; i <= _arg_size; i++) { 111 if (vars.contains(i)) 112 _arg_returned.set_bit(i); 113 } 114 _return_local = _return_local && !(vars.contains_unknown() || vars.contains_allocated()); 115 _return_allocated = _return_allocated && vars.contains_allocated() && !(vars.contains_unknown() || vars.contains_vars()); 116 } 117 118 119 // return true if any element of vars is an argument 120 bool BCEscapeAnalyzer::is_argument(ArgumentMap vars) { 121 for (int i = 0; i <= _arg_size; i++) { 122 if (vars.contains(i)) 123 return true; 124 } 125 return false; 126 } 127 128 // return true if any element of vars is an arg_stack argument 129 bool BCEscapeAnalyzer::is_arg_stack(ArgumentMap vars){ 130 if (_conservative) 131 return true; 132 for (int i = 0; i <= _arg_size; i++) { 133 if (vars.contains(i) && _arg_stack.at(i)) 134 return true; 135 } 136 return false; 137 } 138 139 void BCEscapeAnalyzer::clear_bits(ArgumentMap vars, BitMap &bm) { 140 for (int i = 0; i <= _arg_size; i++) { 141 if (vars.contains(i)) { 142 bm.clear_bit(i); 143 } 144 } 145 } 146 void BCEscapeAnalyzer::set_method_escape(ArgumentMap vars) { 147 clear_bits(vars, _arg_local); 148 } 149 150 void BCEscapeAnalyzer::set_global_escape(ArgumentMap vars) { 151 clear_bits(vars, _arg_local); 152 clear_bits(vars, _arg_stack); 153 if (vars.contains_allocated()) 154 _allocated_escapes = true; 155 } 156 157 void BCEscapeAnalyzer::set_dirty(ArgumentMap vars) { 158 clear_bits(vars, _dirty); 159 } 160 161 bool BCEscapeAnalyzer::is_recursive_call(ciMethod* callee) { 162 for (BCEscapeAnalyzer* scope = this; scope != NULL; scope = scope->_parent) { 163 if (scope->method() == callee) { 164 return true; 165 } 166 } 167 return false; 168 } 169 170 void BCEscapeAnalyzer::invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder) { 171 int i; 172 173 // retrieve information about the callee 174 ciInstanceKlass* klass = target->holder(); 175 ciInstanceKlass* calling_klass = method()->holder(); 176 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder); 177 ciInstanceKlass* actual_recv = callee_holder; 178 179 // compute size of arguments 180 int arg_size = target->arg_size(); 181 if (!target->is_loaded() && code == Bytecodes::_invokestatic) { 182 arg_size--; 183 } 184 int arg_base = MAX2(state._stack_height - arg_size, 0); 185 186 // direct recursive calls are skipped if they can be bound statically without introducing 187 // dependencies and if parameters are passed at the same position as in the current method 188 // other calls are skipped if there are no unescaped arguments passed to them 189 bool directly_recursive = (method() == target) && 190 (code != Bytecodes::_invokevirtual || target->is_final_method() || state._stack[arg_base] .is_empty()); 191 192 // check if analysis of callee can safely be skipped 193 bool skip_callee = true; 194 for (i = state._stack_height - 1; i >= arg_base && skip_callee; i--) { 195 ArgumentMap arg = state._stack[i]; 196 skip_callee = !is_argument(arg) || !is_arg_stack(arg) || (directly_recursive && arg.is_singleton(i - arg_base)); 197 } 198 if (skip_callee) { 199 TRACE_BCEA(3, tty->print_cr("[EA] skipping method %s::%s", holder->name()->as_utf8(), target->name()->as_utf8())); 200 for (i = 0; i < arg_size; i++) { 201 set_method_escape(state.raw_pop()); 202 } 203 return; 204 } 205 206 // determine actual method (use CHA if necessary) 207 ciMethod* inline_target = NULL; 208 if (target->is_loaded() && klass->is_loaded() 209 && (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized()) 210 && target->will_link(klass, callee_holder, code)) { 211 if (code == Bytecodes::_invokestatic 212 || code == Bytecodes::_invokespecial 213 || code == Bytecodes::_invokevirtual && target->is_final_method()) { 214 inline_target = target; 215 } else { 216 inline_target = target->find_monomorphic_target(calling_klass, callee_holder, actual_recv); 217 } 218 } 219 220 if (inline_target != NULL && !is_recursive_call(inline_target)) { 221 // analyze callee 222 BCEscapeAnalyzer analyzer(inline_target, this); 223 224 // adjust escape state of actual parameters 225 bool must_record_dependencies = false; 226 for (i = arg_size - 1; i >= 0; i--) { 227 ArgumentMap arg = state.raw_pop(); 228 if (!is_argument(arg)) 229 continue; 230 if (!is_arg_stack(arg)) { 231 // arguments have already been recognized as escaping 232 } else if (analyzer.is_arg_stack(i) && !analyzer.is_arg_returned(i)) { 233 set_method_escape(arg); 234 must_record_dependencies = true; 235 } else { 236 set_global_escape(arg); 237 } 238 } 239 240 // record dependencies if at least one parameter retained stack-allocatable 241 if (must_record_dependencies) { 242 if (code == Bytecodes::_invokeinterface || code == Bytecodes::_invokevirtual && !target->is_final_method()) { 243 _dependencies.append(actual_recv); 244 _dependencies.append(inline_target); 245 } 246 _dependencies.appendAll(analyzer.dependencies()); 247 } 248 } else { 249 TRACE_BCEA(1, tty->print_cr("[EA] virtual method %s is not monomorphic.", 250 target->name()->as_utf8())); 251 // conservatively mark all actual parameters as escaping globally 252 for (i = 0; i < arg_size; i++) { 253 ArgumentMap arg = state.raw_pop(); 254 if (!is_argument(arg)) 255 continue; 256 set_global_escape(arg); 257 } 258 } 259 } 260 261 bool BCEscapeAnalyzer::contains(uint arg_set1, uint arg_set2) { 262 return ((~arg_set1) | arg_set2) == 0; 263 } 264 265 266 void BCEscapeAnalyzer::iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors) { 267 268 blk->set_processed(); 269 ciBytecodeStream s(method()); 270 int limit_bci = blk->limit_bci(); 271 bool fall_through = false; 272 ArgumentMap allocated_obj; 273 allocated_obj.add_allocated(); 274 ArgumentMap unknown_obj; 275 unknown_obj.add_unknown(); 276 ArgumentMap empty_map; 277 278 s.reset_to_bci(blk->start_bci()); 279 while (s.next() != ciBytecodeStream::EOBC() && s.cur_bci() < limit_bci) { 280 fall_through = true; 281 switch (s.cur_bc()) { 282 case Bytecodes::_nop: 283 break; 284 case Bytecodes::_aconst_null: 285 state.apush(empty_map); 286 break; 287 case Bytecodes::_iconst_m1: 288 case Bytecodes::_iconst_0: 289 case Bytecodes::_iconst_1: 290 case Bytecodes::_iconst_2: 291 case Bytecodes::_iconst_3: 292 case Bytecodes::_iconst_4: 293 case Bytecodes::_iconst_5: 294 case Bytecodes::_fconst_0: 295 case Bytecodes::_fconst_1: 296 case Bytecodes::_fconst_2: 297 case Bytecodes::_bipush: 298 case Bytecodes::_sipush: 299 state.spush(); 300 break; 301 case Bytecodes::_lconst_0: 302 case Bytecodes::_lconst_1: 303 case Bytecodes::_dconst_0: 304 case Bytecodes::_dconst_1: 305 state.lpush(); 306 break; 307 case Bytecodes::_ldc: 308 case Bytecodes::_ldc_w: 309 case Bytecodes::_ldc2_w: 310 if (type2size[s.get_constant().basic_type()] == 1) { 311 state.spush(); 312 } else { 313 state.lpush(); 314 } 315 break; 316 case Bytecodes::_aload: 317 state.apush(state._vars[s.get_index()]); 318 break; 319 case Bytecodes::_iload: 320 case Bytecodes::_fload: 321 case Bytecodes::_iload_0: 322 case Bytecodes::_iload_1: 323 case Bytecodes::_iload_2: 324 case Bytecodes::_iload_3: 325 case Bytecodes::_fload_0: 326 case Bytecodes::_fload_1: 327 case Bytecodes::_fload_2: 328 case Bytecodes::_fload_3: 329 state.spush(); 330 break; 331 case Bytecodes::_lload: 332 case Bytecodes::_dload: 333 case Bytecodes::_lload_0: 334 case Bytecodes::_lload_1: 335 case Bytecodes::_lload_2: 336 case Bytecodes::_lload_3: 337 case Bytecodes::_dload_0: 338 case Bytecodes::_dload_1: 339 case Bytecodes::_dload_2: 340 case Bytecodes::_dload_3: 341 state.lpush(); 342 break; 343 case Bytecodes::_aload_0: 344 state.apush(state._vars[0]); 345 break; 346 case Bytecodes::_aload_1: 347 state.apush(state._vars[1]); 348 break; 349 case Bytecodes::_aload_2: 350 state.apush(state._vars[2]); 351 break; 352 case Bytecodes::_aload_3: 353 state.apush(state._vars[3]); 354 break; 355 case Bytecodes::_iaload: 356 case Bytecodes::_faload: 357 case Bytecodes::_baload: 358 case Bytecodes::_caload: 359 case Bytecodes::_saload: 360 state.spop(); 361 set_method_escape(state.apop()); 362 state.spush(); 363 break; 364 case Bytecodes::_laload: 365 case Bytecodes::_daload: 366 state.spop(); 367 set_method_escape(state.apop()); 368 state.lpush(); 369 break; 370 case Bytecodes::_aaload: 371 { state.spop(); 372 ArgumentMap array = state.apop(); 373 set_method_escape(array); 374 state.apush(unknown_obj); 375 set_dirty(array); 376 } 377 break; 378 case Bytecodes::_istore: 379 case Bytecodes::_fstore: 380 case Bytecodes::_istore_0: 381 case Bytecodes::_istore_1: 382 case Bytecodes::_istore_2: 383 case Bytecodes::_istore_3: 384 case Bytecodes::_fstore_0: 385 case Bytecodes::_fstore_1: 386 case Bytecodes::_fstore_2: 387 case Bytecodes::_fstore_3: 388 state.spop(); 389 break; 390 case Bytecodes::_lstore: 391 case Bytecodes::_dstore: 392 case Bytecodes::_lstore_0: 393 case Bytecodes::_lstore_1: 394 case Bytecodes::_lstore_2: 395 case Bytecodes::_lstore_3: 396 case Bytecodes::_dstore_0: 397 case Bytecodes::_dstore_1: 398 case Bytecodes::_dstore_2: 399 case Bytecodes::_dstore_3: 400 state.lpop(); 401 break; 402 case Bytecodes::_astore: 403 state._vars[s.get_index()] = state.apop(); 404 break; 405 case Bytecodes::_astore_0: 406 state._vars[0] = state.apop(); 407 break; 408 case Bytecodes::_astore_1: 409 state._vars[1] = state.apop(); 410 break; 411 case Bytecodes::_astore_2: 412 state._vars[2] = state.apop(); 413 break; 414 case Bytecodes::_astore_3: 415 state._vars[3] = state.apop(); 416 break; 417 case Bytecodes::_iastore: 418 case Bytecodes::_fastore: 419 case Bytecodes::_bastore: 420 case Bytecodes::_castore: 421 case Bytecodes::_sastore: 422 { 423 state.spop(); 424 state.spop(); 425 ArgumentMap arr = state.apop(); 426 set_method_escape(arr); 427 break; 428 } 429 case Bytecodes::_lastore: 430 case Bytecodes::_dastore: 431 { 432 state.lpop(); 433 state.spop(); 434 ArgumentMap arr = state.apop(); 435 set_method_escape(arr); 436 break; 437 } 438 case Bytecodes::_aastore: 439 { 440 set_global_escape(state.apop()); 441 state.spop(); 442 ArgumentMap arr = state.apop(); 443 break; 444 } 445 case Bytecodes::_pop: 446 state.raw_pop(); 447 break; 448 case Bytecodes::_pop2: 449 state.raw_pop(); 450 state.raw_pop(); 451 break; 452 case Bytecodes::_dup: 453 { ArgumentMap w1 = state.raw_pop(); 454 state.raw_push(w1); 455 state.raw_push(w1); 456 } 457 break; 458 case Bytecodes::_dup_x1: 459 { ArgumentMap w1 = state.raw_pop(); 460 ArgumentMap w2 = state.raw_pop(); 461 state.raw_push(w1); 462 state.raw_push(w2); 463 state.raw_push(w1); 464 } 465 break; 466 case Bytecodes::_dup_x2: 467 { ArgumentMap w1 = state.raw_pop(); 468 ArgumentMap w2 = state.raw_pop(); 469 ArgumentMap w3 = state.raw_pop(); 470 state.raw_push(w1); 471 state.raw_push(w3); 472 state.raw_push(w2); 473 state.raw_push(w1); 474 } 475 break; 476 case Bytecodes::_dup2: 477 { ArgumentMap w1 = state.raw_pop(); 478 ArgumentMap w2 = state.raw_pop(); 479 state.raw_push(w2); 480 state.raw_push(w1); 481 state.raw_push(w2); 482 state.raw_push(w1); 483 } 484 break; 485 case Bytecodes::_dup2_x1: 486 { ArgumentMap w1 = state.raw_pop(); 487 ArgumentMap w2 = state.raw_pop(); 488 ArgumentMap w3 = state.raw_pop(); 489 state.raw_push(w2); 490 state.raw_push(w1); 491 state.raw_push(w3); 492 state.raw_push(w2); 493 state.raw_push(w1); 494 } 495 break; 496 case Bytecodes::_dup2_x2: 497 { ArgumentMap w1 = state.raw_pop(); 498 ArgumentMap w2 = state.raw_pop(); 499 ArgumentMap w3 = state.raw_pop(); 500 ArgumentMap w4 = state.raw_pop(); 501 state.raw_push(w2); 502 state.raw_push(w1); 503 state.raw_push(w4); 504 state.raw_push(w3); 505 state.raw_push(w2); 506 state.raw_push(w1); 507 } 508 break; 509 case Bytecodes::_swap: 510 { ArgumentMap w1 = state.raw_pop(); 511 ArgumentMap w2 = state.raw_pop(); 512 state.raw_push(w1); 513 state.raw_push(w2); 514 } 515 break; 516 case Bytecodes::_iadd: 517 case Bytecodes::_fadd: 518 case Bytecodes::_isub: 519 case Bytecodes::_fsub: 520 case Bytecodes::_imul: 521 case Bytecodes::_fmul: 522 case Bytecodes::_idiv: 523 case Bytecodes::_fdiv: 524 case Bytecodes::_irem: 525 case Bytecodes::_frem: 526 case Bytecodes::_iand: 527 case Bytecodes::_ior: 528 case Bytecodes::_ixor: 529 state.spop(); 530 state.spop(); 531 state.spush(); 532 break; 533 case Bytecodes::_ladd: 534 case Bytecodes::_dadd: 535 case Bytecodes::_lsub: 536 case Bytecodes::_dsub: 537 case Bytecodes::_lmul: 538 case Bytecodes::_dmul: 539 case Bytecodes::_ldiv: 540 case Bytecodes::_ddiv: 541 case Bytecodes::_lrem: 542 case Bytecodes::_drem: 543 case Bytecodes::_land: 544 case Bytecodes::_lor: 545 case Bytecodes::_lxor: 546 state.lpop(); 547 state.lpop(); 548 state.lpush(); 549 break; 550 case Bytecodes::_ishl: 551 case Bytecodes::_ishr: 552 case Bytecodes::_iushr: 553 state.spop(); 554 state.spop(); 555 state.spush(); 556 break; 557 case Bytecodes::_lshl: 558 case Bytecodes::_lshr: 559 case Bytecodes::_lushr: 560 state.spop(); 561 state.lpop(); 562 state.lpush(); 563 break; 564 case Bytecodes::_ineg: 565 case Bytecodes::_fneg: 566 state.spop(); 567 state.spush(); 568 break; 569 case Bytecodes::_lneg: 570 case Bytecodes::_dneg: 571 state.lpop(); 572 state.lpush(); 573 break; 574 case Bytecodes::_iinc: 575 break; 576 case Bytecodes::_i2l: 577 case Bytecodes::_i2d: 578 case Bytecodes::_f2l: 579 case Bytecodes::_f2d: 580 state.spop(); 581 state.lpush(); 582 break; 583 case Bytecodes::_i2f: 584 case Bytecodes::_f2i: 585 state.spop(); 586 state.spush(); 587 break; 588 case Bytecodes::_l2i: 589 case Bytecodes::_l2f: 590 case Bytecodes::_d2i: 591 case Bytecodes::_d2f: 592 state.lpop(); 593 state.spush(); 594 break; 595 case Bytecodes::_l2d: 596 case Bytecodes::_d2l: 597 state.lpop(); 598 state.lpush(); 599 break; 600 case Bytecodes::_i2b: 601 case Bytecodes::_i2c: 602 case Bytecodes::_i2s: 603 state.spop(); 604 state.spush(); 605 break; 606 case Bytecodes::_lcmp: 607 case Bytecodes::_dcmpl: 608 case Bytecodes::_dcmpg: 609 state.lpop(); 610 state.lpop(); 611 state.spush(); 612 break; 613 case Bytecodes::_fcmpl: 614 case Bytecodes::_fcmpg: 615 state.spop(); 616 state.spop(); 617 state.spush(); 618 break; 619 case Bytecodes::_ifeq: 620 case Bytecodes::_ifne: 621 case Bytecodes::_iflt: 622 case Bytecodes::_ifge: 623 case Bytecodes::_ifgt: 624 case Bytecodes::_ifle: 625 { 626 state.spop(); 627 int dest_bci = s.get_dest(); 628 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 629 assert(s.next_bci() == limit_bci, "branch must end block"); 630 successors.push(_methodBlocks->block_containing(dest_bci)); 631 break; 632 } 633 case Bytecodes::_if_icmpeq: 634 case Bytecodes::_if_icmpne: 635 case Bytecodes::_if_icmplt: 636 case Bytecodes::_if_icmpge: 637 case Bytecodes::_if_icmpgt: 638 case Bytecodes::_if_icmple: 639 { 640 state.spop(); 641 state.spop(); 642 int dest_bci = s.get_dest(); 643 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 644 assert(s.next_bci() == limit_bci, "branch must end block"); 645 successors.push(_methodBlocks->block_containing(dest_bci)); 646 break; 647 } 648 case Bytecodes::_if_acmpeq: 649 case Bytecodes::_if_acmpne: 650 { 651 set_method_escape(state.apop()); 652 set_method_escape(state.apop()); 653 int dest_bci = s.get_dest(); 654 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 655 assert(s.next_bci() == limit_bci, "branch must end block"); 656 successors.push(_methodBlocks->block_containing(dest_bci)); 657 break; 658 } 659 case Bytecodes::_goto: 660 { 661 int dest_bci = s.get_dest(); 662 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 663 assert(s.next_bci() == limit_bci, "branch must end block"); 664 successors.push(_methodBlocks->block_containing(dest_bci)); 665 fall_through = false; 666 break; 667 } 668 case Bytecodes::_jsr: 669 { 670 int dest_bci = s.get_dest(); 671 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 672 assert(s.next_bci() == limit_bci, "branch must end block"); 673 state.apush(empty_map); 674 successors.push(_methodBlocks->block_containing(dest_bci)); 675 fall_through = false; 676 break; 677 } 678 case Bytecodes::_ret: 679 // we don't track the destination of a "ret" instruction 680 assert(s.next_bci() == limit_bci, "branch must end block"); 681 fall_through = false; 682 break; 683 case Bytecodes::_return: 684 assert(s.next_bci() == limit_bci, "return must end block"); 685 fall_through = false; 686 break; 687 case Bytecodes::_tableswitch: 688 { 689 state.spop(); 690 Bytecode_tableswitch* switch_ = Bytecode_tableswitch_at(s.cur_bcp()); 691 int len = switch_->length(); 692 int dest_bci; 693 for (int i = 0; i < len; i++) { 694 dest_bci = s.cur_bci() + switch_->dest_offset_at(i); 695 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 696 successors.push(_methodBlocks->block_containing(dest_bci)); 697 } 698 dest_bci = s.cur_bci() + switch_->default_offset(); 699 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 700 successors.push(_methodBlocks->block_containing(dest_bci)); 701 assert(s.next_bci() == limit_bci, "branch must end block"); 702 fall_through = false; 703 break; 704 } 705 case Bytecodes::_lookupswitch: 706 { 707 state.spop(); 708 Bytecode_lookupswitch* switch_ = Bytecode_lookupswitch_at(s.cur_bcp()); 709 int len = switch_->number_of_pairs(); 710 int dest_bci; 711 for (int i = 0; i < len; i++) { 712 dest_bci = s.cur_bci() + switch_->pair_at(i)->offset(); 713 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 714 successors.push(_methodBlocks->block_containing(dest_bci)); 715 } 716 dest_bci = s.cur_bci() + switch_->default_offset(); 717 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 718 successors.push(_methodBlocks->block_containing(dest_bci)); 719 fall_through = false; 720 break; 721 } 722 case Bytecodes::_ireturn: 723 case Bytecodes::_freturn: 724 state.spop(); 725 fall_through = false; 726 break; 727 case Bytecodes::_lreturn: 728 case Bytecodes::_dreturn: 729 state.lpop(); 730 fall_through = false; 731 break; 732 case Bytecodes::_areturn: 733 set_returned(state.apop()); 734 fall_through = false; 735 break; 736 case Bytecodes::_getstatic: 737 case Bytecodes::_getfield: 738 { bool will_link; 739 ciField* field = s.get_field(will_link); 740 BasicType field_type = field->type()->basic_type(); 741 if (s.cur_bc() != Bytecodes::_getstatic) { 742 set_method_escape(state.apop()); 743 } 744 if (field_type == T_OBJECT || field_type == T_ARRAY) { 745 state.apush(unknown_obj); 746 } else if (type2size[field_type] == 1) { 747 state.spush(); 748 } else { 749 state.lpush(); 750 } 751 } 752 break; 753 case Bytecodes::_putstatic: 754 case Bytecodes::_putfield: 755 { bool will_link; 756 ciField* field = s.get_field(will_link); 757 BasicType field_type = field->type()->basic_type(); 758 if (field_type == T_OBJECT || field_type == T_ARRAY) { 759 set_global_escape(state.apop()); 760 } else if (type2size[field_type] == 1) { 761 state.spop(); 762 } else { 763 state.lpop(); 764 } 765 if (s.cur_bc() != Bytecodes::_putstatic) { 766 ArgumentMap p = state.apop(); 767 set_method_escape(p); 768 } 769 } 770 break; 771 case Bytecodes::_invokevirtual: 772 case Bytecodes::_invokespecial: 773 case Bytecodes::_invokestatic: 774 case Bytecodes::_invokeinterface: 775 { bool will_link; 776 ciMethod* target = s.get_method(will_link); 777 ciKlass* holder = s.get_declared_method_holder(); 778 invoke(state, s.cur_bc(), target, holder); 779 ciType* return_type = target->return_type(); 780 if (!return_type->is_primitive_type()) { 781 state.apush(unknown_obj); 782 } else if (return_type->is_one_word()) { 783 state.spush(); 784 } else if (return_type->is_two_word()) { 785 state.lpush(); 786 } 787 } 788 break; 789 case Bytecodes::_xxxunusedxxx: 790 ShouldNotReachHere(); 791 break; 792 case Bytecodes::_new: 793 state.apush(allocated_obj); 794 break; 795 case Bytecodes::_newarray: 796 case Bytecodes::_anewarray: 797 state.spop(); 798 state.apush(allocated_obj); 799 break; 800 case Bytecodes::_multianewarray: 801 { int i = s.cur_bcp()[3]; 802 while (i-- > 0) state.spop(); 803 state.apush(allocated_obj); 804 } 805 break; 806 case Bytecodes::_arraylength: 807 set_method_escape(state.apop()); 808 state.spush(); 809 break; 810 case Bytecodes::_athrow: 811 set_global_escape(state.apop()); 812 fall_through = false; 813 break; 814 case Bytecodes::_checkcast: 815 { ArgumentMap obj = state.apop(); 816 set_method_escape(obj); 817 state.apush(obj); 818 } 819 break; 820 case Bytecodes::_instanceof: 821 set_method_escape(state.apop()); 822 state.spush(); 823 break; 824 case Bytecodes::_monitorenter: 825 case Bytecodes::_monitorexit: 826 state.apop(); 827 break; 828 case Bytecodes::_wide: 829 ShouldNotReachHere(); 830 break; 831 case Bytecodes::_ifnull: 832 case Bytecodes::_ifnonnull: 833 { 834 set_method_escape(state.apop()); 835 int dest_bci = s.get_dest(); 836 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 837 assert(s.next_bci() == limit_bci, "branch must end block"); 838 successors.push(_methodBlocks->block_containing(dest_bci)); 839 break; 840 } 841 case Bytecodes::_goto_w: 842 { 843 int dest_bci = s.get_far_dest(); 844 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 845 assert(s.next_bci() == limit_bci, "branch must end block"); 846 successors.push(_methodBlocks->block_containing(dest_bci)); 847 fall_through = false; 848 break; 849 } 850 case Bytecodes::_jsr_w: 851 { 852 int dest_bci = s.get_far_dest(); 853 assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block"); 854 assert(s.next_bci() == limit_bci, "branch must end block"); 855 state.apush(empty_map); 856 successors.push(_methodBlocks->block_containing(dest_bci)); 857 fall_through = false; 858 break; 859 } 860 case Bytecodes::_breakpoint: 861 break; 862 default: 863 ShouldNotReachHere(); 864 break; 865 } 866 867 } 868 if (fall_through) { 869 int fall_through_bci = s.cur_bci(); 870 if (fall_through_bci < _method->code_size()) { 871 assert(_methodBlocks->is_block_start(fall_through_bci), "must fall through to block start."); 872 successors.push(_methodBlocks->block_containing(fall_through_bci)); 873 } 874 } 875 } 876 877 void BCEscapeAnalyzer::merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state) { 878 StateInfo *d_state = blockstates+dest->index(); 879 int nlocals = _method->max_locals(); 880 881 // exceptions may cause transfer of control to handlers in the middle of a 882 // block, so we don't merge the incoming state of exception handlers 883 if (dest->is_handler()) 884 return; 885 if (!d_state->_initialized ) { 886 // destination not initialized, just copy 887 for (int i = 0; i < nlocals; i++) { 888 d_state->_vars[i] = s_state->_vars[i]; 889 } 890 for (int i = 0; i < s_state->_stack_height; i++) { 891 d_state->_stack[i] = s_state->_stack[i]; 892 } 893 d_state->_stack_height = s_state->_stack_height; 894 d_state->_max_stack = s_state->_max_stack; 895 d_state->_initialized = true; 896 } else if (!dest->processed()) { 897 // we have not yet walked the bytecodes of dest, we can merge 898 // the states 899 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match"); 900 for (int i = 0; i < nlocals; i++) { 901 d_state->_vars[i].set_union(s_state->_vars[i]); 902 } 903 for (int i = 0; i < s_state->_stack_height; i++) { 904 d_state->_stack[i].set_union(s_state->_stack[i]); 905 } 906 } else { 907 // the bytecodes of dest have already been processed, mark any 908 // arguments in the source state which are not in the dest state 909 // as global escape. 910 // Future refinement: we only need to mark these variable to the 911 // maximum escape of any variables in dest state 912 assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match"); 913 ArgumentMap extra_vars; 914 for (int i = 0; i < nlocals; i++) { 915 ArgumentMap t; 916 t = s_state->_vars[i]; 917 t.set_difference(d_state->_vars[i]); 918 extra_vars.set_union(t); 919 } 920 for (int i = 0; i < s_state->_stack_height; i++) { 921 ArgumentMap t; 922 t.clear(); 923 t = s_state->_stack[i]; 924 t.set_difference(d_state->_stack[i]); 925 extra_vars.set_union(t); 926 } 927 set_global_escape(extra_vars); 928 } 929 } 930 931 void BCEscapeAnalyzer::iterate_blocks(Arena *arena) { 932 int numblocks = _methodBlocks->num_blocks(); 933 int stkSize = _method->max_stack(); 934 int numLocals = _method->max_locals(); 935 StateInfo state; 936 937 int datacount = (numblocks + 1) * (stkSize + numLocals); 938 int datasize = datacount * sizeof(ArgumentMap); 939 StateInfo *blockstates = (StateInfo *) arena->Amalloc(_methodBlocks->num_blocks() * sizeof(StateInfo)); 940 ArgumentMap *statedata = (ArgumentMap *) arena->Amalloc(datasize); 941 for (int i = 0; i < datacount; i++) ::new ((void*)&statedata[i]) ArgumentMap(); 942 ArgumentMap *dp = statedata; 943 state._vars = dp; 944 dp += numLocals; 945 state._stack = dp; 946 dp += stkSize; 947 state._initialized = false; 948 state._max_stack = stkSize; 949 for (int i = 0; i < numblocks; i++) { 950 blockstates[i]._vars = dp; 951 dp += numLocals; 952 blockstates[i]._stack = dp; 953 dp += stkSize; 954 blockstates[i]._initialized = false; 955 blockstates[i]._stack_height = 0; 956 blockstates[i]._max_stack = stkSize; 957 } 958 GrowableArray<ciBlock *> worklist(arena, numblocks / 4, 0, NULL); 959 GrowableArray<ciBlock *> successors(arena, 4, 0, NULL); 960 961 _methodBlocks->clear_processed(); 962 963 // initialize block 0 state from method signature 964 ArgumentMap allVars; // all oop arguments to method 965 ciSignature* sig = method()->signature(); 966 int j = 0; 967 if (!method()->is_static()) { 968 // record information for "this" 969 blockstates[0]._vars[j].set(j); 970 allVars.add(j); 971 j++; 972 } 973 for (int i = 0; i < sig->count(); i++) { 974 ciType* t = sig->type_at(i); 975 if (!t->is_primitive_type()) { 976 blockstates[0]._vars[j].set(j); 977 allVars.add(j); 978 } 979 j += t->size(); 980 } 981 blockstates[0]._initialized = true; 982 assert(j == _arg_size, "just checking"); 983 984 ArgumentMap unknown_map; 985 unknown_map.add_unknown(); 986 987 worklist.push(_methodBlocks->block_containing(0)); 988 while(worklist.length() > 0) { 989 ciBlock *blk = worklist.pop(); 990 StateInfo *blkState = blockstates+blk->index(); 991 if (blk->is_handler() || blk->is_ret_target()) { 992 // for an exception handler or a target of a ret instruction, we assume the worst case, 993 // that any variable or stack slot could contain any argument 994 for (int i = 0; i < numLocals; i++) { 995 state._vars[i] = allVars; 996 } 997 if (blk->is_handler()) { 998 state._stack_height = 1; 999 } else { 1000 state._stack_height = blkState->_stack_height; 1001 } 1002 for (int i = 0; i < state._stack_height; i++) { 1003 state._stack[i] = allVars; 1004 } 1005 } else { 1006 for (int i = 0; i < numLocals; i++) { 1007 state._vars[i] = blkState->_vars[i]; 1008 } 1009 for (int i = 0; i < blkState->_stack_height; i++) { 1010 state._stack[i] = blkState->_stack[i]; 1011 } 1012 state._stack_height = blkState->_stack_height; 1013 } 1014 iterate_one_block(blk, state, successors); 1015 // if this block has any exception handlers, push them 1016 // onto successor list 1017 if (blk->has_handler()) { 1018 DEBUG_ONLY(int handler_count = 0;) 1019 int blk_start = blk->start_bci(); 1020 int blk_end = blk->limit_bci(); 1021 for (int i = 0; i < numblocks; i++) { 1022 ciBlock *b = _methodBlocks->block(i); 1023 if (b->is_handler()) { 1024 int ex_start = b->ex_start_bci(); 1025 int ex_end = b->ex_limit_bci(); 1026 if ((ex_start >= blk_start && ex_start < blk_end) || 1027 (ex_end > blk_start && ex_end <= blk_end)) { 1028 successors.push(b); 1029 } 1030 DEBUG_ONLY(handler_count++;) 1031 } 1032 } 1033 assert(handler_count > 0, "must find at least one handler"); 1034 } 1035 // merge computed variable state with successors 1036 while(successors.length() > 0) { 1037 ciBlock *succ = successors.pop(); 1038 merge_block_states(blockstates, succ, &state); 1039 if (!succ->processed()) 1040 worklist.push(succ); 1041 } 1042 } 1043 } 1044 1045 bool BCEscapeAnalyzer::do_analysis() { 1046 Arena* arena = CURRENT_ENV->arena(); 1047 // identify basic blocks 1048 _methodBlocks = _method->get_method_blocks(); 1049 1050 iterate_blocks(arena); 1051 // TEMPORARY 1052 return true; 1053 } 1054 1055 vmIntrinsics::ID BCEscapeAnalyzer::known_intrinsic() { 1056 vmIntrinsics::ID iid = method()->intrinsic_id(); 1057 1058 if (iid == vmIntrinsics::_getClass || 1059 iid == vmIntrinsics::_hashCode) 1060 return iid; 1061 else 1062 return vmIntrinsics::_none; 1063 } 1064 1065 bool BCEscapeAnalyzer::compute_escape_for_intrinsic(vmIntrinsics::ID iid) { 1066 ArgumentMap empty; 1067 empty.clear(); 1068 switch (iid) { 1069 case vmIntrinsics::_getClass: 1070 _return_local = false; 1071 break; 1072 case vmIntrinsics::_hashCode: 1073 // initialized state is correct 1074 break; 1075 default: 1076 assert(false, "unexpected intrinsic"); 1077 } 1078 return true; 1079 } 1080 1081 void BCEscapeAnalyzer::initialize() { 1082 int i; 1083 1084 // clear escape information (method may have been deoptimized) 1085 methodData()->clear_escape_info(); 1086 1087 // initialize escape state of object parameters 1088 ciSignature* sig = method()->signature(); 1089 int j = 0; 1090 if (!method()->is_static()) { 1091 _arg_local.set_bit(0); 1092 _arg_stack.set_bit(0); 1093 j++; 1094 } 1095 for (i = 0; i < sig->count(); i++) { 1096 ciType* t = sig->type_at(i); 1097 if (!t->is_primitive_type()) { 1098 _arg_local.set_bit(j); 1099 _arg_stack.set_bit(j); 1100 } 1101 j += t->size(); 1102 } 1103 assert(j == _arg_size, "just checking"); 1104 1105 // start with optimistic assumption 1106 ciType *rt = _method->return_type(); 1107 if (rt->is_primitive_type()) { 1108 _return_local = false; 1109 _return_allocated = false; 1110 } else { 1111 _return_local = true; 1112 _return_allocated = true; 1113 } 1114 _allocated_escapes = false; 1115 } 1116 1117 void BCEscapeAnalyzer::clear_escape_info() { 1118 ciSignature* sig = method()->signature(); 1119 int arg_count = sig->count(); 1120 ArgumentMap var; 1121 for (int i = 0; i < arg_count; i++) { 1122 var.clear(); 1123 var.set(i); 1124 set_global_escape(var); 1125 } 1126 _arg_local.clear(); 1127 _arg_stack.clear(); 1128 _arg_returned.clear(); 1129 _return_local = false; 1130 _return_allocated = false; 1131 _allocated_escapes = true; 1132 } 1133 1134 1135 void BCEscapeAnalyzer::compute_escape_info() { 1136 int i; 1137 assert(!methodData()->has_escape_info(), "do not overwrite escape info"); 1138 1139 vmIntrinsics::ID iid = known_intrinsic(); 1140 1141 // check if method can be analyzed 1142 if (iid == vmIntrinsics::_none && (method()->is_abstract() || method()->is_native() || !method()->holder()->is_initialized() 1143 || _level > MaxBCEAEstimateLevel 1144 || method()->code_size() > MaxBCEAEstimateSize)) { 1145 if (BCEATraceLevel >= 1) { 1146 tty->print("Skipping method because: "); 1147 if (method()->is_abstract()) 1148 tty->print_cr("method is abstract."); 1149 else if (method()->is_native()) 1150 tty->print_cr("method is native."); 1151 else if (!method()->holder()->is_initialized()) 1152 tty->print_cr("class of method is not initialized."); 1153 else if (_level > MaxBCEAEstimateLevel) 1154 tty->print_cr("level (%d) exceeds MaxBCEAEstimateLevel (%d).", 1155 _level, MaxBCEAEstimateLevel); 1156 else if (method()->code_size() > MaxBCEAEstimateSize) 1157 tty->print_cr("code size (%d) exceeds MaxBCEAEstimateSize.", 1158 method()->code_size(), MaxBCEAEstimateSize); 1159 else 1160 ShouldNotReachHere(); 1161 } 1162 clear_escape_info(); 1163 1164 return; 1165 } 1166 1167 if (BCEATraceLevel >= 1) { 1168 tty->print("[EA] estimating escape information for"); 1169 if (iid != vmIntrinsics::_none) 1170 tty->print(" intrinsic"); 1171 method()->print_short_name(); 1172 tty->print_cr(" (%d bytes)", method()->code_size()); 1173 } 1174 1175 bool success; 1176 1177 initialize(); 1178 1179 // do not scan method if it has no object parameters 1180 if (_arg_local.is_empty()) { 1181 methodData()->set_eflag(methodDataOopDesc::estimated); 1182 return; 1183 } 1184 1185 if (iid != vmIntrinsics::_none) 1186 success = compute_escape_for_intrinsic(iid); 1187 else { 1188 success = do_analysis(); 1189 } 1190 1191 // dump result of bytecode analysis 1192 #ifndef PRODUCT 1193 if (BCEATraceLevel >= 3) { 1194 tty->print("[EA] estimated escape information for"); 1195 if (iid != vmIntrinsics::_none) 1196 tty->print(" intrinsic"); 1197 method()->print_short_name(); 1198 tty->print_cr(has_dependencies() ? " (not stored)" : ""); 1199 tty->print(" non-escaping args: "); 1200 _arg_local.print_on(tty); 1201 tty->print(" stack-allocatable args: "); 1202 _arg_stack.print_on(tty); 1203 if (_return_local) { 1204 tty->print(" returned args: "); 1205 _arg_returned.print_on(tty); 1206 } else if (is_return_allocated()) { 1207 tty->print_cr(" allocated return values"); 1208 } else { 1209 tty->print_cr(" non-local return values"); 1210 } 1211 tty->cr(); 1212 tty->print(" flags: "); 1213 if (_return_allocated) 1214 tty->print(" return_allocated"); 1215 tty->cr(); 1216 } 1217 1218 #endif 1219 // don't store interprocedural escape information if it introduces dependencies 1220 // or if method data is empty 1221 // 1222 if (!has_dependencies() && !methodData()->is_empty()) { 1223 for (i = 0; i < _arg_size; i++) { 1224 if (_arg_local.at(i)) { 1225 assert(_arg_stack.at(i), "inconsistent escape info"); 1226 methodData()->set_arg_local(i); 1227 methodData()->set_arg_stack(i); 1228 } else if (_arg_stack.at(i)) { 1229 methodData()->set_arg_stack(i); 1230 } 1231 if (_arg_returned.at(i)) { 1232 methodData()->set_arg_returned(i); 1233 } 1234 } 1235 if (_return_local) { 1236 methodData()->set_eflag(methodDataOopDesc::return_local); 1237 } 1238 methodData()->set_eflag(methodDataOopDesc::estimated); 1239 } 1240 } 1241 1242 void BCEscapeAnalyzer::read_escape_info() { 1243 assert(methodData()->has_escape_info(), "no escape info available"); 1244 1245 // read escape information from method descriptor 1246 for (int i = 0; i < _arg_size; i++) { 1247 _arg_local.at_put(i, methodData()->is_arg_local(i)); 1248 _arg_stack.at_put(i, methodData()->is_arg_stack(i)); 1249 _arg_returned.at_put(i, methodData()->is_arg_returned(i)); 1250 } 1251 _return_local = methodData()->eflag_set(methodDataOopDesc::return_local); 1252 1253 // dump result of loaded escape information 1254 #ifndef PRODUCT 1255 if (BCEATraceLevel >= 4) { 1256 tty->print(" non-escaping args: "); 1257 _arg_local.print_on(tty); 1258 tty->print(" stack-allocatable args: "); 1259 _arg_stack.print_on(tty); 1260 if (_return_local) { 1261 tty->print(" returned args: "); 1262 _arg_returned.print_on(tty); 1263 } else { 1264 tty->print_cr(" non-local return values"); 1265 } 1266 tty->print(" modified args: "); 1267 tty->cr(); 1268 } 1269 #endif 1270 1271 } 1272 1273 1274 BCEscapeAnalyzer::BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent) 1275 : _conservative(method == NULL || !EstimateArgEscape) 1276 , _method(method) 1277 , _methodData(method ? method->method_data() : NULL) 1278 , _arg_size(method ? method->arg_size() : 0) 1279 , _stack() 1280 , _arg_local(_arg_size) 1281 , _arg_stack(_arg_size) 1282 , _arg_returned(_arg_size) 1283 , _dirty(_arg_size) 1284 , _return_local(false) 1285 , _return_allocated(false) 1286 , _allocated_escapes(false) 1287 , _dependencies() 1288 , _parent(parent) 1289 , _level(parent == NULL ? 0 : parent->level() + 1) { 1290 if (!_conservative) { 1291 _arg_local.clear(); 1292 _arg_stack.clear(); 1293 _arg_returned.clear(); 1294 _dirty.clear(); 1295 Arena* arena = CURRENT_ENV->arena(); 1296 1297 if (methodData() == NULL) 1298 return; 1299 bool printit = _method->should_print_assembly(); 1300 if (methodData()->has_escape_info()) { 1301 TRACE_BCEA(2, tty->print_cr("[EA] Reading previous results for %s.%s", 1302 method->holder()->name()->as_utf8(), 1303 method->name()->as_utf8())); 1304 read_escape_info(); 1305 } else { 1306 TRACE_BCEA(2, tty->print_cr("[EA] computing results for %s.%s", 1307 method->holder()->name()->as_utf8(), 1308 method->name()->as_utf8())); 1309 1310 compute_escape_info(); 1311 methodData()->update_escape_info(); 1312 } 1313 } 1314 } 1315 1316 void BCEscapeAnalyzer::copy_dependencies(Dependencies *deps) { 1317 if(!has_dependencies()) 1318 return; 1319 for (int i = 0; i < _dependencies.length(); i+=2) { 1320 ciKlass *k = _dependencies[i]->as_klass(); 1321 ciMethod *m = _dependencies[i+1]->as_method(); 1322 deps->assert_unique_concrete_method(k, m); 1323 } 1324 } 1325