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