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