duke@435: /* mikael@6198: * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "ci/bcEscapeAnalyzer.hpp" twisti@3050: #include "ci/ciCallSite.hpp" coleenp@4037: #include "ci/ciObjArray.hpp" twisti@3969: #include "ci/ciMemberName.hpp" stefank@2314: #include "ci/ciMethodHandle.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "compiler/compileLog.hpp" stefank@2314: #include "opto/addnode.hpp" stefank@2314: #include "opto/callGenerator.hpp" stefank@2314: #include "opto/callnode.hpp" stefank@2314: #include "opto/cfgnode.hpp" stefank@2314: #include "opto/connode.hpp" stefank@2314: #include "opto/parse.hpp" stefank@2314: #include "opto/rootnode.hpp" stefank@2314: #include "opto/runtime.hpp" stefank@2314: #include "opto/subnode.hpp" duke@435: duke@435: duke@435: // Utility function. duke@435: const TypeFunc* CallGenerator::tf() const { duke@435: return TypeFunc::make(method()); duke@435: } duke@435: duke@435: //-----------------------------ParseGenerator--------------------------------- duke@435: // Internal class which handles all direct bytecode traversal. duke@435: class ParseGenerator : public InlineCallGenerator { duke@435: private: duke@435: bool _is_osr; duke@435: float _expected_uses; duke@435: duke@435: public: duke@435: ParseGenerator(ciMethod* method, float expected_uses, bool is_osr = false) duke@435: : InlineCallGenerator(method) duke@435: { duke@435: _is_osr = is_osr; duke@435: _expected_uses = expected_uses; twisti@3100: assert(InlineTree::check_can_parse(method) == NULL, "parse must be possible"); duke@435: } duke@435: duke@435: virtual bool is_parse() const { return true; } roland@7041: virtual JVMState* generate(JVMState* jvms); duke@435: int is_osr() { return _is_osr; } duke@435: duke@435: }; duke@435: roland@7041: JVMState* ParseGenerator::generate(JVMState* jvms) { duke@435: Compile* C = Compile::current(); duke@435: duke@435: if (is_osr()) { duke@435: // The JVMS for a OSR has a single argument (see its TypeFunc). duke@435: assert(jvms->depth() == 1, "no inline OSR"); duke@435: } duke@435: duke@435: if (C->failing()) { duke@435: return NULL; // bailing out of the compile; do not try to parse duke@435: } duke@435: roland@7041: Parse parser(jvms, method(), _expected_uses); duke@435: // Grab signature for matching/allocation duke@435: #ifdef ASSERT duke@435: if (parser.tf() != (parser.depth() == 1 ? C->tf() : tf())) { duke@435: MutexLockerEx ml(Compile_lock, Mutex::_no_safepoint_check_flag); duke@435: assert(C->env()->system_dictionary_modification_counter_changed(), duke@435: "Must invalidate if TypeFuncs differ"); duke@435: } duke@435: #endif duke@435: duke@435: GraphKit& exits = parser.exits(); duke@435: duke@435: if (C->failing()) { duke@435: while (exits.pop_exception_state() != NULL) ; duke@435: return NULL; duke@435: } duke@435: duke@435: assert(exits.jvms()->same_calls_as(jvms), "sanity"); duke@435: duke@435: // Simply return the exit state of the parser, duke@435: // augmented by any exceptional states. duke@435: return exits.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: //---------------------------DirectCallGenerator------------------------------ duke@435: // Internal class which handles all out-of-line calls w/o receiver type checks. duke@435: class DirectCallGenerator : public CallGenerator { never@1515: private: never@1515: CallStaticJavaNode* _call_node; never@1515: // Force separate memory and I/O projections for the exceptional never@1515: // paths to facilitate late inlinig. never@1515: bool _separate_io_proj; never@1515: never@1515: public: never@1515: DirectCallGenerator(ciMethod* method, bool separate_io_proj) never@1515: : CallGenerator(method), never@1515: _separate_io_proj(separate_io_proj) duke@435: { duke@435: } roland@7041: virtual JVMState* generate(JVMState* jvms); never@1515: never@1515: CallStaticJavaNode* call_node() const { return _call_node; } duke@435: }; duke@435: roland@7041: JVMState* DirectCallGenerator::generate(JVMState* jvms) { duke@435: GraphKit kit(jvms); duke@435: bool is_static = method()->is_static(); duke@435: address target = is_static ? SharedRuntime::get_resolve_static_call_stub() duke@435: : SharedRuntime::get_resolve_opt_virtual_call_stub(); duke@435: duke@435: if (kit.C->log() != NULL) { duke@435: kit.C->log()->elem("direct_call bci='%d'", jvms->bci()); duke@435: } duke@435: kvn@5110: CallStaticJavaNode *call = new (kit.C) CallStaticJavaNode(kit.C, tf(), target, method(), kit.bci()); never@3745: _call_node = call; // Save the call node in case we need it later duke@435: if (!is_static) { duke@435: // Make an explicit receiver null_check as part of this call. duke@435: // Since we share a map with the caller, his JVMS gets adjusted. twisti@4313: kit.null_check_receiver_before_call(method()); duke@435: if (kit.stopped()) { duke@435: // And dump it back to the caller, decorated with any exceptions: duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: // Mark the call node as virtual, sort of: duke@435: call->set_optimized_virtual(true); twisti@3969: if (method()->is_method_handle_intrinsic() || twisti@3969: method()->is_compiled_lambda_form()) { twisti@1572: call->set_method_handle_invoke(true); twisti@1700: } duke@435: } duke@435: kit.set_arguments_for_java_call(call); never@1515: kit.set_edges_for_java_call(call, false, _separate_io_proj); never@1515: Node* ret = kit.set_results_for_java_call(call, _separate_io_proj); duke@435: kit.push_node(method()->return_type()->basic_type(), ret); duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: twisti@1572: //--------------------------VirtualCallGenerator------------------------------ twisti@1572: // Internal class which handles all out-of-line calls checking receiver type. duke@435: class VirtualCallGenerator : public CallGenerator { duke@435: private: duke@435: int _vtable_index; duke@435: public: duke@435: VirtualCallGenerator(ciMethod* method, int vtable_index) duke@435: : CallGenerator(method), _vtable_index(vtable_index) duke@435: { coleenp@4037: assert(vtable_index == Method::invalid_vtable_index || duke@435: vtable_index >= 0, "either invalid or usable"); duke@435: } duke@435: virtual bool is_virtual() const { return true; } roland@7041: virtual JVMState* generate(JVMState* jvms); duke@435: }; duke@435: roland@7041: JVMState* VirtualCallGenerator::generate(JVMState* jvms) { duke@435: GraphKit kit(jvms); duke@435: Node* receiver = kit.argument(0); duke@435: duke@435: if (kit.C->log() != NULL) { duke@435: kit.C->log()->elem("virtual_call bci='%d'", jvms->bci()); duke@435: } duke@435: duke@435: // If the receiver is a constant null, do not torture the system duke@435: // by attempting to call through it. The compile will proceed duke@435: // correctly, but may bail out in final_graph_reshaping, because duke@435: // the call instruction will have a seemingly deficient out-count. duke@435: // (The bailout says something misleading about an "infinite loop".) duke@435: if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) { duke@435: kit.inc_sp(method()->arg_size()); // restore arguments duke@435: kit.uncommon_trap(Deoptimization::Reason_null_check, duke@435: Deoptimization::Action_none, duke@435: NULL, "null receiver"); duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: // Ideally we would unconditionally do a null check here and let it duke@435: // be converted to an implicit check based on profile information. duke@435: // However currently the conversion to implicit null checks in duke@435: // Block::implicit_null_check() only looks for loads and stores, not calls. duke@435: ciMethod *caller = kit.method(); duke@435: ciMethodData *caller_md = (caller == NULL) ? NULL : caller->method_data(); goetz@6486: if (!UseInlineCaches || !ImplicitNullChecks || !os::zero_page_read_protected() || duke@435: ((ImplicitNullCheckThreshold > 0) && caller_md && duke@435: (caller_md->trap_count(Deoptimization::Reason_null_check) duke@435: >= (uint)ImplicitNullCheckThreshold))) { duke@435: // Make an explicit receiver null_check as part of this call. duke@435: // Since we share a map with the caller, his JVMS gets adjusted. twisti@4313: receiver = kit.null_check_receiver_before_call(method()); duke@435: if (kit.stopped()) { duke@435: // And dump it back to the caller, decorated with any exceptions: duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: } duke@435: duke@435: assert(!method()->is_static(), "virtual call must not be to static"); duke@435: assert(!method()->is_final(), "virtual call should not be to final"); duke@435: assert(!method()->is_private(), "virtual call should not be to private"); coleenp@4037: assert(_vtable_index == Method::invalid_vtable_index || !UseInlineCaches, duke@435: "no vtable calls if +UseInlineCaches "); duke@435: address target = SharedRuntime::get_resolve_virtual_call_stub(); duke@435: // Normal inline cache used for call kvn@4115: CallDynamicJavaNode *call = new (kit.C) CallDynamicJavaNode(tf(), target, method(), _vtable_index, kit.bci()); duke@435: kit.set_arguments_for_java_call(call); duke@435: kit.set_edges_for_java_call(call); duke@435: Node* ret = kit.set_results_for_java_call(call); duke@435: kit.push_node(method()->return_type()->basic_type(), ret); duke@435: duke@435: // Represent the effect of an implicit receiver null_check duke@435: // as part of this call. Since we share a map with the caller, duke@435: // his JVMS gets adjusted. duke@435: kit.cast_not_null(receiver); duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: CallGenerator* CallGenerator::for_inline(ciMethod* m, float expected_uses) { twisti@3100: if (InlineTree::check_can_parse(m) != NULL) return NULL; duke@435: return new ParseGenerator(m, expected_uses); duke@435: } duke@435: duke@435: // As a special case, the JVMS passed to this CallGenerator is duke@435: // for the method execution already in progress, not just the JVMS duke@435: // of the caller. Thus, this CallGenerator cannot be mixed with others! duke@435: CallGenerator* CallGenerator::for_osr(ciMethod* m, int osr_bci) { twisti@3100: if (InlineTree::check_can_parse(m) != NULL) return NULL; duke@435: float past_uses = m->interpreter_invocation_count(); duke@435: float expected_uses = past_uses; duke@435: return new ParseGenerator(m, expected_uses, true); duke@435: } duke@435: never@1515: CallGenerator* CallGenerator::for_direct_call(ciMethod* m, bool separate_io_proj) { duke@435: assert(!m->is_abstract(), "for_direct_call mismatch"); never@1515: return new DirectCallGenerator(m, separate_io_proj); duke@435: } duke@435: duke@435: CallGenerator* CallGenerator::for_virtual_call(ciMethod* m, int vtable_index) { duke@435: assert(!m->is_static(), "for_virtual_call mismatch"); twisti@3969: assert(!m->is_method_handle_intrinsic(), "should be a direct call"); duke@435: return new VirtualCallGenerator(m, vtable_index); duke@435: } duke@435: never@1515: // Allow inlining decisions to be delayed never@1515: class LateInlineCallGenerator : public DirectCallGenerator { roland@4409: protected: never@1515: CallGenerator* _inline_cg; never@1515: roland@4409: virtual bool do_late_inline_check(JVMState* jvms) { return true; } roland@4409: never@1515: public: never@1515: LateInlineCallGenerator(ciMethod* method, CallGenerator* inline_cg) : never@1515: DirectCallGenerator(method, true), _inline_cg(inline_cg) {} never@1515: never@1515: virtual bool is_late_inline() const { return true; } never@1515: never@1515: // Convert the CallStaticJava into an inline never@1515: virtual void do_late_inline(); never@1515: roland@7041: virtual JVMState* generate(JVMState* jvms) { roland@4357: Compile *C = Compile::current(); roland@4357: C->print_inlining_skip(this); roland@4357: never@1515: // Record that this call site should be revisited once the main never@1515: // parse is finished. roland@4409: if (!is_mh_late_inline()) { roland@4409: C->add_late_inline(this); roland@4409: } never@1515: never@1515: // Emit the CallStaticJava and request separate projections so never@1515: // that the late inlining logic can distinguish between fall never@1515: // through and exceptional uses of the memory and io projections never@1515: // as is done for allocations and macro expansion. roland@7041: return DirectCallGenerator::generate(jvms); never@1515: } roland@4409: roland@4409: virtual void print_inlining_late(const char* msg) { roland@4409: CallNode* call = call_node(); roland@4409: Compile* C = Compile::current(); roland@4409: C->print_inlining_insert(this); roland@4409: C->print_inlining(method(), call->jvms()->depth()-1, call->jvms()->bci(), msg); roland@4409: } roland@4409: never@1515: }; never@1515: never@1515: void LateInlineCallGenerator::do_late_inline() { never@1515: // Can't inline it kvn@5110: CallStaticJavaNode* call = call_node(); kvn@5110: if (call == NULL || call->outcnt() == 0 || kvn@5110: call->in(0) == NULL || call->in(0)->is_top()) { never@1515: return; roland@4538: } never@1515: kvn@5110: const TypeTuple *r = call->tf()->domain(); roland@4409: for (int i1 = 0; i1 < method()->arg_size(); i1++) { kvn@5110: if (call->in(TypeFunc::Parms + i1)->is_top() && r->field_at(TypeFunc::Parms + i1) != Type::HALF) { roland@4409: assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing"); roland@4409: return; roland@4409: } roland@4409: } roland@4409: kvn@5110: if (call->in(TypeFunc::Memory)->is_top()) { roland@4409: assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing"); roland@4409: return; roland@4409: } roland@4409: kvn@5110: Compile* C = Compile::current(); kvn@5110: // Remove inlined methods from Compiler's lists. kvn@5110: if (call->is_macro()) { kvn@5110: C->remove_macro_node(call); kvn@5110: } never@1515: never@1515: // Make a clone of the JVMState that appropriate to use for driving a parse kvn@5110: JVMState* old_jvms = call->jvms(); kvn@5110: JVMState* jvms = old_jvms->clone_shallow(C); never@1515: uint size = call->req(); kvn@4115: SafePointNode* map = new (C) SafePointNode(size, jvms); never@1515: for (uint i1 = 0; i1 < size; i1++) { never@1515: map->init_req(i1, call->in(i1)); never@1515: } never@1515: never@1515: // Make sure the state is a MergeMem for parsing. never@1515: if (!map->in(TypeFunc::Memory)->is_MergeMem()) { roland@4357: Node* mem = MergeMemNode::make(C, map->in(TypeFunc::Memory)); roland@4357: C->initial_gvn()->set_type_bottom(mem); roland@4357: map->set_req(TypeFunc::Memory, mem); never@1515: } never@1515: kvn@5110: uint nargs = method()->arg_size(); kvn@5110: // blow away old call arguments kvn@5110: Node* top = C->top(); kvn@5110: for (uint i1 = 0; i1 < nargs; i1++) { kvn@5110: map->set_req(TypeFunc::Parms + i1, top); kvn@5110: } never@1515: jvms->set_map(map); kvn@5110: kvn@5110: // Make enough space in the expression stack to transfer kvn@5110: // the incoming arguments and return value. never@1515: map->ensure_stack(jvms, jvms->method()->max_stack()); kvn@5110: for (uint i1 = 0; i1 < nargs; i1++) { kvn@5110: map->set_argument(jvms, i1, call->in(TypeFunc::Parms + i1)); never@1515: } never@1515: kvn@5110: // This check is done here because for_method_handle_inline() method kvn@5110: // needs jvms for inlined state. roland@4409: if (!do_late_inline_check(jvms)) { roland@4409: map->disconnect_inputs(NULL, C); roland@4409: return; roland@4409: } roland@4409: roland@4357: C->print_inlining_insert(this); roland@4357: never@1515: CompileLog* log = C->log(); never@1515: if (log != NULL) { never@1515: log->head("late_inline method='%d'", log->identify(method())); never@1515: JVMState* p = jvms; never@1515: while (p != NULL) { never@1515: log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method())); never@1515: p = p->caller(); never@1515: } never@1515: log->tail("late_inline"); never@1515: } never@1515: never@1515: // Setup default node notes to be picked up by the inlining kvn@6679: Node_Notes* old_nn = C->node_notes_at(call->_idx); never@1515: if (old_nn != NULL) { never@1515: Node_Notes* entry_nn = old_nn->clone(C); never@1515: entry_nn->set_jvms(jvms); never@1515: C->set_default_node_notes(entry_nn); never@1515: } never@1515: never@1515: // Now perform the inling using the synthesized JVMState roland@7041: JVMState* new_jvms = _inline_cg->generate(jvms); never@1515: if (new_jvms == NULL) return; // no change never@1515: if (C->failing()) return; never@1515: never@1515: // Capture any exceptional control flow never@1515: GraphKit kit(new_jvms); never@1515: never@1515: // Find the result object never@1515: Node* result = C->top(); never@1515: int result_size = method()->return_type()->size(); never@1515: if (result_size != 0 && !kit.stopped()) { never@1515: result = (result_size == 1) ? kit.pop() : kit.pop_pair(); never@1515: } never@1515: roland@4409: C->set_has_loops(C->has_loops() || _inline_cg->method()->has_loops()); roland@4409: C->env()->notice_inlined_method(_inline_cg->method()); roland@4409: C->set_inlining_progress(true); roland@4409: roland@7041: kit.replace_call(call, result, true); never@1515: } never@1515: never@1515: never@1515: CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) { never@1515: return new LateInlineCallGenerator(method, inline_cg); never@1515: } never@1515: roland@4409: class LateInlineMHCallGenerator : public LateInlineCallGenerator { roland@4409: ciMethod* _caller; roland@4409: int _attempt; roland@4409: bool _input_not_const; roland@4409: roland@4409: virtual bool do_late_inline_check(JVMState* jvms); roland@4409: virtual bool already_attempted() const { return _attempt > 0; } roland@4409: roland@4409: public: roland@4409: LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) : roland@4409: LateInlineCallGenerator(callee, NULL), _caller(caller), _attempt(0), _input_not_const(input_not_const) {} roland@4409: roland@4409: virtual bool is_mh_late_inline() const { return true; } roland@4409: roland@7041: virtual JVMState* generate(JVMState* jvms) { roland@7041: JVMState* new_jvms = LateInlineCallGenerator::generate(jvms); roland@4409: if (_input_not_const) { roland@4409: // inlining won't be possible so no need to enqueue right now. roland@4409: call_node()->set_generator(this); roland@4409: } else { roland@4409: Compile::current()->add_late_inline(this); roland@4409: } roland@4409: return new_jvms; roland@4409: } roland@4409: roland@4409: virtual void print_inlining_late(const char* msg) { roland@4409: if (!_input_not_const) return; roland@4409: LateInlineCallGenerator::print_inlining_late(msg); roland@4409: } roland@4409: }; roland@4409: roland@4409: bool LateInlineMHCallGenerator::do_late_inline_check(JVMState* jvms) { roland@4409: roland@4409: CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), _input_not_const); roland@4409: roland@4409: if (!_input_not_const) { roland@4409: _attempt++; roland@4409: } roland@4409: roland@4409: if (cg != NULL) { roland@4409: assert(!cg->is_late_inline() && cg->is_inline(), "we're doing late inlining"); roland@4409: _inline_cg = cg; roland@4409: Compile::current()->dec_number_of_mh_late_inlines(); roland@4409: return true; roland@4409: } roland@4409: roland@4409: call_node()->set_generator(this); roland@4409: return false; roland@4409: } roland@4409: roland@4409: CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) { roland@4409: Compile::current()->inc_number_of_mh_late_inlines(); roland@4409: CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const); roland@4409: return cg; roland@4409: } roland@4409: roland@4409: class LateInlineStringCallGenerator : public LateInlineCallGenerator { roland@4409: roland@4409: public: roland@4409: LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) : roland@4409: LateInlineCallGenerator(method, inline_cg) {} roland@4409: roland@7041: virtual JVMState* generate(JVMState* jvms) { roland@4409: Compile *C = Compile::current(); roland@4409: C->print_inlining_skip(this); roland@4409: roland@4409: C->add_string_late_inline(this); roland@4409: roland@7041: JVMState* new_jvms = DirectCallGenerator::generate(jvms); roland@4409: return new_jvms; roland@4409: } roland@5991: roland@5991: virtual bool is_string_late_inline() const { return true; } roland@4409: }; roland@4409: roland@4409: CallGenerator* CallGenerator::for_string_late_inline(ciMethod* method, CallGenerator* inline_cg) { roland@4409: return new LateInlineStringCallGenerator(method, inline_cg); roland@4409: } roland@4409: kvn@5110: class LateInlineBoxingCallGenerator : public LateInlineCallGenerator { kvn@5110: kvn@5110: public: kvn@5110: LateInlineBoxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) : kvn@5110: LateInlineCallGenerator(method, inline_cg) {} kvn@5110: roland@7041: virtual JVMState* generate(JVMState* jvms) { kvn@5110: Compile *C = Compile::current(); kvn@5110: C->print_inlining_skip(this); kvn@5110: kvn@5110: C->add_boxing_late_inline(this); kvn@5110: roland@7041: JVMState* new_jvms = DirectCallGenerator::generate(jvms); kvn@5110: return new_jvms; kvn@5110: } kvn@5110: }; kvn@5110: kvn@5110: CallGenerator* CallGenerator::for_boxing_late_inline(ciMethod* method, CallGenerator* inline_cg) { kvn@5110: return new LateInlineBoxingCallGenerator(method, inline_cg); kvn@5110: } duke@435: duke@435: //---------------------------WarmCallGenerator-------------------------------- duke@435: // Internal class which handles initial deferral of inlining decisions. duke@435: class WarmCallGenerator : public CallGenerator { duke@435: WarmCallInfo* _call_info; duke@435: CallGenerator* _if_cold; duke@435: CallGenerator* _if_hot; duke@435: bool _is_virtual; // caches virtuality of if_cold duke@435: bool _is_inline; // caches inline-ness of if_hot duke@435: duke@435: public: duke@435: WarmCallGenerator(WarmCallInfo* ci, duke@435: CallGenerator* if_cold, duke@435: CallGenerator* if_hot) duke@435: : CallGenerator(if_cold->method()) duke@435: { duke@435: assert(method() == if_hot->method(), "consistent choices"); duke@435: _call_info = ci; duke@435: _if_cold = if_cold; duke@435: _if_hot = if_hot; duke@435: _is_virtual = if_cold->is_virtual(); duke@435: _is_inline = if_hot->is_inline(); duke@435: } duke@435: duke@435: virtual bool is_inline() const { return _is_inline; } duke@435: virtual bool is_virtual() const { return _is_virtual; } duke@435: virtual bool is_deferred() const { return true; } duke@435: roland@7041: virtual JVMState* generate(JVMState* jvms); duke@435: }; duke@435: duke@435: duke@435: CallGenerator* CallGenerator::for_warm_call(WarmCallInfo* ci, duke@435: CallGenerator* if_cold, duke@435: CallGenerator* if_hot) { duke@435: return new WarmCallGenerator(ci, if_cold, if_hot); duke@435: } duke@435: roland@7041: JVMState* WarmCallGenerator::generate(JVMState* jvms) { duke@435: Compile* C = Compile::current(); duke@435: if (C->log() != NULL) { duke@435: C->log()->elem("warm_call bci='%d'", jvms->bci()); duke@435: } roland@7041: jvms = _if_cold->generate(jvms); duke@435: if (jvms != NULL) { duke@435: Node* m = jvms->map()->control(); duke@435: if (m->is_CatchProj()) m = m->in(0); else m = C->top(); duke@435: if (m->is_Catch()) m = m->in(0); else m = C->top(); duke@435: if (m->is_Proj()) m = m->in(0); else m = C->top(); duke@435: if (m->is_CallJava()) { duke@435: _call_info->set_call(m->as_Call()); duke@435: _call_info->set_hot_cg(_if_hot); duke@435: #ifndef PRODUCT duke@435: if (PrintOpto || PrintOptoInlining) { duke@435: tty->print_cr("Queueing for warm inlining at bci %d:", jvms->bci()); duke@435: tty->print("WCI: "); duke@435: _call_info->print(); duke@435: } duke@435: #endif duke@435: _call_info->set_heat(_call_info->compute_heat()); duke@435: C->set_warm_calls(_call_info->insert_into(C->warm_calls())); duke@435: } duke@435: } duke@435: return jvms; duke@435: } duke@435: duke@435: void WarmCallInfo::make_hot() { never@1515: Unimplemented(); duke@435: } duke@435: duke@435: void WarmCallInfo::make_cold() { duke@435: // No action: Just dequeue. duke@435: } duke@435: duke@435: duke@435: //------------------------PredictedCallGenerator------------------------------ duke@435: // Internal class which handles all out-of-line calls checking receiver type. duke@435: class PredictedCallGenerator : public CallGenerator { duke@435: ciKlass* _predicted_receiver; duke@435: CallGenerator* _if_missed; duke@435: CallGenerator* _if_hit; duke@435: float _hit_prob; duke@435: duke@435: public: duke@435: PredictedCallGenerator(ciKlass* predicted_receiver, duke@435: CallGenerator* if_missed, duke@435: CallGenerator* if_hit, float hit_prob) duke@435: : CallGenerator(if_missed->method()) duke@435: { duke@435: // The call profile data may predict the hit_prob as extreme as 0 or 1. duke@435: // Remove the extremes values from the range. duke@435: if (hit_prob > PROB_MAX) hit_prob = PROB_MAX; duke@435: if (hit_prob < PROB_MIN) hit_prob = PROB_MIN; duke@435: duke@435: _predicted_receiver = predicted_receiver; duke@435: _if_missed = if_missed; duke@435: _if_hit = if_hit; duke@435: _hit_prob = hit_prob; duke@435: } duke@435: duke@435: virtual bool is_virtual() const { return true; } duke@435: virtual bool is_inline() const { return _if_hit->is_inline(); } duke@435: virtual bool is_deferred() const { return _if_hit->is_deferred(); } duke@435: roland@7041: virtual JVMState* generate(JVMState* jvms); duke@435: }; duke@435: duke@435: duke@435: CallGenerator* CallGenerator::for_predicted_call(ciKlass* predicted_receiver, duke@435: CallGenerator* if_missed, duke@435: CallGenerator* if_hit, duke@435: float hit_prob) { duke@435: return new PredictedCallGenerator(predicted_receiver, if_missed, if_hit, hit_prob); duke@435: } duke@435: duke@435: roland@7041: JVMState* PredictedCallGenerator::generate(JVMState* jvms) { duke@435: GraphKit kit(jvms); duke@435: PhaseGVN& gvn = kit.gvn(); duke@435: // We need an explicit receiver null_check before checking its type. duke@435: // We share a map with the caller, so his JVMS gets adjusted. duke@435: Node* receiver = kit.argument(0); duke@435: duke@435: CompileLog* log = kit.C->log(); duke@435: if (log != NULL) { duke@435: log->elem("predicted_call bci='%d' klass='%d'", duke@435: jvms->bci(), log->identify(_predicted_receiver)); duke@435: } duke@435: twisti@4313: receiver = kit.null_check_receiver_before_call(method()); duke@435: if (kit.stopped()) { duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: roland@7041: // Make a copy of the replaced nodes in case we need to restore them roland@7041: ReplacedNodes replaced_nodes = kit.map()->replaced_nodes(); roland@7041: replaced_nodes.clone(); roland@7041: duke@435: Node* exact_receiver = receiver; // will get updated in place... duke@435: Node* slow_ctl = kit.type_check_receiver(receiver, duke@435: _predicted_receiver, _hit_prob, duke@435: &exact_receiver); duke@435: duke@435: SafePointNode* slow_map = NULL; duke@435: JVMState* slow_jvms; duke@435: { PreserveJVMState pjvms(&kit); duke@435: kit.set_control(slow_ctl); duke@435: if (!kit.stopped()) { roland@7041: slow_jvms = _if_missed->generate(kit.sync_jvms()); twisti@3313: if (kit.failing()) twisti@3313: return NULL; // might happen because of NodeCountInliningCutoff twisti@3313: assert(slow_jvms != NULL, "must be"); duke@435: kit.add_exception_states_from(slow_jvms); duke@435: kit.set_map(slow_jvms->map()); duke@435: if (!kit.stopped()) duke@435: slow_map = kit.stop(); duke@435: } duke@435: } duke@435: kvn@728: if (kit.stopped()) { kvn@728: // Instance exactly does not matches the desired type. kvn@728: kit.set_jvms(slow_jvms); kvn@728: return kit.transfer_exceptions_into_jvms(); kvn@728: } kvn@728: duke@435: // fall through if the instance exactly matches the desired type duke@435: kit.replace_in_map(receiver, exact_receiver); duke@435: duke@435: // Make the hot call: roland@7041: JVMState* new_jvms = _if_hit->generate(kit.sync_jvms()); duke@435: if (new_jvms == NULL) { duke@435: // Inline failed, so make a direct call. duke@435: assert(_if_hit->is_inline(), "must have been a failed inline"); duke@435: CallGenerator* cg = CallGenerator::for_direct_call(_if_hit->method()); roland@7041: new_jvms = cg->generate(kit.sync_jvms()); duke@435: } duke@435: kit.add_exception_states_from(new_jvms); duke@435: kit.set_jvms(new_jvms); duke@435: duke@435: // Need to merge slow and fast? duke@435: if (slow_map == NULL) { duke@435: // The fast path is the only path remaining. duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: if (kit.stopped()) { duke@435: // Inlined method threw an exception, so it's just the slow path after all. duke@435: kit.set_jvms(slow_jvms); duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: roland@7041: // There are 2 branches and the replaced nodes are only valid on roland@7041: // one: restore the replaced nodes to what they were before the roland@7041: // branch. roland@7041: kit.map()->set_replaced_nodes(replaced_nodes); roland@7041: duke@435: // Finish the diamond. duke@435: kit.C->set_has_split_ifs(true); // Has chance for split-if optimization kvn@4115: RegionNode* region = new (kit.C) RegionNode(3); duke@435: region->init_req(1, kit.control()); duke@435: region->init_req(2, slow_map->control()); duke@435: kit.set_control(gvn.transform(region)); duke@435: Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO); duke@435: iophi->set_req(2, slow_map->i_o()); duke@435: kit.set_i_o(gvn.transform(iophi)); kvn@7026: // Merge memory duke@435: kit.merge_memory(slow_map->merged_memory(), region, 2); kvn@7026: // Transform new memory Phis. kvn@7026: for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) { kvn@7026: Node* phi = mms.memory(); kvn@7026: if (phi->is_Phi() && phi->in(0) == region) { kvn@7026: mms.set_memory(gvn.transform(phi)); kvn@7026: } kvn@7026: } duke@435: uint tos = kit.jvms()->stkoff() + kit.sp(); duke@435: uint limit = slow_map->req(); duke@435: for (uint i = TypeFunc::Parms; i < limit; i++) { duke@435: // Skip unused stack slots; fast forward to monoff(); duke@435: if (i == tos) { duke@435: i = kit.jvms()->monoff(); duke@435: if( i >= limit ) break; duke@435: } duke@435: Node* m = kit.map()->in(i); duke@435: Node* n = slow_map->in(i); duke@435: if (m != n) { roland@6313: const Type* t = gvn.type(m)->meet_speculative(gvn.type(n)); duke@435: Node* phi = PhiNode::make(region, m, t); duke@435: phi->set_req(2, n); duke@435: kit.map()->set_req(i, gvn.transform(phi)); duke@435: } duke@435: } duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: roland@4409: CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool delayed_forbidden) { twisti@3969: assert(callee->is_method_handle_intrinsic() || twisti@3969: callee->is_compiled_lambda_form(), "for_method_handle_call mismatch"); roland@4409: bool input_not_const; roland@4409: CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, input_not_const); roland@4409: Compile* C = Compile::current(); roland@4409: if (cg != NULL) { roland@4409: if (!delayed_forbidden && AlwaysIncrementalInline) { roland@4409: return CallGenerator::for_late_inline(callee, cg); roland@4409: } else { roland@4409: return cg; roland@4409: } roland@4409: } roland@4409: int bci = jvms->bci(); roland@4409: ciCallProfile profile = caller->call_profile_at_bci(bci); roland@4409: int call_site_count = caller->scale_count(profile.count()); roland@4409: roland@4409: if (IncrementalInline && call_site_count > 0 && roland@4409: (input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())) { roland@4409: return CallGenerator::for_mh_late_inline(caller, callee, input_not_const); roland@4409: } else { twisti@4414: // Out-of-line call. roland@4409: return CallGenerator::for_direct_call(callee); roland@4409: } twisti@3313: } twisti@3313: roland@4409: CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool& input_not_const) { twisti@3969: GraphKit kit(jvms); twisti@3969: PhaseGVN& gvn = kit.gvn(); twisti@3969: Compile* C = kit.C; twisti@3969: vmIntrinsics::ID iid = callee->intrinsic_id(); roland@4409: input_not_const = true; twisti@3969: switch (iid) { twisti@3969: case vmIntrinsics::_invokeBasic: twisti@3969: { twisti@4313: // Get MethodHandle receiver: twisti@3969: Node* receiver = kit.argument(0); twisti@3969: if (receiver->Opcode() == Op_ConP) { roland@4409: input_not_const = false; twisti@3969: const TypeOopPtr* oop_ptr = receiver->bottom_type()->is_oopptr(); twisti@3969: ciMethod* target = oop_ptr->const_oop()->as_method_handle()->get_vmtarget(); twisti@3969: guarantee(!target->is_method_handle_intrinsic(), "should not happen"); // XXX remove coleenp@4037: const int vtable_index = Method::invalid_vtable_index; roland@5991: CallGenerator* cg = C->call_generator(target, vtable_index, false, jvms, true, PROB_ALWAYS, NULL, true, true); vlivanov@6106: assert(cg == NULL || !cg->is_late_inline() || cg->is_mh_late_inline(), "no late inline here"); twisti@3969: if (cg != NULL && cg->is_inline()) twisti@3969: return cg; never@3105: } never@3105: } twisti@3969: break; never@3105: twisti@3969: case vmIntrinsics::_linkToVirtual: twisti@3969: case vmIntrinsics::_linkToStatic: twisti@3969: case vmIntrinsics::_linkToSpecial: twisti@3969: case vmIntrinsics::_linkToInterface: twisti@3969: { twisti@4313: // Get MemberName argument: twisti@3969: Node* member_name = kit.argument(callee->arg_size() - 1); twisti@3969: if (member_name->Opcode() == Op_ConP) { roland@4409: input_not_const = false; twisti@3969: const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr(); twisti@3969: ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget(); twisti@3969: twisti@3969: // In lamda forms we erase signature types to avoid resolving issues twisti@3969: // involving class loaders. When we optimize a method handle invoke twisti@3969: // to a direct call we must cast the receiver and arguments to its twisti@3969: // actual types. twisti@3969: ciSignature* signature = target->signature(); twisti@3969: const int receiver_skip = target->is_static() ? 0 : 1; twisti@3969: // Cast receiver to its type. twisti@3969: if (!target->is_static()) { twisti@3969: Node* arg = kit.argument(0); twisti@3969: const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr(); twisti@3969: const Type* sig_type = TypeOopPtr::make_from_klass(signature->accessing_klass()); twisti@3969: if (arg_type != NULL && !arg_type->higher_equal(sig_type)) { kvn@4115: Node* cast_obj = gvn.transform(new (C) CheckCastPPNode(kit.control(), arg, sig_type)); twisti@3969: kit.set_argument(0, cast_obj); twisti@3969: } twisti@3969: } twisti@3969: // Cast reference arguments to its type. twisti@3969: for (int i = 0; i < signature->count(); i++) { twisti@3969: ciType* t = signature->type_at(i); twisti@3969: if (t->is_klass()) { twisti@3969: Node* arg = kit.argument(receiver_skip + i); twisti@3969: const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr(); twisti@3969: const Type* sig_type = TypeOopPtr::make_from_klass(t->as_klass()); twisti@3969: if (arg_type != NULL && !arg_type->higher_equal(sig_type)) { kvn@4115: Node* cast_obj = gvn.transform(new (C) CheckCastPPNode(kit.control(), arg, sig_type)); twisti@3969: kit.set_argument(receiver_skip + i, cast_obj); twisti@3969: } twisti@3969: } twisti@3969: } twisti@4414: twisti@4414: // Try to get the most accurate receiver type twisti@4414: const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual); twisti@4414: const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface); twisti@4414: int vtable_index = Method::invalid_vtable_index; twisti@4414: bool call_does_dispatch = false; twisti@4414: roland@5991: ciKlass* speculative_receiver_type = NULL; twisti@4414: if (is_virtual_or_interface) { twisti@4414: ciInstanceKlass* klass = target->holder(); twisti@4414: Node* receiver_node = kit.argument(0); twisti@4414: const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr(); twisti@4414: // call_does_dispatch and vtable_index are out-parameters. They might be changed. roland@6746: // optimize_virtual_call() takes 2 different holder roland@6746: // arguments for a corner case that doesn't apply here (see roland@6746: // Parse::do_call()) roland@6746: target = C->optimize_virtual_call(caller, jvms->bci(), klass, klass, roland@6746: target, receiver_type, is_virtual, twisti@4414: call_does_dispatch, vtable_index); // out-parameters roland@5991: // We lack profiling at this call but type speculation may roland@5991: // provide us with a type roland@5991: speculative_receiver_type = receiver_type->speculative_type(); twisti@4414: } twisti@4414: roland@5991: CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms, true, PROB_ALWAYS, speculative_receiver_type, true, true); vlivanov@6106: assert(cg == NULL || !cg->is_late_inline() || cg->is_mh_late_inline(), "no late inline here"); twisti@3969: if (cg != NULL && cg->is_inline()) twisti@3969: return cg; twisti@3969: } never@2949: } twisti@3969: break; twisti@3969: twisti@3969: default: kvn@3971: fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid))); twisti@3969: break; never@2949: } never@2949: return NULL; never@2949: } never@2949: twisti@1573: kvn@7026: //------------------------PredicatedIntrinsicGenerator------------------------------ kvn@7026: // Internal class which handles all predicated Intrinsic calls. kvn@7026: class PredicatedIntrinsicGenerator : public CallGenerator { kvn@4205: CallGenerator* _intrinsic; kvn@4205: CallGenerator* _cg; kvn@4205: kvn@4205: public: kvn@7026: PredicatedIntrinsicGenerator(CallGenerator* intrinsic, kvn@7026: CallGenerator* cg) kvn@4205: : CallGenerator(cg->method()) kvn@4205: { kvn@4205: _intrinsic = intrinsic; kvn@4205: _cg = cg; kvn@4205: } kvn@4205: kvn@4205: virtual bool is_virtual() const { return true; } kvn@4205: virtual bool is_inlined() const { return true; } kvn@4205: virtual bool is_intrinsic() const { return true; } kvn@4205: roland@7041: virtual JVMState* generate(JVMState* jvms); kvn@4205: }; kvn@4205: kvn@4205: kvn@7026: CallGenerator* CallGenerator::for_predicated_intrinsic(CallGenerator* intrinsic, kvn@7026: CallGenerator* cg) { kvn@7026: return new PredicatedIntrinsicGenerator(intrinsic, cg); kvn@4205: } kvn@4205: kvn@4205: roland@7041: JVMState* PredicatedIntrinsicGenerator::generate(JVMState* jvms) { kvn@7026: // The code we want to generate here is: kvn@7026: // if (receiver == NULL) kvn@7026: // uncommon_Trap kvn@7026: // if (predicate(0)) kvn@7026: // do_intrinsic(0) kvn@7026: // else kvn@7026: // if (predicate(1)) kvn@7026: // do_intrinsic(1) kvn@7026: // ... kvn@7026: // else kvn@7026: // do_java_comp kvn@7026: kvn@4205: GraphKit kit(jvms); kvn@4205: PhaseGVN& gvn = kit.gvn(); kvn@4205: kvn@4205: CompileLog* log = kit.C->log(); kvn@4205: if (log != NULL) { kvn@7026: log->elem("predicated_intrinsic bci='%d' method='%d'", kvn@4205: jvms->bci(), log->identify(method())); kvn@4205: } kvn@4205: kvn@7026: if (!method()->is_static()) { kvn@7026: // We need an explicit receiver null_check before checking its type in predicate. kvn@7026: // We share a map with the caller, so his JVMS gets adjusted. kvn@7026: Node* receiver = kit.null_check_receiver_before_call(method()); kvn@7026: if (kit.stopped()) { kvn@7026: return kit.transfer_exceptions_into_jvms(); kvn@4205: } kvn@4205: } kvn@4205: kvn@7026: int n_predicates = _intrinsic->predicates_count(); kvn@7026: assert(n_predicates > 0, "sanity"); kvn@7026: kvn@7026: JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1)); kvn@7026: kvn@7026: // Region for normal compilation code if intrinsic failed. kvn@7026: Node* slow_region = new (kit.C) RegionNode(1); kvn@7026: kvn@7026: int results = 0; kvn@7026: for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) { kvn@7026: #ifdef ASSERT kvn@7026: JVMState* old_jvms = kit.jvms(); kvn@7026: SafePointNode* old_map = kit.map(); kvn@7026: Node* old_io = old_map->i_o(); kvn@7026: Node* old_mem = old_map->memory(); kvn@7026: Node* old_exc = old_map->next_exception(); kvn@7026: #endif kvn@7026: Node* else_ctrl = _intrinsic->generate_predicate(kit.sync_jvms(), predicate); kvn@7026: #ifdef ASSERT kvn@7026: // Assert(no_new_memory && no_new_io && no_new_exceptions) after generate_predicate. kvn@7026: assert(old_jvms == kit.jvms(), "generate_predicate should not change jvm state"); kvn@7026: SafePointNode* new_map = kit.map(); kvn@7026: assert(old_io == new_map->i_o(), "generate_predicate should not change i_o"); kvn@7026: assert(old_mem == new_map->memory(), "generate_predicate should not change memory"); kvn@7026: assert(old_exc == new_map->next_exception(), "generate_predicate should not add exceptions"); kvn@7026: #endif kvn@7026: if (!kit.stopped()) { kvn@7026: PreserveJVMState pjvms(&kit); kvn@7026: // Generate intrinsic code: roland@7041: JVMState* new_jvms = _intrinsic->generate(kit.sync_jvms()); kvn@7026: if (new_jvms == NULL) { kvn@7026: // Intrinsic failed, use normal compilation path for this predicate. kvn@7026: slow_region->add_req(kit.control()); kvn@7026: } else { kvn@7026: kit.add_exception_states_from(new_jvms); kvn@7026: kit.set_jvms(new_jvms); kvn@7026: if (!kit.stopped()) { kvn@7026: result_jvms[results++] = kit.jvms(); kvn@7026: } kvn@7026: } kvn@7026: } kvn@7026: if (else_ctrl == NULL) { kvn@7026: else_ctrl = kit.C->top(); kvn@7026: } kvn@7026: kit.set_control(else_ctrl); kvn@7026: } kvn@7026: if (!kit.stopped()) { kvn@7026: // Final 'else' after predicates. kvn@7026: slow_region->add_req(kit.control()); kvn@7026: } kvn@7026: if (slow_region->req() > 1) { kvn@7026: PreserveJVMState pjvms(&kit); kvn@7026: // Generate normal compilation code: kvn@7026: kit.set_control(gvn.transform(slow_region)); roland@7041: JVMState* new_jvms = _cg->generate(kit.sync_jvms()); kvn@7026: if (kit.failing()) kvn@7026: return NULL; // might happen because of NodeCountInliningCutoff kvn@7026: assert(new_jvms != NULL, "must be"); kvn@7026: kit.add_exception_states_from(new_jvms); kvn@7026: kit.set_jvms(new_jvms); kvn@7026: if (!kit.stopped()) { kvn@7026: result_jvms[results++] = kit.jvms(); kvn@7026: } kvn@7026: } kvn@7026: kvn@7026: if (results == 0) { kvn@7026: // All paths ended in uncommon traps. kvn@7026: (void) kit.stop(); kvn@4205: return kit.transfer_exceptions_into_jvms(); kvn@4205: } kvn@4205: kvn@7026: if (results == 1) { // Only one path kvn@7026: kit.set_jvms(result_jvms[0]); kvn@4205: return kit.transfer_exceptions_into_jvms(); kvn@4205: } kvn@4205: kvn@7026: // Merge all paths. kvn@7026: kit.C->set_has_split_ifs(true); // Has chance for split-if optimization kvn@7026: RegionNode* region = new (kit.C) RegionNode(results + 1); kvn@7026: Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO); kvn@7026: for (int i = 0; i < results; i++) { kvn@7026: JVMState* jvms = result_jvms[i]; kvn@7026: int path = i + 1; kvn@7026: SafePointNode* map = jvms->map(); kvn@7026: region->init_req(path, map->control()); kvn@7026: iophi->set_req(path, map->i_o()); kvn@7026: if (i == 0) { kvn@7026: kit.set_jvms(jvms); kvn@7026: } else { kvn@7026: kit.merge_memory(map->merged_memory(), region, path); kvn@7026: } kvn@7026: } kvn@7026: kit.set_control(gvn.transform(region)); kvn@7026: kit.set_i_o(gvn.transform(iophi)); kvn@7026: // Transform new memory Phis. kvn@7026: for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) { kvn@7026: Node* phi = mms.memory(); kvn@7026: if (phi->is_Phi() && phi->in(0) == region) { kvn@7026: mms.set_memory(gvn.transform(phi)); kvn@7026: } kvn@4205: } kvn@4205: kvn@7026: // Merge debug info. kvn@7026: Node** ins = NEW_RESOURCE_ARRAY(Node*, results); kvn@4205: uint tos = kit.jvms()->stkoff() + kit.sp(); kvn@7026: Node* map = kit.map(); kvn@7026: uint limit = map->req(); kvn@4205: for (uint i = TypeFunc::Parms; i < limit; i++) { kvn@4205: // Skip unused stack slots; fast forward to monoff(); kvn@4205: if (i == tos) { kvn@4205: i = kit.jvms()->monoff(); kvn@4205: if( i >= limit ) break; kvn@4205: } kvn@7026: Node* n = map->in(i); kvn@7026: ins[0] = n; kvn@7026: const Type* t = gvn.type(n); kvn@7026: bool needs_phi = false; kvn@7026: for (int j = 1; j < results; j++) { kvn@7026: JVMState* jvms = result_jvms[j]; kvn@7026: Node* jmap = jvms->map(); kvn@7026: Node* m = NULL; kvn@7026: if (jmap->req() > i) { kvn@7026: m = jmap->in(i); kvn@7026: if (m != n) { kvn@7026: needs_phi = true; kvn@7026: t = t->meet_speculative(gvn.type(m)); kvn@7026: } kvn@7026: } kvn@7026: ins[j] = m; kvn@7026: } kvn@7026: if (needs_phi) { kvn@7026: Node* phi = PhiNode::make(region, n, t); kvn@7026: for (int j = 1; j < results; j++) { kvn@7026: phi->set_req(j + 1, ins[j]); kvn@7026: } kvn@7026: map->set_req(i, gvn.transform(phi)); kvn@4205: } kvn@4205: } kvn@7026: kvn@4205: return kit.transfer_exceptions_into_jvms(); kvn@4205: } kvn@4205: duke@435: //-------------------------UncommonTrapCallGenerator----------------------------- duke@435: // Internal class which handles all out-of-line calls checking receiver type. duke@435: class UncommonTrapCallGenerator : public CallGenerator { duke@435: Deoptimization::DeoptReason _reason; duke@435: Deoptimization::DeoptAction _action; duke@435: duke@435: public: duke@435: UncommonTrapCallGenerator(ciMethod* m, duke@435: Deoptimization::DeoptReason reason, duke@435: Deoptimization::DeoptAction action) duke@435: : CallGenerator(m) duke@435: { duke@435: _reason = reason; duke@435: _action = action; duke@435: } duke@435: duke@435: virtual bool is_virtual() const { ShouldNotReachHere(); return false; } duke@435: virtual bool is_trap() const { return true; } duke@435: roland@7041: virtual JVMState* generate(JVMState* jvms); duke@435: }; duke@435: duke@435: duke@435: CallGenerator* duke@435: CallGenerator::for_uncommon_trap(ciMethod* m, duke@435: Deoptimization::DeoptReason reason, duke@435: Deoptimization::DeoptAction action) { duke@435: return new UncommonTrapCallGenerator(m, reason, action); duke@435: } duke@435: duke@435: roland@7041: JVMState* UncommonTrapCallGenerator::generate(JVMState* jvms) { duke@435: GraphKit kit(jvms); duke@435: // Take the trap with arguments pushed on the stack. (Cf. null_check_receiver). duke@435: int nargs = method()->arg_size(); duke@435: kit.inc_sp(nargs); duke@435: assert(nargs <= kit.sp() && kit.sp() <= jvms->stk_size(), "sane sp w/ args pushed"); duke@435: if (_reason == Deoptimization::Reason_class_check && duke@435: _action == Deoptimization::Action_maybe_recompile) { duke@435: // Temp fix for 6529811 duke@435: // Don't allow uncommon_trap to override our decision to recompile in the event duke@435: // of a class cast failure for a monomorphic call as it will never let us convert duke@435: // the call to either bi-morphic or megamorphic and can lead to unc-trap loops duke@435: bool keep_exact_action = true; duke@435: kit.uncommon_trap(_reason, _action, NULL, "monomorphic vcall checkcast", false, keep_exact_action); duke@435: } else { duke@435: kit.uncommon_trap(_reason, _action); duke@435: } duke@435: return kit.transfer_exceptions_into_jvms(); duke@435: } duke@435: duke@435: // (Note: Moved hook_up_call to GraphKit::set_edges_for_java_call.) duke@435: duke@435: // (Node: Merged hook_up_exits into ParseGenerator::generate.) duke@435: duke@435: #define NODES_OVERHEAD_PER_METHOD (30.0) duke@435: #define NODES_PER_BYTECODE (9.5) duke@435: duke@435: void WarmCallInfo::init(JVMState* call_site, ciMethod* call_method, ciCallProfile& profile, float prof_factor) { duke@435: int call_count = profile.count(); duke@435: int code_size = call_method->code_size(); duke@435: duke@435: // Expected execution count is based on the historical count: duke@435: _count = call_count < 0 ? 1 : call_site->method()->scale_count(call_count, prof_factor); duke@435: duke@435: // Expected profit from inlining, in units of simple call-overheads. duke@435: _profit = 1.0; duke@435: duke@435: // Expected work performed by the call in units of call-overheads. duke@435: // %%% need an empirical curve fit for "work" (time in call) duke@435: float bytecodes_per_call = 3; duke@435: _work = 1.0 + code_size / bytecodes_per_call; duke@435: duke@435: // Expected size of compilation graph: duke@435: // -XX:+PrintParseStatistics once reported: duke@435: // Methods seen: 9184 Methods parsed: 9184 Nodes created: 1582391 duke@435: // Histogram of 144298 parsed bytecodes: duke@435: // %%% Need an better predictor for graph size. duke@435: _size = NODES_OVERHEAD_PER_METHOD + (NODES_PER_BYTECODE * code_size); duke@435: } duke@435: duke@435: // is_cold: Return true if the node should never be inlined. duke@435: // This is true if any of the key metrics are extreme. duke@435: bool WarmCallInfo::is_cold() const { duke@435: if (count() < WarmCallMinCount) return true; duke@435: if (profit() < WarmCallMinProfit) return true; duke@435: if (work() > WarmCallMaxWork) return true; duke@435: if (size() > WarmCallMaxSize) return true; duke@435: return false; duke@435: } duke@435: duke@435: // is_hot: Return true if the node should be inlined immediately. duke@435: // This is true if any of the key metrics are extreme. duke@435: bool WarmCallInfo::is_hot() const { duke@435: assert(!is_cold(), "eliminate is_cold cases before testing is_hot"); duke@435: if (count() >= HotCallCountThreshold) return true; duke@435: if (profit() >= HotCallProfitThreshold) return true; duke@435: if (work() <= HotCallTrivialWork) return true; duke@435: if (size() <= HotCallTrivialSize) return true; duke@435: return false; duke@435: } duke@435: duke@435: // compute_heat: duke@435: float WarmCallInfo::compute_heat() const { duke@435: assert(!is_cold(), "compute heat only on warm nodes"); duke@435: assert(!is_hot(), "compute heat only on warm nodes"); duke@435: int min_size = MAX2(0, (int)HotCallTrivialSize); duke@435: int max_size = MIN2(500, (int)WarmCallMaxSize); duke@435: float method_size = (size() - min_size) / MAX2(1, max_size - min_size); duke@435: float size_factor; duke@435: if (method_size < 0.05) size_factor = 4; // 2 sigmas better than avg. duke@435: else if (method_size < 0.15) size_factor = 2; // 1 sigma better than avg. duke@435: else if (method_size < 0.5) size_factor = 1; // better than avg. duke@435: else size_factor = 0.5; // worse than avg. duke@435: return (count() * profit() * size_factor); duke@435: } duke@435: duke@435: bool WarmCallInfo::warmer_than(WarmCallInfo* that) { duke@435: assert(this != that, "compare only different WCIs"); duke@435: assert(this->heat() != 0 && that->heat() != 0, "call compute_heat 1st"); duke@435: if (this->heat() > that->heat()) return true; duke@435: if (this->heat() < that->heat()) return false; duke@435: assert(this->heat() == that->heat(), "no NaN heat allowed"); duke@435: // Equal heat. Break the tie some other way. duke@435: if (!this->call() || !that->call()) return (address)this > (address)that; duke@435: return this->call()->_idx > that->call()->_idx; duke@435: } duke@435: duke@435: //#define UNINIT_NEXT ((WarmCallInfo*)badAddress) duke@435: #define UNINIT_NEXT ((WarmCallInfo*)NULL) duke@435: duke@435: WarmCallInfo* WarmCallInfo::insert_into(WarmCallInfo* head) { duke@435: assert(next() == UNINIT_NEXT, "not yet on any list"); duke@435: WarmCallInfo* prev_p = NULL; duke@435: WarmCallInfo* next_p = head; duke@435: while (next_p != NULL && next_p->warmer_than(this)) { duke@435: prev_p = next_p; duke@435: next_p = prev_p->next(); duke@435: } duke@435: // Install this between prev_p and next_p. duke@435: this->set_next(next_p); duke@435: if (prev_p == NULL) duke@435: head = this; duke@435: else duke@435: prev_p->set_next(this); duke@435: return head; duke@435: } duke@435: duke@435: WarmCallInfo* WarmCallInfo::remove_from(WarmCallInfo* head) { duke@435: WarmCallInfo* prev_p = NULL; duke@435: WarmCallInfo* next_p = head; duke@435: while (next_p != this) { duke@435: assert(next_p != NULL, "this must be in the list somewhere"); duke@435: prev_p = next_p; duke@435: next_p = prev_p->next(); duke@435: } duke@435: next_p = this->next(); duke@435: debug_only(this->set_next(UNINIT_NEXT)); duke@435: // Remove this from between prev_p and next_p. duke@435: if (prev_p == NULL) duke@435: head = next_p; duke@435: else duke@435: prev_p->set_next(next_p); duke@435: return head; duke@435: } duke@435: never@2725: WarmCallInfo WarmCallInfo::_always_hot(WarmCallInfo::MAX_VALUE(), WarmCallInfo::MAX_VALUE(), never@2725: WarmCallInfo::MIN_VALUE(), WarmCallInfo::MIN_VALUE()); never@2725: WarmCallInfo WarmCallInfo::_always_cold(WarmCallInfo::MIN_VALUE(), WarmCallInfo::MIN_VALUE(), never@2725: WarmCallInfo::MAX_VALUE(), WarmCallInfo::MAX_VALUE()); duke@435: duke@435: WarmCallInfo* WarmCallInfo::always_hot() { never@2725: assert(_always_hot.is_hot(), "must always be hot"); never@2725: return &_always_hot; duke@435: } duke@435: duke@435: WarmCallInfo* WarmCallInfo::always_cold() { never@2725: assert(_always_cold.is_cold(), "must always be cold"); never@2725: return &_always_cold; duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void WarmCallInfo::print() const { duke@435: tty->print("%s : C=%6.1f P=%6.1f W=%6.1f S=%6.1f H=%6.1f -> %p", duke@435: is_cold() ? "cold" : is_hot() ? "hot " : "warm", duke@435: count(), profit(), work(), size(), compute_heat(), next()); duke@435: tty->cr(); duke@435: if (call() != NULL) call()->dump(); duke@435: } duke@435: duke@435: void print_wci(WarmCallInfo* ci) { duke@435: ci->print(); duke@435: } duke@435: duke@435: void WarmCallInfo::print_all() const { duke@435: for (const WarmCallInfo* p = this; p != NULL; p = p->next()) duke@435: p->print(); duke@435: } duke@435: duke@435: int WarmCallInfo::count_all() const { duke@435: int cnt = 0; duke@435: for (const WarmCallInfo* p = this; p != NULL; p = p->next()) duke@435: cnt++; duke@435: return cnt; duke@435: } duke@435: duke@435: #endif //PRODUCT