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