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         set_modified(arr, OFFSET_ANY, type2size[T_OBJECT]*HeapWordSize);
 558         break;
 559       }
 560       case Bytecodes::_pop:
 561         state.raw_pop();
 562         break;
 563       case Bytecodes::_pop2:
 564         state.raw_pop();
 565         state.raw_pop();
 566         break;
 567       case Bytecodes::_dup:
 568         { ArgumentMap w1 = state.raw_pop();
 569           state.raw_push(w1);
 570           state.raw_push(w1);
 571         }
 572         break;
 573       case Bytecodes::_dup_x1:
 574         { ArgumentMap w1 = state.raw_pop();
 575           ArgumentMap w2 = state.raw_pop();
 576           state.raw_push(w1);
 577           state.raw_push(w2);
 578           state.raw_push(w1);
 579         }
 580         break;
 581       case Bytecodes::_dup_x2:
 582         { ArgumentMap w1 = state.raw_pop();
 583           ArgumentMap w2 = state.raw_pop();
 584           ArgumentMap w3 = state.raw_pop();
 585           state.raw_push(w1);
 586           state.raw_push(w3);
 587           state.raw_push(w2);
 588           state.raw_push(w1);
 589         }
 590         break;
 591       case Bytecodes::_dup2:
 592         { ArgumentMap w1 = state.raw_pop();
 593           ArgumentMap w2 = state.raw_pop();
 594           state.raw_push(w2);
 595           state.raw_push(w1);
 596           state.raw_push(w2);
 597           state.raw_push(w1);
 598         }
 599         break;
 600       case Bytecodes::_dup2_x1:
 601         { ArgumentMap w1 = state.raw_pop();
 602           ArgumentMap w2 = state.raw_pop();
 603           ArgumentMap w3 = state.raw_pop();
 604           state.raw_push(w2);
 605           state.raw_push(w1);
 606           state.raw_push(w3);
 607           state.raw_push(w2);
 608           state.raw_push(w1);
 609         }
 610         break;
 611       case Bytecodes::_dup2_x2:
 612         { ArgumentMap w1 = state.raw_pop();
 613           ArgumentMap w2 = state.raw_pop();
 614           ArgumentMap w3 = state.raw_pop();
 615           ArgumentMap w4 = state.raw_pop();
 616           state.raw_push(w2);
 617           state.raw_push(w1);
 618           state.raw_push(w4);
 619           state.raw_push(w3);
 620           state.raw_push(w2);
 621           state.raw_push(w1);
 622         }
 623         break;
 624       case Bytecodes::_swap:
 625         { ArgumentMap w1 = state.raw_pop();
 626           ArgumentMap w2 = state.raw_pop();
 627           state.raw_push(w1);
 628           state.raw_push(w2);
 629         }
 630         break;
 631       case Bytecodes::_iadd:
 632       case Bytecodes::_fadd:
 633       case Bytecodes::_isub:
 634       case Bytecodes::_fsub:
 635       case Bytecodes::_imul:
 636       case Bytecodes::_fmul:
 637       case Bytecodes::_idiv:
 638       case Bytecodes::_fdiv:
 639       case Bytecodes::_irem:
 640       case Bytecodes::_frem:
 641       case Bytecodes::_iand:
 642       case Bytecodes::_ior:
 643       case Bytecodes::_ixor:
 644         state.spop();
 645         state.spop();
 646         state.spush();
 647         break;
 648       case Bytecodes::_ladd:
 649       case Bytecodes::_dadd:
 650       case Bytecodes::_lsub:
 651       case Bytecodes::_dsub:
 652       case Bytecodes::_lmul:
 653       case Bytecodes::_dmul:
 654       case Bytecodes::_ldiv:
 655       case Bytecodes::_ddiv:
 656       case Bytecodes::_lrem:
 657       case Bytecodes::_drem:
 658       case Bytecodes::_land:
 659       case Bytecodes::_lor:
 660       case Bytecodes::_lxor:
 661         state.lpop();
 662         state.lpop();
 663         state.lpush();
 664         break;
 665       case Bytecodes::_ishl:
 666       case Bytecodes::_ishr:
 667       case Bytecodes::_iushr:
 668         state.spop();
 669         state.spop();
 670         state.spush();
 671         break;
 672       case Bytecodes::_lshl:
 673       case Bytecodes::_lshr:
 674       case Bytecodes::_lushr:
 675         state.spop();
 676         state.lpop();
 677         state.lpush();
 678         break;
 679       case Bytecodes::_ineg:
 680       case Bytecodes::_fneg:
 681         state.spop();
 682         state.spush();
 683         break;
 684       case Bytecodes::_lneg:
 685       case Bytecodes::_dneg:
 686         state.lpop();
 687         state.lpush();
 688         break;
 689       case Bytecodes::_iinc:
 690         break;
 691       case Bytecodes::_i2l:
 692       case Bytecodes::_i2d:
 693       case Bytecodes::_f2l:
 694       case Bytecodes::_f2d:
 695         state.spop();
 696         state.lpush();
 697         break;
 698       case Bytecodes::_i2f:
 699       case Bytecodes::_f2i:
 700         state.spop();
 701         state.spush();
 702         break;
 703       case Bytecodes::_l2i:
 704       case Bytecodes::_l2f:
 705       case Bytecodes::_d2i:
 706       case Bytecodes::_d2f:
 707         state.lpop();
 708         state.spush();
 709         break;
 710       case Bytecodes::_l2d:
 711       case Bytecodes::_d2l:
 712         state.lpop();
 713         state.lpush();
 714         break;
 715       case Bytecodes::_i2b:
 716       case Bytecodes::_i2c:
 717       case Bytecodes::_i2s:
 718         state.spop();
 719         state.spush();
 720         break;
 721       case Bytecodes::_lcmp:
 722       case Bytecodes::_dcmpl:
 723       case Bytecodes::_dcmpg:
 724         state.lpop();
 725         state.lpop();
 726         state.spush();
 727         break;
 728       case Bytecodes::_fcmpl:
 729       case Bytecodes::_fcmpg:
 730         state.spop();
 731         state.spop();
 732         state.spush();
 733         break;
 734       case Bytecodes::_ifeq:
 735       case Bytecodes::_ifne:
 736       case Bytecodes::_iflt:
 737       case Bytecodes::_ifge:
 738       case Bytecodes::_ifgt:
 739       case Bytecodes::_ifle:
 740       {
 741         state.spop();
 742         int dest_bci = s.get_dest();
 743         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 744         assert(s.next_bci() == limit_bci, "branch must end block");
 745         successors.push(_methodBlocks->block_containing(dest_bci));
 746         break;
 747       }
 748       case Bytecodes::_if_icmpeq:
 749       case Bytecodes::_if_icmpne:
 750       case Bytecodes::_if_icmplt:
 751       case Bytecodes::_if_icmpge:
 752       case Bytecodes::_if_icmpgt:
 753       case Bytecodes::_if_icmple:
 754       {
 755         state.spop();
 756         state.spop();
 757         int dest_bci = s.get_dest();
 758         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 759         assert(s.next_bci() == limit_bci, "branch must end block");
 760         successors.push(_methodBlocks->block_containing(dest_bci));
 761         break;
 762       }
 763       case Bytecodes::_if_acmpeq:
 764       case Bytecodes::_if_acmpne:
 765       {
 766         set_method_escape(state.apop());
 767         set_method_escape(state.apop());
 768         int dest_bci = s.get_dest();
 769         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 770         assert(s.next_bci() == limit_bci, "branch must end block");
 771         successors.push(_methodBlocks->block_containing(dest_bci));
 772         break;
 773       }
 774       case Bytecodes::_goto:
 775       {
 776         int dest_bci = s.get_dest();
 777         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 778         assert(s.next_bci() == limit_bci, "branch must end block");
 779         successors.push(_methodBlocks->block_containing(dest_bci));
 780         fall_through = false;
 781         break;
 782       }
 783       case Bytecodes::_jsr:
 784       {
 785         int dest_bci = s.get_dest();
 786         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 787         assert(s.next_bci() == limit_bci, "branch must end block");
 788         state.apush(empty_map);
 789         successors.push(_methodBlocks->block_containing(dest_bci));
 790         fall_through = false;
 791         break;
 792       }
 793       case Bytecodes::_ret:
 794         // we don't track  the destination of a "ret" instruction
 795         assert(s.next_bci() == limit_bci, "branch must end block");
 796         fall_through = false;
 797         break;
 798       case Bytecodes::_return:
 799         assert(s.next_bci() == limit_bci, "return must end block");
 800         fall_through = false;
 801         break;
 802       case Bytecodes::_tableswitch:
 803         {
 804           state.spop();
 805           Bytecode_tableswitch sw(&s);
 806           int len = sw.length();
 807           int dest_bci;
 808           for (int i = 0; i < len; i++) {
 809             dest_bci = s.cur_bci() + sw.dest_offset_at(i);
 810             assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 811             successors.push(_methodBlocks->block_containing(dest_bci));
 812           }
 813           dest_bci = s.cur_bci() + sw.default_offset();
 814           assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 815           successors.push(_methodBlocks->block_containing(dest_bci));
 816           assert(s.next_bci() == limit_bci, "branch must end block");
 817           fall_through = false;
 818           break;
 819         }
 820       case Bytecodes::_lookupswitch:
 821         {
 822           state.spop();
 823           Bytecode_lookupswitch sw(&s);
 824           int len = sw.number_of_pairs();
 825           int dest_bci;
 826           for (int i = 0; i < len; i++) {
 827             dest_bci = s.cur_bci() + sw.pair_at(i).offset();
 828             assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 829             successors.push(_methodBlocks->block_containing(dest_bci));
 830           }
 831           dest_bci = s.cur_bci() + sw.default_offset();
 832           assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 833           successors.push(_methodBlocks->block_containing(dest_bci));
 834           fall_through = false;
 835           break;
 836         }
 837       case Bytecodes::_ireturn:
 838       case Bytecodes::_freturn:
 839         state.spop();
 840         fall_through = false;
 841         break;
 842       case Bytecodes::_lreturn:
 843       case Bytecodes::_dreturn:
 844         state.lpop();
 845         fall_through = false;
 846         break;
 847       case Bytecodes::_areturn:
 848         set_returned(state.apop());
 849         fall_through = false;
 850         break;
 851       case Bytecodes::_getstatic:
 852       case Bytecodes::_getfield:
 853         { bool ignored_will_link;
 854           ciField* field = s.get_field(ignored_will_link);
 855           BasicType field_type = field->type()->basic_type();
 856           if (s.cur_bc() != Bytecodes::_getstatic) {
 857             set_method_escape(state.apop());
 858           }
 859           if (is_reference_type(field_type)) {
 860             state.apush(unknown_obj);
 861           } else if (type2size[field_type] == 1) {
 862             state.spush();
 863           } else {
 864             state.lpush();
 865           }
 866         }
 867         break;
 868       case Bytecodes::_putstatic:
 869       case Bytecodes::_putfield:
 870         { bool will_link;
 871           ciField* field = s.get_field(will_link);
 872           BasicType field_type = field->type()->basic_type();
 873           if (is_reference_type(field_type)) {
 874             set_global_escape(state.apop());
 875           } else if (type2size[field_type] == 1) {
 876             state.spop();
 877           } else {
 878             state.lpop();
 879           }
 880           if (s.cur_bc() != Bytecodes::_putstatic) {
 881             ArgumentMap p = state.apop();
 882             set_method_escape(p);
 883             set_modified(p, will_link ? field->offset_in_bytes() : OFFSET_ANY, type2size[field_type]*HeapWordSize);
 884           }
 885         }
 886         break;
 887       case Bytecodes::_invokevirtual:
 888       case Bytecodes::_invokespecial:
 889       case Bytecodes::_invokestatic:
 890       case Bytecodes::_invokedynamic:
 891       case Bytecodes::_invokeinterface:
 892         { bool ignored_will_link;
 893           ciSignature* declared_signature = nullptr;
 894           ciMethod* target = s.get_method(ignored_will_link, &declared_signature);
 895           ciKlass*  holder = s.get_declared_method_holder();
 896           assert(declared_signature != nullptr, "cannot be null");
 897           // If the current bytecode has an attached appendix argument,
 898           // push an unknown object to represent that argument. (Analysis
 899           // of dynamic call sites, especially invokehandle calls, needs
 900           // the appendix argument on the stack, in addition to "regular" arguments
 901           // pushed onto the stack by bytecode instructions preceding the call.)
 902           //
 903           // The escape analyzer does _not_ use the ciBytecodeStream::has_appendix(s)
 904           // method to determine whether the current bytecode has an appendix argument.
 905           // The has_appendix() method obtains the appendix from the
 906           // ConstantPoolCacheEntry::_f1 field, which can happen concurrently with
 907           // resolution of dynamic call sites. Callees in the
 908           // ciBytecodeStream::get_method() call above also access the _f1 field;
 909           // interleaving the get_method() and has_appendix() calls in the current
 910           // method with call site resolution can lead to an inconsistent view of
 911           // the current method's argument count. In particular, some interleaving(s)
 912           // can cause the method's argument count to not include the appendix, which
 913           // then leads to stack over-/underflow in the escape analyzer.
 914           //
 915           // Instead of pushing the argument if has_appendix() is true, the escape analyzer
 916           // pushes an appendix for all call sites targeted by invokedynamic and invokehandle
 917           // instructions, except if the call site is the _invokeBasic intrinsic
 918           // (that intrinsic is always targeted by an invokehandle instruction but does
 919           // not have an appendix argument).
 920           if (target->is_loaded() &&
 921               Bytecodes::has_optional_appendix(s.cur_bc_raw()) &&
 922               target->intrinsic_id() != vmIntrinsics::_invokeBasic) {
 923             state.apush(unknown_obj);
 924           }
 925           // Pass in raw bytecode because we need to see invokehandle instructions.
 926           invoke(state, s.cur_bc_raw(), target, holder);
 927           // We are using the return type of the declared signature here because
 928           // it might be a more concrete type than the one from the target (for
 929           // e.g. invokedynamic and invokehandle).
 930           ciType* return_type = declared_signature->return_type();
 931           if (!return_type->is_primitive_type()) {
 932             state.apush(unknown_obj);
 933           } else if (return_type->is_one_word()) {
 934             state.spush();
 935           } else if (return_type->is_two_word()) {
 936             state.lpush();
 937           }
 938         }
 939         break;
 940       case Bytecodes::_new:
 941         state.apush(allocated_obj);
 942         break;
 943       case Bytecodes::_newarray:
 944       case Bytecodes::_anewarray:
 945         state.spop();
 946         state.apush(allocated_obj);
 947         break;
 948       case Bytecodes::_multianewarray:
 949         { int i = s.cur_bcp()[3];
 950           while (i-- > 0) state.spop();
 951           state.apush(allocated_obj);
 952         }
 953         break;
 954       case Bytecodes::_arraylength:
 955         set_method_escape(state.apop());
 956         state.spush();
 957         break;
 958       case Bytecodes::_athrow:
 959         set_global_escape(state.apop());
 960         fall_through = false;
 961         break;
 962       case Bytecodes::_checkcast:
 963         { ArgumentMap obj = state.apop();
 964           set_method_escape(obj);
 965           state.apush(obj);
 966         }
 967         break;
 968       case Bytecodes::_instanceof:
 969         set_method_escape(state.apop());
 970         state.spush();
 971         break;
 972       case Bytecodes::_monitorenter:
 973       case Bytecodes::_monitorexit:
 974         state.apop();
 975         break;
 976       case Bytecodes::_wide:
 977         ShouldNotReachHere();
 978         break;
 979       case Bytecodes::_ifnull:
 980       case Bytecodes::_ifnonnull:
 981       {
 982         set_method_escape(state.apop());
 983         int dest_bci = s.get_dest();
 984         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 985         assert(s.next_bci() == limit_bci, "branch must end block");
 986         successors.push(_methodBlocks->block_containing(dest_bci));
 987         break;
 988       }
 989       case Bytecodes::_goto_w:
 990       {
 991         int dest_bci = s.get_far_dest();
 992         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
 993         assert(s.next_bci() == limit_bci, "branch must end block");
 994         successors.push(_methodBlocks->block_containing(dest_bci));
 995         fall_through = false;
 996         break;
 997       }
 998       case Bytecodes::_jsr_w:
 999       {
1000         int dest_bci = s.get_far_dest();
1001         assert(_methodBlocks->is_block_start(dest_bci), "branch destination must start a block");
1002         assert(s.next_bci() == limit_bci, "branch must end block");
1003         state.apush(empty_map);
1004         successors.push(_methodBlocks->block_containing(dest_bci));
1005         fall_through = false;
1006         break;
1007       }
1008       case Bytecodes::_breakpoint:
1009         break;
1010       default:
1011         ShouldNotReachHere();
1012         break;
1013     }
1014 
1015   }
1016   if (fall_through) {
1017     int fall_through_bci = s.cur_bci();
1018     if (fall_through_bci < _method->code_size()) {
1019       assert(_methodBlocks->is_block_start(fall_through_bci), "must fall through to block start.");
1020       successors.push(_methodBlocks->block_containing(fall_through_bci));
1021     }
1022   }
1023 }
1024 
1025 void BCEscapeAnalyzer::merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state) {
1026   StateInfo *d_state = blockstates + dest->index();
1027   int nlocals = _method->max_locals();
1028 
1029   // exceptions may cause transfer of control to handlers in the middle of a
1030   // block, so we don't merge the incoming state of exception handlers
1031   if (dest->is_handler())
1032     return;
1033   if (!d_state->_initialized ) {
1034     // destination not initialized, just copy
1035     for (int i = 0; i < nlocals; i++) {
1036       d_state->_vars[i] = s_state->_vars[i];
1037     }
1038     for (int i = 0; i < s_state->_stack_height; i++) {
1039       d_state->_stack[i] = s_state->_stack[i];
1040     }
1041     d_state->_stack_height = s_state->_stack_height;
1042     d_state->_max_stack = s_state->_max_stack;
1043     d_state->_initialized = true;
1044   } else if (!dest->processed()) {
1045     // we have not yet walked the bytecodes of dest, we can merge
1046     // the states
1047     assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
1048     for (int i = 0; i < nlocals; i++) {
1049       d_state->_vars[i].set_union(s_state->_vars[i]);
1050     }
1051     for (int i = 0; i < s_state->_stack_height; i++) {
1052       d_state->_stack[i].set_union(s_state->_stack[i]);
1053     }
1054   } else {
1055     // the bytecodes of dest have already been processed, mark any
1056     // arguments in the source state which are not in the dest state
1057     // as global escape.
1058     // Future refinement:  we only need to mark these variable to the
1059     // maximum escape of any variables in dest state
1060     assert(d_state->_stack_height == s_state->_stack_height, "computed stack heights must match");
1061     ArgumentMap extra_vars;
1062     for (int i = 0; i < nlocals; i++) {
1063       ArgumentMap t;
1064       t = s_state->_vars[i];
1065       t.set_difference(d_state->_vars[i]);
1066       extra_vars.set_union(t);
1067     }
1068     for (int i = 0; i < s_state->_stack_height; i++) {
1069       ArgumentMap t;
1070       //extra_vars |= !d_state->_vars[i] & s_state->_vars[i];
1071       t.clear();
1072       t = s_state->_stack[i];
1073       t.set_difference(d_state->_stack[i]);
1074       extra_vars.set_union(t);
1075     }
1076     set_global_escape(extra_vars, true);
1077   }
1078 }
1079 
1080 void BCEscapeAnalyzer::iterate_blocks(Arena *arena) {
1081   int numblocks = _methodBlocks->num_blocks();
1082   int stkSize   = _method->max_stack();
1083   int numLocals = _method->max_locals();
1084   StateInfo state;
1085 
1086   int datacount = (numblocks + 1) * (stkSize + numLocals);
1087   int datasize = datacount * sizeof(ArgumentMap);
1088   StateInfo *blockstates = (StateInfo *) arena->Amalloc(numblocks * sizeof(StateInfo));
1089   ArgumentMap *statedata  = (ArgumentMap *) arena->Amalloc(datasize);
1090   for (int i = 0; i < datacount; i++) ::new ((void*)&statedata[i]) ArgumentMap();
1091   ArgumentMap *dp = statedata;
1092   state._vars = dp;
1093   dp += numLocals;
1094   state._stack = dp;
1095   dp += stkSize;
1096   state._initialized = false;
1097   state._max_stack = stkSize;
1098   for (int i = 0; i < numblocks; i++) {
1099     blockstates[i]._vars = dp;
1100     dp += numLocals;
1101     blockstates[i]._stack = dp;
1102     dp += stkSize;
1103     blockstates[i]._initialized = false;
1104     blockstates[i]._stack_height = 0;
1105     blockstates[i]._max_stack  = stkSize;
1106   }
1107   GrowableArray<ciBlock *> worklist(arena, numblocks / 4, 0, nullptr);
1108   GrowableArray<ciBlock *> successors(arena, 4, 0, nullptr);
1109 
1110   _methodBlocks->clear_processed();
1111 
1112   // initialize block 0 state from method signature
1113   ArgumentMap allVars;   // all oop arguments to method
1114   ciSignature* sig = method()->signature();
1115   int j = 0;
1116   ciBlock* first_blk = _methodBlocks->block_containing(0);
1117   int fb_i = first_blk->index();
1118   if (!method()->is_static()) {
1119     // record information for "this"
1120     blockstates[fb_i]._vars[j].set(j);
1121     allVars.add(j);
1122     j++;
1123   }
1124   for (int i = 0; i < sig->count(); i++) {
1125     ciType* t = sig->type_at(i);
1126     if (!t->is_primitive_type()) {
1127       blockstates[fb_i]._vars[j].set(j);
1128       allVars.add(j);
1129     }
1130     j += t->size();
1131   }
1132   blockstates[fb_i]._initialized = true;
1133   assert(j == _arg_size, "just checking");
1134 
1135   ArgumentMap unknown_map;
1136   unknown_map.add_unknown();
1137 
1138   worklist.push(first_blk);
1139   while(worklist.length() > 0) {
1140     ciBlock *blk = worklist.pop();
1141     StateInfo *blkState = blockstates + blk->index();
1142     if (blk->is_handler() || blk->is_ret_target()) {
1143       // for an exception handler or a target of a ret instruction, we assume the worst case,
1144       // that any variable could contain any argument
1145       for (int i = 0; i < numLocals; i++) {
1146         state._vars[i] = allVars;
1147       }
1148       if (blk->is_handler()) {
1149         state._stack_height = 1;
1150       } else {
1151         state._stack_height = blkState->_stack_height;
1152       }
1153       for (int i = 0; i < state._stack_height; i++) {
1154 // ??? should this be unknown_map ???
1155         state._stack[i] = allVars;
1156       }
1157     } else {
1158       for (int i = 0; i < numLocals; i++) {
1159         state._vars[i] = blkState->_vars[i];
1160       }
1161       for (int i = 0; i < blkState->_stack_height; i++) {
1162         state._stack[i] = blkState->_stack[i];
1163       }
1164       state._stack_height = blkState->_stack_height;
1165     }
1166     iterate_one_block(blk, state, successors);
1167     // if this block has any exception handlers, push them
1168     // onto successor list
1169     if (blk->has_handler()) {
1170       DEBUG_ONLY(int handler_count = 0;)
1171       int blk_start = blk->start_bci();
1172       int blk_end = blk->limit_bci();
1173       for (int i = 0; i < numblocks; i++) {
1174         ciBlock *b = _methodBlocks->block(i);
1175         if (b->is_handler()) {
1176           int ex_start = b->ex_start_bci();
1177           int ex_end = b->ex_limit_bci();
1178           if ((ex_start >= blk_start && ex_start < blk_end) ||
1179               (ex_end > blk_start && ex_end <= blk_end)) {
1180             successors.push(b);
1181           }
1182           DEBUG_ONLY(handler_count++;)
1183         }
1184       }
1185       assert(handler_count > 0, "must find at least one handler");
1186     }
1187     // merge computed variable state with successors
1188     while(successors.length() > 0) {
1189       ciBlock *succ = successors.pop();
1190       merge_block_states(blockstates, succ, &state);
1191       if (!succ->processed())
1192         worklist.push(succ);
1193     }
1194   }
1195 }
1196 
1197 void BCEscapeAnalyzer::do_analysis() {
1198   Arena* arena = CURRENT_ENV->arena();
1199   // identify basic blocks
1200   _methodBlocks = _method->get_method_blocks();
1201 
1202   iterate_blocks(arena);
1203 }
1204 
1205 vmIntrinsicID BCEscapeAnalyzer::known_intrinsic() {
1206   vmIntrinsicID iid = method()->intrinsic_id();
1207   if (iid == vmIntrinsics::_getClass ||
1208       iid == vmIntrinsics::_hashCode) {
1209     return iid;
1210   } else {
1211     return vmIntrinsics::_none;
1212   }
1213 }
1214 
1215 void BCEscapeAnalyzer::compute_escape_for_intrinsic(vmIntrinsicID iid) {
1216   switch (iid) {
1217     case vmIntrinsics::_getClass:
1218       _return_local = false;
1219       _return_allocated = false;
1220       break;
1221     case vmIntrinsics::_hashCode:
1222       // initialized state is correct
1223       break;
1224   default:
1225     assert(false, "unexpected intrinsic");
1226   }
1227 }
1228 
1229 void BCEscapeAnalyzer::initialize() {
1230   int i;
1231 
1232   // clear escape information (method may have been deoptimized)
1233   methodData()->clear_escape_info();
1234 
1235   // initialize escape state of object parameters
1236   ciSignature* sig = method()->signature();
1237   int j = 0;
1238   if (!method()->is_static()) {
1239     _arg_local.set(0);
1240     _arg_stack.set(0);
1241     j++;
1242   }
1243   for (i = 0; i < sig->count(); i++) {
1244     ciType* t = sig->type_at(i);
1245     if (!t->is_primitive_type()) {
1246       _arg_local.set(j);
1247       _arg_stack.set(j);
1248     }
1249     j += t->size();
1250   }
1251   assert(j == _arg_size, "just checking");
1252 
1253   // start with optimistic assumption
1254   ciType *rt = _method->return_type();
1255   if (rt->is_primitive_type()) {
1256     _return_local = false;
1257     _return_allocated = false;
1258   } else {
1259     _return_local = true;
1260     _return_allocated = true;
1261   }
1262   _allocated_escapes = false;
1263   _unknown_modified = false;
1264 }
1265 
1266 void BCEscapeAnalyzer::clear_escape_info() {
1267   ciSignature* sig = method()->signature();
1268   int arg_count = sig->count();
1269   ArgumentMap var;
1270   if (!method()->is_static()) {
1271     arg_count++;  // allow for "this"
1272   }
1273   for (int i = 0; i < arg_count; i++) {
1274     set_arg_modified(i, OFFSET_ANY, 4);
1275     var.clear();
1276     var.set(i);
1277     set_modified(var, OFFSET_ANY, 4);
1278     set_global_escape(var);
1279   }
1280   _arg_local.clear();
1281   _arg_stack.clear();
1282   _arg_returned.clear();
1283   _return_local = false;
1284   _return_allocated = false;
1285   _allocated_escapes = true;
1286   _unknown_modified = true;
1287 }
1288 
1289 
1290 void BCEscapeAnalyzer::compute_escape_info() {
1291   int i;
1292   assert(!methodData()->has_escape_info(), "do not overwrite escape info");
1293 
1294   vmIntrinsicID iid = known_intrinsic();
1295 
1296   // check if method can be analyzed
1297   if (iid == vmIntrinsics::_none && (method()->is_abstract() || method()->is_native() || !method()->holder()->is_initialized()
1298       || _level > MaxBCEAEstimateLevel
1299       || method()->code_size() > MaxBCEAEstimateSize)) {
1300     if (BCEATraceLevel >= 1) {
1301       tty->print("Skipping method because: ");
1302       if (method()->is_abstract())
1303         tty->print_cr("method is abstract.");
1304       else if (method()->is_native())
1305         tty->print_cr("method is native.");
1306       else if (!method()->holder()->is_initialized())
1307         tty->print_cr("class of method is not initialized.");
1308       else if (_level > MaxBCEAEstimateLevel)
1309         tty->print_cr("level (%d) exceeds MaxBCEAEstimateLevel (%d).",
1310                       _level, (int) MaxBCEAEstimateLevel);
1311       else if (method()->code_size() > MaxBCEAEstimateSize)
1312         tty->print_cr("code size (%d) exceeds MaxBCEAEstimateSize (%d).",
1313                       method()->code_size(), (int) MaxBCEAEstimateSize);
1314       else
1315         ShouldNotReachHere();
1316     }
1317     clear_escape_info();
1318 
1319     return;
1320   }
1321 
1322   if (BCEATraceLevel >= 1) {
1323     tty->print("[EA] estimating escape information for");
1324     if (iid != vmIntrinsics::_none)
1325       tty->print(" intrinsic");
1326     method()->print_short_name();
1327     tty->print_cr(" (%d bytes)", method()->code_size());
1328   }
1329 
1330   initialize();
1331 
1332   // Do not scan method if it has no object parameters and
1333   // does not returns an object (_return_allocated is set in initialize()).
1334   if (_arg_local.is_empty() && !_return_allocated) {
1335     // Clear all info since method's bytecode was not analysed and
1336     // set pessimistic escape information.
1337     clear_escape_info();
1338     methodData()->set_eflag(MethodData::allocated_escapes);
1339     methodData()->set_eflag(MethodData::unknown_modified);
1340     methodData()->set_eflag(MethodData::estimated);
1341     return;
1342   }
1343 
1344   if (iid != vmIntrinsics::_none)
1345     compute_escape_for_intrinsic(iid);
1346   else {
1347     do_analysis();
1348   }
1349 
1350   // don't store interprocedural escape information if it introduces
1351   // dependencies or if method data is empty
1352   //
1353   if (!has_dependencies() && !methodData()->is_empty()) {
1354     for (i = 0; i < _arg_size; i++) {
1355       if (_arg_local.test(i)) {
1356         assert(_arg_stack.test(i), "inconsistent escape info");
1357         methodData()->set_arg_local(i);
1358         methodData()->set_arg_stack(i);
1359       } else if (_arg_stack.test(i)) {
1360         methodData()->set_arg_stack(i);
1361       }
1362       if (_arg_returned.test(i)) {
1363         methodData()->set_arg_returned(i);
1364       }
1365       methodData()->set_arg_modified(i, _arg_modified[i]);
1366     }
1367     if (_return_local) {
1368       methodData()->set_eflag(MethodData::return_local);
1369     }
1370     if (_return_allocated) {
1371       methodData()->set_eflag(MethodData::return_allocated);
1372     }
1373     if (_allocated_escapes) {
1374       methodData()->set_eflag(MethodData::allocated_escapes);
1375     }
1376     if (_unknown_modified) {
1377       methodData()->set_eflag(MethodData::unknown_modified);
1378     }
1379     methodData()->set_eflag(MethodData::estimated);
1380   }
1381 }
1382 
1383 void BCEscapeAnalyzer::read_escape_info() {
1384   assert(methodData()->has_escape_info(), "no escape info available");
1385 
1386   // read escape information from method descriptor
1387   for (int i = 0; i < _arg_size; i++) {
1388     if (methodData()->is_arg_local(i))
1389       _arg_local.set(i);
1390     if (methodData()->is_arg_stack(i))
1391       _arg_stack.set(i);
1392     if (methodData()->is_arg_returned(i))
1393       _arg_returned.set(i);
1394     _arg_modified[i] = methodData()->arg_modified(i);
1395   }
1396   _return_local = methodData()->eflag_set(MethodData::return_local);
1397   _return_allocated = methodData()->eflag_set(MethodData::return_allocated);
1398   _allocated_escapes = methodData()->eflag_set(MethodData::allocated_escapes);
1399   _unknown_modified = methodData()->eflag_set(MethodData::unknown_modified);
1400 
1401 }
1402 
1403 #ifndef PRODUCT
1404 void BCEscapeAnalyzer::dump() {
1405   tty->print("[EA] estimated escape information for");
1406   method()->print_short_name();
1407   tty->print_cr(has_dependencies() ? " (not stored)" : "");
1408   tty->print("     non-escaping args:      ");
1409   _arg_local.print();
1410   tty->print("     stack-allocatable args: ");
1411   _arg_stack.print();
1412   if (_return_local) {
1413     tty->print("     returned args:          ");
1414     _arg_returned.print();
1415   } else if (is_return_allocated()) {
1416     tty->print_cr("     return allocated value");
1417   } else {
1418     tty->print_cr("     return non-local value");
1419   }
1420   tty->print("     modified args: ");
1421   for (int i = 0; i < _arg_size; i++) {
1422     if (_arg_modified[i] == 0)
1423       tty->print("    0");
1424     else
1425       tty->print("    0x%x", _arg_modified[i]);
1426   }
1427   tty->cr();
1428   tty->print("     flags: ");
1429   if (_return_allocated)
1430     tty->print(" return_allocated");
1431   if (_allocated_escapes)
1432     tty->print(" allocated_escapes");
1433   if (_unknown_modified)
1434     tty->print(" unknown_modified");
1435   tty->cr();
1436 }
1437 #endif
1438 
1439 BCEscapeAnalyzer::BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent)
1440     : _arena(CURRENT_ENV->arena())
1441     , _conservative(method == nullptr || !EstimateArgEscape)
1442     , _method(method)
1443     , _methodData(method ? method->method_data() : nullptr)
1444     , _arg_size(method ? method->arg_size() : 0)
1445     , _arg_local(_arena)
1446     , _arg_stack(_arena)
1447     , _arg_returned(_arena)
1448     , _return_local(false)
1449     , _return_allocated(false)
1450     , _allocated_escapes(false)
1451     , _unknown_modified(false)
1452     , _dependencies(_arena, 4, 0, nullptr)
1453     , _parent(parent)
1454     , _level(parent == nullptr ? 0 : parent->level() + 1) {
1455   if (!_conservative) {
1456     _arg_local.clear();
1457     _arg_stack.clear();
1458     _arg_returned.clear();
1459     Arena* arena = CURRENT_ENV->arena();
1460     _arg_modified = (uint *) arena->Amalloc(_arg_size * sizeof(uint));
1461     Copy::zero_to_bytes(_arg_modified, _arg_size * sizeof(uint));
1462 
1463     if (methodData() == nullptr)
1464       return;
1465     if (methodData()->has_escape_info()) {
1466       TRACE_BCEA(2, tty->print_cr("[EA] Reading previous results for %s.%s",
1467                                   method->holder()->name()->as_utf8(),
1468                                   method->name()->as_utf8()));
1469       read_escape_info();
1470     } else {
1471       TRACE_BCEA(2, tty->print_cr("[EA] computing results for %s.%s",
1472                                   method->holder()->name()->as_utf8(),
1473                                   method->name()->as_utf8()));
1474 
1475       compute_escape_info();
1476       methodData()->update_escape_info();
1477     }
1478 #ifndef PRODUCT
1479     if (BCEATraceLevel >= 3) {
1480       // dump escape information
1481       dump();
1482     }
1483 #endif
1484   }
1485 }
1486 
1487 void BCEscapeAnalyzer::copy_dependencies(Dependencies *deps) {
1488   if (ciEnv::current()->jvmti_can_hotswap_or_post_breakpoint()) {
1489     // Also record evol dependencies so redefinition of the
1490     // callee will trigger recompilation.
1491     deps->assert_evol_method(method());
1492   }
1493   for (int i = 0; i < _dependencies.length(); i+=4) {
1494     ciKlass*  recv_klass      = _dependencies.at(i+0)->as_klass();
1495     ciMethod* target          = _dependencies.at(i+1)->as_method();
1496     ciKlass*  resolved_klass  = _dependencies.at(i+2)->as_klass();
1497     ciMethod* resolved_method = _dependencies.at(i+3)->as_method();
1498     deps->assert_unique_concrete_method(recv_klass, target, resolved_klass, resolved_method);
1499   }
1500 }