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