src/share/vm/opto/callGenerator.cpp

Thu, 17 Nov 2016 16:06:56 +0000

author
dbuck
date
Thu, 17 Nov 2016 16:06:56 +0000
changeset 8999
53d23b6b25cd
parent 8651
a50ab9692b6f
child 8856
ac27a9c85bea
permissions
-rw-r--r--

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

mercurial