src/share/vm/ci/bcEscapeAnalyzer.cpp

Wed, 12 Oct 2011 21:00:13 -0700

author
twisti
date
Wed, 12 Oct 2011 21:00:13 -0700
changeset 3197
5eb9169b1a14
parent 2812
548597e74aa4
child 3318
cc81b9c09bbb
permissions
-rw-r--r--

7092712: JSR 292: unloaded invokedynamic call sites can lead to a crash with signature types not on BCP
Reviewed-by: jrose, never

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

mercurial