42 #include "gc/shared/c2/barrierSetC2.hpp"
43 #include "jfr/jfrEvents.hpp"
44 #include "jvm_io.h"
45 #include "memory/allocation.hpp"
46 #include "memory/arena.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "opto/addnode.hpp"
49 #include "opto/block.hpp"
50 #include "opto/c2compiler.hpp"
51 #include "opto/callGenerator.hpp"
52 #include "opto/callnode.hpp"
53 #include "opto/castnode.hpp"
54 #include "opto/cfgnode.hpp"
55 #include "opto/chaitin.hpp"
56 #include "opto/compile.hpp"
57 #include "opto/connode.hpp"
58 #include "opto/convertnode.hpp"
59 #include "opto/divnode.hpp"
60 #include "opto/escape.hpp"
61 #include "opto/idealGraphPrinter.hpp"
62 #include "opto/locknode.hpp"
63 #include "opto/loopnode.hpp"
64 #include "opto/machnode.hpp"
65 #include "opto/macro.hpp"
66 #include "opto/matcher.hpp"
67 #include "opto/mathexactnode.hpp"
68 #include "opto/memnode.hpp"
69 #include "opto/mulnode.hpp"
70 #include "opto/narrowptrnode.hpp"
71 #include "opto/node.hpp"
72 #include "opto/opaquenode.hpp"
73 #include "opto/opcodes.hpp"
74 #include "opto/output.hpp"
75 #include "opto/parse.hpp"
76 #include "opto/phaseX.hpp"
77 #include "opto/rootnode.hpp"
78 #include "opto/runtime.hpp"
79 #include "opto/stringopts.hpp"
80 #include "opto/type.hpp"
81 #include "opto/vector.hpp"
82 #include "opto/vectornode.hpp"
83 #include "runtime/globals_extension.hpp"
84 #include "runtime/sharedRuntime.hpp"
85 #include "runtime/signature.hpp"
86 #include "runtime/stubRoutines.hpp"
87 #include "runtime/timer.hpp"
88 #include "utilities/align.hpp"
388 // as dead to be conservative about the dead node count at any
389 // given time.
390 if (!dead->is_Con()) {
391 record_dead_node(dead->_idx);
392 }
393 if (dead->is_macro()) {
394 remove_macro_node(dead);
395 }
396 if (dead->is_expensive()) {
397 remove_expensive_node(dead);
398 }
399 if (dead->is_OpaqueTemplateAssertionPredicate()) {
400 remove_template_assertion_predicate_opaque(dead->as_OpaqueTemplateAssertionPredicate());
401 }
402 if (dead->is_ParsePredicate()) {
403 remove_parse_predicate(dead->as_ParsePredicate());
404 }
405 if (dead->for_post_loop_opts_igvn()) {
406 remove_from_post_loop_opts_igvn(dead);
407 }
408 if (dead->for_merge_stores_igvn()) {
409 remove_from_merge_stores_igvn(dead);
410 }
411 if (dead->is_Call()) {
412 remove_useless_late_inlines( &_late_inlines, dead);
413 remove_useless_late_inlines( &_string_late_inlines, dead);
414 remove_useless_late_inlines( &_boxing_late_inlines, dead);
415 remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
416
417 if (dead->is_CallStaticJava()) {
418 remove_unstable_if_trap(dead->as_CallStaticJava(), false);
419 }
420 }
421 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
422 bs->unregister_potential_barrier_node(dead);
423 }
424
425 // Disconnect all useless nodes by disconnecting those at the boundary.
426 void Compile::disconnect_useless_nodes(Unique_Node_List& useful, Unique_Node_List& worklist, const Unique_Node_List* root_and_safepoints) {
427 uint next = 0;
435 // Use raw traversal of out edges since this code removes out edges
436 int max = n->outcnt();
437 for (int j = 0; j < max; ++j) {
438 Node* child = n->raw_out(j);
439 if (!useful.member(child)) {
440 assert(!child->is_top() || child != top(),
441 "If top is cached in Compile object it is in useful list");
442 // Only need to remove this out-edge to the useless node
443 n->raw_del_out(j);
444 --j;
445 --max;
446 if (child->is_data_proj_of_pure_function(n)) {
447 worklist.push(n);
448 }
449 }
450 }
451 if (n->outcnt() == 1 && n->has_special_unique_user()) {
452 assert(useful.member(n->unique_out()), "do not push a useless node");
453 worklist.push(n->unique_out());
454 }
455 }
456
457 remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes
458 remove_useless_nodes(_parse_predicates, useful); // remove useless Parse Predicate nodes
459 // Remove useless Template Assertion Predicate opaque nodes
460 remove_useless_nodes(_template_assertion_predicate_opaques, useful);
461 remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes
462 remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
463 remove_useless_nodes(_for_merge_stores_igvn, useful); // remove useless node recorded for merge stores IGVN pass
464 remove_useless_unstable_if_traps(useful); // remove useless unstable_if traps
465 remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes
466 #ifdef ASSERT
467 if (_modified_nodes != nullptr) {
468 _modified_nodes->remove_useless_nodes(useful.member_set());
469 }
470 #endif
471
472 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
473 bs->eliminate_useless_gc_barriers(useful, this);
474 // clean up the late inline lists
475 remove_useless_late_inlines( &_late_inlines, useful);
476 remove_useless_late_inlines( &_string_late_inlines, useful);
477 remove_useless_late_inlines( &_boxing_late_inlines, useful);
478 remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
479 DEBUG_ONLY(verify_graph_edges(true /*check for no_dead_code*/, root_and_safepoints);)
480 }
481
482 // ============================================================================
629 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
630 Options options, DirectiveSet* directive)
631 : Phase(Compiler),
632 _compile_id(ci_env->compile_id()),
633 _options(options),
634 _method(target),
635 _entry_bci(osr_bci),
636 _ilt(nullptr),
637 _stub_function(nullptr),
638 _stub_name(nullptr),
639 _stub_id(StubId::NO_STUBID),
640 _stub_entry_point(nullptr),
641 _max_node_limit(MaxNodeLimit),
642 _post_loop_opts_phase(false),
643 _merge_stores_phase(false),
644 _allow_macro_nodes(true),
645 _inlining_progress(false),
646 _inlining_incrementally(false),
647 _do_cleanup(false),
648 _has_reserved_stack_access(target->has_reserved_stack_access()),
649 #ifndef PRODUCT
650 _igv_idx(0),
651 _trace_opto_output(directive->TraceOptoOutputOption),
652 #endif
653 _has_method_handle_invokes(false),
654 _clinit_barrier_on_entry(false),
655 _stress_seed(0),
656 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
657 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
658 _env(ci_env),
659 _directive(directive),
660 _log(ci_env->log()),
661 _first_failure_details(nullptr),
662 _intrinsics(comp_arena(), 0, 0, nullptr),
663 _macro_nodes(comp_arena(), 8, 0, nullptr),
664 _parse_predicates(comp_arena(), 8, 0, nullptr),
665 _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
666 _expensive_nodes(comp_arena(), 8, 0, nullptr),
667 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
668 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
669 _unstable_if_traps(comp_arena(), 8, 0, nullptr),
670 _coarsened_locks(comp_arena(), 8, 0, nullptr),
671 _congraph(nullptr),
672 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
673 _unique(0),
674 _dead_node_count(0),
675 _dead_node_list(comp_arena()),
676 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
677 _node_arena_two(mtCompiler, Arena::Tag::tag_node),
678 _node_arena(&_node_arena_one),
679 _mach_constant_base_node(nullptr),
680 _Compile_types(mtCompiler, Arena::Tag::tag_type),
681 _initial_gvn(nullptr),
682 _igvn_worklist(nullptr),
683 _types(nullptr),
684 _node_hash(nullptr),
685 _late_inlines(comp_arena(), 2, 0, nullptr),
686 _string_late_inlines(comp_arena(), 2, 0, nullptr),
687 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
754 #define MINIMUM_NODE_HASH 1023
755
756 // GVN that will be run immediately on new nodes
757 uint estimated_size = method()->code_size()*4+64;
758 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
759 _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
760 _types = new (comp_arena()) Type_Array(comp_arena());
761 _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
762 PhaseGVN gvn;
763 set_initial_gvn(&gvn);
764
765 { // Scope for timing the parser
766 TracePhase tp(_t_parser);
767
768 // Put top into the hash table ASAP.
769 initial_gvn()->transform(top());
770
771 // Set up tf(), start(), and find a CallGenerator.
772 CallGenerator* cg = nullptr;
773 if (is_osr_compilation()) {
774 const TypeTuple *domain = StartOSRNode::osr_domain();
775 const TypeTuple *range = TypeTuple::make_range(method()->signature());
776 init_tf(TypeFunc::make(domain, range));
777 StartNode* s = new StartOSRNode(root(), domain);
778 initial_gvn()->set_type_bottom(s);
779 verify_start(s);
780 cg = CallGenerator::for_osr(method(), entry_bci());
781 } else {
782 // Normal case.
783 init_tf(TypeFunc::make(method()));
784 StartNode* s = new StartNode(root(), tf()->domain());
785 initial_gvn()->set_type_bottom(s);
786 verify_start(s);
787 float past_uses = method()->interpreter_invocation_count();
788 float expected_uses = past_uses;
789 cg = CallGenerator::for_inline(method(), expected_uses);
790 }
791 if (failing()) return;
792 if (cg == nullptr) {
793 const char* reason = InlineTree::check_can_parse(method());
794 assert(reason != nullptr, "expect reason for parse failure");
795 stringStream ss;
796 ss.print("cannot parse method: %s", reason);
797 record_method_not_compilable(ss.as_string());
798 return;
799 }
800
801 gvn.set_type(root(), root()->bottom_type());
802
803 JVMState* jvms = build_start_state(start(), tf());
804 if ((jvms = cg->generate(jvms)) == nullptr) {
865 print_ideal_ir("print_ideal");
866 }
867 #endif
868
869 #ifdef ASSERT
870 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
871 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
872 #endif
873
874 // Dump compilation data to replay it.
875 if (directive->DumpReplayOption) {
876 env()->dump_replay_data(_compile_id);
877 }
878 if (directive->DumpInlineOption && (ilt() != nullptr)) {
879 env()->dump_inline_data(_compile_id);
880 }
881
882 // Now that we know the size of all the monitors we can add a fixed slot
883 // for the original deopt pc.
884 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
885 set_fixed_slots(next_slot);
886
887 // Compute when to use implicit null checks. Used by matching trap based
888 // nodes and NullCheck optimization.
889 set_allowed_deopt_reasons();
890
891 // Now generate code
892 Code_Gen();
893 }
894
895 //------------------------------Compile----------------------------------------
896 // Compile a runtime stub
897 Compile::Compile(ciEnv* ci_env,
898 TypeFunc_generator generator,
899 address stub_function,
900 const char* stub_name,
901 StubId stub_id,
902 int is_fancy_jump,
903 bool pass_tls,
904 bool return_pc,
905 DirectiveSet* directive)
906 : Phase(Compiler),
907 _compile_id(0),
908 _options(Options::for_runtime_stub()),
909 _method(nullptr),
910 _entry_bci(InvocationEntryBci),
911 _stub_function(stub_function),
912 _stub_name(stub_name),
913 _stub_id(stub_id),
914 _stub_entry_point(nullptr),
915 _max_node_limit(MaxNodeLimit),
916 _post_loop_opts_phase(false),
917 _merge_stores_phase(false),
918 _allow_macro_nodes(true),
919 _inlining_progress(false),
920 _inlining_incrementally(false),
921 _has_reserved_stack_access(false),
922 #ifndef PRODUCT
923 _igv_idx(0),
924 _trace_opto_output(directive->TraceOptoOutputOption),
925 #endif
926 _has_method_handle_invokes(false),
927 _clinit_barrier_on_entry(false),
928 _stress_seed(0),
929 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
930 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
931 _env(ci_env),
932 _directive(directive),
933 _log(ci_env->log()),
934 _first_failure_details(nullptr),
935 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
936 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
937 _congraph(nullptr),
938 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
939 _unique(0),
940 _dead_node_count(0),
941 _dead_node_list(comp_arena()),
1055 _fixed_slots = 0;
1056 set_has_split_ifs(false);
1057 set_has_loops(false); // first approximation
1058 set_has_stringbuilder(false);
1059 set_has_boxed_value(false);
1060 _trap_can_recompile = false; // no traps emitted yet
1061 _major_progress = true; // start out assuming good things will happen
1062 set_has_unsafe_access(false);
1063 set_max_vector_size(0);
1064 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1065 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1066 set_decompile_count(0);
1067
1068 #ifndef PRODUCT
1069 _phase_counter = 0;
1070 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1071 #endif
1072
1073 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1074 _loop_opts_cnt = LoopOptsCount;
1075 set_do_inlining(Inline);
1076 set_max_inline_size(MaxInlineSize);
1077 set_freq_inline_size(FreqInlineSize);
1078 set_do_scheduling(OptoScheduling);
1079
1080 set_do_vector_loop(false);
1081 set_has_monitors(false);
1082 set_has_scoped_access(false);
1083
1084 if (AllowVectorizeOnDemand) {
1085 if (has_method() && _directive->VectorizeOption) {
1086 set_do_vector_loop(true);
1087 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());})
1088 } else if (has_method() && method()->name() != nullptr &&
1089 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1090 set_do_vector_loop(true);
1091 }
1092 }
1093 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1094 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());})
1339
1340 // Known instance (scalarizable allocation) alias only with itself.
1341 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1342 tj->is_oopptr()->is_known_instance();
1343
1344 // Process weird unsafe references.
1345 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1346 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1347 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1348 tj = TypeOopPtr::BOTTOM;
1349 ptr = tj->ptr();
1350 offset = tj->offset();
1351 }
1352
1353 // Array pointers need some flattening
1354 const TypeAryPtr* ta = tj->isa_aryptr();
1355 if (ta && ta->is_stable()) {
1356 // Erase stability property for alias analysis.
1357 tj = ta = ta->cast_to_stable(false);
1358 }
1359 if( ta && is_known_inst ) {
1360 if ( offset != Type::OffsetBot &&
1361 offset > arrayOopDesc::length_offset_in_bytes() ) {
1362 offset = Type::OffsetBot; // Flatten constant access into array body only
1363 tj = ta = ta->
1364 remove_speculative()->
1365 cast_to_ptr_type(ptr)->
1366 with_offset(offset);
1367 }
1368 } else if (ta) {
1369 // For arrays indexed by constant indices, we flatten the alias
1370 // space to include all of the array body. Only the header, klass
1371 // and array length can be accessed un-aliased.
1372 if( offset != Type::OffsetBot ) {
1373 if( ta->const_oop() ) { // MethodData* or Method*
1374 offset = Type::OffsetBot; // Flatten constant access into array body
1375 tj = ta = ta->
1376 remove_speculative()->
1377 cast_to_ptr_type(ptr)->
1378 cast_to_exactness(false)->
1379 with_offset(offset);
1380 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1381 // range is OK as-is.
1382 tj = ta = TypeAryPtr::RANGE;
1383 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1384 tj = TypeInstPtr::KLASS; // all klass loads look alike
1385 ta = TypeAryPtr::RANGE; // generic ignored junk
1386 ptr = TypePtr::BotPTR;
1387 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1388 tj = TypeInstPtr::MARK;
1389 ta = TypeAryPtr::RANGE; // generic ignored junk
1390 ptr = TypePtr::BotPTR;
1391 } else { // Random constant offset into array body
1392 offset = Type::OffsetBot; // Flatten constant access into array body
1393 tj = ta = ta->
1394 remove_speculative()->
1395 cast_to_ptr_type(ptr)->
1396 cast_to_exactness(false)->
1397 with_offset(offset);
1398 }
1399 }
1400 // Arrays of fixed size alias with arrays of unknown size.
1401 if (ta->size() != TypeInt::POS) {
1402 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1403 tj = ta = ta->
1404 remove_speculative()->
1405 cast_to_ptr_type(ptr)->
1406 with_ary(tary)->
1407 cast_to_exactness(false);
1408 }
1409 // Arrays of known objects become arrays of unknown objects.
1410 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1411 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1412 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1413 }
1414 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1415 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1416 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1417 }
1418 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1419 // cannot be distinguished by bytecode alone.
1420 if (ta->elem() == TypeInt::BOOL) {
1421 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1422 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1423 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);
1424 }
1425 // During the 2nd round of IterGVN, NotNull castings are removed.
1426 // Make sure the Bottom and NotNull variants alias the same.
1427 // Also, make sure exact and non-exact variants alias the same.
1428 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1429 tj = ta = ta->
1430 remove_speculative()->
1431 cast_to_ptr_type(TypePtr::BotPTR)->
1432 cast_to_exactness(false)->
1433 with_offset(offset);
1434 }
1435 }
1436
1437 // Oop pointers need some flattening
1438 const TypeInstPtr *to = tj->isa_instptr();
1439 if (to && to != TypeOopPtr::BOTTOM) {
1440 ciInstanceKlass* ik = to->instance_klass();
1441 if( ptr == TypePtr::Constant ) {
1442 if (ik != ciEnv::current()->Class_klass() ||
1443 offset < ik->layout_helper_size_in_bytes()) {
1453 } else if( is_known_inst ) {
1454 tj = to; // Keep NotNull and klass_is_exact for instance type
1455 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1456 // During the 2nd round of IterGVN, NotNull castings are removed.
1457 // Make sure the Bottom and NotNull variants alias the same.
1458 // Also, make sure exact and non-exact variants alias the same.
1459 tj = to = to->
1460 remove_speculative()->
1461 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1462 cast_to_ptr_type(TypePtr::BotPTR)->
1463 cast_to_exactness(false);
1464 }
1465 if (to->speculative() != nullptr) {
1466 tj = to = to->remove_speculative();
1467 }
1468 // Canonicalize the holder of this field
1469 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1470 // First handle header references such as a LoadKlassNode, even if the
1471 // object's klass is unloaded at compile time (4965979).
1472 if (!is_known_inst) { // Do it only for non-instance types
1473 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, offset);
1474 }
1475 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1476 // Static fields are in the space above the normal instance
1477 // fields in the java.lang.Class instance.
1478 if (ik != ciEnv::current()->Class_klass()) {
1479 to = nullptr;
1480 tj = TypeOopPtr::BOTTOM;
1481 offset = tj->offset();
1482 }
1483 } else {
1484 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1485 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1486 assert(tj->offset() == offset, "no change to offset expected");
1487 bool xk = to->klass_is_exact();
1488 int instance_id = to->instance_id();
1489
1490 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1491 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1492 // its interfaces are included.
1493 if (xk && ik->equals(canonical_holder)) {
1494 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id), "exact type should be canonical type");
1495 } else {
1496 assert(xk || !is_known_inst, "Known instance should be exact type");
1497 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id);
1498 }
1499 }
1500 }
1501
1502 // Klass pointers to object array klasses need some flattening
1503 const TypeKlassPtr *tk = tj->isa_klassptr();
1504 if( tk ) {
1505 // If we are referencing a field within a Klass, we need
1506 // to assume the worst case of an Object. Both exact and
1507 // inexact types must flatten to the same alias class so
1508 // use NotNull as the PTR.
1509 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1510 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1511 env()->Object_klass(),
1512 offset);
1513 }
1514
1515 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1516 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1517 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1518 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), offset);
1519 } else {
1520 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, offset);
1521 }
1522 }
1523
1524 // Check for precise loads from the primary supertype array and force them
1525 // to the supertype cache alias index. Check for generic array loads from
1526 // the primary supertype array and also force them to the supertype cache
1527 // alias index. Since the same load can reach both, we need to merge
1528 // these 2 disparate memories into the same alias class. Since the
1529 // primary supertype array is read-only, there's no chance of confusion
1530 // where we bypass an array load and an array store.
1531 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1532 if (offset == Type::OffsetBot ||
1533 (offset >= primary_supers_offset &&
1534 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1535 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1536 offset = in_bytes(Klass::secondary_super_cache_offset());
1537 tj = tk = tk->with_offset(offset);
1538 }
1539 }
1540
1541 // Flatten all Raw pointers together.
1542 if (tj->base() == Type::RawPtr)
1543 tj = TypeRawPtr::BOTTOM;
1633 intptr_t key = (intptr_t) adr_type;
1634 key ^= key >> logAliasCacheSize;
1635 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1636 }
1637
1638
1639 //-----------------------------grow_alias_types--------------------------------
1640 void Compile::grow_alias_types() {
1641 const int old_ats = _max_alias_types; // how many before?
1642 const int new_ats = old_ats; // how many more?
1643 const int grow_ats = old_ats+new_ats; // how many now?
1644 _max_alias_types = grow_ats;
1645 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1646 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1647 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1648 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1649 }
1650
1651
1652 //--------------------------------find_alias_type------------------------------
1653 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1654 if (!do_aliasing()) {
1655 return alias_type(AliasIdxBot);
1656 }
1657
1658 AliasCacheEntry* ace = probe_alias_cache(adr_type);
1659 if (ace->_adr_type == adr_type) {
1660 return alias_type(ace->_index);
1661 }
1662
1663 // Handle special cases.
1664 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1665 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1666
1667 // Do it the slow way.
1668 const TypePtr* flat = flatten_alias_type(adr_type);
1669
1670 #ifdef ASSERT
1671 {
1672 ResourceMark rm;
1673 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1674 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1675 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1676 Type::str(adr_type));
1677 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1678 const TypeOopPtr* foop = flat->is_oopptr();
1679 // Scalarizable allocations have exact klass always.
1680 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1690 if (alias_type(i)->adr_type() == flat) {
1691 idx = i;
1692 break;
1693 }
1694 }
1695
1696 if (idx == AliasIdxTop) {
1697 if (no_create) return nullptr;
1698 // Grow the array if necessary.
1699 if (_num_alias_types == _max_alias_types) grow_alias_types();
1700 // Add a new alias type.
1701 idx = _num_alias_types++;
1702 _alias_types[idx]->Init(idx, flat);
1703 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1704 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1705 if (flat->isa_instptr()) {
1706 if (flat->offset() == java_lang_Class::klass_offset()
1707 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1708 alias_type(idx)->set_rewritable(false);
1709 }
1710 if (flat->isa_aryptr()) {
1711 #ifdef ASSERT
1712 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1713 // (T_BYTE has the weakest alignment and size restrictions...)
1714 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1715 #endif
1716 if (flat->offset() == TypePtr::OffsetBot) {
1717 alias_type(idx)->set_element(flat->is_aryptr()->elem());
1718 }
1719 }
1720 if (flat->isa_klassptr()) {
1721 if (UseCompactObjectHeaders) {
1722 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1723 alias_type(idx)->set_rewritable(false);
1724 }
1725 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1726 alias_type(idx)->set_rewritable(false);
1727 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1728 alias_type(idx)->set_rewritable(false);
1729 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1730 alias_type(idx)->set_rewritable(false);
1731 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1732 alias_type(idx)->set_rewritable(false);
1733 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1734 alias_type(idx)->set_rewritable(false);
1735 }
1736 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1737 // but the base pointer type is not distinctive enough to identify
1738 // references into JavaThread.)
1739
1740 // Check for final fields.
1741 const TypeInstPtr* tinst = flat->isa_instptr();
1742 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1743 ciField* field;
1744 if (tinst->const_oop() != nullptr &&
1745 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1746 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1747 // static field
1748 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1749 field = k->get_field_by_offset(tinst->offset(), true);
1750 } else {
1751 ciInstanceKlass *k = tinst->instance_klass();
1752 field = k->get_field_by_offset(tinst->offset(), false);
1753 }
1754 assert(field == nullptr ||
1755 original_field == nullptr ||
1756 (field->holder() == original_field->holder() &&
1757 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1758 field->is_static() == original_field->is_static()), "wrong field?");
1759 // Set field() and is_rewritable() attributes.
1760 if (field != nullptr) alias_type(idx)->set_field(field);
1761 }
1762 }
1763
1764 // Fill the cache for next time.
1765 ace->_adr_type = adr_type;
1766 ace->_index = idx;
1767 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1768
1769 // Might as well try to fill the cache for the flattened version, too.
1770 AliasCacheEntry* face = probe_alias_cache(flat);
1771 if (face->_adr_type == nullptr) {
1772 face->_adr_type = flat;
1773 face->_index = idx;
1774 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1775 }
1776
1777 return alias_type(idx);
1778 }
1779
1780
1781 Compile::AliasType* Compile::alias_type(ciField* field) {
1782 const TypeOopPtr* t;
1783 if (field->is_static())
1784 t = TypeInstPtr::make(field->holder()->java_mirror());
1785 else
1786 t = TypeOopPtr::make_from_klass_raw(field->holder());
1787 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1788 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1789 return atp;
1790 }
1791
1792
1793 //------------------------------have_alias_type--------------------------------
1794 bool Compile::have_alias_type(const TypePtr* adr_type) {
1876 assert(!C->major_progress(), "not cleared");
1877
1878 if (_for_post_loop_igvn.length() > 0) {
1879 while (_for_post_loop_igvn.length() > 0) {
1880 Node* n = _for_post_loop_igvn.pop();
1881 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1882 igvn._worklist.push(n);
1883 }
1884 igvn.optimize();
1885 if (failing()) return;
1886 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1887 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1888
1889 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1890 if (C->major_progress()) {
1891 C->clear_major_progress(); // ensure that major progress is now clear
1892 }
1893 }
1894 }
1895
1896 void Compile::record_for_merge_stores_igvn(Node* n) {
1897 if (!n->for_merge_stores_igvn()) {
1898 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
1899 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1900 _for_merge_stores_igvn.append(n);
1901 }
1902 }
1903
1904 void Compile::remove_from_merge_stores_igvn(Node* n) {
1905 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1906 _for_merge_stores_igvn.remove(n);
1907 }
1908
1909 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
1910 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
1911 // the stores, and we merge the wrong sequence of stores.
1912 // Example:
1913 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
1914 // Apply MergeStores:
1915 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
1994 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
1995 Bytecodes::Code c = iter.cur_bc();
1996 Node* lhs = nullptr;
1997 Node* rhs = nullptr;
1998 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
1999 lhs = unc->peek_operand(0);
2000 rhs = unc->peek_operand(1);
2001 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2002 lhs = unc->peek_operand(0);
2003 }
2004
2005 ResourceMark rm;
2006 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2007 assert(live_locals.is_valid(), "broken liveness info");
2008 int len = (int)live_locals.size();
2009
2010 for (int i = 0; i < len; i++) {
2011 Node* local = unc->local(jvms, i);
2012 // kill local using the liveness of next_bci.
2013 // give up when the local looks like an operand to secure reexecution.
2014 if (!live_locals.at(i) && !local->is_top() && local != lhs && local!= rhs) {
2015 uint idx = jvms->locoff() + i;
2016 #ifdef ASSERT
2017 if (PrintOpto && Verbose) {
2018 tty->print("[unstable_if] kill local#%d: ", idx);
2019 local->dump();
2020 tty->cr();
2021 }
2022 #endif
2023 igvn.replace_input_of(unc, idx, top());
2024 modified = true;
2025 }
2026 }
2027 }
2028
2029 // keep the mondified trap for late query
2030 if (modified) {
2031 trap->set_modified();
2032 } else {
2033 _unstable_if_traps.delete_at(i);
2034 }
2035 }
2036 igvn.optimize();
2037 }
2038
2039 // StringOpts and late inlining of string methods
2040 void Compile::inline_string_calls(bool parse_time) {
2041 {
2042 // remove useless nodes to make the usage analysis simpler
2043 ResourceMark rm;
2044 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2045 }
2046
2047 {
2048 ResourceMark rm;
2049 print_method(PHASE_BEFORE_STRINGOPTS, 3);
2215
2216 if (_string_late_inlines.length() > 0) {
2217 assert(has_stringbuilder(), "inconsistent");
2218
2219 inline_string_calls(false);
2220
2221 if (failing()) return;
2222
2223 inline_incrementally_cleanup(igvn);
2224 }
2225
2226 set_inlining_incrementally(false);
2227 }
2228
2229 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2230 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2231 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2232 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2233 // as if "inlining_incrementally() == true" were set.
2234 assert(inlining_incrementally() == false, "not allowed");
2235 assert(_modified_nodes == nullptr, "not allowed");
2236 assert(_late_inlines.length() > 0, "sanity");
2237
2238 while (_late_inlines.length() > 0) {
2239 igvn_worklist()->ensure_empty(); // should be done with igvn
2240
2241 while (inline_incrementally_one()) {
2242 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2243 }
2244 if (failing()) return;
2245
2246 inline_incrementally_cleanup(igvn);
2247 }
2248 }
2249
2250 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2251 if (_loop_opts_cnt > 0) {
2252 while (major_progress() && (_loop_opts_cnt > 0)) {
2253 TracePhase tp(_t_idealLoop);
2254 PhaseIdealLoop::optimize(igvn, mode);
2255 _loop_opts_cnt--;
2256 if (failing()) return false;
2257 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2258 }
2259 }
2260 return true;
2261 }
2262
2263 // Remove edges from "root" to each SafePoint at a backward branch.
2264 // They were inserted during parsing (see add_safepoint()) to make
2265 // infinite loops without calls or exceptions visible to root, i.e.,
2266 // useful.
2267 void Compile::remove_root_to_sfpts_edges(PhaseIterGVN& igvn) {
2371 print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
2372 }
2373 assert(!has_vbox_nodes(), "sanity");
2374
2375 if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2376 Compile::TracePhase tp(_t_renumberLive);
2377 igvn_worklist()->ensure_empty(); // should be done with igvn
2378 {
2379 ResourceMark rm;
2380 PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
2381 }
2382 igvn.reset();
2383 igvn.optimize();
2384 if (failing()) return;
2385 }
2386
2387 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2388 // safepoints
2389 remove_root_to_sfpts_edges(igvn);
2390
2391 if (failing()) return;
2392
2393 if (has_loops()) {
2394 print_method(PHASE_BEFORE_LOOP_OPTS, 2);
2395 }
2396
2397 // Perform escape analysis
2398 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2399 if (has_loops()) {
2400 // Cleanup graph (remove dead nodes).
2401 TracePhase tp(_t_idealLoop);
2402 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2403 if (failing()) return;
2404 }
2405 bool progress;
2406 print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2407 do {
2408 ConnectionGraph::do_analysis(this, &igvn);
2409
2410 if (failing()) return;
2411
2412 int mcount = macro_count(); // Record number of allocations and locks before IGVN
2413
2414 // Optimize out fields loads from scalar replaceable allocations.
2415 igvn.optimize();
2416 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2417
2418 if (failing()) return;
2419
2420 if (congraph() != nullptr && macro_count() > 0) {
2421 TracePhase tp(_t_macroEliminate);
2422 PhaseMacroExpand mexp(igvn);
2423 mexp.eliminate_macro_nodes();
2424 if (failing()) return;
2425 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2426
2427 igvn.set_delay_transform(false);
2428 igvn.optimize();
2429 if (failing()) return;
2430
2431 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2432 }
2433
2434 ConnectionGraph::verify_ram_nodes(this, root());
2435 if (failing()) return;
2436
2437 progress = do_iterative_escape_analysis() &&
2438 (macro_count() < mcount) &&
2439 ConnectionGraph::has_candidates(this);
2440 // Try again if candidates exist and made progress
2441 // by removing some allocations and/or locks.
2442 } while (progress);
2443 }
2444
2445 // Loop transforms on the ideal graph. Range Check Elimination,
2446 // peeling, unrolling, etc.
2447
2448 // Set loop opts counter
2449 if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2450 {
2501 // Loop transforms on the ideal graph. Range Check Elimination,
2502 // peeling, unrolling, etc.
2503 if (!optimize_loops(igvn, LoopOptsDefault)) {
2504 return;
2505 }
2506
2507 if (failing()) return;
2508
2509 C->clear_major_progress(); // ensure that major progress is now clear
2510
2511 process_for_post_loop_opts_igvn(igvn);
2512
2513 process_for_merge_stores_igvn(igvn);
2514
2515 if (failing()) return;
2516
2517 #ifdef ASSERT
2518 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
2519 #endif
2520
2521 {
2522 TracePhase tp(_t_macroExpand);
2523 print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);
2524 PhaseMacroExpand mex(igvn);
2525 // Do not allow new macro nodes once we start to eliminate and expand
2526 C->reset_allow_macro_nodes();
2527 // Last attempt to eliminate macro nodes before expand
2528 mex.eliminate_macro_nodes();
2529 if (failing()) {
2530 return;
2531 }
2532 mex.eliminate_opaque_looplimit_macro_nodes();
2533 if (failing()) {
2534 return;
2535 }
2536 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2537 if (mex.expand_macro_nodes()) {
2538 assert(failing(), "must bail out w/ explicit message");
2539 return;
2540 }
2541 print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
2542 }
2543
2544 {
2545 TracePhase tp(_t_barrierExpand);
2546 if (bs->expand_barriers(this, igvn)) {
2547 assert(failing(), "must bail out w/ explicit message");
2548 return;
2549 }
2550 print_method(PHASE_BARRIER_EXPANSION, 2);
2551 }
2552
2553 if (C->max_vector_size() > 0) {
2554 C->optimize_logic_cones(igvn);
2555 igvn.optimize();
2556 if (failing()) return;
2557 }
2558
2559 DEBUG_ONLY( _modified_nodes = nullptr; )
2560
2561 assert(igvn._worklist.size() == 0, "not empty");
2562
2563 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
2564
2565 if (_late_inlines.length() > 0) {
2566 // More opportunities to optimize virtual and MH calls.
2567 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
2568 process_late_inline_calls_no_inline(igvn);
2569 if (failing()) return;
2570 }
2571 } // (End scope of igvn; run destructor if necessary for asserts.)
2572
2573 check_no_dead_use();
2574
2575 // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
2576 // to remove hashes to unlock nodes for modifications.
2577 C->node_hash()->clear();
2578
2579 // A method with only infinite loops has no edges entering loops from root
2580 {
2581 TracePhase tp(_t_graphReshaping);
2582 if (final_graph_reshaping()) {
2583 assert(failing(), "must bail out w/ explicit message");
2584 return;
2585 }
2586 }
2587
2588 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2589 DEBUG_ONLY(set_phase_optimize_finished();)
2590 }
3296 case Op_CmpD3:
3297 case Op_StoreD:
3298 case Op_LoadD:
3299 case Op_LoadD_unaligned:
3300 frc.inc_double_count();
3301 break;
3302 case Op_Opaque1: // Remove Opaque Nodes before matching
3303 n->subsume_by(n->in(1), this);
3304 break;
3305 case Op_CallLeafPure: {
3306 // If the pure call is not supported, then lower to a CallLeaf.
3307 if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
3308 CallNode* call = n->as_Call();
3309 CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
3310 call->_name, TypeRawPtr::BOTTOM);
3311 new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
3312 new_call->init_req(TypeFunc::I_O, C->top());
3313 new_call->init_req(TypeFunc::Memory, C->top());
3314 new_call->init_req(TypeFunc::ReturnAdr, C->top());
3315 new_call->init_req(TypeFunc::FramePtr, C->top());
3316 for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain()->cnt(); i++) {
3317 new_call->init_req(i, call->in(i));
3318 }
3319 n->subsume_by(new_call, this);
3320 }
3321 frc.inc_call_count();
3322 break;
3323 }
3324 case Op_CallStaticJava:
3325 case Op_CallJava:
3326 case Op_CallDynamicJava:
3327 frc.inc_java_call_count(); // Count java call site;
3328 case Op_CallRuntime:
3329 case Op_CallLeaf:
3330 case Op_CallLeafVector:
3331 case Op_CallLeafNoFP: {
3332 assert (n->is_Call(), "");
3333 CallNode *call = n->as_Call();
3334 // Count call sites where the FP mode bit would have to be flipped.
3335 // Do not count uncommon runtime calls:
3336 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
3342 int nop = n->Opcode();
3343 // Clone shared simple arguments to uncommon calls, item (1).
3344 if (n->outcnt() > 1 &&
3345 !n->is_Proj() &&
3346 nop != Op_CreateEx &&
3347 nop != Op_CheckCastPP &&
3348 nop != Op_DecodeN &&
3349 nop != Op_DecodeNKlass &&
3350 !n->is_Mem() &&
3351 !n->is_Phi()) {
3352 Node *x = n->clone();
3353 call->set_req(TypeFunc::Parms, x);
3354 }
3355 }
3356 break;
3357 }
3358 case Op_StoreB:
3359 case Op_StoreC:
3360 case Op_StoreI:
3361 case Op_StoreL:
3362 case Op_CompareAndSwapB:
3363 case Op_CompareAndSwapS:
3364 case Op_CompareAndSwapI:
3365 case Op_CompareAndSwapL:
3366 case Op_CompareAndSwapP:
3367 case Op_CompareAndSwapN:
3368 case Op_WeakCompareAndSwapB:
3369 case Op_WeakCompareAndSwapS:
3370 case Op_WeakCompareAndSwapI:
3371 case Op_WeakCompareAndSwapL:
3372 case Op_WeakCompareAndSwapP:
3373 case Op_WeakCompareAndSwapN:
3374 case Op_CompareAndExchangeB:
3375 case Op_CompareAndExchangeS:
3376 case Op_CompareAndExchangeI:
3377 case Op_CompareAndExchangeL:
3378 case Op_CompareAndExchangeP:
3379 case Op_CompareAndExchangeN:
3380 case Op_GetAndAddS:
3381 case Op_GetAndAddB:
3894 k->subsume_by(m, this);
3895 }
3896 }
3897 }
3898 break;
3899 }
3900 case Op_CmpUL: {
3901 if (!Matcher::has_match_rule(Op_CmpUL)) {
3902 // No support for unsigned long comparisons
3903 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3904 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3905 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3906 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3907 Node* andl = new AndLNode(orl, remove_sign_mask);
3908 Node* cmp = new CmpLNode(andl, n->in(2));
3909 n->subsume_by(cmp, this);
3910 }
3911 break;
3912 }
3913 #ifdef ASSERT
3914 case Op_ConNKlass: {
3915 const TypePtr* tp = n->as_Type()->type()->make_ptr();
3916 ciKlass* klass = tp->is_klassptr()->exact_klass();
3917 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
3918 break;
3919 }
3920 #endif
3921 default:
3922 assert(!n->is_Call(), "");
3923 assert(!n->is_Mem(), "");
3924 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3925 break;
3926 }
3927 }
3928
3929 //------------------------------final_graph_reshaping_walk---------------------
3930 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3931 // requires that the walk visits a node's inputs before visiting the node.
3932 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3933 Unique_Node_List sfpt;
4269 }
4270 }
4271
4272 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4273 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4274 }
4275
4276 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4277 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4278 }
4279
4280 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4281 if (holder->is_initialized()) {
4282 return false;
4283 }
4284 if (holder->is_being_initialized()) {
4285 if (accessing_method->holder() == holder) {
4286 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4287 // <init>, or a static method. In all those cases, there was an initialization
4288 // barrier on the holder klass passed.
4289 if (accessing_method->is_static_initializer() ||
4290 accessing_method->is_object_initializer() ||
4291 accessing_method->is_static()) {
4292 return false;
4293 }
4294 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4295 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4296 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4297 // child class can become fully initialized while its parent class is still being initialized.
4298 if (accessing_method->is_static_initializer()) {
4299 return false;
4300 }
4301 }
4302 ciMethod* root = method(); // the root method of compilation
4303 if (root != accessing_method) {
4304 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4305 }
4306 }
4307 return true;
4308 }
4309
4310 #ifndef PRODUCT
4311 //------------------------------verify_bidirectional_edges---------------------
4312 // For each input edge to a node (ie - for each Use-Def edge), verify that
4313 // there is a corresponding Def-Use edge.
4314 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4315 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4316 uint stack_size = live_nodes() >> 4;
4317 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4318 if (root_and_safepoints != nullptr) {
4348 if (in != nullptr && !in->is_top()) {
4349 // Count instances of `next`
4350 int cnt = 0;
4351 for (uint idx = 0; idx < in->_outcnt; idx++) {
4352 if (in->_out[idx] == n) {
4353 cnt++;
4354 }
4355 }
4356 assert(cnt > 0, "Failed to find Def-Use edge.");
4357 // Check for duplicate edges
4358 // walk the input array downcounting the input edges to n
4359 for (uint j = 0; j < length; j++) {
4360 if (n->in(j) == in) {
4361 cnt--;
4362 }
4363 }
4364 assert(cnt == 0, "Mismatched edge count.");
4365 } else if (in == nullptr) {
4366 assert(i == 0 || i >= n->req() ||
4367 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4368 (n->is_Unlock() && i == (n->req() - 1)) ||
4369 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4370 "only region, phi, arraycopy, unlock or membar nodes have null data edges");
4371 } else {
4372 assert(in->is_top(), "sanity");
4373 // Nothing to check.
4374 }
4375 }
4376 }
4377 }
4378
4379 //------------------------------verify_graph_edges---------------------------
4380 // Walk the Graph and verify that there is a one-to-one correspondence
4381 // between Use-Def edges and Def-Use edges in the graph.
4382 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4383 if (VerifyGraphEdges) {
4384 Unique_Node_List visited;
4385
4386 // Call graph walk to check edges
4387 verify_bidirectional_edges(visited, root_and_safepoints);
4388 if (no_dead_code) {
4389 // Now make sure that no visited node is used by an unvisited node.
4390 bool dead_nodes = false;
4501 // (1) subklass is already limited to a subtype of superklass => always ok
4502 // (2) subklass does not overlap with superklass => always fail
4503 // (3) superklass has NO subtypes and we can check with a simple compare.
4504 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4505 if (skip) {
4506 return SSC_full_test; // Let caller generate the general case.
4507 }
4508
4509 if (subk->is_java_subtype_of(superk)) {
4510 return SSC_always_true; // (0) and (1) this test cannot fail
4511 }
4512
4513 if (!subk->maybe_java_subtype_of(superk)) {
4514 return SSC_always_false; // (2) true path dead; no dynamic test needed
4515 }
4516
4517 const Type* superelem = superk;
4518 if (superk->isa_aryklassptr()) {
4519 int ignored;
4520 superelem = superk->is_aryklassptr()->base_element_type(ignored);
4521 }
4522
4523 if (superelem->isa_instklassptr()) {
4524 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4525 if (!ik->has_subklass()) {
4526 if (!ik->is_final()) {
4527 // Add a dependency if there is a chance of a later subclass.
4528 dependencies()->assert_leaf_type(ik);
4529 }
4530 if (!superk->maybe_java_subtype_of(subk)) {
4531 return SSC_always_false;
4532 }
4533 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4534 }
4535 } else {
4536 // A primitive array type has no subtypes.
4537 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4538 }
4539
4540 return SSC_full_test;
4984 const Type* t = igvn.type_or_null(n);
4985 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
4986 if (n->is_Type()) {
4987 t = n->as_Type()->type();
4988 assert(t == t->remove_speculative(), "no more speculative types");
4989 }
4990 // Iterate over outs - endless loops is unreachable from below
4991 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4992 Node *m = n->fast_out(i);
4993 if (not_a_node(m)) {
4994 continue;
4995 }
4996 worklist.push(m);
4997 }
4998 }
4999 igvn.check_no_speculative_types();
5000 #endif
5001 }
5002 }
5003
5004 // Auxiliary methods to support randomized stressing/fuzzing.
5005
5006 void Compile::initialize_stress_seed(const DirectiveSet* directive) {
5007 if (FLAG_IS_DEFAULT(StressSeed) || (FLAG_IS_ERGO(StressSeed) && directive->RepeatCompilationOption)) {
5008 _stress_seed = static_cast<uint>(Ticks::now().nanoseconds());
5009 FLAG_SET_ERGO(StressSeed, _stress_seed);
5010 } else {
5011 _stress_seed = StressSeed;
5012 }
5013 if (_log != nullptr) {
5014 _log->elem("stress_test seed='%u'", _stress_seed);
5015 }
5016 }
5017
5018 int Compile::random() {
5019 _stress_seed = os::next_random(_stress_seed);
5020 return static_cast<int>(_stress_seed);
5021 }
5022
5023 // This method can be called the arbitrary number of times, with current count
5339 } else {
5340 _debug_network_printer->update_compiled_method(C->method());
5341 }
5342 tty->print_cr("Method printed over network stream to IGV");
5343 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5344 }
5345 #endif // !PRODUCT
5346
5347 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5348 if (type != nullptr && phase->type(value)->higher_equal(type)) {
5349 return value;
5350 }
5351 Node* result = nullptr;
5352 if (bt == T_BYTE) {
5353 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5354 result = new RShiftINode(result, phase->intcon(24));
5355 } else if (bt == T_BOOLEAN) {
5356 result = new AndINode(value, phase->intcon(0xFF));
5357 } else if (bt == T_CHAR) {
5358 result = new AndINode(value,phase->intcon(0xFFFF));
5359 } else {
5360 assert(bt == T_SHORT, "unexpected narrow type");
5361 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5362 result = new RShiftINode(result, phase->intcon(16));
5363 }
5364 if (transform_res) {
5365 result = phase->transform(result);
5366 }
5367 return result;
5368 }
5369
5370 void Compile::record_method_not_compilable_oom() {
5371 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5372 }
|
42 #include "gc/shared/c2/barrierSetC2.hpp"
43 #include "jfr/jfrEvents.hpp"
44 #include "jvm_io.h"
45 #include "memory/allocation.hpp"
46 #include "memory/arena.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "opto/addnode.hpp"
49 #include "opto/block.hpp"
50 #include "opto/c2compiler.hpp"
51 #include "opto/callGenerator.hpp"
52 #include "opto/callnode.hpp"
53 #include "opto/castnode.hpp"
54 #include "opto/cfgnode.hpp"
55 #include "opto/chaitin.hpp"
56 #include "opto/compile.hpp"
57 #include "opto/connode.hpp"
58 #include "opto/convertnode.hpp"
59 #include "opto/divnode.hpp"
60 #include "opto/escape.hpp"
61 #include "opto/idealGraphPrinter.hpp"
62 #include "opto/inlinetypenode.hpp"
63 #include "opto/locknode.hpp"
64 #include "opto/loopnode.hpp"
65 #include "opto/machnode.hpp"
66 #include "opto/macro.hpp"
67 #include "opto/matcher.hpp"
68 #include "opto/mathexactnode.hpp"
69 #include "opto/memnode.hpp"
70 #include "opto/movenode.hpp"
71 #include "opto/mulnode.hpp"
72 #include "opto/narrowptrnode.hpp"
73 #include "opto/node.hpp"
74 #include "opto/opaquenode.hpp"
75 #include "opto/opcodes.hpp"
76 #include "opto/output.hpp"
77 #include "opto/parse.hpp"
78 #include "opto/phaseX.hpp"
79 #include "opto/rootnode.hpp"
80 #include "opto/runtime.hpp"
81 #include "opto/stringopts.hpp"
82 #include "opto/type.hpp"
83 #include "opto/vector.hpp"
84 #include "opto/vectornode.hpp"
85 #include "runtime/globals_extension.hpp"
86 #include "runtime/sharedRuntime.hpp"
87 #include "runtime/signature.hpp"
88 #include "runtime/stubRoutines.hpp"
89 #include "runtime/timer.hpp"
90 #include "utilities/align.hpp"
390 // as dead to be conservative about the dead node count at any
391 // given time.
392 if (!dead->is_Con()) {
393 record_dead_node(dead->_idx);
394 }
395 if (dead->is_macro()) {
396 remove_macro_node(dead);
397 }
398 if (dead->is_expensive()) {
399 remove_expensive_node(dead);
400 }
401 if (dead->is_OpaqueTemplateAssertionPredicate()) {
402 remove_template_assertion_predicate_opaque(dead->as_OpaqueTemplateAssertionPredicate());
403 }
404 if (dead->is_ParsePredicate()) {
405 remove_parse_predicate(dead->as_ParsePredicate());
406 }
407 if (dead->for_post_loop_opts_igvn()) {
408 remove_from_post_loop_opts_igvn(dead);
409 }
410 if (dead->is_InlineType()) {
411 remove_inline_type(dead);
412 }
413 if (dead->for_merge_stores_igvn()) {
414 remove_from_merge_stores_igvn(dead);
415 }
416 if (dead->is_Call()) {
417 remove_useless_late_inlines( &_late_inlines, dead);
418 remove_useless_late_inlines( &_string_late_inlines, dead);
419 remove_useless_late_inlines( &_boxing_late_inlines, dead);
420 remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
421
422 if (dead->is_CallStaticJava()) {
423 remove_unstable_if_trap(dead->as_CallStaticJava(), false);
424 }
425 }
426 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
427 bs->unregister_potential_barrier_node(dead);
428 }
429
430 // Disconnect all useless nodes by disconnecting those at the boundary.
431 void Compile::disconnect_useless_nodes(Unique_Node_List& useful, Unique_Node_List& worklist, const Unique_Node_List* root_and_safepoints) {
432 uint next = 0;
440 // Use raw traversal of out edges since this code removes out edges
441 int max = n->outcnt();
442 for (int j = 0; j < max; ++j) {
443 Node* child = n->raw_out(j);
444 if (!useful.member(child)) {
445 assert(!child->is_top() || child != top(),
446 "If top is cached in Compile object it is in useful list");
447 // Only need to remove this out-edge to the useless node
448 n->raw_del_out(j);
449 --j;
450 --max;
451 if (child->is_data_proj_of_pure_function(n)) {
452 worklist.push(n);
453 }
454 }
455 }
456 if (n->outcnt() == 1 && n->has_special_unique_user()) {
457 assert(useful.member(n->unique_out()), "do not push a useless node");
458 worklist.push(n->unique_out());
459 }
460 if (n->outcnt() == 0) {
461 worklist.push(n);
462 }
463 }
464
465 remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes
466 remove_useless_nodes(_parse_predicates, useful); // remove useless Parse Predicate nodes
467 // Remove useless Template Assertion Predicate opaque nodes
468 remove_useless_nodes(_template_assertion_predicate_opaques, useful);
469 remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes
470 remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
471 remove_useless_nodes(_inline_type_nodes, useful); // remove useless inline type nodes
472 #ifdef ASSERT
473 if (_modified_nodes != nullptr) {
474 _modified_nodes->remove_useless_nodes(useful.member_set());
475 }
476 #endif
477 remove_useless_nodes(_for_merge_stores_igvn, useful); // remove useless node recorded for merge stores IGVN pass
478 remove_useless_unstable_if_traps(useful); // remove useless unstable_if traps
479 remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes
480 #ifdef ASSERT
481 if (_modified_nodes != nullptr) {
482 _modified_nodes->remove_useless_nodes(useful.member_set());
483 }
484 #endif
485
486 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
487 bs->eliminate_useless_gc_barriers(useful, this);
488 // clean up the late inline lists
489 remove_useless_late_inlines( &_late_inlines, useful);
490 remove_useless_late_inlines( &_string_late_inlines, useful);
491 remove_useless_late_inlines( &_boxing_late_inlines, useful);
492 remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
493 DEBUG_ONLY(verify_graph_edges(true /*check for no_dead_code*/, root_and_safepoints);)
494 }
495
496 // ============================================================================
643 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
644 Options options, DirectiveSet* directive)
645 : Phase(Compiler),
646 _compile_id(ci_env->compile_id()),
647 _options(options),
648 _method(target),
649 _entry_bci(osr_bci),
650 _ilt(nullptr),
651 _stub_function(nullptr),
652 _stub_name(nullptr),
653 _stub_id(StubId::NO_STUBID),
654 _stub_entry_point(nullptr),
655 _max_node_limit(MaxNodeLimit),
656 _post_loop_opts_phase(false),
657 _merge_stores_phase(false),
658 _allow_macro_nodes(true),
659 _inlining_progress(false),
660 _inlining_incrementally(false),
661 _do_cleanup(false),
662 _has_reserved_stack_access(target->has_reserved_stack_access()),
663 _has_circular_inline_type(false),
664 #ifndef PRODUCT
665 _igv_idx(0),
666 _trace_opto_output(directive->TraceOptoOutputOption),
667 #endif
668 _has_method_handle_invokes(false),
669 _clinit_barrier_on_entry(false),
670 _stress_seed(0),
671 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
672 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
673 _env(ci_env),
674 _directive(directive),
675 _log(ci_env->log()),
676 _first_failure_details(nullptr),
677 _intrinsics(comp_arena(), 0, 0, nullptr),
678 _macro_nodes(comp_arena(), 8, 0, nullptr),
679 _parse_predicates(comp_arena(), 8, 0, nullptr),
680 _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
681 _expensive_nodes(comp_arena(), 8, 0, nullptr),
682 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
683 _inline_type_nodes (comp_arena(), 8, 0, nullptr),
684 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
685 _unstable_if_traps(comp_arena(), 8, 0, nullptr),
686 _coarsened_locks(comp_arena(), 8, 0, nullptr),
687 _congraph(nullptr),
688 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
689 _unique(0),
690 _dead_node_count(0),
691 _dead_node_list(comp_arena()),
692 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
693 _node_arena_two(mtCompiler, Arena::Tag::tag_node),
694 _node_arena(&_node_arena_one),
695 _mach_constant_base_node(nullptr),
696 _Compile_types(mtCompiler, Arena::Tag::tag_type),
697 _initial_gvn(nullptr),
698 _igvn_worklist(nullptr),
699 _types(nullptr),
700 _node_hash(nullptr),
701 _late_inlines(comp_arena(), 2, 0, nullptr),
702 _string_late_inlines(comp_arena(), 2, 0, nullptr),
703 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
770 #define MINIMUM_NODE_HASH 1023
771
772 // GVN that will be run immediately on new nodes
773 uint estimated_size = method()->code_size()*4+64;
774 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
775 _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
776 _types = new (comp_arena()) Type_Array(comp_arena());
777 _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
778 PhaseGVN gvn;
779 set_initial_gvn(&gvn);
780
781 { // Scope for timing the parser
782 TracePhase tp(_t_parser);
783
784 // Put top into the hash table ASAP.
785 initial_gvn()->transform(top());
786
787 // Set up tf(), start(), and find a CallGenerator.
788 CallGenerator* cg = nullptr;
789 if (is_osr_compilation()) {
790 init_tf(TypeFunc::make(method(), /* is_osr_compilation = */ true));
791 StartNode* s = new StartOSRNode(root(), tf()->domain_sig());
792 initial_gvn()->set_type_bottom(s);
793 verify_start(s);
794 cg = CallGenerator::for_osr(method(), entry_bci());
795 } else {
796 // Normal case.
797 init_tf(TypeFunc::make(method()));
798 StartNode* s = new StartNode(root(), tf()->domain_cc());
799 initial_gvn()->set_type_bottom(s);
800 verify_start(s);
801 float past_uses = method()->interpreter_invocation_count();
802 float expected_uses = past_uses;
803 cg = CallGenerator::for_inline(method(), expected_uses);
804 }
805 if (failing()) return;
806 if (cg == nullptr) {
807 const char* reason = InlineTree::check_can_parse(method());
808 assert(reason != nullptr, "expect reason for parse failure");
809 stringStream ss;
810 ss.print("cannot parse method: %s", reason);
811 record_method_not_compilable(ss.as_string());
812 return;
813 }
814
815 gvn.set_type(root(), root()->bottom_type());
816
817 JVMState* jvms = build_start_state(start(), tf());
818 if ((jvms = cg->generate(jvms)) == nullptr) {
879 print_ideal_ir("print_ideal");
880 }
881 #endif
882
883 #ifdef ASSERT
884 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
885 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
886 #endif
887
888 // Dump compilation data to replay it.
889 if (directive->DumpReplayOption) {
890 env()->dump_replay_data(_compile_id);
891 }
892 if (directive->DumpInlineOption && (ilt() != nullptr)) {
893 env()->dump_inline_data(_compile_id);
894 }
895
896 // Now that we know the size of all the monitors we can add a fixed slot
897 // for the original deopt pc.
898 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
899 if (needs_stack_repair()) {
900 // One extra slot for the special stack increment value
901 next_slot += 2;
902 }
903 // TODO 8284443 Only reserve extra slot if needed
904 if (InlineTypeReturnedAsFields) {
905 // One extra slot to hold the null marker for a nullable
906 // inline type return if we run out of registers.
907 next_slot += 2;
908 }
909 set_fixed_slots(next_slot);
910
911 // Compute when to use implicit null checks. Used by matching trap based
912 // nodes and NullCheck optimization.
913 set_allowed_deopt_reasons();
914
915 // Now generate code
916 Code_Gen();
917 }
918
919 //------------------------------Compile----------------------------------------
920 // Compile a runtime stub
921 Compile::Compile(ciEnv* ci_env,
922 TypeFunc_generator generator,
923 address stub_function,
924 const char* stub_name,
925 StubId stub_id,
926 int is_fancy_jump,
927 bool pass_tls,
928 bool return_pc,
929 DirectiveSet* directive)
930 : Phase(Compiler),
931 _compile_id(0),
932 _options(Options::for_runtime_stub()),
933 _method(nullptr),
934 _entry_bci(InvocationEntryBci),
935 _stub_function(stub_function),
936 _stub_name(stub_name),
937 _stub_id(stub_id),
938 _stub_entry_point(nullptr),
939 _max_node_limit(MaxNodeLimit),
940 _post_loop_opts_phase(false),
941 _merge_stores_phase(false),
942 _allow_macro_nodes(true),
943 _inlining_progress(false),
944 _inlining_incrementally(false),
945 _has_reserved_stack_access(false),
946 _has_circular_inline_type(false),
947 #ifndef PRODUCT
948 _igv_idx(0),
949 _trace_opto_output(directive->TraceOptoOutputOption),
950 #endif
951 _has_method_handle_invokes(false),
952 _clinit_barrier_on_entry(false),
953 _stress_seed(0),
954 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
955 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
956 _env(ci_env),
957 _directive(directive),
958 _log(ci_env->log()),
959 _first_failure_details(nullptr),
960 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
961 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
962 _congraph(nullptr),
963 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
964 _unique(0),
965 _dead_node_count(0),
966 _dead_node_list(comp_arena()),
1080 _fixed_slots = 0;
1081 set_has_split_ifs(false);
1082 set_has_loops(false); // first approximation
1083 set_has_stringbuilder(false);
1084 set_has_boxed_value(false);
1085 _trap_can_recompile = false; // no traps emitted yet
1086 _major_progress = true; // start out assuming good things will happen
1087 set_has_unsafe_access(false);
1088 set_max_vector_size(0);
1089 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1090 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1091 set_decompile_count(0);
1092
1093 #ifndef PRODUCT
1094 _phase_counter = 0;
1095 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1096 #endif
1097
1098 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1099 _loop_opts_cnt = LoopOptsCount;
1100 _has_flat_accesses = false;
1101 _flat_accesses_share_alias = true;
1102 _scalarize_in_safepoints = false;
1103
1104 set_do_inlining(Inline);
1105 set_max_inline_size(MaxInlineSize);
1106 set_freq_inline_size(FreqInlineSize);
1107 set_do_scheduling(OptoScheduling);
1108
1109 set_do_vector_loop(false);
1110 set_has_monitors(false);
1111 set_has_scoped_access(false);
1112
1113 if (AllowVectorizeOnDemand) {
1114 if (has_method() && _directive->VectorizeOption) {
1115 set_do_vector_loop(true);
1116 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());})
1117 } else if (has_method() && method()->name() != nullptr &&
1118 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1119 set_do_vector_loop(true);
1120 }
1121 }
1122 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1123 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());})
1368
1369 // Known instance (scalarizable allocation) alias only with itself.
1370 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1371 tj->is_oopptr()->is_known_instance();
1372
1373 // Process weird unsafe references.
1374 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1375 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1376 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1377 tj = TypeOopPtr::BOTTOM;
1378 ptr = tj->ptr();
1379 offset = tj->offset();
1380 }
1381
1382 // Array pointers need some flattening
1383 const TypeAryPtr* ta = tj->isa_aryptr();
1384 if (ta && ta->is_stable()) {
1385 // Erase stability property for alias analysis.
1386 tj = ta = ta->cast_to_stable(false);
1387 }
1388 if (ta && ta->is_not_flat()) {
1389 // Erase not flat property for alias analysis.
1390 tj = ta = ta->cast_to_not_flat(false);
1391 }
1392 if (ta && ta->is_not_null_free()) {
1393 // Erase not null free property for alias analysis.
1394 tj = ta = ta->cast_to_not_null_free(false);
1395 }
1396
1397 if( ta && is_known_inst ) {
1398 if ( offset != Type::OffsetBot &&
1399 offset > arrayOopDesc::length_offset_in_bytes() ) {
1400 offset = Type::OffsetBot; // Flatten constant access into array body only
1401 tj = ta = ta->
1402 remove_speculative()->
1403 cast_to_ptr_type(ptr)->
1404 with_offset(offset);
1405 }
1406 } else if (ta) {
1407 // For arrays indexed by constant indices, we flatten the alias
1408 // space to include all of the array body. Only the header, klass
1409 // and array length can be accessed un-aliased.
1410 // For flat inline type array, each field has its own slice so
1411 // we must include the field offset.
1412 if( offset != Type::OffsetBot ) {
1413 if( ta->const_oop() ) { // MethodData* or Method*
1414 offset = Type::OffsetBot; // Flatten constant access into array body
1415 tj = ta = ta->
1416 remove_speculative()->
1417 cast_to_ptr_type(ptr)->
1418 cast_to_exactness(false)->
1419 with_offset(offset);
1420 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1421 // range is OK as-is.
1422 tj = ta = TypeAryPtr::RANGE;
1423 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1424 tj = TypeInstPtr::KLASS; // all klass loads look alike
1425 ta = TypeAryPtr::RANGE; // generic ignored junk
1426 ptr = TypePtr::BotPTR;
1427 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1428 tj = TypeInstPtr::MARK;
1429 ta = TypeAryPtr::RANGE; // generic ignored junk
1430 ptr = TypePtr::BotPTR;
1431 } else { // Random constant offset into array body
1432 offset = Type::OffsetBot; // Flatten constant access into array body
1433 tj = ta = ta->
1434 remove_speculative()->
1435 cast_to_ptr_type(ptr)->
1436 cast_to_exactness(false)->
1437 with_offset(offset);
1438 }
1439 }
1440 // Arrays of fixed size alias with arrays of unknown size.
1441 if (ta->size() != TypeInt::POS) {
1442 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1443 tj = ta = ta->
1444 remove_speculative()->
1445 cast_to_ptr_type(ptr)->
1446 with_ary(tary)->
1447 cast_to_exactness(false);
1448 }
1449 // Arrays of known objects become arrays of unknown objects.
1450 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1451 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1452 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1453 }
1454 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1455 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1456 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1457 }
1458 // Initially all flattened array accesses share a single slice
1459 if (ta->is_flat() && ta->elem() != TypeInstPtr::BOTTOM && _flat_accesses_share_alias) {
1460 const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size(), /* stable= */ false, /* flat= */ true);
1461 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), Type::Offset(Type::OffsetBot));
1462 }
1463 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1464 // cannot be distinguished by bytecode alone.
1465 if (ta->elem() == TypeInt::BOOL) {
1466 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1467 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1468 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,Type::Offset(offset), ta->field_offset());
1469 }
1470 // During the 2nd round of IterGVN, NotNull castings are removed.
1471 // Make sure the Bottom and NotNull variants alias the same.
1472 // Also, make sure exact and non-exact variants alias the same.
1473 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1474 tj = ta = ta->
1475 remove_speculative()->
1476 cast_to_ptr_type(TypePtr::BotPTR)->
1477 cast_to_exactness(false)->
1478 with_offset(offset);
1479 }
1480 }
1481
1482 // Oop pointers need some flattening
1483 const TypeInstPtr *to = tj->isa_instptr();
1484 if (to && to != TypeOopPtr::BOTTOM) {
1485 ciInstanceKlass* ik = to->instance_klass();
1486 if( ptr == TypePtr::Constant ) {
1487 if (ik != ciEnv::current()->Class_klass() ||
1488 offset < ik->layout_helper_size_in_bytes()) {
1498 } else if( is_known_inst ) {
1499 tj = to; // Keep NotNull and klass_is_exact for instance type
1500 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1501 // During the 2nd round of IterGVN, NotNull castings are removed.
1502 // Make sure the Bottom and NotNull variants alias the same.
1503 // Also, make sure exact and non-exact variants alias the same.
1504 tj = to = to->
1505 remove_speculative()->
1506 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1507 cast_to_ptr_type(TypePtr::BotPTR)->
1508 cast_to_exactness(false);
1509 }
1510 if (to->speculative() != nullptr) {
1511 tj = to = to->remove_speculative();
1512 }
1513 // Canonicalize the holder of this field
1514 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1515 // First handle header references such as a LoadKlassNode, even if the
1516 // object's klass is unloaded at compile time (4965979).
1517 if (!is_known_inst) { // Do it only for non-instance types
1518 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, Type::Offset(offset));
1519 }
1520 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1521 // Static fields are in the space above the normal instance
1522 // fields in the java.lang.Class instance.
1523 if (ik != ciEnv::current()->Class_klass()) {
1524 to = nullptr;
1525 tj = TypeOopPtr::BOTTOM;
1526 offset = tj->offset();
1527 }
1528 } else {
1529 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1530 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1531 assert(tj->offset() == offset, "no change to offset expected");
1532 bool xk = to->klass_is_exact();
1533 int instance_id = to->instance_id();
1534
1535 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1536 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1537 // its interfaces are included.
1538 if (xk && ik->equals(canonical_holder)) {
1539 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id), "exact type should be canonical type");
1540 } else {
1541 assert(xk || !is_known_inst, "Known instance should be exact type");
1542 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id);
1543 }
1544 }
1545 }
1546
1547 // Klass pointers to object array klasses need some flattening
1548 const TypeKlassPtr *tk = tj->isa_klassptr();
1549 if( tk ) {
1550 // If we are referencing a field within a Klass, we need
1551 // to assume the worst case of an Object. Both exact and
1552 // inexact types must flatten to the same alias class so
1553 // use NotNull as the PTR.
1554 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1555 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1556 env()->Object_klass(),
1557 Type::Offset(offset));
1558 }
1559
1560 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1561 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1562 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1563 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), Type::Offset(offset));
1564 } else {
1565 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_flat(), tk->is_null_free(), tk->is_atomic(), tk->is_aryklassptr()->is_vm_type());
1566 }
1567 }
1568 // Check for precise loads from the primary supertype array and force them
1569 // to the supertype cache alias index. Check for generic array loads from
1570 // the primary supertype array and also force them to the supertype cache
1571 // alias index. Since the same load can reach both, we need to merge
1572 // these 2 disparate memories into the same alias class. Since the
1573 // primary supertype array is read-only, there's no chance of confusion
1574 // where we bypass an array load and an array store.
1575 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1576 if (offset == Type::OffsetBot ||
1577 (offset >= primary_supers_offset &&
1578 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1579 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1580 offset = in_bytes(Klass::secondary_super_cache_offset());
1581 tj = tk = tk->with_offset(offset);
1582 }
1583 }
1584
1585 // Flatten all Raw pointers together.
1586 if (tj->base() == Type::RawPtr)
1587 tj = TypeRawPtr::BOTTOM;
1677 intptr_t key = (intptr_t) adr_type;
1678 key ^= key >> logAliasCacheSize;
1679 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1680 }
1681
1682
1683 //-----------------------------grow_alias_types--------------------------------
1684 void Compile::grow_alias_types() {
1685 const int old_ats = _max_alias_types; // how many before?
1686 const int new_ats = old_ats; // how many more?
1687 const int grow_ats = old_ats+new_ats; // how many now?
1688 _max_alias_types = grow_ats;
1689 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1690 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1691 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1692 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1693 }
1694
1695
1696 //--------------------------------find_alias_type------------------------------
1697 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1698 if (!do_aliasing()) {
1699 return alias_type(AliasIdxBot);
1700 }
1701
1702 AliasCacheEntry* ace = nullptr;
1703 if (!uncached) {
1704 ace = probe_alias_cache(adr_type);
1705 if (ace->_adr_type == adr_type) {
1706 return alias_type(ace->_index);
1707 }
1708 }
1709
1710 // Handle special cases.
1711 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1712 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1713
1714 // Do it the slow way.
1715 const TypePtr* flat = flatten_alias_type(adr_type);
1716
1717 #ifdef ASSERT
1718 {
1719 ResourceMark rm;
1720 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1721 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1722 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1723 Type::str(adr_type));
1724 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1725 const TypeOopPtr* foop = flat->is_oopptr();
1726 // Scalarizable allocations have exact klass always.
1727 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1737 if (alias_type(i)->adr_type() == flat) {
1738 idx = i;
1739 break;
1740 }
1741 }
1742
1743 if (idx == AliasIdxTop) {
1744 if (no_create) return nullptr;
1745 // Grow the array if necessary.
1746 if (_num_alias_types == _max_alias_types) grow_alias_types();
1747 // Add a new alias type.
1748 idx = _num_alias_types++;
1749 _alias_types[idx]->Init(idx, flat);
1750 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1751 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1752 if (flat->isa_instptr()) {
1753 if (flat->offset() == java_lang_Class::klass_offset()
1754 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1755 alias_type(idx)->set_rewritable(false);
1756 }
1757 ciField* field = nullptr;
1758 if (flat->isa_aryptr()) {
1759 #ifdef ASSERT
1760 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1761 // (T_BYTE has the weakest alignment and size restrictions...)
1762 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1763 #endif
1764 const Type* elemtype = flat->is_aryptr()->elem();
1765 if (flat->offset() == TypePtr::OffsetBot) {
1766 alias_type(idx)->set_element(elemtype);
1767 }
1768 int field_offset = flat->is_aryptr()->field_offset().get();
1769 if (flat->is_flat() &&
1770 field_offset != Type::OffsetBot) {
1771 ciInlineKlass* vk = elemtype->inline_klass();
1772 field_offset += vk->payload_offset();
1773 field = vk->get_field_by_offset(field_offset, false);
1774 }
1775 }
1776 if (flat->isa_klassptr()) {
1777 if (UseCompactObjectHeaders) {
1778 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1779 alias_type(idx)->set_rewritable(false);
1780 }
1781 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1782 alias_type(idx)->set_rewritable(false);
1783 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1784 alias_type(idx)->set_rewritable(false);
1785 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1786 alias_type(idx)->set_rewritable(false);
1787 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1788 alias_type(idx)->set_rewritable(false);
1789 if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1790 alias_type(idx)->set_rewritable(false);
1791 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1792 alias_type(idx)->set_rewritable(false);
1793 }
1794 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1795 // but the base pointer type is not distinctive enough to identify
1796 // references into JavaThread.)
1797
1798 // Check for final fields.
1799 const TypeInstPtr* tinst = flat->isa_instptr();
1800 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1801 if (tinst->const_oop() != nullptr &&
1802 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1803 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1804 // static field
1805 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1806 field = k->get_field_by_offset(tinst->offset(), true);
1807 } else if (tinst->is_inlinetypeptr()) {
1808 // Inline type field
1809 ciInlineKlass* vk = tinst->inline_klass();
1810 field = vk->get_field_by_offset(tinst->offset(), false);
1811 } else {
1812 ciInstanceKlass *k = tinst->instance_klass();
1813 field = k->get_field_by_offset(tinst->offset(), false);
1814 }
1815 }
1816 assert(field == nullptr ||
1817 original_field == nullptr ||
1818 (field->holder() == original_field->holder() &&
1819 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1820 field->is_static() == original_field->is_static()), "wrong field?");
1821 // Set field() and is_rewritable() attributes.
1822 if (field != nullptr) {
1823 alias_type(idx)->set_field(field);
1824 if (flat->isa_aryptr()) {
1825 // Fields of flat arrays are rewritable although they are declared final
1826 assert(flat->is_flat(), "must be a flat array");
1827 alias_type(idx)->set_rewritable(true);
1828 }
1829 }
1830 }
1831
1832 // Fill the cache for next time.
1833 if (!uncached) {
1834 ace->_adr_type = adr_type;
1835 ace->_index = idx;
1836 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1837
1838 // Might as well try to fill the cache for the flattened version, too.
1839 AliasCacheEntry* face = probe_alias_cache(flat);
1840 if (face->_adr_type == nullptr) {
1841 face->_adr_type = flat;
1842 face->_index = idx;
1843 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1844 }
1845 }
1846
1847 return alias_type(idx);
1848 }
1849
1850
1851 Compile::AliasType* Compile::alias_type(ciField* field) {
1852 const TypeOopPtr* t;
1853 if (field->is_static())
1854 t = TypeInstPtr::make(field->holder()->java_mirror());
1855 else
1856 t = TypeOopPtr::make_from_klass_raw(field->holder());
1857 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1858 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1859 return atp;
1860 }
1861
1862
1863 //------------------------------have_alias_type--------------------------------
1864 bool Compile::have_alias_type(const TypePtr* adr_type) {
1946 assert(!C->major_progress(), "not cleared");
1947
1948 if (_for_post_loop_igvn.length() > 0) {
1949 while (_for_post_loop_igvn.length() > 0) {
1950 Node* n = _for_post_loop_igvn.pop();
1951 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1952 igvn._worklist.push(n);
1953 }
1954 igvn.optimize();
1955 if (failing()) return;
1956 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1957 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1958
1959 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1960 if (C->major_progress()) {
1961 C->clear_major_progress(); // ensure that major progress is now clear
1962 }
1963 }
1964 }
1965
1966 void Compile::add_inline_type(Node* n) {
1967 assert(n->is_InlineType(), "unexpected node");
1968 _inline_type_nodes.push(n);
1969 }
1970
1971 void Compile::remove_inline_type(Node* n) {
1972 assert(n->is_InlineType(), "unexpected node");
1973 if (_inline_type_nodes.contains(n)) {
1974 _inline_type_nodes.remove(n);
1975 }
1976 }
1977
1978 // Does the return value keep otherwise useless inline type allocations alive?
1979 static bool return_val_keeps_allocations_alive(Node* ret_val) {
1980 ResourceMark rm;
1981 Unique_Node_List wq;
1982 wq.push(ret_val);
1983 bool some_allocations = false;
1984 for (uint i = 0; i < wq.size(); i++) {
1985 Node* n = wq.at(i);
1986 if (n->outcnt() > 1) {
1987 // Some other use for the allocation
1988 return false;
1989 } else if (n->is_InlineType()) {
1990 wq.push(n->in(1));
1991 } else if (n->is_Phi()) {
1992 for (uint j = 1; j < n->req(); j++) {
1993 wq.push(n->in(j));
1994 }
1995 } else if (n->is_CheckCastPP() &&
1996 n->in(1)->is_Proj() &&
1997 n->in(1)->in(0)->is_Allocate()) {
1998 some_allocations = true;
1999 } else if (n->is_CheckCastPP()) {
2000 wq.push(n->in(1));
2001 }
2002 }
2003 return some_allocations;
2004 }
2005
2006 void Compile::process_inline_types(PhaseIterGVN &igvn, bool remove) {
2007 // Make sure that the return value does not keep an otherwise unused allocation alive
2008 if (tf()->returns_inline_type_as_fields()) {
2009 Node* ret = nullptr;
2010 for (uint i = 1; i < root()->req(); i++) {
2011 Node* in = root()->in(i);
2012 if (in->Opcode() == Op_Return) {
2013 assert(ret == nullptr, "only one return");
2014 ret = in;
2015 }
2016 }
2017 if (ret != nullptr) {
2018 Node* ret_val = ret->in(TypeFunc::Parms);
2019 if (igvn.type(ret_val)->isa_oopptr() &&
2020 return_val_keeps_allocations_alive(ret_val)) {
2021 igvn.replace_input_of(ret, TypeFunc::Parms, InlineTypeNode::tagged_klass(igvn.type(ret_val)->inline_klass(), igvn));
2022 assert(ret_val->outcnt() == 0, "should be dead now");
2023 igvn.remove_dead_node(ret_val);
2024 }
2025 }
2026 }
2027 if (_inline_type_nodes.length() == 0) {
2028 return;
2029 }
2030 // Scalarize inline types in safepoint debug info.
2031 // Delay this until all inlining is over to avoid getting inconsistent debug info.
2032 set_scalarize_in_safepoints(true);
2033 for (int i = _inline_type_nodes.length()-1; i >= 0; i--) {
2034 InlineTypeNode* vt = _inline_type_nodes.at(i)->as_InlineType();
2035 vt->make_scalar_in_safepoints(&igvn);
2036 igvn.record_for_igvn(vt);
2037 }
2038 if (remove) {
2039 // Remove inline type nodes by replacing them with their oop input
2040 while (_inline_type_nodes.length() > 0) {
2041 InlineTypeNode* vt = _inline_type_nodes.pop()->as_InlineType();
2042 if (vt->outcnt() == 0) {
2043 igvn.remove_dead_node(vt);
2044 continue;
2045 }
2046 for (DUIterator i = vt->outs(); vt->has_out(i); i++) {
2047 DEBUG_ONLY(bool must_be_buffered = false);
2048 Node* u = vt->out(i);
2049 // Check if any users are blackholes. If so, rewrite them to use either the
2050 // allocated buffer, or individual components, instead of the inline type node
2051 // that goes away.
2052 if (u->is_Blackhole()) {
2053 BlackholeNode* bh = u->as_Blackhole();
2054
2055 // Unlink the old input
2056 int idx = bh->find_edge(vt);
2057 assert(idx != -1, "The edge should be there");
2058 bh->del_req(idx);
2059 --i;
2060
2061 if (vt->is_allocated(&igvn)) {
2062 // Already has the allocated instance, blackhole that
2063 bh->add_req(vt->get_oop());
2064 } else {
2065 // Not allocated yet, blackhole the components
2066 for (uint c = 0; c < vt->field_count(); c++) {
2067 bh->add_req(vt->field_value(c));
2068 }
2069 }
2070
2071 // Node modified, record for IGVN
2072 igvn.record_for_igvn(bh);
2073 }
2074 #ifdef ASSERT
2075 // Verify that inline type is buffered when replacing by oop
2076 else if (u->is_InlineType()) {
2077 // InlineType uses don't need buffering because they are about to be replaced as well
2078 } else if (u->is_Phi()) {
2079 // TODO 8302217 Remove this once InlineTypeNodes are reliably pushed through
2080 } else {
2081 must_be_buffered = true;
2082 }
2083 if (must_be_buffered && !vt->is_allocated(&igvn)) {
2084 vt->dump(0);
2085 u->dump(0);
2086 assert(false, "Should have been buffered");
2087 }
2088 #endif
2089 }
2090 igvn.replace_node(vt, vt->get_oop());
2091 }
2092 }
2093 igvn.optimize();
2094 }
2095
2096 void Compile::adjust_flat_array_access_aliases(PhaseIterGVN& igvn) {
2097 if (!_has_flat_accesses) {
2098 return;
2099 }
2100 // Initially, all flat array accesses share the same slice to
2101 // keep dependencies with Object[] array accesses (that could be
2102 // to a flat array) correct. We're done with parsing so we
2103 // now know all flat array accesses in this compile
2104 // unit. Let's move flat array accesses to their own slice,
2105 // one per element field. This should help memory access
2106 // optimizations.
2107 ResourceMark rm;
2108 Unique_Node_List wq;
2109 wq.push(root());
2110
2111 Node_List mergememnodes;
2112 Node_List memnodes;
2113
2114 // Alias index currently shared by all flat memory accesses
2115 int index = get_alias_index(TypeAryPtr::INLINES);
2116
2117 // Find MergeMem nodes and flat array accesses
2118 for (uint i = 0; i < wq.size(); i++) {
2119 Node* n = wq.at(i);
2120 if (n->is_Mem()) {
2121 const TypePtr* adr_type = nullptr;
2122 adr_type = get_adr_type(get_alias_index(n->adr_type()));
2123 if (adr_type == TypeAryPtr::INLINES) {
2124 memnodes.push(n);
2125 }
2126 } else if (n->is_MergeMem()) {
2127 MergeMemNode* mm = n->as_MergeMem();
2128 if (mm->memory_at(index) != mm->base_memory()) {
2129 mergememnodes.push(n);
2130 }
2131 }
2132 for (uint j = 0; j < n->req(); j++) {
2133 Node* m = n->in(j);
2134 if (m != nullptr) {
2135 wq.push(m);
2136 }
2137 }
2138 }
2139
2140 if (memnodes.size() > 0) {
2141 _flat_accesses_share_alias = false;
2142
2143 // We are going to change the slice for the flat array
2144 // accesses so we need to clear the cache entries that refer to
2145 // them.
2146 for (uint i = 0; i < AliasCacheSize; i++) {
2147 AliasCacheEntry* ace = &_alias_cache[i];
2148 if (ace->_adr_type != nullptr &&
2149 ace->_adr_type->is_flat()) {
2150 ace->_adr_type = nullptr;
2151 ace->_index = (i != 0) ? 0 : AliasIdxTop; // Make sure the nullptr adr_type resolves to AliasIdxTop
2152 }
2153 }
2154
2155 // Find what aliases we are going to add
2156 int start_alias = num_alias_types()-1;
2157 int stop_alias = 0;
2158
2159 for (uint i = 0; i < memnodes.size(); i++) {
2160 Node* m = memnodes.at(i);
2161 const TypePtr* adr_type = nullptr;
2162 adr_type = m->adr_type();
2163 #ifdef ASSERT
2164 m->as_Mem()->set_adr_type(adr_type);
2165 #endif
2166 int idx = get_alias_index(adr_type);
2167 start_alias = MIN2(start_alias, idx);
2168 stop_alias = MAX2(stop_alias, idx);
2169 }
2170
2171 assert(stop_alias >= start_alias, "should have expanded aliases");
2172
2173 Node_Stack stack(0);
2174 #ifdef ASSERT
2175 VectorSet seen(Thread::current()->resource_area());
2176 #endif
2177 // Now let's fix the memory graph so each flat array access
2178 // is moved to the right slice. Start from the MergeMem nodes.
2179 uint last = unique();
2180 for (uint i = 0; i < mergememnodes.size(); i++) {
2181 MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2182 Node* n = current->memory_at(index);
2183 MergeMemNode* mm = nullptr;
2184 do {
2185 // Follow memory edges through memory accesses, phis and
2186 // narrow membars and push nodes on the stack. Once we hit
2187 // bottom memory, we pop element off the stack one at a
2188 // time, in reverse order, and move them to the right slice
2189 // by changing their memory edges.
2190 if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() || n->adr_type() == TypeAryPtr::INLINES) {
2191 assert(!seen.test_set(n->_idx), "");
2192 // Uses (a load for instance) will need to be moved to the
2193 // right slice as well and will get a new memory state
2194 // that we don't know yet. The use could also be the
2195 // backedge of a loop. We put a place holder node between
2196 // the memory node and its uses. We replace that place
2197 // holder with the correct memory state once we know it,
2198 // i.e. when nodes are popped off the stack. Using the
2199 // place holder make the logic work in the presence of
2200 // loops.
2201 if (n->outcnt() > 1) {
2202 Node* place_holder = nullptr;
2203 assert(!n->has_out_with(Op_Node), "");
2204 for (DUIterator k = n->outs(); n->has_out(k); k++) {
2205 Node* u = n->out(k);
2206 if (u != current && u->_idx < last) {
2207 bool success = false;
2208 for (uint l = 0; l < u->req(); l++) {
2209 if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2210 continue;
2211 }
2212 Node* in = u->in(l);
2213 if (in == n) {
2214 if (place_holder == nullptr) {
2215 place_holder = new Node(1);
2216 place_holder->init_req(0, n);
2217 }
2218 igvn.replace_input_of(u, l, place_holder);
2219 success = true;
2220 }
2221 }
2222 if (success) {
2223 --k;
2224 }
2225 }
2226 }
2227 }
2228 if (n->is_Phi()) {
2229 stack.push(n, 1);
2230 n = n->in(1);
2231 } else if (n->is_Mem()) {
2232 stack.push(n, n->req());
2233 n = n->in(MemNode::Memory);
2234 } else {
2235 assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2236 stack.push(n, n->req());
2237 n = n->in(0)->in(TypeFunc::Memory);
2238 }
2239 } else {
2240 assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || (n->is_Proj() && n->in(0)->is_Initialize()), "");
2241 // Build a new MergeMem node to carry the new memory state
2242 // as we build it. IGVN should fold extraneous MergeMem
2243 // nodes.
2244 mm = MergeMemNode::make(n);
2245 igvn.register_new_node_with_optimizer(mm);
2246 while (stack.size() > 0) {
2247 Node* m = stack.node();
2248 uint idx = stack.index();
2249 if (m->is_Mem()) {
2250 // Move memory node to its new slice
2251 const TypePtr* adr_type = m->adr_type();
2252 int alias = get_alias_index(adr_type);
2253 Node* prev = mm->memory_at(alias);
2254 igvn.replace_input_of(m, MemNode::Memory, prev);
2255 mm->set_memory_at(alias, m);
2256 } else if (m->is_Phi()) {
2257 // We need as many new phis as there are new aliases
2258 igvn.replace_input_of(m, idx, mm);
2259 if (idx == m->req()-1) {
2260 Node* r = m->in(0);
2261 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2262 const TypePtr* adr_type = get_adr_type(j);
2263 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2264 continue;
2265 }
2266 Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2267 igvn.register_new_node_with_optimizer(phi);
2268 for (uint k = 1; k < m->req(); k++) {
2269 phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2270 }
2271 mm->set_memory_at(j, phi);
2272 }
2273 Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2274 igvn.register_new_node_with_optimizer(base_phi);
2275 for (uint k = 1; k < m->req(); k++) {
2276 base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2277 }
2278 mm->set_base_memory(base_phi);
2279 }
2280 } else {
2281 // This is a MemBarCPUOrder node from
2282 // Parse::array_load()/Parse::array_store(), in the
2283 // branch that handles flat arrays hidden under
2284 // an Object[] array. We also need one new membar per
2285 // new alias to keep the unknown access that the
2286 // membars protect properly ordered with accesses to
2287 // known flat array.
2288 assert(m->is_Proj(), "projection expected");
2289 Node* ctrl = m->in(0)->in(TypeFunc::Control);
2290 igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2291 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2292 const TypePtr* adr_type = get_adr_type(j);
2293 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2294 continue;
2295 }
2296 MemBarNode* mb = new MemBarCPUOrderNode(this, j, nullptr);
2297 igvn.register_new_node_with_optimizer(mb);
2298 Node* mem = mm->memory_at(j);
2299 mb->init_req(TypeFunc::Control, ctrl);
2300 mb->init_req(TypeFunc::Memory, mem);
2301 ctrl = new ProjNode(mb, TypeFunc::Control);
2302 igvn.register_new_node_with_optimizer(ctrl);
2303 mem = new ProjNode(mb, TypeFunc::Memory);
2304 igvn.register_new_node_with_optimizer(mem);
2305 mm->set_memory_at(j, mem);
2306 }
2307 igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2308 }
2309 if (idx < m->req()-1) {
2310 idx += 1;
2311 stack.set_index(idx);
2312 n = m->in(idx);
2313 break;
2314 }
2315 // Take care of place holder nodes
2316 if (m->has_out_with(Op_Node)) {
2317 Node* place_holder = m->find_out_with(Op_Node);
2318 if (place_holder != nullptr) {
2319 Node* mm_clone = mm->clone();
2320 igvn.register_new_node_with_optimizer(mm_clone);
2321 Node* hook = new Node(1);
2322 hook->init_req(0, mm);
2323 igvn.replace_node(place_holder, mm_clone);
2324 hook->destruct(&igvn);
2325 }
2326 assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2327 }
2328 stack.pop();
2329 }
2330 }
2331 } while(stack.size() > 0);
2332 // Fix the memory state at the MergeMem we started from
2333 igvn.rehash_node_delayed(current);
2334 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2335 const TypePtr* adr_type = get_adr_type(j);
2336 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2337 continue;
2338 }
2339 current->set_memory_at(j, mm);
2340 }
2341 current->set_memory_at(index, current->base_memory());
2342 }
2343 igvn.optimize();
2344 }
2345 print_method(PHASE_SPLIT_INLINES_ARRAY, 2);
2346 #ifdef ASSERT
2347 if (!_flat_accesses_share_alias) {
2348 wq.clear();
2349 wq.push(root());
2350 for (uint i = 0; i < wq.size(); i++) {
2351 Node* n = wq.at(i);
2352 assert(n->adr_type() != TypeAryPtr::INLINES, "should have been removed from the graph");
2353 for (uint j = 0; j < n->req(); j++) {
2354 Node* m = n->in(j);
2355 if (m != nullptr) {
2356 wq.push(m);
2357 }
2358 }
2359 }
2360 }
2361 #endif
2362 }
2363
2364 void Compile::record_for_merge_stores_igvn(Node* n) {
2365 if (!n->for_merge_stores_igvn()) {
2366 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
2367 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2368 _for_merge_stores_igvn.append(n);
2369 }
2370 }
2371
2372 void Compile::remove_from_merge_stores_igvn(Node* n) {
2373 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2374 _for_merge_stores_igvn.remove(n);
2375 }
2376
2377 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
2378 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
2379 // the stores, and we merge the wrong sequence of stores.
2380 // Example:
2381 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
2382 // Apply MergeStores:
2383 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
2462 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
2463 Bytecodes::Code c = iter.cur_bc();
2464 Node* lhs = nullptr;
2465 Node* rhs = nullptr;
2466 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
2467 lhs = unc->peek_operand(0);
2468 rhs = unc->peek_operand(1);
2469 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2470 lhs = unc->peek_operand(0);
2471 }
2472
2473 ResourceMark rm;
2474 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2475 assert(live_locals.is_valid(), "broken liveness info");
2476 int len = (int)live_locals.size();
2477
2478 for (int i = 0; i < len; i++) {
2479 Node* local = unc->local(jvms, i);
2480 // kill local using the liveness of next_bci.
2481 // give up when the local looks like an operand to secure reexecution.
2482 if (!live_locals.at(i) && !local->is_top() && local != lhs && local != rhs) {
2483 uint idx = jvms->locoff() + i;
2484 #ifdef ASSERT
2485 if (PrintOpto && Verbose) {
2486 tty->print("[unstable_if] kill local#%d: ", idx);
2487 local->dump();
2488 tty->cr();
2489 }
2490 #endif
2491 igvn.replace_input_of(unc, idx, top());
2492 modified = true;
2493 }
2494 }
2495 }
2496
2497 // keep the modified trap for late query
2498 if (modified) {
2499 trap->set_modified();
2500 } else {
2501 _unstable_if_traps.delete_at(i);
2502 }
2503 }
2504 igvn.optimize();
2505 }
2506
2507 // StringOpts and late inlining of string methods
2508 void Compile::inline_string_calls(bool parse_time) {
2509 {
2510 // remove useless nodes to make the usage analysis simpler
2511 ResourceMark rm;
2512 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2513 }
2514
2515 {
2516 ResourceMark rm;
2517 print_method(PHASE_BEFORE_STRINGOPTS, 3);
2683
2684 if (_string_late_inlines.length() > 0) {
2685 assert(has_stringbuilder(), "inconsistent");
2686
2687 inline_string_calls(false);
2688
2689 if (failing()) return;
2690
2691 inline_incrementally_cleanup(igvn);
2692 }
2693
2694 set_inlining_incrementally(false);
2695 }
2696
2697 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2698 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2699 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2700 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2701 // as if "inlining_incrementally() == true" were set.
2702 assert(inlining_incrementally() == false, "not allowed");
2703 #ifdef ASSERT
2704 Unique_Node_List* modified_nodes = _modified_nodes;
2705 _modified_nodes = nullptr;
2706 #endif
2707 assert(_late_inlines.length() > 0, "sanity");
2708
2709 while (_late_inlines.length() > 0) {
2710 igvn_worklist()->ensure_empty(); // should be done with igvn
2711
2712 while (inline_incrementally_one()) {
2713 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2714 }
2715 if (failing()) return;
2716
2717 inline_incrementally_cleanup(igvn);
2718 }
2719 DEBUG_ONLY( _modified_nodes = modified_nodes; )
2720 }
2721
2722 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2723 if (_loop_opts_cnt > 0) {
2724 while (major_progress() && (_loop_opts_cnt > 0)) {
2725 TracePhase tp(_t_idealLoop);
2726 PhaseIdealLoop::optimize(igvn, mode);
2727 _loop_opts_cnt--;
2728 if (failing()) return false;
2729 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2730 }
2731 }
2732 return true;
2733 }
2734
2735 // Remove edges from "root" to each SafePoint at a backward branch.
2736 // They were inserted during parsing (see add_safepoint()) to make
2737 // infinite loops without calls or exceptions visible to root, i.e.,
2738 // useful.
2739 void Compile::remove_root_to_sfpts_edges(PhaseIterGVN& igvn) {
2843 print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
2844 }
2845 assert(!has_vbox_nodes(), "sanity");
2846
2847 if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2848 Compile::TracePhase tp(_t_renumberLive);
2849 igvn_worklist()->ensure_empty(); // should be done with igvn
2850 {
2851 ResourceMark rm;
2852 PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
2853 }
2854 igvn.reset();
2855 igvn.optimize();
2856 if (failing()) return;
2857 }
2858
2859 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2860 // safepoints
2861 remove_root_to_sfpts_edges(igvn);
2862
2863 // Process inline type nodes now that all inlining is over
2864 process_inline_types(igvn);
2865
2866 adjust_flat_array_access_aliases(igvn);
2867
2868 if (failing()) return;
2869
2870 if (C->macro_count() > 0) {
2871 // Eliminate some macro nodes before EA to reduce analysis pressure
2872 PhaseMacroExpand mexp(igvn);
2873 mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
2874 if (failing()) {
2875 return;
2876 }
2877 igvn.set_delay_transform(false);
2878 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2879 }
2880
2881 if (has_loops()) {
2882 print_method(PHASE_BEFORE_LOOP_OPTS, 2);
2883 }
2884
2885 // Perform escape analysis
2886 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2887 if (has_loops()) {
2888 // Cleanup graph (remove dead nodes).
2889 TracePhase tp(_t_idealLoop);
2890 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2891 if (failing()) {
2892 return;
2893 }
2894 print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2895 if (C->macro_count() > 0) {
2896 // Eliminate some macro nodes before EA to reduce analysis pressure
2897 PhaseMacroExpand mexp(igvn);
2898 mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
2899 if (failing()) {
2900 return;
2901 }
2902 igvn.set_delay_transform(false);
2903 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2904 }
2905 }
2906
2907 bool progress;
2908 do {
2909 ConnectionGraph::do_analysis(this, &igvn);
2910
2911 if (failing()) return;
2912
2913 int mcount = macro_count(); // Record number of allocations and locks before IGVN
2914
2915 // Optimize out fields loads from scalar replaceable allocations.
2916 igvn.optimize();
2917 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2918
2919 if (failing()) return;
2920
2921 if (congraph() != nullptr && macro_count() > 0) {
2922 TracePhase tp(_t_macroEliminate);
2923 PhaseMacroExpand mexp(igvn);
2924 mexp.eliminate_macro_nodes();
2925 if (failing()) {
2926 return;
2927 }
2928 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2929
2930 igvn.set_delay_transform(false);
2931 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2932 }
2933
2934 ConnectionGraph::verify_ram_nodes(this, root());
2935 if (failing()) return;
2936
2937 progress = do_iterative_escape_analysis() &&
2938 (macro_count() < mcount) &&
2939 ConnectionGraph::has_candidates(this);
2940 // Try again if candidates exist and made progress
2941 // by removing some allocations and/or locks.
2942 } while (progress);
2943 }
2944
2945 // Loop transforms on the ideal graph. Range Check Elimination,
2946 // peeling, unrolling, etc.
2947
2948 // Set loop opts counter
2949 if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2950 {
3001 // Loop transforms on the ideal graph. Range Check Elimination,
3002 // peeling, unrolling, etc.
3003 if (!optimize_loops(igvn, LoopOptsDefault)) {
3004 return;
3005 }
3006
3007 if (failing()) return;
3008
3009 C->clear_major_progress(); // ensure that major progress is now clear
3010
3011 process_for_post_loop_opts_igvn(igvn);
3012
3013 process_for_merge_stores_igvn(igvn);
3014
3015 if (failing()) return;
3016
3017 #ifdef ASSERT
3018 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
3019 #endif
3020
3021 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
3022
3023 if (_late_inlines.length() > 0) {
3024 // More opportunities to optimize virtual and MH calls.
3025 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
3026 process_late_inline_calls_no_inline(igvn);
3027 }
3028
3029 {
3030 TracePhase tp(_t_macroExpand);
3031 PhaseMacroExpand mex(igvn);
3032 // Last attempt to eliminate macro nodes.
3033 mex.eliminate_macro_nodes();
3034 if (failing()) {
3035 return;
3036 }
3037
3038 print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);
3039 // Do not allow new macro nodes once we start to eliminate and expand
3040 C->reset_allow_macro_nodes();
3041 // Last attempt to eliminate macro nodes before expand
3042 mex.eliminate_macro_nodes();
3043 if (failing()) {
3044 return;
3045 }
3046 mex.eliminate_opaque_looplimit_macro_nodes();
3047 if (failing()) {
3048 return;
3049 }
3050 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
3051 if (mex.expand_macro_nodes()) {
3052 assert(failing(), "must bail out w/ explicit message");
3053 return;
3054 }
3055 print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
3056 }
3057
3058 // Process inline type nodes again and remove them. From here
3059 // on we don't need to keep track of field values anymore.
3060 process_inline_types(igvn, /* remove= */ true);
3061
3062 {
3063 TracePhase tp(_t_barrierExpand);
3064 if (bs->expand_barriers(this, igvn)) {
3065 assert(failing(), "must bail out w/ explicit message");
3066 return;
3067 }
3068 print_method(PHASE_BARRIER_EXPANSION, 2);
3069 }
3070
3071 if (C->max_vector_size() > 0) {
3072 C->optimize_logic_cones(igvn);
3073 igvn.optimize();
3074 if (failing()) return;
3075 }
3076
3077 DEBUG_ONLY( _modified_nodes = nullptr; )
3078 DEBUG_ONLY( _late_inlines.clear(); )
3079
3080 assert(igvn._worklist.size() == 0, "not empty");
3081 } // (End scope of igvn; run destructor if necessary for asserts.)
3082
3083 check_no_dead_use();
3084
3085 // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
3086 // to remove hashes to unlock nodes for modifications.
3087 C->node_hash()->clear();
3088
3089 // A method with only infinite loops has no edges entering loops from root
3090 {
3091 TracePhase tp(_t_graphReshaping);
3092 if (final_graph_reshaping()) {
3093 assert(failing(), "must bail out w/ explicit message");
3094 return;
3095 }
3096 }
3097
3098 print_method(PHASE_OPTIMIZE_FINISHED, 2);
3099 DEBUG_ONLY(set_phase_optimize_finished();)
3100 }
3806 case Op_CmpD3:
3807 case Op_StoreD:
3808 case Op_LoadD:
3809 case Op_LoadD_unaligned:
3810 frc.inc_double_count();
3811 break;
3812 case Op_Opaque1: // Remove Opaque Nodes before matching
3813 n->subsume_by(n->in(1), this);
3814 break;
3815 case Op_CallLeafPure: {
3816 // If the pure call is not supported, then lower to a CallLeaf.
3817 if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
3818 CallNode* call = n->as_Call();
3819 CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
3820 call->_name, TypeRawPtr::BOTTOM);
3821 new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
3822 new_call->init_req(TypeFunc::I_O, C->top());
3823 new_call->init_req(TypeFunc::Memory, C->top());
3824 new_call->init_req(TypeFunc::ReturnAdr, C->top());
3825 new_call->init_req(TypeFunc::FramePtr, C->top());
3826 for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain_sig()->cnt(); i++) {
3827 new_call->init_req(i, call->in(i));
3828 }
3829 n->subsume_by(new_call, this);
3830 }
3831 frc.inc_call_count();
3832 break;
3833 }
3834 case Op_CallStaticJava:
3835 case Op_CallJava:
3836 case Op_CallDynamicJava:
3837 frc.inc_java_call_count(); // Count java call site;
3838 case Op_CallRuntime:
3839 case Op_CallLeaf:
3840 case Op_CallLeafVector:
3841 case Op_CallLeafNoFP: {
3842 assert (n->is_Call(), "");
3843 CallNode *call = n->as_Call();
3844 // Count call sites where the FP mode bit would have to be flipped.
3845 // Do not count uncommon runtime calls:
3846 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
3852 int nop = n->Opcode();
3853 // Clone shared simple arguments to uncommon calls, item (1).
3854 if (n->outcnt() > 1 &&
3855 !n->is_Proj() &&
3856 nop != Op_CreateEx &&
3857 nop != Op_CheckCastPP &&
3858 nop != Op_DecodeN &&
3859 nop != Op_DecodeNKlass &&
3860 !n->is_Mem() &&
3861 !n->is_Phi()) {
3862 Node *x = n->clone();
3863 call->set_req(TypeFunc::Parms, x);
3864 }
3865 }
3866 break;
3867 }
3868 case Op_StoreB:
3869 case Op_StoreC:
3870 case Op_StoreI:
3871 case Op_StoreL:
3872 case Op_StoreLSpecial:
3873 case Op_CompareAndSwapB:
3874 case Op_CompareAndSwapS:
3875 case Op_CompareAndSwapI:
3876 case Op_CompareAndSwapL:
3877 case Op_CompareAndSwapP:
3878 case Op_CompareAndSwapN:
3879 case Op_WeakCompareAndSwapB:
3880 case Op_WeakCompareAndSwapS:
3881 case Op_WeakCompareAndSwapI:
3882 case Op_WeakCompareAndSwapL:
3883 case Op_WeakCompareAndSwapP:
3884 case Op_WeakCompareAndSwapN:
3885 case Op_CompareAndExchangeB:
3886 case Op_CompareAndExchangeS:
3887 case Op_CompareAndExchangeI:
3888 case Op_CompareAndExchangeL:
3889 case Op_CompareAndExchangeP:
3890 case Op_CompareAndExchangeN:
3891 case Op_GetAndAddS:
3892 case Op_GetAndAddB:
4405 k->subsume_by(m, this);
4406 }
4407 }
4408 }
4409 break;
4410 }
4411 case Op_CmpUL: {
4412 if (!Matcher::has_match_rule(Op_CmpUL)) {
4413 // No support for unsigned long comparisons
4414 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
4415 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
4416 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
4417 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
4418 Node* andl = new AndLNode(orl, remove_sign_mask);
4419 Node* cmp = new CmpLNode(andl, n->in(2));
4420 n->subsume_by(cmp, this);
4421 }
4422 break;
4423 }
4424 #ifdef ASSERT
4425 case Op_InlineType: {
4426 n->dump(-1);
4427 assert(false, "inline type node was not removed");
4428 break;
4429 }
4430 case Op_ConNKlass: {
4431 const TypePtr* tp = n->as_Type()->type()->make_ptr();
4432 ciKlass* klass = tp->is_klassptr()->exact_klass();
4433 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
4434 break;
4435 }
4436 #endif
4437 default:
4438 assert(!n->is_Call(), "");
4439 assert(!n->is_Mem(), "");
4440 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4441 break;
4442 }
4443 }
4444
4445 //------------------------------final_graph_reshaping_walk---------------------
4446 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4447 // requires that the walk visits a node's inputs before visiting the node.
4448 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
4449 Unique_Node_List sfpt;
4785 }
4786 }
4787
4788 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4789 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4790 }
4791
4792 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4793 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4794 }
4795
4796 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4797 if (holder->is_initialized()) {
4798 return false;
4799 }
4800 if (holder->is_being_initialized()) {
4801 if (accessing_method->holder() == holder) {
4802 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4803 // <init>, or a static method. In all those cases, there was an initialization
4804 // barrier on the holder klass passed.
4805 if (accessing_method->is_class_initializer() ||
4806 accessing_method->is_object_constructor() ||
4807 accessing_method->is_static()) {
4808 return false;
4809 }
4810 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4811 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4812 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4813 // child class can become fully initialized while its parent class is still being initialized.
4814 if (accessing_method->is_class_initializer()) {
4815 return false;
4816 }
4817 }
4818 ciMethod* root = method(); // the root method of compilation
4819 if (root != accessing_method) {
4820 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4821 }
4822 }
4823 return true;
4824 }
4825
4826 #ifndef PRODUCT
4827 //------------------------------verify_bidirectional_edges---------------------
4828 // For each input edge to a node (ie - for each Use-Def edge), verify that
4829 // there is a corresponding Def-Use edge.
4830 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4831 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4832 uint stack_size = live_nodes() >> 4;
4833 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4834 if (root_and_safepoints != nullptr) {
4864 if (in != nullptr && !in->is_top()) {
4865 // Count instances of `next`
4866 int cnt = 0;
4867 for (uint idx = 0; idx < in->_outcnt; idx++) {
4868 if (in->_out[idx] == n) {
4869 cnt++;
4870 }
4871 }
4872 assert(cnt > 0, "Failed to find Def-Use edge.");
4873 // Check for duplicate edges
4874 // walk the input array downcounting the input edges to n
4875 for (uint j = 0; j < length; j++) {
4876 if (n->in(j) == in) {
4877 cnt--;
4878 }
4879 }
4880 assert(cnt == 0, "Mismatched edge count.");
4881 } else if (in == nullptr) {
4882 assert(i == 0 || i >= n->req() ||
4883 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4884 (n->is_Allocate() && i >= AllocateNode::InlineType) ||
4885 (n->is_Unlock() && i == (n->req() - 1)) ||
4886 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4887 "only region, phi, arraycopy, allocate, unlock or membar nodes have null data edges");
4888 } else {
4889 assert(in->is_top(), "sanity");
4890 // Nothing to check.
4891 }
4892 }
4893 }
4894 }
4895
4896 //------------------------------verify_graph_edges---------------------------
4897 // Walk the Graph and verify that there is a one-to-one correspondence
4898 // between Use-Def edges and Def-Use edges in the graph.
4899 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4900 if (VerifyGraphEdges) {
4901 Unique_Node_List visited;
4902
4903 // Call graph walk to check edges
4904 verify_bidirectional_edges(visited, root_and_safepoints);
4905 if (no_dead_code) {
4906 // Now make sure that no visited node is used by an unvisited node.
4907 bool dead_nodes = false;
5018 // (1) subklass is already limited to a subtype of superklass => always ok
5019 // (2) subklass does not overlap with superklass => always fail
5020 // (3) superklass has NO subtypes and we can check with a simple compare.
5021 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
5022 if (skip) {
5023 return SSC_full_test; // Let caller generate the general case.
5024 }
5025
5026 if (subk->is_java_subtype_of(superk)) {
5027 return SSC_always_true; // (0) and (1) this test cannot fail
5028 }
5029
5030 if (!subk->maybe_java_subtype_of(superk)) {
5031 return SSC_always_false; // (2) true path dead; no dynamic test needed
5032 }
5033
5034 const Type* superelem = superk;
5035 if (superk->isa_aryklassptr()) {
5036 int ignored;
5037 superelem = superk->is_aryklassptr()->base_element_type(ignored);
5038
5039 // Do not fold the subtype check to an array klass pointer comparison for null-able inline type arrays
5040 // because null-free [LMyValue <: null-able [LMyValue but the klasses are different. Perform a full test.
5041 if (!superk->is_aryklassptr()->is_null_free() && superk->is_aryklassptr()->elem()->isa_instklassptr() &&
5042 superk->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->is_inlinetype()) {
5043 return SSC_full_test;
5044 }
5045 }
5046
5047 if (superelem->isa_instklassptr()) {
5048 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
5049 if (!ik->has_subklass()) {
5050 if (!ik->is_final()) {
5051 // Add a dependency if there is a chance of a later subclass.
5052 dependencies()->assert_leaf_type(ik);
5053 }
5054 if (!superk->maybe_java_subtype_of(subk)) {
5055 return SSC_always_false;
5056 }
5057 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5058 }
5059 } else {
5060 // A primitive array type has no subtypes.
5061 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5062 }
5063
5064 return SSC_full_test;
5508 const Type* t = igvn.type_or_null(n);
5509 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
5510 if (n->is_Type()) {
5511 t = n->as_Type()->type();
5512 assert(t == t->remove_speculative(), "no more speculative types");
5513 }
5514 // Iterate over outs - endless loops is unreachable from below
5515 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
5516 Node *m = n->fast_out(i);
5517 if (not_a_node(m)) {
5518 continue;
5519 }
5520 worklist.push(m);
5521 }
5522 }
5523 igvn.check_no_speculative_types();
5524 #endif
5525 }
5526 }
5527
5528 Node* Compile::optimize_acmp(PhaseGVN* phase, Node* a, Node* b) {
5529 const TypeInstPtr* ta = phase->type(a)->isa_instptr();
5530 const TypeInstPtr* tb = phase->type(b)->isa_instptr();
5531 if (!EnableValhalla || ta == nullptr || tb == nullptr ||
5532 ta->is_zero_type() || tb->is_zero_type() ||
5533 !ta->can_be_inline_type() || !tb->can_be_inline_type()) {
5534 // Use old acmp if one operand is null or not an inline type
5535 return new CmpPNode(a, b);
5536 } else if (ta->is_inlinetypeptr() || tb->is_inlinetypeptr()) {
5537 // We know that one operand is an inline type. Therefore,
5538 // new acmp will only return true if both operands are nullptr.
5539 // Check if both operands are null by or'ing the oops.
5540 a = phase->transform(new CastP2XNode(nullptr, a));
5541 b = phase->transform(new CastP2XNode(nullptr, b));
5542 a = phase->transform(new OrXNode(a, b));
5543 return new CmpXNode(a, phase->MakeConX(0));
5544 }
5545 // Use new acmp
5546 return nullptr;
5547 }
5548
5549 // Auxiliary methods to support randomized stressing/fuzzing.
5550
5551 void Compile::initialize_stress_seed(const DirectiveSet* directive) {
5552 if (FLAG_IS_DEFAULT(StressSeed) || (FLAG_IS_ERGO(StressSeed) && directive->RepeatCompilationOption)) {
5553 _stress_seed = static_cast<uint>(Ticks::now().nanoseconds());
5554 FLAG_SET_ERGO(StressSeed, _stress_seed);
5555 } else {
5556 _stress_seed = StressSeed;
5557 }
5558 if (_log != nullptr) {
5559 _log->elem("stress_test seed='%u'", _stress_seed);
5560 }
5561 }
5562
5563 int Compile::random() {
5564 _stress_seed = os::next_random(_stress_seed);
5565 return static_cast<int>(_stress_seed);
5566 }
5567
5568 // This method can be called the arbitrary number of times, with current count
5884 } else {
5885 _debug_network_printer->update_compiled_method(C->method());
5886 }
5887 tty->print_cr("Method printed over network stream to IGV");
5888 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5889 }
5890 #endif // !PRODUCT
5891
5892 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5893 if (type != nullptr && phase->type(value)->higher_equal(type)) {
5894 return value;
5895 }
5896 Node* result = nullptr;
5897 if (bt == T_BYTE) {
5898 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5899 result = new RShiftINode(result, phase->intcon(24));
5900 } else if (bt == T_BOOLEAN) {
5901 result = new AndINode(value, phase->intcon(0xFF));
5902 } else if (bt == T_CHAR) {
5903 result = new AndINode(value,phase->intcon(0xFFFF));
5904 } else if (bt == T_FLOAT) {
5905 result = new MoveI2FNode(value);
5906 } else {
5907 assert(bt == T_SHORT, "unexpected narrow type");
5908 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5909 result = new RShiftINode(result, phase->intcon(16));
5910 }
5911 if (transform_res) {
5912 result = phase->transform(result);
5913 }
5914 return result;
5915 }
5916
5917 void Compile::record_method_not_compilable_oom() {
5918 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5919 }
|