Mercurial > hg > graal-jvmci-8
comparison src/share/vm/c1/c1_GraphBuilder.cpp @ 6266:1d7922586cf6
7023639: JSR 292 method handle invocation needs a fast path for compiled code
6984705: JSR 292 method handle creation should not go through JNI
Summary: remove assembly code for JDK 7 chained method handles
Reviewed-by: jrose, twisti, kvn, mhaupt
Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>
author | twisti |
---|---|
date | Tue, 24 Jul 2012 10:51:00 -0700 |
parents | 6d8f36bcef55 |
children | 977007096840 |
comparison
equal
deleted
inserted
replaced
6241:aba91a731143 | 6266:1d7922586cf6 |
---|---|
29 #include "c1/c1_GraphBuilder.hpp" | 29 #include "c1/c1_GraphBuilder.hpp" |
30 #include "c1/c1_InstructionPrinter.hpp" | 30 #include "c1/c1_InstructionPrinter.hpp" |
31 #include "ci/ciCallSite.hpp" | 31 #include "ci/ciCallSite.hpp" |
32 #include "ci/ciField.hpp" | 32 #include "ci/ciField.hpp" |
33 #include "ci/ciKlass.hpp" | 33 #include "ci/ciKlass.hpp" |
34 #include "ci/ciMethodHandle.hpp" | 34 #include "ci/ciMemberName.hpp" |
35 #include "compiler/compileBroker.hpp" | 35 #include "compiler/compileBroker.hpp" |
36 #include "interpreter/bytecode.hpp" | 36 #include "interpreter/bytecode.hpp" |
37 #include "runtime/sharedRuntime.hpp" | 37 #include "runtime/sharedRuntime.hpp" |
38 #include "runtime/compilationPolicy.hpp" | 38 #include "runtime/compilationPolicy.hpp" |
39 #include "utilities/bitMap.inline.hpp" | 39 #include "utilities/bitMap.inline.hpp" |
912 } | 912 } |
913 | 913 |
914 | 914 |
915 void GraphBuilder::store_local(ValueType* type, int index) { | 915 void GraphBuilder::store_local(ValueType* type, int index) { |
916 Value x = pop(type); | 916 Value x = pop(type); |
917 store_local(state(), x, type, index); | 917 store_local(state(), x, index); |
918 } | 918 } |
919 | 919 |
920 | 920 |
921 void GraphBuilder::store_local(ValueStack* state, Value x, ValueType* type, int index) { | 921 void GraphBuilder::store_local(ValueStack* state, Value x, int index) { |
922 if (parsing_jsr()) { | 922 if (parsing_jsr()) { |
923 // We need to do additional tracking of the location of the return | 923 // We need to do additional tracking of the location of the return |
924 // address for jsrs since we don't handle arbitrary jsr/ret | 924 // address for jsrs since we don't handle arbitrary jsr/ret |
925 // constructs. Here we are figuring out in which circumstances we | 925 // constructs. Here we are figuring out in which circumstances we |
926 // need to bail out. | 926 // need to bail out. |
1533 BasicType field_type = field_val.basic_type(); | 1533 BasicType field_type = field_val.basic_type(); |
1534 switch (field_type) { | 1534 switch (field_type) { |
1535 case T_ARRAY: | 1535 case T_ARRAY: |
1536 case T_OBJECT: | 1536 case T_OBJECT: |
1537 if (field_val.as_object()->should_be_constant()) { | 1537 if (field_val.as_object()->should_be_constant()) { |
1538 constant = new Constant(as_ValueType(field_val)); | 1538 constant = new Constant(as_ValueType(field_val)); |
1539 } | 1539 } |
1540 break; | 1540 break; |
1541 | 1541 |
1542 default: | 1542 default: |
1543 constant = new Constant(as_ValueType(field_val)); | 1543 constant = new Constant(as_ValueType(field_val)); |
1560 state_before = copy_state_for_exception(); | 1560 state_before = copy_state_for_exception(); |
1561 } | 1561 } |
1562 append(new StoreField(append(obj), offset, field, val, true, state_before, needs_patching)); | 1562 append(new StoreField(append(obj), offset, field, val, true, state_before, needs_patching)); |
1563 } | 1563 } |
1564 break; | 1564 break; |
1565 case Bytecodes::_getfield : | 1565 case Bytecodes::_getfield: { |
1566 { | 1566 // Check for compile-time constants, i.e., trusted final non-static fields. |
1567 Instruction* constant = NULL; | |
1568 obj = apop(); | |
1569 ObjectType* obj_type = obj->type()->as_ObjectType(); | |
1570 if (obj_type->is_constant() && !PatchALot) { | |
1571 ciObject* const_oop = obj_type->constant_value(); | |
1572 if (field->is_constant()) { | |
1573 ciConstant field_val = field->constant_value_of(const_oop); | |
1574 BasicType field_type = field_val.basic_type(); | |
1575 switch (field_type) { | |
1576 case T_ARRAY: | |
1577 case T_OBJECT: | |
1578 if (field_val.as_object()->should_be_constant()) { | |
1579 constant = new Constant(as_ValueType(field_val)); | |
1580 } | |
1581 break; | |
1582 default: | |
1583 constant = new Constant(as_ValueType(field_val)); | |
1584 } | |
1585 } else { | |
1586 // For constant CallSites treat the target field as a compile time constant. | |
1587 if (const_oop->is_call_site()) { | |
1588 ciCallSite* call_site = const_oop->as_call_site(); | |
1589 if (field->is_call_site_target()) { | |
1590 ciMethodHandle* target = call_site->get_target(); | |
1591 if (target != NULL) { // just in case | |
1592 ciConstant field_val(T_OBJECT, target); | |
1593 constant = new Constant(as_ValueType(field_val)); | |
1594 // Add a dependence for invalidation of the optimization. | |
1595 if (!call_site->is_constant_call_site()) { | |
1596 dependency_recorder()->assert_call_site_target_value(call_site, target); | |
1597 } | |
1598 } | |
1599 } | |
1600 } | |
1601 } | |
1602 } | |
1603 if (constant != NULL) { | |
1604 push(type, append(constant)); | |
1605 } else { | |
1567 if (state_before == NULL) { | 1606 if (state_before == NULL) { |
1568 state_before = copy_state_for_exception(); | 1607 state_before = copy_state_for_exception(); |
1569 } | 1608 } |
1570 LoadField* load = new LoadField(apop(), offset, field, false, state_before, needs_patching); | 1609 LoadField* load = new LoadField(obj, offset, field, false, state_before, needs_patching); |
1571 Value replacement = !needs_patching ? _memory->load(load) : load; | 1610 Value replacement = !needs_patching ? _memory->load(load) : load; |
1572 if (replacement != load) { | 1611 if (replacement != load) { |
1573 assert(replacement->is_linked() || !replacement->can_be_linked(), "should already by linked"); | 1612 assert(replacement->is_linked() || !replacement->can_be_linked(), "should already by linked"); |
1574 push(type, replacement); | 1613 push(type, replacement); |
1575 } else { | 1614 } else { |
1576 push(type, append(load)); | 1615 push(type, append(load)); |
1577 } | 1616 } |
1578 break; | |
1579 } | |
1580 | |
1581 case Bytecodes::_putfield : | |
1582 { Value val = pop(type); | |
1583 if (state_before == NULL) { | |
1584 state_before = copy_state_for_exception(); | |
1585 } | |
1586 StoreField* store = new StoreField(apop(), offset, field, val, false, state_before, needs_patching); | |
1587 if (!needs_patching) store = _memory->store(store); | |
1588 if (store != NULL) { | |
1589 append(store); | |
1590 } | |
1591 } | 1617 } |
1592 break; | 1618 break; |
1593 default : | 1619 } |
1620 case Bytecodes::_putfield: { | |
1621 Value val = pop(type); | |
1622 obj = apop(); | |
1623 if (state_before == NULL) { | |
1624 state_before = copy_state_for_exception(); | |
1625 } | |
1626 StoreField* store = new StoreField(obj, offset, field, val, false, state_before, needs_patching); | |
1627 if (!needs_patching) store = _memory->store(store); | |
1628 if (store != NULL) { | |
1629 append(store); | |
1630 } | |
1631 break; | |
1632 } | |
1633 default: | |
1594 ShouldNotReachHere(); | 1634 ShouldNotReachHere(); |
1595 break; | 1635 break; |
1596 } | 1636 } |
1597 } | 1637 } |
1598 | 1638 |
1602 return compilation()->dependency_recorder(); | 1642 return compilation()->dependency_recorder(); |
1603 } | 1643 } |
1604 | 1644 |
1605 | 1645 |
1606 void GraphBuilder::invoke(Bytecodes::Code code) { | 1646 void GraphBuilder::invoke(Bytecodes::Code code) { |
1647 const bool has_receiver = | |
1648 code == Bytecodes::_invokespecial || | |
1649 code == Bytecodes::_invokevirtual || | |
1650 code == Bytecodes::_invokeinterface; | |
1651 const bool is_invokedynamic = (code == Bytecodes::_invokedynamic); | |
1652 | |
1607 bool will_link; | 1653 bool will_link; |
1608 ciMethod* target = stream()->get_method(will_link); | 1654 ciMethod* target = stream()->get_method(will_link); |
1655 ciKlass* holder = stream()->get_declared_method_holder(); | |
1656 const Bytecodes::Code bc_raw = stream()->cur_bc_raw(); | |
1657 | |
1658 // FIXME bail out for now | |
1659 if ((bc_raw == Bytecodes::_invokehandle || is_invokedynamic) && !will_link) { | |
1660 BAILOUT("unlinked call site (FIXME needs patching or recompile support)"); | |
1661 } | |
1662 | |
1609 // we have to make sure the argument size (incl. the receiver) | 1663 // we have to make sure the argument size (incl. the receiver) |
1610 // is correct for compilation (the call would fail later during | 1664 // is correct for compilation (the call would fail later during |
1611 // linkage anyway) - was bug (gri 7/28/99) | 1665 // linkage anyway) - was bug (gri 7/28/99) |
1612 if (target->is_loaded() && target->is_static() != (code == Bytecodes::_invokestatic)) BAILOUT("will cause link error"); | 1666 { |
1667 // Use raw to get rewritten bytecode. | |
1668 const bool is_invokestatic = bc_raw == Bytecodes::_invokestatic; | |
1669 const bool allow_static = | |
1670 is_invokestatic || | |
1671 bc_raw == Bytecodes::_invokehandle || | |
1672 bc_raw == Bytecodes::_invokedynamic; | |
1673 if (target->is_loaded()) { | |
1674 if (( target->is_static() && !allow_static) || | |
1675 (!target->is_static() && is_invokestatic)) { | |
1676 BAILOUT("will cause link error"); | |
1677 } | |
1678 } | |
1679 } | |
1613 ciInstanceKlass* klass = target->holder(); | 1680 ciInstanceKlass* klass = target->holder(); |
1614 | 1681 |
1615 // check if CHA possible: if so, change the code to invoke_special | 1682 // check if CHA possible: if so, change the code to invoke_special |
1616 ciInstanceKlass* calling_klass = method()->holder(); | 1683 ciInstanceKlass* calling_klass = method()->holder(); |
1617 ciKlass* holder = stream()->get_declared_method_holder(); | |
1618 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder); | 1684 ciInstanceKlass* callee_holder = ciEnv::get_instance_klass_for_declared_method_holder(holder); |
1619 ciInstanceKlass* actual_recv = callee_holder; | 1685 ciInstanceKlass* actual_recv = callee_holder; |
1620 | 1686 |
1621 // some methods are obviously bindable without any type checks so | 1687 // Some methods are obviously bindable without any type checks so |
1622 // convert them directly to an invokespecial. | 1688 // convert them directly to an invokespecial or invokestatic. |
1623 if (target->is_loaded() && !target->is_abstract() && | 1689 if (target->is_loaded() && !target->is_abstract() && target->can_be_statically_bound()) { |
1624 target->can_be_statically_bound() && code == Bytecodes::_invokevirtual) { | 1690 switch (bc_raw) { |
1625 code = Bytecodes::_invokespecial; | 1691 case Bytecodes::_invokevirtual: code = Bytecodes::_invokespecial; break; |
1626 } | 1692 case Bytecodes::_invokehandle: code = Bytecodes::_invokestatic; break; |
1627 | 1693 } |
1628 bool is_invokedynamic = code == Bytecodes::_invokedynamic; | 1694 } |
1695 | |
1696 // Push appendix argument (MethodType, CallSite, etc.), if one. | |
1697 if (stream()->has_appendix()) { | |
1698 ciObject* appendix = stream()->get_appendix(); | |
1699 Value arg = append(new Constant(new ObjectConstant(appendix))); | |
1700 apush(arg); | |
1701 } | |
1629 | 1702 |
1630 // NEEDS_CLEANUP | 1703 // NEEDS_CLEANUP |
1631 // I've added the target-is_loaded() test below but I don't really understand | 1704 // I've added the target->is_loaded() test below but I don't really understand |
1632 // how klass->is_loaded() can be true and yet target->is_loaded() is false. | 1705 // how klass->is_loaded() can be true and yet target->is_loaded() is false. |
1633 // this happened while running the JCK invokevirtual tests under doit. TKR | 1706 // this happened while running the JCK invokevirtual tests under doit. TKR |
1634 ciMethod* cha_monomorphic_target = NULL; | 1707 ciMethod* cha_monomorphic_target = NULL; |
1635 ciMethod* exact_target = NULL; | 1708 ciMethod* exact_target = NULL; |
1636 Value better_receiver = NULL; | 1709 Value better_receiver = NULL; |
1637 if (UseCHA && DeoptC1 && klass->is_loaded() && target->is_loaded() && | 1710 if (UseCHA && DeoptC1 && klass->is_loaded() && target->is_loaded() && |
1638 !target->is_method_handle_invoke()) { | 1711 !(// %%% FIXME: Are both of these relevant? |
1712 target->is_method_handle_intrinsic() || | |
1713 target->is_compiled_lambda_form())) { | |
1639 Value receiver = NULL; | 1714 Value receiver = NULL; |
1640 ciInstanceKlass* receiver_klass = NULL; | 1715 ciInstanceKlass* receiver_klass = NULL; |
1641 bool type_is_exact = false; | 1716 bool type_is_exact = false; |
1642 // try to find a precise receiver type | 1717 // try to find a precise receiver type |
1643 if (will_link && !target->is_static()) { | 1718 if (will_link && !target->is_static()) { |
1759 code == Bytecodes::_invokespecial || | 1834 code == Bytecodes::_invokespecial || |
1760 code == Bytecodes::_invokevirtual && target->is_final_method() || | 1835 code == Bytecodes::_invokevirtual && target->is_final_method() || |
1761 code == Bytecodes::_invokedynamic) { | 1836 code == Bytecodes::_invokedynamic) { |
1762 ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target; | 1837 ciMethod* inline_target = (cha_monomorphic_target != NULL) ? cha_monomorphic_target : target; |
1763 bool success = false; | 1838 bool success = false; |
1764 if (target->is_method_handle_invoke()) { | 1839 if (target->is_method_handle_intrinsic()) { |
1765 // method handle invokes | 1840 // method handle invokes |
1766 success = !is_invokedynamic ? for_method_handle_inline(target) : for_invokedynamic_inline(target); | 1841 success = for_method_handle_inline(target); |
1767 } | 1842 } else { |
1768 if (!success) { | |
1769 // static binding => check if callee is ok | 1843 // static binding => check if callee is ok |
1770 success = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL), better_receiver); | 1844 success = try_inline(inline_target, (cha_monomorphic_target != NULL) || (exact_target != NULL), code, better_receiver); |
1771 } | 1845 } |
1772 CHECK_BAILOUT(); | 1846 CHECK_BAILOUT(); |
1773 | 1847 |
1774 #ifndef PRODUCT | |
1775 // printing | |
1776 if (PrintInlining && !success) { | |
1777 // if it was successfully inlined, then it was already printed. | |
1778 print_inline_result(inline_target, success); | |
1779 } | |
1780 #endif | |
1781 clear_inline_bailout(); | 1848 clear_inline_bailout(); |
1782 if (success) { | 1849 if (success) { |
1783 // Register dependence if JVMTI has either breakpoint | 1850 // Register dependence if JVMTI has either breakpoint |
1784 // setting or hotswapping of methods capabilities since they may | 1851 // setting or hotswapping of methods capabilities since they may |
1785 // cause deoptimization. | 1852 // cause deoptimization. |
1786 if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) { | 1853 if (compilation()->env()->jvmti_can_hotswap_or_post_breakpoint()) { |
1787 dependency_recorder()->assert_evol_method(inline_target); | 1854 dependency_recorder()->assert_evol_method(inline_target); |
1788 } | 1855 } |
1789 return; | 1856 return; |
1790 } | 1857 } |
1791 } | 1858 } else { |
1792 } | 1859 print_inlining(target, "no static binding", /*success*/ false); |
1860 } | |
1861 } else { | |
1862 print_inlining(target, "not inlineable", /*success*/ false); | |
1863 } | |
1864 | |
1793 // If we attempted an inline which did not succeed because of a | 1865 // If we attempted an inline which did not succeed because of a |
1794 // bailout during construction of the callee graph, the entire | 1866 // bailout during construction of the callee graph, the entire |
1795 // compilation has to be aborted. This is fairly rare and currently | 1867 // compilation has to be aborted. This is fairly rare and currently |
1796 // seems to only occur for jasm-generated classes which contain | 1868 // seems to only occur for jasm-generated classes which contain |
1797 // jsr/ret pairs which are not associated with finally clauses and | 1869 // jsr/ret pairs which are not associated with finally clauses and |
1801 // stack at a ret in these situations.) | 1873 // stack at a ret in these situations.) |
1802 CHECK_BAILOUT(); | 1874 CHECK_BAILOUT(); |
1803 | 1875 |
1804 // inlining not successful => standard invoke | 1876 // inlining not successful => standard invoke |
1805 bool is_loaded = target->is_loaded(); | 1877 bool is_loaded = target->is_loaded(); |
1806 bool has_receiver = | |
1807 code == Bytecodes::_invokespecial || | |
1808 code == Bytecodes::_invokevirtual || | |
1809 code == Bytecodes::_invokeinterface; | |
1810 ValueType* result_type = as_ValueType(target->return_type()); | 1878 ValueType* result_type = as_ValueType(target->return_type()); |
1811 | 1879 |
1812 // We require the debug info to be the "state before" because | 1880 // We require the debug info to be the "state before" because |
1813 // invokedynamics may deoptimize. | 1881 // invokedynamics may deoptimize. |
1814 ValueStack* state_before = is_invokedynamic ? copy_state_before() : copy_state_exhandling(); | 1882 ValueStack* state_before = is_invokedynamic ? copy_state_before() : copy_state_exhandling(); |
1853 if (cha_monomorphic_target != NULL) { | 1921 if (cha_monomorphic_target != NULL) { |
1854 target_klass = cha_monomorphic_target->holder(); | 1922 target_klass = cha_monomorphic_target->holder(); |
1855 } else if (exact_target != NULL) { | 1923 } else if (exact_target != NULL) { |
1856 target_klass = exact_target->holder(); | 1924 target_klass = exact_target->holder(); |
1857 } | 1925 } |
1858 profile_call(recv, target_klass); | 1926 profile_call(target, recv, target_klass); |
1859 } | 1927 } |
1860 } | 1928 } |
1861 | 1929 |
1862 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before); | 1930 Invoke* result = new Invoke(code, result_type, recv, args, vtable_index, target, state_before); |
1863 // push result | 1931 // push result |
3095 } | 3163 } |
3096 return recur_level; | 3164 return recur_level; |
3097 } | 3165 } |
3098 | 3166 |
3099 | 3167 |
3100 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, Value receiver) { | 3168 bool GraphBuilder::try_inline(ciMethod* callee, bool holder_known, Bytecodes::Code bc, Value receiver) { |
3101 // Clear out any existing inline bailout condition | 3169 const char* msg = NULL; |
3170 | |
3171 // clear out any existing inline bailout condition | |
3102 clear_inline_bailout(); | 3172 clear_inline_bailout(); |
3103 | 3173 |
3104 if (callee->should_exclude()) { | 3174 // exclude methods we don't want to inline |
3105 // callee is excluded | 3175 msg = should_not_inline(callee); |
3106 INLINE_BAILOUT("excluded by CompilerOracle") | 3176 if (msg != NULL) { |
3107 } else if (callee->should_not_inline()) { | 3177 print_inlining(callee, msg, /*success*/ false); |
3108 // callee is excluded | 3178 return false; |
3109 INLINE_BAILOUT("disallowed by CompilerOracle") | 3179 } |
3110 } else if (!callee->can_be_compiled()) { | 3180 |
3111 // callee is not compilable (prob. has breakpoints) | 3181 // handle intrinsics |
3112 INLINE_BAILOUT("not compilable (disabled)") | 3182 if (callee->intrinsic_id() != vmIntrinsics::_none) { |
3113 } else if (callee->intrinsic_id() != vmIntrinsics::_none && try_inline_intrinsics(callee)) { | 3183 if (try_inline_intrinsics(callee)) { |
3114 // intrinsics can be native or not | 3184 print_inlining(callee, "intrinsic"); |
3185 return true; | |
3186 } | |
3187 // try normal inlining | |
3188 } | |
3189 | |
3190 // certain methods cannot be parsed at all | |
3191 msg = check_can_parse(callee); | |
3192 if (msg != NULL) { | |
3193 print_inlining(callee, msg, /*success*/ false); | |
3194 return false; | |
3195 } | |
3196 | |
3197 // If bytecode not set use the current one. | |
3198 if (bc == Bytecodes::_illegal) { | |
3199 bc = code(); | |
3200 } | |
3201 if (try_inline_full(callee, holder_known, bc, receiver)) | |
3115 return true; | 3202 return true; |
3116 } else if (callee->is_native()) { | 3203 print_inlining(callee, _inline_bailout_msg, /*success*/ false); |
3117 // non-intrinsic natives cannot be inlined | 3204 return false; |
3118 INLINE_BAILOUT("non-intrinsic native") | 3205 } |
3119 } else if (callee->is_abstract()) { | 3206 |
3120 INLINE_BAILOUT("abstract") | 3207 |
3121 } else { | 3208 const char* GraphBuilder::check_can_parse(ciMethod* callee) const { |
3122 return try_inline_full(callee, holder_known, NULL, receiver); | 3209 // Certain methods cannot be parsed at all: |
3123 } | 3210 if ( callee->is_native()) return "native method"; |
3211 if ( callee->is_abstract()) return "abstract method"; | |
3212 if (!callee->can_be_compiled()) return "not compilable (disabled)"; | |
3213 return NULL; | |
3214 } | |
3215 | |
3216 | |
3217 // negative filter: should callee NOT be inlined? returns NULL, ok to inline, or rejection msg | |
3218 const char* GraphBuilder::should_not_inline(ciMethod* callee) const { | |
3219 if ( callee->should_exclude()) return "excluded by CompilerOracle"; | |
3220 if ( callee->should_not_inline()) return "disallowed by CompilerOracle"; | |
3221 if ( callee->dont_inline()) return "don't inline by annotation"; | |
3222 return NULL; | |
3124 } | 3223 } |
3125 | 3224 |
3126 | 3225 |
3127 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) { | 3226 bool GraphBuilder::try_inline_intrinsics(ciMethod* callee) { |
3128 if (!InlineNatives ) INLINE_BAILOUT("intrinsic method inlining disabled"); | 3227 if (!InlineNatives ) INLINE_BAILOUT("intrinsic method inlining disabled"); |
3302 Value recv = NULL; | 3401 Value recv = NULL; |
3303 if (has_receiver) { | 3402 if (has_receiver) { |
3304 recv = args->at(0); | 3403 recv = args->at(0); |
3305 null_check(recv); | 3404 null_check(recv); |
3306 } | 3405 } |
3307 profile_call(recv, NULL); | 3406 profile_call(callee, recv, NULL); |
3308 } | 3407 } |
3309 } | 3408 } |
3310 } | 3409 } |
3311 | 3410 |
3312 Intrinsic* result = new Intrinsic(result_type, id, args, has_receiver, state_before, | 3411 Intrinsic* result = new Intrinsic(result_type, id, args, has_receiver, state_before, |
3313 preserves_state, cantrap); | 3412 preserves_state, cantrap); |
3314 // append instruction & push result | 3413 // append instruction & push result |
3315 Value value = append_split(result); | 3414 Value value = append_split(result); |
3316 if (result_type != voidType) push(result_type, value); | 3415 if (result_type != voidType) push(result_type, value); |
3317 | |
3318 #ifndef PRODUCT | |
3319 // printing | |
3320 if (PrintInlining) { | |
3321 print_inline_result(callee, true); | |
3322 } | |
3323 #endif | |
3324 | 3416 |
3325 // done | 3417 // done |
3326 return true; | 3418 return true; |
3327 } | 3419 } |
3328 | 3420 |
3475 _state = orig_state; | 3567 _state = orig_state; |
3476 _last = orig_last; | 3568 _last = orig_last; |
3477 } | 3569 } |
3478 | 3570 |
3479 | 3571 |
3480 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, BlockBegin* cont_block, Value receiver) { | 3572 bool GraphBuilder::try_inline_full(ciMethod* callee, bool holder_known, Bytecodes::Code bc, Value receiver) { |
3481 assert(!callee->is_native(), "callee must not be native"); | 3573 assert(!callee->is_native(), "callee must not be native"); |
3482 if (CompilationPolicy::policy()->should_not_inline(compilation()->env(), callee)) { | 3574 if (CompilationPolicy::policy()->should_not_inline(compilation()->env(), callee)) { |
3483 INLINE_BAILOUT("inlining prohibited by policy"); | 3575 INLINE_BAILOUT("inlining prohibited by policy"); |
3484 } | 3576 } |
3485 // first perform tests of things it's not possible to inline | 3577 // first perform tests of things it's not possible to inline |
3506 | 3598 |
3507 // now perform tests that are based on flag settings | 3599 // now perform tests that are based on flag settings |
3508 if (callee->force_inline() || callee->should_inline()) { | 3600 if (callee->force_inline() || callee->should_inline()) { |
3509 // ignore heuristic controls on inlining | 3601 // ignore heuristic controls on inlining |
3510 if (callee->force_inline()) | 3602 if (callee->force_inline()) |
3511 CompileTask::print_inlining(callee, scope()->level(), bci(), "force inline by annotation"); | 3603 print_inlining(callee, "force inline by annotation"); |
3512 } else { | 3604 } else { |
3513 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("too-deep inlining"); | 3605 if (inline_level() > MaxInlineLevel ) INLINE_BAILOUT("inlining too deep"); |
3514 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("too-deep recursive inlining"); | 3606 if (recursive_inline_level(callee) > MaxRecursiveInlineLevel) INLINE_BAILOUT("recursive inlining too deep"); |
3515 if (callee->code_size_for_inlining() > max_inline_size() ) INLINE_BAILOUT("callee is too large"); | 3607 if (callee->code_size_for_inlining() > max_inline_size() ) INLINE_BAILOUT("callee is too large"); |
3516 | 3608 |
3517 // don't inline throwable methods unless the inlining tree is rooted in a throwable class | 3609 // don't inline throwable methods unless the inlining tree is rooted in a throwable class |
3518 if (callee->name() == ciSymbol::object_initializer_name() && | 3610 if (callee->name() == ciSymbol::object_initializer_name() && |
3519 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) { | 3611 callee->holder()->is_subclass_of(ciEnv::current()->Throwable_klass())) { |
3528 } | 3620 } |
3529 | 3621 |
3530 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) { | 3622 if (compilation()->env()->num_inlined_bytecodes() > DesiredMethodLimit) { |
3531 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit"); | 3623 INLINE_BAILOUT("total inlining greater than DesiredMethodLimit"); |
3532 } | 3624 } |
3533 } | 3625 // printing |
3534 | 3626 print_inlining(callee, ""); |
3535 #ifndef PRODUCT | 3627 } |
3536 // printing | |
3537 if (PrintInlining) { | |
3538 print_inline_result(callee, true); | |
3539 } | |
3540 #endif | |
3541 | 3628 |
3542 // NOTE: Bailouts from this point on, which occur at the | 3629 // NOTE: Bailouts from this point on, which occur at the |
3543 // GraphBuilder level, do not cause bailout just of the inlining but | 3630 // GraphBuilder level, do not cause bailout just of the inlining but |
3544 // in fact of the entire compilation. | 3631 // in fact of the entire compilation. |
3545 | 3632 |
3546 BlockBegin* orig_block = block(); | 3633 BlockBegin* orig_block = block(); |
3547 | 3634 |
3635 const bool is_invokedynamic = bc == Bytecodes::_invokedynamic; | |
3636 const bool has_receiver = (bc != Bytecodes::_invokestatic && !is_invokedynamic); | |
3637 | |
3548 const int args_base = state()->stack_size() - callee->arg_size(); | 3638 const int args_base = state()->stack_size() - callee->arg_size(); |
3549 assert(args_base >= 0, "stack underflow during inlining"); | 3639 assert(args_base >= 0, "stack underflow during inlining"); |
3550 | 3640 |
3551 // Insert null check if necessary | 3641 // Insert null check if necessary |
3552 Value recv = NULL; | 3642 Value recv = NULL; |
3553 if (code() != Bytecodes::_invokestatic && | 3643 if (has_receiver) { |
3554 code() != Bytecodes::_invokedynamic) { | |
3555 // note: null check must happen even if first instruction of callee does | 3644 // note: null check must happen even if first instruction of callee does |
3556 // an implicit null check since the callee is in a different scope | 3645 // an implicit null check since the callee is in a different scope |
3557 // and we must make sure exception handling does the right thing | 3646 // and we must make sure exception handling does the right thing |
3558 assert(!callee->is_static(), "callee must not be static"); | 3647 assert(!callee->is_static(), "callee must not be static"); |
3559 assert(callee->arg_size() > 0, "must have at least a receiver"); | 3648 assert(callee->arg_size() > 0, "must have at least a receiver"); |
3565 // Note that we'd collect profile data in this method if we wanted it. | 3654 // Note that we'd collect profile data in this method if we wanted it. |
3566 // this may be redundant here... | 3655 // this may be redundant here... |
3567 compilation()->set_would_profile(true); | 3656 compilation()->set_would_profile(true); |
3568 | 3657 |
3569 if (profile_calls()) { | 3658 if (profile_calls()) { |
3570 profile_call(recv, holder_known ? callee->holder() : NULL); | 3659 profile_call(callee, recv, holder_known ? callee->holder() : NULL); |
3571 } | 3660 } |
3572 } | 3661 } |
3573 | 3662 |
3574 // Introduce a new callee continuation point - if the callee has | 3663 // Introduce a new callee continuation point - if the callee has |
3575 // more than one return instruction or the return does not allow | 3664 // more than one return instruction or the return does not allow |
3576 // fall-through of control flow, all return instructions of the | 3665 // fall-through of control flow, all return instructions of the |
3577 // callee will need to be replaced by Goto's pointing to this | 3666 // callee will need to be replaced by Goto's pointing to this |
3578 // continuation point. | 3667 // continuation point. |
3579 BlockBegin* cont = cont_block != NULL ? cont_block : block_at(next_bci()); | 3668 BlockBegin* cont = block_at(next_bci()); |
3580 bool continuation_existed = true; | 3669 bool continuation_existed = true; |
3581 if (cont == NULL) { | 3670 if (cont == NULL) { |
3582 cont = new BlockBegin(next_bci()); | 3671 cont = new BlockBegin(next_bci()); |
3583 // low number so that continuation gets parsed as early as possible | 3672 // low number so that continuation gets parsed as early as possible |
3584 cont->set_depth_first_number(0); | 3673 cont->set_depth_first_number(0); |
3607 | 3696 |
3608 // Pass parameters into callee state: add assignments | 3697 // Pass parameters into callee state: add assignments |
3609 // note: this will also ensure that all arguments are computed before being passed | 3698 // note: this will also ensure that all arguments are computed before being passed |
3610 ValueStack* callee_state = state(); | 3699 ValueStack* callee_state = state(); |
3611 ValueStack* caller_state = state()->caller_state(); | 3700 ValueStack* caller_state = state()->caller_state(); |
3612 { int i = args_base; | 3701 for (int i = args_base; i < caller_state->stack_size(); ) { |
3613 while (i < caller_state->stack_size()) { | 3702 const int arg_no = i - args_base; |
3614 const int par_no = i - args_base; | 3703 Value arg = caller_state->stack_at_inc(i); |
3615 Value arg = caller_state->stack_at_inc(i); | 3704 store_local(callee_state, arg, arg_no); |
3616 // NOTE: take base() of arg->type() to avoid problems storing | |
3617 // constants | |
3618 if (receiver != NULL && par_no == 0) { | |
3619 arg = receiver; | |
3620 } | |
3621 store_local(callee_state, arg, arg->type()->base(), par_no); | |
3622 } | |
3623 } | 3705 } |
3624 | 3706 |
3625 // Remove args from stack. | 3707 // Remove args from stack. |
3626 // Note that we preserve locals state in case we can use it later | 3708 // Note that we preserve locals state in case we can use it later |
3627 // (see use of pop_scope() below) | 3709 // (see use of pop_scope() below) |
3693 // off the Goto to the continuation, allowing control to fall | 3775 // off the Goto to the continuation, allowing control to fall |
3694 // through back into the caller block and effectively performing | 3776 // through back into the caller block and effectively performing |
3695 // block merging. This allows load elimination and CSE to take place | 3777 // block merging. This allows load elimination and CSE to take place |
3696 // across multiple callee scopes if they are relatively simple, and | 3778 // across multiple callee scopes if they are relatively simple, and |
3697 // is currently essential to making inlining profitable. | 3779 // is currently essential to making inlining profitable. |
3698 if (cont_block == NULL) { | 3780 if (num_returns() == 1 |
3699 if (num_returns() == 1 | 3781 && block() == orig_block |
3700 && block() == orig_block | 3782 && block() == inline_cleanup_block()) { |
3701 && block() == inline_cleanup_block()) { | 3783 _last = inline_cleanup_return_prev(); |
3702 _last = inline_cleanup_return_prev(); | 3784 _state = inline_cleanup_state(); |
3703 _state = inline_cleanup_state(); | 3785 } else if (continuation_preds == cont->number_of_preds()) { |
3704 } else if (continuation_preds == cont->number_of_preds()) { | 3786 // Inlining caused that the instructions after the invoke in the |
3705 // Inlining caused that the instructions after the invoke in the | 3787 // caller are not reachable any more. So skip filling this block |
3706 // caller are not reachable any more. So skip filling this block | 3788 // with instructions! |
3707 // with instructions! | 3789 assert(cont == continuation(), ""); |
3708 assert(cont == continuation(), ""); | 3790 assert(_last && _last->as_BlockEnd(), ""); |
3791 _skip_block = true; | |
3792 } else { | |
3793 // Resume parsing in continuation block unless it was already parsed. | |
3794 // Note that if we don't change _last here, iteration in | |
3795 // iterate_bytecodes_for_block will stop when we return. | |
3796 if (!continuation()->is_set(BlockBegin::was_visited_flag)) { | |
3797 // add continuation to work list instead of parsing it immediately | |
3709 assert(_last && _last->as_BlockEnd(), ""); | 3798 assert(_last && _last->as_BlockEnd(), ""); |
3799 scope_data()->parent()->add_to_work_list(continuation()); | |
3710 _skip_block = true; | 3800 _skip_block = true; |
3711 } else { | |
3712 // Resume parsing in continuation block unless it was already parsed. | |
3713 // Note that if we don't change _last here, iteration in | |
3714 // iterate_bytecodes_for_block will stop when we return. | |
3715 if (!continuation()->is_set(BlockBegin::was_visited_flag)) { | |
3716 // add continuation to work list instead of parsing it immediately | |
3717 assert(_last && _last->as_BlockEnd(), ""); | |
3718 scope_data()->parent()->add_to_work_list(continuation()); | |
3719 _skip_block = true; | |
3720 } | |
3721 } | 3801 } |
3722 } | 3802 } |
3723 | 3803 |
3724 // Fill the exception handler for synchronized methods with instructions | 3804 // Fill the exception handler for synchronized methods with instructions |
3725 if (callee->is_synchronized() && sync_handler->state() != NULL) { | 3805 if (callee->is_synchronized() && sync_handler->state() != NULL) { |
3733 return true; | 3813 return true; |
3734 } | 3814 } |
3735 | 3815 |
3736 | 3816 |
3737 bool GraphBuilder::for_method_handle_inline(ciMethod* callee) { | 3817 bool GraphBuilder::for_method_handle_inline(ciMethod* callee) { |
3738 assert(!callee->is_static(), "change next line"); | 3818 ValueStack* state_before = state()->copy_for_parsing(); |
3739 int index = state()->stack_size() - (callee->arg_size_no_receiver() + 1); | 3819 vmIntrinsics::ID iid = callee->intrinsic_id(); |
3740 Value receiver = state()->stack_at(index); | 3820 switch (iid) { |
3741 | 3821 case vmIntrinsics::_invokeBasic: |
3742 if (receiver->type()->is_constant()) { | 3822 { |
3743 ciMethodHandle* method_handle = receiver->type()->as_ObjectType()->constant_value()->as_method_handle(); | 3823 // get MethodHandle receiver |
3744 | 3824 const int args_base = state()->stack_size() - callee->arg_size(); |
3745 // Set the callee to have access to the class and signature in | 3825 ValueType* type = state()->stack_at(args_base)->type(); |
3746 // the MethodHandleCompiler. | 3826 if (type->is_constant()) { |
3747 method_handle->set_callee(callee); | 3827 ciMethod* target = type->as_ObjectType()->constant_value()->as_method_handle()->get_vmtarget(); |
3748 method_handle->set_caller(method()); | 3828 guarantee(!target->is_method_handle_intrinsic(), "should not happen"); // XXX remove |
3749 | 3829 Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual; |
3750 // Get an adapter for the MethodHandle. | 3830 if (try_inline(target, /*holder_known*/ true, bc)) { |
3751 ciMethod* method_handle_adapter = method_handle->get_method_handle_adapter(); | |
3752 if (method_handle_adapter != NULL) { | |
3753 return try_inline(method_handle_adapter, /*holder_known=*/ true); | |
3754 } | |
3755 } else if (receiver->as_CheckCast()) { | |
3756 // Match MethodHandle.selectAlternative idiom | |
3757 Phi* phi = receiver->as_CheckCast()->obj()->as_Phi(); | |
3758 | |
3759 if (phi != NULL && phi->operand_count() == 2) { | |
3760 // Get the two MethodHandle inputs from the Phi. | |
3761 Value op1 = phi->operand_at(0); | |
3762 Value op2 = phi->operand_at(1); | |
3763 ObjectType* op1type = op1->type()->as_ObjectType(); | |
3764 ObjectType* op2type = op2->type()->as_ObjectType(); | |
3765 | |
3766 if (op1type->is_constant() && op2type->is_constant()) { | |
3767 ciMethodHandle* mh1 = op1type->constant_value()->as_method_handle(); | |
3768 ciMethodHandle* mh2 = op2type->constant_value()->as_method_handle(); | |
3769 | |
3770 // Set the callee to have access to the class and signature in | |
3771 // the MethodHandleCompiler. | |
3772 mh1->set_callee(callee); | |
3773 mh1->set_caller(method()); | |
3774 mh2->set_callee(callee); | |
3775 mh2->set_caller(method()); | |
3776 | |
3777 // Get adapters for the MethodHandles. | |
3778 ciMethod* mh1_adapter = mh1->get_method_handle_adapter(); | |
3779 ciMethod* mh2_adapter = mh2->get_method_handle_adapter(); | |
3780 | |
3781 if (mh1_adapter != NULL && mh2_adapter != NULL) { | |
3782 set_inline_cleanup_info(); | |
3783 | |
3784 // Build the If guard | |
3785 BlockBegin* one = new BlockBegin(next_bci()); | |
3786 BlockBegin* two = new BlockBegin(next_bci()); | |
3787 BlockBegin* end = new BlockBegin(next_bci()); | |
3788 Instruction* iff = append(new If(phi, If::eql, false, op1, one, two, NULL, false)); | |
3789 block()->set_end(iff->as_BlockEnd()); | |
3790 | |
3791 // Connect up the states | |
3792 one->merge(block()->end()->state()); | |
3793 two->merge(block()->end()->state()); | |
3794 | |
3795 // Save the state for the second inlinee | |
3796 ValueStack* state_before = copy_state_before(); | |
3797 | |
3798 // Parse first adapter | |
3799 _last = _block = one; | |
3800 if (!try_inline_full(mh1_adapter, /*holder_known=*/ true, end, NULL)) { | |
3801 restore_inline_cleanup_info(); | |
3802 block()->clear_end(); // remove appended iff | |
3803 return false; | |
3804 } | |
3805 | |
3806 // Parse second adapter | |
3807 _last = _block = two; | |
3808 _state = state_before; | |
3809 if (!try_inline_full(mh2_adapter, /*holder_known=*/ true, end, NULL)) { | |
3810 restore_inline_cleanup_info(); | |
3811 block()->clear_end(); // remove appended iff | |
3812 return false; | |
3813 } | |
3814 | |
3815 connect_to_end(end); | |
3816 return true; | 3831 return true; |
3817 } | 3832 } |
3818 } | 3833 } else { |
3819 } | 3834 print_inlining(callee, "receiver not constant", /*success*/ false); |
3820 } | 3835 } |
3821 return false; | 3836 } |
3822 } | 3837 break; |
3823 | 3838 |
3824 | 3839 case vmIntrinsics::_linkToVirtual: |
3825 bool GraphBuilder::for_invokedynamic_inline(ciMethod* callee) { | 3840 case vmIntrinsics::_linkToStatic: |
3826 // Get the MethodHandle from the CallSite. | 3841 case vmIntrinsics::_linkToSpecial: |
3827 ciCallSite* call_site = stream()->get_call_site(); | 3842 case vmIntrinsics::_linkToInterface: |
3828 ciMethodHandle* method_handle = call_site->get_target(); | 3843 { |
3829 | 3844 // pop MemberName argument |
3830 // Set the callee to have access to the class and signature in the | 3845 const int args_base = state()->stack_size() - callee->arg_size(); |
3831 // MethodHandleCompiler. | 3846 ValueType* type = apop()->type(); |
3832 method_handle->set_callee(callee); | 3847 if (type->is_constant()) { |
3833 method_handle->set_caller(method()); | 3848 ciMethod* target = type->as_ObjectType()->constant_value()->as_member_name()->get_vmtarget(); |
3834 | 3849 // If the target is another method handle invoke try recursivly to get |
3835 // Get an adapter for the MethodHandle. | 3850 // a better target. |
3836 ciMethod* method_handle_adapter = method_handle->get_invokedynamic_adapter(); | 3851 if (target->is_method_handle_intrinsic()) { |
3837 if (method_handle_adapter != NULL) { | 3852 if (for_method_handle_inline(target)) { |
3838 if (try_inline(method_handle_adapter, /*holder_known=*/ true)) { | 3853 return true; |
3839 // Add a dependence for invalidation of the optimization. | 3854 } |
3840 if (!call_site->is_constant_call_site()) { | 3855 } else { |
3841 dependency_recorder()->assert_call_site_target_value(call_site, method_handle); | 3856 ciSignature* signature = target->signature(); |
3842 } | 3857 const int receiver_skip = target->is_static() ? 0 : 1; |
3843 return true; | 3858 // Cast receiver to its type. |
3844 } | 3859 if (!target->is_static()) { |
3845 } | 3860 ciKlass* tk = signature->accessing_klass(); |
3861 Value obj = state()->stack_at(args_base); | |
3862 if (obj->exact_type() == NULL && | |
3863 obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) { | |
3864 TypeCast* c = new TypeCast(tk, obj, state_before); | |
3865 append(c); | |
3866 state()->stack_at_put(args_base, c); | |
3867 } | |
3868 } | |
3869 // Cast reference arguments to its type. | |
3870 for (int i = 0, j = 0; i < signature->count(); i++) { | |
3871 ciType* t = signature->type_at(i); | |
3872 if (t->is_klass()) { | |
3873 ciKlass* tk = t->as_klass(); | |
3874 Value obj = state()->stack_at(args_base + receiver_skip + j); | |
3875 if (obj->exact_type() == NULL && | |
3876 obj->declared_type() != tk && tk != compilation()->env()->Object_klass()) { | |
3877 TypeCast* c = new TypeCast(t, obj, state_before); | |
3878 append(c); | |
3879 state()->stack_at_put(args_base + receiver_skip + j, c); | |
3880 } | |
3881 } | |
3882 j += t->size(); // long and double take two slots | |
3883 } | |
3884 Bytecodes::Code bc = target->is_static() ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual; | |
3885 if (try_inline(target, /*holder_known*/ true, bc)) { | |
3886 return true; | |
3887 } | |
3888 } | |
3889 } else { | |
3890 print_inlining(callee, "MemberName not constant", /*success*/ false); | |
3891 } | |
3892 } | |
3893 break; | |
3894 | |
3895 default: | |
3896 fatal(err_msg("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid))); | |
3897 break; | |
3898 } | |
3899 set_state(state_before); | |
3846 return false; | 3900 return false; |
3847 } | 3901 } |
3848 | 3902 |
3849 | 3903 |
3850 void GraphBuilder::inline_bailout(const char* msg) { | 3904 void GraphBuilder::inline_bailout(const char* msg) { |
4032 push(result_type, result); | 4086 push(result_type, result); |
4033 compilation()->set_has_unsafe_access(true); | 4087 compilation()->set_has_unsafe_access(true); |
4034 } | 4088 } |
4035 | 4089 |
4036 | 4090 |
4091 void GraphBuilder::print_inlining(ciMethod* callee, const char* msg, bool success) { | |
4092 if (!PrintInlining) return; | |
4093 assert(msg != NULL, "must be"); | |
4094 CompileTask::print_inlining(callee, scope()->level(), bci(), msg); | |
4095 if (success && CIPrintMethodCodes) { | |
4096 callee->print_codes(); | |
4097 } | |
4098 } | |
4099 | |
4100 | |
4037 #ifndef PRODUCT | 4101 #ifndef PRODUCT |
4038 void GraphBuilder::print_inline_result(ciMethod* callee, bool res) { | |
4039 CompileTask::print_inlining(callee, scope()->level(), bci(), _inline_bailout_msg); | |
4040 if (res && CIPrintMethodCodes) { | |
4041 callee->print_codes(); | |
4042 } | |
4043 } | |
4044 | |
4045 | |
4046 void GraphBuilder::print_stats() { | 4102 void GraphBuilder::print_stats() { |
4047 vmap()->print(); | 4103 vmap()->print(); |
4048 } | 4104 } |
4049 #endif // PRODUCT | 4105 #endif // PRODUCT |
4050 | 4106 |
4051 void GraphBuilder::profile_call(Value recv, ciKlass* known_holder) { | 4107 void GraphBuilder::profile_call(ciMethod* callee, Value recv, ciKlass* known_holder) { |
4052 append(new ProfileCall(method(), bci(), recv, known_holder)); | 4108 append(new ProfileCall(method(), bci(), callee, recv, known_holder)); |
4053 } | 4109 } |
4054 | 4110 |
4055 void GraphBuilder::profile_invocation(ciMethod* callee, ValueStack* state) { | 4111 void GraphBuilder::profile_invocation(ciMethod* callee, ValueStack* state) { |
4056 append(new ProfileInvoke(callee, state)); | 4112 append(new ProfileInvoke(callee, state)); |
4057 } | 4113 } |