src/share/vm/opto/callGenerator.cpp

Wed, 13 Aug 2014 11:00:22 +0200

author
roland
date
Wed, 13 Aug 2014 11:00:22 +0200
changeset 7041
411e30e5fbb8
parent 7026
922c87c9aed4
child 7287
8ed0a8dbea70
permissions
-rw-r--r--

8026796: Make replace_in_map() on parent maps generic
Summary: propagate node replacements along control flow edges to callers
Reviewed-by: kvn, vlivanov

duke@435 1 /*
mikael@6198 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "ci/bcEscapeAnalyzer.hpp"
twisti@3050 27 #include "ci/ciCallSite.hpp"
coleenp@4037 28 #include "ci/ciObjArray.hpp"
twisti@3969 29 #include "ci/ciMemberName.hpp"
stefank@2314 30 #include "ci/ciMethodHandle.hpp"
stefank@2314 31 #include "classfile/javaClasses.hpp"
stefank@2314 32 #include "compiler/compileLog.hpp"
stefank@2314 33 #include "opto/addnode.hpp"
stefank@2314 34 #include "opto/callGenerator.hpp"
stefank@2314 35 #include "opto/callnode.hpp"
stefank@2314 36 #include "opto/cfgnode.hpp"
stefank@2314 37 #include "opto/connode.hpp"
stefank@2314 38 #include "opto/parse.hpp"
stefank@2314 39 #include "opto/rootnode.hpp"
stefank@2314 40 #include "opto/runtime.hpp"
stefank@2314 41 #include "opto/subnode.hpp"
duke@435 42
duke@435 43
duke@435 44 // Utility function.
duke@435 45 const TypeFunc* CallGenerator::tf() const {
duke@435 46 return TypeFunc::make(method());
duke@435 47 }
duke@435 48
duke@435 49 //-----------------------------ParseGenerator---------------------------------
duke@435 50 // Internal class which handles all direct bytecode traversal.
duke@435 51 class ParseGenerator : public InlineCallGenerator {
duke@435 52 private:
duke@435 53 bool _is_osr;
duke@435 54 float _expected_uses;
duke@435 55
duke@435 56 public:
duke@435 57 ParseGenerator(ciMethod* method, float expected_uses, bool is_osr = false)
duke@435 58 : InlineCallGenerator(method)
duke@435 59 {
duke@435 60 _is_osr = is_osr;
duke@435 61 _expected_uses = expected_uses;
twisti@3100 62 assert(InlineTree::check_can_parse(method) == NULL, "parse must be possible");
duke@435 63 }
duke@435 64
duke@435 65 virtual bool is_parse() const { return true; }
roland@7041 66 virtual JVMState* generate(JVMState* jvms);
duke@435 67 int is_osr() { return _is_osr; }
duke@435 68
duke@435 69 };
duke@435 70
roland@7041 71 JVMState* ParseGenerator::generate(JVMState* jvms) {
duke@435 72 Compile* C = Compile::current();
duke@435 73
duke@435 74 if (is_osr()) {
duke@435 75 // The JVMS for a OSR has a single argument (see its TypeFunc).
duke@435 76 assert(jvms->depth() == 1, "no inline OSR");
duke@435 77 }
duke@435 78
duke@435 79 if (C->failing()) {
duke@435 80 return NULL; // bailing out of the compile; do not try to parse
duke@435 81 }
duke@435 82
roland@7041 83 Parse parser(jvms, method(), _expected_uses);
duke@435 84 // Grab signature for matching/allocation
duke@435 85 #ifdef ASSERT
duke@435 86 if (parser.tf() != (parser.depth() == 1 ? C->tf() : tf())) {
duke@435 87 MutexLockerEx ml(Compile_lock, Mutex::_no_safepoint_check_flag);
duke@435 88 assert(C->env()->system_dictionary_modification_counter_changed(),
duke@435 89 "Must invalidate if TypeFuncs differ");
duke@435 90 }
duke@435 91 #endif
duke@435 92
duke@435 93 GraphKit& exits = parser.exits();
duke@435 94
duke@435 95 if (C->failing()) {
duke@435 96 while (exits.pop_exception_state() != NULL) ;
duke@435 97 return NULL;
duke@435 98 }
duke@435 99
duke@435 100 assert(exits.jvms()->same_calls_as(jvms), "sanity");
duke@435 101
duke@435 102 // Simply return the exit state of the parser,
duke@435 103 // augmented by any exceptional states.
duke@435 104 return exits.transfer_exceptions_into_jvms();
duke@435 105 }
duke@435 106
duke@435 107 //---------------------------DirectCallGenerator------------------------------
duke@435 108 // Internal class which handles all out-of-line calls w/o receiver type checks.
duke@435 109 class DirectCallGenerator : public CallGenerator {
never@1515 110 private:
never@1515 111 CallStaticJavaNode* _call_node;
never@1515 112 // Force separate memory and I/O projections for the exceptional
never@1515 113 // paths to facilitate late inlinig.
never@1515 114 bool _separate_io_proj;
never@1515 115
never@1515 116 public:
never@1515 117 DirectCallGenerator(ciMethod* method, bool separate_io_proj)
never@1515 118 : CallGenerator(method),
never@1515 119 _separate_io_proj(separate_io_proj)
duke@435 120 {
duke@435 121 }
roland@7041 122 virtual JVMState* generate(JVMState* jvms);
never@1515 123
never@1515 124 CallStaticJavaNode* call_node() const { return _call_node; }
duke@435 125 };
duke@435 126
roland@7041 127 JVMState* DirectCallGenerator::generate(JVMState* jvms) {
duke@435 128 GraphKit kit(jvms);
duke@435 129 bool is_static = method()->is_static();
duke@435 130 address target = is_static ? SharedRuntime::get_resolve_static_call_stub()
duke@435 131 : SharedRuntime::get_resolve_opt_virtual_call_stub();
duke@435 132
duke@435 133 if (kit.C->log() != NULL) {
duke@435 134 kit.C->log()->elem("direct_call bci='%d'", jvms->bci());
duke@435 135 }
duke@435 136
kvn@5110 137 CallStaticJavaNode *call = new (kit.C) CallStaticJavaNode(kit.C, tf(), target, method(), kit.bci());
never@3745 138 _call_node = call; // Save the call node in case we need it later
duke@435 139 if (!is_static) {
duke@435 140 // Make an explicit receiver null_check as part of this call.
duke@435 141 // Since we share a map with the caller, his JVMS gets adjusted.
twisti@4313 142 kit.null_check_receiver_before_call(method());
duke@435 143 if (kit.stopped()) {
duke@435 144 // And dump it back to the caller, decorated with any exceptions:
duke@435 145 return kit.transfer_exceptions_into_jvms();
duke@435 146 }
duke@435 147 // Mark the call node as virtual, sort of:
duke@435 148 call->set_optimized_virtual(true);
twisti@3969 149 if (method()->is_method_handle_intrinsic() ||
twisti@3969 150 method()->is_compiled_lambda_form()) {
twisti@1572 151 call->set_method_handle_invoke(true);
twisti@1700 152 }
duke@435 153 }
duke@435 154 kit.set_arguments_for_java_call(call);
never@1515 155 kit.set_edges_for_java_call(call, false, _separate_io_proj);
never@1515 156 Node* ret = kit.set_results_for_java_call(call, _separate_io_proj);
duke@435 157 kit.push_node(method()->return_type()->basic_type(), ret);
duke@435 158 return kit.transfer_exceptions_into_jvms();
duke@435 159 }
duke@435 160
twisti@1572 161 //--------------------------VirtualCallGenerator------------------------------
twisti@1572 162 // Internal class which handles all out-of-line calls checking receiver type.
duke@435 163 class VirtualCallGenerator : public CallGenerator {
duke@435 164 private:
duke@435 165 int _vtable_index;
duke@435 166 public:
duke@435 167 VirtualCallGenerator(ciMethod* method, int vtable_index)
duke@435 168 : CallGenerator(method), _vtable_index(vtable_index)
duke@435 169 {
coleenp@4037 170 assert(vtable_index == Method::invalid_vtable_index ||
duke@435 171 vtable_index >= 0, "either invalid or usable");
duke@435 172 }
duke@435 173 virtual bool is_virtual() const { return true; }
roland@7041 174 virtual JVMState* generate(JVMState* jvms);
duke@435 175 };
duke@435 176
roland@7041 177 JVMState* VirtualCallGenerator::generate(JVMState* jvms) {
duke@435 178 GraphKit kit(jvms);
duke@435 179 Node* receiver = kit.argument(0);
duke@435 180
duke@435 181 if (kit.C->log() != NULL) {
duke@435 182 kit.C->log()->elem("virtual_call bci='%d'", jvms->bci());
duke@435 183 }
duke@435 184
duke@435 185 // If the receiver is a constant null, do not torture the system
duke@435 186 // by attempting to call through it. The compile will proceed
duke@435 187 // correctly, but may bail out in final_graph_reshaping, because
duke@435 188 // the call instruction will have a seemingly deficient out-count.
duke@435 189 // (The bailout says something misleading about an "infinite loop".)
duke@435 190 if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) {
duke@435 191 kit.inc_sp(method()->arg_size()); // restore arguments
duke@435 192 kit.uncommon_trap(Deoptimization::Reason_null_check,
duke@435 193 Deoptimization::Action_none,
duke@435 194 NULL, "null receiver");
duke@435 195 return kit.transfer_exceptions_into_jvms();
duke@435 196 }
duke@435 197
duke@435 198 // Ideally we would unconditionally do a null check here and let it
duke@435 199 // be converted to an implicit check based on profile information.
duke@435 200 // However currently the conversion to implicit null checks in
duke@435 201 // Block::implicit_null_check() only looks for loads and stores, not calls.
duke@435 202 ciMethod *caller = kit.method();
duke@435 203 ciMethodData *caller_md = (caller == NULL) ? NULL : caller->method_data();
goetz@6486 204 if (!UseInlineCaches || !ImplicitNullChecks || !os::zero_page_read_protected() ||
duke@435 205 ((ImplicitNullCheckThreshold > 0) && caller_md &&
duke@435 206 (caller_md->trap_count(Deoptimization::Reason_null_check)
duke@435 207 >= (uint)ImplicitNullCheckThreshold))) {
duke@435 208 // Make an explicit receiver null_check as part of this call.
duke@435 209 // Since we share a map with the caller, his JVMS gets adjusted.
twisti@4313 210 receiver = kit.null_check_receiver_before_call(method());
duke@435 211 if (kit.stopped()) {
duke@435 212 // And dump it back to the caller, decorated with any exceptions:
duke@435 213 return kit.transfer_exceptions_into_jvms();
duke@435 214 }
duke@435 215 }
duke@435 216
duke@435 217 assert(!method()->is_static(), "virtual call must not be to static");
duke@435 218 assert(!method()->is_final(), "virtual call should not be to final");
duke@435 219 assert(!method()->is_private(), "virtual call should not be to private");
coleenp@4037 220 assert(_vtable_index == Method::invalid_vtable_index || !UseInlineCaches,
duke@435 221 "no vtable calls if +UseInlineCaches ");
duke@435 222 address target = SharedRuntime::get_resolve_virtual_call_stub();
duke@435 223 // Normal inline cache used for call
kvn@4115 224 CallDynamicJavaNode *call = new (kit.C) CallDynamicJavaNode(tf(), target, method(), _vtable_index, kit.bci());
duke@435 225 kit.set_arguments_for_java_call(call);
duke@435 226 kit.set_edges_for_java_call(call);
duke@435 227 Node* ret = kit.set_results_for_java_call(call);
duke@435 228 kit.push_node(method()->return_type()->basic_type(), ret);
duke@435 229
duke@435 230 // Represent the effect of an implicit receiver null_check
duke@435 231 // as part of this call. Since we share a map with the caller,
duke@435 232 // his JVMS gets adjusted.
duke@435 233 kit.cast_not_null(receiver);
duke@435 234 return kit.transfer_exceptions_into_jvms();
duke@435 235 }
duke@435 236
duke@435 237 CallGenerator* CallGenerator::for_inline(ciMethod* m, float expected_uses) {
twisti@3100 238 if (InlineTree::check_can_parse(m) != NULL) return NULL;
duke@435 239 return new ParseGenerator(m, expected_uses);
duke@435 240 }
duke@435 241
duke@435 242 // As a special case, the JVMS passed to this CallGenerator is
duke@435 243 // for the method execution already in progress, not just the JVMS
duke@435 244 // of the caller. Thus, this CallGenerator cannot be mixed with others!
duke@435 245 CallGenerator* CallGenerator::for_osr(ciMethod* m, int osr_bci) {
twisti@3100 246 if (InlineTree::check_can_parse(m) != NULL) return NULL;
duke@435 247 float past_uses = m->interpreter_invocation_count();
duke@435 248 float expected_uses = past_uses;
duke@435 249 return new ParseGenerator(m, expected_uses, true);
duke@435 250 }
duke@435 251
never@1515 252 CallGenerator* CallGenerator::for_direct_call(ciMethod* m, bool separate_io_proj) {
duke@435 253 assert(!m->is_abstract(), "for_direct_call mismatch");
never@1515 254 return new DirectCallGenerator(m, separate_io_proj);
duke@435 255 }
duke@435 256
duke@435 257 CallGenerator* CallGenerator::for_virtual_call(ciMethod* m, int vtable_index) {
duke@435 258 assert(!m->is_static(), "for_virtual_call mismatch");
twisti@3969 259 assert(!m->is_method_handle_intrinsic(), "should be a direct call");
duke@435 260 return new VirtualCallGenerator(m, vtable_index);
duke@435 261 }
duke@435 262
never@1515 263 // Allow inlining decisions to be delayed
never@1515 264 class LateInlineCallGenerator : public DirectCallGenerator {
roland@4409 265 protected:
never@1515 266 CallGenerator* _inline_cg;
never@1515 267
roland@4409 268 virtual bool do_late_inline_check(JVMState* jvms) { return true; }
roland@4409 269
never@1515 270 public:
never@1515 271 LateInlineCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
never@1515 272 DirectCallGenerator(method, true), _inline_cg(inline_cg) {}
never@1515 273
never@1515 274 virtual bool is_late_inline() const { return true; }
never@1515 275
never@1515 276 // Convert the CallStaticJava into an inline
never@1515 277 virtual void do_late_inline();
never@1515 278
roland@7041 279 virtual JVMState* generate(JVMState* jvms) {
roland@4357 280 Compile *C = Compile::current();
roland@4357 281 C->print_inlining_skip(this);
roland@4357 282
never@1515 283 // Record that this call site should be revisited once the main
never@1515 284 // parse is finished.
roland@4409 285 if (!is_mh_late_inline()) {
roland@4409 286 C->add_late_inline(this);
roland@4409 287 }
never@1515 288
never@1515 289 // Emit the CallStaticJava and request separate projections so
never@1515 290 // that the late inlining logic can distinguish between fall
never@1515 291 // through and exceptional uses of the memory and io projections
never@1515 292 // as is done for allocations and macro expansion.
roland@7041 293 return DirectCallGenerator::generate(jvms);
never@1515 294 }
roland@4409 295
roland@4409 296 virtual void print_inlining_late(const char* msg) {
roland@4409 297 CallNode* call = call_node();
roland@4409 298 Compile* C = Compile::current();
roland@4409 299 C->print_inlining_insert(this);
roland@4409 300 C->print_inlining(method(), call->jvms()->depth()-1, call->jvms()->bci(), msg);
roland@4409 301 }
roland@4409 302
never@1515 303 };
never@1515 304
never@1515 305 void LateInlineCallGenerator::do_late_inline() {
never@1515 306 // Can't inline it
kvn@5110 307 CallStaticJavaNode* call = call_node();
kvn@5110 308 if (call == NULL || call->outcnt() == 0 ||
kvn@5110 309 call->in(0) == NULL || call->in(0)->is_top()) {
never@1515 310 return;
roland@4538 311 }
never@1515 312
kvn@5110 313 const TypeTuple *r = call->tf()->domain();
roland@4409 314 for (int i1 = 0; i1 < method()->arg_size(); i1++) {
kvn@5110 315 if (call->in(TypeFunc::Parms + i1)->is_top() && r->field_at(TypeFunc::Parms + i1) != Type::HALF) {
roland@4409 316 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
roland@4409 317 return;
roland@4409 318 }
roland@4409 319 }
roland@4409 320
kvn@5110 321 if (call->in(TypeFunc::Memory)->is_top()) {
roland@4409 322 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
roland@4409 323 return;
roland@4409 324 }
roland@4409 325
kvn@5110 326 Compile* C = Compile::current();
kvn@5110 327 // Remove inlined methods from Compiler's lists.
kvn@5110 328 if (call->is_macro()) {
kvn@5110 329 C->remove_macro_node(call);
kvn@5110 330 }
never@1515 331
never@1515 332 // Make a clone of the JVMState that appropriate to use for driving a parse
kvn@5110 333 JVMState* old_jvms = call->jvms();
kvn@5110 334 JVMState* jvms = old_jvms->clone_shallow(C);
never@1515 335 uint size = call->req();
kvn@4115 336 SafePointNode* map = new (C) SafePointNode(size, jvms);
never@1515 337 for (uint i1 = 0; i1 < size; i1++) {
never@1515 338 map->init_req(i1, call->in(i1));
never@1515 339 }
never@1515 340
never@1515 341 // Make sure the state is a MergeMem for parsing.
never@1515 342 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
roland@4357 343 Node* mem = MergeMemNode::make(C, map->in(TypeFunc::Memory));
roland@4357 344 C->initial_gvn()->set_type_bottom(mem);
roland@4357 345 map->set_req(TypeFunc::Memory, mem);
never@1515 346 }
never@1515 347
kvn@5110 348 uint nargs = method()->arg_size();
kvn@5110 349 // blow away old call arguments
kvn@5110 350 Node* top = C->top();
kvn@5110 351 for (uint i1 = 0; i1 < nargs; i1++) {
kvn@5110 352 map->set_req(TypeFunc::Parms + i1, top);
kvn@5110 353 }
never@1515 354 jvms->set_map(map);
kvn@5110 355
kvn@5110 356 // Make enough space in the expression stack to transfer
kvn@5110 357 // the incoming arguments and return value.
never@1515 358 map->ensure_stack(jvms, jvms->method()->max_stack());
kvn@5110 359 for (uint i1 = 0; i1 < nargs; i1++) {
kvn@5110 360 map->set_argument(jvms, i1, call->in(TypeFunc::Parms + i1));
never@1515 361 }
never@1515 362
kvn@5110 363 // This check is done here because for_method_handle_inline() method
kvn@5110 364 // needs jvms for inlined state.
roland@4409 365 if (!do_late_inline_check(jvms)) {
roland@4409 366 map->disconnect_inputs(NULL, C);
roland@4409 367 return;
roland@4409 368 }
roland@4409 369
roland@4357 370 C->print_inlining_insert(this);
roland@4357 371
never@1515 372 CompileLog* log = C->log();
never@1515 373 if (log != NULL) {
never@1515 374 log->head("late_inline method='%d'", log->identify(method()));
never@1515 375 JVMState* p = jvms;
never@1515 376 while (p != NULL) {
never@1515 377 log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
never@1515 378 p = p->caller();
never@1515 379 }
never@1515 380 log->tail("late_inline");
never@1515 381 }
never@1515 382
never@1515 383 // Setup default node notes to be picked up by the inlining
kvn@6679 384 Node_Notes* old_nn = C->node_notes_at(call->_idx);
never@1515 385 if (old_nn != NULL) {
never@1515 386 Node_Notes* entry_nn = old_nn->clone(C);
never@1515 387 entry_nn->set_jvms(jvms);
never@1515 388 C->set_default_node_notes(entry_nn);
never@1515 389 }
never@1515 390
never@1515 391 // Now perform the inling using the synthesized JVMState
roland@7041 392 JVMState* new_jvms = _inline_cg->generate(jvms);
never@1515 393 if (new_jvms == NULL) return; // no change
never@1515 394 if (C->failing()) return;
never@1515 395
never@1515 396 // Capture any exceptional control flow
never@1515 397 GraphKit kit(new_jvms);
never@1515 398
never@1515 399 // Find the result object
never@1515 400 Node* result = C->top();
never@1515 401 int result_size = method()->return_type()->size();
never@1515 402 if (result_size != 0 && !kit.stopped()) {
never@1515 403 result = (result_size == 1) ? kit.pop() : kit.pop_pair();
never@1515 404 }
never@1515 405
roland@4409 406 C->set_has_loops(C->has_loops() || _inline_cg->method()->has_loops());
roland@4409 407 C->env()->notice_inlined_method(_inline_cg->method());
roland@4409 408 C->set_inlining_progress(true);
roland@4409 409
roland@7041 410 kit.replace_call(call, result, true);
never@1515 411 }
never@1515 412
never@1515 413
never@1515 414 CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) {
never@1515 415 return new LateInlineCallGenerator(method, inline_cg);
never@1515 416 }
never@1515 417
roland@4409 418 class LateInlineMHCallGenerator : public LateInlineCallGenerator {
roland@4409 419 ciMethod* _caller;
roland@4409 420 int _attempt;
roland@4409 421 bool _input_not_const;
roland@4409 422
roland@4409 423 virtual bool do_late_inline_check(JVMState* jvms);
roland@4409 424 virtual bool already_attempted() const { return _attempt > 0; }
roland@4409 425
roland@4409 426 public:
roland@4409 427 LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) :
roland@4409 428 LateInlineCallGenerator(callee, NULL), _caller(caller), _attempt(0), _input_not_const(input_not_const) {}
roland@4409 429
roland@4409 430 virtual bool is_mh_late_inline() const { return true; }
roland@4409 431
roland@7041 432 virtual JVMState* generate(JVMState* jvms) {
roland@7041 433 JVMState* new_jvms = LateInlineCallGenerator::generate(jvms);
roland@4409 434 if (_input_not_const) {
roland@4409 435 // inlining won't be possible so no need to enqueue right now.
roland@4409 436 call_node()->set_generator(this);
roland@4409 437 } else {
roland@4409 438 Compile::current()->add_late_inline(this);
roland@4409 439 }
roland@4409 440 return new_jvms;
roland@4409 441 }
roland@4409 442
roland@4409 443 virtual void print_inlining_late(const char* msg) {
roland@4409 444 if (!_input_not_const) return;
roland@4409 445 LateInlineCallGenerator::print_inlining_late(msg);
roland@4409 446 }
roland@4409 447 };
roland@4409 448
roland@4409 449 bool LateInlineMHCallGenerator::do_late_inline_check(JVMState* jvms) {
roland@4409 450
roland@4409 451 CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), _input_not_const);
roland@4409 452
roland@4409 453 if (!_input_not_const) {
roland@4409 454 _attempt++;
roland@4409 455 }
roland@4409 456
roland@4409 457 if (cg != NULL) {
roland@4409 458 assert(!cg->is_late_inline() && cg->is_inline(), "we're doing late inlining");
roland@4409 459 _inline_cg = cg;
roland@4409 460 Compile::current()->dec_number_of_mh_late_inlines();
roland@4409 461 return true;
roland@4409 462 }
roland@4409 463
roland@4409 464 call_node()->set_generator(this);
roland@4409 465 return false;
roland@4409 466 }
roland@4409 467
roland@4409 468 CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) {
roland@4409 469 Compile::current()->inc_number_of_mh_late_inlines();
roland@4409 470 CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const);
roland@4409 471 return cg;
roland@4409 472 }
roland@4409 473
roland@4409 474 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
roland@4409 475
roland@4409 476 public:
roland@4409 477 LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
roland@4409 478 LateInlineCallGenerator(method, inline_cg) {}
roland@4409 479
roland@7041 480 virtual JVMState* generate(JVMState* jvms) {
roland@4409 481 Compile *C = Compile::current();
roland@4409 482 C->print_inlining_skip(this);
roland@4409 483
roland@4409 484 C->add_string_late_inline(this);
roland@4409 485
roland@7041 486 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
roland@4409 487 return new_jvms;
roland@4409 488 }
roland@5991 489
roland@5991 490 virtual bool is_string_late_inline() const { return true; }
roland@4409 491 };
roland@4409 492
roland@4409 493 CallGenerator* CallGenerator::for_string_late_inline(ciMethod* method, CallGenerator* inline_cg) {
roland@4409 494 return new LateInlineStringCallGenerator(method, inline_cg);
roland@4409 495 }
roland@4409 496
kvn@5110 497 class LateInlineBoxingCallGenerator : public LateInlineCallGenerator {
kvn@5110 498
kvn@5110 499 public:
kvn@5110 500 LateInlineBoxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
kvn@5110 501 LateInlineCallGenerator(method, inline_cg) {}
kvn@5110 502
roland@7041 503 virtual JVMState* generate(JVMState* jvms) {
kvn@5110 504 Compile *C = Compile::current();
kvn@5110 505 C->print_inlining_skip(this);
kvn@5110 506
kvn@5110 507 C->add_boxing_late_inline(this);
kvn@5110 508
roland@7041 509 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
kvn@5110 510 return new_jvms;
kvn@5110 511 }
kvn@5110 512 };
kvn@5110 513
kvn@5110 514 CallGenerator* CallGenerator::for_boxing_late_inline(ciMethod* method, CallGenerator* inline_cg) {
kvn@5110 515 return new LateInlineBoxingCallGenerator(method, inline_cg);
kvn@5110 516 }
duke@435 517
duke@435 518 //---------------------------WarmCallGenerator--------------------------------
duke@435 519 // Internal class which handles initial deferral of inlining decisions.
duke@435 520 class WarmCallGenerator : public CallGenerator {
duke@435 521 WarmCallInfo* _call_info;
duke@435 522 CallGenerator* _if_cold;
duke@435 523 CallGenerator* _if_hot;
duke@435 524 bool _is_virtual; // caches virtuality of if_cold
duke@435 525 bool _is_inline; // caches inline-ness of if_hot
duke@435 526
duke@435 527 public:
duke@435 528 WarmCallGenerator(WarmCallInfo* ci,
duke@435 529 CallGenerator* if_cold,
duke@435 530 CallGenerator* if_hot)
duke@435 531 : CallGenerator(if_cold->method())
duke@435 532 {
duke@435 533 assert(method() == if_hot->method(), "consistent choices");
duke@435 534 _call_info = ci;
duke@435 535 _if_cold = if_cold;
duke@435 536 _if_hot = if_hot;
duke@435 537 _is_virtual = if_cold->is_virtual();
duke@435 538 _is_inline = if_hot->is_inline();
duke@435 539 }
duke@435 540
duke@435 541 virtual bool is_inline() const { return _is_inline; }
duke@435 542 virtual bool is_virtual() const { return _is_virtual; }
duke@435 543 virtual bool is_deferred() const { return true; }
duke@435 544
roland@7041 545 virtual JVMState* generate(JVMState* jvms);
duke@435 546 };
duke@435 547
duke@435 548
duke@435 549 CallGenerator* CallGenerator::for_warm_call(WarmCallInfo* ci,
duke@435 550 CallGenerator* if_cold,
duke@435 551 CallGenerator* if_hot) {
duke@435 552 return new WarmCallGenerator(ci, if_cold, if_hot);
duke@435 553 }
duke@435 554
roland@7041 555 JVMState* WarmCallGenerator::generate(JVMState* jvms) {
duke@435 556 Compile* C = Compile::current();
duke@435 557 if (C->log() != NULL) {
duke@435 558 C->log()->elem("warm_call bci='%d'", jvms->bci());
duke@435 559 }
roland@7041 560 jvms = _if_cold->generate(jvms);
duke@435 561 if (jvms != NULL) {
duke@435 562 Node* m = jvms->map()->control();
duke@435 563 if (m->is_CatchProj()) m = m->in(0); else m = C->top();
duke@435 564 if (m->is_Catch()) m = m->in(0); else m = C->top();
duke@435 565 if (m->is_Proj()) m = m->in(0); else m = C->top();
duke@435 566 if (m->is_CallJava()) {
duke@435 567 _call_info->set_call(m->as_Call());
duke@435 568 _call_info->set_hot_cg(_if_hot);
duke@435 569 #ifndef PRODUCT
duke@435 570 if (PrintOpto || PrintOptoInlining) {
duke@435 571 tty->print_cr("Queueing for warm inlining at bci %d:", jvms->bci());
duke@435 572 tty->print("WCI: ");
duke@435 573 _call_info->print();
duke@435 574 }
duke@435 575 #endif
duke@435 576 _call_info->set_heat(_call_info->compute_heat());
duke@435 577 C->set_warm_calls(_call_info->insert_into(C->warm_calls()));
duke@435 578 }
duke@435 579 }
duke@435 580 return jvms;
duke@435 581 }
duke@435 582
duke@435 583 void WarmCallInfo::make_hot() {
never@1515 584 Unimplemented();
duke@435 585 }
duke@435 586
duke@435 587 void WarmCallInfo::make_cold() {
duke@435 588 // No action: Just dequeue.
duke@435 589 }
duke@435 590
duke@435 591
duke@435 592 //------------------------PredictedCallGenerator------------------------------
duke@435 593 // Internal class which handles all out-of-line calls checking receiver type.
duke@435 594 class PredictedCallGenerator : public CallGenerator {
duke@435 595 ciKlass* _predicted_receiver;
duke@435 596 CallGenerator* _if_missed;
duke@435 597 CallGenerator* _if_hit;
duke@435 598 float _hit_prob;
duke@435 599
duke@435 600 public:
duke@435 601 PredictedCallGenerator(ciKlass* predicted_receiver,
duke@435 602 CallGenerator* if_missed,
duke@435 603 CallGenerator* if_hit, float hit_prob)
duke@435 604 : CallGenerator(if_missed->method())
duke@435 605 {
duke@435 606 // The call profile data may predict the hit_prob as extreme as 0 or 1.
duke@435 607 // Remove the extremes values from the range.
duke@435 608 if (hit_prob > PROB_MAX) hit_prob = PROB_MAX;
duke@435 609 if (hit_prob < PROB_MIN) hit_prob = PROB_MIN;
duke@435 610
duke@435 611 _predicted_receiver = predicted_receiver;
duke@435 612 _if_missed = if_missed;
duke@435 613 _if_hit = if_hit;
duke@435 614 _hit_prob = hit_prob;
duke@435 615 }
duke@435 616
duke@435 617 virtual bool is_virtual() const { return true; }
duke@435 618 virtual bool is_inline() const { return _if_hit->is_inline(); }
duke@435 619 virtual bool is_deferred() const { return _if_hit->is_deferred(); }
duke@435 620
roland@7041 621 virtual JVMState* generate(JVMState* jvms);
duke@435 622 };
duke@435 623
duke@435 624
duke@435 625 CallGenerator* CallGenerator::for_predicted_call(ciKlass* predicted_receiver,
duke@435 626 CallGenerator* if_missed,
duke@435 627 CallGenerator* if_hit,
duke@435 628 float hit_prob) {
duke@435 629 return new PredictedCallGenerator(predicted_receiver, if_missed, if_hit, hit_prob);
duke@435 630 }
duke@435 631
duke@435 632
roland@7041 633 JVMState* PredictedCallGenerator::generate(JVMState* jvms) {
duke@435 634 GraphKit kit(jvms);
duke@435 635 PhaseGVN& gvn = kit.gvn();
duke@435 636 // We need an explicit receiver null_check before checking its type.
duke@435 637 // We share a map with the caller, so his JVMS gets adjusted.
duke@435 638 Node* receiver = kit.argument(0);
duke@435 639
duke@435 640 CompileLog* log = kit.C->log();
duke@435 641 if (log != NULL) {
duke@435 642 log->elem("predicted_call bci='%d' klass='%d'",
duke@435 643 jvms->bci(), log->identify(_predicted_receiver));
duke@435 644 }
duke@435 645
twisti@4313 646 receiver = kit.null_check_receiver_before_call(method());
duke@435 647 if (kit.stopped()) {
duke@435 648 return kit.transfer_exceptions_into_jvms();
duke@435 649 }
duke@435 650
roland@7041 651 // Make a copy of the replaced nodes in case we need to restore them
roland@7041 652 ReplacedNodes replaced_nodes = kit.map()->replaced_nodes();
roland@7041 653 replaced_nodes.clone();
roland@7041 654
duke@435 655 Node* exact_receiver = receiver; // will get updated in place...
duke@435 656 Node* slow_ctl = kit.type_check_receiver(receiver,
duke@435 657 _predicted_receiver, _hit_prob,
duke@435 658 &exact_receiver);
duke@435 659
duke@435 660 SafePointNode* slow_map = NULL;
duke@435 661 JVMState* slow_jvms;
duke@435 662 { PreserveJVMState pjvms(&kit);
duke@435 663 kit.set_control(slow_ctl);
duke@435 664 if (!kit.stopped()) {
roland@7041 665 slow_jvms = _if_missed->generate(kit.sync_jvms());
twisti@3313 666 if (kit.failing())
twisti@3313 667 return NULL; // might happen because of NodeCountInliningCutoff
twisti@3313 668 assert(slow_jvms != NULL, "must be");
duke@435 669 kit.add_exception_states_from(slow_jvms);
duke@435 670 kit.set_map(slow_jvms->map());
duke@435 671 if (!kit.stopped())
duke@435 672 slow_map = kit.stop();
duke@435 673 }
duke@435 674 }
duke@435 675
kvn@728 676 if (kit.stopped()) {
kvn@728 677 // Instance exactly does not matches the desired type.
kvn@728 678 kit.set_jvms(slow_jvms);
kvn@728 679 return kit.transfer_exceptions_into_jvms();
kvn@728 680 }
kvn@728 681
duke@435 682 // fall through if the instance exactly matches the desired type
duke@435 683 kit.replace_in_map(receiver, exact_receiver);
duke@435 684
duke@435 685 // Make the hot call:
roland@7041 686 JVMState* new_jvms = _if_hit->generate(kit.sync_jvms());
duke@435 687 if (new_jvms == NULL) {
duke@435 688 // Inline failed, so make a direct call.
duke@435 689 assert(_if_hit->is_inline(), "must have been a failed inline");
duke@435 690 CallGenerator* cg = CallGenerator::for_direct_call(_if_hit->method());
roland@7041 691 new_jvms = cg->generate(kit.sync_jvms());
duke@435 692 }
duke@435 693 kit.add_exception_states_from(new_jvms);
duke@435 694 kit.set_jvms(new_jvms);
duke@435 695
duke@435 696 // Need to merge slow and fast?
duke@435 697 if (slow_map == NULL) {
duke@435 698 // The fast path is the only path remaining.
duke@435 699 return kit.transfer_exceptions_into_jvms();
duke@435 700 }
duke@435 701
duke@435 702 if (kit.stopped()) {
duke@435 703 // Inlined method threw an exception, so it's just the slow path after all.
duke@435 704 kit.set_jvms(slow_jvms);
duke@435 705 return kit.transfer_exceptions_into_jvms();
duke@435 706 }
duke@435 707
roland@7041 708 // There are 2 branches and the replaced nodes are only valid on
roland@7041 709 // one: restore the replaced nodes to what they were before the
roland@7041 710 // branch.
roland@7041 711 kit.map()->set_replaced_nodes(replaced_nodes);
roland@7041 712
duke@435 713 // Finish the diamond.
duke@435 714 kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
kvn@4115 715 RegionNode* region = new (kit.C) RegionNode(3);
duke@435 716 region->init_req(1, kit.control());
duke@435 717 region->init_req(2, slow_map->control());
duke@435 718 kit.set_control(gvn.transform(region));
duke@435 719 Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
duke@435 720 iophi->set_req(2, slow_map->i_o());
duke@435 721 kit.set_i_o(gvn.transform(iophi));
kvn@7026 722 // Merge memory
duke@435 723 kit.merge_memory(slow_map->merged_memory(), region, 2);
kvn@7026 724 // Transform new memory Phis.
kvn@7026 725 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
kvn@7026 726 Node* phi = mms.memory();
kvn@7026 727 if (phi->is_Phi() && phi->in(0) == region) {
kvn@7026 728 mms.set_memory(gvn.transform(phi));
kvn@7026 729 }
kvn@7026 730 }
duke@435 731 uint tos = kit.jvms()->stkoff() + kit.sp();
duke@435 732 uint limit = slow_map->req();
duke@435 733 for (uint i = TypeFunc::Parms; i < limit; i++) {
duke@435 734 // Skip unused stack slots; fast forward to monoff();
duke@435 735 if (i == tos) {
duke@435 736 i = kit.jvms()->monoff();
duke@435 737 if( i >= limit ) break;
duke@435 738 }
duke@435 739 Node* m = kit.map()->in(i);
duke@435 740 Node* n = slow_map->in(i);
duke@435 741 if (m != n) {
roland@6313 742 const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
duke@435 743 Node* phi = PhiNode::make(region, m, t);
duke@435 744 phi->set_req(2, n);
duke@435 745 kit.map()->set_req(i, gvn.transform(phi));
duke@435 746 }
duke@435 747 }
duke@435 748 return kit.transfer_exceptions_into_jvms();
duke@435 749 }
duke@435 750
duke@435 751
roland@4409 752 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool delayed_forbidden) {
twisti@3969 753 assert(callee->is_method_handle_intrinsic() ||
twisti@3969 754 callee->is_compiled_lambda_form(), "for_method_handle_call mismatch");
roland@4409 755 bool input_not_const;
roland@4409 756 CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, input_not_const);
roland@4409 757 Compile* C = Compile::current();
roland@4409 758 if (cg != NULL) {
roland@4409 759 if (!delayed_forbidden && AlwaysIncrementalInline) {
roland@4409 760 return CallGenerator::for_late_inline(callee, cg);
roland@4409 761 } else {
roland@4409 762 return cg;
roland@4409 763 }
roland@4409 764 }
roland@4409 765 int bci = jvms->bci();
roland@4409 766 ciCallProfile profile = caller->call_profile_at_bci(bci);
roland@4409 767 int call_site_count = caller->scale_count(profile.count());
roland@4409 768
roland@4409 769 if (IncrementalInline && call_site_count > 0 &&
roland@4409 770 (input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())) {
roland@4409 771 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
roland@4409 772 } else {
twisti@4414 773 // Out-of-line call.
roland@4409 774 return CallGenerator::for_direct_call(callee);
roland@4409 775 }
twisti@3313 776 }
twisti@3313 777
roland@4409 778 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool& input_not_const) {
twisti@3969 779 GraphKit kit(jvms);
twisti@3969 780 PhaseGVN& gvn = kit.gvn();
twisti@3969 781 Compile* C = kit.C;
twisti@3969 782 vmIntrinsics::ID iid = callee->intrinsic_id();
roland@4409 783 input_not_const = true;
twisti@3969 784 switch (iid) {
twisti@3969 785 case vmIntrinsics::_invokeBasic:
twisti@3969 786 {
twisti@4313 787 // Get MethodHandle receiver:
twisti@3969 788 Node* receiver = kit.argument(0);
twisti@3969 789 if (receiver->Opcode() == Op_ConP) {
roland@4409 790 input_not_const = false;
twisti@3969 791 const TypeOopPtr* oop_ptr = receiver->bottom_type()->is_oopptr();
twisti@3969 792 ciMethod* target = oop_ptr->const_oop()->as_method_handle()->get_vmtarget();
twisti@3969 793 guarantee(!target->is_method_handle_intrinsic(), "should not happen"); // XXX remove
coleenp@4037 794 const int vtable_index = Method::invalid_vtable_index;
roland@5991 795 CallGenerator* cg = C->call_generator(target, vtable_index, false, jvms, true, PROB_ALWAYS, NULL, true, true);
vlivanov@6106 796 assert(cg == NULL || !cg->is_late_inline() || cg->is_mh_late_inline(), "no late inline here");
twisti@3969 797 if (cg != NULL && cg->is_inline())
twisti@3969 798 return cg;
never@3105 799 }
never@3105 800 }
twisti@3969 801 break;
never@3105 802
twisti@3969 803 case vmIntrinsics::_linkToVirtual:
twisti@3969 804 case vmIntrinsics::_linkToStatic:
twisti@3969 805 case vmIntrinsics::_linkToSpecial:
twisti@3969 806 case vmIntrinsics::_linkToInterface:
twisti@3969 807 {
twisti@4313 808 // Get MemberName argument:
twisti@3969 809 Node* member_name = kit.argument(callee->arg_size() - 1);
twisti@3969 810 if (member_name->Opcode() == Op_ConP) {
roland@4409 811 input_not_const = false;
twisti@3969 812 const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
twisti@3969 813 ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
twisti@3969 814
twisti@3969 815 // In lamda forms we erase signature types to avoid resolving issues
twisti@3969 816 // involving class loaders. When we optimize a method handle invoke
twisti@3969 817 // to a direct call we must cast the receiver and arguments to its
twisti@3969 818 // actual types.
twisti@3969 819 ciSignature* signature = target->signature();
twisti@3969 820 const int receiver_skip = target->is_static() ? 0 : 1;
twisti@3969 821 // Cast receiver to its type.
twisti@3969 822 if (!target->is_static()) {
twisti@3969 823 Node* arg = kit.argument(0);
twisti@3969 824 const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr();
twisti@3969 825 const Type* sig_type = TypeOopPtr::make_from_klass(signature->accessing_klass());
twisti@3969 826 if (arg_type != NULL && !arg_type->higher_equal(sig_type)) {
kvn@4115 827 Node* cast_obj = gvn.transform(new (C) CheckCastPPNode(kit.control(), arg, sig_type));
twisti@3969 828 kit.set_argument(0, cast_obj);
twisti@3969 829 }
twisti@3969 830 }
twisti@3969 831 // Cast reference arguments to its type.
twisti@3969 832 for (int i = 0; i < signature->count(); i++) {
twisti@3969 833 ciType* t = signature->type_at(i);
twisti@3969 834 if (t->is_klass()) {
twisti@3969 835 Node* arg = kit.argument(receiver_skip + i);
twisti@3969 836 const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr();
twisti@3969 837 const Type* sig_type = TypeOopPtr::make_from_klass(t->as_klass());
twisti@3969 838 if (arg_type != NULL && !arg_type->higher_equal(sig_type)) {
kvn@4115 839 Node* cast_obj = gvn.transform(new (C) CheckCastPPNode(kit.control(), arg, sig_type));
twisti@3969 840 kit.set_argument(receiver_skip + i, cast_obj);
twisti@3969 841 }
twisti@3969 842 }
twisti@3969 843 }
twisti@4414 844
twisti@4414 845 // Try to get the most accurate receiver type
twisti@4414 846 const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual);
twisti@4414 847 const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
twisti@4414 848 int vtable_index = Method::invalid_vtable_index;
twisti@4414 849 bool call_does_dispatch = false;
twisti@4414 850
roland@5991 851 ciKlass* speculative_receiver_type = NULL;
twisti@4414 852 if (is_virtual_or_interface) {
twisti@4414 853 ciInstanceKlass* klass = target->holder();
twisti@4414 854 Node* receiver_node = kit.argument(0);
twisti@4414 855 const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
twisti@4414 856 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
roland@6746 857 // optimize_virtual_call() takes 2 different holder
roland@6746 858 // arguments for a corner case that doesn't apply here (see
roland@6746 859 // Parse::do_call())
roland@6746 860 target = C->optimize_virtual_call(caller, jvms->bci(), klass, klass,
roland@6746 861 target, receiver_type, is_virtual,
twisti@4414 862 call_does_dispatch, vtable_index); // out-parameters
roland@5991 863 // We lack profiling at this call but type speculation may
roland@5991 864 // provide us with a type
roland@5991 865 speculative_receiver_type = receiver_type->speculative_type();
twisti@4414 866 }
twisti@4414 867
roland@5991 868 CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms, true, PROB_ALWAYS, speculative_receiver_type, true, true);
vlivanov@6106 869 assert(cg == NULL || !cg->is_late_inline() || cg->is_mh_late_inline(), "no late inline here");
twisti@3969 870 if (cg != NULL && cg->is_inline())
twisti@3969 871 return cg;
twisti@3969 872 }
never@2949 873 }
twisti@3969 874 break;
twisti@3969 875
twisti@3969 876 default:
kvn@3971 877 fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
twisti@3969 878 break;
never@2949 879 }
never@2949 880 return NULL;
never@2949 881 }
never@2949 882
twisti@1573 883
kvn@7026 884 //------------------------PredicatedIntrinsicGenerator------------------------------
kvn@7026 885 // Internal class which handles all predicated Intrinsic calls.
kvn@7026 886 class PredicatedIntrinsicGenerator : public CallGenerator {
kvn@4205 887 CallGenerator* _intrinsic;
kvn@4205 888 CallGenerator* _cg;
kvn@4205 889
kvn@4205 890 public:
kvn@7026 891 PredicatedIntrinsicGenerator(CallGenerator* intrinsic,
kvn@7026 892 CallGenerator* cg)
kvn@4205 893 : CallGenerator(cg->method())
kvn@4205 894 {
kvn@4205 895 _intrinsic = intrinsic;
kvn@4205 896 _cg = cg;
kvn@4205 897 }
kvn@4205 898
kvn@4205 899 virtual bool is_virtual() const { return true; }
kvn@4205 900 virtual bool is_inlined() const { return true; }
kvn@4205 901 virtual bool is_intrinsic() const { return true; }
kvn@4205 902
roland@7041 903 virtual JVMState* generate(JVMState* jvms);
kvn@4205 904 };
kvn@4205 905
kvn@4205 906
kvn@7026 907 CallGenerator* CallGenerator::for_predicated_intrinsic(CallGenerator* intrinsic,
kvn@7026 908 CallGenerator* cg) {
kvn@7026 909 return new PredicatedIntrinsicGenerator(intrinsic, cg);
kvn@4205 910 }
kvn@4205 911
kvn@4205 912
roland@7041 913 JVMState* PredicatedIntrinsicGenerator::generate(JVMState* jvms) {
kvn@7026 914 // The code we want to generate here is:
kvn@7026 915 // if (receiver == NULL)
kvn@7026 916 // uncommon_Trap
kvn@7026 917 // if (predicate(0))
kvn@7026 918 // do_intrinsic(0)
kvn@7026 919 // else
kvn@7026 920 // if (predicate(1))
kvn@7026 921 // do_intrinsic(1)
kvn@7026 922 // ...
kvn@7026 923 // else
kvn@7026 924 // do_java_comp
kvn@7026 925
kvn@4205 926 GraphKit kit(jvms);
kvn@4205 927 PhaseGVN& gvn = kit.gvn();
kvn@4205 928
kvn@4205 929 CompileLog* log = kit.C->log();
kvn@4205 930 if (log != NULL) {
kvn@7026 931 log->elem("predicated_intrinsic bci='%d' method='%d'",
kvn@4205 932 jvms->bci(), log->identify(method()));
kvn@4205 933 }
kvn@4205 934
kvn@7026 935 if (!method()->is_static()) {
kvn@7026 936 // We need an explicit receiver null_check before checking its type in predicate.
kvn@7026 937 // We share a map with the caller, so his JVMS gets adjusted.
kvn@7026 938 Node* receiver = kit.null_check_receiver_before_call(method());
kvn@7026 939 if (kit.stopped()) {
kvn@7026 940 return kit.transfer_exceptions_into_jvms();
kvn@4205 941 }
kvn@4205 942 }
kvn@4205 943
kvn@7026 944 int n_predicates = _intrinsic->predicates_count();
kvn@7026 945 assert(n_predicates > 0, "sanity");
kvn@7026 946
kvn@7026 947 JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
kvn@7026 948
kvn@7026 949 // Region for normal compilation code if intrinsic failed.
kvn@7026 950 Node* slow_region = new (kit.C) RegionNode(1);
kvn@7026 951
kvn@7026 952 int results = 0;
kvn@7026 953 for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
kvn@7026 954 #ifdef ASSERT
kvn@7026 955 JVMState* old_jvms = kit.jvms();
kvn@7026 956 SafePointNode* old_map = kit.map();
kvn@7026 957 Node* old_io = old_map->i_o();
kvn@7026 958 Node* old_mem = old_map->memory();
kvn@7026 959 Node* old_exc = old_map->next_exception();
kvn@7026 960 #endif
kvn@7026 961 Node* else_ctrl = _intrinsic->generate_predicate(kit.sync_jvms(), predicate);
kvn@7026 962 #ifdef ASSERT
kvn@7026 963 // Assert(no_new_memory && no_new_io && no_new_exceptions) after generate_predicate.
kvn@7026 964 assert(old_jvms == kit.jvms(), "generate_predicate should not change jvm state");
kvn@7026 965 SafePointNode* new_map = kit.map();
kvn@7026 966 assert(old_io == new_map->i_o(), "generate_predicate should not change i_o");
kvn@7026 967 assert(old_mem == new_map->memory(), "generate_predicate should not change memory");
kvn@7026 968 assert(old_exc == new_map->next_exception(), "generate_predicate should not add exceptions");
kvn@7026 969 #endif
kvn@7026 970 if (!kit.stopped()) {
kvn@7026 971 PreserveJVMState pjvms(&kit);
kvn@7026 972 // Generate intrinsic code:
roland@7041 973 JVMState* new_jvms = _intrinsic->generate(kit.sync_jvms());
kvn@7026 974 if (new_jvms == NULL) {
kvn@7026 975 // Intrinsic failed, use normal compilation path for this predicate.
kvn@7026 976 slow_region->add_req(kit.control());
kvn@7026 977 } else {
kvn@7026 978 kit.add_exception_states_from(new_jvms);
kvn@7026 979 kit.set_jvms(new_jvms);
kvn@7026 980 if (!kit.stopped()) {
kvn@7026 981 result_jvms[results++] = kit.jvms();
kvn@7026 982 }
kvn@7026 983 }
kvn@7026 984 }
kvn@7026 985 if (else_ctrl == NULL) {
kvn@7026 986 else_ctrl = kit.C->top();
kvn@7026 987 }
kvn@7026 988 kit.set_control(else_ctrl);
kvn@7026 989 }
kvn@7026 990 if (!kit.stopped()) {
kvn@7026 991 // Final 'else' after predicates.
kvn@7026 992 slow_region->add_req(kit.control());
kvn@7026 993 }
kvn@7026 994 if (slow_region->req() > 1) {
kvn@7026 995 PreserveJVMState pjvms(&kit);
kvn@7026 996 // Generate normal compilation code:
kvn@7026 997 kit.set_control(gvn.transform(slow_region));
roland@7041 998 JVMState* new_jvms = _cg->generate(kit.sync_jvms());
kvn@7026 999 if (kit.failing())
kvn@7026 1000 return NULL; // might happen because of NodeCountInliningCutoff
kvn@7026 1001 assert(new_jvms != NULL, "must be");
kvn@7026 1002 kit.add_exception_states_from(new_jvms);
kvn@7026 1003 kit.set_jvms(new_jvms);
kvn@7026 1004 if (!kit.stopped()) {
kvn@7026 1005 result_jvms[results++] = kit.jvms();
kvn@7026 1006 }
kvn@7026 1007 }
kvn@7026 1008
kvn@7026 1009 if (results == 0) {
kvn@7026 1010 // All paths ended in uncommon traps.
kvn@7026 1011 (void) kit.stop();
kvn@4205 1012 return kit.transfer_exceptions_into_jvms();
kvn@4205 1013 }
kvn@4205 1014
kvn@7026 1015 if (results == 1) { // Only one path
kvn@7026 1016 kit.set_jvms(result_jvms[0]);
kvn@4205 1017 return kit.transfer_exceptions_into_jvms();
kvn@4205 1018 }
kvn@4205 1019
kvn@7026 1020 // Merge all paths.
kvn@7026 1021 kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
kvn@7026 1022 RegionNode* region = new (kit.C) RegionNode(results + 1);
kvn@7026 1023 Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
kvn@7026 1024 for (int i = 0; i < results; i++) {
kvn@7026 1025 JVMState* jvms = result_jvms[i];
kvn@7026 1026 int path = i + 1;
kvn@7026 1027 SafePointNode* map = jvms->map();
kvn@7026 1028 region->init_req(path, map->control());
kvn@7026 1029 iophi->set_req(path, map->i_o());
kvn@7026 1030 if (i == 0) {
kvn@7026 1031 kit.set_jvms(jvms);
kvn@7026 1032 } else {
kvn@7026 1033 kit.merge_memory(map->merged_memory(), region, path);
kvn@7026 1034 }
kvn@7026 1035 }
kvn@7026 1036 kit.set_control(gvn.transform(region));
kvn@7026 1037 kit.set_i_o(gvn.transform(iophi));
kvn@7026 1038 // Transform new memory Phis.
kvn@7026 1039 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
kvn@7026 1040 Node* phi = mms.memory();
kvn@7026 1041 if (phi->is_Phi() && phi->in(0) == region) {
kvn@7026 1042 mms.set_memory(gvn.transform(phi));
kvn@7026 1043 }
kvn@4205 1044 }
kvn@4205 1045
kvn@7026 1046 // Merge debug info.
kvn@7026 1047 Node** ins = NEW_RESOURCE_ARRAY(Node*, results);
kvn@4205 1048 uint tos = kit.jvms()->stkoff() + kit.sp();
kvn@7026 1049 Node* map = kit.map();
kvn@7026 1050 uint limit = map->req();
kvn@4205 1051 for (uint i = TypeFunc::Parms; i < limit; i++) {
kvn@4205 1052 // Skip unused stack slots; fast forward to monoff();
kvn@4205 1053 if (i == tos) {
kvn@4205 1054 i = kit.jvms()->monoff();
kvn@4205 1055 if( i >= limit ) break;
kvn@4205 1056 }
kvn@7026 1057 Node* n = map->in(i);
kvn@7026 1058 ins[0] = n;
kvn@7026 1059 const Type* t = gvn.type(n);
kvn@7026 1060 bool needs_phi = false;
kvn@7026 1061 for (int j = 1; j < results; j++) {
kvn@7026 1062 JVMState* jvms = result_jvms[j];
kvn@7026 1063 Node* jmap = jvms->map();
kvn@7026 1064 Node* m = NULL;
kvn@7026 1065 if (jmap->req() > i) {
kvn@7026 1066 m = jmap->in(i);
kvn@7026 1067 if (m != n) {
kvn@7026 1068 needs_phi = true;
kvn@7026 1069 t = t->meet_speculative(gvn.type(m));
kvn@7026 1070 }
kvn@7026 1071 }
kvn@7026 1072 ins[j] = m;
kvn@7026 1073 }
kvn@7026 1074 if (needs_phi) {
kvn@7026 1075 Node* phi = PhiNode::make(region, n, t);
kvn@7026 1076 for (int j = 1; j < results; j++) {
kvn@7026 1077 phi->set_req(j + 1, ins[j]);
kvn@7026 1078 }
kvn@7026 1079 map->set_req(i, gvn.transform(phi));
kvn@4205 1080 }
kvn@4205 1081 }
kvn@7026 1082
kvn@4205 1083 return kit.transfer_exceptions_into_jvms();
kvn@4205 1084 }
kvn@4205 1085
duke@435 1086 //-------------------------UncommonTrapCallGenerator-----------------------------
duke@435 1087 // Internal class which handles all out-of-line calls checking receiver type.
duke@435 1088 class UncommonTrapCallGenerator : public CallGenerator {
duke@435 1089 Deoptimization::DeoptReason _reason;
duke@435 1090 Deoptimization::DeoptAction _action;
duke@435 1091
duke@435 1092 public:
duke@435 1093 UncommonTrapCallGenerator(ciMethod* m,
duke@435 1094 Deoptimization::DeoptReason reason,
duke@435 1095 Deoptimization::DeoptAction action)
duke@435 1096 : CallGenerator(m)
duke@435 1097 {
duke@435 1098 _reason = reason;
duke@435 1099 _action = action;
duke@435 1100 }
duke@435 1101
duke@435 1102 virtual bool is_virtual() const { ShouldNotReachHere(); return false; }
duke@435 1103 virtual bool is_trap() const { return true; }
duke@435 1104
roland@7041 1105 virtual JVMState* generate(JVMState* jvms);
duke@435 1106 };
duke@435 1107
duke@435 1108
duke@435 1109 CallGenerator*
duke@435 1110 CallGenerator::for_uncommon_trap(ciMethod* m,
duke@435 1111 Deoptimization::DeoptReason reason,
duke@435 1112 Deoptimization::DeoptAction action) {
duke@435 1113 return new UncommonTrapCallGenerator(m, reason, action);
duke@435 1114 }
duke@435 1115
duke@435 1116
roland@7041 1117 JVMState* UncommonTrapCallGenerator::generate(JVMState* jvms) {
duke@435 1118 GraphKit kit(jvms);
duke@435 1119 // Take the trap with arguments pushed on the stack. (Cf. null_check_receiver).
duke@435 1120 int nargs = method()->arg_size();
duke@435 1121 kit.inc_sp(nargs);
duke@435 1122 assert(nargs <= kit.sp() && kit.sp() <= jvms->stk_size(), "sane sp w/ args pushed");
duke@435 1123 if (_reason == Deoptimization::Reason_class_check &&
duke@435 1124 _action == Deoptimization::Action_maybe_recompile) {
duke@435 1125 // Temp fix for 6529811
duke@435 1126 // Don't allow uncommon_trap to override our decision to recompile in the event
duke@435 1127 // of a class cast failure for a monomorphic call as it will never let us convert
duke@435 1128 // the call to either bi-morphic or megamorphic and can lead to unc-trap loops
duke@435 1129 bool keep_exact_action = true;
duke@435 1130 kit.uncommon_trap(_reason, _action, NULL, "monomorphic vcall checkcast", false, keep_exact_action);
duke@435 1131 } else {
duke@435 1132 kit.uncommon_trap(_reason, _action);
duke@435 1133 }
duke@435 1134 return kit.transfer_exceptions_into_jvms();
duke@435 1135 }
duke@435 1136
duke@435 1137 // (Note: Moved hook_up_call to GraphKit::set_edges_for_java_call.)
duke@435 1138
duke@435 1139 // (Node: Merged hook_up_exits into ParseGenerator::generate.)
duke@435 1140
duke@435 1141 #define NODES_OVERHEAD_PER_METHOD (30.0)
duke@435 1142 #define NODES_PER_BYTECODE (9.5)
duke@435 1143
duke@435 1144 void WarmCallInfo::init(JVMState* call_site, ciMethod* call_method, ciCallProfile& profile, float prof_factor) {
duke@435 1145 int call_count = profile.count();
duke@435 1146 int code_size = call_method->code_size();
duke@435 1147
duke@435 1148 // Expected execution count is based on the historical count:
duke@435 1149 _count = call_count < 0 ? 1 : call_site->method()->scale_count(call_count, prof_factor);
duke@435 1150
duke@435 1151 // Expected profit from inlining, in units of simple call-overheads.
duke@435 1152 _profit = 1.0;
duke@435 1153
duke@435 1154 // Expected work performed by the call in units of call-overheads.
duke@435 1155 // %%% need an empirical curve fit for "work" (time in call)
duke@435 1156 float bytecodes_per_call = 3;
duke@435 1157 _work = 1.0 + code_size / bytecodes_per_call;
duke@435 1158
duke@435 1159 // Expected size of compilation graph:
duke@435 1160 // -XX:+PrintParseStatistics once reported:
duke@435 1161 // Methods seen: 9184 Methods parsed: 9184 Nodes created: 1582391
duke@435 1162 // Histogram of 144298 parsed bytecodes:
duke@435 1163 // %%% Need an better predictor for graph size.
duke@435 1164 _size = NODES_OVERHEAD_PER_METHOD + (NODES_PER_BYTECODE * code_size);
duke@435 1165 }
duke@435 1166
duke@435 1167 // is_cold: Return true if the node should never be inlined.
duke@435 1168 // This is true if any of the key metrics are extreme.
duke@435 1169 bool WarmCallInfo::is_cold() const {
duke@435 1170 if (count() < WarmCallMinCount) return true;
duke@435 1171 if (profit() < WarmCallMinProfit) return true;
duke@435 1172 if (work() > WarmCallMaxWork) return true;
duke@435 1173 if (size() > WarmCallMaxSize) return true;
duke@435 1174 return false;
duke@435 1175 }
duke@435 1176
duke@435 1177 // is_hot: Return true if the node should be inlined immediately.
duke@435 1178 // This is true if any of the key metrics are extreme.
duke@435 1179 bool WarmCallInfo::is_hot() const {
duke@435 1180 assert(!is_cold(), "eliminate is_cold cases before testing is_hot");
duke@435 1181 if (count() >= HotCallCountThreshold) return true;
duke@435 1182 if (profit() >= HotCallProfitThreshold) return true;
duke@435 1183 if (work() <= HotCallTrivialWork) return true;
duke@435 1184 if (size() <= HotCallTrivialSize) return true;
duke@435 1185 return false;
duke@435 1186 }
duke@435 1187
duke@435 1188 // compute_heat:
duke@435 1189 float WarmCallInfo::compute_heat() const {
duke@435 1190 assert(!is_cold(), "compute heat only on warm nodes");
duke@435 1191 assert(!is_hot(), "compute heat only on warm nodes");
duke@435 1192 int min_size = MAX2(0, (int)HotCallTrivialSize);
duke@435 1193 int max_size = MIN2(500, (int)WarmCallMaxSize);
duke@435 1194 float method_size = (size() - min_size) / MAX2(1, max_size - min_size);
duke@435 1195 float size_factor;
duke@435 1196 if (method_size < 0.05) size_factor = 4; // 2 sigmas better than avg.
duke@435 1197 else if (method_size < 0.15) size_factor = 2; // 1 sigma better than avg.
duke@435 1198 else if (method_size < 0.5) size_factor = 1; // better than avg.
duke@435 1199 else size_factor = 0.5; // worse than avg.
duke@435 1200 return (count() * profit() * size_factor);
duke@435 1201 }
duke@435 1202
duke@435 1203 bool WarmCallInfo::warmer_than(WarmCallInfo* that) {
duke@435 1204 assert(this != that, "compare only different WCIs");
duke@435 1205 assert(this->heat() != 0 && that->heat() != 0, "call compute_heat 1st");
duke@435 1206 if (this->heat() > that->heat()) return true;
duke@435 1207 if (this->heat() < that->heat()) return false;
duke@435 1208 assert(this->heat() == that->heat(), "no NaN heat allowed");
duke@435 1209 // Equal heat. Break the tie some other way.
duke@435 1210 if (!this->call() || !that->call()) return (address)this > (address)that;
duke@435 1211 return this->call()->_idx > that->call()->_idx;
duke@435 1212 }
duke@435 1213
duke@435 1214 //#define UNINIT_NEXT ((WarmCallInfo*)badAddress)
duke@435 1215 #define UNINIT_NEXT ((WarmCallInfo*)NULL)
duke@435 1216
duke@435 1217 WarmCallInfo* WarmCallInfo::insert_into(WarmCallInfo* head) {
duke@435 1218 assert(next() == UNINIT_NEXT, "not yet on any list");
duke@435 1219 WarmCallInfo* prev_p = NULL;
duke@435 1220 WarmCallInfo* next_p = head;
duke@435 1221 while (next_p != NULL && next_p->warmer_than(this)) {
duke@435 1222 prev_p = next_p;
duke@435 1223 next_p = prev_p->next();
duke@435 1224 }
duke@435 1225 // Install this between prev_p and next_p.
duke@435 1226 this->set_next(next_p);
duke@435 1227 if (prev_p == NULL)
duke@435 1228 head = this;
duke@435 1229 else
duke@435 1230 prev_p->set_next(this);
duke@435 1231 return head;
duke@435 1232 }
duke@435 1233
duke@435 1234 WarmCallInfo* WarmCallInfo::remove_from(WarmCallInfo* head) {
duke@435 1235 WarmCallInfo* prev_p = NULL;
duke@435 1236 WarmCallInfo* next_p = head;
duke@435 1237 while (next_p != this) {
duke@435 1238 assert(next_p != NULL, "this must be in the list somewhere");
duke@435 1239 prev_p = next_p;
duke@435 1240 next_p = prev_p->next();
duke@435 1241 }
duke@435 1242 next_p = this->next();
duke@435 1243 debug_only(this->set_next(UNINIT_NEXT));
duke@435 1244 // Remove this from between prev_p and next_p.
duke@435 1245 if (prev_p == NULL)
duke@435 1246 head = next_p;
duke@435 1247 else
duke@435 1248 prev_p->set_next(next_p);
duke@435 1249 return head;
duke@435 1250 }
duke@435 1251
never@2725 1252 WarmCallInfo WarmCallInfo::_always_hot(WarmCallInfo::MAX_VALUE(), WarmCallInfo::MAX_VALUE(),
never@2725 1253 WarmCallInfo::MIN_VALUE(), WarmCallInfo::MIN_VALUE());
never@2725 1254 WarmCallInfo WarmCallInfo::_always_cold(WarmCallInfo::MIN_VALUE(), WarmCallInfo::MIN_VALUE(),
never@2725 1255 WarmCallInfo::MAX_VALUE(), WarmCallInfo::MAX_VALUE());
duke@435 1256
duke@435 1257 WarmCallInfo* WarmCallInfo::always_hot() {
never@2725 1258 assert(_always_hot.is_hot(), "must always be hot");
never@2725 1259 return &_always_hot;
duke@435 1260 }
duke@435 1261
duke@435 1262 WarmCallInfo* WarmCallInfo::always_cold() {
never@2725 1263 assert(_always_cold.is_cold(), "must always be cold");
never@2725 1264 return &_always_cold;
duke@435 1265 }
duke@435 1266
duke@435 1267
duke@435 1268 #ifndef PRODUCT
duke@435 1269
duke@435 1270 void WarmCallInfo::print() const {
duke@435 1271 tty->print("%s : C=%6.1f P=%6.1f W=%6.1f S=%6.1f H=%6.1f -> %p",
duke@435 1272 is_cold() ? "cold" : is_hot() ? "hot " : "warm",
duke@435 1273 count(), profit(), work(), size(), compute_heat(), next());
duke@435 1274 tty->cr();
duke@435 1275 if (call() != NULL) call()->dump();
duke@435 1276 }
duke@435 1277
duke@435 1278 void print_wci(WarmCallInfo* ci) {
duke@435 1279 ci->print();
duke@435 1280 }
duke@435 1281
duke@435 1282 void WarmCallInfo::print_all() const {
duke@435 1283 for (const WarmCallInfo* p = this; p != NULL; p = p->next())
duke@435 1284 p->print();
duke@435 1285 }
duke@435 1286
duke@435 1287 int WarmCallInfo::count_all() const {
duke@435 1288 int cnt = 0;
duke@435 1289 for (const WarmCallInfo* p = this; p != NULL; p = p->next())
duke@435 1290 cnt++;
duke@435 1291 return cnt;
duke@435 1292 }
duke@435 1293
duke@435 1294 #endif //PRODUCT

mercurial