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