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