src/share/vm/opto/callGenerator.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5991
b2ee5dc63353
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_OPTO_CALLGENERATOR_HPP
aoqi@0 26 #define SHARE_VM_OPTO_CALLGENERATOR_HPP
aoqi@0 27
aoqi@0 28 #include "compiler/compileBroker.hpp"
aoqi@0 29 #include "opto/callnode.hpp"
aoqi@0 30 #include "opto/compile.hpp"
aoqi@0 31 #include "opto/type.hpp"
aoqi@0 32 #include "runtime/deoptimization.hpp"
aoqi@0 33
aoqi@0 34 class Parse;
aoqi@0 35
aoqi@0 36 //---------------------------CallGenerator-------------------------------------
aoqi@0 37 // The subclasses of this class handle generation of ideal nodes for
aoqi@0 38 // call sites and method entry points.
aoqi@0 39
aoqi@0 40 class CallGenerator : public ResourceObj {
aoqi@0 41 public:
aoqi@0 42 enum {
aoqi@0 43 xxxunusedxxx
aoqi@0 44 };
aoqi@0 45
aoqi@0 46 private:
aoqi@0 47 ciMethod* _method; // The method being called.
aoqi@0 48
aoqi@0 49 protected:
aoqi@0 50 CallGenerator(ciMethod* method) : _method(method) {}
aoqi@0 51
aoqi@0 52 public:
aoqi@0 53 // Accessors
aoqi@0 54 ciMethod* method() const { return _method; }
aoqi@0 55
aoqi@0 56 // is_inline: At least some code implementing the method is copied here.
aoqi@0 57 virtual bool is_inline() const { return false; }
aoqi@0 58 // is_intrinsic: There's a method-specific way of generating the inline code.
aoqi@0 59 virtual bool is_intrinsic() const { return false; }
aoqi@0 60 // is_parse: Bytecodes implementing the specific method are copied here.
aoqi@0 61 virtual bool is_parse() const { return false; }
aoqi@0 62 // is_virtual: The call uses the receiver type to select or check the method.
aoqi@0 63 virtual bool is_virtual() const { return false; }
aoqi@0 64 // is_deferred: The decision whether to inline or not is deferred.
aoqi@0 65 virtual bool is_deferred() const { return false; }
aoqi@0 66 // is_predicted: Uses an explicit check against a predicted type.
aoqi@0 67 virtual bool is_predicted() const { return false; }
aoqi@0 68 // is_trap: Does not return to the caller. (E.g., uncommon trap.)
aoqi@0 69 virtual bool is_trap() const { return false; }
aoqi@0 70 // does_virtual_dispatch: Should try inlining as normal method first.
aoqi@0 71 virtual bool does_virtual_dispatch() const { return false; }
aoqi@0 72
aoqi@0 73 // is_late_inline: supports conversion of call into an inline
aoqi@0 74 virtual bool is_late_inline() const { return false; }
aoqi@0 75 // same but for method handle calls
aoqi@0 76 virtual bool is_mh_late_inline() const { return false; }
aoqi@0 77 virtual bool is_string_late_inline() const{ return false; }
aoqi@0 78
aoqi@0 79 // for method handle calls: have we tried inlinining the call already?
aoqi@0 80 virtual bool already_attempted() const { ShouldNotReachHere(); return false; }
aoqi@0 81
aoqi@0 82 // Replace the call with an inline version of the code
aoqi@0 83 virtual void do_late_inline() { ShouldNotReachHere(); }
aoqi@0 84
aoqi@0 85 virtual CallStaticJavaNode* call_node() const { ShouldNotReachHere(); return NULL; }
aoqi@0 86
aoqi@0 87 // Note: It is possible for a CG to be both inline and virtual.
aoqi@0 88 // (The hashCode intrinsic does a vtable check and an inlined fast path.)
aoqi@0 89
aoqi@0 90 // Utilities:
aoqi@0 91 const TypeFunc* tf() const;
aoqi@0 92
aoqi@0 93 // The given jvms has state and arguments for a call to my method.
aoqi@0 94 // Edges after jvms->argoff() carry all (pre-popped) argument values.
aoqi@0 95 //
aoqi@0 96 // Update the map with state and return values (if any) and return it.
aoqi@0 97 // The return values (0, 1, or 2) must be pushed on the map's stack,
aoqi@0 98 // and the sp of the jvms incremented accordingly.
aoqi@0 99 //
aoqi@0 100 // The jvms is returned on success. Alternatively, a copy of the
aoqi@0 101 // given jvms, suitably updated, may be returned, in which case the
aoqi@0 102 // caller should discard the original jvms.
aoqi@0 103 //
aoqi@0 104 // The non-Parm edges of the returned map will contain updated global state,
aoqi@0 105 // and one or two edges before jvms->sp() will carry any return values.
aoqi@0 106 // Other map edges may contain locals or monitors, and should not
aoqi@0 107 // be changed in meaning.
aoqi@0 108 //
aoqi@0 109 // If the call traps, the returned map must have a control edge of top.
aoqi@0 110 // If the call can throw, the returned map must report has_exceptions().
aoqi@0 111 //
aoqi@0 112 // If the result is NULL, it means that this CallGenerator was unable
aoqi@0 113 // to handle the given call, and another CallGenerator should be consulted.
aoqi@0 114 virtual JVMState* generate(JVMState* jvms, Parse* parent_parser) = 0;
aoqi@0 115
aoqi@0 116 // How to generate a call site that is inlined:
aoqi@0 117 static CallGenerator* for_inline(ciMethod* m, float expected_uses = -1);
aoqi@0 118 // How to generate code for an on-stack replacement handler.
aoqi@0 119 static CallGenerator* for_osr(ciMethod* m, int osr_bci);
aoqi@0 120
aoqi@0 121 // How to generate vanilla out-of-line call sites:
aoqi@0 122 static CallGenerator* for_direct_call(ciMethod* m, bool separate_io_projs = false); // static, special
aoqi@0 123 static CallGenerator* for_virtual_call(ciMethod* m, int vtable_index); // virtual, interface
aoqi@0 124 static CallGenerator* for_dynamic_call(ciMethod* m); // invokedynamic
aoqi@0 125
aoqi@0 126 static CallGenerator* for_method_handle_call( JVMState* jvms, ciMethod* caller, ciMethod* callee, bool delayed_forbidden);
aoqi@0 127 static CallGenerator* for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool& input_not_const);
aoqi@0 128
aoqi@0 129 // How to generate a replace a direct call with an inline version
aoqi@0 130 static CallGenerator* for_late_inline(ciMethod* m, CallGenerator* inline_cg);
aoqi@0 131 static CallGenerator* for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const);
aoqi@0 132 static CallGenerator* for_string_late_inline(ciMethod* m, CallGenerator* inline_cg);
aoqi@0 133 static CallGenerator* for_boxing_late_inline(ciMethod* m, CallGenerator* inline_cg);
aoqi@0 134
aoqi@0 135 // How to make a call but defer the decision whether to inline or not.
aoqi@0 136 static CallGenerator* for_warm_call(WarmCallInfo* ci,
aoqi@0 137 CallGenerator* if_cold,
aoqi@0 138 CallGenerator* if_hot);
aoqi@0 139
aoqi@0 140 // How to make a call that optimistically assumes a receiver type:
aoqi@0 141 static CallGenerator* for_predicted_call(ciKlass* predicted_receiver,
aoqi@0 142 CallGenerator* if_missed,
aoqi@0 143 CallGenerator* if_hit,
aoqi@0 144 float hit_prob);
aoqi@0 145
aoqi@0 146 // How to make a call that optimistically assumes a MethodHandle target:
aoqi@0 147 static CallGenerator* for_predicted_dynamic_call(ciMethodHandle* predicted_method_handle,
aoqi@0 148 CallGenerator* if_missed,
aoqi@0 149 CallGenerator* if_hit,
aoqi@0 150 float hit_prob);
aoqi@0 151
aoqi@0 152 // How to make a call that gives up and goes back to the interpreter:
aoqi@0 153 static CallGenerator* for_uncommon_trap(ciMethod* m,
aoqi@0 154 Deoptimization::DeoptReason reason,
aoqi@0 155 Deoptimization::DeoptAction action);
aoqi@0 156
aoqi@0 157 // Registry for intrinsics:
aoqi@0 158 static CallGenerator* for_intrinsic(ciMethod* m);
aoqi@0 159 static void register_intrinsic(ciMethod* m, CallGenerator* cg);
aoqi@0 160 static CallGenerator* for_predicted_intrinsic(CallGenerator* intrinsic,
aoqi@0 161 CallGenerator* cg);
aoqi@0 162 virtual Node* generate_predicate(JVMState* jvms) { return NULL; };
aoqi@0 163
aoqi@0 164 virtual void print_inlining_late(const char* msg) { ShouldNotReachHere(); }
aoqi@0 165
aoqi@0 166 static void print_inlining(Compile* C, ciMethod* callee, int inline_level, int bci, const char* msg) {
aoqi@0 167 if (C->print_inlining()) {
aoqi@0 168 C->print_inlining(callee, inline_level, bci, msg);
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171 };
aoqi@0 172
aoqi@0 173
aoqi@0 174 //------------------------InlineCallGenerator----------------------------------
aoqi@0 175 class InlineCallGenerator : public CallGenerator {
aoqi@0 176 protected:
aoqi@0 177 InlineCallGenerator(ciMethod* method) : CallGenerator(method) {}
aoqi@0 178
aoqi@0 179 public:
aoqi@0 180 virtual bool is_inline() const { return true; }
aoqi@0 181 };
aoqi@0 182
aoqi@0 183
aoqi@0 184 //---------------------------WarmCallInfo--------------------------------------
aoqi@0 185 // A struct to collect information about a given call site.
aoqi@0 186 // Helps sort call sites into "hot", "medium", and "cold".
aoqi@0 187 // Participates in the queueing of "medium" call sites for possible inlining.
aoqi@0 188 class WarmCallInfo : public ResourceObj {
aoqi@0 189 private:
aoqi@0 190
aoqi@0 191 CallNode* _call; // The CallNode which may be inlined.
aoqi@0 192 CallGenerator* _hot_cg;// CG for expanding the call node
aoqi@0 193
aoqi@0 194 // These are the metrics we use to evaluate call sites:
aoqi@0 195
aoqi@0 196 float _count; // How often do we expect to reach this site?
aoqi@0 197 float _profit; // How much time do we expect to save by inlining?
aoqi@0 198 float _work; // How long do we expect the average call to take?
aoqi@0 199 float _size; // How big do we expect the inlined code to be?
aoqi@0 200
aoqi@0 201 float _heat; // Combined score inducing total order on call sites.
aoqi@0 202 WarmCallInfo* _next; // Next cooler call info in pending queue.
aoqi@0 203
aoqi@0 204 // Count is the number of times this call site is expected to be executed.
aoqi@0 205 // Large count is favorable for inlining, because the extra compilation
aoqi@0 206 // work will be amortized more completely.
aoqi@0 207
aoqi@0 208 // Profit is a rough measure of the amount of time we expect to save
aoqi@0 209 // per execution of this site if we inline it. (1.0 == call overhead)
aoqi@0 210 // Large profit favors inlining. Negative profit disables inlining.
aoqi@0 211
aoqi@0 212 // Work is a rough measure of the amount of time a typical out-of-line
aoqi@0 213 // call from this site is expected to take. (1.0 == call, no-op, return)
aoqi@0 214 // Small work is somewhat favorable for inlining, since methods with
aoqi@0 215 // short "hot" traces are more likely to inline smoothly.
aoqi@0 216
aoqi@0 217 // Size is the number of graph nodes we expect this method to produce,
aoqi@0 218 // not counting the inlining of any further warm calls it may include.
aoqi@0 219 // Small size favors inlining, since small methods are more likely to
aoqi@0 220 // inline smoothly. The size is estimated by examining the native code
aoqi@0 221 // if available. The method bytecodes are also examined, assuming
aoqi@0 222 // empirically observed node counts for each kind of bytecode.
aoqi@0 223
aoqi@0 224 // Heat is the combined "goodness" of a site's inlining. If we were
aoqi@0 225 // omniscient, it would be the difference of two sums of future execution
aoqi@0 226 // times of code emitted for this site (amortized across multiple sites if
aoqi@0 227 // sharing applies). The two sums are for versions of this call site with
aoqi@0 228 // and without inlining.
aoqi@0 229
aoqi@0 230 // We approximate this mythical quantity by playing with averages,
aoqi@0 231 // rough estimates, and assumptions that history repeats itself.
aoqi@0 232 // The basic formula count * profit is heuristically adjusted
aoqi@0 233 // by looking at the expected compilation and execution times of
aoqi@0 234 // of the inlined call.
aoqi@0 235
aoqi@0 236 // Note: Some of these metrics may not be present in the final product,
aoqi@0 237 // but exist in development builds to experiment with inline policy tuning.
aoqi@0 238
aoqi@0 239 // This heuristic framework does not model well the very significant
aoqi@0 240 // effects of multiple-level inlining. It is possible to see no immediate
aoqi@0 241 // profit from inlining X->Y, but to get great profit from a subsequent
aoqi@0 242 // inlining X->Y->Z.
aoqi@0 243
aoqi@0 244 // This framework does not take well into account the problem of N**2 code
aoqi@0 245 // size in a clique of mutually inlinable methods.
aoqi@0 246
aoqi@0 247 WarmCallInfo* next() const { return _next; }
aoqi@0 248 void set_next(WarmCallInfo* n) { _next = n; }
aoqi@0 249
aoqi@0 250 static WarmCallInfo _always_hot;
aoqi@0 251 static WarmCallInfo _always_cold;
aoqi@0 252
aoqi@0 253 // Constructor intitialization of always_hot and always_cold
aoqi@0 254 WarmCallInfo(float c, float p, float w, float s) {
aoqi@0 255 _call = NULL;
aoqi@0 256 _hot_cg = NULL;
aoqi@0 257 _next = NULL;
aoqi@0 258 _count = c;
aoqi@0 259 _profit = p;
aoqi@0 260 _work = w;
aoqi@0 261 _size = s;
aoqi@0 262 _heat = 0;
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 public:
aoqi@0 266 // Because WarmInfo objects live over the entire lifetime of the
aoqi@0 267 // Compile object, they are allocated into the comp_arena, which
aoqi@0 268 // does not get resource marked or reset during the compile process
aoqi@0 269 void *operator new( size_t x, Compile* C ) throw() { return C->comp_arena()->Amalloc(x); }
aoqi@0 270 void operator delete( void * ) { } // fast deallocation
aoqi@0 271
aoqi@0 272 static WarmCallInfo* always_hot();
aoqi@0 273 static WarmCallInfo* always_cold();
aoqi@0 274
aoqi@0 275 WarmCallInfo() {
aoqi@0 276 _call = NULL;
aoqi@0 277 _hot_cg = NULL;
aoqi@0 278 _next = NULL;
aoqi@0 279 _count = _profit = _work = _size = _heat = 0;
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 CallNode* call() const { return _call; }
aoqi@0 283 float count() const { return _count; }
aoqi@0 284 float size() const { return _size; }
aoqi@0 285 float work() const { return _work; }
aoqi@0 286 float profit() const { return _profit; }
aoqi@0 287 float heat() const { return _heat; }
aoqi@0 288
aoqi@0 289 void set_count(float x) { _count = x; }
aoqi@0 290 void set_size(float x) { _size = x; }
aoqi@0 291 void set_work(float x) { _work = x; }
aoqi@0 292 void set_profit(float x) { _profit = x; }
aoqi@0 293 void set_heat(float x) { _heat = x; }
aoqi@0 294
aoqi@0 295 // Load initial heuristics from profiles, etc.
aoqi@0 296 // The heuristics can be tweaked further by the caller.
aoqi@0 297 void init(JVMState* call_site, ciMethod* call_method, ciCallProfile& profile, float prof_factor);
aoqi@0 298
aoqi@0 299 static float MAX_VALUE() { return +1.0e10; }
aoqi@0 300 static float MIN_VALUE() { return -1.0e10; }
aoqi@0 301
aoqi@0 302 float compute_heat() const;
aoqi@0 303
aoqi@0 304 void set_call(CallNode* call) { _call = call; }
aoqi@0 305 void set_hot_cg(CallGenerator* cg) { _hot_cg = cg; }
aoqi@0 306
aoqi@0 307 // Do not queue very hot or very cold calls.
aoqi@0 308 // Make very cold ones out of line immediately.
aoqi@0 309 // Inline very hot ones immediately.
aoqi@0 310 // These queries apply various tunable limits
aoqi@0 311 // to the above metrics in a systematic way.
aoqi@0 312 // Test for coldness before testing for hotness.
aoqi@0 313 bool is_cold() const;
aoqi@0 314 bool is_hot() const;
aoqi@0 315
aoqi@0 316 // Force a warm call to be hot. This worklists the call node for inlining.
aoqi@0 317 void make_hot();
aoqi@0 318
aoqi@0 319 // Force a warm call to be cold. This worklists the call node for out-of-lining.
aoqi@0 320 void make_cold();
aoqi@0 321
aoqi@0 322 // A reproducible total ordering, in which heat is the major key.
aoqi@0 323 bool warmer_than(WarmCallInfo* that);
aoqi@0 324
aoqi@0 325 // List management. These methods are called with the list head,
aoqi@0 326 // and return the new list head, inserting or removing the receiver.
aoqi@0 327 WarmCallInfo* insert_into(WarmCallInfo* head);
aoqi@0 328 WarmCallInfo* remove_from(WarmCallInfo* head);
aoqi@0 329
aoqi@0 330 #ifndef PRODUCT
aoqi@0 331 void print() const;
aoqi@0 332 void print_all() const;
aoqi@0 333 int count_all() const;
aoqi@0 334 #endif
aoqi@0 335 };
aoqi@0 336
aoqi@0 337 #endif // SHARE_VM_OPTO_CALLGENERATOR_HPP

mercurial