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(-1),
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 int 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()),
1054 _fixed_slots = 0;
1055 set_has_split_ifs(false);
1056 set_has_loops(false); // first approximation
1057 set_has_stringbuilder(false);
1058 set_has_boxed_value(false);
1059 _trap_can_recompile = false; // no traps emitted yet
1060 _major_progress = true; // start out assuming good things will happen
1061 set_has_unsafe_access(false);
1062 set_max_vector_size(0);
1063 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1064 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1065 set_decompile_count(0);
1066
1067 #ifndef PRODUCT
1068 _phase_counter = 0;
1069 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1070 #endif
1071
1072 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1073 _loop_opts_cnt = LoopOptsCount;
1074 set_do_inlining(Inline);
1075 set_max_inline_size(MaxInlineSize);
1076 set_freq_inline_size(FreqInlineSize);
1077 set_do_scheduling(OptoScheduling);
1078
1079 set_do_vector_loop(false);
1080 set_has_monitors(false);
1081 set_has_scoped_access(false);
1082
1083 if (AllowVectorizeOnDemand) {
1084 if (has_method() && _directive->VectorizeOption) {
1085 set_do_vector_loop(true);
1086 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());})
1087 } else if (has_method() && method()->name() != nullptr &&
1088 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1089 set_do_vector_loop(true);
1090 }
1091 }
1092 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1093 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());})
1338
1339 // Known instance (scalarizable allocation) alias only with itself.
1340 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1341 tj->is_oopptr()->is_known_instance();
1342
1343 // Process weird unsafe references.
1344 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1345 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1346 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1347 tj = TypeOopPtr::BOTTOM;
1348 ptr = tj->ptr();
1349 offset = tj->offset();
1350 }
1351
1352 // Array pointers need some flattening
1353 const TypeAryPtr* ta = tj->isa_aryptr();
1354 if (ta && ta->is_stable()) {
1355 // Erase stability property for alias analysis.
1356 tj = ta = ta->cast_to_stable(false);
1357 }
1358 if( ta && is_known_inst ) {
1359 if ( offset != Type::OffsetBot &&
1360 offset > arrayOopDesc::length_offset_in_bytes() ) {
1361 offset = Type::OffsetBot; // Flatten constant access into array body only
1362 tj = ta = ta->
1363 remove_speculative()->
1364 cast_to_ptr_type(ptr)->
1365 with_offset(offset);
1366 }
1367 } else if (ta) {
1368 // For arrays indexed by constant indices, we flatten the alias
1369 // space to include all of the array body. Only the header, klass
1370 // and array length can be accessed un-aliased.
1371 if( offset != Type::OffsetBot ) {
1372 if( ta->const_oop() ) { // MethodData* or Method*
1373 offset = Type::OffsetBot; // Flatten constant access into array body
1374 tj = ta = ta->
1375 remove_speculative()->
1376 cast_to_ptr_type(ptr)->
1377 cast_to_exactness(false)->
1378 with_offset(offset);
1379 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1380 // range is OK as-is.
1381 tj = ta = TypeAryPtr::RANGE;
1382 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1383 tj = TypeInstPtr::KLASS; // all klass loads look alike
1384 ta = TypeAryPtr::RANGE; // generic ignored junk
1385 ptr = TypePtr::BotPTR;
1386 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1387 tj = TypeInstPtr::MARK;
1388 ta = TypeAryPtr::RANGE; // generic ignored junk
1389 ptr = TypePtr::BotPTR;
1390 } else { // Random constant offset into array body
1391 offset = Type::OffsetBot; // Flatten constant access into array body
1392 tj = ta = ta->
1393 remove_speculative()->
1394 cast_to_ptr_type(ptr)->
1395 cast_to_exactness(false)->
1396 with_offset(offset);
1397 }
1398 }
1399 // Arrays of fixed size alias with arrays of unknown size.
1400 if (ta->size() != TypeInt::POS) {
1401 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1402 tj = ta = ta->
1403 remove_speculative()->
1404 cast_to_ptr_type(ptr)->
1405 with_ary(tary)->
1406 cast_to_exactness(false);
1407 }
1408 // Arrays of known objects become arrays of unknown objects.
1409 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1410 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1411 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1412 }
1413 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1414 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1415 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,offset);
1416 }
1417 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1418 // cannot be distinguished by bytecode alone.
1419 if (ta->elem() == TypeInt::BOOL) {
1420 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1421 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1422 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);
1423 }
1424 // During the 2nd round of IterGVN, NotNull castings are removed.
1425 // Make sure the Bottom and NotNull variants alias the same.
1426 // Also, make sure exact and non-exact variants alias the same.
1427 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1428 tj = ta = ta->
1429 remove_speculative()->
1430 cast_to_ptr_type(TypePtr::BotPTR)->
1431 cast_to_exactness(false)->
1432 with_offset(offset);
1433 }
1434 }
1435
1436 // Oop pointers need some flattening
1437 const TypeInstPtr *to = tj->isa_instptr();
1438 if (to && to != TypeOopPtr::BOTTOM) {
1439 ciInstanceKlass* ik = to->instance_klass();
1440 if( ptr == TypePtr::Constant ) {
1441 if (ik != ciEnv::current()->Class_klass() ||
1442 offset < ik->layout_helper_size_in_bytes()) {
1452 } else if( is_known_inst ) {
1453 tj = to; // Keep NotNull and klass_is_exact for instance type
1454 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1455 // During the 2nd round of IterGVN, NotNull castings are removed.
1456 // Make sure the Bottom and NotNull variants alias the same.
1457 // Also, make sure exact and non-exact variants alias the same.
1458 tj = to = to->
1459 remove_speculative()->
1460 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1461 cast_to_ptr_type(TypePtr::BotPTR)->
1462 cast_to_exactness(false);
1463 }
1464 if (to->speculative() != nullptr) {
1465 tj = to = to->remove_speculative();
1466 }
1467 // Canonicalize the holder of this field
1468 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1469 // First handle header references such as a LoadKlassNode, even if the
1470 // object's klass is unloaded at compile time (4965979).
1471 if (!is_known_inst) { // Do it only for non-instance types
1472 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, offset);
1473 }
1474 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1475 // Static fields are in the space above the normal instance
1476 // fields in the java.lang.Class instance.
1477 if (ik != ciEnv::current()->Class_klass()) {
1478 to = nullptr;
1479 tj = TypeOopPtr::BOTTOM;
1480 offset = tj->offset();
1481 }
1482 } else {
1483 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1484 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1485 assert(tj->offset() == offset, "no change to offset expected");
1486 bool xk = to->klass_is_exact();
1487 int instance_id = to->instance_id();
1488
1489 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1490 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1491 // its interfaces are included.
1492 if (xk && ik->equals(canonical_holder)) {
1493 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id), "exact type should be canonical type");
1494 } else {
1495 assert(xk || !is_known_inst, "Known instance should be exact type");
1496 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id);
1497 }
1498 }
1499 }
1500
1501 // Klass pointers to object array klasses need some flattening
1502 const TypeKlassPtr *tk = tj->isa_klassptr();
1503 if( tk ) {
1504 // If we are referencing a field within a Klass, we need
1505 // to assume the worst case of an Object. Both exact and
1506 // inexact types must flatten to the same alias class so
1507 // use NotNull as the PTR.
1508 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1509 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1510 env()->Object_klass(),
1511 offset);
1512 }
1513
1514 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1515 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1516 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1517 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), offset);
1518 } else {
1519 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, offset);
1520 }
1521 }
1522
1523 // Check for precise loads from the primary supertype array and force them
1524 // to the supertype cache alias index. Check for generic array loads from
1525 // the primary supertype array and also force them to the supertype cache
1526 // alias index. Since the same load can reach both, we need to merge
1527 // these 2 disparate memories into the same alias class. Since the
1528 // primary supertype array is read-only, there's no chance of confusion
1529 // where we bypass an array load and an array store.
1530 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1531 if (offset == Type::OffsetBot ||
1532 (offset >= primary_supers_offset &&
1533 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1534 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1535 offset = in_bytes(Klass::secondary_super_cache_offset());
1536 tj = tk = tk->with_offset(offset);
1537 }
1538 }
1539
1540 // Flatten all Raw pointers together.
1541 if (tj->base() == Type::RawPtr)
1542 tj = TypeRawPtr::BOTTOM;
1632 intptr_t key = (intptr_t) adr_type;
1633 key ^= key >> logAliasCacheSize;
1634 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1635 }
1636
1637
1638 //-----------------------------grow_alias_types--------------------------------
1639 void Compile::grow_alias_types() {
1640 const int old_ats = _max_alias_types; // how many before?
1641 const int new_ats = old_ats; // how many more?
1642 const int grow_ats = old_ats+new_ats; // how many now?
1643 _max_alias_types = grow_ats;
1644 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1645 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1646 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1647 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1648 }
1649
1650
1651 //--------------------------------find_alias_type------------------------------
1652 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1653 if (!do_aliasing()) {
1654 return alias_type(AliasIdxBot);
1655 }
1656
1657 AliasCacheEntry* ace = probe_alias_cache(adr_type);
1658 if (ace->_adr_type == adr_type) {
1659 return alias_type(ace->_index);
1660 }
1661
1662 // Handle special cases.
1663 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1664 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1665
1666 // Do it the slow way.
1667 const TypePtr* flat = flatten_alias_type(adr_type);
1668
1669 #ifdef ASSERT
1670 {
1671 ResourceMark rm;
1672 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1673 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1674 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1675 Type::str(adr_type));
1676 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1677 const TypeOopPtr* foop = flat->is_oopptr();
1678 // Scalarizable allocations have exact klass always.
1679 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1689 if (alias_type(i)->adr_type() == flat) {
1690 idx = i;
1691 break;
1692 }
1693 }
1694
1695 if (idx == AliasIdxTop) {
1696 if (no_create) return nullptr;
1697 // Grow the array if necessary.
1698 if (_num_alias_types == _max_alias_types) grow_alias_types();
1699 // Add a new alias type.
1700 idx = _num_alias_types++;
1701 _alias_types[idx]->Init(idx, flat);
1702 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1703 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1704 if (flat->isa_instptr()) {
1705 if (flat->offset() == java_lang_Class::klass_offset()
1706 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1707 alias_type(idx)->set_rewritable(false);
1708 }
1709 if (flat->isa_aryptr()) {
1710 #ifdef ASSERT
1711 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1712 // (T_BYTE has the weakest alignment and size restrictions...)
1713 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1714 #endif
1715 if (flat->offset() == TypePtr::OffsetBot) {
1716 alias_type(idx)->set_element(flat->is_aryptr()->elem());
1717 }
1718 }
1719 if (flat->isa_klassptr()) {
1720 if (UseCompactObjectHeaders) {
1721 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1722 alias_type(idx)->set_rewritable(false);
1723 }
1724 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1725 alias_type(idx)->set_rewritable(false);
1726 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1727 alias_type(idx)->set_rewritable(false);
1728 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1729 alias_type(idx)->set_rewritable(false);
1730 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1731 alias_type(idx)->set_rewritable(false);
1732 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1733 alias_type(idx)->set_rewritable(false);
1734 }
1735 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1736 // but the base pointer type is not distinctive enough to identify
1737 // references into JavaThread.)
1738
1739 // Check for final fields.
1740 const TypeInstPtr* tinst = flat->isa_instptr();
1741 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1742 ciField* field;
1743 if (tinst->const_oop() != nullptr &&
1744 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1745 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1746 // static field
1747 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1748 field = k->get_field_by_offset(tinst->offset(), true);
1749 } else {
1750 ciInstanceKlass *k = tinst->instance_klass();
1751 field = k->get_field_by_offset(tinst->offset(), false);
1752 }
1753 assert(field == nullptr ||
1754 original_field == nullptr ||
1755 (field->holder() == original_field->holder() &&
1756 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1757 field->is_static() == original_field->is_static()), "wrong field?");
1758 // Set field() and is_rewritable() attributes.
1759 if (field != nullptr) alias_type(idx)->set_field(field);
1760 }
1761 }
1762
1763 // Fill the cache for next time.
1764 ace->_adr_type = adr_type;
1765 ace->_index = idx;
1766 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1767
1768 // Might as well try to fill the cache for the flattened version, too.
1769 AliasCacheEntry* face = probe_alias_cache(flat);
1770 if (face->_adr_type == nullptr) {
1771 face->_adr_type = flat;
1772 face->_index = idx;
1773 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1774 }
1775
1776 return alias_type(idx);
1777 }
1778
1779
1780 Compile::AliasType* Compile::alias_type(ciField* field) {
1781 const TypeOopPtr* t;
1782 if (field->is_static())
1783 t = TypeInstPtr::make(field->holder()->java_mirror());
1784 else
1785 t = TypeOopPtr::make_from_klass_raw(field->holder());
1786 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1787 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1788 return atp;
1789 }
1790
1791
1792 //------------------------------have_alias_type--------------------------------
1793 bool Compile::have_alias_type(const TypePtr* adr_type) {
1875 assert(!C->major_progress(), "not cleared");
1876
1877 if (_for_post_loop_igvn.length() > 0) {
1878 while (_for_post_loop_igvn.length() > 0) {
1879 Node* n = _for_post_loop_igvn.pop();
1880 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1881 igvn._worklist.push(n);
1882 }
1883 igvn.optimize();
1884 if (failing()) return;
1885 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1886 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1887
1888 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1889 if (C->major_progress()) {
1890 C->clear_major_progress(); // ensure that major progress is now clear
1891 }
1892 }
1893 }
1894
1895 void Compile::record_for_merge_stores_igvn(Node* n) {
1896 if (!n->for_merge_stores_igvn()) {
1897 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
1898 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1899 _for_merge_stores_igvn.append(n);
1900 }
1901 }
1902
1903 void Compile::remove_from_merge_stores_igvn(Node* n) {
1904 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1905 _for_merge_stores_igvn.remove(n);
1906 }
1907
1908 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
1909 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
1910 // the stores, and we merge the wrong sequence of stores.
1911 // Example:
1912 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
1913 // Apply MergeStores:
1914 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
1993 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
1994 Bytecodes::Code c = iter.cur_bc();
1995 Node* lhs = nullptr;
1996 Node* rhs = nullptr;
1997 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
1998 lhs = unc->peek_operand(0);
1999 rhs = unc->peek_operand(1);
2000 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2001 lhs = unc->peek_operand(0);
2002 }
2003
2004 ResourceMark rm;
2005 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2006 assert(live_locals.is_valid(), "broken liveness info");
2007 int len = (int)live_locals.size();
2008
2009 for (int i = 0; i < len; i++) {
2010 Node* local = unc->local(jvms, i);
2011 // kill local using the liveness of next_bci.
2012 // give up when the local looks like an operand to secure reexecution.
2013 if (!live_locals.at(i) && !local->is_top() && local != lhs && local!= rhs) {
2014 uint idx = jvms->locoff() + i;
2015 #ifdef ASSERT
2016 if (PrintOpto && Verbose) {
2017 tty->print("[unstable_if] kill local#%d: ", idx);
2018 local->dump();
2019 tty->cr();
2020 }
2021 #endif
2022 igvn.replace_input_of(unc, idx, top());
2023 modified = true;
2024 }
2025 }
2026 }
2027
2028 // keep the mondified trap for late query
2029 if (modified) {
2030 trap->set_modified();
2031 } else {
2032 _unstable_if_traps.delete_at(i);
2033 }
2034 }
2035 igvn.optimize();
2036 }
2037
2038 // StringOpts and late inlining of string methods
2039 void Compile::inline_string_calls(bool parse_time) {
2040 {
2041 // remove useless nodes to make the usage analysis simpler
2042 ResourceMark rm;
2043 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2044 }
2045
2046 {
2047 ResourceMark rm;
2048 print_method(PHASE_BEFORE_STRINGOPTS, 3);
2214
2215 if (_string_late_inlines.length() > 0) {
2216 assert(has_stringbuilder(), "inconsistent");
2217
2218 inline_string_calls(false);
2219
2220 if (failing()) return;
2221
2222 inline_incrementally_cleanup(igvn);
2223 }
2224
2225 set_inlining_incrementally(false);
2226 }
2227
2228 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2229 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2230 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2231 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2232 // as if "inlining_incrementally() == true" were set.
2233 assert(inlining_incrementally() == false, "not allowed");
2234 assert(_modified_nodes == nullptr, "not allowed");
2235 assert(_late_inlines.length() > 0, "sanity");
2236
2237 while (_late_inlines.length() > 0) {
2238 igvn_worklist()->ensure_empty(); // should be done with igvn
2239
2240 while (inline_incrementally_one()) {
2241 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2242 }
2243 if (failing()) return;
2244
2245 inline_incrementally_cleanup(igvn);
2246 }
2247 }
2248
2249 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2250 if (_loop_opts_cnt > 0) {
2251 while (major_progress() && (_loop_opts_cnt > 0)) {
2252 TracePhase tp(_t_idealLoop);
2253 PhaseIdealLoop::optimize(igvn, mode);
2254 _loop_opts_cnt--;
2255 if (failing()) return false;
2256 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2257 }
2258 }
2259 return true;
2260 }
2261
2262 // Remove edges from "root" to each SafePoint at a backward branch.
2263 // They were inserted during parsing (see add_safepoint()) to make
2264 // infinite loops without calls or exceptions visible to root, i.e.,
2265 // useful.
2266 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_from_gvn(initial_gvn());
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 }
3323 int nop = n->Opcode();
3324 // Clone shared simple arguments to uncommon calls, item (1).
3325 if (n->outcnt() > 1 &&
3326 !n->is_Proj() &&
3327 nop != Op_CreateEx &&
3328 nop != Op_CheckCastPP &&
3329 nop != Op_DecodeN &&
3330 nop != Op_DecodeNKlass &&
3331 !n->is_Mem() &&
3332 !n->is_Phi()) {
3333 Node *x = n->clone();
3334 call->set_req(TypeFunc::Parms, x);
3335 }
3336 }
3337 break;
3338 }
3339 case Op_StoreB:
3340 case Op_StoreC:
3341 case Op_StoreI:
3342 case Op_StoreL:
3343 case Op_CompareAndSwapB:
3344 case Op_CompareAndSwapS:
3345 case Op_CompareAndSwapI:
3346 case Op_CompareAndSwapL:
3347 case Op_CompareAndSwapP:
3348 case Op_CompareAndSwapN:
3349 case Op_WeakCompareAndSwapB:
3350 case Op_WeakCompareAndSwapS:
3351 case Op_WeakCompareAndSwapI:
3352 case Op_WeakCompareAndSwapL:
3353 case Op_WeakCompareAndSwapP:
3354 case Op_WeakCompareAndSwapN:
3355 case Op_CompareAndExchangeB:
3356 case Op_CompareAndExchangeS:
3357 case Op_CompareAndExchangeI:
3358 case Op_CompareAndExchangeL:
3359 case Op_CompareAndExchangeP:
3360 case Op_CompareAndExchangeN:
3361 case Op_GetAndAddS:
3362 case Op_GetAndAddB:
3866 k->subsume_by(m, this);
3867 }
3868 }
3869 }
3870 break;
3871 }
3872 case Op_CmpUL: {
3873 if (!Matcher::has_match_rule(Op_CmpUL)) {
3874 // No support for unsigned long comparisons
3875 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3876 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3877 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3878 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3879 Node* andl = new AndLNode(orl, remove_sign_mask);
3880 Node* cmp = new CmpLNode(andl, n->in(2));
3881 n->subsume_by(cmp, this);
3882 }
3883 break;
3884 }
3885 #ifdef ASSERT
3886 case Op_ConNKlass: {
3887 const TypePtr* tp = n->as_Type()->type()->make_ptr();
3888 ciKlass* klass = tp->is_klassptr()->exact_klass();
3889 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
3890 break;
3891 }
3892 #endif
3893 default:
3894 assert(!n->is_Call(), "");
3895 assert(!n->is_Mem(), "");
3896 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3897 break;
3898 }
3899 }
3900
3901 //------------------------------final_graph_reshaping_walk---------------------
3902 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3903 // requires that the walk visits a node's inputs before visiting the node.
3904 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3905 Unique_Node_List sfpt;
4241 }
4242 }
4243
4244 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4245 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4246 }
4247
4248 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4249 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4250 }
4251
4252 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4253 if (holder->is_initialized()) {
4254 return false;
4255 }
4256 if (holder->is_being_initialized()) {
4257 if (accessing_method->holder() == holder) {
4258 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4259 // <init>, or a static method. In all those cases, there was an initialization
4260 // barrier on the holder klass passed.
4261 if (accessing_method->is_static_initializer() ||
4262 accessing_method->is_object_initializer() ||
4263 accessing_method->is_static()) {
4264 return false;
4265 }
4266 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4267 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4268 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4269 // child class can become fully initialized while its parent class is still being initialized.
4270 if (accessing_method->is_static_initializer()) {
4271 return false;
4272 }
4273 }
4274 ciMethod* root = method(); // the root method of compilation
4275 if (root != accessing_method) {
4276 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4277 }
4278 }
4279 return true;
4280 }
4281
4282 #ifndef PRODUCT
4283 //------------------------------verify_bidirectional_edges---------------------
4284 // For each input edge to a node (ie - for each Use-Def edge), verify that
4285 // there is a corresponding Def-Use edge.
4286 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4287 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4288 uint stack_size = live_nodes() >> 4;
4289 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4290 if (root_and_safepoints != nullptr) {
4320 if (in != nullptr && !in->is_top()) {
4321 // Count instances of `next`
4322 int cnt = 0;
4323 for (uint idx = 0; idx < in->_outcnt; idx++) {
4324 if (in->_out[idx] == n) {
4325 cnt++;
4326 }
4327 }
4328 assert(cnt > 0, "Failed to find Def-Use edge.");
4329 // Check for duplicate edges
4330 // walk the input array downcounting the input edges to n
4331 for (uint j = 0; j < length; j++) {
4332 if (n->in(j) == in) {
4333 cnt--;
4334 }
4335 }
4336 assert(cnt == 0, "Mismatched edge count.");
4337 } else if (in == nullptr) {
4338 assert(i == 0 || i >= n->req() ||
4339 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4340 (n->is_Unlock() && i == (n->req() - 1)) ||
4341 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4342 "only region, phi, arraycopy, unlock or membar nodes have null data edges");
4343 } else {
4344 assert(in->is_top(), "sanity");
4345 // Nothing to check.
4346 }
4347 }
4348 }
4349 }
4350
4351 //------------------------------verify_graph_edges---------------------------
4352 // Walk the Graph and verify that there is a one-to-one correspondence
4353 // between Use-Def edges and Def-Use edges in the graph.
4354 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4355 if (VerifyGraphEdges) {
4356 Unique_Node_List visited;
4357
4358 // Call graph walk to check edges
4359 verify_bidirectional_edges(visited, root_and_safepoints);
4360 if (no_dead_code) {
4361 // Now make sure that no visited node is used by an unvisited node.
4362 bool dead_nodes = false;
4473 // (1) subklass is already limited to a subtype of superklass => always ok
4474 // (2) subklass does not overlap with superklass => always fail
4475 // (3) superklass has NO subtypes and we can check with a simple compare.
4476 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4477 if (skip) {
4478 return SSC_full_test; // Let caller generate the general case.
4479 }
4480
4481 if (subk->is_java_subtype_of(superk)) {
4482 return SSC_always_true; // (0) and (1) this test cannot fail
4483 }
4484
4485 if (!subk->maybe_java_subtype_of(superk)) {
4486 return SSC_always_false; // (2) true path dead; no dynamic test needed
4487 }
4488
4489 const Type* superelem = superk;
4490 if (superk->isa_aryklassptr()) {
4491 int ignored;
4492 superelem = superk->is_aryklassptr()->base_element_type(ignored);
4493 }
4494
4495 if (superelem->isa_instklassptr()) {
4496 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4497 if (!ik->has_subklass()) {
4498 if (!ik->is_final()) {
4499 // Add a dependency if there is a chance of a later subclass.
4500 dependencies()->assert_leaf_type(ik);
4501 }
4502 if (!superk->maybe_java_subtype_of(subk)) {
4503 return SSC_always_false;
4504 }
4505 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4506 }
4507 } else {
4508 // A primitive array type has no subtypes.
4509 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4510 }
4511
4512 return SSC_full_test;
4956 const Type* t = igvn.type_or_null(n);
4957 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
4958 if (n->is_Type()) {
4959 t = n->as_Type()->type();
4960 assert(t == t->remove_speculative(), "no more speculative types");
4961 }
4962 // Iterate over outs - endless loops is unreachable from below
4963 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
4964 Node *m = n->fast_out(i);
4965 if (not_a_node(m)) {
4966 continue;
4967 }
4968 worklist.push(m);
4969 }
4970 }
4971 igvn.check_no_speculative_types();
4972 #endif
4973 }
4974 }
4975
4976 // Auxiliary methods to support randomized stressing/fuzzing.
4977
4978 void Compile::initialize_stress_seed(const DirectiveSet* directive) {
4979 if (FLAG_IS_DEFAULT(StressSeed) || (FLAG_IS_ERGO(StressSeed) && directive->RepeatCompilationOption)) {
4980 _stress_seed = static_cast<uint>(Ticks::now().nanoseconds());
4981 FLAG_SET_ERGO(StressSeed, _stress_seed);
4982 } else {
4983 _stress_seed = StressSeed;
4984 }
4985 if (_log != nullptr) {
4986 _log->elem("stress_test seed='%u'", _stress_seed);
4987 }
4988 }
4989
4990 int Compile::random() {
4991 _stress_seed = os::next_random(_stress_seed);
4992 return static_cast<int>(_stress_seed);
4993 }
4994
4995 // This method can be called the arbitrary number of times, with current count
5311 } else {
5312 _debug_network_printer->update_compiled_method(C->method());
5313 }
5314 tty->print_cr("Method printed over network stream to IGV");
5315 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5316 }
5317 #endif // !PRODUCT
5318
5319 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5320 if (type != nullptr && phase->type(value)->higher_equal(type)) {
5321 return value;
5322 }
5323 Node* result = nullptr;
5324 if (bt == T_BYTE) {
5325 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5326 result = new RShiftINode(result, phase->intcon(24));
5327 } else if (bt == T_BOOLEAN) {
5328 result = new AndINode(value, phase->intcon(0xFF));
5329 } else if (bt == T_CHAR) {
5330 result = new AndINode(value,phase->intcon(0xFFFF));
5331 } else {
5332 assert(bt == T_SHORT, "unexpected narrow type");
5333 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5334 result = new RShiftINode(result, phase->intcon(16));
5335 }
5336 if (transform_res) {
5337 result = phase->transform(result);
5338 }
5339 return result;
5340 }
5341
5342 void Compile::record_method_not_compilable_oom() {
5343 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5344 }
|
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(-1),
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 int 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()),
1079 _fixed_slots = 0;
1080 set_has_split_ifs(false);
1081 set_has_loops(false); // first approximation
1082 set_has_stringbuilder(false);
1083 set_has_boxed_value(false);
1084 _trap_can_recompile = false; // no traps emitted yet
1085 _major_progress = true; // start out assuming good things will happen
1086 set_has_unsafe_access(false);
1087 set_max_vector_size(0);
1088 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1089 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1090 set_decompile_count(0);
1091
1092 #ifndef PRODUCT
1093 _phase_counter = 0;
1094 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1095 #endif
1096
1097 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1098 _loop_opts_cnt = LoopOptsCount;
1099 _has_flat_accesses = false;
1100 _flat_accesses_share_alias = true;
1101 _scalarize_in_safepoints = false;
1102
1103 set_do_inlining(Inline);
1104 set_max_inline_size(MaxInlineSize);
1105 set_freq_inline_size(FreqInlineSize);
1106 set_do_scheduling(OptoScheduling);
1107
1108 set_do_vector_loop(false);
1109 set_has_monitors(false);
1110 set_has_scoped_access(false);
1111
1112 if (AllowVectorizeOnDemand) {
1113 if (has_method() && _directive->VectorizeOption) {
1114 set_do_vector_loop(true);
1115 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());})
1116 } else if (has_method() && method()->name() != nullptr &&
1117 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1118 set_do_vector_loop(true);
1119 }
1120 }
1121 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1122 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());})
1367
1368 // Known instance (scalarizable allocation) alias only with itself.
1369 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1370 tj->is_oopptr()->is_known_instance();
1371
1372 // Process weird unsafe references.
1373 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1374 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1375 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1376 tj = TypeOopPtr::BOTTOM;
1377 ptr = tj->ptr();
1378 offset = tj->offset();
1379 }
1380
1381 // Array pointers need some flattening
1382 const TypeAryPtr* ta = tj->isa_aryptr();
1383 if (ta && ta->is_stable()) {
1384 // Erase stability property for alias analysis.
1385 tj = ta = ta->cast_to_stable(false);
1386 }
1387 if (ta && ta->is_not_flat()) {
1388 // Erase not flat property for alias analysis.
1389 tj = ta = ta->cast_to_not_flat(false);
1390 }
1391 if (ta && ta->is_not_null_free()) {
1392 // Erase not null free property for alias analysis.
1393 tj = ta = ta->cast_to_not_null_free(false);
1394 }
1395
1396 if( ta && is_known_inst ) {
1397 if ( offset != Type::OffsetBot &&
1398 offset > arrayOopDesc::length_offset_in_bytes() ) {
1399 offset = Type::OffsetBot; // Flatten constant access into array body only
1400 tj = ta = ta->
1401 remove_speculative()->
1402 cast_to_ptr_type(ptr)->
1403 with_offset(offset);
1404 }
1405 } else if (ta) {
1406 // For arrays indexed by constant indices, we flatten the alias
1407 // space to include all of the array body. Only the header, klass
1408 // and array length can be accessed un-aliased.
1409 // For flat inline type array, each field has its own slice so
1410 // we must include the field offset.
1411 if( offset != Type::OffsetBot ) {
1412 if( ta->const_oop() ) { // MethodData* or Method*
1413 offset = Type::OffsetBot; // Flatten constant access into array body
1414 tj = ta = ta->
1415 remove_speculative()->
1416 cast_to_ptr_type(ptr)->
1417 cast_to_exactness(false)->
1418 with_offset(offset);
1419 } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1420 // range is OK as-is.
1421 tj = ta = TypeAryPtr::RANGE;
1422 } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1423 tj = TypeInstPtr::KLASS; // all klass loads look alike
1424 ta = TypeAryPtr::RANGE; // generic ignored junk
1425 ptr = TypePtr::BotPTR;
1426 } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1427 tj = TypeInstPtr::MARK;
1428 ta = TypeAryPtr::RANGE; // generic ignored junk
1429 ptr = TypePtr::BotPTR;
1430 } else { // Random constant offset into array body
1431 offset = Type::OffsetBot; // Flatten constant access into array body
1432 tj = ta = ta->
1433 remove_speculative()->
1434 cast_to_ptr_type(ptr)->
1435 cast_to_exactness(false)->
1436 with_offset(offset);
1437 }
1438 }
1439 // Arrays of fixed size alias with arrays of unknown size.
1440 if (ta->size() != TypeInt::POS) {
1441 const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1442 tj = ta = ta->
1443 remove_speculative()->
1444 cast_to_ptr_type(ptr)->
1445 with_ary(tary)->
1446 cast_to_exactness(false);
1447 }
1448 // Arrays of known objects become arrays of unknown objects.
1449 if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1450 const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1451 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1452 }
1453 if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1454 const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1455 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), ta->field_offset());
1456 }
1457 // Initially all flattened array accesses share a single slice
1458 if (ta->is_flat() && ta->elem() != TypeInstPtr::BOTTOM && _flat_accesses_share_alias) {
1459 const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size(), /* stable= */ false, /* flat= */ true);
1460 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,nullptr,false,Type::Offset(offset), Type::Offset(Type::OffsetBot));
1461 }
1462 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1463 // cannot be distinguished by bytecode alone.
1464 if (ta->elem() == TypeInt::BOOL) {
1465 const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1466 ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1467 tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,Type::Offset(offset), ta->field_offset());
1468 }
1469 // During the 2nd round of IterGVN, NotNull castings are removed.
1470 // Make sure the Bottom and NotNull variants alias the same.
1471 // Also, make sure exact and non-exact variants alias the same.
1472 if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != nullptr) {
1473 tj = ta = ta->
1474 remove_speculative()->
1475 cast_to_ptr_type(TypePtr::BotPTR)->
1476 cast_to_exactness(false)->
1477 with_offset(offset);
1478 }
1479 }
1480
1481 // Oop pointers need some flattening
1482 const TypeInstPtr *to = tj->isa_instptr();
1483 if (to && to != TypeOopPtr::BOTTOM) {
1484 ciInstanceKlass* ik = to->instance_klass();
1485 if( ptr == TypePtr::Constant ) {
1486 if (ik != ciEnv::current()->Class_klass() ||
1487 offset < ik->layout_helper_size_in_bytes()) {
1497 } else if( is_known_inst ) {
1498 tj = to; // Keep NotNull and klass_is_exact for instance type
1499 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1500 // During the 2nd round of IterGVN, NotNull castings are removed.
1501 // Make sure the Bottom and NotNull variants alias the same.
1502 // Also, make sure exact and non-exact variants alias the same.
1503 tj = to = to->
1504 remove_speculative()->
1505 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1506 cast_to_ptr_type(TypePtr::BotPTR)->
1507 cast_to_exactness(false);
1508 }
1509 if (to->speculative() != nullptr) {
1510 tj = to = to->remove_speculative();
1511 }
1512 // Canonicalize the holder of this field
1513 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1514 // First handle header references such as a LoadKlassNode, even if the
1515 // object's klass is unloaded at compile time (4965979).
1516 if (!is_known_inst) { // Do it only for non-instance types
1517 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, Type::Offset(offset));
1518 }
1519 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1520 // Static fields are in the space above the normal instance
1521 // fields in the java.lang.Class instance.
1522 if (ik != ciEnv::current()->Class_klass()) {
1523 to = nullptr;
1524 tj = TypeOopPtr::BOTTOM;
1525 offset = tj->offset();
1526 }
1527 } else {
1528 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1529 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1530 assert(tj->offset() == offset, "no change to offset expected");
1531 bool xk = to->klass_is_exact();
1532 int instance_id = to->instance_id();
1533
1534 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1535 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1536 // its interfaces are included.
1537 if (xk && ik->equals(canonical_holder)) {
1538 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id), "exact type should be canonical type");
1539 } else {
1540 assert(xk || !is_known_inst, "Known instance should be exact type");
1541 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id);
1542 }
1543 }
1544 }
1545
1546 // Klass pointers to object array klasses need some flattening
1547 const TypeKlassPtr *tk = tj->isa_klassptr();
1548 if( tk ) {
1549 // If we are referencing a field within a Klass, we need
1550 // to assume the worst case of an Object. Both exact and
1551 // inexact types must flatten to the same alias class so
1552 // use NotNull as the PTR.
1553 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1554 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1555 env()->Object_klass(),
1556 Type::Offset(offset));
1557 }
1558
1559 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1560 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1561 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1562 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), Type::Offset(offset));
1563 } else {
1564 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());
1565 }
1566 }
1567 // Check for precise loads from the primary supertype array and force them
1568 // to the supertype cache alias index. Check for generic array loads from
1569 // the primary supertype array and also force them to the supertype cache
1570 // alias index. Since the same load can reach both, we need to merge
1571 // these 2 disparate memories into the same alias class. Since the
1572 // primary supertype array is read-only, there's no chance of confusion
1573 // where we bypass an array load and an array store.
1574 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1575 if (offset == Type::OffsetBot ||
1576 (offset >= primary_supers_offset &&
1577 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1578 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1579 offset = in_bytes(Klass::secondary_super_cache_offset());
1580 tj = tk = tk->with_offset(offset);
1581 }
1582 }
1583
1584 // Flatten all Raw pointers together.
1585 if (tj->base() == Type::RawPtr)
1586 tj = TypeRawPtr::BOTTOM;
1676 intptr_t key = (intptr_t) adr_type;
1677 key ^= key >> logAliasCacheSize;
1678 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1679 }
1680
1681
1682 //-----------------------------grow_alias_types--------------------------------
1683 void Compile::grow_alias_types() {
1684 const int old_ats = _max_alias_types; // how many before?
1685 const int new_ats = old_ats; // how many more?
1686 const int grow_ats = old_ats+new_ats; // how many now?
1687 _max_alias_types = grow_ats;
1688 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1689 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1690 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1691 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1692 }
1693
1694
1695 //--------------------------------find_alias_type------------------------------
1696 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1697 if (!do_aliasing()) {
1698 return alias_type(AliasIdxBot);
1699 }
1700
1701 AliasCacheEntry* ace = nullptr;
1702 if (!uncached) {
1703 ace = probe_alias_cache(adr_type);
1704 if (ace->_adr_type == adr_type) {
1705 return alias_type(ace->_index);
1706 }
1707 }
1708
1709 // Handle special cases.
1710 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1711 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1712
1713 // Do it the slow way.
1714 const TypePtr* flat = flatten_alias_type(adr_type);
1715
1716 #ifdef ASSERT
1717 {
1718 ResourceMark rm;
1719 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1720 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1721 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1722 Type::str(adr_type));
1723 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1724 const TypeOopPtr* foop = flat->is_oopptr();
1725 // Scalarizable allocations have exact klass always.
1726 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1736 if (alias_type(i)->adr_type() == flat) {
1737 idx = i;
1738 break;
1739 }
1740 }
1741
1742 if (idx == AliasIdxTop) {
1743 if (no_create) return nullptr;
1744 // Grow the array if necessary.
1745 if (_num_alias_types == _max_alias_types) grow_alias_types();
1746 // Add a new alias type.
1747 idx = _num_alias_types++;
1748 _alias_types[idx]->Init(idx, flat);
1749 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1750 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1751 if (flat->isa_instptr()) {
1752 if (flat->offset() == java_lang_Class::klass_offset()
1753 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1754 alias_type(idx)->set_rewritable(false);
1755 }
1756 ciField* field = nullptr;
1757 if (flat->isa_aryptr()) {
1758 #ifdef ASSERT
1759 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1760 // (T_BYTE has the weakest alignment and size restrictions...)
1761 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1762 #endif
1763 const Type* elemtype = flat->is_aryptr()->elem();
1764 if (flat->offset() == TypePtr::OffsetBot) {
1765 alias_type(idx)->set_element(elemtype);
1766 }
1767 int field_offset = flat->is_aryptr()->field_offset().get();
1768 if (flat->is_flat() &&
1769 field_offset != Type::OffsetBot) {
1770 ciInlineKlass* vk = elemtype->inline_klass();
1771 field_offset += vk->payload_offset();
1772 field = vk->get_field_by_offset(field_offset, false);
1773 }
1774 }
1775 if (flat->isa_klassptr()) {
1776 if (UseCompactObjectHeaders) {
1777 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1778 alias_type(idx)->set_rewritable(false);
1779 }
1780 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1781 alias_type(idx)->set_rewritable(false);
1782 if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1783 alias_type(idx)->set_rewritable(false);
1784 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1785 alias_type(idx)->set_rewritable(false);
1786 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1787 alias_type(idx)->set_rewritable(false);
1788 if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1789 alias_type(idx)->set_rewritable(false);
1790 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1791 alias_type(idx)->set_rewritable(false);
1792 }
1793 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1794 // but the base pointer type is not distinctive enough to identify
1795 // references into JavaThread.)
1796
1797 // Check for final fields.
1798 const TypeInstPtr* tinst = flat->isa_instptr();
1799 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1800 if (tinst->const_oop() != nullptr &&
1801 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1802 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1803 // static field
1804 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1805 field = k->get_field_by_offset(tinst->offset(), true);
1806 } else if (tinst->is_inlinetypeptr()) {
1807 // Inline type field
1808 ciInlineKlass* vk = tinst->inline_klass();
1809 field = vk->get_field_by_offset(tinst->offset(), false);
1810 } else {
1811 ciInstanceKlass *k = tinst->instance_klass();
1812 field = k->get_field_by_offset(tinst->offset(), false);
1813 }
1814 }
1815 assert(field == nullptr ||
1816 original_field == nullptr ||
1817 (field->holder() == original_field->holder() &&
1818 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1819 field->is_static() == original_field->is_static()), "wrong field?");
1820 // Set field() and is_rewritable() attributes.
1821 if (field != nullptr) {
1822 alias_type(idx)->set_field(field);
1823 if (flat->isa_aryptr()) {
1824 // Fields of flat arrays are rewritable although they are declared final
1825 assert(flat->is_flat(), "must be a flat array");
1826 alias_type(idx)->set_rewritable(true);
1827 }
1828 }
1829 }
1830
1831 // Fill the cache for next time.
1832 if (!uncached) {
1833 ace->_adr_type = adr_type;
1834 ace->_index = idx;
1835 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1836
1837 // Might as well try to fill the cache for the flattened version, too.
1838 AliasCacheEntry* face = probe_alias_cache(flat);
1839 if (face->_adr_type == nullptr) {
1840 face->_adr_type = flat;
1841 face->_index = idx;
1842 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1843 }
1844 }
1845
1846 return alias_type(idx);
1847 }
1848
1849
1850 Compile::AliasType* Compile::alias_type(ciField* field) {
1851 const TypeOopPtr* t;
1852 if (field->is_static())
1853 t = TypeInstPtr::make(field->holder()->java_mirror());
1854 else
1855 t = TypeOopPtr::make_from_klass_raw(field->holder());
1856 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1857 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1858 return atp;
1859 }
1860
1861
1862 //------------------------------have_alias_type--------------------------------
1863 bool Compile::have_alias_type(const TypePtr* adr_type) {
1945 assert(!C->major_progress(), "not cleared");
1946
1947 if (_for_post_loop_igvn.length() > 0) {
1948 while (_for_post_loop_igvn.length() > 0) {
1949 Node* n = _for_post_loop_igvn.pop();
1950 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1951 igvn._worklist.push(n);
1952 }
1953 igvn.optimize();
1954 if (failing()) return;
1955 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1956 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1957
1958 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1959 if (C->major_progress()) {
1960 C->clear_major_progress(); // ensure that major progress is now clear
1961 }
1962 }
1963 }
1964
1965 void Compile::add_inline_type(Node* n) {
1966 assert(n->is_InlineType(), "unexpected node");
1967 _inline_type_nodes.push(n);
1968 }
1969
1970 void Compile::remove_inline_type(Node* n) {
1971 assert(n->is_InlineType(), "unexpected node");
1972 if (_inline_type_nodes.contains(n)) {
1973 _inline_type_nodes.remove(n);
1974 }
1975 }
1976
1977 // Does the return value keep otherwise useless inline type allocations alive?
1978 static bool return_val_keeps_allocations_alive(Node* ret_val) {
1979 ResourceMark rm;
1980 Unique_Node_List wq;
1981 wq.push(ret_val);
1982 bool some_allocations = false;
1983 for (uint i = 0; i < wq.size(); i++) {
1984 Node* n = wq.at(i);
1985 if (n->outcnt() > 1) {
1986 // Some other use for the allocation
1987 return false;
1988 } else if (n->is_InlineType()) {
1989 wq.push(n->in(1));
1990 } else if (n->is_Phi()) {
1991 for (uint j = 1; j < n->req(); j++) {
1992 wq.push(n->in(j));
1993 }
1994 } else if (n->is_CheckCastPP() &&
1995 n->in(1)->is_Proj() &&
1996 n->in(1)->in(0)->is_Allocate()) {
1997 some_allocations = true;
1998 } else if (n->is_CheckCastPP()) {
1999 wq.push(n->in(1));
2000 }
2001 }
2002 return some_allocations;
2003 }
2004
2005 void Compile::process_inline_types(PhaseIterGVN &igvn, bool remove) {
2006 // Make sure that the return value does not keep an otherwise unused allocation alive
2007 if (tf()->returns_inline_type_as_fields()) {
2008 Node* ret = nullptr;
2009 for (uint i = 1; i < root()->req(); i++) {
2010 Node* in = root()->in(i);
2011 if (in->Opcode() == Op_Return) {
2012 assert(ret == nullptr, "only one return");
2013 ret = in;
2014 }
2015 }
2016 if (ret != nullptr) {
2017 Node* ret_val = ret->in(TypeFunc::Parms);
2018 if (igvn.type(ret_val)->isa_oopptr() &&
2019 return_val_keeps_allocations_alive(ret_val)) {
2020 igvn.replace_input_of(ret, TypeFunc::Parms, InlineTypeNode::tagged_klass(igvn.type(ret_val)->inline_klass(), igvn));
2021 assert(ret_val->outcnt() == 0, "should be dead now");
2022 igvn.remove_dead_node(ret_val);
2023 }
2024 }
2025 }
2026 if (_inline_type_nodes.length() == 0) {
2027 return;
2028 }
2029 // Scalarize inline types in safepoint debug info.
2030 // Delay this until all inlining is over to avoid getting inconsistent debug info.
2031 set_scalarize_in_safepoints(true);
2032 for (int i = _inline_type_nodes.length()-1; i >= 0; i--) {
2033 InlineTypeNode* vt = _inline_type_nodes.at(i)->as_InlineType();
2034 vt->make_scalar_in_safepoints(&igvn);
2035 igvn.record_for_igvn(vt);
2036 }
2037 if (remove) {
2038 // Remove inline type nodes by replacing them with their oop input
2039 while (_inline_type_nodes.length() > 0) {
2040 InlineTypeNode* vt = _inline_type_nodes.pop()->as_InlineType();
2041 if (vt->outcnt() == 0) {
2042 igvn.remove_dead_node(vt);
2043 continue;
2044 }
2045 for (DUIterator i = vt->outs(); vt->has_out(i); i++) {
2046 DEBUG_ONLY(bool must_be_buffered = false);
2047 Node* u = vt->out(i);
2048 // Check if any users are blackholes. If so, rewrite them to use either the
2049 // allocated buffer, or individual components, instead of the inline type node
2050 // that goes away.
2051 if (u->is_Blackhole()) {
2052 BlackholeNode* bh = u->as_Blackhole();
2053
2054 // Unlink the old input
2055 int idx = bh->find_edge(vt);
2056 assert(idx != -1, "The edge should be there");
2057 bh->del_req(idx);
2058 --i;
2059
2060 if (vt->is_allocated(&igvn)) {
2061 // Already has the allocated instance, blackhole that
2062 bh->add_req(vt->get_oop());
2063 } else {
2064 // Not allocated yet, blackhole the components
2065 for (uint c = 0; c < vt->field_count(); c++) {
2066 bh->add_req(vt->field_value(c));
2067 }
2068 }
2069
2070 // Node modified, record for IGVN
2071 igvn.record_for_igvn(bh);
2072 }
2073 #ifdef ASSERT
2074 // Verify that inline type is buffered when replacing by oop
2075 else if (u->is_InlineType()) {
2076 // InlineType uses don't need buffering because they are about to be replaced as well
2077 } else if (u->is_Phi()) {
2078 // TODO 8302217 Remove this once InlineTypeNodes are reliably pushed through
2079 } else {
2080 must_be_buffered = true;
2081 }
2082 if (must_be_buffered && !vt->is_allocated(&igvn)) {
2083 vt->dump(0);
2084 u->dump(0);
2085 assert(false, "Should have been buffered");
2086 }
2087 #endif
2088 }
2089 igvn.replace_node(vt, vt->get_oop());
2090 }
2091 }
2092 igvn.optimize();
2093 }
2094
2095 void Compile::adjust_flat_array_access_aliases(PhaseIterGVN& igvn) {
2096 if (!_has_flat_accesses) {
2097 return;
2098 }
2099 // Initially, all flat array accesses share the same slice to
2100 // keep dependencies with Object[] array accesses (that could be
2101 // to a flat array) correct. We're done with parsing so we
2102 // now know all flat array accesses in this compile
2103 // unit. Let's move flat array accesses to their own slice,
2104 // one per element field. This should help memory access
2105 // optimizations.
2106 ResourceMark rm;
2107 Unique_Node_List wq;
2108 wq.push(root());
2109
2110 Node_List mergememnodes;
2111 Node_List memnodes;
2112
2113 // Alias index currently shared by all flat memory accesses
2114 int index = get_alias_index(TypeAryPtr::INLINES);
2115
2116 // Find MergeMem nodes and flat array accesses
2117 for (uint i = 0; i < wq.size(); i++) {
2118 Node* n = wq.at(i);
2119 if (n->is_Mem()) {
2120 const TypePtr* adr_type = nullptr;
2121 adr_type = get_adr_type(get_alias_index(n->adr_type()));
2122 if (adr_type == TypeAryPtr::INLINES) {
2123 memnodes.push(n);
2124 }
2125 } else if (n->is_MergeMem()) {
2126 MergeMemNode* mm = n->as_MergeMem();
2127 if (mm->memory_at(index) != mm->base_memory()) {
2128 mergememnodes.push(n);
2129 }
2130 }
2131 for (uint j = 0; j < n->req(); j++) {
2132 Node* m = n->in(j);
2133 if (m != nullptr) {
2134 wq.push(m);
2135 }
2136 }
2137 }
2138
2139 if (memnodes.size() > 0) {
2140 _flat_accesses_share_alias = false;
2141
2142 // We are going to change the slice for the flat array
2143 // accesses so we need to clear the cache entries that refer to
2144 // them.
2145 for (uint i = 0; i < AliasCacheSize; i++) {
2146 AliasCacheEntry* ace = &_alias_cache[i];
2147 if (ace->_adr_type != nullptr &&
2148 ace->_adr_type->is_flat()) {
2149 ace->_adr_type = nullptr;
2150 ace->_index = (i != 0) ? 0 : AliasIdxTop; // Make sure the nullptr adr_type resolves to AliasIdxTop
2151 }
2152 }
2153
2154 // Find what aliases we are going to add
2155 int start_alias = num_alias_types()-1;
2156 int stop_alias = 0;
2157
2158 for (uint i = 0; i < memnodes.size(); i++) {
2159 Node* m = memnodes.at(i);
2160 const TypePtr* adr_type = nullptr;
2161 adr_type = m->adr_type();
2162 #ifdef ASSERT
2163 m->as_Mem()->set_adr_type(adr_type);
2164 #endif
2165 int idx = get_alias_index(adr_type);
2166 start_alias = MIN2(start_alias, idx);
2167 stop_alias = MAX2(stop_alias, idx);
2168 }
2169
2170 assert(stop_alias >= start_alias, "should have expanded aliases");
2171
2172 Node_Stack stack(0);
2173 #ifdef ASSERT
2174 VectorSet seen(Thread::current()->resource_area());
2175 #endif
2176 // Now let's fix the memory graph so each flat array access
2177 // is moved to the right slice. Start from the MergeMem nodes.
2178 uint last = unique();
2179 for (uint i = 0; i < mergememnodes.size(); i++) {
2180 MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2181 Node* n = current->memory_at(index);
2182 MergeMemNode* mm = nullptr;
2183 do {
2184 // Follow memory edges through memory accesses, phis and
2185 // narrow membars and push nodes on the stack. Once we hit
2186 // bottom memory, we pop element off the stack one at a
2187 // time, in reverse order, and move them to the right slice
2188 // by changing their memory edges.
2189 if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() || n->adr_type() == TypeAryPtr::INLINES) {
2190 assert(!seen.test_set(n->_idx), "");
2191 // Uses (a load for instance) will need to be moved to the
2192 // right slice as well and will get a new memory state
2193 // that we don't know yet. The use could also be the
2194 // backedge of a loop. We put a place holder node between
2195 // the memory node and its uses. We replace that place
2196 // holder with the correct memory state once we know it,
2197 // i.e. when nodes are popped off the stack. Using the
2198 // place holder make the logic work in the presence of
2199 // loops.
2200 if (n->outcnt() > 1) {
2201 Node* place_holder = nullptr;
2202 assert(!n->has_out_with(Op_Node), "");
2203 for (DUIterator k = n->outs(); n->has_out(k); k++) {
2204 Node* u = n->out(k);
2205 if (u != current && u->_idx < last) {
2206 bool success = false;
2207 for (uint l = 0; l < u->req(); l++) {
2208 if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2209 continue;
2210 }
2211 Node* in = u->in(l);
2212 if (in == n) {
2213 if (place_holder == nullptr) {
2214 place_holder = new Node(1);
2215 place_holder->init_req(0, n);
2216 }
2217 igvn.replace_input_of(u, l, place_holder);
2218 success = true;
2219 }
2220 }
2221 if (success) {
2222 --k;
2223 }
2224 }
2225 }
2226 }
2227 if (n->is_Phi()) {
2228 stack.push(n, 1);
2229 n = n->in(1);
2230 } else if (n->is_Mem()) {
2231 stack.push(n, n->req());
2232 n = n->in(MemNode::Memory);
2233 } else {
2234 assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2235 stack.push(n, n->req());
2236 n = n->in(0)->in(TypeFunc::Memory);
2237 }
2238 } else {
2239 assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || (n->is_Proj() && n->in(0)->is_Initialize()), "");
2240 // Build a new MergeMem node to carry the new memory state
2241 // as we build it. IGVN should fold extraneous MergeMem
2242 // nodes.
2243 mm = MergeMemNode::make(n);
2244 igvn.register_new_node_with_optimizer(mm);
2245 while (stack.size() > 0) {
2246 Node* m = stack.node();
2247 uint idx = stack.index();
2248 if (m->is_Mem()) {
2249 // Move memory node to its new slice
2250 const TypePtr* adr_type = m->adr_type();
2251 int alias = get_alias_index(adr_type);
2252 Node* prev = mm->memory_at(alias);
2253 igvn.replace_input_of(m, MemNode::Memory, prev);
2254 mm->set_memory_at(alias, m);
2255 } else if (m->is_Phi()) {
2256 // We need as many new phis as there are new aliases
2257 igvn.replace_input_of(m, idx, mm);
2258 if (idx == m->req()-1) {
2259 Node* r = m->in(0);
2260 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2261 const TypePtr* adr_type = get_adr_type(j);
2262 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2263 continue;
2264 }
2265 Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2266 igvn.register_new_node_with_optimizer(phi);
2267 for (uint k = 1; k < m->req(); k++) {
2268 phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2269 }
2270 mm->set_memory_at(j, phi);
2271 }
2272 Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2273 igvn.register_new_node_with_optimizer(base_phi);
2274 for (uint k = 1; k < m->req(); k++) {
2275 base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2276 }
2277 mm->set_base_memory(base_phi);
2278 }
2279 } else {
2280 // This is a MemBarCPUOrder node from
2281 // Parse::array_load()/Parse::array_store(), in the
2282 // branch that handles flat arrays hidden under
2283 // an Object[] array. We also need one new membar per
2284 // new alias to keep the unknown access that the
2285 // membars protect properly ordered with accesses to
2286 // known flat array.
2287 assert(m->is_Proj(), "projection expected");
2288 Node* ctrl = m->in(0)->in(TypeFunc::Control);
2289 igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2290 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2291 const TypePtr* adr_type = get_adr_type(j);
2292 if (!adr_type->isa_aryptr() || !adr_type->is_flat() || j == (uint)index) {
2293 continue;
2294 }
2295 MemBarNode* mb = new MemBarCPUOrderNode(this, j, nullptr);
2296 igvn.register_new_node_with_optimizer(mb);
2297 Node* mem = mm->memory_at(j);
2298 mb->init_req(TypeFunc::Control, ctrl);
2299 mb->init_req(TypeFunc::Memory, mem);
2300 ctrl = new ProjNode(mb, TypeFunc::Control);
2301 igvn.register_new_node_with_optimizer(ctrl);
2302 mem = new ProjNode(mb, TypeFunc::Memory);
2303 igvn.register_new_node_with_optimizer(mem);
2304 mm->set_memory_at(j, mem);
2305 }
2306 igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2307 }
2308 if (idx < m->req()-1) {
2309 idx += 1;
2310 stack.set_index(idx);
2311 n = m->in(idx);
2312 break;
2313 }
2314 // Take care of place holder nodes
2315 if (m->has_out_with(Op_Node)) {
2316 Node* place_holder = m->find_out_with(Op_Node);
2317 if (place_holder != nullptr) {
2318 Node* mm_clone = mm->clone();
2319 igvn.register_new_node_with_optimizer(mm_clone);
2320 Node* hook = new Node(1);
2321 hook->init_req(0, mm);
2322 igvn.replace_node(place_holder, mm_clone);
2323 hook->destruct(&igvn);
2324 }
2325 assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2326 }
2327 stack.pop();
2328 }
2329 }
2330 } while(stack.size() > 0);
2331 // Fix the memory state at the MergeMem we started from
2332 igvn.rehash_node_delayed(current);
2333 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2334 const TypePtr* adr_type = get_adr_type(j);
2335 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2336 continue;
2337 }
2338 current->set_memory_at(j, mm);
2339 }
2340 current->set_memory_at(index, current->base_memory());
2341 }
2342 igvn.optimize();
2343 }
2344 print_method(PHASE_SPLIT_INLINES_ARRAY, 2);
2345 #ifdef ASSERT
2346 if (!_flat_accesses_share_alias) {
2347 wq.clear();
2348 wq.push(root());
2349 for (uint i = 0; i < wq.size(); i++) {
2350 Node* n = wq.at(i);
2351 assert(n->adr_type() != TypeAryPtr::INLINES, "should have been removed from the graph");
2352 for (uint j = 0; j < n->req(); j++) {
2353 Node* m = n->in(j);
2354 if (m != nullptr) {
2355 wq.push(m);
2356 }
2357 }
2358 }
2359 }
2360 #endif
2361 }
2362
2363 void Compile::record_for_merge_stores_igvn(Node* n) {
2364 if (!n->for_merge_stores_igvn()) {
2365 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
2366 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2367 _for_merge_stores_igvn.append(n);
2368 }
2369 }
2370
2371 void Compile::remove_from_merge_stores_igvn(Node* n) {
2372 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2373 _for_merge_stores_igvn.remove(n);
2374 }
2375
2376 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
2377 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
2378 // the stores, and we merge the wrong sequence of stores.
2379 // Example:
2380 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
2381 // Apply MergeStores:
2382 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
2461 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
2462 Bytecodes::Code c = iter.cur_bc();
2463 Node* lhs = nullptr;
2464 Node* rhs = nullptr;
2465 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
2466 lhs = unc->peek_operand(0);
2467 rhs = unc->peek_operand(1);
2468 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2469 lhs = unc->peek_operand(0);
2470 }
2471
2472 ResourceMark rm;
2473 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2474 assert(live_locals.is_valid(), "broken liveness info");
2475 int len = (int)live_locals.size();
2476
2477 for (int i = 0; i < len; i++) {
2478 Node* local = unc->local(jvms, i);
2479 // kill local using the liveness of next_bci.
2480 // give up when the local looks like an operand to secure reexecution.
2481 if (!live_locals.at(i) && !local->is_top() && local != lhs && local != rhs) {
2482 uint idx = jvms->locoff() + i;
2483 #ifdef ASSERT
2484 if (PrintOpto && Verbose) {
2485 tty->print("[unstable_if] kill local#%d: ", idx);
2486 local->dump();
2487 tty->cr();
2488 }
2489 #endif
2490 igvn.replace_input_of(unc, idx, top());
2491 modified = true;
2492 }
2493 }
2494 }
2495
2496 // keep the modified trap for late query
2497 if (modified) {
2498 trap->set_modified();
2499 } else {
2500 _unstable_if_traps.delete_at(i);
2501 }
2502 }
2503 igvn.optimize();
2504 }
2505
2506 // StringOpts and late inlining of string methods
2507 void Compile::inline_string_calls(bool parse_time) {
2508 {
2509 // remove useless nodes to make the usage analysis simpler
2510 ResourceMark rm;
2511 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2512 }
2513
2514 {
2515 ResourceMark rm;
2516 print_method(PHASE_BEFORE_STRINGOPTS, 3);
2682
2683 if (_string_late_inlines.length() > 0) {
2684 assert(has_stringbuilder(), "inconsistent");
2685
2686 inline_string_calls(false);
2687
2688 if (failing()) return;
2689
2690 inline_incrementally_cleanup(igvn);
2691 }
2692
2693 set_inlining_incrementally(false);
2694 }
2695
2696 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2697 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2698 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2699 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2700 // as if "inlining_incrementally() == true" were set.
2701 assert(inlining_incrementally() == false, "not allowed");
2702 #ifdef ASSERT
2703 Unique_Node_List* modified_nodes = _modified_nodes;
2704 _modified_nodes = nullptr;
2705 #endif
2706 assert(_late_inlines.length() > 0, "sanity");
2707
2708 while (_late_inlines.length() > 0) {
2709 igvn_worklist()->ensure_empty(); // should be done with igvn
2710
2711 while (inline_incrementally_one()) {
2712 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2713 }
2714 if (failing()) return;
2715
2716 inline_incrementally_cleanup(igvn);
2717 }
2718 DEBUG_ONLY( _modified_nodes = modified_nodes; )
2719 }
2720
2721 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2722 if (_loop_opts_cnt > 0) {
2723 while (major_progress() && (_loop_opts_cnt > 0)) {
2724 TracePhase tp(_t_idealLoop);
2725 PhaseIdealLoop::optimize(igvn, mode);
2726 _loop_opts_cnt--;
2727 if (failing()) return false;
2728 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2729 }
2730 }
2731 return true;
2732 }
2733
2734 // Remove edges from "root" to each SafePoint at a backward branch.
2735 // They were inserted during parsing (see add_safepoint()) to make
2736 // infinite loops without calls or exceptions visible to root, i.e.,
2737 // useful.
2738 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_from_gvn(initial_gvn());
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 }
3833 int nop = n->Opcode();
3834 // Clone shared simple arguments to uncommon calls, item (1).
3835 if (n->outcnt() > 1 &&
3836 !n->is_Proj() &&
3837 nop != Op_CreateEx &&
3838 nop != Op_CheckCastPP &&
3839 nop != Op_DecodeN &&
3840 nop != Op_DecodeNKlass &&
3841 !n->is_Mem() &&
3842 !n->is_Phi()) {
3843 Node *x = n->clone();
3844 call->set_req(TypeFunc::Parms, x);
3845 }
3846 }
3847 break;
3848 }
3849 case Op_StoreB:
3850 case Op_StoreC:
3851 case Op_StoreI:
3852 case Op_StoreL:
3853 case Op_StoreLSpecial:
3854 case Op_CompareAndSwapB:
3855 case Op_CompareAndSwapS:
3856 case Op_CompareAndSwapI:
3857 case Op_CompareAndSwapL:
3858 case Op_CompareAndSwapP:
3859 case Op_CompareAndSwapN:
3860 case Op_WeakCompareAndSwapB:
3861 case Op_WeakCompareAndSwapS:
3862 case Op_WeakCompareAndSwapI:
3863 case Op_WeakCompareAndSwapL:
3864 case Op_WeakCompareAndSwapP:
3865 case Op_WeakCompareAndSwapN:
3866 case Op_CompareAndExchangeB:
3867 case Op_CompareAndExchangeS:
3868 case Op_CompareAndExchangeI:
3869 case Op_CompareAndExchangeL:
3870 case Op_CompareAndExchangeP:
3871 case Op_CompareAndExchangeN:
3872 case Op_GetAndAddS:
3873 case Op_GetAndAddB:
4377 k->subsume_by(m, this);
4378 }
4379 }
4380 }
4381 break;
4382 }
4383 case Op_CmpUL: {
4384 if (!Matcher::has_match_rule(Op_CmpUL)) {
4385 // No support for unsigned long comparisons
4386 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
4387 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
4388 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
4389 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
4390 Node* andl = new AndLNode(orl, remove_sign_mask);
4391 Node* cmp = new CmpLNode(andl, n->in(2));
4392 n->subsume_by(cmp, this);
4393 }
4394 break;
4395 }
4396 #ifdef ASSERT
4397 case Op_InlineType: {
4398 n->dump(-1);
4399 assert(false, "inline type node was not removed");
4400 break;
4401 }
4402 case Op_ConNKlass: {
4403 const TypePtr* tp = n->as_Type()->type()->make_ptr();
4404 ciKlass* klass = tp->is_klassptr()->exact_klass();
4405 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
4406 break;
4407 }
4408 #endif
4409 default:
4410 assert(!n->is_Call(), "");
4411 assert(!n->is_Mem(), "");
4412 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4413 break;
4414 }
4415 }
4416
4417 //------------------------------final_graph_reshaping_walk---------------------
4418 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4419 // requires that the walk visits a node's inputs before visiting the node.
4420 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
4421 Unique_Node_List sfpt;
4757 }
4758 }
4759
4760 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4761 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4762 }
4763
4764 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4765 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4766 }
4767
4768 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4769 if (holder->is_initialized()) {
4770 return false;
4771 }
4772 if (holder->is_being_initialized()) {
4773 if (accessing_method->holder() == holder) {
4774 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4775 // <init>, or a static method. In all those cases, there was an initialization
4776 // barrier on the holder klass passed.
4777 if (accessing_method->is_class_initializer() ||
4778 accessing_method->is_object_constructor() ||
4779 accessing_method->is_static()) {
4780 return false;
4781 }
4782 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4783 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4784 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4785 // child class can become fully initialized while its parent class is still being initialized.
4786 if (accessing_method->is_class_initializer()) {
4787 return false;
4788 }
4789 }
4790 ciMethod* root = method(); // the root method of compilation
4791 if (root != accessing_method) {
4792 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4793 }
4794 }
4795 return true;
4796 }
4797
4798 #ifndef PRODUCT
4799 //------------------------------verify_bidirectional_edges---------------------
4800 // For each input edge to a node (ie - for each Use-Def edge), verify that
4801 // there is a corresponding Def-Use edge.
4802 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4803 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4804 uint stack_size = live_nodes() >> 4;
4805 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4806 if (root_and_safepoints != nullptr) {
4836 if (in != nullptr && !in->is_top()) {
4837 // Count instances of `next`
4838 int cnt = 0;
4839 for (uint idx = 0; idx < in->_outcnt; idx++) {
4840 if (in->_out[idx] == n) {
4841 cnt++;
4842 }
4843 }
4844 assert(cnt > 0, "Failed to find Def-Use edge.");
4845 // Check for duplicate edges
4846 // walk the input array downcounting the input edges to n
4847 for (uint j = 0; j < length; j++) {
4848 if (n->in(j) == in) {
4849 cnt--;
4850 }
4851 }
4852 assert(cnt == 0, "Mismatched edge count.");
4853 } else if (in == nullptr) {
4854 assert(i == 0 || i >= n->req() ||
4855 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4856 (n->is_Allocate() && i >= AllocateNode::InlineType) ||
4857 (n->is_Unlock() && i == (n->req() - 1)) ||
4858 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4859 "only region, phi, arraycopy, allocate, unlock or membar nodes have null data edges");
4860 } else {
4861 assert(in->is_top(), "sanity");
4862 // Nothing to check.
4863 }
4864 }
4865 }
4866 }
4867
4868 //------------------------------verify_graph_edges---------------------------
4869 // Walk the Graph and verify that there is a one-to-one correspondence
4870 // between Use-Def edges and Def-Use edges in the graph.
4871 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4872 if (VerifyGraphEdges) {
4873 Unique_Node_List visited;
4874
4875 // Call graph walk to check edges
4876 verify_bidirectional_edges(visited, root_and_safepoints);
4877 if (no_dead_code) {
4878 // Now make sure that no visited node is used by an unvisited node.
4879 bool dead_nodes = false;
4990 // (1) subklass is already limited to a subtype of superklass => always ok
4991 // (2) subklass does not overlap with superklass => always fail
4992 // (3) superklass has NO subtypes and we can check with a simple compare.
4993 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4994 if (skip) {
4995 return SSC_full_test; // Let caller generate the general case.
4996 }
4997
4998 if (subk->is_java_subtype_of(superk)) {
4999 return SSC_always_true; // (0) and (1) this test cannot fail
5000 }
5001
5002 if (!subk->maybe_java_subtype_of(superk)) {
5003 return SSC_always_false; // (2) true path dead; no dynamic test needed
5004 }
5005
5006 const Type* superelem = superk;
5007 if (superk->isa_aryklassptr()) {
5008 int ignored;
5009 superelem = superk->is_aryklassptr()->base_element_type(ignored);
5010
5011 // Do not fold the subtype check to an array klass pointer comparison for null-able inline type arrays
5012 // because null-free [LMyValue <: null-able [LMyValue but the klasses are different. Perform a full test.
5013 if (!superk->is_aryklassptr()->is_null_free() && superk->is_aryklassptr()->elem()->isa_instklassptr() &&
5014 superk->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->is_inlinetype()) {
5015 return SSC_full_test;
5016 }
5017 }
5018
5019 if (superelem->isa_instklassptr()) {
5020 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
5021 if (!ik->has_subklass()) {
5022 if (!ik->is_final()) {
5023 // Add a dependency if there is a chance of a later subclass.
5024 dependencies()->assert_leaf_type(ik);
5025 }
5026 if (!superk->maybe_java_subtype_of(subk)) {
5027 return SSC_always_false;
5028 }
5029 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5030 }
5031 } else {
5032 // A primitive array type has no subtypes.
5033 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5034 }
5035
5036 return SSC_full_test;
5480 const Type* t = igvn.type_or_null(n);
5481 assert((t == nullptr) || (t == t->remove_speculative()), "no more speculative types");
5482 if (n->is_Type()) {
5483 t = n->as_Type()->type();
5484 assert(t == t->remove_speculative(), "no more speculative types");
5485 }
5486 // Iterate over outs - endless loops is unreachable from below
5487 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
5488 Node *m = n->fast_out(i);
5489 if (not_a_node(m)) {
5490 continue;
5491 }
5492 worklist.push(m);
5493 }
5494 }
5495 igvn.check_no_speculative_types();
5496 #endif
5497 }
5498 }
5499
5500 Node* Compile::optimize_acmp(PhaseGVN* phase, Node* a, Node* b) {
5501 const TypeInstPtr* ta = phase->type(a)->isa_instptr();
5502 const TypeInstPtr* tb = phase->type(b)->isa_instptr();
5503 if (!EnableValhalla || ta == nullptr || tb == nullptr ||
5504 ta->is_zero_type() || tb->is_zero_type() ||
5505 !ta->can_be_inline_type() || !tb->can_be_inline_type()) {
5506 // Use old acmp if one operand is null or not an inline type
5507 return new CmpPNode(a, b);
5508 } else if (ta->is_inlinetypeptr() || tb->is_inlinetypeptr()) {
5509 // We know that one operand is an inline type. Therefore,
5510 // new acmp will only return true if both operands are nullptr.
5511 // Check if both operands are null by or'ing the oops.
5512 a = phase->transform(new CastP2XNode(nullptr, a));
5513 b = phase->transform(new CastP2XNode(nullptr, b));
5514 a = phase->transform(new OrXNode(a, b));
5515 return new CmpXNode(a, phase->MakeConX(0));
5516 }
5517 // Use new acmp
5518 return nullptr;
5519 }
5520
5521 // Auxiliary methods to support randomized stressing/fuzzing.
5522
5523 void Compile::initialize_stress_seed(const DirectiveSet* directive) {
5524 if (FLAG_IS_DEFAULT(StressSeed) || (FLAG_IS_ERGO(StressSeed) && directive->RepeatCompilationOption)) {
5525 _stress_seed = static_cast<uint>(Ticks::now().nanoseconds());
5526 FLAG_SET_ERGO(StressSeed, _stress_seed);
5527 } else {
5528 _stress_seed = StressSeed;
5529 }
5530 if (_log != nullptr) {
5531 _log->elem("stress_test seed='%u'", _stress_seed);
5532 }
5533 }
5534
5535 int Compile::random() {
5536 _stress_seed = os::next_random(_stress_seed);
5537 return static_cast<int>(_stress_seed);
5538 }
5539
5540 // This method can be called the arbitrary number of times, with current count
5856 } else {
5857 _debug_network_printer->update_compiled_method(C->method());
5858 }
5859 tty->print_cr("Method printed over network stream to IGV");
5860 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5861 }
5862 #endif // !PRODUCT
5863
5864 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5865 if (type != nullptr && phase->type(value)->higher_equal(type)) {
5866 return value;
5867 }
5868 Node* result = nullptr;
5869 if (bt == T_BYTE) {
5870 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5871 result = new RShiftINode(result, phase->intcon(24));
5872 } else if (bt == T_BOOLEAN) {
5873 result = new AndINode(value, phase->intcon(0xFF));
5874 } else if (bt == T_CHAR) {
5875 result = new AndINode(value,phase->intcon(0xFFFF));
5876 } else if (bt == T_FLOAT) {
5877 result = new MoveI2FNode(value);
5878 } else {
5879 assert(bt == T_SHORT, "unexpected narrow type");
5880 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5881 result = new RShiftINode(result, phase->intcon(16));
5882 }
5883 if (transform_res) {
5884 result = phase->transform(result);
5885 }
5886 return result;
5887 }
5888
5889 void Compile::record_method_not_compilable_oom() {
5890 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5891 }
|