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