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