1674 }
1675
1676 if (cha_monomorphic_target != NULL) {
1677 if (cha_monomorphic_target->is_abstract()) {
1678 // Do not optimize for abstract methods
1679 cha_monomorphic_target = NULL;
1680 }
1681 }
1682
1683 if (cha_monomorphic_target != NULL) {
1684 if (!(target->is_final_method())) {
1685 // If we inlined because CHA revealed only a single target method,
1686 // then we are dependent on that target method not getting overridden
1687 // by dynamic class loading. Be sure to test the "static" receiver
1688 // dest_method here, as opposed to the actual receiver, which may
1689 // falsely lead us to believe that the receiver is final or private.
1690 dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target);
1691 }
1692 code = Bytecodes::_invokespecial;
1693 }
1694 // check if we could do inlining
1695 if (!PatchALot && Inline && klass->is_loaded() &&
1696 (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
1697 && target->will_link(klass, callee_holder, code)) {
1698 // callee is known => check if we have static binding
1699 assert(target->is_loaded(), "callee must be known");
1700 if (code == Bytecodes::_invokestatic ||
1701 code == Bytecodes::_invokespecial ||
1702 code == Bytecodes::_invokevirtual && target->is_final_method() ||
1703 code == Bytecodes::_invokedynamic) {
1704 ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target;
1705 bool success = false;
1706 if (target->is_method_handle_invoke()) {
1707 // method handle invokes
1708 success = !is_invokedynamic ? for_method_handle_inline(target) : for_invokedynamic_inline(target);
1709 }
1710 if (!success) {
1711 // static binding => check if callee is ok
1712 success = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL));
1713 }
1714 CHECK_BAILOUT();
1715
1716 #ifndef PRODUCT
1717 // printing
1718 if (PrintInlining && !success) {
1719 // if it was successfully inlined, then it was already printed.
1720 print_inline_result(inline_target, success);
1721 }
1722 #endif
1723 clear_inline_bailout();
1724 if (success) {
1725 // Register dependence if JVMTI has either breakpoint
1726 // setting or hotswapping of methods capabilities since they may
1727 // cause deoptimization.
1728 if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) {
1729 dependency_recorder()->assert_evol_method(inline_target);
1730 }
1731 return;
1732 }
1733 }
1734 }
1735 // If we attempted an inline which did not succeed because of a
1736 // bailout during construction of the callee graph, the entire
1737 // compilation has to be aborted. This is fairly rare and currently
1738 // seems to only occur for jasm-generated classes which contain
1739 // jsr/ret pairs which are not associated with finally clauses and
1740 // do not have exception handlers in the containing method, and are
1741 // therefore not caught early enough to abort the inlining without
1742 // corrupting the graph. (We currently bail out with a non-empty
1743 // stack at a ret in these situations.)
1744 CHECK_BAILOUT();
1745
1746 // inlining not successful => standard invoke
1747 bool is_loaded = target->is_loaded();
1748 bool has_receiver =
1749 code == Bytecodes::_invokespecial ||
1750 code == Bytecodes::_invokevirtual ||
1751 code == Bytecodes::_invokeinterface;
1752 ValueType* result_type = as_ValueType(target->return_type());
1753
1754 // We require the debug info to be the "state before" because
1784
1785 if (is_profiling()) {
1786 if (recv != NULL && profile_calls()) {
1787 null_check(recv);
1788 }
1789 // Note that we'd collect profile data in this method if we wanted it.
1790 compilation()->set_would_profile(true);
1791
1792 if (profile_calls()) {
1793 assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set");
1794 ciKlass* target_klass = NULL;
1795 if (cha_monomorphic_target != NULL) {
1796 target_klass = cha_monomorphic_target->holder();
1797 } else if (exact_target != NULL) {
1798 target_klass = exact_target->holder();
1799 }
1800 profile_call(recv, target_klass);
1801 }
1802 }
1803
1804 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before);
1805 // push result
1806 append_split(result);
1807
1808 if (result_type != voidType) {
1809 if (method()->is_strict()) {
1810 push(result_type, round_fp(result));
1811 } else {
1812 push(result_type, result);
1813 }
1814 }
1815 }
1816
1817
1818 void GraphBuilder::new_instance(int klass_index) {
1819 ValueStack* state_before = copy_state_exhandling();
1820 bool will_link;
1821 ciKlass* klass = stream()->get_klass(will_link);
1822 assert(klass->is_instance_klass(), "must be an instance klass");
1823 NewInstance* new_instance = new NewInstance(klass->as_instance_klass(), state_before);
1824 _memory->new_instance(new_instance);
3017 if (_compilation->env()->jvmti_can_access_local_variables()) {
3018 s = state()->copy(ValueStack::ExceptionState, bci);
3019 } else {
3020 s = state()->copy(ValueStack::EmptyExceptionState, bci);
3021 }
3022 }
3023 return s;
3024 }
3025
3026 int GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const {
3027 int recur_level = 0;
3028 for (IRScope* s = scope(); s != NULL; s = s->caller()) {
3029 if (s->method() == cur_callee) {
3030 ++recur_level;
3031 }
3032 }
3033 return recur_level;
3034 }
3035
3036
3037 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known) {
3038 // Clear out any existing inline bailout condition
3039 clear_inline_bailout();
3040
3041 if (callee->should_exclude()) {
3042 // callee is excluded
3043 INLINE_BAILOUT("excluded by CompilerOracle")
3044 } else if (callee->should_not_inline()) {
3045 // callee is excluded
3046 INLINE_BAILOUT("disallowed by CompilerOracle")
3047 } else if (!callee->can_be_compiled()) {
3048 // callee is not compilable (prob. has breakpoints)
3049 INLINE_BAILOUT("not compilable (disabled)")
3050 } else if (callee->intrinsic_id() != vmIntrinsics::_none && try_inline_intrinsics(callee)) {
3051 // intrinsics can be native or not
3052 return true;
3053 } else if (callee->is_native()) {
3054 // non-intrinsic natives cannot be inlined
3055 INLINE_BAILOUT("non-intrinsic native")
3056 } else if (callee->is_abstract()) {
3057 INLINE_BAILOUT("abstract")
3058 } else {
3059 return try_inline_full(callee, holder_known);
3060 }
3061 }
3062
3063
3064 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) {
3065 if (!InlineNatives ) INLINE_BAILOUT("intrinsic method inlining disabled");
3066 if (callee->is_synchronized()) {
3067 // We don't currently support any synchronized intrinsics
3068 return false;
3069 }
3070
3071 // callee seems like a good candidate
3072 // determine id
3073 bool preserves_state = false;
3074 bool cantrap = true;
3075 vmIntrinsics::ID id = callee->intrinsic_id();
3076 switch (id) {
3077 case vmIntrinsics::_arraycopy :
3078 if (!InlineArrayCopy) return false;
3079 break;
3388 if (!default_handler) {
3389 pop_scope();
3390 bci = _state->caller_state()->bci();
3391 _state = _state->caller_state()->copy_for_parsing();
3392 }
3393 }
3394
3395 // perform the throw as if at the the call site
3396 apush(exception);
3397 throw_op(bci);
3398
3399 BlockEnd* end = last()->as_BlockEnd();
3400 block()->set_end(end);
3401
3402 _block = orig_block;
3403 _state = orig_state;
3404 _last = orig_last;
3405 }
3406
3407
3408 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, BlockBegin* cont_block) {
3409 assert(!callee->is_native(), "callee must not be native");
3410 if (CompilationPolicy::policy()->should_not_inline(compilation()->env(), callee)) {
3411 INLINE_BAILOUT("inlining prohibited by policy");
3412 }
3413 // first perform tests of things it's not possible to inline
3414 if (callee->has_exception_handlers() &&
3415 !InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers");
3416 if (callee->is_synchronized() &&
3417 !InlineSynchronizedMethods ) INLINE_BAILOUT("callee is synchronized");
3418 if (!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet");
3419 if (!callee->has_balanced_monitors()) INLINE_BAILOUT("callee's monitors do not match");
3420
3421 // Proper inlining of methods with jsrs requires a little more work.
3422 if (callee->has_jsrs() ) INLINE_BAILOUT("jsrs not handled properly by inliner yet");
3423
3424 // When SSE2 is used on intel, then no special handling is needed
3425 // for strictfp because the enum-constant is fixed at compile time,
3426 // the check for UseSSE2 is needed here
3427 if (strict_fp_requires_explicit_rounding && UseSSE < 2 && method()->is_strict() != callee->is_strict()) {
3428 INLINE_BAILOUT("caller and callee have different strict fp requirements");
3429 }
3430
3431 if (is_profiling() && !callee->ensure_method_data()) {
3432 INLINE_BAILOUT("mdo allocation failed");
3433 }
3434
3435 // now perform tests that are based on flag settings
3436 if (callee->should_inline()) {
3437 // ignore heuristic controls on inlining
3438 } else {
3439 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("too-deep inlining");
3440 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("too-deep recursive inlining");
3441 if (callee->code_size_for_inlining() > max_inline_size() ) INLINE_BAILOUT("callee is too large");
3442
3443 // don't inline throwable methods unless the inlining tree is rooted in a throwable class
3444 if (callee->name() == ciSymbol::object_initializer_name() &&
3445 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3446 // Throwable constructor call
3447 IRScope* top = scope();
3448 while (top->caller() != NULL) {
3449 top = top->caller();
3450 }
3451 if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3452 INLINE_BAILOUT("don't inline Throwable constructors");
3453 }
3454 }
3455
3456 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) {
3457 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit");
3458 }
3459 }
3460
3461 #ifndef PRODUCT
3519 // Record number of predecessors of continuation block before
3520 // inlining, to detect if inlined method has edges to its
3521 // continuation after inlining.
3522 int continuation_preds = cont->number_of_preds();
3523
3524 // Push callee scope
3525 push_scope(callee, cont);
3526
3527 // the BlockListBuilder for the callee could have bailed out
3528 CHECK_BAILOUT_(false);
3529
3530 // Temporarily set up bytecode stream so we can append instructions
3531 // (only using the bci of this stream)
3532 scope_data()->set_stream(scope_data()->parent()->stream());
3533
3534 // Pass parameters into callee state: add assignments
3535 // note: this will also ensure that all arguments are computed before being passed
3536 ValueStack* callee_state = state();
3537 ValueStack* caller_state = state()->caller_state();
3538 { int i = args_base;
3539 while (i < caller_state->stack_size()) {
3540 const int par_no = i - args_base;
3541 Value arg = caller_state->stack_at_inc(i);
3542 // NOTE: take base() of arg->type() to avoid problems storing
3543 // constants
3544 store_local(callee_state, arg, arg->type()->base(), par_no);
3545 }
3546 }
3547
3548 // Remove args from stack.
3549 // Note that we preserve locals state in case we can use it later
3550 // (see use of pop_scope() below)
3551 caller_state->truncate_stack(args_base);
3552 assert(callee_state->stack_size() == 0, "callee stack must be empty");
3553
3554 Value lock;
3555 BlockBegin* sync_handler;
3556
3557 // Inline the locking of the receiver if the callee is synchronized
3558 if (callee->is_synchronized()) {
3559 lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror())))
3560 : state()->local_at(0);
3561 sync_handler = new BlockBegin(SynchronizationEntryBCI);
3656 return true;
3657 }
3658
3659
3660 bool GraphBuilder::for_method_handle_inline(ciMethod* callee) {
3661 assert(!callee->is_static(), "change next line");
3662 int index = state()->stack_size() - (callee->arg_size_no_receiver() + 1);
3663 Value receiver = state()->stack_at(index);
3664
3665 if (receiver->type()->is_constant()) {
3666 ciMethodHandle* method_handle = receiver->type()->as_ObjectType()->constant_value()->as_method_handle();
3667
3668 // Set the callee to have access to the class and signature in
3669 // the MethodHandleCompiler.
3670 method_handle->set_callee(callee);
3671 method_handle->set_caller(method());
3672
3673 // Get an adapter for the MethodHandle.
3674 ciMethod* method_handle_adapter = method_handle->get_method_handle_adapter();
3675 if (method_handle_adapter != NULL) {
3676 return try_inline(method_handle_adapter, /*holder_known=*/ true);
3677 }
3678 } else if (receiver->as_CheckCast()) {
3679 // Match MethodHandle.selectAlternative idiom
3680 Phi* phi = receiver->as_CheckCast()->obj()->as_Phi();
3681
3682 if (phi != NULL && phi->operand_count() == 2) {
3683 // Get the two MethodHandle inputs from the Phi.
3684 Value op1 = phi->operand_at(0);
3685 Value op2 = phi->operand_at(1);
3686 ciMethodHandle* mh1 = op1->type()->as_ObjectType()->constant_value()->as_method_handle();
3687 ciMethodHandle* mh2 = op2->type()->as_ObjectType()->constant_value()->as_method_handle();
3688
3689 // Set the callee to have access to the class and signature in
3690 // the MethodHandleCompiler.
3691 mh1->set_callee(callee);
3692 mh1->set_caller(method());
3693 mh2->set_callee(callee);
3694 mh2->set_caller(method());
3695
3696 // Get adapters for the MethodHandles.
3699
3700 if (mh1_adapter != NULL && mh2_adapter != NULL) {
3701 set_inline_cleanup_info();
3702
3703 // Build the If guard
3704 BlockBegin* one = new BlockBegin(next_bci());
3705 BlockBegin* two = new BlockBegin(next_bci());
3706 BlockBegin* end = new BlockBegin(next_bci());
3707 Instruction* iff = append(new If(phi, If::eql, false, op1, one, two, NULL, false));
3708 block()->set_end(iff->as_BlockEnd());
3709
3710 // Connect up the states
3711 one->merge(block()->end()->state());
3712 two->merge(block()->end()->state());
3713
3714 // Save the state for the second inlinee
3715 ValueStack* state_before = copy_state_before();
3716
3717 // Parse first adapter
3718 _last = _block = one;
3719 if (!try_inline_full(mh1_adapter, /*holder_known=*/ true, end)) {
3720 restore_inline_cleanup_info();
3721 block()->clear_end(); // remove appended iff
3722 return false;
3723 }
3724
3725 // Parse second adapter
3726 _last = _block = two;
3727 _state = state_before;
3728 if (!try_inline_full(mh2_adapter, /*holder_known=*/ true, end)) {
3729 restore_inline_cleanup_info();
3730 block()->clear_end(); // remove appended iff
3731 return false;
3732 }
3733
3734 connect_to_end(end);
3735 return true;
3736 }
3737 }
3738 }
3739 return false;
3740 }
3741
3742
3743 bool GraphBuilder::for_invokedynamic_inline(ciMethod* callee) {
3744 // Get the MethodHandle from the CallSite.
3745 ciCallSite* call_site = stream()->get_call_site();
3746 ciMethodHandle* method_handle = call_site->get_target();
3747
3748 // Set the callee to have access to the class and signature in the
3749 // MethodHandleCompiler.
3750 method_handle->set_callee(callee);
3751 method_handle->set_caller(method());
3752
3753 // Get an adapter for the MethodHandle.
3754 ciMethod* method_handle_adapter = method_handle->get_invokedynamic_adapter();
3755 if (method_handle_adapter != NULL) {
3756 if (try_inline(method_handle_adapter, /*holder_known=*/ true)) {
3757 // Add a dependence for invalidation of the optimization.
3758 if (!call_site->is_constant_call_site()) {
3759 dependency_recorder()->assert_call_site_target_value(call_site, method_handle);
3760 }
3761 return true;
3762 }
3763 }
3764 return false;
3765 }
3766
3767
3768 void GraphBuilder::inline_bailout(const char* msg) {
3769 assert(msg != NULL, "inline bailout msg must exist");
3770 _inline_bailout_msg = msg;
3771 }
3772
3773
3774 void GraphBuilder::clear_inline_bailout() {
3775 _inline_bailout_msg = NULL;
3776 }
|
1674 }
1675
1676 if (cha_monomorphic_target != NULL) {
1677 if (cha_monomorphic_target->is_abstract()) {
1678 // Do not optimize for abstract methods
1679 cha_monomorphic_target = NULL;
1680 }
1681 }
1682
1683 if (cha_monomorphic_target != NULL) {
1684 if (!(target->is_final_method())) {
1685 // If we inlined because CHA revealed only a single target method,
1686 // then we are dependent on that target method not getting overridden
1687 // by dynamic class loading. Be sure to test the "static" receiver
1688 // dest_method here, as opposed to the actual receiver, which may
1689 // falsely lead us to believe that the receiver is final or private.
1690 dependency_recorder()->assert_unique_concrete_method(actual_recv, cha_monomorphic_target);
1691 }
1692 code = Bytecodes::_invokespecial;
1693 }
1694
1695 Value dynamic_receiver = NULL;
1696
1697 if (C1TypeProfileInlining &&
1698 code != Bytecodes::_invokestatic &&
1699 code != Bytecodes::_invokespecial &&
1700 code != Bytecodes::_invokedynamic &&
1701 (code != Bytecodes::_invokevirtual || !target->is_loaded() || !target->is_final_method())) {
1702
1703 ciInstanceKlass* k = method()->profile_single_hot_receiver(bci());
1704
1705 if (k != NULL) {
1706 // Profiling reveals a single klass at the call. Attempt inlining (with a guard).
1707 ValueStack* state_before = copy_state_before();
1708
1709 klass = k;
1710
1711 ciMethod* receiver_method = target->resolve_invoke(calling_klass, k);
1712
1713 assert(klass->is_loaded() && target->is_loaded(), "should be");
1714 assert(!target->is_static(), "should be");
1715
1716 int index = state()->stack_size() - (target->arg_size_no_receiver() + 1);
1717 Value receiver = state()->stack_at(index);
1718 // Insert a guard (specialized checkcast instruction)
1719 CheckCast* c = new CheckCast(k, receiver, state_before);
1720 c->set_profile_inlining();
1721 c->set_direct_compare(true);
1722 dynamic_receiver = append_split(c);
1723
1724 cha_monomorphic_target = receiver_method;
1725 code = Bytecodes::_invokespecial;
1726
1727 if (TraceC1ProfileInlining) {
1728 ttyLocker ttyl;
1729 tty->print("C1ProfileInlining: virtual method inlining ");
1730 _compilation->method()->print_short_name(tty);
1731 tty->print(" in ");
1732 method()->print_short_name(tty);
1733 tty->print(" at bci = %d", bci());
1734 tty->print(" to ");
1735 receiver_method->print_short_name(tty);
1736 tty->print(" expects class ");
1737 k->print_name();
1738 tty->cr();
1739 }
1740 }
1741 }
1742
1743 // check if we could do inlining
1744 bool do_profiling = false;
1745 bool inlining_attempted = false;
1746 if (!PatchALot && Inline && klass->is_loaded() &&
1747 (klass->is_initialized() || klass->is_interface() && target->holder()->is_initialized())
1748 && target->will_link(klass, callee_holder, code)) {
1749 // callee is known => check if we have static binding
1750 assert(target->is_loaded(), "callee must be known");
1751 if (code == Bytecodes::_invokestatic ||
1752 code == Bytecodes::_invokespecial ||
1753 code == Bytecodes::_invokevirtual && target->is_final_method() ||
1754 code == Bytecodes::_invokedynamic) {
1755 ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target;
1756 bool success = false;
1757 if (target->is_method_handle_invoke()) {
1758 // method handle invokes
1759 success = !is_invokedynamic ? for_method_handle_inline(target) : for_invokedynamic_inline(target);
1760 }
1761 if (!success) {
1762 // static binding => check if callee is ok
1763 inlining_attempted = true;
1764 success = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL), C1ProfileInlining ? &do_profiling : NULL, dynamic_receiver);
1765 }
1766 CHECK_BAILOUT();
1767
1768 #ifndef PRODUCT
1769 // printing
1770 if (PrintInlining && !success) {
1771 // if it was successfully inlined, then it was already printed.
1772 print_inline_result(inline_target, success);
1773 }
1774 #endif
1775 clear_inline_bailout();
1776 if (success) {
1777 // Register dependence if JVMTI has either breakpoint
1778 // setting or hotswapping of methods capabilities since they may
1779 // cause deoptimization.
1780 if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) {
1781 dependency_recorder()->assert_evol_method(inline_target);
1782 }
1783 return;
1784 }
1785 }
1786 }
1787
1788 if (C1TypeProfileInlining && (code == Bytecodes::_invokevirtual || code == Bytecodes::_invokeinterface) && !inlining_attempted) {
1789 if (method()->method_data()) {
1790 ciVirtualCallData* call = (ciVirtualCallData*)method()->method_data()->bci_to_data(bci())->as_VirtualCallData();
1791
1792 if (!call->is_hot() && !call->is_warm() && call->receiver(1) == NULL) {
1793 do_profiling = true;
1794
1795 if (TraceC1ProfileInlining) {
1796 ttyLocker ttyl;
1797 tty->print("C1ProfileInlining: virtual method profiling ");
1798 _compilation->method()->print_short_name(tty);
1799 tty->print(" in ");
1800 method()->print_short_name(tty);
1801 tty->print_cr(" at bci = %d", bci());
1802 }
1803 }
1804 }
1805 }
1806
1807 assert(!do_profiling || C1ProfileInlining, "profiling only if C1InlineFrequent");
1808
1809 // If we attempted an inline which did not succeed because of a
1810 // bailout during construction of the callee graph, the entire
1811 // compilation has to be aborted. This is fairly rare and currently
1812 // seems to only occur for jasm-generated classes which contain
1813 // jsr/ret pairs which are not associated with finally clauses and
1814 // do not have exception handlers in the containing method, and are
1815 // therefore not caught early enough to abort the inlining without
1816 // corrupting the graph. (We currently bail out with a non-empty
1817 // stack at a ret in these situations.)
1818 CHECK_BAILOUT();
1819
1820 // inlining not successful => standard invoke
1821 bool is_loaded = target->is_loaded();
1822 bool has_receiver =
1823 code == Bytecodes::_invokespecial ||
1824 code == Bytecodes::_invokevirtual ||
1825 code == Bytecodes::_invokeinterface;
1826 ValueType* result_type = as_ValueType(target->return_type());
1827
1828 // We require the debug info to be the "state before" because
1858
1859 if (is_profiling()) {
1860 if (recv != NULL && profile_calls()) {
1861 null_check(recv);
1862 }
1863 // Note that we'd collect profile data in this method if we wanted it.
1864 compilation()->set_would_profile(true);
1865
1866 if (profile_calls()) {
1867 assert(cha_monomorphic_target == NULL || exact_target == NULL, "both can not be set");
1868 ciKlass* target_klass = NULL;
1869 if (cha_monomorphic_target != NULL) {
1870 target_klass = cha_monomorphic_target->holder();
1871 } else if (exact_target != NULL) {
1872 target_klass = exact_target->holder();
1873 }
1874 profile_call(recv, target_klass);
1875 }
1876 }
1877
1878 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before, do_profiling);
1879 // push result
1880 append_split(result);
1881
1882 if (result_type != voidType) {
1883 if (method()->is_strict()) {
1884 push(result_type, round_fp(result));
1885 } else {
1886 push(result_type, result);
1887 }
1888 }
1889 }
1890
1891
1892 void GraphBuilder::new_instance(int klass_index) {
1893 ValueStack* state_before = copy_state_exhandling();
1894 bool will_link;
1895 ciKlass* klass = stream()->get_klass(will_link);
1896 assert(klass->is_instance_klass(), "must be an instance klass");
1897 NewInstance* new_instance = new NewInstance(klass->as_instance_klass(), state_before);
1898 _memory->new_instance(new_instance);
3091 if (_compilation->env()->jvmti_can_access_local_variables()) {
3092 s = state()->copy(ValueStack::ExceptionState, bci);
3093 } else {
3094 s = state()->copy(ValueStack::EmptyExceptionState, bci);
3095 }
3096 }
3097 return s;
3098 }
3099
3100 int GraphBuilder::recursive_inline_level(ciMethod* cur_callee) const {
3101 int recur_level = 0;
3102 for (IRScope* s = scope(); s != NULL; s = s->caller()) {
3103 if (s->method() == cur_callee) {
3104 ++recur_level;
3105 }
3106 }
3107 return recur_level;
3108 }
3109
3110
3111 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, bool* do_profiling, Value dynamic_receiver) {
3112 // Clear out any existing inline bailout condition
3113 clear_inline_bailout();
3114
3115 if (callee->should_exclude()) {
3116 // callee is excluded
3117 INLINE_BAILOUT("excluded by CompilerOracle")
3118 } else if (callee->should_not_inline()) {
3119 // callee is excluded
3120 INLINE_BAILOUT("disallowed by CompilerOracle")
3121 } else if (!callee->can_be_compiled()) {
3122 // callee is not compilable (prob. has breakpoints)
3123 INLINE_BAILOUT("not compilable (disabled)")
3124 } else if (callee->intrinsic_id() != vmIntrinsics::_none && try_inline_intrinsics(callee)) {
3125 // intrinsics can be native or not
3126 return true;
3127 } else if (callee->is_native()) {
3128 // non-intrinsic natives cannot be inlined
3129 INLINE_BAILOUT("non-intrinsic native")
3130 } else if (callee->is_abstract()) {
3131 INLINE_BAILOUT("abstract")
3132 } else {
3133 return try_inline_full(callee, holder_known, do_profiling, dynamic_receiver);
3134 }
3135 }
3136
3137
3138 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) {
3139 if (!InlineNatives ) INLINE_BAILOUT("intrinsic method inlining disabled");
3140 if (callee->is_synchronized()) {
3141 // We don't currently support any synchronized intrinsics
3142 return false;
3143 }
3144
3145 // callee seems like a good candidate
3146 // determine id
3147 bool preserves_state = false;
3148 bool cantrap = true;
3149 vmIntrinsics::ID id = callee->intrinsic_id();
3150 switch (id) {
3151 case vmIntrinsics::_arraycopy :
3152 if (!InlineArrayCopy) return false;
3153 break;
3462 if (!default_handler) {
3463 pop_scope();
3464 bci = _state->caller_state()->bci();
3465 _state = _state->caller_state()->copy_for_parsing();
3466 }
3467 }
3468
3469 // perform the throw as if at the the call site
3470 apush(exception);
3471 throw_op(bci);
3472
3473 BlockEnd* end = last()->as_BlockEnd();
3474 block()->set_end(end);
3475
3476 _block = orig_block;
3477 _state = orig_state;
3478 _last = orig_last;
3479 }
3480
3481
3482 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, bool* do_profiling, Value dynamic_receiver, BlockBegin* cont_block) {
3483 assert(!callee->is_native(), "callee must not be native");
3484 if (CompilationPolicy::policy()->should_not_inline(compilation()->env(), callee)) {
3485 INLINE_BAILOUT("inlining prohibited by policy");
3486 }
3487 // first perform tests of things it's not possible to inline
3488 if (callee->has_exception_handlers() &&
3489 !InlineMethodsWithExceptionHandlers) INLINE_BAILOUT("callee has exception handlers");
3490 if (callee->is_synchronized() &&
3491 !InlineSynchronizedMethods ) INLINE_BAILOUT("callee is synchronized");
3492 if (!callee->holder()->is_initialized()) INLINE_BAILOUT("callee's klass not initialized yet");
3493 if (!callee->has_balanced_monitors()) INLINE_BAILOUT("callee's monitors do not match");
3494
3495 // Proper inlining of methods with jsrs requires a little more work.
3496 if (callee->has_jsrs() ) INLINE_BAILOUT("jsrs not handled properly by inliner yet");
3497
3498 // When SSE2 is used on intel, then no special handling is needed
3499 // for strictfp because the enum-constant is fixed at compile time,
3500 // the check for UseSSE2 is needed here
3501 if (strict_fp_requires_explicit_rounding && UseSSE < 2 && method()->is_strict() != callee->is_strict()) {
3502 INLINE_BAILOUT("caller and callee have different strict fp requirements");
3503 }
3504
3505 if (is_profiling() && !callee->ensure_method_data()) {
3506 INLINE_BAILOUT("mdo allocation failed");
3507 }
3508
3509 // now perform tests that are based on flag settings
3510 if (callee->should_inline()) {
3511 // ignore heuristic controls on inlining
3512 } else {
3513 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("too-deep inlining");
3514 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("too-deep recursive inlining");
3515
3516 if (callee->code_size_for_inlining() > max_inline_size()) {
3517
3518 if (do_profiling != NULL && callee->code_size() <= C1ProfileInlineSize) {
3519 bool warm = false;
3520 bool do_inlining = method()->profile_is_hot(bci(), warm);
3521
3522 if (!do_inlining) {
3523 *do_profiling = !warm;
3524 INLINE_BAILOUT("callee is too large and too infrequent");
3525 } else if (TraceC1ProfileInlining) {
3526 ttyLocker ttyl;
3527 tty->print("C1ProfileInlining: method inlining of ");
3528 callee->print_short_name(tty);
3529 tty->print(" from ");
3530 method()->print_short_name(tty);
3531 tty->print(" at bci = %d", bci());
3532 tty->print(" in ");
3533 _compilation->method()->print_short_name(tty);
3534 tty->cr();
3535 }
3536
3537 } else {
3538 INLINE_BAILOUT(" callee is too large");
3539 }
3540 }
3541
3542 // don't inline throwable methods unless the inlining tree is rooted in a throwable class
3543 if (callee->name() == ciSymbol::object_initializer_name() &&
3544 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3545 // Throwable constructor call
3546 IRScope* top = scope();
3547 while (top->caller() != NULL) {
3548 top = top->caller();
3549 }
3550 if (!top->method()->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) {
3551 INLINE_BAILOUT("don't inline Throwable constructors");
3552 }
3553 }
3554
3555 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) {
3556 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit");
3557 }
3558 }
3559
3560 #ifndef PRODUCT
3618 // Record number of predecessors of continuation block before
3619 // inlining, to detect if inlined method has edges to its
3620 // continuation after inlining.
3621 int continuation_preds = cont->number_of_preds();
3622
3623 // Push callee scope
3624 push_scope(callee, cont);
3625
3626 // the BlockListBuilder for the callee could have bailed out
3627 CHECK_BAILOUT_(false);
3628
3629 // Temporarily set up bytecode stream so we can append instructions
3630 // (only using the bci of this stream)
3631 scope_data()->set_stream(scope_data()->parent()->stream());
3632
3633 // Pass parameters into callee state: add assignments
3634 // note: this will also ensure that all arguments are computed before being passed
3635 ValueStack* callee_state = state();
3636 ValueStack* caller_state = state()->caller_state();
3637 { int i = args_base;
3638 bool do_replace = dynamic_receiver != NULL;
3639 while (i < caller_state->stack_size()) {
3640 const int par_no = i - args_base;
3641 Value arg = caller_state->stack_at_inc(i);
3642 if (do_replace) {
3643 // Profiling data drove us to inline with a single expected
3644 // receiver class. Offer more accurate type information in
3645 // inlinee by replacing receiver with result of guard
3646 // instruction (= expected receiver class).
3647 arg = dynamic_receiver;
3648 do_replace = false;
3649 }
3650 // NOTE: take base() of arg->type() to avoid problems storing
3651 // constants
3652 store_local(callee_state, arg, arg->type()->base(), par_no);
3653 }
3654 }
3655
3656 // Remove args from stack.
3657 // Note that we preserve locals state in case we can use it later
3658 // (see use of pop_scope() below)
3659 caller_state->truncate_stack(args_base);
3660 assert(callee_state->stack_size() == 0, "callee stack must be empty");
3661
3662 Value lock;
3663 BlockBegin* sync_handler;
3664
3665 // Inline the locking of the receiver if the callee is synchronized
3666 if (callee->is_synchronized()) {
3667 lock = callee->is_static() ? append(new Constant(new InstanceConstant(callee->holder()->java_mirror())))
3668 : state()->local_at(0);
3669 sync_handler = new BlockBegin(SynchronizationEntryBCI);
3764 return true;
3765 }
3766
3767
3768 bool GraphBuilder::for_method_handle_inline(ciMethod* callee) {
3769 assert(!callee->is_static(), "change next line");
3770 int index = state()->stack_size() - (callee->arg_size_no_receiver() + 1);
3771 Value receiver = state()->stack_at(index);
3772
3773 if (receiver->type()->is_constant()) {
3774 ciMethodHandle* method_handle = receiver->type()->as_ObjectType()->constant_value()->as_method_handle();
3775
3776 // Set the callee to have access to the class and signature in
3777 // the MethodHandleCompiler.
3778 method_handle->set_callee(callee);
3779 method_handle->set_caller(method());
3780
3781 // Get an adapter for the MethodHandle.
3782 ciMethod* method_handle_adapter = method_handle->get_method_handle_adapter();
3783 if (method_handle_adapter != NULL) {
3784 return try_inline(method_handle_adapter, /*holder_known=*/ true, NULL, NULL);
3785 }
3786 } else if (receiver->as_CheckCast()) {
3787 // Match MethodHandle.selectAlternative idiom
3788 Phi* phi = receiver->as_CheckCast()->obj()->as_Phi();
3789
3790 if (phi != NULL && phi->operand_count() == 2) {
3791 // Get the two MethodHandle inputs from the Phi.
3792 Value op1 = phi->operand_at(0);
3793 Value op2 = phi->operand_at(1);
3794 ciMethodHandle* mh1 = op1->type()->as_ObjectType()->constant_value()->as_method_handle();
3795 ciMethodHandle* mh2 = op2->type()->as_ObjectType()->constant_value()->as_method_handle();
3796
3797 // Set the callee to have access to the class and signature in
3798 // the MethodHandleCompiler.
3799 mh1->set_callee(callee);
3800 mh1->set_caller(method());
3801 mh2->set_callee(callee);
3802 mh2->set_caller(method());
3803
3804 // Get adapters for the MethodHandles.
3807
3808 if (mh1_adapter != NULL && mh2_adapter != NULL) {
3809 set_inline_cleanup_info();
3810
3811 // Build the If guard
3812 BlockBegin* one = new BlockBegin(next_bci());
3813 BlockBegin* two = new BlockBegin(next_bci());
3814 BlockBegin* end = new BlockBegin(next_bci());
3815 Instruction* iff = append(new If(phi, If::eql, false, op1, one, two, NULL, false));
3816 block()->set_end(iff->as_BlockEnd());
3817
3818 // Connect up the states
3819 one->merge(block()->end()->state());
3820 two->merge(block()->end()->state());
3821
3822 // Save the state for the second inlinee
3823 ValueStack* state_before = copy_state_before();
3824
3825 // Parse first adapter
3826 _last = _block = one;
3827 if (!try_inline_full(mh1_adapter, /*holder_known=*/ true, NULL, NULL, end)) {
3828 restore_inline_cleanup_info();
3829 block()->clear_end(); // remove appended iff
3830 return false;
3831 }
3832
3833 // Parse second adapter
3834 _last = _block = two;
3835 _state = state_before;
3836 if (!try_inline_full(mh2_adapter, /*holder_known=*/ true, NULL, NULL, end)) {
3837 restore_inline_cleanup_info();
3838 block()->clear_end(); // remove appended iff
3839 return false;
3840 }
3841
3842 connect_to_end(end);
3843 return true;
3844 }
3845 }
3846 }
3847 return false;
3848 }
3849
3850
3851 bool GraphBuilder::for_invokedynamic_inline(ciMethod* callee) {
3852 // Get the MethodHandle from the CallSite.
3853 ciCallSite* call_site = stream()->get_call_site();
3854 ciMethodHandle* method_handle = call_site->get_target();
3855
3856 // Set the callee to have access to the class and signature in the
3857 // MethodHandleCompiler.
3858 method_handle->set_callee(callee);
3859 method_handle->set_caller(method());
3860
3861 // Get an adapter for the MethodHandle.
3862 ciMethod* method_handle_adapter = method_handle->get_invokedynamic_adapter();
3863 if (method_handle_adapter != NULL) {
3864 if (try_inline(method_handle_adapter, /*holder_known=*/ true, NULL, NULL)) {
3865 // Add a dependence for invalidation of the optimization.
3866 if (!call_site->is_constant_call_site()) {
3867 dependency_recorder()->assert_call_site_target_value(call_site, method_handle);
3868 }
3869 return true;
3870 }
3871 }
3872 return false;
3873 }
3874
3875
3876 void GraphBuilder::inline_bailout(const char* msg) {
3877 assert(msg != NULL, "inline bailout msg must exist");
3878 _inline_bailout_msg = msg;
3879 }
3880
3881
3882 void GraphBuilder::clear_inline_bailout() {
3883 _inline_bailout_msg = NULL;
3884 }
|