1 /*
   2  * Copyright 1999-2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_ciMethod.cpp.incl"
  27 
  28 // ciMethod
  29 //
  30 // This class represents a methodOop in the HotSpot virtual
  31 // machine.
  32 
  33 
  34 // ------------------------------------------------------------------
  35 // ciMethod::ciMethod
  36 //
  37 // Loaded method.
  38 ciMethod::ciMethod(methodHandle h_m) : ciObject(h_m) {
  39   assert(h_m() != NULL, "no null method");
  40 
  41   // These fields are always filled in in loaded methods.
  42   _flags = ciFlags(h_m()->access_flags());
  43 
  44   // Easy to compute, so fill them in now.
  45   _max_stack          = h_m()->max_stack();
  46   _max_locals         = h_m()->max_locals();
  47   _code_size          = h_m()->code_size();
  48   _intrinsic_id       = h_m()->intrinsic_id();
  49   _handler_count      = h_m()->exception_table()->length() / 4;
  50   _uses_monitors      = h_m()->access_flags().has_monitor_bytecodes();
  51   _balanced_monitors  = !_uses_monitors || h_m()->access_flags().is_monitor_matching();
  52   _is_compilable      = !h_m()->is_not_compilable();
  53   // Lazy fields, filled in on demand.  Require allocation.
  54   _code               = NULL;
  55   _exception_handlers = NULL;
  56   _liveness           = NULL;
  57   _bcea = NULL;
  58   _method_blocks = NULL;
  59 #ifdef COMPILER2
  60   _flow               = NULL;
  61 #endif // COMPILER2
  62 
  63   ciEnv *env = CURRENT_ENV;
  64   if (env->jvmti_can_hotswap_or_post_breakpoint() && _is_compilable) {
  65     // 6328518 check hotswap conditions under the right lock.
  66     MutexLocker locker(Compile_lock);
  67     if (Dependencies::check_evol_method(h_m()) != NULL) {
  68       _is_compilable = false;
  69     }
  70   } else {
  71     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
  72   }
  73 
  74   if (instanceKlass::cast(h_m()->method_holder())->is_linked()) {
  75     _can_be_statically_bound = h_m()->can_be_statically_bound();
  76   } else {
  77     // Have to use a conservative value in this case.
  78     _can_be_statically_bound = false;
  79   }
  80 
  81   // Adjust the definition of this condition to be more useful:
  82   // %%% take these conditions into account in vtable generation
  83   if (!_can_be_statically_bound && h_m()->is_private())
  84     _can_be_statically_bound = true;
  85   if (_can_be_statically_bound && h_m()->is_abstract())
  86     _can_be_statically_bound = false;
  87 
  88   // generating _signature may allow GC and therefore move m.
  89   // These fields are always filled in.
  90   _name = env->get_object(h_m()->name())->as_symbol();
  91   _holder = env->get_object(h_m()->method_holder())->as_instance_klass();
  92   ciSymbol* sig_symbol = env->get_object(h_m()->signature())->as_symbol();
  93   _signature = new (env->arena()) ciSignature(_holder, sig_symbol);
  94   _method_data = NULL;
  95   // Take a snapshot of these values, so they will be commensurate with the MDO.
  96   if (ProfileInterpreter) {
  97     int invcnt = h_m()->interpreter_invocation_count();
  98     // if the value overflowed report it as max int
  99     _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
 100     _interpreter_throwout_count   = h_m()->interpreter_throwout_count();
 101   } else {
 102     _interpreter_invocation_count = 0;
 103     _interpreter_throwout_count = 0;
 104   }
 105   if (_interpreter_invocation_count == 0)
 106     _interpreter_invocation_count = 1;
 107 }
 108 
 109 
 110 // ------------------------------------------------------------------
 111 // ciMethod::ciMethod
 112 //
 113 // Unloaded method.
 114 ciMethod::ciMethod(ciInstanceKlass* holder,
 115                    ciSymbol* name,
 116                    ciSymbol* signature) : ciObject(ciMethodKlass::make()) {
 117   // These fields are always filled in.
 118   _name = name;
 119   _holder = holder;
 120   _signature = new (CURRENT_ENV->arena()) ciSignature(_holder, signature);
 121   _intrinsic_id = vmIntrinsics::_none;
 122   _liveness = NULL;
 123   _can_be_statically_bound = false;
 124   _bcea = NULL;
 125   _method_blocks = NULL;
 126   _method_data = NULL;
 127 #ifdef COMPILER2
 128   _flow = NULL;
 129 #endif // COMPILER2
 130 }
 131 
 132 
 133 // ------------------------------------------------------------------
 134 // ciMethod::load_code
 135 //
 136 // Load the bytecodes and exception handler table for this method.
 137 void ciMethod::load_code() {
 138   VM_ENTRY_MARK;
 139   assert(is_loaded(), "only loaded methods have code");
 140 
 141   methodOop me = get_methodOop();
 142   Arena* arena = CURRENT_THREAD_ENV->arena();
 143 
 144   // Load the bytecodes.
 145   _code = (address)arena->Amalloc(code_size());
 146   memcpy(_code, me->code_base(), code_size());
 147 
 148   // Revert any breakpoint bytecodes in ci's copy
 149   if (me->number_of_breakpoints() > 0) {
 150     BreakpointInfo* bp = instanceKlass::cast(me->method_holder())->breakpoints();
 151     for (; bp != NULL; bp = bp->next()) {
 152       if (bp->match(me)) {
 153         code_at_put(bp->bci(), bp->orig_bytecode());
 154       }
 155     }
 156   }
 157 
 158   // And load the exception table.
 159   typeArrayOop exc_table = me->exception_table();
 160 
 161   // Allocate one extra spot in our list of exceptions.  This
 162   // last entry will be used to represent the possibility that
 163   // an exception escapes the method.  See ciExceptionHandlerStream
 164   // for details.
 165   _exception_handlers =
 166     (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
 167                                          * (_handler_count + 1));
 168   if (_handler_count > 0) {
 169     for (int i=0; i<_handler_count; i++) {
 170       int base = i*4;
 171       _exception_handlers[i] = new (arena) ciExceptionHandler(
 172                                 holder(),
 173             /* start    */      exc_table->int_at(base),
 174             /* limit    */      exc_table->int_at(base+1),
 175             /* goto pc  */      exc_table->int_at(base+2),
 176             /* cp index */      exc_table->int_at(base+3));
 177     }
 178   }
 179 
 180   // Put an entry at the end of our list to represent the possibility
 181   // of exceptional exit.
 182   _exception_handlers[_handler_count] =
 183     new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
 184 
 185   if (CIPrintMethodCodes) {
 186     print_codes();
 187   }
 188 }
 189 
 190 
 191 // ------------------------------------------------------------------
 192 // ciMethod::has_linenumber_table
 193 //
 194 // length unknown until decompression
 195 bool    ciMethod::has_linenumber_table() const {
 196   check_is_loaded();
 197   VM_ENTRY_MARK;
 198   return get_methodOop()->has_linenumber_table();
 199 }
 200 
 201 
 202 // ------------------------------------------------------------------
 203 // ciMethod::compressed_linenumber_table
 204 u_char* ciMethod::compressed_linenumber_table() const {
 205   check_is_loaded();
 206   VM_ENTRY_MARK;
 207   return get_methodOop()->compressed_linenumber_table();
 208 }
 209 
 210 
 211 // ------------------------------------------------------------------
 212 // ciMethod::line_number_from_bci
 213 int ciMethod::line_number_from_bci(int bci) const {
 214   check_is_loaded();
 215   VM_ENTRY_MARK;
 216   return get_methodOop()->line_number_from_bci(bci);
 217 }
 218 
 219 
 220 // ------------------------------------------------------------------
 221 // ciMethod::vtable_index
 222 //
 223 // Get the position of this method's entry in the vtable, if any.
 224 int ciMethod::vtable_index() {
 225   check_is_loaded();
 226   assert(holder()->is_linked(), "must be linked");
 227   VM_ENTRY_MARK;
 228   return get_methodOop()->vtable_index();
 229 }
 230 
 231 
 232 // ------------------------------------------------------------------
 233 // ciMethod::native_entry
 234 //
 235 // Get the address of this method's native code, if any.
 236 address ciMethod::native_entry() {
 237   check_is_loaded();
 238   assert(flags().is_native(), "must be native method");
 239   VM_ENTRY_MARK;
 240   methodOop method = get_methodOop();
 241   address entry = method->native_function();
 242   assert(entry != NULL, "must be valid entry point");
 243   return entry;
 244 }
 245 
 246 
 247 // ------------------------------------------------------------------
 248 // ciMethod::interpreter_entry
 249 //
 250 // Get the entry point for running this method in the interpreter.
 251 address ciMethod::interpreter_entry() {
 252   check_is_loaded();
 253   VM_ENTRY_MARK;
 254   methodHandle mh(THREAD, get_methodOop());
 255   return Interpreter::entry_for_method(mh);
 256 }
 257 
 258 
 259 // ------------------------------------------------------------------
 260 // ciMethod::uses_balanced_monitors
 261 //
 262 // Does this method use monitors in a strict stack-disciplined manner?
 263 bool ciMethod::has_balanced_monitors() {
 264   check_is_loaded();
 265   if (_balanced_monitors) return true;
 266 
 267   // Analyze the method to see if monitors are used properly.
 268   VM_ENTRY_MARK;
 269   methodHandle method(THREAD, get_methodOop());
 270   assert(method->has_monitor_bytecodes(), "should have checked this");
 271 
 272   // Check to see if a previous compilation computed the
 273   // monitor-matching analysis.
 274   if (method->guaranteed_monitor_matching()) {
 275     _balanced_monitors = true;
 276     return true;
 277   }
 278 
 279   {
 280     EXCEPTION_MARK;
 281     ResourceMark rm(THREAD);
 282     GeneratePairingInfo gpi(method);
 283     gpi.compute_map(CATCH);
 284     if (!gpi.monitor_safe()) {
 285       return false;
 286     }
 287     method->set_guaranteed_monitor_matching();
 288     _balanced_monitors = true;
 289   }
 290   return true;
 291 }
 292 
 293 
 294 // ------------------------------------------------------------------
 295 // ciMethod::get_flow_analysis
 296 ciTypeFlow* ciMethod::get_flow_analysis() {
 297 #ifdef COMPILER2
 298   if (_flow == NULL) {
 299     ciEnv* env = CURRENT_ENV;
 300     _flow = new (env->arena()) ciTypeFlow(env, this);
 301     _flow->do_flow();
 302   }
 303   return _flow;
 304 #else // COMPILER2
 305   ShouldNotReachHere();
 306   return NULL;
 307 #endif // COMPILER2
 308 }
 309 
 310 
 311 // ------------------------------------------------------------------
 312 // ciMethod::get_osr_flow_analysis
 313 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
 314 #ifdef COMPILER2
 315   // OSR entry points are always place after a call bytecode of some sort
 316   assert(osr_bci >= 0, "must supply valid OSR entry point");
 317   ciEnv* env = CURRENT_ENV;
 318   ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
 319   flow->do_flow();
 320   return flow;
 321 #else // COMPILER2
 322   ShouldNotReachHere();
 323   return NULL;
 324 #endif // COMPILER2
 325 }
 326 
 327 // ------------------------------------------------------------------
 328 // ciMethod::raw_liveness_at_bci
 329 //
 330 // Which local variables are live at a specific bci?
 331 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) {
 332   check_is_loaded();
 333   if (_liveness == NULL) {
 334     // Create the liveness analyzer.
 335     Arena* arena = CURRENT_ENV->arena();
 336     _liveness = new (arena) MethodLiveness(arena, this);
 337     _liveness->compute_liveness();
 338   }
 339   return _liveness->get_liveness_at(bci);
 340 }
 341 
 342 // ------------------------------------------------------------------
 343 // ciMethod::liveness_at_bci
 344 //
 345 // Which local variables are live at a specific bci?  When debugging
 346 // will return true for all locals in some cases to improve debug
 347 // information.
 348 MethodLivenessResult ciMethod::liveness_at_bci(int bci) {
 349   MethodLivenessResult result = raw_liveness_at_bci(bci);
 350   if (CURRENT_ENV->jvmti_can_access_local_variables() || DeoptimizeALot || CompileTheWorld) {
 351     // Keep all locals live for the user's edification and amusement.
 352     result.at_put_range(0, result.size(), true);
 353   }
 354   return result;
 355 }
 356 
 357 // ciMethod::live_local_oops_at_bci
 358 //
 359 // find all the live oops in the locals array for a particular bci
 360 // Compute what the interpreter believes by using the interpreter
 361 // oopmap generator. This is used as a double check during osr to
 362 // guard against conservative result from MethodLiveness making us
 363 // think a dead oop is live.  MethodLiveness is conservative in the
 364 // sense that it may consider locals to be live which cannot be live,
 365 // like in the case where a local could contain an oop or  a primitive
 366 // along different paths.  In that case the local must be dead when
 367 // those paths merge. Since the interpreter's viewpoint is used when
 368 // gc'ing an interpreter frame we need to use its viewpoint  during
 369 // OSR when loading the locals.
 370 
 371 BitMap ciMethod::live_local_oops_at_bci(int bci) {
 372   VM_ENTRY_MARK;
 373   InterpreterOopMap mask;
 374   OopMapCache::compute_one_oop_map(get_methodOop(), bci, &mask);
 375   int mask_size = max_locals();
 376   BitMap result(mask_size);
 377   result.clear();
 378   int i;
 379   for (i = 0; i < mask_size ; i++ ) {
 380     if (mask.is_oop(i)) result.set_bit(i);
 381   }
 382   return result;
 383 }
 384 
 385 
 386 #ifdef COMPILER1
 387 // ------------------------------------------------------------------
 388 // ciMethod::bci_block_start
 389 //
 390 // Marks all bcis where a new basic block starts
 391 const BitMap ciMethod::bci_block_start() {
 392   check_is_loaded();
 393   if (_liveness == NULL) {
 394     // Create the liveness analyzer.
 395     Arena* arena = CURRENT_ENV->arena();
 396     _liveness = new (arena) MethodLiveness(arena, this);
 397     _liveness->compute_liveness();
 398   }
 399 
 400   return _liveness->get_bci_block_start();
 401 }
 402 #endif // COMPILER1
 403 
 404 
 405 // ------------------------------------------------------------------
 406 // ciMethod::call_profile_at_bci
 407 //
 408 // Get the ciCallProfile for the invocation of this method.
 409 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 410 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
 411   ResourceMark rm;
 412   ciCallProfile result;
 413   if (method_data() != NULL && method_data()->is_mature()) {
 414     ciProfileData* data = method_data()->bci_to_data(bci);
 415     if (data != NULL && data->is_CounterData()) {
 416       // Every profiled call site has a counter.
 417       int count = data->as_CounterData()->count();
 418 
 419       if (!data->is_ReceiverTypeData()) {
 420         result._receiver_count[0] = 0;  // that's a definite zero
 421       } else { // ReceiverTypeData is a subclass of CounterData
 422         ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 423         // In addition, virtual call sites have receiver type information
 424         int receivers_count_total = 0;
 425         int morphism = 0;
 426         for (uint i = 0; i < call->row_limit(); i++) {
 427           ciKlass* receiver = call->receiver(i);
 428           if (receiver == NULL)  continue;
 429           morphism += 1;
 430           int rcount = call->receiver_count(i);
 431           if (rcount == 0) rcount = 1; // Should be valid value
 432           receivers_count_total += rcount;
 433           // Add the receiver to result data.
 434           result.add_receiver(receiver, rcount);
 435           // If we extend profiling to record methods,
 436           // we will set result._method also.
 437         }
 438         // Determine call site's morphism.
 439         // The call site count is 0 with known morphism (onlt 1 or 2 receivers)
 440         // or < 0 in the case of a type check failured for checkcast, aastore, instanceof.
 441         // The call site count is > 0 in the case of a polymorphic virtual call.
 442         if (morphism > 0 && morphism == result._limit) {
 443            // The morphism <= MorphismLimit.
 444            if ((morphism <  ciCallProfile::MorphismLimit) ||
 445                (morphism == ciCallProfile::MorphismLimit && count == 0)) {
 446 #ifdef ASSERT
 447              if (count > 0) {
 448                tty->print_cr("bci: %d", bci);
 449                this->print_codes();
 450                assert(false, "this call site should not be polymorphic");
 451              }
 452 #endif
 453              result._morphism = morphism;
 454            }
 455         }
 456         // Make the count consistent if this is a call profile. If count is
 457         // zero or less, presume that this is a typecheck profile and
 458         // do nothing.  Otherwise, increase count to be the sum of all
 459         // receiver's counts.
 460         if (count >= 0) {
 461           count += receivers_count_total;
 462         }
 463       }
 464       result._count = count;
 465     }
 466   }
 467   return result;
 468 }
 469 
 470 // ------------------------------------------------------------------
 471 // Add new receiver and sort data by receiver's profile count.
 472 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
 473   // Add new receiver and sort data by receiver's counts when we have space
 474   // for it otherwise replace the less called receiver (less called receiver
 475   // is placed to the last array element which is not used).
 476   // First array's element contains most called receiver.
 477   int i = _limit;
 478   for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
 479     _receiver[i] = _receiver[i-1];
 480     _receiver_count[i] = _receiver_count[i-1];
 481   }
 482   _receiver[i] = receiver;
 483   _receiver_count[i] = receiver_count;
 484   if (_limit < MorphismLimit) _limit++;
 485 }
 486 
 487 // ------------------------------------------------------------------
 488 // ciMethod::find_monomorphic_target
 489 //
 490 // Given a certain calling environment, find the monomorphic target
 491 // for the call.  Return NULL if the call is not monomorphic in
 492 // its calling environment, or if there are only abstract methods.
 493 // The returned method is never abstract.
 494 // Note: If caller uses a non-null result, it must inform dependencies
 495 // via assert_unique_concrete_method or assert_leaf_type.
 496 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 497                                             ciInstanceKlass* callee_holder,
 498                                             ciInstanceKlass* actual_recv) {
 499   check_is_loaded();
 500 
 501   if (actual_recv->is_interface()) {
 502     // %%% We cannot trust interface types, yet.  See bug 6312651.
 503     return NULL;
 504   }
 505 
 506   ciMethod* root_m = resolve_invoke(caller, actual_recv);
 507   if (root_m == NULL) {
 508     // Something went wrong looking up the actual receiver method.
 509     return NULL;
 510   }
 511   assert(!root_m->is_abstract(), "resolve_invoke promise");
 512 
 513   // Make certain quick checks even if UseCHA is false.
 514 
 515   // Is it private or final?
 516   if (root_m->can_be_statically_bound()) {
 517     return root_m;
 518   }
 519 
 520   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 521     // Easy case.  There is no other place to put a method, so don't bother
 522     // to go through the VM_ENTRY_MARK and all the rest.
 523     return root_m;
 524   }
 525 
 526   // Array methods (clone, hashCode, etc.) are always statically bound.
 527   // If we were to see an array type here, we'd return root_m.
 528   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 529   // The inline_native_clone intrinsic narrows Object to T[] properly,
 530   // so there is no need to do the same job here.
 531 
 532   if (!UseCHA)  return NULL;
 533 
 534   VM_ENTRY_MARK;
 535 
 536   methodHandle target;
 537   {
 538     MutexLocker locker(Compile_lock);
 539     klassOop context = actual_recv->get_klassOop();
 540     target = Dependencies::find_unique_concrete_method(context,
 541                                                        root_m->get_methodOop());
 542     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 543   }
 544 
 545 #ifndef PRODUCT
 546   if (TraceDependencies && target() != NULL && target() != root_m->get_methodOop()) {
 547     tty->print("found a non-root unique target method");
 548     tty->print_cr("  context = %s", instanceKlass::cast(actual_recv->get_klassOop())->external_name());
 549     tty->print("  method  = ");
 550     target->print_short_name(tty);
 551     tty->cr();
 552   }
 553 #endif //PRODUCT
 554 
 555   if (target() == NULL) {
 556     return NULL;
 557   }
 558   if (target() == root_m->get_methodOop()) {
 559     return root_m;
 560   }
 561   if (!root_m->is_public() &&
 562       !root_m->is_protected()) {
 563     // If we are going to reason about inheritance, it's easiest
 564     // if the method in question is public, protected, or private.
 565     // If the answer is not root_m, it is conservatively correct
 566     // to return NULL, even if the CHA encountered irrelevant
 567     // methods in other packages.
 568     // %%% TO DO: Work out logic for package-private methods
 569     // with the same name but different vtable indexes.
 570     return NULL;
 571   }
 572   return CURRENT_THREAD_ENV->get_object(target())->as_method();
 573 }
 574 
 575 // ------------------------------------------------------------------
 576 // ciMethod::resolve_invoke
 577 //
 578 // Given a known receiver klass, find the target for the call.
 579 // Return NULL if the call has no target or the target is abstract.
 580 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver) {
 581    check_is_loaded();
 582    VM_ENTRY_MARK;
 583 
 584    KlassHandle caller_klass (THREAD, caller->get_klassOop());
 585    KlassHandle h_recv       (THREAD, exact_receiver->get_klassOop());
 586    KlassHandle h_resolved   (THREAD, holder()->get_klassOop());
 587    symbolHandle h_name      (THREAD, name()->get_symbolOop());
 588    symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 589 
 590    methodHandle m;
 591    // Only do exact lookup if receiver klass has been linked.  Otherwise,
 592    // the vtable has not been setup, and the LinkResolver will fail.
 593    if (h_recv->oop_is_javaArray()
 594         ||
 595        instanceKlass::cast(h_recv())->is_linked() && !exact_receiver->is_interface()) {
 596      if (holder()->is_interface()) {
 597        m = LinkResolver::resolve_interface_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 598      } else {
 599        m = LinkResolver::resolve_virtual_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 600      }
 601    }
 602 
 603    if (m.is_null()) {
 604      // Return NULL only if there was a problem with lookup (uninitialized class, etc.)
 605      return NULL;
 606    }
 607 
 608    ciMethod* result = this;
 609    if (m() != get_methodOop()) {
 610      result = CURRENT_THREAD_ENV->get_object(m())->as_method();
 611    }
 612 
 613    // Don't return abstract methods because they aren't
 614    // optimizable or interesting.
 615    if (result->is_abstract()) {
 616      return NULL;
 617    } else {
 618      return result;
 619    }
 620 }
 621 
 622 // ------------------------------------------------------------------
 623 // ciMethod::resolve_vtable_index
 624 //
 625 // Given a known receiver klass, find the vtable index for the call.
 626 // Return methodOopDesc::invalid_vtable_index if the vtable_index is unknown.
 627 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 628    check_is_loaded();
 629 
 630    int vtable_index = methodOopDesc::invalid_vtable_index;
 631    // Only do lookup if receiver klass has been linked.  Otherwise,
 632    // the vtable has not been setup, and the LinkResolver will fail.
 633    if (!receiver->is_interface()
 634        && (!receiver->is_instance_klass() ||
 635            receiver->as_instance_klass()->is_linked())) {
 636      VM_ENTRY_MARK;
 637 
 638      KlassHandle caller_klass (THREAD, caller->get_klassOop());
 639      KlassHandle h_recv       (THREAD, receiver->get_klassOop());
 640      symbolHandle h_name      (THREAD, name()->get_symbolOop());
 641      symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 642 
 643      vtable_index = LinkResolver::resolve_virtual_vtable_index(h_recv, h_recv, h_name, h_signature, caller_klass);
 644      if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
 645        // A statically bound method.  Return "no such index".
 646        vtable_index = methodOopDesc::invalid_vtable_index;
 647      }
 648    }
 649 
 650    return vtable_index;
 651 }
 652 
 653 // ------------------------------------------------------------------
 654 // ciMethod::interpreter_call_site_count
 655 int ciMethod::interpreter_call_site_count(int bci) {
 656   if (method_data() != NULL) {
 657     ResourceMark rm;
 658     ciProfileData* data = method_data()->bci_to_data(bci);
 659     if (data != NULL && data->is_CounterData()) {
 660       return scale_count(data->as_CounterData()->count());
 661     }
 662   }
 663   return -1;  // unknown
 664 }
 665 
 666 // ------------------------------------------------------------------
 667 // Adjust a CounterData count to be commensurate with
 668 // interpreter_invocation_count.  If the MDO exists for
 669 // only 25% of the time the method exists, then the
 670 // counts in the MDO should be scaled by 4X, so that
 671 // they can be usefully and stably compared against the
 672 // invocation counts in methods.
 673 int ciMethod::scale_count(int count, float prof_factor) {
 674   if (count > 0 && method_data() != NULL) {
 675     int current_mileage = method_data()->current_mileage();
 676     int creation_mileage = method_data()->creation_mileage();
 677     int counter_life = current_mileage - creation_mileage;
 678     int method_life = interpreter_invocation_count();
 679     // counter_life due to backedge_counter could be > method_life
 680     if (counter_life > method_life)
 681       counter_life = method_life;
 682     if (0 < counter_life && counter_life <= method_life) {
 683       count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
 684       count = (count > 0) ? count : 1;
 685     }
 686   }
 687   return count;
 688 }
 689 
 690 // ------------------------------------------------------------------
 691 // invokedynamic support
 692 //
 693 bool ciMethod::is_method_handle_invoke() const {
 694   check_is_loaded();
 695   bool flag = ((flags().as_int() & JVM_MH_INVOKE_BITS) == JVM_MH_INVOKE_BITS);
 696 #ifdef ASSERT
 697   {
 698     VM_ENTRY_MARK;
 699     bool flag2 = get_methodOop()->is_method_handle_invoke();
 700     assert(flag == flag2, "consistent");
 701   }
 702 #endif //ASSERT
 703   return flag;
 704 }
 705 
 706 bool ciMethod::is_method_handle_adapter() const {
 707   check_is_loaded();
 708   VM_ENTRY_MARK;
 709   return get_methodOop()->is_method_handle_adapter();
 710 }
 711 
 712 ciInstance* ciMethod::method_handle_type() {
 713   check_is_loaded();
 714   VM_ENTRY_MARK;
 715   oop mtype = get_methodOop()->method_handle_type();
 716   return CURRENT_THREAD_ENV->get_object(mtype)->as_instance();
 717 }
 718 
 719 
 720 // ------------------------------------------------------------------
 721 // ciMethod::build_method_data
 722 //
 723 // Generate new methodDataOop objects at compile time.
 724 void ciMethod::build_method_data(methodHandle h_m) {
 725   EXCEPTION_CONTEXT;
 726   if (is_native() || is_abstract() || h_m()->is_accessor()) return;
 727   if (h_m()->method_data() == NULL) {
 728     methodOopDesc::build_interpreter_method_data(h_m, THREAD);
 729     if (HAS_PENDING_EXCEPTION) {
 730       CLEAR_PENDING_EXCEPTION;
 731     }
 732   }
 733   if (h_m()->method_data() != NULL) {
 734     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 735     _method_data->load_data();
 736   } else {
 737     _method_data = CURRENT_ENV->get_empty_methodData();
 738   }
 739 }
 740 
 741 // public, retroactive version
 742 void ciMethod::build_method_data() {
 743   if (_method_data == NULL || _method_data->is_empty()) {
 744     GUARDED_VM_ENTRY({
 745       build_method_data(get_methodOop());
 746     });
 747   }
 748 }
 749 
 750 
 751 // ------------------------------------------------------------------
 752 // ciMethod::method_data
 753 //
 754 ciMethodData* ciMethod::method_data() {
 755   if (_method_data != NULL) {
 756     return _method_data;
 757   }
 758   VM_ENTRY_MARK;
 759   ciEnv* env = CURRENT_ENV;
 760   Thread* my_thread = JavaThread::current();
 761   methodHandle h_m(my_thread, get_methodOop());
 762 
 763   if (Tier1UpdateMethodData && is_tier1_compile(env->comp_level())) {
 764     build_method_data(h_m);
 765   }
 766 
 767   if (h_m()->method_data() != NULL) {
 768     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 769     _method_data->load_data();
 770   } else {
 771     _method_data = CURRENT_ENV->get_empty_methodData();
 772   }
 773   return _method_data;
 774 
 775 }
 776 
 777 
 778 // ------------------------------------------------------------------
 779 // ciMethod::will_link
 780 //
 781 // Will this method link in a specific calling context?
 782 bool ciMethod::will_link(ciKlass* accessing_klass,
 783                          ciKlass* declared_method_holder,
 784                          Bytecodes::Code bc) {
 785   if (!is_loaded()) {
 786     // Method lookup failed.
 787     return false;
 788   }
 789 
 790   // The link checks have been front-loaded into the get_method
 791   // call.  This method (ciMethod::will_link()) will be removed
 792   // in the future.
 793 
 794   return true;
 795 }
 796 
 797 // ------------------------------------------------------------------
 798 // ciMethod::should_exclude
 799 //
 800 // Should this method be excluded from compilation?
 801 bool ciMethod::should_exclude() {
 802   check_is_loaded();
 803   VM_ENTRY_MARK;
 804   methodHandle mh(THREAD, get_methodOop());
 805   bool ignore;
 806   return CompilerOracle::should_exclude(mh, ignore);
 807 }
 808 
 809 // ------------------------------------------------------------------
 810 // ciMethod::should_inline
 811 //
 812 // Should this method be inlined during compilation?
 813 bool ciMethod::should_inline() {
 814   check_is_loaded();
 815   VM_ENTRY_MARK;
 816   methodHandle mh(THREAD, get_methodOop());
 817   return CompilerOracle::should_inline(mh);
 818 }
 819 
 820 // ------------------------------------------------------------------
 821 // ciMethod::should_not_inline
 822 //
 823 // Should this method be disallowed from inlining during compilation?
 824 bool ciMethod::should_not_inline() {
 825   check_is_loaded();
 826   VM_ENTRY_MARK;
 827   methodHandle mh(THREAD, get_methodOop());
 828   return CompilerOracle::should_not_inline(mh);
 829 }
 830 
 831 // ------------------------------------------------------------------
 832 // ciMethod::should_print_assembly
 833 //
 834 // Should the compiler print the generated code for this method?
 835 bool ciMethod::should_print_assembly() {
 836   check_is_loaded();
 837   VM_ENTRY_MARK;
 838   methodHandle mh(THREAD, get_methodOop());
 839   return CompilerOracle::should_print(mh);
 840 }
 841 
 842 // ------------------------------------------------------------------
 843 // ciMethod::break_at_execute
 844 //
 845 // Should the compiler insert a breakpoint into the generated code
 846 // method?
 847 bool ciMethod::break_at_execute() {
 848   check_is_loaded();
 849   VM_ENTRY_MARK;
 850   methodHandle mh(THREAD, get_methodOop());
 851   return CompilerOracle::should_break_at(mh);
 852 }
 853 
 854 // ------------------------------------------------------------------
 855 // ciMethod::has_option
 856 //
 857 bool ciMethod::has_option(const char* option) {
 858   check_is_loaded();
 859   VM_ENTRY_MARK;
 860   methodHandle mh(THREAD, get_methodOop());
 861   return CompilerOracle::has_option_string(mh, option);
 862 }
 863 
 864 // ------------------------------------------------------------------
 865 // ciMethod::can_be_compiled
 866 //
 867 // Have previous compilations of this method succeeded?
 868 bool ciMethod::can_be_compiled() {
 869   check_is_loaded();
 870   return _is_compilable;
 871 }
 872 
 873 // ------------------------------------------------------------------
 874 // ciMethod::set_not_compilable
 875 //
 876 // Tell the VM that this method cannot be compiled at all.
 877 void ciMethod::set_not_compilable() {
 878   check_is_loaded();
 879   VM_ENTRY_MARK;
 880   _is_compilable = false;
 881   get_methodOop()->set_not_compilable();
 882 }
 883 
 884 // ------------------------------------------------------------------
 885 // ciMethod::can_be_osr_compiled
 886 //
 887 // Have previous compilations of this method succeeded?
 888 //
 889 // Implementation note: the VM does not currently keep track
 890 // of failed OSR compilations per bci.  The entry_bci parameter
 891 // is currently unused.
 892 bool ciMethod::can_be_osr_compiled(int entry_bci) {
 893   check_is_loaded();
 894   VM_ENTRY_MARK;
 895   return !get_methodOop()->access_flags().is_not_osr_compilable();
 896 }
 897 
 898 // ------------------------------------------------------------------
 899 // ciMethod::has_compiled_code
 900 bool ciMethod::has_compiled_code() {
 901   VM_ENTRY_MARK;
 902   return get_methodOop()->code() != NULL;
 903 }
 904 
 905 // ------------------------------------------------------------------
 906 // ciMethod::instructions_size
 907 // This is a rough metric for "fat" methods, compared
 908 // before inlining with InlineSmallCode.
 909 // The CodeBlob::instructions_size accessor includes
 910 // junk like exception handler, stubs, and constant table,
 911 // which are not highly relevant to an inlined method.
 912 // So we use the more specific accessor nmethod::code_size.
 913 int ciMethod::instructions_size() {
 914   GUARDED_VM_ENTRY(
 915     nmethod* code = get_methodOop()->code();
 916     // if there's no compiled code or the code was produced by the
 917     // tier1 profiler return 0 for the code size.  This should
 918     // probably be based on the compilation level of the nmethod but
 919     // that currently isn't properly recorded.
 920     if (code == NULL ||
 921         (TieredCompilation && code->compiler() != NULL && code->compiler()->is_c1())) {
 922       return 0;
 923     }
 924     return code->code_end() - code->verified_entry_point();
 925   )
 926 }
 927 
 928 // ------------------------------------------------------------------
 929 // ciMethod::log_nmethod_identity
 930 void ciMethod::log_nmethod_identity(xmlStream* log) {
 931   GUARDED_VM_ENTRY(
 932     nmethod* code = get_methodOop()->code();
 933     if (code != NULL) {
 934       code->log_identity(log);
 935     }
 936   )
 937 }
 938 
 939 // ------------------------------------------------------------------
 940 // ciMethod::is_not_reached
 941 bool ciMethod::is_not_reached(int bci) {
 942   check_is_loaded();
 943   VM_ENTRY_MARK;
 944   return Interpreter::is_not_reached(
 945                methodHandle(THREAD, get_methodOop()), bci);
 946 }
 947 
 948 // ------------------------------------------------------------------
 949 // ciMethod::was_never_executed
 950 bool ciMethod::was_executed_more_than(int times) {
 951   VM_ENTRY_MARK;
 952   return get_methodOop()->was_executed_more_than(times);
 953 }
 954 
 955 // ------------------------------------------------------------------
 956 // ciMethod::has_unloaded_classes_in_signature
 957 bool ciMethod::has_unloaded_classes_in_signature() {
 958   VM_ENTRY_MARK;
 959   {
 960     EXCEPTION_MARK;
 961     methodHandle m(THREAD, get_methodOop());
 962     bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
 963     if( HAS_PENDING_EXCEPTION ) {
 964       CLEAR_PENDING_EXCEPTION;
 965       return true;     // Declare that we may have unloaded classes
 966     }
 967     return has_unloaded;
 968   }
 969 }
 970 
 971 // ------------------------------------------------------------------
 972 // ciMethod::is_klass_loaded
 973 bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
 974   VM_ENTRY_MARK;
 975   return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
 976 }
 977 
 978 // ------------------------------------------------------------------
 979 // ciMethod::check_call
 980 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
 981   VM_ENTRY_MARK;
 982   {
 983     EXCEPTION_MARK;
 984     HandleMark hm(THREAD);
 985     constantPoolHandle pool (THREAD, get_methodOop()->constants());
 986     methodHandle spec_method;
 987     KlassHandle  spec_klass;
 988     LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
 989     if (HAS_PENDING_EXCEPTION) {
 990       CLEAR_PENDING_EXCEPTION;
 991       return false;
 992     } else {
 993       return (spec_method->is_static() == is_static);
 994     }
 995   }
 996   return false;
 997 }
 998 
 999 // ------------------------------------------------------------------
