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