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