1000 // ciMethod::print_codes
1001 //
1002 // Print the bytecodes for this method.
1003 void ciMethod::print_codes_on(outputStream* st) {
1004   check_is_loaded();
1005   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
1006 }
1007 
1008 
1009 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1010   check_is_loaded(); \
1011   VM_ENTRY_MARK; \
1012   return get_methodOop()->flag_accessor(); \
1013 }
1014 
1015 bool ciMethod::is_empty_method() const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1016 bool ciMethod::is_vanilla_constructor() const {  FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
1017 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1018 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1019 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1020 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1021 
1022 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1023   if (_bcea == NULL) {
1024     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
1025   }
1026   return _bcea;
1027 }
1028 
1029 ciMethodBlocks  *ciMethod::get_method_blocks() {
1030   Arena *arena = CURRENT_ENV->arena();
1031   if (_method_blocks == NULL) {
1032     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1033   }
1034   return _method_blocks;
1035 }
1036 
1037 #undef FETCH_FLAG_FROM_VM
1038 
1039 
1040 // ------------------------------------------------------------------
1041 // ciMethod::print_codes
1042 //
1043 // Print a range of the bytecodes for this method.
1044 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1045   check_is_loaded();
1046   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1047 }
1048 
1049 // ------------------------------------------------------------------
1050 // ciMethod::print_name
1051 //
1052 // Print the name of this method, including signature and some flags.
1053 void ciMethod::print_name(outputStream* st) {
1054   check_is_loaded();
1055   GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1056 }
1057 
1058 // ------------------------------------------------------------------
1059 // ciMethod::print_short_name
1060 //
1061 // Print the name of this method, without signature.
1062 void ciMethod::print_short_name(outputStream* st) {
1063   check_is_loaded();
1064   GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1065 }
1066 
1067 // ------------------------------------------------------------------
1068 // ciMethod::print_impl
1069 //
1070 // Implementation of the print method.
1071 void ciMethod::print_impl(outputStream* st) {
1072   ciObject::print_impl(st);
1073   st->print(" name=");
1074   name()->print_symbol_on(st);
1075   st->print(" holder=");
1076   holder()->print_name_on(st);
1077   st->print(" signature=");
1078   signature()->as_symbol()->print_symbol_on(st);
1079   if (is_loaded()) {
1080     st->print(" loaded=true flags=");
1081     flags().print_member_flags(st);
1082   } else {
1083     st->print(" loaded=false");
1084   }
1085 }