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