Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/callGenerator.hpp
32285 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_OPTO_CALLGENERATOR_HPP25#define SHARE_VM_OPTO_CALLGENERATOR_HPP2627#include "compiler/compileBroker.hpp"28#include "opto/callnode.hpp"29#include "opto/compile.hpp"30#include "opto/type.hpp"31#include "runtime/deoptimization.hpp"3233//---------------------------CallGenerator-------------------------------------34// The subclasses of this class handle generation of ideal nodes for35// call sites and method entry points.3637class CallGenerator : public ResourceObj {38public:39enum {40xxxunusedxxx41};4243private:44ciMethod* _method; // The method being called.4546protected:47CallGenerator(ciMethod* method) : _method(method) {}4849public:50// Accessors51ciMethod* method() const { return _method; }5253// is_inline: At least some code implementing the method is copied here.54virtual bool is_inline() const { return false; }55// is_intrinsic: There's a method-specific way of generating the inline code.56virtual bool is_intrinsic() const { return false; }57// is_parse: Bytecodes implementing the specific method are copied here.58virtual bool is_parse() const { return false; }59// is_virtual: The call uses the receiver type to select or check the method.60virtual bool is_virtual() const { return false; }61// is_deferred: The decision whether to inline or not is deferred.62virtual bool is_deferred() const { return false; }63// is_predicated: Uses an explicit check (predicate).64virtual bool is_predicated() const { return false; }65virtual int predicates_count() const { return 0; }66// is_trap: Does not return to the caller. (E.g., uncommon trap.)67virtual bool is_trap() const { return false; }68// does_virtual_dispatch: Should try inlining as normal method first.69virtual bool does_virtual_dispatch() const { return false; }7071// is_late_inline: supports conversion of call into an inline72virtual bool is_late_inline() const { return false; }73// same but for method handle calls74virtual bool is_mh_late_inline() const { return false; }75virtual bool is_string_late_inline() const{ return false; }7677// for method handle calls: have we tried inlinining the call already?78virtual bool already_attempted() const { ShouldNotReachHere(); return false; }7980// Replace the call with an inline version of the code81virtual void do_late_inline() { ShouldNotReachHere(); }8283virtual CallStaticJavaNode* call_node() const { ShouldNotReachHere(); return NULL; }8485// Note: It is possible for a CG to be both inline and virtual.86// (The hashCode intrinsic does a vtable check and an inlined fast path.)8788// Utilities:89const TypeFunc* tf() const;9091// The given jvms has state and arguments for a call to my method.92// Edges after jvms->argoff() carry all (pre-popped) argument values.93//94// Update the map with state and return values (if any) and return it.95// The return values (0, 1, or 2) must be pushed on the map's stack,96// and the sp of the jvms incremented accordingly.97//98// The jvms is returned on success. Alternatively, a copy of the99// given jvms, suitably updated, may be returned, in which case the100// caller should discard the original jvms.101//102// The non-Parm edges of the returned map will contain updated global state,103// and one or two edges before jvms->sp() will carry any return values.104// Other map edges may contain locals or monitors, and should not105// be changed in meaning.106//107// If the call traps, the returned map must have a control edge of top.108// If the call can throw, the returned map must report has_exceptions().109//110// If the result is NULL, it means that this CallGenerator was unable111// to handle the given call, and another CallGenerator should be consulted.112virtual JVMState* generate(JVMState* jvms) = 0;113114// How to generate a call site that is inlined:115static CallGenerator* for_inline(ciMethod* m, float expected_uses = -1);116// How to generate code for an on-stack replacement handler.117static CallGenerator* for_osr(ciMethod* m, int osr_bci);118119// How to generate vanilla out-of-line call sites:120static CallGenerator* for_direct_call(ciMethod* m, bool separate_io_projs = false); // static, special121static CallGenerator* for_virtual_call(ciMethod* m, int vtable_index); // virtual, interface122static CallGenerator* for_dynamic_call(ciMethod* m); // invokedynamic123124static CallGenerator* for_method_handle_call( JVMState* jvms, ciMethod* caller, ciMethod* callee, bool delayed_forbidden);125static CallGenerator* for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool& input_not_const);126127// How to generate a replace a direct call with an inline version128static CallGenerator* for_late_inline(ciMethod* m, CallGenerator* inline_cg);129static CallGenerator* for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const);130static CallGenerator* for_string_late_inline(ciMethod* m, CallGenerator* inline_cg);131static CallGenerator* for_boxing_late_inline(ciMethod* m, CallGenerator* inline_cg);132133// How to make a call but defer the decision whether to inline or not.134static CallGenerator* for_warm_call(WarmCallInfo* ci,135CallGenerator* if_cold,136CallGenerator* if_hot);137138// How to make a call that optimistically assumes a receiver type:139static CallGenerator* for_predicted_call(ciKlass* predicted_receiver,140CallGenerator* if_missed,141CallGenerator* if_hit,142float hit_prob);143144// How to make a call that optimistically assumes a MethodHandle target:145static CallGenerator* for_predicted_dynamic_call(ciMethodHandle* predicted_method_handle,146CallGenerator* if_missed,147CallGenerator* if_hit,148float hit_prob);149150// How to make a call that gives up and goes back to the interpreter:151static CallGenerator* for_uncommon_trap(ciMethod* m,152Deoptimization::DeoptReason reason,153Deoptimization::DeoptAction action);154155// Registry for intrinsics:156static CallGenerator* for_intrinsic(ciMethod* m);157static void register_intrinsic(ciMethod* m, CallGenerator* cg);158static CallGenerator* for_predicated_intrinsic(CallGenerator* intrinsic,159CallGenerator* cg);160virtual Node* generate_predicate(JVMState* jvms, int predicate) { return NULL; };161162virtual void print_inlining_late(const char* msg) { ShouldNotReachHere(); }163164static void print_inlining(Compile* C, ciMethod* callee, int inline_level, int bci, const char* msg) {165if (C->print_inlining()) {166C->print_inlining(callee, inline_level, bci, msg);167}168}169};170171172//------------------------InlineCallGenerator----------------------------------173class InlineCallGenerator : public CallGenerator {174protected:175InlineCallGenerator(ciMethod* method) : CallGenerator(method) {}176177public:178virtual bool is_inline() const { return true; }179};180181182//---------------------------WarmCallInfo--------------------------------------183// A struct to collect information about a given call site.184// Helps sort call sites into "hot", "medium", and "cold".185// Participates in the queueing of "medium" call sites for possible inlining.186class WarmCallInfo : public ResourceObj {187private:188189CallNode* _call; // The CallNode which may be inlined.190CallGenerator* _hot_cg;// CG for expanding the call node191192// These are the metrics we use to evaluate call sites:193194float _count; // How often do we expect to reach this site?195float _profit; // How much time do we expect to save by inlining?196float _work; // How long do we expect the average call to take?197float _size; // How big do we expect the inlined code to be?198199float _heat; // Combined score inducing total order on call sites.200WarmCallInfo* _next; // Next cooler call info in pending queue.201202// Count is the number of times this call site is expected to be executed.203// Large count is favorable for inlining, because the extra compilation204// work will be amortized more completely.205206// Profit is a rough measure of the amount of time we expect to save207// per execution of this site if we inline it. (1.0 == call overhead)208// Large profit favors inlining. Negative profit disables inlining.209210// Work is a rough measure of the amount of time a typical out-of-line211// call from this site is expected to take. (1.0 == call, no-op, return)212// Small work is somewhat favorable for inlining, since methods with213// short "hot" traces are more likely to inline smoothly.214215// Size is the number of graph nodes we expect this method to produce,216// not counting the inlining of any further warm calls it may include.217// Small size favors inlining, since small methods are more likely to218// inline smoothly. The size is estimated by examining the native code219// if available. The method bytecodes are also examined, assuming220// empirically observed node counts for each kind of bytecode.221222// Heat is the combined "goodness" of a site's inlining. If we were223// omniscient, it would be the difference of two sums of future execution224// times of code emitted for this site (amortized across multiple sites if225// sharing applies). The two sums are for versions of this call site with226// and without inlining.227228// We approximate this mythical quantity by playing with averages,229// rough estimates, and assumptions that history repeats itself.230// The basic formula count * profit is heuristically adjusted231// by looking at the expected compilation and execution times of232// of the inlined call.233234// Note: Some of these metrics may not be present in the final product,235// but exist in development builds to experiment with inline policy tuning.236237// This heuristic framework does not model well the very significant238// effects of multiple-level inlining. It is possible to see no immediate239// profit from inlining X->Y, but to get great profit from a subsequent240// inlining X->Y->Z.241242// This framework does not take well into account the problem of N**2 code243// size in a clique of mutually inlinable methods.244245WarmCallInfo* next() const { return _next; }246void set_next(WarmCallInfo* n) { _next = n; }247248static WarmCallInfo _always_hot;249static WarmCallInfo _always_cold;250251// Constructor intitialization of always_hot and always_cold252WarmCallInfo(float c, float p, float w, float s) {253_call = NULL;254_hot_cg = NULL;255_next = NULL;256_count = c;257_profit = p;258_work = w;259_size = s;260_heat = 0;261}262263public:264// Because WarmInfo objects live over the entire lifetime of the265// Compile object, they are allocated into the comp_arena, which266// does not get resource marked or reset during the compile process267void *operator new( size_t x, Compile* C ) throw() { return C->comp_arena()->Amalloc(x); }268void operator delete( void * ) { } // fast deallocation269270static WarmCallInfo* always_hot();271static WarmCallInfo* always_cold();272273WarmCallInfo() {274_call = NULL;275_hot_cg = NULL;276_next = NULL;277_count = _profit = _work = _size = _heat = 0;278}279280CallNode* call() const { return _call; }281float count() const { return _count; }282float size() const { return _size; }283float work() const { return _work; }284float profit() const { return _profit; }285float heat() const { return _heat; }286287void set_count(float x) { _count = x; }288void set_size(float x) { _size = x; }289void set_work(float x) { _work = x; }290void set_profit(float x) { _profit = x; }291void set_heat(float x) { _heat = x; }292293// Load initial heuristics from profiles, etc.294// The heuristics can be tweaked further by the caller.295void init(JVMState* call_site, ciMethod* call_method, ciCallProfile& profile, float prof_factor);296297static float MAX_VALUE() { return +1.0e10; }298static float MIN_VALUE() { return -1.0e10; }299300float compute_heat() const;301302void set_call(CallNode* call) { _call = call; }303void set_hot_cg(CallGenerator* cg) { _hot_cg = cg; }304305// Do not queue very hot or very cold calls.306// Make very cold ones out of line immediately.307// Inline very hot ones immediately.308// These queries apply various tunable limits309// to the above metrics in a systematic way.310// Test for coldness before testing for hotness.311bool is_cold() const;312bool is_hot() const;313314// Force a warm call to be hot. This worklists the call node for inlining.315void make_hot();316317// Force a warm call to be cold. This worklists the call node for out-of-lining.318void make_cold();319320// A reproducible total ordering, in which heat is the major key.321bool warmer_than(WarmCallInfo* that);322323// List management. These methods are called with the list head,324// and return the new list head, inserting or removing the receiver.325WarmCallInfo* insert_into(WarmCallInfo* head);326WarmCallInfo* remove_from(WarmCallInfo* head);327328#ifndef PRODUCT329void print() const;330void print_all() const;331int count_all() const;332#endif333};334335#endif // SHARE_VM_OPTO_CALLGENERATOR_HPP336337338