src/share/vm/opto/callGenerator.hpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial