ciMethod.cpp revision 196:d1605aabd0a1
11541Srgrimes/*
21541Srgrimes * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
201541Srgrimes * CA 95054 USA or visit www.sun.com if you need additional information or
211541Srgrimes * have any questions.
221541Srgrimes *
231541Srgrimes */
241541Srgrimes
251541Srgrimes#include "incls/_precompiled.incl"
261541Srgrimes#include "incls/_ciMethod.cpp.incl"
271541Srgrimes
281541Srgrimes// ciMethod
291541Srgrimes//
301541Srgrimes// This class represents a methodOop in the HotSpot virtual
311541Srgrimes// machine.
321541Srgrimes
331541Srgrimes
346151Sdg// ------------------------------------------------------------------
351541Srgrimes// ciMethod::ciMethod
361541Srgrimes//
371541Srgrimes// Loaded method.
382979SwollmanciMethod::ciMethod(methodHandle h_m) : ciObject(h_m) {
391541Srgrimes  assert(h_m() != NULL, "no null method");
401541Srgrimes
411541Srgrimes  // These fields are always filled in in loaded methods.
421541Srgrimes  _flags = ciFlags(h_m()->access_flags());
431541Srgrimes
441541Srgrimes  // Easy to compute, so fill them in now.
451541Srgrimes  _max_stack          = h_m()->max_stack();
461541Srgrimes  _max_locals         = h_m()->max_locals();
471541Srgrimes  _code_size          = h_m()->code_size();
481541Srgrimes  _intrinsic_id       = h_m()->intrinsic_id();
491541Srgrimes  _handler_count      = h_m()->exception_table()->length() / 4;
501541Srgrimes  _uses_monitors      = h_m()->access_flags().has_monitor_bytecodes();
511541Srgrimes  _balanced_monitors  = !_uses_monitors || h_m()->access_flags().is_monitor_matching();
521541Srgrimes  _is_compilable      = !h_m()->is_not_compilable();
531541Srgrimes  // Lazy fields, filled in on demand.  Require allocation.
541541Srgrimes  _code               = NULL;
551541Srgrimes  _exception_handlers = NULL;
561541Srgrimes  _liveness           = NULL;
571541Srgrimes  _bcea = NULL;
581541Srgrimes  _method_blocks = NULL;
591541Srgrimes#ifdef COMPILER2
601541Srgrimes  _flow               = NULL;
611541Srgrimes#endif // COMPILER2
621541Srgrimes
631541Srgrimes  if (JvmtiExport::can_hotswap_or_post_breakpoint() && _is_compilable) {
641541Srgrimes    // 6328518 check hotswap conditions under the right lock.
651541Srgrimes    MutexLocker locker(Compile_lock);
661541Srgrimes    if (Dependencies::check_evol_method(h_m()) != NULL) {
671541Srgrimes      _is_compilable = false;
681541Srgrimes    }
691541Srgrimes  } else {
701541Srgrimes    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
711541Srgrimes  }
721541Srgrimes
731541Srgrimes  if (instanceKlass::cast(h_m()->method_holder())->is_linked()) {
741541Srgrimes    _can_be_statically_bound = h_m()->can_be_statically_bound();
751541Srgrimes  } else {
761541Srgrimes    // Have to use a conservative value in this case.
771541Srgrimes    _can_be_statically_bound = false;
781541Srgrimes  }
791541Srgrimes
801541Srgrimes  // Adjust the definition of this condition to be more useful:
811541Srgrimes  // %%% take these conditions into account in vtable generation
821541Srgrimes  if (!_can_be_statically_bound && h_m()->is_private())
831541Srgrimes    _can_be_statically_bound = true;
841541Srgrimes  if (_can_be_statically_bound && h_m()->is_abstract())
851541Srgrimes    _can_be_statically_bound = false;
861541Srgrimes
871541Srgrimes  ciEnv *env = CURRENT_ENV;
881541Srgrimes  // generating _signature may allow GC and therefore move m.
891541Srgrimes  // These fields are always filled in.
901541Srgrimes  _name = env->get_object(h_m()->name())->as_symbol();
911541Srgrimes  _holder = env->get_object(h_m()->method_holder())->as_instance_klass();
921541Srgrimes  ciSymbol* sig_symbol = env->get_object(h_m()->signature())->as_symbol();
931541Srgrimes  _signature = new (env->arena()) ciSignature(_holder, sig_symbol);
941541Srgrimes  _method_data = NULL;
951541Srgrimes  // Take a snapshot of these values, so they will be commensurate with the MDO.
961541Srgrimes  if (ProfileInterpreter) {
971541Srgrimes    int invcnt = h_m()->interpreter_invocation_count();
981541Srgrimes    // if the value overflowed report it as max int
991541Srgrimes    _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
1001541Srgrimes    _interpreter_throwout_count   = h_m()->interpreter_throwout_count();
1011541Srgrimes  } else {
1021541Srgrimes    _interpreter_invocation_count = 0;
1031541Srgrimes    _interpreter_throwout_count = 0;
1041541Srgrimes  }
1051541Srgrimes  if (_interpreter_invocation_count == 0)
1061541Srgrimes    _interpreter_invocation_count = 1;
1071541Srgrimes}
1081541Srgrimes
1091541Srgrimes
1101541Srgrimes// ------------------------------------------------------------------
1111541Srgrimes// ciMethod::ciMethod
1122946Swollman//
1132946Swollman// Unloaded method.
1141541SrgrimesciMethod::ciMethod(ciInstanceKlass* holder,
1151541Srgrimes                   ciSymbol* name,
1161541Srgrimes                   ciSymbol* signature) : ciObject(ciMethodKlass::make()) {
1171541Srgrimes  // These fields are always filled in.
1181549Srgrimes  _name = name;
1191541Srgrimes  _holder = holder;
1201541Srgrimes  _signature = new (CURRENT_ENV->arena()) ciSignature(_holder, signature);
1211541Srgrimes  _intrinsic_id = vmIntrinsics::_none;
1221541Srgrimes  _liveness = NULL;
1231541Srgrimes  _can_be_statically_bound = false;
1241541Srgrimes  _bcea = NULL;
1251541Srgrimes  _method_blocks = NULL;
1261541Srgrimes  _method_data = NULL;
1271541Srgrimes#ifdef COMPILER2
1281541Srgrimes  _flow = NULL;
1291541Srgrimes#endif // COMPILER2
1301541Srgrimes}
1311541Srgrimes
1321541Srgrimes
1331541Srgrimes// ------------------------------------------------------------------
1341541Srgrimes// ciMethod::load_code
1351541Srgrimes//
1361549Srgrimes// Load the bytecodes and exception handler table for this method.
1371541Srgrimesvoid ciMethod::load_code() {
1381541Srgrimes  VM_ENTRY_MARK;
1391541Srgrimes  assert(is_loaded(), "only loaded methods have code");
1401541Srgrimes
1411541Srgrimes  methodOop me = get_methodOop();
1421541Srgrimes  Arena* arena = CURRENT_THREAD_ENV->arena();
1431541Srgrimes
1441541Srgrimes  // Load the bytecodes.
1451541Srgrimes  _code = (address)arena->Amalloc(code_size());
1461541Srgrimes  memcpy(_code, me->code_base(), code_size());
1471541Srgrimes
1481541Srgrimes  // Revert any breakpoint bytecodes in ci's copy
1491541Srgrimes  if (me->number_of_breakpoints() > 0) {
1501541Srgrimes    BreakpointInfo* bp = instanceKlass::cast(me->method_holder())->breakpoints();
1511541Srgrimes    for (; bp != NULL; bp = bp->next()) {
1521541Srgrimes      if (bp->match(me)) {
1531541Srgrimes        code_at_put(bp->bci(), bp->orig_bytecode());
1541541Srgrimes      }
1551541Srgrimes    }
1563311Sphk  }
1573311Sphk
1581541Srgrimes  // And load the exception table.
1591541Srgrimes  typeArrayOop exc_table = me->exception_table();
1601541Srgrimes
1611541Srgrimes  // Allocate one extra spot in our list of exceptions.  This
1621541Srgrimes  // last entry will be used to represent the possibility that
1633311Sphk  // an exception escapes the method.  See ciExceptionHandlerStream
1643311Sphk  // for details.
1651541Srgrimes  _exception_handlers =
1661541Srgrimes    (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
1671541Srgrimes                                         * (_handler_count + 1));
1681541Srgrimes  if (_handler_count > 0) {
1691541Srgrimes    for (int i=0; i<_handler_count; i++) {
1701541Srgrimes      int base = i*4;
1713311Sphk      _exception_handlers[i] = new (arena) ciExceptionHandler(
1723311Sphk                                holder(),
1731541Srgrimes            /* start    */      exc_table->int_at(base),
1741541Srgrimes            /* limit    */      exc_table->int_at(base+1),
1751541Srgrimes            /* goto pc  */      exc_table->int_at(base+2),
1761541Srgrimes            /* cp index */      exc_table->int_at(base+3));
1771541Srgrimes    }
1781541Srgrimes  }
1791541Srgrimes
1801541Srgrimes  // Put an entry at the end of our list to represent the possibility
1811541Srgrimes  // of exceptional exit.
1821541Srgrimes  _exception_handlers[_handler_count] =
1831541Srgrimes    new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
1841541Srgrimes
1851541Srgrimes  if (CIPrintMethodCodes) {
1861541Srgrimes    print_codes();
1871541Srgrimes  }
1881541Srgrimes}
1891541Srgrimes
1901541Srgrimes
1911541Srgrimes// ------------------------------------------------------------------
1921541Srgrimes// ciMethod::has_linenumber_table
1931541Srgrimes//
1941541Srgrimes// length unknown until decompression
1951541Srgrimesbool    ciMethod::has_linenumber_table() const {
1961541Srgrimes  check_is_loaded();
1971541Srgrimes  VM_ENTRY_MARK;
1981541Srgrimes  return get_methodOop()->has_linenumber_table();
1991541Srgrimes}
2001541Srgrimes
2011541Srgrimes
2021541Srgrimes// ------------------------------------------------------------------
2031541Srgrimes// ciMethod::compressed_linenumber_table
2041541Srgrimesu_char* ciMethod::compressed_linenumber_table() const {
2051541Srgrimes  check_is_loaded();
2061541Srgrimes  VM_ENTRY_MARK;
2071541Srgrimes  return get_methodOop()->compressed_linenumber_table();
2081541Srgrimes}
2091541Srgrimes
2101541Srgrimes
2111541Srgrimes// ------------------------------------------------------------------
2121541Srgrimes// ciMethod::line_number_from_bci
2131541Srgrimesint ciMethod::line_number_from_bci(int bci) const {
2141541Srgrimes  check_is_loaded();
2151541Srgrimes  VM_ENTRY_MARK;
2161541Srgrimes  return get_methodOop()->line_number_from_bci(bci);
2171541Srgrimes}
2181541Srgrimes
2191541Srgrimes
2201541Srgrimes// ------------------------------------------------------------------
2211541Srgrimes// ciMethod::vtable_index
2221541Srgrimes//
2231541Srgrimes// Get the position of this method's entry in the vtable, if any.
2241541Srgrimesint ciMethod::vtable_index() {
2251541Srgrimes  check_is_loaded();
2261541Srgrimes  assert(holder()->is_linked(), "must be linked");
2271541Srgrimes  VM_ENTRY_MARK;
2281541Srgrimes  return get_methodOop()->vtable_index();
2291541Srgrimes}
2301549Srgrimes
2311541Srgrimes
2321541Srgrimes// ------------------------------------------------------------------
2331541Srgrimes// ciMethod::native_entry
2341541Srgrimes//
2351541Srgrimes// Get the address of this method's native code, if any.
2361541Srgrimesaddress ciMethod::native_entry() {
2371541Srgrimes  check_is_loaded();
2381541Srgrimes  assert(flags().is_native(), "must be native method");
2391541Srgrimes  VM_ENTRY_MARK;
2401541Srgrimes  methodOop method = get_methodOop();
2411541Srgrimes  address entry = method->native_function();
2421541Srgrimes  assert(entry != NULL, "must be valid entry point");
2431541Srgrimes  return entry;
2441541Srgrimes}
2451541Srgrimes
2461541Srgrimes
2471541Srgrimes// ------------------------------------------------------------------
2481541Srgrimes// ciMethod::interpreter_entry
2491541Srgrimes//
2501541Srgrimes// Get the entry point for running this method in the interpreter.
2511541Srgrimesaddress ciMethod::interpreter_entry() {
2521541Srgrimes  check_is_loaded();
2533311Sphk  VM_ENTRY_MARK;
2543311Sphk  methodHandle mh(THREAD, get_methodOop());
2551541Srgrimes  return Interpreter::entry_for_method(mh);
2561541Srgrimes}
2571541Srgrimes
2581541Srgrimes
2591541Srgrimes// ------------------------------------------------------------------
2601541Srgrimes// ciMethod::uses_balanced_monitors
2611541Srgrimes//
2621541Srgrimes// Does this method use monitors in a strict stack-disciplined manner?
2631541Srgrimesbool ciMethod::has_balanced_monitors() {
2641541Srgrimes  check_is_loaded();
2651541Srgrimes  if (_balanced_monitors) return true;
2661541Srgrimes
2671541Srgrimes  // Analyze the method to see if monitors are used properly.
2681541Srgrimes  VM_ENTRY_MARK;
2691541Srgrimes  methodHandle method(THREAD, get_methodOop());
2701549Srgrimes  assert(method->has_monitor_bytecodes(), "should have checked this");
2711541Srgrimes
2721541Srgrimes  // Check to see if a previous compilation computed the
2731541Srgrimes  // monitor-matching analysis.
2741541Srgrimes  if (method->guaranteed_monitor_matching()) {
2751541Srgrimes    _balanced_monitors = true;
2761541Srgrimes    return true;
2771541Srgrimes  }
2781541Srgrimes
2791541Srgrimes  {
2801541Srgrimes    EXCEPTION_MARK;
2811541Srgrimes    ResourceMark rm(THREAD);
2821541Srgrimes    GeneratePairingInfo gpi(method);
2831541Srgrimes    gpi.compute_map(CATCH);
2841541Srgrimes    if (!gpi.monitor_safe()) {
2851541Srgrimes      return false;
2861541Srgrimes    }
2871541Srgrimes    method->set_guaranteed_monitor_matching();
2881541Srgrimes    _balanced_monitors = true;
2891541Srgrimes  }
2901541Srgrimes  return true;
2911541Srgrimes}
2921541Srgrimes
2931541Srgrimes
2941541Srgrimes// ------------------------------------------------------------------
2951541Srgrimes// ciMethod::get_flow_analysis
2961541SrgrimesciTypeFlow* ciMethod::get_flow_analysis() {
2971541Srgrimes#ifdef COMPILER2
2981541Srgrimes  if (_flow == NULL) {
2991541Srgrimes    ciEnv* env = CURRENT_ENV;
3001549Srgrimes    _flow = new (env->arena()) ciTypeFlow(env, this);
3011541Srgrimes    _flow->do_flow();
3021541Srgrimes  }
3031541Srgrimes  return _flow;
3041541Srgrimes#else // COMPILER2
3051541Srgrimes  ShouldNotReachHere();
3061541Srgrimes  return NULL;
3071541Srgrimes#endif // COMPILER2
3081541Srgrimes}
3091541Srgrimes
3101541Srgrimes
3111541Srgrimes// ------------------------------------------------------------------
3121541Srgrimes// ciMethod::get_osr_flow_analysis
3131541SrgrimesciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
3141541Srgrimes#ifdef COMPILER2
3151541Srgrimes  // OSR entry points are always place after a call bytecode of some sort
3161541Srgrimes  assert(osr_bci >= 0, "must supply valid OSR entry point");
3171541Srgrimes  ciEnv* env = CURRENT_ENV;
3181541Srgrimes  ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
3191541Srgrimes  flow->do_flow();
3201541Srgrimes  return flow;
3211541Srgrimes#else // COMPILER2
3221541Srgrimes  ShouldNotReachHere();
3231549Srgrimes  return NULL;
3241541Srgrimes#endif // COMPILER2
3251541Srgrimes}
3261541Srgrimes
3271541Srgrimes// ------------------------------------------------------------------
3281541Srgrimes// ciMethod::liveness_at_bci
3291541Srgrimes//
3301541Srgrimes// Which local variables are live at a specific bci?
3311541SrgrimesMethodLivenessResult ciMethod::liveness_at_bci(int bci) {
3321541Srgrimes  check_is_loaded();
3331541Srgrimes  if (_liveness == NULL) {
3341541Srgrimes    // Create the liveness analyzer.
3351541Srgrimes    Arena* arena = CURRENT_ENV->arena();
3361541Srgrimes    _liveness = new (arena) MethodLiveness(arena, this);
3371541Srgrimes    _liveness->compute_liveness();
3381541Srgrimes  }
3391541Srgrimes  MethodLivenessResult result = _liveness->get_liveness_at(bci);
3401541Srgrimes  if (JvmtiExport::can_access_local_variables() || DeoptimizeALot || CompileTheWorld) {
3411541Srgrimes    // Keep all locals live for the user's edification and amusement.
3421541Srgrimes    result.at_put_range(0, result.size(), true);
3431541Srgrimes  }
3441541Srgrimes  return result;
3451549Srgrimes}
3461541Srgrimes
3471541Srgrimes// ciMethod::live_local_oops_at_bci
3481541Srgrimes//
3491541Srgrimes// find all the live oops in the locals array for a particular bci
3501541Srgrimes// Compute what the interpreter believes by using the interpreter
3511541Srgrimes// oopmap generator. This is used as a double check during osr to
3521541Srgrimes// guard against conservative result from MethodLiveness making us
3531541Srgrimes// think a dead oop is live.  MethodLiveness is conservative in the
3541541Srgrimes// sense that it may consider locals to be live which cannot be live,
3551541Srgrimes// like in the case where a local could contain an oop or  a primitive
3561541Srgrimes// along different paths.  In that case the local must be dead when
3571541Srgrimes// those paths merge. Since the interpreter's viewpoint is used when
3581541Srgrimes// gc'ing an interpreter frame we need to use its viewpoint  during
3596151Sdg// OSR when loading the locals.
3606151Sdg
3611541SrgrimesBitMap ciMethod::live_local_oops_at_bci(int bci) {
3621541Srgrimes  VM_ENTRY_MARK;
3631541Srgrimes  InterpreterOopMap mask;
3641541Srgrimes  OopMapCache::compute_one_oop_map(get_methodOop(), bci, &mask);
3651541Srgrimes  int mask_size = max_locals();
3661541Srgrimes  BitMap result(mask_size);
3671541Srgrimes  result.clear();
3681549Srgrimes  int i;
3691541Srgrimes  for (i = 0; i < mask_size ; i++ ) {
3701541Srgrimes    if (mask.is_oop(i)) result.set_bit(i);
3711541Srgrimes  }
3721541Srgrimes  return result;
3731541Srgrimes}
3741541Srgrimes
3751541Srgrimes
3761541Srgrimes#ifdef COMPILER1
3771541Srgrimes// ------------------------------------------------------------------
3781541Srgrimes// ciMethod::bci_block_start
3791549Srgrimes//
3801541Srgrimes// Marks all bcis where a new basic block starts
3811541Srgrimesconst BitMap ciMethod::bci_block_start() {
3821541Srgrimes  check_is_loaded();
3831541Srgrimes  if (_liveness == NULL) {
3841541Srgrimes    // Create the liveness analyzer.
3851541Srgrimes    Arena* arena = CURRENT_ENV->arena();
3861541Srgrimes    _liveness = new (arena) MethodLiveness(arena, this);
3871541Srgrimes    _liveness->compute_liveness();
3881541Srgrimes  }
3891541Srgrimes
3901541Srgrimes  return _liveness->get_bci_block_start();
3911541Srgrimes}
3921541Srgrimes#endif // COMPILER1
3931549Srgrimes
3941541Srgrimes
3951541Srgrimes// ------------------------------------------------------------------
3961541Srgrimes// ciMethod::call_profile_at_bci
3971541Srgrimes//
3981541Srgrimes// Get the ciCallProfile for the invocation of this method.
3991541Srgrimes// Also reports receiver types for non-call type checks (if TypeProfileCasts).
4001541SrgrimesciCallProfile ciMethod::call_profile_at_bci(int bci) {
4011541Srgrimes  ResourceMark rm;
4021541Srgrimes  ciCallProfile result;
4031541Srgrimes  if (method_data() != NULL && method_data()->is_mature()) {
4041541Srgrimes    ciProfileData* data = method_data()->bci_to_data(bci);
4051541Srgrimes    if (data != NULL && data->is_CounterData()) {
4061541Srgrimes      // Every profiled call site has a counter.
4071541Srgrimes      int count = data->as_CounterData()->count();
4081541Srgrimes
4091541Srgrimes      if (!data->is_ReceiverTypeData()) {
4101541Srgrimes        result._receiver_count[0] = 0;  // that's a definite zero
4111541Srgrimes      } else { // ReceiverTypeData is a subclass of CounterData
4121541Srgrimes        ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
4131541Srgrimes        // In addition, virtual call sites have receiver type information
4141541Srgrimes        int receivers_count_total = 0;
4151541Srgrimes        int morphism = 0;
4161541Srgrimes        for (uint i = 0; i < call->row_limit(); i++) {
4171541Srgrimes          ciKlass* receiver = call->receiver(i);
4181541Srgrimes          if (receiver == NULL)  continue;
4191541Srgrimes          morphism += 1;
4201541Srgrimes          int rcount = call->receiver_count(i);
4211541Srgrimes          if (rcount == 0) rcount = 1; // Should be valid value
4221541Srgrimes          receivers_count_total += rcount;
4231541Srgrimes          // Add the receiver to result data.
4241541Srgrimes          result.add_receiver(receiver, rcount);
4251541Srgrimes          // If we extend profiling to record methods,
4263311Sphk          // we will set result._method also.
4271541Srgrimes        }
4283311Sphk        // Determine call site's morphism.
4293311Sphk        // The call site count could be == (receivers_count_total + 1)
4303311Sphk        // not only in the case of a polymorphic call but also in the case
4313311Sphk        // when a method data snapshot is taken after the site count was updated
4323311Sphk        // but before receivers counters were updated.
4333311Sphk        if (morphism == result._limit) {
4343311Sphk           // There were no array klasses and morphism <= MorphismLimit.
4353311Sphk           if (morphism <  ciCallProfile::MorphismLimit ||
4363311Sphk               morphism == ciCallProfile::MorphismLimit &&
4373311Sphk               (receivers_count_total+1) >= count) {
4383311Sphk             result._morphism = morphism;
4393311Sphk           }
4403311Sphk        }
4413311Sphk        // Make the count consistent if this is a call profile. If count is
4421541Srgrimes        // zero or less, presume that this is a typecheck profile and
4431541Srgrimes        // do nothing.  Otherwise, increase count to be the sum of all
4441549Srgrimes        // receiver's counts.
4451541Srgrimes        if (count > 0) {
4461541Srgrimes          if (count < receivers_count_total) {
4471541Srgrimes            count = receivers_count_total;
4481541Srgrimes          }
4491541Srgrimes        }
4501541Srgrimes      }
4511541Srgrimes      result._count = count;
4521541Srgrimes    }
4531541Srgrimes  }
4541549Srgrimes  return result;
4551541Srgrimes}
4561541Srgrimes
4571541Srgrimes// ------------------------------------------------------------------
4581541Srgrimes// Add new receiver and sort data by receiver's profile count.
4591541Srgrimesvoid ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
4601549Srgrimes  // Add new receiver and sort data by receiver's counts when we have space
4611541Srgrimes  // for it otherwise replace the less called receiver (less called receiver
4621541Srgrimes  // is placed to the last array element which is not used).
4631541Srgrimes  // First array's element contains most called receiver.
4641541Srgrimes  int i = _limit;
4651541Srgrimes  for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
4661541Srgrimes    _receiver[i] = _receiver[i-1];
4671541Srgrimes    _receiver_count[i] = _receiver_count[i-1];
4681541Srgrimes  }
4691541Srgrimes  _receiver[i] = receiver;
4701541Srgrimes  _receiver_count[i] = receiver_count;
4711541Srgrimes  if (_limit < MorphismLimit) _limit++;
4721541Srgrimes}
4731541Srgrimes
4741541Srgrimes// ------------------------------------------------------------------
4751541Srgrimes// ciMethod::find_monomorphic_target
4761541Srgrimes//
4771541Srgrimes// Given a certain calling environment, find the monomorphic target
4781541Srgrimes// for the call.  Return NULL if the call is not monomorphic in
4791541Srgrimes// its calling environment, or if there are only abstract methods.
4801541Srgrimes// The returned method is never abstract.
4811541Srgrimes// Note: If caller uses a non-null result, it must inform dependencies
4821541Srgrimes// via assert_unique_concrete_method or assert_leaf_type.
4831541SrgrimesciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
4841541Srgrimes                                            ciInstanceKlass* callee_holder,
4851541Srgrimes                                            ciInstanceKlass* actual_recv) {
4861541Srgrimes  check_is_loaded();
4871541Srgrimes
4881549Srgrimes  if (actual_recv->is_interface()) {
4891541Srgrimes    // %%% We cannot trust interface types, yet.  See bug 6312651.
4901541Srgrimes    return NULL;
4911541Srgrimes  }
4921541Srgrimes
4931541Srgrimes  ciMethod* root_m = resolve_invoke(caller, actual_recv);
4941541Srgrimes  if (root_m == NULL) {
4951541Srgrimes    // Something went wrong looking up the actual receiver method.
4961541Srgrimes    return NULL;
4971541Srgrimes  }
4981541Srgrimes  assert(!root_m->is_abstract(), "resolve_invoke promise");
4991549Srgrimes
5001541Srgrimes  // Make certain quick checks even if UseCHA is false.
5011541Srgrimes
5021541Srgrimes  // Is it private or final?
5031541Srgrimes  if (root_m->can_be_statically_bound()) {
5041541Srgrimes    return root_m;
5051541Srgrimes  }
5061541Srgrimes
5071541Srgrimes  if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
5081541Srgrimes    // Easy case.  There is no other place to put a method, so don't bother
5091541Srgrimes    // to go through the VM_ENTRY_MARK and all the rest.
5101541Srgrimes    return root_m;
5111541Srgrimes  }
5121541Srgrimes
5131541Srgrimes  // Array methods (clone, hashCode, etc.) are always statically bound.
5141541Srgrimes  // If we were to see an array type here, we'd return root_m.
5151541Srgrimes  // However, this method processes only ciInstanceKlasses.  (See 4962591.)
5161549Srgrimes  // The inline_native_clone intrinsic narrows Object to T[] properly,
5171541Srgrimes  // so there is no need to do the same job here.
5181541Srgrimes
5191541Srgrimes  if (!UseCHA)  return NULL;
5201541Srgrimes
5211541Srgrimes  VM_ENTRY_MARK;
5221541Srgrimes
523  methodHandle target;
524  {
525    MutexLocker locker(Compile_lock);
526    klassOop context = actual_recv->get_klassOop();
527    target = Dependencies::find_unique_concrete_method(context,
528                                                       root_m->get_methodOop());
529    // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
530  }
531
532#ifndef PRODUCT
533  if (TraceDependencies && target() != NULL && target() != root_m->get_methodOop()) {
534    tty->print("found a non-root unique target method");
535    tty->print_cr("  context = %s", instanceKlass::cast(actual_recv->get_klassOop())->external_name());
536    tty->print("  method  = ");
537    target->print_short_name(tty);
538    tty->cr();
539  }
540#endif //PRODUCT
541
542  if (target() == NULL) {
543    return NULL;
544  }
545  if (target() == root_m->get_methodOop()) {
546    return root_m;
547  }
548  if (!root_m->is_public() &&
549      !root_m->is_protected()) {
550    // If we are going to reason about inheritance, it's easiest
551    // if the method in question is public, protected, or private.
552    // If the answer is not root_m, it is conservatively correct
553    // to return NULL, even if the CHA encountered irrelevant
554    // methods in other packages.
555    // %%% TO DO: Work out logic for package-private methods
556    // with the same name but different vtable indexes.
557    return NULL;
558  }
559  return CURRENT_THREAD_ENV->get_object(target())->as_method();
560}
561
562// ------------------------------------------------------------------
563// ciMethod::resolve_invoke
564//
565// Given a known receiver klass, find the target for the call.
566// Return NULL if the call has no target or the target is abstract.
567ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver) {
568   check_is_loaded();
569   VM_ENTRY_MARK;
570
571   KlassHandle caller_klass (THREAD, caller->get_klassOop());
572   KlassHandle h_recv       (THREAD, exact_receiver->get_klassOop());
573   KlassHandle h_resolved   (THREAD, holder()->get_klassOop());
574   symbolHandle h_name      (THREAD, name()->get_symbolOop());
575   symbolHandle h_signature (THREAD, signature()->get_symbolOop());
576
577   methodHandle m;
578   // Only do exact lookup if receiver klass has been linked.  Otherwise,
579   // the vtable has not been setup, and the LinkResolver will fail.
580   if (h_recv->oop_is_javaArray()
581        ||
582       instanceKlass::cast(h_recv())->is_linked() && !exact_receiver->is_interface()) {
583     if (holder()->is_interface()) {
584       m = LinkResolver::resolve_interface_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
585     } else {
586       m = LinkResolver::resolve_virtual_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
587     }
588   }
589
590   if (m.is_null()) {
591     // Return NULL only if there was a problem with lookup (uninitialized class, etc.)
592     return NULL;
593   }
594
595   ciMethod* result = this;
596   if (m() != get_methodOop()) {
597     result = CURRENT_THREAD_ENV->get_object(m())->as_method();
598   }
599
600   // Don't return abstract methods because they aren't
601   // optimizable or interesting.
602   if (result->is_abstract()) {
603     return NULL;
604   } else {
605     return result;
606   }
607}
608
609// ------------------------------------------------------------------
610// ciMethod::resolve_vtable_index
611//
612// Given a known receiver klass, find the vtable index for the call.
613// Return methodOopDesc::invalid_vtable_index if the vtable_index is unknown.
614int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
615   check_is_loaded();
616
617   int vtable_index = methodOopDesc::invalid_vtable_index;
618   // Only do lookup if receiver klass has been linked.  Otherwise,
619   // the vtable has not been setup, and the LinkResolver will fail.
620   if (!receiver->is_interface()
621       && (!receiver->is_instance_klass() ||
622           receiver->as_instance_klass()->is_linked())) {
623     VM_ENTRY_MARK;
624
625     KlassHandle caller_klass (THREAD, caller->get_klassOop());
626     KlassHandle h_recv       (THREAD, receiver->get_klassOop());
627     symbolHandle h_name      (THREAD, name()->get_symbolOop());
628     symbolHandle h_signature (THREAD, signature()->get_symbolOop());
629
630     vtable_index = LinkResolver::resolve_virtual_vtable_index(h_recv, h_recv, h_name, h_signature, caller_klass);
631     if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
632       // A statically bound method.  Return "no such index".
633       vtable_index = methodOopDesc::invalid_vtable_index;
634     }
635   }
636
637   return vtable_index;
638}
639
640// ------------------------------------------------------------------
641// ciMethod::interpreter_call_site_count
642int ciMethod::interpreter_call_site_count(int bci) {
643  if (method_data() != NULL) {
644    ResourceMark rm;
645    ciProfileData* data = method_data()->bci_to_data(bci);
646    if (data != NULL && data->is_CounterData()) {
647      return scale_count(data->as_CounterData()->count());
648    }
649  }
650  return -1;  // unknown
651}
652
653// ------------------------------------------------------------------
654// Adjust a CounterData count to be commensurate with
655// interpreter_invocation_count.  If the MDO exists for
656// only 25% of the time the method exists, then the
657// counts in the MDO should be scaled by 4X, so that
658// they can be usefully and stably compared against the
659// invocation counts in methods.
660int ciMethod::scale_count(int count, float prof_factor) {
661  if (count > 0 && method_data() != NULL) {
662    int current_mileage = method_data()->current_mileage();
663    int creation_mileage = method_data()->creation_mileage();
664    int counter_life = current_mileage - creation_mileage;
665    int method_life = interpreter_invocation_count();
666    // counter_life due to backedge_counter could be > method_life
667    if (counter_life > method_life)
668      counter_life = method_life;
669    if (0 < counter_life && counter_life <= method_life) {
670      count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
671      count = (count > 0) ? count : 1;
672    }
673  }
674  return count;
675}
676
677// ------------------------------------------------------------------
678// ciMethod::build_method_data
679//
680// Generate new methodDataOop objects at compile time.
681void ciMethod::build_method_data(methodHandle h_m) {
682  EXCEPTION_CONTEXT;
683  if (is_native() || is_abstract() || h_m()->is_accessor()) return;
684  if (h_m()->method_data() == NULL) {
685    methodOopDesc::build_interpreter_method_data(h_m, THREAD);
686    if (HAS_PENDING_EXCEPTION) {
687      CLEAR_PENDING_EXCEPTION;
688    }
689  }
690  if (h_m()->method_data() != NULL) {
691    _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
692    _method_data->load_data();
693  } else {
694    _method_data = CURRENT_ENV->get_empty_methodData();
695  }
696}
697
698// public, retroactive version
699void ciMethod::build_method_data() {
700  if (_method_data == NULL || _method_data->is_empty()) {
701    GUARDED_VM_ENTRY({
702      build_method_data(get_methodOop());
703    });
704  }
705}
706
707
708// ------------------------------------------------------------------
709// ciMethod::method_data
710//
711ciMethodData* ciMethod::method_data() {
712  if (_method_data != NULL) {
713    return _method_data;
714  }
715  VM_ENTRY_MARK;
716  ciEnv* env = CURRENT_ENV;
717  Thread* my_thread = JavaThread::current();
718  methodHandle h_m(my_thread, get_methodOop());
719
720  if (Tier1UpdateMethodData && is_tier1_compile(env->comp_level())) {
721    build_method_data(h_m);
722  }
723
724  if (h_m()->method_data() != NULL) {
725    _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
726    _method_data->load_data();
727  } else {
728    _method_data = CURRENT_ENV->get_empty_methodData();
729  }
730  return _method_data;
731
732}
733
734
735// ------------------------------------------------------------------
736// ciMethod::will_link
737//
738// Will this method link in a specific calling context?
739bool ciMethod::will_link(ciKlass* accessing_klass,
740                         ciKlass* declared_method_holder,
741                         Bytecodes::Code bc) {
742  if (!is_loaded()) {
743    // Method lookup failed.
744    return false;
745  }
746
747  // The link checks have been front-loaded into the get_method
748  // call.  This method (ciMethod::will_link()) will be removed
749  // in the future.
750
751  return true;
752}
753
754// ------------------------------------------------------------------
755// ciMethod::should_exclude
756//
757// Should this method be excluded from compilation?
758bool ciMethod::should_exclude() {
759  check_is_loaded();
760  VM_ENTRY_MARK;
761  methodHandle mh(THREAD, get_methodOop());
762  bool ignore;
763  return CompilerOracle::should_exclude(mh, ignore);
764}
765
766// ------------------------------------------------------------------
767// ciMethod::should_inline
768//
769// Should this method be inlined during compilation?
770bool ciMethod::should_inline() {
771  check_is_loaded();
772  VM_ENTRY_MARK;
773  methodHandle mh(THREAD, get_methodOop());
774  return CompilerOracle::should_inline(mh);
775}
776
777// ------------------------------------------------------------------
778// ciMethod::should_not_inline
779//
780// Should this method be disallowed from inlining during compilation?
781bool ciMethod::should_not_inline() {
782  check_is_loaded();
783  VM_ENTRY_MARK;
784  methodHandle mh(THREAD, get_methodOop());
785  return CompilerOracle::should_not_inline(mh);
786}
787
788// ------------------------------------------------------------------
789// ciMethod::should_print_assembly
790//
791// Should the compiler print the generated code for this method?
792bool ciMethod::should_print_assembly() {
793  check_is_loaded();
794  VM_ENTRY_MARK;
795  methodHandle mh(THREAD, get_methodOop());
796  return CompilerOracle::should_print(mh);
797}
798
799// ------------------------------------------------------------------
800// ciMethod::break_at_execute
801//
802// Should the compiler insert a breakpoint into the generated code
803// method?
804bool ciMethod::break_at_execute() {
805  check_is_loaded();
806  VM_ENTRY_MARK;
807  methodHandle mh(THREAD, get_methodOop());
808  return CompilerOracle::should_break_at(mh);
809}
810
811// ------------------------------------------------------------------
812// ciMethod::has_option
813//
814bool ciMethod::has_option(const char* option) {
815  check_is_loaded();
816  VM_ENTRY_MARK;
817  methodHandle mh(THREAD, get_methodOop());
818  return CompilerOracle::has_option_string(mh, option);
819}
820
821// ------------------------------------------------------------------
822// ciMethod::can_be_compiled
823//
824// Have previous compilations of this method succeeded?
825bool ciMethod::can_be_compiled() {
826  check_is_loaded();
827  return _is_compilable;
828}
829
830// ------------------------------------------------------------------
831// ciMethod::set_not_compilable
832//
833// Tell the VM that this method cannot be compiled at all.
834void ciMethod::set_not_compilable() {
835  check_is_loaded();
836  VM_ENTRY_MARK;
837  _is_compilable = false;
838  get_methodOop()->set_not_compilable();
839}
840
841// ------------------------------------------------------------------
842// ciMethod::can_be_osr_compiled
843//
844// Have previous compilations of this method succeeded?
845//
846// Implementation note: the VM does not currently keep track
847// of failed OSR compilations per bci.  The entry_bci parameter
848// is currently unused.
849bool ciMethod::can_be_osr_compiled(int entry_bci) {
850  check_is_loaded();
851  VM_ENTRY_MARK;
852  return !get_methodOop()->access_flags().is_not_osr_compilable();
853}
854
855// ------------------------------------------------------------------
856// ciMethod::has_compiled_code
857bool ciMethod::has_compiled_code() {
858  VM_ENTRY_MARK;
859  return get_methodOop()->code() != NULL;
860}
861
862// ------------------------------------------------------------------
863// ciMethod::instructions_size
864// This is a rough metric for "fat" methods, compared
865// before inlining with InlineSmallCode.
866// The CodeBlob::instructions_size accessor includes
867// junk like exception handler, stubs, and constant table,
868// which are not highly relevant to an inlined method.
869// So we use the more specific accessor nmethod::code_size.
870int ciMethod::instructions_size() {
871  GUARDED_VM_ENTRY(
872    nmethod* code = get_methodOop()->code();
873    // if there's no compiled code or the code was produced by the
874    // tier1 profiler return 0 for the code size.  This should
875    // probably be based on the compilation level of the nmethod but
876    // that currently isn't properly recorded.
877    if (code == NULL ||
878        (TieredCompilation && code->compiler() != NULL && code->compiler()->is_c1())) {
879      return 0;
880    }
881    return code->code_end() - code->verified_entry_point();
882  )
883}
884
885// ------------------------------------------------------------------
886// ciMethod::log_nmethod_identity
887void ciMethod::log_nmethod_identity(xmlStream* log) {
888  GUARDED_VM_ENTRY(
889    nmethod* code = get_methodOop()->code();
890    if (code != NULL) {
891      code->log_identity(log);
892    }
893  )
894}
895
896// ------------------------------------------------------------------
897// ciMethod::is_not_reached
898bool ciMethod::is_not_reached(int bci) {
899  check_is_loaded();
900  VM_ENTRY_MARK;
901  return Interpreter::is_not_reached(
902               methodHandle(THREAD, get_methodOop()), bci);
903}
904
905// ------------------------------------------------------------------
906// ciMethod::was_never_executed
907bool ciMethod::was_executed_more_than(int times) {
908  VM_ENTRY_MARK;
909  return get_methodOop()->was_executed_more_than(times);
910}
911
912// ------------------------------------------------------------------
913// ciMethod::has_unloaded_classes_in_signature
914bool ciMethod::has_unloaded_classes_in_signature() {
915  VM_ENTRY_MARK;
916  {
917    EXCEPTION_MARK;
918    methodHandle m(THREAD, get_methodOop());
919    bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
920    if( HAS_PENDING_EXCEPTION ) {
921      CLEAR_PENDING_EXCEPTION;
922      return true;     // Declare that we may have unloaded classes
923    }
924    return has_unloaded;
925  }
926}
927
928// ------------------------------------------------------------------
929// ciMethod::is_klass_loaded
930bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
931  VM_ENTRY_MARK;
932  return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
933}
934
935// ------------------------------------------------------------------
936// ciMethod::check_call
937bool ciMethod::check_call(int refinfo_index, bool is_static) const {
938  VM_ENTRY_MARK;
939  {
940    EXCEPTION_MARK;
941    HandleMark hm(THREAD);
942    constantPoolHandle pool (THREAD, get_methodOop()->constants());
943    methodHandle spec_method;
944    KlassHandle  spec_klass;
945    LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
946    if (HAS_PENDING_EXCEPTION) {
947      CLEAR_PENDING_EXCEPTION;
948      return false;
949    } else {
950      return (spec_method->is_static() == is_static);
951    }
952  }
953  return false;
954}
955
956// ------------------------------------------------------------------
957// ciMethod::print_codes
958//
959// Print the bytecodes for this method.
960void ciMethod::print_codes_on(outputStream* st) {
961  check_is_loaded();
962  GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
963}
964
965
966#define FETCH_FLAG_FROM_VM(flag_accessor) { \
967  check_is_loaded(); \
968  VM_ENTRY_MARK; \
969  return get_methodOop()->flag_accessor(); \
970}
971
972bool ciMethod::is_empty_method() const {         FETCH_FLAG_FROM_VM(is_empty_method); }
973bool ciMethod::is_vanilla_constructor() const {  FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
974bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
975bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
976bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
977bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
978
979BCEscapeAnalyzer  *ciMethod::get_bcea() {
980  if (_bcea == NULL) {
981    _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
982  }
983  return _bcea;
984}
985
986ciMethodBlocks  *ciMethod::get_method_blocks() {
987  Arena *arena = CURRENT_ENV->arena();
988  if (_method_blocks == NULL) {
989    _method_blocks = new (arena) ciMethodBlocks(arena, this);
990  }
991  return _method_blocks;
992}
993
994#undef FETCH_FLAG_FROM_VM
995
996
997// ------------------------------------------------------------------
998// ciMethod::print_codes
999//
1000// Print a range of the bytecodes for this method.
1001void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1002  check_is_loaded();
1003  GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1004}
1005
1006// ------------------------------------------------------------------
1007// ciMethod::print_name
1008//
1009// Print the name of this method, including signature and some flags.
1010void ciMethod::print_name(outputStream* st) {
1011  check_is_loaded();
1012  GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1013}
1014
1015// ------------------------------------------------------------------
1016// ciMethod::print_short_name
1017//
1018// Print the name of this method, without signature.
1019void ciMethod::print_short_name(outputStream* st) {
1020  check_is_loaded();
1021  GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1022}
1023
1024// ------------------------------------------------------------------
1025// ciMethod::print_impl
1026//
1027// Implementation of the print method.
1028void ciMethod::print_impl(outputStream* st) {
1029  ciObject::print_impl(st);
1030  st->print(" name=");
1031  name()->print_symbol_on(st);
1032  st->print(" holder=");
1033  holder()->print_name_on(st);
1034  st->print(" signature=");
1035  signature()->as_symbol()->print_symbol_on(st);
1036  if (is_loaded()) {
1037    st->print(" loaded=true flags=");
1038    flags().print_member_flags(st);
1039  } else {
1040    st->print(" loaded=false");
1041  }
1042}
1043