36 #include "gc/shared/barrierSet.hpp"
37 #include "gc/shared/c2/barrierSetC2.hpp"
38 #include "jfr/jfrEvents.hpp"
39 #include "jvm_io.h"
40 #include "memory/allocation.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "opto/addnode.hpp"
43 #include "opto/block.hpp"
44 #include "opto/c2compiler.hpp"
45 #include "opto/callGenerator.hpp"
46 #include "opto/callnode.hpp"
47 #include "opto/castnode.hpp"
48 #include "opto/cfgnode.hpp"
49 #include "opto/chaitin.hpp"
50 #include "opto/compile.hpp"
51 #include "opto/connode.hpp"
52 #include "opto/convertnode.hpp"
53 #include "opto/divnode.hpp"
54 #include "opto/escape.hpp"
55 #include "opto/idealGraphPrinter.hpp"
56 #include "opto/loopnode.hpp"
57 #include "opto/machnode.hpp"
58 #include "opto/macro.hpp"
59 #include "opto/matcher.hpp"
60 #include "opto/mathexactnode.hpp"
61 #include "opto/memnode.hpp"
62 #include "opto/mulnode.hpp"
63 #include "opto/narrowptrnode.hpp"
64 #include "opto/node.hpp"
65 #include "opto/opcodes.hpp"
66 #include "opto/output.hpp"
67 #include "opto/parse.hpp"
68 #include "opto/phaseX.hpp"
69 #include "opto/rootnode.hpp"
70 #include "opto/runtime.hpp"
71 #include "opto/stringopts.hpp"
72 #include "opto/type.hpp"
73 #include "opto/vector.hpp"
74 #include "opto/vectornode.hpp"
75 #include "runtime/globals_extension.hpp"
375 // Constant node that has no out-edges and has only one in-edge from
376 // root is usually dead. However, sometimes reshaping walk makes
377 // it reachable by adding use edges. So, we will NOT count Con nodes
378 // as dead to be conservative about the dead node count at any
379 // given time.
380 if (!dead->is_Con()) {
381 record_dead_node(dead->_idx);
382 }
383 if (dead->is_macro()) {
384 remove_macro_node(dead);
385 }
386 if (dead->is_expensive()) {
387 remove_expensive_node(dead);
388 }
389 if (dead->Opcode() == Op_Opaque4) {
390 remove_template_assertion_predicate_opaq(dead);
391 }
392 if (dead->for_post_loop_opts_igvn()) {
393 remove_from_post_loop_opts_igvn(dead);
394 }
395 if (dead->is_Call()) {
396 remove_useless_late_inlines( &_late_inlines, dead);
397 remove_useless_late_inlines( &_string_late_inlines, dead);
398 remove_useless_late_inlines( &_boxing_late_inlines, dead);
399 remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
400
401 if (dead->is_CallStaticJava()) {
402 remove_unstable_if_trap(dead->as_CallStaticJava(), false);
403 }
404 }
405 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
406 bs->unregister_potential_barrier_node(dead);
407 }
408
409 // Disconnect all useless nodes by disconnecting those at the boundary.
410 void Compile::disconnect_useless_nodes(Unique_Node_List &useful, Unique_Node_List* worklist) {
411 uint next = 0;
412 while (next < useful.size()) {
413 Node *n = useful.at(next++);
414 if (n->is_SafePoint()) {
415 // We're done with a parsing phase. Replaced nodes are not valid
416 // beyond that point.
417 n->as_SafePoint()->delete_replaced_nodes();
418 }
419 // Use raw traversal of out edges since this code removes out edges
420 int max = n->outcnt();
421 for (int j = 0; j < max; ++j) {
422 Node* child = n->raw_out(j);
423 if (!useful.member(child)) {
424 assert(!child->is_top() || child != top(),
425 "If top is cached in Compile object it is in useful list");
426 // Only need to remove this out-edge to the useless node
427 n->raw_del_out(j);
428 --j;
429 --max;
430 }
431 }
432 if (n->outcnt() == 1 && n->has_special_unique_user()) {
433 worklist->push(n->unique_out());
434 }
435 }
436
437 remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes
438 remove_useless_nodes(_parse_predicate_opaqs, useful); // remove useless Parse Predicate opaque nodes
439 remove_useless_nodes(_template_assertion_predicate_opaqs, useful); // remove useless Assertion Predicate opaque nodes
440 remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes
441 remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
442 remove_useless_unstable_if_traps(useful); // remove useless unstable_if traps
443 remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes
444 #ifdef ASSERT
445 if (_modified_nodes != nullptr) {
446 _modified_nodes->remove_useless_nodes(useful.member_set());
447 }
448 #endif
449
450 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
451 bs->eliminate_useless_gc_barriers(useful, this);
452 // clean up the late inline lists
453 remove_useless_late_inlines( &_late_inlines, useful);
454 remove_useless_late_inlines( &_string_late_inlines, useful);
455 remove_useless_late_inlines( &_boxing_late_inlines, useful);
456 remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
457 debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
458 }
459
460 // ============================================================================
461 //------------------------------CompileWrapper---------------------------------
583 // the continuation bci for on stack replacement.
584
585
586 Compile::Compile( ciEnv* ci_env, ciMethod* target, int osr_bci,
587 Options options, DirectiveSet* directive)
588 : Phase(Compiler),
589 _compile_id(ci_env->compile_id()),
590 _options(options),
591 _method(target),
592 _entry_bci(osr_bci),
593 _ilt(nullptr),
594 _stub_function(nullptr),
595 _stub_name(nullptr),
596 _stub_entry_point(nullptr),
597 _max_node_limit(MaxNodeLimit),
598 _post_loop_opts_phase(false),
599 _inlining_progress(false),
600 _inlining_incrementally(false),
601 _do_cleanup(false),
602 _has_reserved_stack_access(target->has_reserved_stack_access()),
603 #ifndef PRODUCT
604 _igv_idx(0),
605 _trace_opto_output(directive->TraceOptoOutputOption),
606 #endif
607 _has_method_handle_invokes(false),
608 _clinit_barrier_on_entry(false),
609 _stress_seed(0),
610 _comp_arena(mtCompiler),
611 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
612 _env(ci_env),
613 _directive(directive),
614 _log(ci_env->log()),
615 _failure_reason(nullptr),
616 _intrinsics (comp_arena(), 0, 0, nullptr),
617 _macro_nodes (comp_arena(), 8, 0, nullptr),
618 _parse_predicate_opaqs (comp_arena(), 8, 0, nullptr),
619 _template_assertion_predicate_opaqs (comp_arena(), 8, 0, nullptr),
620 _expensive_nodes (comp_arena(), 8, 0, nullptr),
621 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
622 _unstable_if_traps (comp_arena(), 8, 0, nullptr),
623 _coarsened_locks (comp_arena(), 8, 0, nullptr),
624 _congraph(nullptr),
625 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
626 _dead_node_list(comp_arena()),
627 _dead_node_count(0),
628 _node_arena(mtCompiler),
629 _old_arena(mtCompiler),
630 _mach_constant_base_node(nullptr),
631 _Compile_types(mtCompiler),
632 _initial_gvn(nullptr),
633 _for_igvn(nullptr),
634 _late_inlines(comp_arena(), 2, 0, nullptr),
635 _string_late_inlines(comp_arena(), 2, 0, nullptr),
636 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
637 _vector_reboxing_late_inlines(comp_arena(), 2, 0, nullptr),
638 _late_inlines_pos(0),
639 _number_of_mh_late_inlines(0),
640 _print_inlining_stream(new (mtCompiler) stringStream()),
641 _print_inlining_list(nullptr),
706 // Node list that Iterative GVN will start with
707 Unique_Node_List for_igvn(comp_arena());
708 set_for_igvn(&for_igvn);
709
710 // GVN that will be run immediately on new nodes
711 uint estimated_size = method()->code_size()*4+64;
712 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
713 PhaseGVN gvn(node_arena(), estimated_size);
714 set_initial_gvn(&gvn);
715
716 print_inlining_init();
717 { // Scope for timing the parser
718 TracePhase tp("parse", &timers[_t_parser]);
719
720 // Put top into the hash table ASAP.
721 initial_gvn()->transform_no_reclaim(top());
722
723 // Set up tf(), start(), and find a CallGenerator.
724 CallGenerator* cg = nullptr;
725 if (is_osr_compilation()) {
726 const TypeTuple *domain = StartOSRNode::osr_domain();
727 const TypeTuple *range = TypeTuple::make_range(method()->signature());
728 init_tf(TypeFunc::make(domain, range));
729 StartNode* s = new StartOSRNode(root(), domain);
730 initial_gvn()->set_type_bottom(s);
731 init_start(s);
732 cg = CallGenerator::for_osr(method(), entry_bci());
733 } else {
734 // Normal case.
735 init_tf(TypeFunc::make(method()));
736 StartNode* s = new StartNode(root(), tf()->domain());
737 initial_gvn()->set_type_bottom(s);
738 init_start(s);
739 if (method()->intrinsic_id() == vmIntrinsics::_Reference_get) {
740 // With java.lang.ref.reference.get() we must go through the
741 // intrinsic - even when get() is the root
742 // method of the compile - so that, if necessary, the value in
743 // the referent field of the reference object gets recorded by
744 // the pre-barrier code.
745 cg = find_intrinsic(method(), false);
746 }
747 if (cg == nullptr) {
748 float past_uses = method()->interpreter_invocation_count();
749 float expected_uses = past_uses;
750 cg = CallGenerator::for_inline(method(), expected_uses);
751 }
752 }
753 if (failing()) return;
754 if (cg == nullptr) {
755 const char* reason = InlineTree::check_can_parse(method());
756 assert(reason != nullptr, "expect reason for parse failure");
845 print_ideal_ir("print_ideal");
846 }
847 #endif
848
849 #ifdef ASSERT
850 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
851 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
852 #endif
853
854 // Dump compilation data to replay it.
855 if (directive->DumpReplayOption) {
856 env()->dump_replay_data(_compile_id);
857 }
858 if (directive->DumpInlineOption && (ilt() != nullptr)) {
859 env()->dump_inline_data(_compile_id);
860 }
861
862 // Now that we know the size of all the monitors we can add a fixed slot
863 // for the original deopt pc.
864 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
865 set_fixed_slots(next_slot);
866
867 // Compute when to use implicit null checks. Used by matching trap based
868 // nodes and NullCheck optimization.
869 set_allowed_deopt_reasons();
870
871 // Now generate code
872 Code_Gen();
873 }
874
875 //------------------------------Compile----------------------------------------
876 // Compile a runtime stub
877 Compile::Compile( ciEnv* ci_env,
878 TypeFunc_generator generator,
879 address stub_function,
880 const char *stub_name,
881 int is_fancy_jump,
882 bool pass_tls,
883 bool return_pc,
884 DirectiveSet* directive)
885 : Phase(Compiler),
886 _compile_id(0),
887 _options(Options::for_runtime_stub()),
888 _method(nullptr),
889 _entry_bci(InvocationEntryBci),
890 _stub_function(stub_function),
891 _stub_name(stub_name),
892 _stub_entry_point(nullptr),
893 _max_node_limit(MaxNodeLimit),
894 _post_loop_opts_phase(false),
895 _inlining_progress(false),
896 _inlining_incrementally(false),
897 _has_reserved_stack_access(false),
898 #ifndef PRODUCT
899 _igv_idx(0),
900 _trace_opto_output(directive->TraceOptoOutputOption),
901 #endif
902 _has_method_handle_invokes(false),
903 _clinit_barrier_on_entry(false),
904 _stress_seed(0),
905 _comp_arena(mtCompiler),
906 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
907 _env(ci_env),
908 _directive(directive),
909 _log(ci_env->log()),
910 _failure_reason(nullptr),
911 _congraph(nullptr),
912 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
913 _dead_node_list(comp_arena()),
914 _dead_node_count(0),
915 _node_arena(mtCompiler),
916 _old_arena(mtCompiler),
917 _mach_constant_base_node(nullptr),
1003 // Create Debug Information Recorder to record scopes, oopmaps, etc.
1004 env()->set_oop_recorder(new OopRecorder(env()->arena()));
1005 env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));
1006 env()->set_dependencies(new Dependencies(env()));
1007
1008 _fixed_slots = 0;
1009 set_has_split_ifs(false);
1010 set_has_loops(false); // first approximation
1011 set_has_stringbuilder(false);
1012 set_has_boxed_value(false);
1013 _trap_can_recompile = false; // no traps emitted yet
1014 _major_progress = true; // start out assuming good things will happen
1015 set_has_unsafe_access(false);
1016 set_max_vector_size(0);
1017 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1018 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1019 set_decompile_count(0);
1020
1021 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1022 _loop_opts_cnt = LoopOptsCount;
1023 set_do_inlining(Inline);
1024 set_max_inline_size(MaxInlineSize);
1025 set_freq_inline_size(FreqInlineSize);
1026 set_do_scheduling(OptoScheduling);
1027
1028 set_do_vector_loop(false);
1029 set_has_monitors(false);
1030
1031 if (AllowVectorizeOnDemand) {
1032 if (has_method() && (_directive->VectorizeOption || _directive->VectorizeDebugOption)) {
1033 set_do_vector_loop(true);
1034 NOT_PRODUCT(if (do_vector_loop() && Verbose) {tty->print("Compile::Init: do vectorized loops (SIMD like) for method %s\n", method()->name()->as_quoted_ascii());})
1035 } else if (has_method() && method()->name() != 0 &&
1036 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1037 set_do_vector_loop(true);
1038 }
1039 }
1040 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1041 NOT_PRODUCT(if (use_cmove() && Verbose && has_method()) {tty->print("Compile::Init: use CMove without profitability tests for method %s\n", method()->name()->as_quoted_ascii());})
1042
1291 // If this method has already thrown a range-check,
1292 // assume it was because we already tried range smearing
1293 // and it failed.
1294 uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1295 return !already_trapped;
1296 }
1297
1298
1299 //------------------------------flatten_alias_type-----------------------------
1300 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1301 assert(do_aliasing(), "Aliasing should be enabled");
1302 int offset = tj->offset();
1303 TypePtr::PTR ptr = tj->ptr();
1304
1305 // Known instance (scalarizable allocation) alias only with itself.
1306 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1307 tj->is_oopptr()->is_known_instance();
1308
1309 // Process weird unsafe references.
1310 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1311 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1312 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1313 tj = TypeOopPtr::BOTTOM;
1314 ptr = tj->ptr();
1315 offset = tj->offset();
1316 }
1317
1318 // Array pointers need some flattening
1319 const TypeAryPtr* ta = tj->isa_aryptr();
1320 if (ta && ta->is_stable()) {
1321 // Erase stability property for alias analysis.
1322 tj = ta = ta->cast_to_stable(false);
1323 }
1324 if( ta && is_known_inst ) {
1325 if ( offset != Type::OffsetBot &&
1326 offset > arrayOopDesc::length_offset_in_bytes() ) {
1327 offset = Type::OffsetBot; // Flatten constant access into array body only
1328 tj = ta = ta->
1329 remove_speculative()->
1330 cast_to_ptr_type(ptr)->
1331 with_offset(offset);
1332 }
1333 } else if (ta) {
1334 // For arrays indexed by constant indices, we flatten the alias
1335 // space to include all of the array body. Only the header, klass
1336 // and array length can be accessed un-aliased.
1337 if( offset != Type::OffsetBot ) {
1338 if( ta->const_oop() ) { // MethodData* or Method*
1339 offset = Type::OffsetBot; // Flatten constant access into array body
1340 tj = ta = ta->
1341 remove_speculative()->
1342 cast_to_ptr_type(ptr)->
1343 cast_to_exactness(false)->
1344 with_offset(offset);
1345 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1346 // range is OK as-is.
1347 tj = ta = TypeAryPtr::RANGE;
1348 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1349 tj = TypeInstPtr::KLASS; // all klass loads look alike
1350 ta = TypeAryPtr::RANGE; // generic ignored junk
1351 ptr = TypePtr::BotPTR;
1352 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1353 tj = TypeInstPtr::MARK;
1354 ta = TypeAryPtr::RANGE; // generic ignored junk
1355 ptr = TypePtr::BotPTR;
1356 } else { // Random constant offset into array body
1357 offset = Type::OffsetBot; // Flatten constant access into array body
1358 tj = ta = ta->
1359 remove_speculative()->
1360 cast_to_ptr_type(ptr)->
1361 cast_to_exactness(false)->
1362 with_offset(offset);
1363 }
1364 }
1365 // Arrays of fixed size alias with arrays of unknown size.
1366 if (ta->size() != TypeInt::POS) {
1367 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1368 tj = ta = ta->
1369 remove_speculative()->
1370 cast_to_ptr_type(ptr)->
1371 with_ary(tary)->
1372 cast_to_exactness(false);
1373 }
1374 // Arrays of known objects become arrays of unknown objects.
1375 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1376 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1377 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1378 }
1379 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1380 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1381 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1382 }
1383 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1384 // cannot be distinguished by bytecode alone.
1385 if (ta->elem() == TypeInt::BOOL) {
1386 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1387 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1388 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);
1389 }
1390 // During the 2nd round of IterGVN, NotNull castings are removed.
1391 // Make sure the Bottom and NotNull variants alias the same.
1392 // Also, make sure exact and non-exact variants alias the same.
1393 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1394 tj = ta = ta->
1395 remove_speculative()->
1396 cast_to_ptr_type(TypePtr::BotPTR)->
1397 cast_to_exactness(false)->
1398 with_offset(offset);
1399 }
1400 }
1401
1402 // Oop pointers need some flattening
1403 const TypeInstPtr *to = tj->isa_instptr();
1404 if (to && to != TypeOopPtr::BOTTOM) {
1405 ciInstanceKlass* ik = to->instance_klass();
1406 if( ptr == TypePtr::Constant ) {
1407 if (ik != ciEnv::current()->Class_klass() ||
1408 offset < ik->layout_helper_size_in_bytes()) {
1418 } else if( is_known_inst ) {
1419 tj = to; // Keep NotNull and klass_is_exact for instance type
1420 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1421 // During the 2nd round of IterGVN, NotNull castings are removed.
1422 // Make sure the Bottom and NotNull variants alias the same.
1423 // Also, make sure exact and non-exact variants alias the same.
1424 tj = to = to->
1425 remove_speculative()->
1426 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1427 cast_to_ptr_type(TypePtr::BotPTR)->
1428 cast_to_exactness(false);
1429 }
1430 if (to->speculative() != nullptr) {
1431 tj = to = to->remove_speculative();
1432 }
1433 // Canonicalize the holder of this field
1434 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1435 // First handle header references such as a LoadKlassNode, even if the
1436 // object's klass is unloaded at compile time (4965979).
1437 if (!is_known_inst) { // Do it only for non-instance types
1438 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, offset);
1439 }
1440 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1441 // Static fields are in the space above the normal instance
1442 // fields in the java.lang.Class instance.
1443 if (ik != ciEnv::current()->Class_klass()) {
1444 to = nullptr;
1445 tj = TypeOopPtr::BOTTOM;
1446 offset = tj->offset();
1447 }
1448 } else {
1449 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1450 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1451 if (!ik->equals(canonical_holder) || tj->offset() != offset) {
1452 if( is_known_inst ) {
1453 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, nullptr, offset, to->instance_id());
1454 } else {
1455 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, nullptr, offset);
1456 }
1457 }
1458 }
1459 }
1460
1461 // Klass pointers to object array klasses need some flattening
1462 const TypeKlassPtr *tk = tj->isa_klassptr();
1463 if( tk ) {
1464 // If we are referencing a field within a Klass, we need
1465 // to assume the worst case of an Object. Both exact and
1466 // inexact types must flatten to the same alias class so
1467 // use NotNull as the PTR.
1468 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1469 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1470 env()->Object_klass(),
1471 offset);
1472 }
1473
1474 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1475 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1476 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1477 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), offset);
1478 } else {
1479 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, offset);
1480 }
1481 }
1482
1483 // Check for precise loads from the primary supertype array and force them
1484 // to the supertype cache alias index. Check for generic array loads from
1485 // the primary supertype array and also force them to the supertype cache
1486 // alias index. Since the same load can reach both, we need to merge
1487 // these 2 disparate memories into the same alias class. Since the
1488 // primary supertype array is read-only, there's no chance of confusion
1489 // where we bypass an array load and an array store.
1490 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1491 if (offset == Type::OffsetBot ||
1492 (offset >= primary_supers_offset &&
1493 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1494 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1495 offset = in_bytes(Klass::secondary_super_cache_offset());
1496 tj = tk = tk->with_offset(offset);
1497 }
1498 }
1499
1500 // Flatten all Raw pointers together.
1501 if (tj->base() == Type::RawPtr)
1502 tj = TypeRawPtr::BOTTOM;
1592 intptr_t key = (intptr_t) adr_type;
1593 key ^= key >> logAliasCacheSize;
1594 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1595 }
1596
1597
1598 //-----------------------------grow_alias_types--------------------------------
1599 void Compile::grow_alias_types() {
1600 const int old_ats = _max_alias_types; // how many before?
1601 const int new_ats = old_ats; // how many more?
1602 const int grow_ats = old_ats+new_ats; // how many now?
1603 _max_alias_types = grow_ats;
1604 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1605 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1606 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1607 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1608 }
1609
1610
1611 //--------------------------------find_alias_type------------------------------
1612 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1613 if (!do_aliasing()) {
1614 return alias_type(AliasIdxBot);
1615 }
1616
1617 AliasCacheEntry* ace = probe_alias_cache(adr_type);
1618 if (ace->_adr_type == adr_type) {
1619 return alias_type(ace->_index);
1620 }
1621
1622 // Handle special cases.
1623 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1624 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1625
1626 // Do it the slow way.
1627 const TypePtr* flat = flatten_alias_type(adr_type);
1628
1629 #ifdef ASSERT
1630 {
1631 ResourceMark rm;
1632 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1633 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1634 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1635 Type::str(adr_type));
1636 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1637 const TypeOopPtr* foop = flat->is_oopptr();
1638 // Scalarizable allocations have exact klass always.
1639 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1649 if (alias_type(i)->adr_type() == flat) {
1650 idx = i;
1651 break;
1652 }
1653 }
1654
1655 if (idx == AliasIdxTop) {
1656 if (no_create) return nullptr;
1657 // Grow the array if necessary.
1658 if (_num_alias_types == _max_alias_types) grow_alias_types();
1659 // Add a new alias type.
1660 idx = _num_alias_types++;
1661 _alias_types[idx]->Init(idx, flat);
1662 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1663 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1664 if (flat->isa_instptr()) {
1665 if (flat->offset() == java_lang_Class::klass_offset()
1666 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1667 alias_type(idx)->set_rewritable(false);
1668 }
1669 if (flat->isa_aryptr()) {
1670 #ifdef ASSERT
1671 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1672 // (T_BYTE has the weakest alignment and size restrictions...)
1673 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1674 #endif
1675 if (flat->offset() == TypePtr::OffsetBot) {
1676 alias_type(idx)->set_element(flat->is_aryptr()->elem());
1677 }
1678 }
1679 if (flat->isa_klassptr()) {
1680 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1681 alias_type(idx)->set_rewritable(false);
1682 if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1683 alias_type(idx)->set_rewritable(false);
1684 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1685 alias_type(idx)->set_rewritable(false);
1686 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1687 alias_type(idx)->set_rewritable(false);
1688 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1689 alias_type(idx)->set_rewritable(false);
1690 }
1691 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1692 // but the base pointer type is not distinctive enough to identify
1693 // references into JavaThread.)
1694
1695 // Check for final fields.
1696 const TypeInstPtr* tinst = flat->isa_instptr();
1697 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1698 ciField* field;
1699 if (tinst->const_oop() != nullptr &&
1700 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1701 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1702 // static field
1703 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1704 field = k->get_field_by_offset(tinst->offset(), true);
1705 } else {
1706 ciInstanceKlass *k = tinst->instance_klass();
1707 field = k->get_field_by_offset(tinst->offset(), false);
1708 }
1709 assert(field == nullptr ||
1710 original_field == nullptr ||
1711 (field->holder() == original_field->holder() &&
1712 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1713 field->is_static() == original_field->is_static()), "wrong field?");
1714 // Set field() and is_rewritable() attributes.
1715 if (field != nullptr) alias_type(idx)->set_field(field);
1716 }
1717 }
1718
1719 // Fill the cache for next time.
1720 ace->_adr_type = adr_type;
1721 ace->_index = idx;
1722 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1723
1724 // Might as well try to fill the cache for the flattened version, too.
1725 AliasCacheEntry* face = probe_alias_cache(flat);
1726 if (face->_adr_type == nullptr) {
1727 face->_adr_type = flat;
1728 face->_index = idx;
1729 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1730 }
1731
1732 return alias_type(idx);
1733 }
1734
1735
1736 Compile::AliasType* Compile::alias_type(ciField* field) {
1737 const TypeOopPtr* t;
1738 if (field->is_static())
1739 t = TypeInstPtr::make(field->holder()->java_mirror());
1740 else
1741 t = TypeOopPtr::make_from_klass_raw(field->holder());
1742 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1743 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1744 return atp;
1745 }
1746
1747
1748 //------------------------------have_alias_type--------------------------------
1749 bool Compile::have_alias_type(const TypePtr* adr_type) {
1827 C->set_post_loop_opts_phase(); // no more loop opts allowed
1828
1829 assert(!C->major_progress(), "not cleared");
1830
1831 if (_for_post_loop_igvn.length() > 0) {
1832 while (_for_post_loop_igvn.length() > 0) {
1833 Node* n = _for_post_loop_igvn.pop();
1834 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1835 igvn._worklist.push(n);
1836 }
1837 igvn.optimize();
1838 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1839
1840 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1841 if (C->major_progress()) {
1842 C->clear_major_progress(); // ensure that major progress is now clear
1843 }
1844 }
1845 }
1846
1847 void Compile::record_unstable_if_trap(UnstableIfTrap* trap) {
1848 if (OptimizeUnstableIf) {
1849 _unstable_if_traps.append(trap);
1850 }
1851 }
1852
1853 void Compile::remove_useless_unstable_if_traps(Unique_Node_List& useful) {
1854 for (int i = _unstable_if_traps.length() - 1; i >= 0; i--) {
1855 UnstableIfTrap* trap = _unstable_if_traps.at(i);
1856 Node* n = trap->uncommon_trap();
1857 if (!useful.member(n)) {
1858 _unstable_if_traps.delete_at(i); // replaces i-th with last element which is known to be useful (already processed)
1859 }
1860 }
1861 }
1862
1863 // Remove the unstable if trap associated with 'unc' from candidates. It is either dead
1864 // or fold-compares case. Return true if succeed or not found.
1865 //
1866 // In rare cases, the found trap has been processed. It is too late to delete it. Return
2116 assert(has_stringbuilder(), "inconsistent");
2117 for_igvn()->clear();
2118 initial_gvn()->replace_with(&igvn);
2119
2120 inline_string_calls(false);
2121
2122 if (failing()) return;
2123
2124 inline_incrementally_cleanup(igvn);
2125 }
2126
2127 set_inlining_incrementally(false);
2128 }
2129
2130 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2131 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2132 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2133 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2134 // as if "inlining_incrementally() == true" were set.
2135 assert(inlining_incrementally() == false, "not allowed");
2136 assert(_modified_nodes == nullptr, "not allowed");
2137 assert(_late_inlines.length() > 0, "sanity");
2138
2139 while (_late_inlines.length() > 0) {
2140 for_igvn()->clear();
2141 initial_gvn()->replace_with(&igvn);
2142
2143 while (inline_incrementally_one()) {
2144 assert(!failing(), "inconsistent");
2145 }
2146 if (failing()) return;
2147
2148 inline_incrementally_cleanup(igvn);
2149 }
2150 }
2151
2152 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2153 if (_loop_opts_cnt > 0) {
2154 while (major_progress() && (_loop_opts_cnt > 0)) {
2155 TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2156 PhaseIdealLoop::optimize(igvn, mode);
2157 _loop_opts_cnt--;
2158 if (failing()) return false;
2159 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2160 }
2161 }
2162 return true;
2163 }
2164
2165 // Remove edges from "root" to each SafePoint at a backward branch.
2166 // They were inserted during parsing (see add_safepoint()) to make
2167 // infinite loops without calls or exceptions visible to root, i.e.,
2168 // useful.
2169 void Compile::remove_root_to_sfpts_edges(PhaseIterGVN& igvn) {
2273 Compile::TracePhase tp("", &timers[_t_renumberLive]);
2274 initial_gvn()->replace_with(&igvn);
2275 Unique_Node_List* old_worklist = for_igvn();
2276 old_worklist->clear();
2277 Unique_Node_List new_worklist(C->comp_arena());
2278 {
2279 ResourceMark rm;
2280 PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2281 }
2282 Unique_Node_List* save_for_igvn = for_igvn();
2283 set_for_igvn(&new_worklist);
2284 igvn = PhaseIterGVN(initial_gvn());
2285 igvn.optimize();
2286 set_for_igvn(old_worklist); // new_worklist is dead beyond this point
2287 }
2288
2289 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2290 // safepoints
2291 remove_root_to_sfpts_edges(igvn);
2292
2293 // Perform escape analysis
2294 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2295 if (has_loops()) {
2296 // Cleanup graph (remove dead nodes).
2297 TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2298 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2299 if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2300 if (failing()) return;
2301 }
2302 bool progress;
2303 do {
2304 ConnectionGraph::do_analysis(this, &igvn);
2305
2306 if (failing()) return;
2307
2308 int mcount = macro_count(); // Record number of allocations and locks before IGVN
2309
2310 // Optimize out fields loads from scalar replaceable allocations.
2311 igvn.optimize();
2312 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2386 print_method(PHASE_ITER_GVN2, 2);
2387
2388 if (failing()) return;
2389
2390 // Loop transforms on the ideal graph. Range Check Elimination,
2391 // peeling, unrolling, etc.
2392 if (!optimize_loops(igvn, LoopOptsDefault)) {
2393 return;
2394 }
2395
2396 if (failing()) return;
2397
2398 C->clear_major_progress(); // ensure that major progress is now clear
2399
2400 process_for_post_loop_opts_igvn(igvn);
2401
2402 #ifdef ASSERT
2403 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
2404 #endif
2405
2406 {
2407 TracePhase tp("macroExpand", &timers[_t_macroExpand]);
2408 PhaseMacroExpand mex(igvn);
2409 if (mex.expand_macro_nodes()) {
2410 assert(failing(), "must bail out w/ explicit message");
2411 return;
2412 }
2413 print_method(PHASE_MACRO_EXPANSION, 2);
2414 }
2415
2416 {
2417 TracePhase tp("barrierExpand", &timers[_t_barrierExpand]);
2418 if (bs->expand_barriers(this, igvn)) {
2419 assert(failing(), "must bail out w/ explicit message");
2420 return;
2421 }
2422 print_method(PHASE_BARRIER_EXPANSION, 2);
2423 }
2424
2425 if (C->max_vector_size() > 0) {
2426 C->optimize_logic_cones(igvn);
2427 igvn.optimize();
2428 }
2429
2430 DEBUG_ONLY( _modified_nodes = nullptr; )
2431
2432 assert(igvn._worklist.size() == 0, "not empty");
2433
2434 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
2435
2436 if (_late_inlines.length() > 0) {
2437 // More opportunities to optimize virtual and MH calls.
2438 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
2439 process_late_inline_calls_no_inline(igvn);
2440 }
2441 } // (End scope of igvn; run destructor if necessary for asserts.)
2442
2443 check_no_dead_use();
2444
2445 process_print_inlining();
2446
2447 // A method with only infinite loops has no edges entering loops from root
2448 {
2449 TracePhase tp("graphReshape", &timers[_t_graphReshaping]);
2450 if (final_graph_reshaping()) {
2451 assert(failing(), "must bail out w/ explicit message");
2452 return;
2453 }
2454 }
2455
2456 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2457 DEBUG_ONLY(set_phase_optimize_finished();)
2458 }
2459
2460 #ifdef ASSERT
3040 // Accumulate any precedence edges
3041 if (mem->in(i) != nullptr) {
3042 n->add_prec(mem->in(i));
3043 }
3044 }
3045 // Everything above this point has been processed.
3046 done = true;
3047 }
3048 // Eliminate the previous StoreCM
3049 prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
3050 assert(mem->outcnt() == 0, "should be dead");
3051 mem->disconnect_inputs(this);
3052 } else {
3053 prev = mem;
3054 }
3055 mem = prev->in(MemNode::Memory);
3056 }
3057 }
3058 }
3059
3060 //------------------------------final_graph_reshaping_impl----------------------
3061 // Implement items 1-5 from final_graph_reshaping below.
3062 void Compile::final_graph_reshaping_impl(Node *n, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3063
3064 if ( n->outcnt() == 0 ) return; // dead node
3065 uint nop = n->Opcode();
3066
3067 // Check for 2-input instruction with "last use" on right input.
3068 // Swap to left input. Implements item (2).
3069 if( n->req() == 3 && // two-input instruction
3070 n->in(1)->outcnt() > 1 && // left use is NOT a last use
3071 (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
3072 n->in(2)->outcnt() == 1 &&// right use IS a last use
3073 !n->in(2)->is_Con() ) { // right use is not a constant
3074 // Check for commutative opcode
3075 switch( nop ) {
3076 case Op_AddI: case Op_AddF: case Op_AddD: case Op_AddL:
3077 case Op_MaxI: case Op_MaxL: case Op_MaxF: case Op_MaxD:
3078 case Op_MinI: case Op_MinL: case Op_MinF: case Op_MinD:
3079 case Op_MulI: case Op_MulF: case Op_MulD: case Op_MulL:
3192 if (n->outcnt() > 1 &&
3193 !n->is_Proj() &&
3194 nop != Op_CreateEx &&
3195 nop != Op_CheckCastPP &&
3196 nop != Op_DecodeN &&
3197 nop != Op_DecodeNKlass &&
3198 !n->is_Mem() &&
3199 !n->is_Phi()) {
3200 Node *x = n->clone();
3201 call->set_req(TypeFunc::Parms, x);
3202 }
3203 }
3204 break;
3205 }
3206
3207 case Op_StoreCM:
3208 {
3209 // Convert OopStore dependence into precedence edge
3210 Node* prec = n->in(MemNode::OopStore);
3211 n->del_req(MemNode::OopStore);
3212 n->add_prec(prec);
3213 eliminate_redundant_card_marks(n);
3214 }
3215
3216 // fall through
3217
3218 case Op_StoreB:
3219 case Op_StoreC:
3220 case Op_StoreI:
3221 case Op_StoreL:
3222 case Op_CompareAndSwapB:
3223 case Op_CompareAndSwapS:
3224 case Op_CompareAndSwapI:
3225 case Op_CompareAndSwapL:
3226 case Op_CompareAndSwapP:
3227 case Op_CompareAndSwapN:
3228 case Op_WeakCompareAndSwapB:
3229 case Op_WeakCompareAndSwapS:
3230 case Op_WeakCompareAndSwapI:
3231 case Op_WeakCompareAndSwapL:
3232 case Op_WeakCompareAndSwapP:
3788 // Replace all nodes with identical edges as m with m
3789 k->subsume_by(m, this);
3790 }
3791 }
3792 }
3793 break;
3794 }
3795 case Op_CmpUL: {
3796 if (!Matcher::has_match_rule(Op_CmpUL)) {
3797 // No support for unsigned long comparisons
3798 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3799 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3800 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3801 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3802 Node* andl = new AndLNode(orl, remove_sign_mask);
3803 Node* cmp = new CmpLNode(andl, n->in(2));
3804 n->subsume_by(cmp, this);
3805 }
3806 break;
3807 }
3808 default:
3809 assert(!n->is_Call(), "");
3810 assert(!n->is_Mem(), "");
3811 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3812 break;
3813 }
3814 }
3815
3816 //------------------------------final_graph_reshaping_walk---------------------
3817 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3818 // requires that the walk visits a node's inputs before visiting the node.
3819 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3820 Unique_Node_List sfpt;
3821
3822 frc._visited.set(root->_idx); // first, mark node as visited
3823 uint cnt = root->req();
3824 Node *n = root;
3825 uint i = 0;
3826 while (true) {
3827 if (i < cnt) {
4169 }
4170 }
4171
4172 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4173 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4174 }
4175
4176 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4177 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4178 }
4179
4180 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4181 if (holder->is_initialized()) {
4182 return false;
4183 }
4184 if (holder->is_being_initialized()) {
4185 if (accessing_method->holder() == holder) {
4186 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4187 // <init>, or a static method. In all those cases, there was an initialization
4188 // barrier on the holder klass passed.
4189 if (accessing_method->is_static_initializer() ||
4190 accessing_method->is_object_initializer() ||
4191 accessing_method->is_static()) {
4192 return false;
4193 }
4194 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4195 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4196 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4197 // child class can become fully initialized while its parent class is still being initialized.
4198 if (accessing_method->is_static_initializer()) {
4199 return false;
4200 }
4201 }
4202 ciMethod* root = method(); // the root method of compilation
4203 if (root != accessing_method) {
4204 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4205 }
4206 }
4207 return true;
4208 }
4209
4210 #ifndef PRODUCT
4211 //------------------------------verify_bidirectional_edges---------------------
4212 // For each input edge to a node (ie - for each Use-Def edge), verify that
4213 // there is a corresponding Def-Use edge.
4214 void Compile::verify_bidirectional_edges(Unique_Node_List &visited) {
4215 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4216 uint stack_size = live_nodes() >> 4;
4217 Node_List nstack(MAX2(stack_size, (uint)OptoNodeListSize));
4218 nstack.push(_root);
4234 if (in != nullptr && !in->is_top()) {
4235 // Count instances of `next`
4236 int cnt = 0;
4237 for (uint idx = 0; idx < in->_outcnt; idx++) {
4238 if (in->_out[idx] == n) {
4239 cnt++;
4240 }
4241 }
4242 assert(cnt > 0, "Failed to find Def-Use edge.");
4243 // Check for duplicate edges
4244 // walk the input array downcounting the input edges to n
4245 for (uint j = 0; j < length; j++) {
4246 if (n->in(j) == in) {
4247 cnt--;
4248 }
4249 }
4250 assert(cnt == 0, "Mismatched edge count.");
4251 } else if (in == nullptr) {
4252 assert(i == 0 || i >= n->req() ||
4253 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4254 (n->is_Unlock() && i == (n->req() - 1)) ||
4255 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4256 "only region, phi, arraycopy, unlock or membar nodes have null data edges");
4257 } else {
4258 assert(in->is_top(), "sanity");
4259 // Nothing to check.
4260 }
4261 }
4262 }
4263 }
4264
4265 //------------------------------verify_graph_edges---------------------------
4266 // Walk the Graph and verify that there is a one-to-one correspondence
4267 // between Use-Def edges and Def-Use edges in the graph.
4268 void Compile::verify_graph_edges(bool no_dead_code) {
4269 if (VerifyGraphEdges) {
4270 Unique_Node_List visited;
4271
4272 // Call graph walk to check edges
4273 verify_bidirectional_edges(visited);
4274 if (no_dead_code) {
4275 // Now make sure that no visited node is used by an unvisited node.
4276 bool dead_nodes = false;
4370 // (1) subklass is already limited to a subtype of superklass => always ok
4371 // (2) subklass does not overlap with superklass => always fail
4372 // (3) superklass has NO subtypes and we can check with a simple compare.
4373 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4374 if (skip) {
4375 return SSC_full_test; // Let caller generate the general case.
4376 }
4377
4378 if (subk->is_java_subtype_of(superk)) {
4379 return SSC_always_true; // (0) and (1) this test cannot fail
4380 }
4381
4382 if (!subk->maybe_java_subtype_of(superk)) {
4383 return SSC_always_false; // (2) true path dead; no dynamic test needed
4384 }
4385
4386 const Type* superelem = superk;
4387 if (superk->isa_aryklassptr()) {
4388 int ignored;
4389 superelem = superk->is_aryklassptr()->base_element_type(ignored);
4390 }
4391
4392 if (superelem->isa_instklassptr()) {
4393 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4394 if (!ik->has_subklass()) {
4395 if (!ik->is_final()) {
4396 // Add a dependency if there is a chance of a later subclass.
4397 dependencies()->assert_leaf_type(ik);
4398 }
4399 if (!superk->maybe_java_subtype_of(subk)) {
4400 return SSC_always_false;
4401 }
4402 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4403 }
4404 } else {
4405 // A primitive array type has no subtypes.
4406 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4407 }
4408
4409 return SSC_full_test;
4921 const Type* t = igvn.type_or_null(n);
4922 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
4923 if (n->is_Type()) {
4924 t = n->as_Type()->type();
4925 assert(t == t->remove_speculative(), "no more speculative types");
4926 }
4927 // Iterate over outs - endless loops is unreachable from below
4928 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4929 Node *m = n->fast_out(i);
4930 if (not_a_node(m)) {
4931 continue;
4932 }
4933 worklist.push(m);
4934 }
4935 }
4936 igvn.check_no_speculative_types();
4937 #endif
4938 }
4939 }
4940
4941 // Auxiliary methods to support randomized stressing/fuzzing.
4942
4943 int Compile::random() {
4944 _stress_seed = os::next_random(_stress_seed);
4945 return static_cast<int>(_stress_seed);
4946 }
4947
4948 // This method can be called the arbitrary number of times, with current count
4949 // as the argument. The logic allows selecting a single candidate from the
4950 // running list of candidates as follows:
4951 // int count = 0;
4952 // Cand* selected = null;
4953 // while(cand = cand->next()) {
4954 // if (randomized_select(++count)) {
4955 // selected = cand;
4956 // }
4957 // }
4958 //
4959 // Including count equalizes the chances any candidate is "selected".
4960 // This is useful when we don't have the complete list of candidates to choose
|
36 #include "gc/shared/barrierSet.hpp"
37 #include "gc/shared/c2/barrierSetC2.hpp"
38 #include "jfr/jfrEvents.hpp"
39 #include "jvm_io.h"
40 #include "memory/allocation.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "opto/addnode.hpp"
43 #include "opto/block.hpp"
44 #include "opto/c2compiler.hpp"
45 #include "opto/callGenerator.hpp"
46 #include "opto/callnode.hpp"
47 #include "opto/castnode.hpp"
48 #include "opto/cfgnode.hpp"
49 #include "opto/chaitin.hpp"
50 #include "opto/compile.hpp"
51 #include "opto/connode.hpp"
52 #include "opto/convertnode.hpp"
53 #include "opto/divnode.hpp"
54 #include "opto/escape.hpp"
55 #include "opto/idealGraphPrinter.hpp"
56 #include "opto/inlinetypenode.hpp"
57 #include "opto/loopnode.hpp"
58 #include "opto/machnode.hpp"
59 #include "opto/macro.hpp"
60 #include "opto/matcher.hpp"
61 #include "opto/mathexactnode.hpp"
62 #include "opto/memnode.hpp"
63 #include "opto/mulnode.hpp"
64 #include "opto/narrowptrnode.hpp"
65 #include "opto/node.hpp"
66 #include "opto/opcodes.hpp"
67 #include "opto/output.hpp"
68 #include "opto/parse.hpp"
69 #include "opto/phaseX.hpp"
70 #include "opto/rootnode.hpp"
71 #include "opto/runtime.hpp"
72 #include "opto/stringopts.hpp"
73 #include "opto/type.hpp"
74 #include "opto/vector.hpp"
75 #include "opto/vectornode.hpp"
76 #include "runtime/globals_extension.hpp"
376 // Constant node that has no out-edges and has only one in-edge from
377 // root is usually dead. However, sometimes reshaping walk makes
378 // it reachable by adding use edges. So, we will NOT count Con nodes
379 // as dead to be conservative about the dead node count at any
380 // given time.
381 if (!dead->is_Con()) {
382 record_dead_node(dead->_idx);
383 }
384 if (dead->is_macro()) {
385 remove_macro_node(dead);
386 }
387 if (dead->is_expensive()) {
388 remove_expensive_node(dead);
389 }
390 if (dead->Opcode() == Op_Opaque4) {
391 remove_template_assertion_predicate_opaq(dead);
392 }
393 if (dead->for_post_loop_opts_igvn()) {
394 remove_from_post_loop_opts_igvn(dead);
395 }
396 if (dead->is_InlineType()) {
397 remove_inline_type(dead);
398 }
399 if (dead->is_Call()) {
400 remove_useless_late_inlines( &_late_inlines, dead);
401 remove_useless_late_inlines( &_string_late_inlines, dead);
402 remove_useless_late_inlines( &_boxing_late_inlines, dead);
403 remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
404
405 if (dead->is_CallStaticJava()) {
406 remove_unstable_if_trap(dead->as_CallStaticJava(), false);
407 }
408 }
409 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
410 bs->unregister_potential_barrier_node(dead);
411 }
412
413 // Disconnect all useless nodes by disconnecting those at the boundary.
414 void Compile::disconnect_useless_nodes(Unique_Node_List &useful, Unique_Node_List* worklist) {
415 uint next = 0;
416 while (next < useful.size()) {
417 Node *n = useful.at(next++);
418 if (n->is_SafePoint()) {
419 // We're done with a parsing phase. Replaced nodes are not valid
420 // beyond that point.
421 n->as_SafePoint()->delete_replaced_nodes();
422 }
423 // Use raw traversal of out edges since this code removes out edges
424 int max = n->outcnt();
425 for (int j = 0; j < max; ++j) {
426 Node* child = n->raw_out(j);
427 if (!useful.member(child)) {
428 assert(!child->is_top() || child != top(),
429 "If top is cached in Compile object it is in useful list");
430 // Only need to remove this out-edge to the useless node
431 n->raw_del_out(j);
432 --j;
433 --max;
434 }
435 }
436 if (n->outcnt() == 1 && n->has_special_unique_user()) {
437 worklist->push(n->unique_out());
438 }
439 if (n->outcnt() == 0) {
440 worklist->push(n);
441 }
442 }
443
444 remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes
445 remove_useless_nodes(_parse_predicate_opaqs, useful); // remove useless Parse Predicate opaque nodes
446 remove_useless_nodes(_template_assertion_predicate_opaqs, useful); // remove useless Assertion Predicate opaque nodes
447 remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes
448 remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
449 remove_useless_nodes(_inline_type_nodes, useful); // remove useless inline type nodes
450 #ifdef ASSERT
451 if (_modified_nodes != nullptr) {
452 _modified_nodes->remove_useless_nodes(useful.member_set());
453 }
454 #endif
455 remove_useless_unstable_if_traps(useful); // remove useless unstable_if traps
456 remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes
457 #ifdef ASSERT
458 if (_modified_nodes != nullptr) {
459 _modified_nodes->remove_useless_nodes(useful.member_set());
460 }
461 #endif
462
463 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
464 bs->eliminate_useless_gc_barriers(useful, this);
465 // clean up the late inline lists
466 remove_useless_late_inlines( &_late_inlines, useful);
467 remove_useless_late_inlines( &_string_late_inlines, useful);
468 remove_useless_late_inlines( &_boxing_late_inlines, useful);
469 remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
470 debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
471 }
472
473 // ============================================================================
474 //------------------------------CompileWrapper---------------------------------
596 // the continuation bci for on stack replacement.
597
598
599 Compile::Compile( ciEnv* ci_env, ciMethod* target, int osr_bci,
600 Options options, DirectiveSet* directive)
601 : Phase(Compiler),
602 _compile_id(ci_env->compile_id()),
603 _options(options),
604 _method(target),
605 _entry_bci(osr_bci),
606 _ilt(nullptr),
607 _stub_function(nullptr),
608 _stub_name(nullptr),
609 _stub_entry_point(nullptr),
610 _max_node_limit(MaxNodeLimit),
611 _post_loop_opts_phase(false),
612 _inlining_progress(false),
613 _inlining_incrementally(false),
614 _do_cleanup(false),
615 _has_reserved_stack_access(target->has_reserved_stack_access()),
616 _has_circular_inline_type(false),
617 #ifndef PRODUCT
618 _igv_idx(0),
619 _trace_opto_output(directive->TraceOptoOutputOption),
620 #endif
621 _has_method_handle_invokes(false),
622 _clinit_barrier_on_entry(false),
623 _stress_seed(0),
624 _comp_arena(mtCompiler),
625 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
626 _env(ci_env),
627 _directive(directive),
628 _log(ci_env->log()),
629 _failure_reason(nullptr),
630 _intrinsics (comp_arena(), 0, 0, nullptr),
631 _macro_nodes (comp_arena(), 8, 0, nullptr),
632 _parse_predicate_opaqs (comp_arena(), 8, 0, nullptr),
633 _template_assertion_predicate_opaqs (comp_arena(), 8, 0, nullptr),
634 _expensive_nodes (comp_arena(), 8, 0, nullptr),
635 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
636 _inline_type_nodes (comp_arena(), 8, 0, nullptr),
637 _unstable_if_traps (comp_arena(), 8, 0, nullptr),
638 _coarsened_locks (comp_arena(), 8, 0, nullptr),
639 _congraph(nullptr),
640 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
641 _dead_node_list(comp_arena()),
642 _dead_node_count(0),
643 _node_arena(mtCompiler),
644 _old_arena(mtCompiler),
645 _mach_constant_base_node(nullptr),
646 _Compile_types(mtCompiler),
647 _initial_gvn(nullptr),
648 _for_igvn(nullptr),
649 _late_inlines(comp_arena(), 2, 0, nullptr),
650 _string_late_inlines(comp_arena(), 2, 0, nullptr),
651 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
652 _vector_reboxing_late_inlines(comp_arena(), 2, 0, nullptr),
653 _late_inlines_pos(0),
654 _number_of_mh_late_inlines(0),
655 _print_inlining_stream(new (mtCompiler) stringStream()),
656 _print_inlining_list(nullptr),
721 // Node list that Iterative GVN will start with
722 Unique_Node_List for_igvn(comp_arena());
723 set_for_igvn(&for_igvn);
724
725 // GVN that will be run immediately on new nodes
726 uint estimated_size = method()->code_size()*4+64;
727 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
728 PhaseGVN gvn(node_arena(), estimated_size);
729 set_initial_gvn(&gvn);
730
731 print_inlining_init();
732 { // Scope for timing the parser
733 TracePhase tp("parse", &timers[_t_parser]);
734
735 // Put top into the hash table ASAP.
736 initial_gvn()->transform_no_reclaim(top());
737
738 // Set up tf(), start(), and find a CallGenerator.
739 CallGenerator* cg = nullptr;
740 if (is_osr_compilation()) {
741 init_tf(TypeFunc::make(method(), /* is_osr_compilation = */ true));
742 StartNode* s = new StartOSRNode(root(), tf()->domain_sig());
743 initial_gvn()->set_type_bottom(s);
744 init_start(s);
745 cg = CallGenerator::for_osr(method(), entry_bci());
746 } else {
747 // Normal case.
748 init_tf(TypeFunc::make(method()));
749 StartNode* s = new StartNode(root(), tf()->domain_cc());
750 initial_gvn()->set_type_bottom(s);
751 init_start(s);
752 if (method()->intrinsic_id() == vmIntrinsics::_Reference_get) {
753 // With java.lang.ref.reference.get() we must go through the
754 // intrinsic - even when get() is the root
755 // method of the compile - so that, if necessary, the value in
756 // the referent field of the reference object gets recorded by
757 // the pre-barrier code.
758 cg = find_intrinsic(method(), false);
759 }
760 if (cg == nullptr) {
761 float past_uses = method()->interpreter_invocation_count();
762 float expected_uses = past_uses;
763 cg = CallGenerator::for_inline(method(), expected_uses);
764 }
765 }
766 if (failing()) return;
767 if (cg == nullptr) {
768 const char* reason = InlineTree::check_can_parse(method());
769 assert(reason != nullptr, "expect reason for parse failure");
858 print_ideal_ir("print_ideal");
859 }
860 #endif
861
862 #ifdef ASSERT
863 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
864 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
865 #endif
866
867 // Dump compilation data to replay it.
868 if (directive->DumpReplayOption) {
869 env()->dump_replay_data(_compile_id);
870 }
871 if (directive->DumpInlineOption && (ilt() != nullptr)) {
872 env()->dump_inline_data(_compile_id);
873 }
874
875 // Now that we know the size of all the monitors we can add a fixed slot
876 // for the original deopt pc.
877 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
878 if (needs_stack_repair()) {
879 // One extra slot for the special stack increment value
880 next_slot += 2;
881 }
882 // TODO 8284443 Only reserve extra slot if needed
883 if (InlineTypeReturnedAsFields) {
884 // One extra slot to hold the IsInit information for a nullable
885 // inline type return if we run out of registers.
886 next_slot += 2;
887 }
888 set_fixed_slots(next_slot);
889
890 // Compute when to use implicit null checks. Used by matching trap based
891 // nodes and NullCheck optimization.
892 set_allowed_deopt_reasons();
893
894 // Now generate code
895 Code_Gen();
896 }
897
898 //------------------------------Compile----------------------------------------
899 // Compile a runtime stub
900 Compile::Compile( ciEnv* ci_env,
901 TypeFunc_generator generator,
902 address stub_function,
903 const char *stub_name,
904 int is_fancy_jump,
905 bool pass_tls,
906 bool return_pc,
907 DirectiveSet* directive)
908 : Phase(Compiler),
909 _compile_id(0),
910 _options(Options::for_runtime_stub()),
911 _method(nullptr),
912 _entry_bci(InvocationEntryBci),
913 _stub_function(stub_function),
914 _stub_name(stub_name),
915 _stub_entry_point(nullptr),
916 _max_node_limit(MaxNodeLimit),
917 _post_loop_opts_phase(false),
918 _inlining_progress(false),
919 _inlining_incrementally(false),
920 _has_reserved_stack_access(false),
921 _has_circular_inline_type(false),
922 #ifndef PRODUCT
923 _igv_idx(0),
924 _trace_opto_output(directive->TraceOptoOutputOption),
925 #endif
926 _has_method_handle_invokes(false),
927 _clinit_barrier_on_entry(false),
928 _stress_seed(0),
929 _comp_arena(mtCompiler),
930 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
931 _env(ci_env),
932 _directive(directive),
933 _log(ci_env->log()),
934 _failure_reason(nullptr),
935 _congraph(nullptr),
936 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
937 _dead_node_list(comp_arena()),
938 _dead_node_count(0),
939 _node_arena(mtCompiler),
940 _old_arena(mtCompiler),
941 _mach_constant_base_node(nullptr),
1027 // Create Debug Information Recorder to record scopes, oopmaps, etc.
1028 env()->set_oop_recorder(new OopRecorder(env()->arena()));
1029 env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));
1030 env()->set_dependencies(new Dependencies(env()));
1031
1032 _fixed_slots = 0;
1033 set_has_split_ifs(false);
1034 set_has_loops(false); // first approximation
1035 set_has_stringbuilder(false);
1036 set_has_boxed_value(false);
1037 _trap_can_recompile = false; // no traps emitted yet
1038 _major_progress = true; // start out assuming good things will happen
1039 set_has_unsafe_access(false);
1040 set_max_vector_size(0);
1041 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1042 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1043 set_decompile_count(0);
1044
1045 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1046 _loop_opts_cnt = LoopOptsCount;
1047 _has_flattened_accesses = false;
1048 _flattened_accesses_share_alias = true;
1049 _scalarize_in_safepoints = false;
1050
1051 set_do_inlining(Inline);
1052 set_max_inline_size(MaxInlineSize);
1053 set_freq_inline_size(FreqInlineSize);
1054 set_do_scheduling(OptoScheduling);
1055
1056 set_do_vector_loop(false);
1057 set_has_monitors(false);
1058
1059 if (AllowVectorizeOnDemand) {
1060 if (has_method() && (_directive->VectorizeOption || _directive->VectorizeDebugOption)) {
1061 set_do_vector_loop(true);
1062 NOT_PRODUCT(if (do_vector_loop() && Verbose) {tty->print("Compile::Init: do vectorized loops (SIMD like) for method %s\n", method()->name()->as_quoted_ascii());})
1063 } else if (has_method() && method()->name() != 0 &&
1064 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1065 set_do_vector_loop(true);
1066 }
1067 }
1068 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1069 NOT_PRODUCT(if (use_cmove() && Verbose && has_method()) {tty->print("Compile::Init: use CMove without profitability tests for method %s\n", method()->name()->as_quoted_ascii());})
1070
1319 // If this method has already thrown a range-check,
1320 // assume it was because we already tried range smearing
1321 // and it failed.
1322 uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1323 return !already_trapped;
1324 }
1325
1326
1327 //------------------------------flatten_alias_type-----------------------------
1328 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1329 assert(do_aliasing(), "Aliasing should be enabled");
1330 int offset = tj->offset();
1331 TypePtr::PTR ptr = tj->ptr();
1332
1333 // Known instance (scalarizable allocation) alias only with itself.
1334 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1335 tj->is_oopptr()->is_known_instance();
1336
1337 // Process weird unsafe references.
1338 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1339 bool default_value_load = EnableValhalla && tj->is_instptr()->instance_klass() == ciEnv::current()->Class_klass();
1340 assert(InlineUnsafeOps || StressReflectiveCode || default_value_load, "indeterminate pointers come only from unsafe ops");
1341 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1342 tj = TypeOopPtr::BOTTOM;
1343 ptr = tj->ptr();
1344 offset = tj->offset();
1345 }
1346
1347 // Array pointers need some flattening
1348 const TypeAryPtr* ta = tj->isa_aryptr();
1349 if (ta && ta->is_stable()) {
1350 // Erase stability property for alias analysis.
1351 tj = ta = ta->cast_to_stable(false);
1352 }
1353 if (ta && ta->is_not_flat()) {
1354 // Erase not flat property for alias analysis.
1355 tj = ta = ta->cast_to_not_flat(false);
1356 }
1357 if (ta && ta->is_not_null_free()) {
1358 // Erase not null free property for alias analysis.
1359 tj = ta = ta->cast_to_not_null_free(false);
1360 }
1361
1362 if( ta && is_known_inst ) {
1363 if ( offset != Type::OffsetBot &&
1364 offset > arrayOopDesc::length_offset_in_bytes() ) {
1365 offset = Type::OffsetBot; // Flatten constant access into array body only
1366 tj = ta = ta->
1367 remove_speculative()->
1368 cast_to_ptr_type(ptr)->
1369 with_offset(offset);
1370 }
1371 } else if (ta) {
1372 // For arrays indexed by constant indices, we flatten the alias
1373 // space to include all of the array body. Only the header, klass
1374 // and array length can be accessed un-aliased.
1375 // For flattened inline type array, each field has its own slice so
1376 // we must include the field offset.
1377 if( offset != Type::OffsetBot ) {
1378 if( ta->const_oop() ) { // MethodData* or Method*
1379 offset = Type::OffsetBot; // Flatten constant access into array body
1380 tj = ta = ta->
1381 remove_speculative()->
1382 cast_to_ptr_type(ptr)->
1383 cast_to_exactness(false)->
1384 with_offset(offset);
1385 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1386 // range is OK as-is.
1387 tj = ta = TypeAryPtr::RANGE;
1388 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1389 tj = TypeInstPtr::KLASS; // all klass loads look alike
1390 ta = TypeAryPtr::RANGE; // generic ignored junk
1391 ptr = TypePtr::BotPTR;
1392 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1393 tj = TypeInstPtr::MARK;
1394 ta = TypeAryPtr::RANGE; // generic ignored junk
1395 ptr = TypePtr::BotPTR;
1396 } else { // Random constant offset into array body
1397 offset = Type::OffsetBot; // Flatten constant access into array body
1398 tj = ta = ta->
1399 remove_speculative()->
1400 cast_to_ptr_type(ptr)->
1401 cast_to_exactness(false)->
1402 with_offset(offset);
1403 }
1404 }
1405 // Arrays of fixed size alias with arrays of unknown size.
1406 if (ta->size() != TypeInt::POS) {
1407 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1408 tj = ta = ta->
1409 remove_speculative()->
1410 cast_to_ptr_type(ptr)->
1411 with_ary(tary)->
1412 cast_to_exactness(false);
1413 }
1414 // Arrays of known objects become arrays of unknown objects.
1415 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1416 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1417 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1418 }
1419 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1420 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1421 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1422 }
1423 // Initially all flattened array accesses share a single slice
1424 if (ta->is_flat() && ta->elem() != TypeInstPtr::BOTTOM && _flattened_accesses_share_alias) {
1425 const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size(), /* stable= */ false, /* flat= */ true);
1426 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), Type::Offset(Type::OffsetBot));
1427 }
1428 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1429 // cannot be distinguished by bytecode alone.
1430 if (ta->elem() == TypeInt::BOOL) {
1431 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1432 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1433 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,Type::Offset(offset), ta->field_offset());
1434 }
1435 // During the 2nd round of IterGVN, NotNull castings are removed.
1436 // Make sure the Bottom and NotNull variants alias the same.
1437 // Also, make sure exact and non-exact variants alias the same.
1438 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1439 tj = ta = ta->
1440 remove_speculative()->
1441 cast_to_ptr_type(TypePtr::BotPTR)->
1442 cast_to_exactness(false)->
1443 with_offset(offset);
1444 }
1445 }
1446
1447 // Oop pointers need some flattening
1448 const TypeInstPtr *to = tj->isa_instptr();
1449 if (to && to != TypeOopPtr::BOTTOM) {
1450 ciInstanceKlass* ik = to->instance_klass();
1451 if( ptr == TypePtr::Constant ) {
1452 if (ik != ciEnv::current()->Class_klass() ||
1453 offset < ik->layout_helper_size_in_bytes()) {
1463 } else if( is_known_inst ) {
1464 tj = to; // Keep NotNull and klass_is_exact for instance type
1465 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1466 // During the 2nd round of IterGVN, NotNull castings are removed.
1467 // Make sure the Bottom and NotNull variants alias the same.
1468 // Also, make sure exact and non-exact variants alias the same.
1469 tj = to = to->
1470 remove_speculative()->
1471 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1472 cast_to_ptr_type(TypePtr::BotPTR)->
1473 cast_to_exactness(false);
1474 }
1475 if (to->speculative() != nullptr) {
1476 tj = to = to->remove_speculative();
1477 }
1478 // Canonicalize the holder of this field
1479 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1480 // First handle header references such as a LoadKlassNode, even if the
1481 // object's klass is unloaded at compile time (4965979).
1482 if (!is_known_inst) { // Do it only for non-instance types
1483 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, Type::Offset(offset));
1484 }
1485 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1486 // Static fields are in the space above the normal instance
1487 // fields in the java.lang.Class instance.
1488 if (ik != ciEnv::current()->Class_klass()) {
1489 to = nullptr;
1490 tj = TypeOopPtr::BOTTOM;
1491 offset = tj->offset();
1492 }
1493 } else {
1494 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1495 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1496 if (!ik->equals(canonical_holder) || tj->offset() != offset) {
1497 if( is_known_inst ) {
1498 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, nullptr, Type::Offset(offset), to->instance_id());
1499 } else {
1500 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, nullptr, Type::Offset(offset));
1501 }
1502 }
1503 }
1504 }
1505
1506 // Klass pointers to object array klasses need some flattening
1507 const TypeKlassPtr *tk = tj->isa_klassptr();
1508 if( tk ) {
1509 // If we are referencing a field within a Klass, we need
1510 // to assume the worst case of an Object. Both exact and
1511 // inexact types must flatten to the same alias class so
1512 // use NotNull as the PTR.
1513 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1514 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1515 env()->Object_klass(),
1516 Type::Offset(offset));
1517 }
1518
1519 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1520 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1521 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1522 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), Type::Offset(offset));
1523 } else {
1524 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, Type::Offset(offset), tk->is_not_flat(), tk->is_not_null_free(), tk->is_null_free());
1525 }
1526 }
1527 // Check for precise loads from the primary supertype array and force them
1528 // to the supertype cache alias index. Check for generic array loads from
1529 // the primary supertype array and also force them to the supertype cache
1530 // alias index. Since the same load can reach both, we need to merge
1531 // these 2 disparate memories into the same alias class. Since the
1532 // primary supertype array is read-only, there's no chance of confusion
1533 // where we bypass an array load and an array store.
1534 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1535 if (offset == Type::OffsetBot ||
1536 (offset >= primary_supers_offset &&
1537 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1538 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1539 offset = in_bytes(Klass::secondary_super_cache_offset());
1540 tj = tk = tk->with_offset(offset);
1541 }
1542 }
1543
1544 // Flatten all Raw pointers together.
1545 if (tj->base() == Type::RawPtr)
1546 tj = TypeRawPtr::BOTTOM;
1636 intptr_t key = (intptr_t) adr_type;
1637 key ^= key >> logAliasCacheSize;
1638 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1639 }
1640
1641
1642 //-----------------------------grow_alias_types--------------------------------
1643 void Compile::grow_alias_types() {
1644 const int old_ats = _max_alias_types; // how many before?
1645 const int new_ats = old_ats; // how many more?
1646 const int grow_ats = old_ats+new_ats; // how many now?
1647 _max_alias_types = grow_ats;
1648 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1649 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1650 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1651 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1652 }
1653
1654
1655 //--------------------------------find_alias_type------------------------------
1656 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1657 if (!do_aliasing()) {
1658 return alias_type(AliasIdxBot);
1659 }
1660
1661 AliasCacheEntry* ace = nullptr;
1662 if (!uncached) {
1663 ace = probe_alias_cache(adr_type);
1664 if (ace->_adr_type == adr_type) {
1665 return alias_type(ace->_index);
1666 }
1667 }
1668
1669 // Handle special cases.
1670 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1671 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1672
1673 // Do it the slow way.
1674 const TypePtr* flat = flatten_alias_type(adr_type);
1675
1676 #ifdef ASSERT
1677 {
1678 ResourceMark rm;
1679 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1680 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1681 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1682 Type::str(adr_type));
1683 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1684 const TypeOopPtr* foop = flat->is_oopptr();
1685 // Scalarizable allocations have exact klass always.
1686 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1696 if (alias_type(i)->adr_type() == flat) {
1697 idx = i;
1698 break;
1699 }
1700 }
1701
1702 if (idx == AliasIdxTop) {
1703 if (no_create) return nullptr;
1704 // Grow the array if necessary.
1705 if (_num_alias_types == _max_alias_types) grow_alias_types();
1706 // Add a new alias type.
1707 idx = _num_alias_types++;
1708 _alias_types[idx]->Init(idx, flat);
1709 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1710 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1711 if (flat->isa_instptr()) {
1712 if (flat->offset() == java_lang_Class::klass_offset()
1713 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1714 alias_type(idx)->set_rewritable(false);
1715 }
1716 ciField* field = nullptr;
1717 if (flat->isa_aryptr()) {
1718 #ifdef ASSERT
1719 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1720 // (T_BYTE has the weakest alignment and size restrictions...)
1721 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1722 #endif
1723 const Type* elemtype = flat->is_aryptr()->elem();
1724 if (flat->offset() == TypePtr::OffsetBot) {
1725 alias_type(idx)->set_element(elemtype);
1726 }
1727 int field_offset = flat->is_aryptr()->field_offset().get();
1728 if (flat->is_flat() &&
1729 field_offset != Type::OffsetBot) {
1730 ciInlineKlass* vk = elemtype->inline_klass();
1731 field_offset += vk->first_field_offset();
1732 field = vk->get_field_by_offset(field_offset, false);
1733 }
1734 }
1735 if (flat->isa_klassptr()) {
1736 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1737 alias_type(idx)->set_rewritable(false);
1738 if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1739 alias_type(idx)->set_rewritable(false);
1740 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1741 alias_type(idx)->set_rewritable(false);
1742 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1743 alias_type(idx)->set_rewritable(false);
1744 if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1745 alias_type(idx)->set_rewritable(false);
1746 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1747 alias_type(idx)->set_rewritable(false);
1748 }
1749 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1750 // but the base pointer type is not distinctive enough to identify
1751 // references into JavaThread.)
1752
1753 // Check for final fields.
1754 const TypeInstPtr* tinst = flat->isa_instptr();
1755 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1756 if (tinst->const_oop() != nullptr &&
1757 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1758 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1759 // static field
1760 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1761 field = k->get_field_by_offset(tinst->offset(), true);
1762 } else if (tinst->is_inlinetypeptr()) {
1763 // Inline type field
1764 ciInlineKlass* vk = tinst->inline_klass();
1765 field = vk->get_field_by_offset(tinst->offset(), false);
1766 } else {
1767 ciInstanceKlass *k = tinst->instance_klass();
1768 field = k->get_field_by_offset(tinst->offset(), false);
1769 }
1770 }
1771 assert(field == nullptr ||
1772 original_field == nullptr ||
1773 (field->holder() == original_field->holder() &&
1774 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1775 field->is_static() == original_field->is_static()), "wrong field?");
1776 // Set field() and is_rewritable() attributes.
1777 if (field != nullptr) {
1778 alias_type(idx)->set_field(field);
1779 if (flat->isa_aryptr()) {
1780 // Fields of flat arrays are rewritable although they are declared final
1781 assert(flat->is_flat(), "must be a flat array");
1782 alias_type(idx)->set_rewritable(true);
1783 }
1784 }
1785 }
1786
1787 // Fill the cache for next time.
1788 if (!uncached) {
1789 ace->_adr_type = adr_type;
1790 ace->_index = idx;
1791 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1792
1793 // Might as well try to fill the cache for the flattened version, too.
1794 AliasCacheEntry* face = probe_alias_cache(flat);
1795 if (face->_adr_type == nullptr) {
1796 face->_adr_type = flat;
1797 face->_index = idx;
1798 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1799 }
1800 }
1801
1802 return alias_type(idx);
1803 }
1804
1805
1806 Compile::AliasType* Compile::alias_type(ciField* field) {
1807 const TypeOopPtr* t;
1808 if (field->is_static())
1809 t = TypeInstPtr::make(field->holder()->java_mirror());
1810 else
1811 t = TypeOopPtr::make_from_klass_raw(field->holder());
1812 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1813 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1814 return atp;
1815 }
1816
1817
1818 //------------------------------have_alias_type--------------------------------
1819 bool Compile::have_alias_type(const TypePtr* adr_type) {
1897 C->set_post_loop_opts_phase(); // no more loop opts allowed
1898
1899 assert(!C->major_progress(), "not cleared");
1900
1901 if (_for_post_loop_igvn.length() > 0) {
1902 while (_for_post_loop_igvn.length() > 0) {
1903 Node* n = _for_post_loop_igvn.pop();
1904 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1905 igvn._worklist.push(n);
1906 }
1907 igvn.optimize();
1908 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1909
1910 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1911 if (C->major_progress()) {
1912 C->clear_major_progress(); // ensure that major progress is now clear
1913 }
1914 }
1915 }
1916
1917 void Compile::add_inline_type(Node* n) {
1918 assert(n->is_InlineType(), "unexpected node");
1919 _inline_type_nodes.push(n);
1920 }
1921
1922 void Compile::remove_inline_type(Node* n) {
1923 assert(n->is_InlineType(), "unexpected node");
1924 if (_inline_type_nodes.contains(n)) {
1925 _inline_type_nodes.remove(n);
1926 }
1927 }
1928
1929 // Does the return value keep otherwise useless inline type allocations alive?
1930 static bool return_val_keeps_allocations_alive(Node* ret_val) {
1931 ResourceMark rm;
1932 Unique_Node_List wq;
1933 wq.push(ret_val);
1934 bool some_allocations = false;
1935 for (uint i = 0; i < wq.size(); i++) {
1936 Node* n = wq.at(i);
1937 if (n->outcnt() > 1) {
1938 // Some other use for the allocation
1939 return false;
1940 } else if (n->is_InlineType()) {
1941 wq.push(n->in(1));
1942 } else if (n->is_Phi()) {
1943 for (uint j = 1; j < n->req(); j++) {
1944 wq.push(n->in(j));
1945 }
1946 } else if (n->is_CheckCastPP() &&
1947 n->in(1)->is_Proj() &&
1948 n->in(1)->in(0)->is_Allocate()) {
1949 some_allocations = true;
1950 } else if (n->is_CheckCastPP()) {
1951 wq.push(n->in(1));
1952 }
1953 }
1954 return some_allocations;
1955 }
1956
1957 void Compile::process_inline_types(PhaseIterGVN &igvn, bool remove) {
1958 // Make sure that the return value does not keep an otherwise unused allocation alive
1959 if (tf()->returns_inline_type_as_fields()) {
1960 Node* ret = nullptr;
1961 for (uint i = 1; i < root()->req(); i++) {
1962 Node* in = root()->in(i);
1963 if (in->Opcode() == Op_Return) {
1964 assert(ret == nullptr, "only one return");
1965 ret = in;
1966 }
1967 }
1968 if (ret != nullptr) {
1969 Node* ret_val = ret->in(TypeFunc::Parms);
1970 if (igvn.type(ret_val)->isa_oopptr() &&
1971 return_val_keeps_allocations_alive(ret_val)) {
1972 igvn.replace_input_of(ret, TypeFunc::Parms, InlineTypeNode::tagged_klass(igvn.type(ret_val)->inline_klass(), igvn));
1973 assert(ret_val->outcnt() == 0, "should be dead now");
1974 igvn.remove_dead_node(ret_val);
1975 }
1976 }
1977 }
1978 if (_inline_type_nodes.length() == 0) {
1979 return;
1980 }
1981 // Scalarize inline types in safepoint debug info.
1982 // Delay this until all inlining is over to avoid getting inconsistent debug info.
1983 set_scalarize_in_safepoints(true);
1984 for (int i = _inline_type_nodes.length()-1; i >= 0; i--) {
1985 _inline_type_nodes.at(i)->as_InlineType()->make_scalar_in_safepoints(&igvn);
1986 }
1987 if (remove) {
1988 // Remove inline type nodes by replacing them with their oop input
1989 while (_inline_type_nodes.length() > 0) {
1990 InlineTypeNode* vt = _inline_type_nodes.pop()->as_InlineType();
1991 if (vt->outcnt() == 0) {
1992 igvn.remove_dead_node(vt);
1993 continue;
1994 }
1995 for (DUIterator i = vt->outs(); vt->has_out(i); i++) {
1996 DEBUG_ONLY(bool must_be_buffered = false);
1997 Node* u = vt->out(i);
1998 // Check if any users are blackholes. If so, rewrite them to use either the
1999 // allocated buffer, or individual components, instead of the inline type node
2000 // that goes away.
2001 if (u->is_Blackhole()) {
2002 BlackholeNode* bh = u->as_Blackhole();
2003
2004 // Unlink the old input
2005 int idx = bh->find_edge(vt);
2006 assert(idx != -1, "The edge should be there");
2007 bh->del_req(idx);
2008 --i;
2009
2010 if (vt->is_allocated(&igvn)) {
2011 // Already has the allocated instance, blackhole that
2012 bh->add_req(vt->get_oop());
2013 } else {
2014 // Not allocated yet, blackhole the components
2015 for (uint c = 0; c < vt->field_count(); c++) {
2016 bh->add_req(vt->field_value(c));
2017 }
2018 }
2019
2020 // Node modified, record for IGVN
2021 igvn.record_for_igvn(bh);
2022 }
2023 #ifdef ASSERT
2024 // Verify that inline type is buffered when replacing by oop
2025 else if (u->is_InlineType()) {
2026 InlineTypeNode* vt2 = u->as_InlineType();
2027 for (uint i = 0; i < vt2->field_count(); ++i) {
2028 if (vt2->field_value(i) == vt && !vt2->field_is_flattened(i)) {
2029 // Use in non-flat field
2030 must_be_buffered = true;
2031 }
2032 }
2033 } else if (u->is_Phi()) {
2034 // TODO 8302217 Remove this once InlineTypeNodes are reliably pushed through
2035 } else if (u->Opcode() != Op_Return || !tf()->returns_inline_type_as_fields()) {
2036 must_be_buffered = true;
2037 }
2038 if (must_be_buffered && !vt->is_allocated(&igvn)) {
2039 vt->dump(0);
2040 u->dump(0);
2041 assert(false, "Should have been buffered");
2042 }
2043 #endif
2044 }
2045 igvn.replace_node(vt, vt->get_oop());
2046 }
2047 }
2048 igvn.optimize();
2049 }
2050
2051 void Compile::adjust_flattened_array_access_aliases(PhaseIterGVN& igvn) {
2052 if (!_has_flattened_accesses) {
2053 return;
2054 }
2055 // Initially, all flattened array accesses share the same slice to
2056 // keep dependencies with Object[] array accesses (that could be
2057 // to a flattened array) correct. We're done with parsing so we
2058 // now know all flattened array accesses in this compile
2059 // unit. Let's move flattened array accesses to their own slice,
2060 // one per element field. This should help memory access
2061 // optimizations.
2062 ResourceMark rm;
2063 Unique_Node_List wq;
2064 wq.push(root());
2065
2066 Node_List mergememnodes;
2067 Node_List memnodes;
2068
2069 // Alias index currently shared by all flattened memory accesses
2070 int index = get_alias_index(TypeAryPtr::INLINES);
2071
2072 // Find MergeMem nodes and flattened array accesses
2073 for (uint i = 0; i < wq.size(); i++) {
2074 Node* n = wq.at(i);
2075 if (n->is_Mem()) {
2076 const TypePtr* adr_type = nullptr;
2077 if (n->Opcode() == Op_StoreCM) {
2078 adr_type = get_adr_type(get_alias_index(n->in(MemNode::OopStore)->adr_type()));
2079 } else {
2080 adr_type = get_adr_type(get_alias_index(n->adr_type()));
2081 }
2082 if (adr_type == TypeAryPtr::INLINES) {
2083 memnodes.push(n);
2084 }
2085 } else if (n->is_MergeMem()) {
2086 MergeMemNode* mm = n->as_MergeMem();
2087 if (mm->memory_at(index) != mm->base_memory()) {
2088 mergememnodes.push(n);
2089 }
2090 }
2091 for (uint j = 0; j < n->req(); j++) {
2092 Node* m = n->in(j);
2093 if (m != nullptr) {
2094 wq.push(m);
2095 }
2096 }
2097 }
2098
2099 if (memnodes.size() > 0) {
2100 _flattened_accesses_share_alias = false;
2101
2102 // We are going to change the slice for the flattened array
2103 // accesses so we need to clear the cache entries that refer to
2104 // them.
2105 for (uint i = 0; i < AliasCacheSize; i++) {
2106 AliasCacheEntry* ace = &_alias_cache[i];
2107 if (ace->_adr_type != nullptr &&
2108 ace->_adr_type->is_flat()) {
2109 ace->_adr_type = nullptr;
2110 ace->_index = (i != 0) ? 0 : AliasIdxTop; // Make sure the nullptr adr_type resolves to AliasIdxTop
2111 }
2112 }
2113
2114 // Find what aliases we are going to add
2115 int start_alias = num_alias_types()-1;
2116 int stop_alias = 0;
2117
2118 for (uint i = 0; i < memnodes.size(); i++) {
2119 Node* m = memnodes.at(i);
2120 const TypePtr* adr_type = nullptr;
2121 if (m->Opcode() == Op_StoreCM) {
2122 adr_type = m->in(MemNode::OopStore)->adr_type();
2123 if (adr_type != TypeAryPtr::INLINES) {
2124 // store was optimized out and we lost track of the adr_type
2125 Node* clone = new StoreCMNode(m->in(MemNode::Control), m->in(MemNode::Memory), m->in(MemNode::Address),
2126 m->adr_type(), m->in(MemNode::ValueIn), m->in(MemNode::OopStore),
2127 get_alias_index(adr_type));
2128 igvn.register_new_node_with_optimizer(clone);
2129 igvn.replace_node(m, clone);
2130 }
2131 } else {
2132 adr_type = m->adr_type();
2133 #ifdef ASSERT
2134 m->as_Mem()->set_adr_type(adr_type);
2135 #endif
2136 }
2137 int idx = get_alias_index(adr_type);
2138 start_alias = MIN2(start_alias, idx);
2139 stop_alias = MAX2(stop_alias, idx);
2140 }
2141
2142 assert(stop_alias >= start_alias, "should have expanded aliases");
2143
2144 Node_Stack stack(0);
2145 #ifdef ASSERT
2146 VectorSet seen(Thread::current()->resource_area());
2147 #endif
2148 // Now let's fix the memory graph so each flattened array access
2149 // is moved to the right slice. Start from the MergeMem nodes.
2150 uint last = unique();
2151 for (uint i = 0; i < mergememnodes.size(); i++) {
2152 MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2153 Node* n = current->memory_at(index);
2154 MergeMemNode* mm = nullptr;
2155 do {
2156 // Follow memory edges through memory accesses, phis and
2157 // narrow membars and push nodes on the stack. Once we hit
2158 // bottom memory, we pop element off the stack one at a
2159 // time, in reverse order, and move them to the right slice
2160 // by changing their memory edges.
2161 if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() || n->adr_type() == TypeAryPtr::INLINES) {
2162 assert(!seen.test_set(n->_idx), "");
2163 // Uses (a load for instance) will need to be moved to the
2164 // right slice as well and will get a new memory state
2165 // that we don't know yet. The use could also be the
2166 // backedge of a loop. We put a place holder node between
2167 // the memory node and its uses. We replace that place
2168 // holder with the correct memory state once we know it,
2169 // i.e. when nodes are popped off the stack. Using the
2170 // place holder make the logic work in the presence of
2171 // loops.
2172 if (n->outcnt() > 1) {
2173 Node* place_holder = nullptr;
2174 assert(!n->has_out_with(Op_Node), "");
2175 for (DUIterator k = n->outs(); n->has_out(k); k++) {
2176 Node* u = n->out(k);
2177 if (u != current && u->_idx < last) {
2178 bool success = false;
2179 for (uint l = 0; l < u->req(); l++) {
2180 if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2181 continue;
2182 }
2183 Node* in = u->in(l);
2184 if (in == n) {
2185 if (place_holder == nullptr) {
2186 place_holder = new Node(1);
2187 place_holder->init_req(0, n);
2188 }
2189 igvn.replace_input_of(u, l, place_holder);
2190 success = true;
2191 }
2192 }
2193 if (success) {
2194 --k;
2195 }
2196 }
2197 }
2198 }
2199 if (n->is_Phi()) {
2200 stack.push(n, 1);
2201 n = n->in(1);
2202 } else if (n->is_Mem()) {
2203 stack.push(n, n->req());
2204 n = n->in(MemNode::Memory);
2205 } else {
2206 assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2207 stack.push(n, n->req());
2208 n = n->in(0)->in(TypeFunc::Memory);
2209 }
2210 } else {
2211 assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || (n->is_Proj() && n->in(0)->is_Initialize()), "");
2212 // Build a new MergeMem node to carry the new memory state
2213 // as we build it. IGVN should fold extraneous MergeMem
2214 // nodes.
2215 mm = MergeMemNode::make(n);
2216 igvn.register_new_node_with_optimizer(mm);
2217 while (stack.size() > 0) {
2218 Node* m = stack.node();
2219 uint idx = stack.index();
2220 if (m->is_Mem()) {
2221 // Move memory node to its new slice
2222 const TypePtr* adr_type = m->adr_type();
2223 int alias = get_alias_index(adr_type);
2224 Node* prev = mm->memory_at(alias);
2225 igvn.replace_input_of(m, MemNode::Memory, prev);
2226 mm->set_memory_at(alias, m);
2227 } else if (m->is_Phi()) {
2228 // We need as many new phis as there are new aliases
2229 igvn.replace_input_of(m, idx, mm);
2230 if (idx == m->req()-1) {
2231 Node* r = m->in(0);
2232 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2233 const TypePtr* adr_type = get_adr_type(j);
2234 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2235 continue;
2236 }
2237 Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2238 igvn.register_new_node_with_optimizer(phi);
2239 for (uint k = 1; k < m->req(); k++) {
2240 phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2241 }
2242 mm->set_memory_at(j, phi);
2243 }
2244 Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2245 igvn.register_new_node_with_optimizer(base_phi);
2246 for (uint k = 1; k < m->req(); k++) {
2247 base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2248 }
2249 mm->set_base_memory(base_phi);
2250 }
2251 } else {
2252 // This is a MemBarCPUOrder node from
2253 // Parse::array_load()/Parse::array_store(), in the
2254 // branch that handles flattened arrays hidden under
2255 // an Object[] array. We also need one new membar per
2256 // new alias to keep the unknown access that the
2257 // membars protect properly ordered with accesses to
2258 // known flattened array.
2259 assert(m->is_Proj(), "projection expected");
2260 Node* ctrl = m->in(0)->in(TypeFunc::Control);
2261 igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2262 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2263 const TypePtr* adr_type = get_adr_type(j);
2264 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2265 continue;
2266 }
2267 MemBarNode* mb = new MemBarCPUOrderNode(this, j, nullptr);
2268 igvn.register_new_node_with_optimizer(mb);
2269 Node* mem = mm->memory_at(j);
2270 mb->init_req(TypeFunc::Control, ctrl);
2271 mb->init_req(TypeFunc::Memory, mem);
2272 ctrl = new ProjNode(mb, TypeFunc::Control);
2273 igvn.register_new_node_with_optimizer(ctrl);
2274 mem = new ProjNode(mb, TypeFunc::Memory);
2275 igvn.register_new_node_with_optimizer(mem);
2276 mm->set_memory_at(j, mem);
2277 }
2278 igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2279 }
2280 if (idx < m->req()-1) {
2281 idx += 1;
2282 stack.set_index(idx);
2283 n = m->in(idx);
2284 break;
2285 }
2286 // Take care of place holder nodes
2287 if (m->has_out_with(Op_Node)) {
2288 Node* place_holder = m->find_out_with(Op_Node);
2289 if (place_holder != nullptr) {
2290 Node* mm_clone = mm->clone();
2291 igvn.register_new_node_with_optimizer(mm_clone);
2292 Node* hook = new Node(1);
2293 hook->init_req(0, mm);
2294 igvn.replace_node(place_holder, mm_clone);
2295 hook->destruct(&igvn);
2296 }
2297 assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2298 }
2299 stack.pop();
2300 }
2301 }
2302 } while(stack.size() > 0);
2303 // Fix the memory state at the MergeMem we started from
2304 igvn.rehash_node_delayed(current);
2305 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2306 const TypePtr* adr_type = get_adr_type(j);
2307 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2308 continue;
2309 }
2310 current->set_memory_at(j, mm);
2311 }
2312 current->set_memory_at(index, current->base_memory());
2313 }
2314 igvn.optimize();
2315 }
2316 print_method(PHASE_SPLIT_INLINES_ARRAY, 2);
2317 #ifdef ASSERT
2318 if (!_flattened_accesses_share_alias) {
2319 wq.clear();
2320 wq.push(root());
2321 for (uint i = 0; i < wq.size(); i++) {
2322 Node* n = wq.at(i);
2323 assert(n->adr_type() != TypeAryPtr::INLINES, "should have been removed from the graph");
2324 for (uint j = 0; j < n->req(); j++) {
2325 Node* m = n->in(j);
2326 if (m != nullptr) {
2327 wq.push(m);
2328 }
2329 }
2330 }
2331 }
2332 #endif
2333 }
2334
2335 void Compile::record_unstable_if_trap(UnstableIfTrap* trap) {
2336 if (OptimizeUnstableIf) {
2337 _unstable_if_traps.append(trap);
2338 }
2339 }
2340
2341 void Compile::remove_useless_unstable_if_traps(Unique_Node_List& useful) {
2342 for (int i = _unstable_if_traps.length() - 1; i >= 0; i--) {
2343 UnstableIfTrap* trap = _unstable_if_traps.at(i);
2344 Node* n = trap->uncommon_trap();
2345 if (!useful.member(n)) {
2346 _unstable_if_traps.delete_at(i); // replaces i-th with last element which is known to be useful (already processed)
2347 }
2348 }
2349 }
2350
2351 // Remove the unstable if trap associated with 'unc' from candidates. It is either dead
2352 // or fold-compares case. Return true if succeed or not found.
2353 //
2354 // In rare cases, the found trap has been processed. It is too late to delete it. Return
2604 assert(has_stringbuilder(), "inconsistent");
2605 for_igvn()->clear();
2606 initial_gvn()->replace_with(&igvn);
2607
2608 inline_string_calls(false);
2609
2610 if (failing()) return;
2611
2612 inline_incrementally_cleanup(igvn);
2613 }
2614
2615 set_inlining_incrementally(false);
2616 }
2617
2618 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2619 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2620 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2621 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2622 // as if "inlining_incrementally() == true" were set.
2623 assert(inlining_incrementally() == false, "not allowed");
2624 #ifdef ASSERT
2625 Unique_Node_List* modified_nodes = _modified_nodes;
2626 _modified_nodes = nullptr;
2627 #endif
2628 assert(_late_inlines.length() > 0, "sanity");
2629
2630 while (_late_inlines.length() > 0) {
2631 for_igvn()->clear();
2632 initial_gvn()->replace_with(&igvn);
2633
2634 while (inline_incrementally_one()) {
2635 assert(!failing(), "inconsistent");
2636 }
2637 if (failing()) return;
2638
2639 inline_incrementally_cleanup(igvn);
2640 }
2641 DEBUG_ONLY( _modified_nodes = modified_nodes; )
2642 }
2643
2644 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2645 if (_loop_opts_cnt > 0) {
2646 while (major_progress() && (_loop_opts_cnt > 0)) {
2647 TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2648 PhaseIdealLoop::optimize(igvn, mode);
2649 _loop_opts_cnt--;
2650 if (failing()) return false;
2651 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2652 }
2653 }
2654 return true;
2655 }
2656
2657 // Remove edges from "root" to each SafePoint at a backward branch.
2658 // They were inserted during parsing (see add_safepoint()) to make
2659 // infinite loops without calls or exceptions visible to root, i.e.,
2660 // useful.
2661 void Compile::remove_root_to_sfpts_edges(PhaseIterGVN& igvn) {
2765 Compile::TracePhase tp("", &timers[_t_renumberLive]);
2766 initial_gvn()->replace_with(&igvn);
2767 Unique_Node_List* old_worklist = for_igvn();
2768 old_worklist->clear();
2769 Unique_Node_List new_worklist(C->comp_arena());
2770 {
2771 ResourceMark rm;
2772 PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2773 }
2774 Unique_Node_List* save_for_igvn = for_igvn();
2775 set_for_igvn(&new_worklist);
2776 igvn = PhaseIterGVN(initial_gvn());
2777 igvn.optimize();
2778 set_for_igvn(old_worklist); // new_worklist is dead beyond this point
2779 }
2780
2781 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2782 // safepoints
2783 remove_root_to_sfpts_edges(igvn);
2784
2785 // Process inline type nodes now that all inlining is over
2786 process_inline_types(igvn);
2787
2788 adjust_flattened_array_access_aliases(igvn);
2789
2790 // Perform escape analysis
2791 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2792 if (has_loops()) {
2793 // Cleanup graph (remove dead nodes).
2794 TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2795 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2796 if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2797 if (failing()) return;
2798 }
2799 bool progress;
2800 do {
2801 ConnectionGraph::do_analysis(this, &igvn);
2802
2803 if (failing()) return;
2804
2805 int mcount = macro_count(); // Record number of allocations and locks before IGVN
2806
2807 // Optimize out fields loads from scalar replaceable allocations.
2808 igvn.optimize();
2809 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2883 print_method(PHASE_ITER_GVN2, 2);
2884
2885 if (failing()) return;
2886
2887 // Loop transforms on the ideal graph. Range Check Elimination,
2888 // peeling, unrolling, etc.
2889 if (!optimize_loops(igvn, LoopOptsDefault)) {
2890 return;
2891 }
2892
2893 if (failing()) return;
2894
2895 C->clear_major_progress(); // ensure that major progress is now clear
2896
2897 process_for_post_loop_opts_igvn(igvn);
2898
2899 #ifdef ASSERT
2900 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
2901 #endif
2902
2903 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
2904
2905 if (_late_inlines.length() > 0) {
2906 // More opportunities to optimize virtual and MH calls.
2907 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
2908 process_late_inline_calls_no_inline(igvn);
2909 }
2910
2911 {
2912 TracePhase tp("macroExpand", &timers[_t_macroExpand]);
2913 PhaseMacroExpand mex(igvn);
2914 if (mex.expand_macro_nodes()) {
2915 assert(failing(), "must bail out w/ explicit message");
2916 return;
2917 }
2918 print_method(PHASE_MACRO_EXPANSION, 2);
2919 }
2920
2921 // Process inline type nodes again and remove them. From here
2922 // on we don't need to keep track of field values anymore.
2923 process_inline_types(igvn, /* remove= */ true);
2924
2925 {
2926 TracePhase tp("barrierExpand", &timers[_t_barrierExpand]);
2927 if (bs->expand_barriers(this, igvn)) {
2928 assert(failing(), "must bail out w/ explicit message");
2929 return;
2930 }
2931 print_method(PHASE_BARRIER_EXPANSION, 2);
2932 }
2933
2934 if (C->max_vector_size() > 0) {
2935 C->optimize_logic_cones(igvn);
2936 igvn.optimize();
2937 }
2938
2939 DEBUG_ONLY( _modified_nodes = nullptr; )
2940 DEBUG_ONLY( _late_inlines.clear(); )
2941
2942 assert(igvn._worklist.size() == 0, "not empty");
2943 } // (End scope of igvn; run destructor if necessary for asserts.)
2944
2945 check_no_dead_use();
2946
2947 process_print_inlining();
2948
2949 // A method with only infinite loops has no edges entering loops from root
2950 {
2951 TracePhase tp("graphReshape", &timers[_t_graphReshaping]);
2952 if (final_graph_reshaping()) {
2953 assert(failing(), "must bail out w/ explicit message");
2954 return;
2955 }
2956 }
2957
2958 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2959 DEBUG_ONLY(set_phase_optimize_finished();)
2960 }
2961
2962 #ifdef ASSERT
3542 // Accumulate any precedence edges
3543 if (mem->in(i) != nullptr) {
3544 n->add_prec(mem->in(i));
3545 }
3546 }
3547 // Everything above this point has been processed.
3548 done = true;
3549 }
3550 // Eliminate the previous StoreCM
3551 prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
3552 assert(mem->outcnt() == 0, "should be dead");
3553 mem->disconnect_inputs(this);
3554 } else {
3555 prev = mem;
3556 }
3557 mem = prev->in(MemNode::Memory);
3558 }
3559 }
3560 }
3561
3562
3563 //------------------------------final_graph_reshaping_impl----------------------
3564 // Implement items 1-5 from final_graph_reshaping below.
3565 void Compile::final_graph_reshaping_impl(Node *n, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3566
3567 if ( n->outcnt() == 0 ) return; // dead node
3568 uint nop = n->Opcode();
3569
3570 // Check for 2-input instruction with "last use" on right input.
3571 // Swap to left input. Implements item (2).
3572 if( n->req() == 3 && // two-input instruction
3573 n->in(1)->outcnt() > 1 && // left use is NOT a last use
3574 (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
3575 n->in(2)->outcnt() == 1 &&// right use IS a last use
3576 !n->in(2)->is_Con() ) { // right use is not a constant
3577 // Check for commutative opcode
3578 switch( nop ) {
3579 case Op_AddI: case Op_AddF: case Op_AddD: case Op_AddL:
3580 case Op_MaxI: case Op_MaxL: case Op_MaxF: case Op_MaxD:
3581 case Op_MinI: case Op_MinL: case Op_MinF: case Op_MinD:
3582 case Op_MulI: case Op_MulF: case Op_MulD: case Op_MulL:
3695 if (n->outcnt() > 1 &&
3696 !n->is_Proj() &&
3697 nop != Op_CreateEx &&
3698 nop != Op_CheckCastPP &&
3699 nop != Op_DecodeN &&
3700 nop != Op_DecodeNKlass &&
3701 !n->is_Mem() &&
3702 !n->is_Phi()) {
3703 Node *x = n->clone();
3704 call->set_req(TypeFunc::Parms, x);
3705 }
3706 }
3707 break;
3708 }
3709
3710 case Op_StoreCM:
3711 {
3712 // Convert OopStore dependence into precedence edge
3713 Node* prec = n->in(MemNode::OopStore);
3714 n->del_req(MemNode::OopStore);
3715 if (prec->is_MergeMem()) {
3716 MergeMemNode* mm = prec->as_MergeMem();
3717 Node* base = mm->base_memory();
3718 for (int i = AliasIdxRaw + 1; i < num_alias_types(); i++) {
3719 const TypePtr* adr_type = get_adr_type(i);
3720 if (adr_type->is_flat()) {
3721 Node* m = mm->memory_at(i);
3722 n->add_prec(m);
3723 }
3724 }
3725 if (mm->outcnt() == 0) {
3726 mm->disconnect_inputs(this);
3727 }
3728 } else {
3729 n->add_prec(prec);
3730 }
3731 eliminate_redundant_card_marks(n);
3732 }
3733
3734 // fall through
3735
3736 case Op_StoreB:
3737 case Op_StoreC:
3738 case Op_StoreI:
3739 case Op_StoreL:
3740 case Op_CompareAndSwapB:
3741 case Op_CompareAndSwapS:
3742 case Op_CompareAndSwapI:
3743 case Op_CompareAndSwapL:
3744 case Op_CompareAndSwapP:
3745 case Op_CompareAndSwapN:
3746 case Op_WeakCompareAndSwapB:
3747 case Op_WeakCompareAndSwapS:
3748 case Op_WeakCompareAndSwapI:
3749 case Op_WeakCompareAndSwapL:
3750 case Op_WeakCompareAndSwapP:
4306 // Replace all nodes with identical edges as m with m
4307 k->subsume_by(m, this);
4308 }
4309 }
4310 }
4311 break;
4312 }
4313 case Op_CmpUL: {
4314 if (!Matcher::has_match_rule(Op_CmpUL)) {
4315 // No support for unsigned long comparisons
4316 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
4317 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
4318 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
4319 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
4320 Node* andl = new AndLNode(orl, remove_sign_mask);
4321 Node* cmp = new CmpLNode(andl, n->in(2));
4322 n->subsume_by(cmp, this);
4323 }
4324 break;
4325 }
4326 #ifdef ASSERT
4327 case Op_InlineType: {
4328 n->dump(-1);
4329 assert(false, "inline type node was not removed");
4330 break;
4331 }
4332 #endif
4333 default:
4334 assert(!n->is_Call(), "");
4335 assert(!n->is_Mem(), "");
4336 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4337 break;
4338 }
4339 }
4340
4341 //------------------------------final_graph_reshaping_walk---------------------
4342 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4343 // requires that the walk visits a node's inputs before visiting the node.
4344 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
4345 Unique_Node_List sfpt;
4346
4347 frc._visited.set(root->_idx); // first, mark node as visited
4348 uint cnt = root->req();
4349 Node *n = root;
4350 uint i = 0;
4351 while (true) {
4352 if (i < cnt) {
4694 }
4695 }
4696
4697 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4698 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4699 }
4700
4701 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4702 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4703 }
4704
4705 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4706 if (holder->is_initialized()) {
4707 return false;
4708 }
4709 if (holder->is_being_initialized()) {
4710 if (accessing_method->holder() == holder) {
4711 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4712 // <init>, or a static method. In all those cases, there was an initialization
4713 // barrier on the holder klass passed.
4714 if (accessing_method->is_class_initializer() ||
4715 accessing_method->is_object_constructor() ||
4716 accessing_method->is_static()) {
4717 return false;
4718 }
4719 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4720 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4721 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4722 // child class can become fully initialized while its parent class is still being initialized.
4723 if (accessing_method->is_class_initializer()) {
4724 return false;
4725 }
4726 }
4727 ciMethod* root = method(); // the root method of compilation
4728 if (root != accessing_method) {
4729 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4730 }
4731 }
4732 return true;
4733 }
4734
4735 #ifndef PRODUCT
4736 //------------------------------verify_bidirectional_edges---------------------
4737 // For each input edge to a node (ie - for each Use-Def edge), verify that
4738 // there is a corresponding Def-Use edge.
4739 void Compile::verify_bidirectional_edges(Unique_Node_List &visited) {
4740 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4741 uint stack_size = live_nodes() >> 4;
4742 Node_List nstack(MAX2(stack_size, (uint)OptoNodeListSize));
4743 nstack.push(_root);
4759 if (in != nullptr && !in->is_top()) {
4760 // Count instances of `next`
4761 int cnt = 0;
4762 for (uint idx = 0; idx < in->_outcnt; idx++) {
4763 if (in->_out[idx] == n) {
4764 cnt++;
4765 }
4766 }
4767 assert(cnt > 0, "Failed to find Def-Use edge.");
4768 // Check for duplicate edges
4769 // walk the input array downcounting the input edges to n
4770 for (uint j = 0; j < length; j++) {
4771 if (n->in(j) == in) {
4772 cnt--;
4773 }
4774 }
4775 assert(cnt == 0, "Mismatched edge count.");
4776 } else if (in == nullptr) {
4777 assert(i == 0 || i >= n->req() ||
4778 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4779 (n->is_Allocate() && i >= AllocateNode::InlineType) ||
4780 (n->is_Unlock() && i == (n->req() - 1)) ||
4781 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4782 "only region, phi, arraycopy, allocate, unlock or membar nodes have null data edges");
4783 } else {
4784 assert(in->is_top(), "sanity");
4785 // Nothing to check.
4786 }
4787 }
4788 }
4789 }
4790
4791 //------------------------------verify_graph_edges---------------------------
4792 // Walk the Graph and verify that there is a one-to-one correspondence
4793 // between Use-Def edges and Def-Use edges in the graph.
4794 void Compile::verify_graph_edges(bool no_dead_code) {
4795 if (VerifyGraphEdges) {
4796 Unique_Node_List visited;
4797
4798 // Call graph walk to check edges
4799 verify_bidirectional_edges(visited);
4800 if (no_dead_code) {
4801 // Now make sure that no visited node is used by an unvisited node.
4802 bool dead_nodes = false;
4896 // (1) subklass is already limited to a subtype of superklass => always ok
4897 // (2) subklass does not overlap with superklass => always fail
4898 // (3) superklass has NO subtypes and we can check with a simple compare.
4899 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4900 if (skip) {
4901 return SSC_full_test; // Let caller generate the general case.
4902 }
4903
4904 if (subk->is_java_subtype_of(superk)) {
4905 return SSC_always_true; // (0) and (1) this test cannot fail
4906 }
4907
4908 if (!subk->maybe_java_subtype_of(superk)) {
4909 return SSC_always_false; // (2) true path dead; no dynamic test needed
4910 }
4911
4912 const Type* superelem = superk;
4913 if (superk->isa_aryklassptr()) {
4914 int ignored;
4915 superelem = superk->is_aryklassptr()->base_element_type(ignored);
4916
4917 // Do not fold the subtype check to an array klass pointer comparison for [V? arrays.
4918 // [QMyValue is a subtype of [LMyValue but the klass for [QMyValue is not equal to
4919 // the klass for [LMyValue. Perform a full test.
4920 if (!superk->is_aryklassptr()->is_null_free() && superk->is_aryklassptr()->elem()->isa_instklassptr() &&
4921 superk->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->is_inlinetype()) {
4922 return SSC_full_test;
4923 }
4924 }
4925
4926 if (superelem->isa_instklassptr()) {
4927 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4928 if (!ik->has_subklass()) {
4929 if (!ik->is_final()) {
4930 // Add a dependency if there is a chance of a later subclass.
4931 dependencies()->assert_leaf_type(ik);
4932 }
4933 if (!superk->maybe_java_subtype_of(subk)) {
4934 return SSC_always_false;
4935 }
4936 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4937 }
4938 } else {
4939 // A primitive array type has no subtypes.
4940 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4941 }
4942
4943 return SSC_full_test;
5455 const Type* t = igvn.type_or_null(n);
5456 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
5457 if (n->is_Type()) {
5458 t = n->as_Type()->type();
5459 assert(t == t->remove_speculative(), "no more speculative types");
5460 }
5461 // Iterate over outs - endless loops is unreachable from below
5462 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
5463 Node *m = n->fast_out(i);
5464 if (not_a_node(m)) {
5465 continue;
5466 }
5467 worklist.push(m);
5468 }
5469 }
5470 igvn.check_no_speculative_types();
5471 #endif
5472 }
5473 }
5474
5475 Node* Compile::optimize_acmp(PhaseGVN* phase, Node* a, Node* b) {
5476 const TypeInstPtr* ta = phase->type(a)->isa_instptr();
5477 const TypeInstPtr* tb = phase->type(b)->isa_instptr();
5478 if (!EnableValhalla || ta == nullptr || tb == nullptr ||
5479 ta->is_zero_type() || tb->is_zero_type() ||
5480 !ta->can_be_inline_type() || !tb->can_be_inline_type()) {
5481 // Use old acmp if one operand is null or not an inline type
5482 return new CmpPNode(a, b);
5483 } else if (ta->is_inlinetypeptr() || tb->is_inlinetypeptr()) {
5484 // We know that one operand is an inline type. Therefore,
5485 // new acmp will only return true if both operands are nullptr.
5486 // Check if both operands are null by or'ing the oops.
5487 a = phase->transform(new CastP2XNode(nullptr, a));
5488 b = phase->transform(new CastP2XNode(nullptr, b));
5489 a = phase->transform(new OrXNode(a, b));
5490 return new CmpXNode(a, phase->MakeConX(0));
5491 }
5492 // Use new acmp
5493 return nullptr;
5494 }
5495
5496 // Auxiliary methods to support randomized stressing/fuzzing.
5497
5498 int Compile::random() {
5499 _stress_seed = os::next_random(_stress_seed);
5500 return static_cast<int>(_stress_seed);
5501 }
5502
5503 // This method can be called the arbitrary number of times, with current count
5504 // as the argument. The logic allows selecting a single candidate from the
5505 // running list of candidates as follows:
5506 // int count = 0;
5507 // Cand* selected = null;
5508 // while(cand = cand->next()) {
5509 // if (randomized_select(++count)) {
5510 // selected = cand;
5511 // }
5512 // }
5513 //
5514 // Including count equalizes the chances any candidate is "selected".
5515 // This is useful when we don't have the complete list of candidates to choose
|