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