7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "asm/macroAssembler.hpp"
26 #include "asm/macroAssembler.inline.hpp"
27 #include "ci/ciReplay.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "code/aotCodeCache.hpp"
30 #include "code/exceptionHandlerTable.hpp"
31 #include "code/nmethod.hpp"
32 #include "compiler/compilationFailureInfo.hpp"
33 #include "compiler/compilationMemoryStatistic.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "compiler/compileLog.hpp"
36 #include "compiler/compiler_globals.hpp"
37 #include "compiler/compilerDefinitions.hpp"
38 #include "compiler/compilerOracle.hpp"
39 #include "compiler/disassembler.hpp"
40 #include "compiler/oopMap.hpp"
41 #include "gc/shared/barrierSet.hpp"
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"
89 #include "utilities/copy.hpp"
90 #include "utilities/hashTable.hpp"
91 #include "utilities/macros.hpp"
92
93 // -------------------- Compile::mach_constant_base_node -----------------------
94 // Constant table base node singleton.
95 MachConstantBaseNode* Compile::mach_constant_base_node() {
96 if (_mach_constant_base_node == nullptr) {
97 _mach_constant_base_node = new MachConstantBaseNode();
98 _mach_constant_base_node->add_req(C->root());
99 }
100 return _mach_constant_base_node;
101 }
102
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
630
631 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
632 Options options, DirectiveSet* directive)
633 : Phase(Compiler),
634 _compile_id(ci_env->compile_id()),
635 _options(options),
636 _method(target),
637 _entry_bci(osr_bci),
638 _ilt(nullptr),
639 _stub_function(nullptr),
640 _stub_name(nullptr),
641 _stub_id(StubId::NO_STUBID),
642 _stub_entry_point(nullptr),
643 _max_node_limit(MaxNodeLimit),
644 _post_loop_opts_phase(false),
645 _merge_stores_phase(false),
646 _allow_macro_nodes(true),
647 _inlining_progress(false),
648 _inlining_incrementally(false),
649 _do_cleanup(false),
650 _has_reserved_stack_access(target->has_reserved_stack_access()),
651 #ifndef PRODUCT
652 _igv_idx(0),
653 _trace_opto_output(directive->TraceOptoOutputOption),
654 #endif
655 _clinit_barrier_on_entry(false),
656 _stress_seed(0),
657 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
658 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
659 _env(ci_env),
660 _directive(directive),
661 _log(ci_env->log()),
662 _first_failure_details(nullptr),
663 _intrinsics(comp_arena(), 0, 0, nullptr),
664 _macro_nodes(comp_arena(), 8, 0, nullptr),
665 _parse_predicates(comp_arena(), 8, 0, nullptr),
666 _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
667 _expensive_nodes(comp_arena(), 8, 0, nullptr),
668 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
669 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
670 _unstable_if_traps(comp_arena(), 8, 0, nullptr),
671 _coarsened_locks(comp_arena(), 8, 0, nullptr),
672 _congraph(nullptr),
673 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
674 _unique(0),
675 _dead_node_count(0),
676 _dead_node_list(comp_arena()),
677 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
678 _node_arena_two(mtCompiler, Arena::Tag::tag_node),
679 _node_arena(&_node_arena_one),
680 _mach_constant_base_node(nullptr),
681 _Compile_types(mtCompiler, Arena::Tag::tag_type),
682 _initial_gvn(nullptr),
683 _igvn_worklist(nullptr),
684 _types(nullptr),
685 _node_hash(nullptr),
686 _late_inlines(comp_arena(), 2, 0, nullptr),
687 _string_late_inlines(comp_arena(), 2, 0, nullptr),
688 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
757 #define MINIMUM_NODE_HASH 1023
758
759 // GVN that will be run immediately on new nodes
760 uint estimated_size = method()->code_size()*4+64;
761 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
762 _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
763 _types = new (comp_arena()) Type_Array(comp_arena());
764 _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
765 PhaseGVN gvn;
766 set_initial_gvn(&gvn);
767
768 { // Scope for timing the parser
769 TracePhase tp(_t_parser);
770
771 // Put top into the hash table ASAP.
772 initial_gvn()->transform(top());
773
774 // Set up tf(), start(), and find a CallGenerator.
775 CallGenerator* cg = nullptr;
776 if (is_osr_compilation()) {
777 const TypeTuple *domain = StartOSRNode::osr_domain();
778 const TypeTuple *range = TypeTuple::make_range(method()->signature());
779 init_tf(TypeFunc::make(domain, range));
780 StartNode* s = new StartOSRNode(root(), domain);
781 initial_gvn()->set_type_bottom(s);
782 verify_start(s);
783 cg = CallGenerator::for_osr(method(), entry_bci());
784 } else {
785 // Normal case.
786 init_tf(TypeFunc::make(method()));
787 StartNode* s = new StartNode(root(), tf()->domain());
788 initial_gvn()->set_type_bottom(s);
789 verify_start(s);
790 float past_uses = method()->interpreter_invocation_count();
791 float expected_uses = past_uses;
792 cg = CallGenerator::for_inline(method(), expected_uses);
793 }
794 if (failing()) return;
795 if (cg == nullptr) {
796 const char* reason = InlineTree::check_can_parse(method());
797 assert(reason != nullptr, "expect reason for parse failure");
798 stringStream ss;
799 ss.print("cannot parse method: %s", reason);
800 record_method_not_compilable(ss.as_string());
801 return;
802 }
803
804 gvn.set_type(root(), root()->bottom_type());
805
806 JVMState* jvms = build_start_state(start(), tf());
807 if ((jvms = cg->generate(jvms)) == nullptr) {
868 print_ideal_ir("PrintIdeal");
869 }
870 #endif
871
872 #ifdef ASSERT
873 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
874 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
875 #endif
876
877 // Dump compilation data to replay it.
878 if (directive->DumpReplayOption) {
879 env()->dump_replay_data(_compile_id);
880 }
881 if (directive->DumpInlineOption && (ilt() != nullptr)) {
882 env()->dump_inline_data(_compile_id);
883 }
884
885 // Now that we know the size of all the monitors we can add a fixed slot
886 // for the original deopt pc.
887 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
888 set_fixed_slots(next_slot);
889
890 // Compute when to use implicit null checks. Used by matching trap based
891 // nodes and NullCheck optimization.
892 set_allowed_deopt_reasons();
893
894 // Now generate code
895 Code_Gen();
896 }
897
898 //------------------------------Compile----------------------------------------
899 // Compile a runtime stub
900 Compile::Compile(ciEnv* ci_env,
901 TypeFunc_generator generator,
902 address stub_function,
903 const char* stub_name,
904 StubId stub_id,
905 int is_fancy_jump,
906 bool pass_tls,
907 bool return_pc,
908 DirectiveSet* directive)
909 : Phase(Compiler),
910 _compile_id(0),
911 _options(Options::for_runtime_stub()),
912 _method(nullptr),
913 _entry_bci(InvocationEntryBci),
914 _stub_function(stub_function),
915 _stub_name(stub_name),
916 _stub_id(stub_id),
917 _stub_entry_point(nullptr),
918 _max_node_limit(MaxNodeLimit),
919 _post_loop_opts_phase(false),
920 _merge_stores_phase(false),
921 _allow_macro_nodes(true),
922 _inlining_progress(false),
923 _inlining_incrementally(false),
924 _has_reserved_stack_access(false),
925 #ifndef PRODUCT
926 _igv_idx(0),
927 _trace_opto_output(directive->TraceOptoOutputOption),
928 #endif
929 _clinit_barrier_on_entry(false),
930 _stress_seed(0),
931 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
932 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
933 _env(ci_env),
934 _directive(directive),
935 _log(ci_env->log()),
936 _first_failure_details(nullptr),
937 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
938 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
939 _congraph(nullptr),
940 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
941 _unique(0),
942 _dead_node_count(0),
943 _dead_node_list(comp_arena()),
944 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
1059 _fixed_slots = 0;
1060 set_has_split_ifs(false);
1061 set_has_loops(false); // first approximation
1062 set_has_stringbuilder(false);
1063 set_has_boxed_value(false);
1064 _trap_can_recompile = false; // no traps emitted yet
1065 _major_progress = true; // start out assuming good things will happen
1066 set_has_unsafe_access(false);
1067 set_max_vector_size(0);
1068 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1069 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1070 set_decompile_count(0);
1071
1072 #ifndef PRODUCT
1073 _phase_counter = 0;
1074 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1075 #endif
1076
1077 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1078 _loop_opts_cnt = LoopOptsCount;
1079 set_do_inlining(Inline);
1080 set_max_inline_size(MaxInlineSize);
1081 set_freq_inline_size(FreqInlineSize);
1082 set_do_scheduling(OptoScheduling);
1083
1084 set_do_vector_loop(false);
1085 set_has_monitors(false);
1086 set_has_scoped_access(false);
1087
1088 if (AllowVectorizeOnDemand) {
1089 if (has_method() && _directive->VectorizeOption) {
1090 set_do_vector_loop(true);
1091 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());})
1092 } else if (has_method() && method()->name() != nullptr &&
1093 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1094 set_do_vector_loop(true);
1095 }
1096 }
1097 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1098 NOT_PRODUCT(if (use_cmove() && Verbose && has_method()) {tty->print("Compile::Init: use CMove without profitability tests for method %s\n", method()->name()->as_quoted_ascii());})
1339 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1340 assert(do_aliasing(), "Aliasing should be enabled");
1341 int offset = tj->offset();
1342 TypePtr::PTR ptr = tj->ptr();
1343
1344 // Known instance (scalarizable allocation) alias only with itself.
1345 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1346 tj->is_oopptr()->is_known_instance();
1347
1348 // Process weird unsafe references.
1349 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1350 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1351 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1352 tj = TypeOopPtr::BOTTOM;
1353 ptr = tj->ptr();
1354 offset = tj->offset();
1355 }
1356
1357 // Array pointers need some flattening
1358 const TypeAryPtr* ta = tj->isa_aryptr();
1359 if (ta && ta->is_stable()) {
1360 // Erase stability property for alias analysis.
1361 tj = ta = ta->cast_to_stable(false);
1362 }
1363 if( ta && is_known_inst ) {
1364 if ( offset != Type::OffsetBot &&
1365 offset > arrayOopDesc::length_offset_in_bytes() ) {
1366 offset = Type::OffsetBot; // Flatten constant access into array body only
1367 tj = ta = ta->
1368 remove_speculative()->
1369 cast_to_ptr_type(ptr)->
1370 with_offset(offset);
1371 }
1372 } else if (ta != nullptr) {
1373 // Common slices
1374 if (offset == arrayOopDesc::length_offset_in_bytes()) {
1375 return TypeAryPtr::RANGE;
1376 } else if (offset == oopDesc::klass_offset_in_bytes()) {
1377 return TypeInstPtr::KLASS;
1378 } else if (offset == oopDesc::mark_offset_in_bytes()) {
1379 return TypeInstPtr::MARK;
1380 }
1381
1382 // Remove size and stability
1383 const TypeAry* normalized_ary = TypeAry::make(ta->elem(), TypeInt::POS, false);
1384 // Remove ptr, const_oop, and offset
1385 if (ta->elem() == Type::BOTTOM) {
1386 // Bottom array (meet of int[] and byte[] for example), accesses to it will be done with
1387 // Unsafe. This should alias with all arrays. For now just leave it as it is (this is
1388 // incorrect, see JDK-8331133).
1389 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, false, Type::OffsetBot);
1390 } else if (ta->elem()->make_oopptr() != nullptr) {
1391 // Object arrays, all of them share the same slice
1392 const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, TypeInt::POS, false);
1393 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, tary, nullptr, false, Type::OffsetBot);
1394 } else {
1395 // Primitive arrays
1396 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, ta->exact_klass(), true, Type::OffsetBot);
1397 }
1398
1399 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1400 // cannot be distinguished by bytecode alone.
1401 if (ta->elem() == TypeInt::BOOL) {
1402 tj = ta = TypeAryPtr::BYTES;
1403 }
1404 }
1405
1406 // Oop pointers need some flattening
1407 const TypeInstPtr *to = tj->isa_instptr();
1408 if (to && to != TypeOopPtr::BOTTOM) {
1409 ciInstanceKlass* ik = to->instance_klass();
1410 if( ptr == TypePtr::Constant ) {
1411 if (ik != ciEnv::current()->Class_klass() ||
1412 offset < ik->layout_helper_size_in_bytes()) {
1413 // No constant oop pointers (such as Strings); they alias with
1414 // unknown strings.
1415 assert(!is_known_inst, "not scalarizable allocation");
1416 tj = to = to->
1417 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1418 remove_speculative()->
1419 cast_to_ptr_type(TypePtr::BotPTR)->
1420 cast_to_exactness(false);
1421 }
1422 } else if( is_known_inst ) {
1423 tj = to; // Keep NotNull and klass_is_exact for instance type
1424 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1425 // During the 2nd round of IterGVN, NotNull castings are removed.
1426 // Make sure the Bottom and NotNull variants alias the same.
1427 // Also, make sure exact and non-exact variants alias the same.
1428 tj = to = to->
1429 remove_speculative()->
1430 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1431 cast_to_ptr_type(TypePtr::BotPTR)->
1432 cast_to_exactness(false);
1433 }
1434 if (to->speculative() != nullptr) {
1435 tj = to = to->remove_speculative();
1436 }
1437 // Canonicalize the holder of this field
1438 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1439 // First handle header references such as a LoadKlassNode, even if the
1440 // object's klass is unloaded at compile time (4965979).
1441 if (!is_known_inst) { // Do it only for non-instance types
1442 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, offset);
1443 }
1444 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1445 // Static fields are in the space above the normal instance
1446 // fields in the java.lang.Class instance.
1447 if (ik != ciEnv::current()->Class_klass()) {
1448 to = nullptr;
1449 tj = TypeOopPtr::BOTTOM;
1450 offset = tj->offset();
1451 }
1452 } else {
1453 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1454 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1455 assert(tj->offset() == offset, "no change to offset expected");
1456 bool xk = to->klass_is_exact();
1457 int instance_id = to->instance_id();
1458
1459 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1460 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1461 // its interfaces are included.
1462 if (xk && ik->equals(canonical_holder)) {
1463 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id), "exact type should be canonical type");
1464 } else {
1465 assert(xk || !is_known_inst, "Known instance should be exact type");
1466 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id);
1467 }
1468 }
1469 }
1470
1471 // Klass pointers to object array klasses need some flattening
1472 const TypeKlassPtr *tk = tj->isa_klassptr();
1473 if( tk ) {
1474 // If we are referencing a field within a Klass, we need
1475 // to assume the worst case of an Object. Both exact and
1476 // inexact types must flatten to the same alias class so
1477 // use NotNull as the PTR.
1478 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1479 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1480 env()->Object_klass(),
1481 offset);
1482 }
1483
1484 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1485 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1486 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1487 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), offset);
1488 } else {
1489 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, offset);
1490 }
1491 }
1492
1493 // Check for precise loads from the primary supertype array and force them
1494 // to the supertype cache alias index. Check for generic array loads from
1495 // the primary supertype array and also force them to the supertype cache
1496 // alias index. Since the same load can reach both, we need to merge
1497 // these 2 disparate memories into the same alias class. Since the
1498 // primary supertype array is read-only, there's no chance of confusion
1499 // where we bypass an array load and an array store.
1500 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1501 if (offset == Type::OffsetBot ||
1502 (offset >= primary_supers_offset &&
1503 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1504 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1505 offset = in_bytes(Klass::secondary_super_cache_offset());
1506 tj = tk = tk->with_offset(offset);
1507 }
1508 }
1509
1510 // Flatten all Raw pointers together.
1511 if (tj->base() == Type::RawPtr)
1512 tj = TypeRawPtr::BOTTOM;
1602 intptr_t key = (intptr_t) adr_type;
1603 key ^= key >> logAliasCacheSize;
1604 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1605 }
1606
1607
1608 //-----------------------------grow_alias_types--------------------------------
1609 void Compile::grow_alias_types() {
1610 const int old_ats = _max_alias_types; // how many before?
1611 const int new_ats = old_ats; // how many more?
1612 const int grow_ats = old_ats+new_ats; // how many now?
1613 _max_alias_types = grow_ats;
1614 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1615 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1616 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1617 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1618 }
1619
1620
1621 //--------------------------------find_alias_type------------------------------
1622 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1623 if (!do_aliasing()) {
1624 return alias_type(AliasIdxBot);
1625 }
1626
1627 AliasCacheEntry* ace = probe_alias_cache(adr_type);
1628 if (ace->_adr_type == adr_type) {
1629 return alias_type(ace->_index);
1630 }
1631
1632 // Handle special cases.
1633 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1634 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1635
1636 // Do it the slow way.
1637 const TypePtr* flat = flatten_alias_type(adr_type);
1638
1639 #ifdef ASSERT
1640 {
1641 ResourceMark rm;
1642 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1643 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1644 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1645 Type::str(adr_type));
1646 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1647 const TypeOopPtr* foop = flat->is_oopptr();
1648 // Scalarizable allocations have exact klass always.
1649 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1659 if (alias_type(i)->adr_type() == flat) {
1660 idx = i;
1661 break;
1662 }
1663 }
1664
1665 if (idx == AliasIdxTop) {
1666 if (no_create) return nullptr;
1667 // Grow the array if necessary.
1668 if (_num_alias_types == _max_alias_types) grow_alias_types();
1669 // Add a new alias type.
1670 idx = _num_alias_types++;
1671 _alias_types[idx]->Init(idx, flat);
1672 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1673 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1674 if (flat->isa_instptr()) {
1675 if (flat->offset() == java_lang_Class::klass_offset()
1676 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1677 alias_type(idx)->set_rewritable(false);
1678 }
1679 if (flat->isa_aryptr()) {
1680 #ifdef ASSERT
1681 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1682 // (T_BYTE has the weakest alignment and size restrictions...)
1683 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1684 #endif
1685 if (flat->offset() == TypePtr::OffsetBot) {
1686 alias_type(idx)->set_element(flat->is_aryptr()->elem());
1687 }
1688 }
1689 if (flat->isa_klassptr()) {
1690 if (UseCompactObjectHeaders) {
1691 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1692 alias_type(idx)->set_rewritable(false);
1693 }
1694 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1695 alias_type(idx)->set_rewritable(false);
1696 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1697 alias_type(idx)->set_rewritable(false);
1698 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1699 alias_type(idx)->set_rewritable(false);
1700 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1701 alias_type(idx)->set_rewritable(false);
1702 }
1703
1704 if (flat->isa_instklassptr()) {
1705 if (flat->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
1706 alias_type(idx)->set_rewritable(false);
1707 }
1708 }
1709 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1710 // but the base pointer type is not distinctive enough to identify
1711 // references into JavaThread.)
1712
1713 // Check for final fields.
1714 const TypeInstPtr* tinst = flat->isa_instptr();
1715 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1716 ciField* field;
1717 if (tinst->const_oop() != nullptr &&
1718 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1719 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1720 // static field
1721 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1722 field = k->get_field_by_offset(tinst->offset(), true);
1723 } else {
1724 ciInstanceKlass *k = tinst->instance_klass();
1725 field = k->get_field_by_offset(tinst->offset(), false);
1726 }
1727 assert(field == nullptr ||
1728 original_field == nullptr ||
1729 (field->holder() == original_field->holder() &&
1730 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1731 field->is_static() == original_field->is_static()), "wrong field?");
1732 // Set field() and is_rewritable() attributes.
1733 if (field != nullptr) alias_type(idx)->set_field(field);
1734 }
1735 }
1736
1737 // Fill the cache for next time.
1738 ace->_adr_type = adr_type;
1739 ace->_index = idx;
1740 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1741
1742 // Might as well try to fill the cache for the flattened version, too.
1743 AliasCacheEntry* face = probe_alias_cache(flat);
1744 if (face->_adr_type == nullptr) {
1745 face->_adr_type = flat;
1746 face->_index = idx;
1747 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1748 }
1749
1750 return alias_type(idx);
1751 }
1752
1753
1754 Compile::AliasType* Compile::alias_type(ciField* field) {
1755 const TypeOopPtr* t;
1756 if (field->is_static())
1757 t = TypeInstPtr::make(field->holder()->java_mirror());
1758 else
1759 t = TypeOopPtr::make_from_klass_raw(field->holder());
1760 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1761 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1762 return atp;
1763 }
1764
1765
1766 //------------------------------have_alias_type--------------------------------
1767 bool Compile::have_alias_type(const TypePtr* adr_type) {
1849 assert(!C->major_progress(), "not cleared");
1850
1851 if (_for_post_loop_igvn.length() > 0) {
1852 while (_for_post_loop_igvn.length() > 0) {
1853 Node* n = _for_post_loop_igvn.pop();
1854 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1855 igvn._worklist.push(n);
1856 }
1857 igvn.optimize();
1858 if (failing()) return;
1859 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1860 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1861
1862 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1863 if (C->major_progress()) {
1864 C->clear_major_progress(); // ensure that major progress is now clear
1865 }
1866 }
1867 }
1868
1869 void Compile::record_for_merge_stores_igvn(Node* n) {
1870 if (!n->for_merge_stores_igvn()) {
1871 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
1872 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1873 _for_merge_stores_igvn.append(n);
1874 }
1875 }
1876
1877 void Compile::remove_from_merge_stores_igvn(Node* n) {
1878 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1879 _for_merge_stores_igvn.remove(n);
1880 }
1881
1882 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
1883 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
1884 // the stores, and we merge the wrong sequence of stores.
1885 // Example:
1886 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
1887 // Apply MergeStores:
1888 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
1967 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
1968 Bytecodes::Code c = iter.cur_bc();
1969 Node* lhs = nullptr;
1970 Node* rhs = nullptr;
1971 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
1972 lhs = unc->peek_operand(0);
1973 rhs = unc->peek_operand(1);
1974 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
1975 lhs = unc->peek_operand(0);
1976 }
1977
1978 ResourceMark rm;
1979 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
1980 assert(live_locals.is_valid(), "broken liveness info");
1981 int len = (int)live_locals.size();
1982
1983 for (int i = 0; i < len; i++) {
1984 Node* local = unc->local(jvms, i);
1985 // kill local using the liveness of next_bci.
1986 // give up when the local looks like an operand to secure reexecution.
1987 if (!live_locals.at(i) && !local->is_top() && local != lhs && local!= rhs) {
1988 uint idx = jvms->locoff() + i;
1989 #ifdef ASSERT
1990 if (PrintOpto && Verbose) {
1991 tty->print("[unstable_if] kill local#%d: ", idx);
1992 local->dump();
1993 tty->cr();
1994 }
1995 #endif
1996 igvn.replace_input_of(unc, idx, top());
1997 modified = true;
1998 }
1999 }
2000 }
2001
2002 // keep the mondified trap for late query
2003 if (modified) {
2004 trap->set_modified();
2005 } else {
2006 _unstable_if_traps.delete_at(i);
2007 }
2008 }
2009 igvn.optimize();
2010 }
2011
2012 // StringOpts and late inlining of string methods
2013 void Compile::inline_string_calls(bool parse_time) {
2014 {
2015 // remove useless nodes to make the usage analysis simpler
2016 ResourceMark rm;
2017 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2018 }
2019
2020 {
2021 ResourceMark rm;
2022 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 if (StressIncrementalInlining) {
2238 shuffle_late_inlines();
2239 }
2240
2241 while (_late_inlines.length() > 0) {
2242 igvn_worklist()->ensure_empty(); // should be done with igvn
2243
2244 while (inline_incrementally_one()) {
2245 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2246 }
2247 if (failing()) return;
2248
2249 inline_incrementally_cleanup(igvn);
2250 }
2251 }
2252
2253 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2254 if (_loop_opts_cnt > 0) {
2255 while (major_progress() && (_loop_opts_cnt > 0)) {
2256 TracePhase tp(_t_idealLoop);
2257 PhaseIdealLoop::optimize(igvn, mode);
2258 _loop_opts_cnt--;
2259 if (failing()) return false;
2260 if (major_progress()) {
2261 print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2262 }
2263 }
2264 }
2265 return true;
2266 }
2267
2268 // Remove edges from "root" to each SafePoint at a backward branch.
2269 // They were inserted during parsing (see add_safepoint()) to make
2270 // infinite loops without calls or exceptions visible to root, i.e.,
2376 print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
2377 }
2378 assert(!has_vbox_nodes(), "sanity");
2379
2380 if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2381 Compile::TracePhase tp(_t_renumberLive);
2382 igvn_worklist()->ensure_empty(); // should be done with igvn
2383 {
2384 ResourceMark rm;
2385 PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
2386 }
2387 igvn.reset();
2388 igvn.optimize();
2389 if (failing()) return;
2390 }
2391
2392 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2393 // safepoints
2394 remove_root_to_sfpts_edges(igvn);
2395
2396 if (failing()) return;
2397
2398 _print_phase_loop_opts = has_loops();
2399 if (_print_phase_loop_opts) {
2400 print_method(PHASE_BEFORE_LOOP_OPTS, 2);
2401 }
2402
2403 // Perform escape analysis
2404 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2405 if (has_loops()) {
2406 // Cleanup graph (remove dead nodes).
2407 TracePhase tp(_t_idealLoop);
2408 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2409 if (failing()) return;
2410 }
2411 bool progress;
2412 print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2413 do {
2414 ConnectionGraph::do_analysis(this, &igvn);
2415
2416 if (failing()) return;
2417
2418 int mcount = macro_count(); // Record number of allocations and locks before IGVN
2419
2420 // Optimize out fields loads from scalar replaceable allocations.
2421 igvn.optimize();
2422 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2423
2424 if (failing()) return;
2425
2426 if (congraph() != nullptr && macro_count() > 0) {
2427 TracePhase tp(_t_macroEliminate);
2428 PhaseMacroExpand mexp(igvn);
2429 mexp.eliminate_macro_nodes();
2430 if (failing()) return;
2431 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2432
2433 igvn.set_delay_transform(false);
2434 igvn.optimize();
2435 if (failing()) return;
2436
2437 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2438 }
2439
2440 ConnectionGraph::verify_ram_nodes(this, root());
2441 if (failing()) return;
2442
2443 progress = do_iterative_escape_analysis() &&
2444 (macro_count() < mcount) &&
2445 ConnectionGraph::has_candidates(this);
2446 // Try again if candidates exist and made progress
2447 // by removing some allocations and/or locks.
2448 } while (progress);
2449 }
2450
2451 // Loop transforms on the ideal graph. Range Check Elimination,
2452 // peeling, unrolling, etc.
2453
2454 // Set loop opts counter
2455 if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2456 {
2457 TracePhase tp(_t_idealLoop);
2458 PhaseIdealLoop::optimize(igvn, LoopOptsDefault);
2459 _loop_opts_cnt--;
2460 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
2461 if (failing()) return;
2462 }
2463 // Loop opts pass if partial peeling occurred in previous pass
2464 if(PartialPeelLoop && major_progress() && (_loop_opts_cnt > 0)) {
2465 TracePhase tp(_t_idealLoop);
2466 PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);
2467 _loop_opts_cnt--;
2468 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
2469 if (failing()) return;
2470 }
2507 // Loop transforms on the ideal graph. Range Check Elimination,
2508 // peeling, unrolling, etc.
2509 if (!optimize_loops(igvn, LoopOptsDefault)) {
2510 return;
2511 }
2512
2513 if (failing()) return;
2514
2515 C->clear_major_progress(); // ensure that major progress is now clear
2516
2517 process_for_post_loop_opts_igvn(igvn);
2518
2519 process_for_merge_stores_igvn(igvn);
2520
2521 if (failing()) return;
2522
2523 #ifdef ASSERT
2524 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
2525 #endif
2526
2527 {
2528 TracePhase tp(_t_macroExpand);
2529 print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);
2530 PhaseMacroExpand mex(igvn);
2531 // Do not allow new macro nodes once we start to eliminate and expand
2532 C->reset_allow_macro_nodes();
2533 // Last attempt to eliminate macro nodes before expand
2534 mex.eliminate_macro_nodes();
2535 if (failing()) {
2536 return;
2537 }
2538 mex.eliminate_opaque_looplimit_macro_nodes();
2539 if (failing()) {
2540 return;
2541 }
2542 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2543 if (mex.expand_macro_nodes()) {
2544 assert(failing(), "must bail out w/ explicit message");
2545 return;
2546 }
2547 print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
2548 }
2549
2550 {
2551 TracePhase tp(_t_barrierExpand);
2552 if (bs->expand_barriers(this, igvn)) {
2553 assert(failing(), "must bail out w/ explicit message");
2554 return;
2555 }
2556 print_method(PHASE_BARRIER_EXPANSION, 2);
2557 }
2558
2559 if (C->max_vector_size() > 0) {
2560 C->optimize_logic_cones(igvn);
2561 igvn.optimize();
2562 if (failing()) return;
2563 }
2564
2565 DEBUG_ONLY( _modified_nodes = nullptr; )
2566
2567 assert(igvn._worklist.size() == 0, "not empty");
2568
2569 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
2570
2571 if (_late_inlines.length() > 0) {
2572 // More opportunities to optimize virtual and MH calls.
2573 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
2574 process_late_inline_calls_no_inline(igvn);
2575 if (failing()) return;
2576 }
2577 } // (End scope of igvn; run destructor if necessary for asserts.)
2578
2579 check_no_dead_use();
2580
2581 // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
2582 // to remove hashes to unlock nodes for modifications.
2583 C->node_hash()->clear();
2584
2585 // A method with only infinite loops has no edges entering loops from root
2586 {
2587 TracePhase tp(_t_graphReshaping);
2588 if (final_graph_reshaping()) {
2589 assert(failing(), "must bail out w/ explicit message");
2590 return;
2591 }
2592 }
2593
2594 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2595 DEBUG_ONLY(set_phase_optimize_finished();)
2596 }
3302 case Op_CmpD3:
3303 case Op_StoreD:
3304 case Op_LoadD:
3305 case Op_LoadD_unaligned:
3306 frc.inc_double_count();
3307 break;
3308 case Op_Opaque1: // Remove Opaque Nodes before matching
3309 n->subsume_by(n->in(1), this);
3310 break;
3311 case Op_CallLeafPure: {
3312 // If the pure call is not supported, then lower to a CallLeaf.
3313 if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
3314 CallNode* call = n->as_Call();
3315 CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
3316 call->_name, TypeRawPtr::BOTTOM);
3317 new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
3318 new_call->init_req(TypeFunc::I_O, C->top());
3319 new_call->init_req(TypeFunc::Memory, C->top());
3320 new_call->init_req(TypeFunc::ReturnAdr, C->top());
3321 new_call->init_req(TypeFunc::FramePtr, C->top());
3322 for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain()->cnt(); i++) {
3323 new_call->init_req(i, call->in(i));
3324 }
3325 n->subsume_by(new_call, this);
3326 }
3327 frc.inc_call_count();
3328 break;
3329 }
3330 case Op_CallStaticJava:
3331 case Op_CallJava:
3332 case Op_CallDynamicJava:
3333 frc.inc_java_call_count(); // Count java call site;
3334 case Op_CallRuntime:
3335 case Op_CallLeaf:
3336 case Op_CallLeafVector:
3337 case Op_CallLeafNoFP: {
3338 assert (n->is_Call(), "");
3339 CallNode *call = n->as_Call();
3340 // Count call sites where the FP mode bit would have to be flipped.
3341 // Do not count uncommon runtime calls:
3342 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
3348 int nop = n->Opcode();
3349 // Clone shared simple arguments to uncommon calls, item (1).
3350 if (n->outcnt() > 1 &&
3351 !n->is_Proj() &&
3352 nop != Op_CreateEx &&
3353 nop != Op_CheckCastPP &&
3354 nop != Op_DecodeN &&
3355 nop != Op_DecodeNKlass &&
3356 !n->is_Mem() &&
3357 !n->is_Phi()) {
3358 Node *x = n->clone();
3359 call->set_req(TypeFunc::Parms, x);
3360 }
3361 }
3362 break;
3363 }
3364 case Op_StoreB:
3365 case Op_StoreC:
3366 case Op_StoreI:
3367 case Op_StoreL:
3368 case Op_CompareAndSwapB:
3369 case Op_CompareAndSwapS:
3370 case Op_CompareAndSwapI:
3371 case Op_CompareAndSwapL:
3372 case Op_CompareAndSwapP:
3373 case Op_CompareAndSwapN:
3374 case Op_WeakCompareAndSwapB:
3375 case Op_WeakCompareAndSwapS:
3376 case Op_WeakCompareAndSwapI:
3377 case Op_WeakCompareAndSwapL:
3378 case Op_WeakCompareAndSwapP:
3379 case Op_WeakCompareAndSwapN:
3380 case Op_CompareAndExchangeB:
3381 case Op_CompareAndExchangeS:
3382 case Op_CompareAndExchangeI:
3383 case Op_CompareAndExchangeL:
3384 case Op_CompareAndExchangeP:
3385 case Op_CompareAndExchangeN:
3386 case Op_GetAndAddS:
3387 case Op_GetAndAddB:
3903 k->subsume_by(m, this);
3904 }
3905 }
3906 }
3907 break;
3908 }
3909 case Op_CmpUL: {
3910 if (!Matcher::has_match_rule(Op_CmpUL)) {
3911 // No support for unsigned long comparisons
3912 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3913 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3914 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3915 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3916 Node* andl = new AndLNode(orl, remove_sign_mask);
3917 Node* cmp = new CmpLNode(andl, n->in(2));
3918 n->subsume_by(cmp, this);
3919 }
3920 break;
3921 }
3922 #ifdef ASSERT
3923 case Op_ConNKlass: {
3924 const TypePtr* tp = n->as_Type()->type()->make_ptr();
3925 ciKlass* klass = tp->is_klassptr()->exact_klass();
3926 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
3927 break;
3928 }
3929 #endif
3930 default:
3931 assert(!n->is_Call(), "");
3932 assert(!n->is_Mem(), "");
3933 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3934 break;
3935 }
3936 }
3937
3938 //------------------------------final_graph_reshaping_walk---------------------
3939 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3940 // requires that the walk visits a node's inputs before visiting the node.
3941 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3942 Unique_Node_List sfpt;
4278 }
4279 }
4280
4281 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4282 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4283 }
4284
4285 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4286 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4287 }
4288
4289 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4290 if (holder->is_initialized()) {
4291 return false;
4292 }
4293 if (holder->is_being_initialized()) {
4294 if (accessing_method->holder() == holder) {
4295 // Access inside a class. The barrier can be elided when access happens in <clinit>,
4296 // <init>, or a static method. In all those cases, there was an initialization
4297 // barrier on the holder klass passed.
4298 if (accessing_method->is_static_initializer() ||
4299 accessing_method->is_object_initializer() ||
4300 accessing_method->is_static()) {
4301 return false;
4302 }
4303 } else if (accessing_method->holder()->is_subclass_of(holder)) {
4304 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4305 // In case of <init> or a static method, the barrier is on the subclass is not enough:
4306 // child class can become fully initialized while its parent class is still being initialized.
4307 if (accessing_method->is_static_initializer()) {
4308 return false;
4309 }
4310 }
4311 ciMethod* root = method(); // the root method of compilation
4312 if (root != accessing_method) {
4313 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4314 }
4315 }
4316 return true;
4317 }
4318
4319 #ifndef PRODUCT
4320 //------------------------------verify_bidirectional_edges---------------------
4321 // For each input edge to a node (ie - for each Use-Def edge), verify that
4322 // there is a corresponding Def-Use edge.
4323 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4324 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4325 uint stack_size = live_nodes() >> 4;
4326 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4327 if (root_and_safepoints != nullptr) {
4357 if (in != nullptr && !in->is_top()) {
4358 // Count instances of `next`
4359 int cnt = 0;
4360 for (uint idx = 0; idx < in->_outcnt; idx++) {
4361 if (in->_out[idx] == n) {
4362 cnt++;
4363 }
4364 }
4365 assert(cnt > 0, "Failed to find Def-Use edge.");
4366 // Check for duplicate edges
4367 // walk the input array downcounting the input edges to n
4368 for (uint j = 0; j < length; j++) {
4369 if (n->in(j) == in) {
4370 cnt--;
4371 }
4372 }
4373 assert(cnt == 0, "Mismatched edge count.");
4374 } else if (in == nullptr) {
4375 assert(i == 0 || i >= n->req() ||
4376 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
4377 (n->is_Unlock() && i == (n->req() - 1)) ||
4378 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4379 "only region, phi, arraycopy, unlock or membar nodes have null data edges");
4380 } else {
4381 assert(in->is_top(), "sanity");
4382 // Nothing to check.
4383 }
4384 }
4385 }
4386 }
4387
4388 //------------------------------verify_graph_edges---------------------------
4389 // Walk the Graph and verify that there is a one-to-one correspondence
4390 // between Use-Def edges and Def-Use edges in the graph.
4391 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4392 if (VerifyGraphEdges) {
4393 Unique_Node_List visited;
4394
4395 // Call graph walk to check edges
4396 verify_bidirectional_edges(visited, root_and_safepoints);
4397 if (no_dead_code) {
4398 // Now make sure that no visited node is used by an unvisited node.
4399 bool dead_nodes = false;
4510 // (1) subklass is already limited to a subtype of superklass => always ok
4511 // (2) subklass does not overlap with superklass => always fail
4512 // (3) superklass has NO subtypes and we can check with a simple compare.
4513 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4514 if (skip) {
4515 return SSC_full_test; // Let caller generate the general case.
4516 }
4517
4518 if (subk->is_java_subtype_of(superk)) {
4519 return SSC_always_true; // (0) and (1) this test cannot fail
4520 }
4521
4522 if (!subk->maybe_java_subtype_of(superk)) {
4523 return SSC_always_false; // (2) true path dead; no dynamic test needed
4524 }
4525
4526 const Type* superelem = superk;
4527 if (superk->isa_aryklassptr()) {
4528 int ignored;
4529 superelem = superk->is_aryklassptr()->base_element_type(ignored);
4530 }
4531
4532 if (superelem->isa_instklassptr()) {
4533 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4534 if (!ik->has_subklass()) {
4535 if (!ik->is_final()) {
4536 // Add a dependency if there is a chance of a later subclass.
4537 dependencies()->assert_leaf_type(ik);
4538 }
4539 if (!superk->maybe_java_subtype_of(subk)) {
4540 return SSC_always_false;
4541 }
4542 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4543 }
4544 } else {
4545 // A primitive array type has no subtypes.
4546 return SSC_easy_test; // (3) caller can do a simple ptr comparison
4547 }
4548
4549 return SSC_full_test;
5342 } else {
5343 _debug_network_printer->update_compiled_method(C->method());
5344 }
5345 tty->print_cr("Method printed over network stream to IGV");
5346 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5347 }
5348 #endif // !PRODUCT
5349
5350 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5351 if (type != nullptr && phase->type(value)->higher_equal(type)) {
5352 return value;
5353 }
5354 Node* result = nullptr;
5355 if (bt == T_BYTE) {
5356 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5357 result = new RShiftINode(result, phase->intcon(24));
5358 } else if (bt == T_BOOLEAN) {
5359 result = new AndINode(value, phase->intcon(0xFF));
5360 } else if (bt == T_CHAR) {
5361 result = new AndINode(value,phase->intcon(0xFFFF));
5362 } else {
5363 assert(bt == T_SHORT, "unexpected narrow type");
5364 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5365 result = new RShiftINode(result, phase->intcon(16));
5366 }
5367 if (transform_res) {
5368 result = phase->transform(result);
5369 }
5370 return result;
5371 }
5372
5373 void Compile::record_method_not_compilable_oom() {
5374 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5375 }
5376
5377 #ifndef PRODUCT
5378 // Collects all the control inputs from nodes on the worklist and from their data dependencies
5379 static void find_candidate_control_inputs(Unique_Node_List& worklist, Unique_Node_List& candidates) {
5380 // Follow non-control edges until we reach CFG nodes
5381 for (uint i = 0; i < worklist.size(); i++) {
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "asm/macroAssembler.hpp"
26 #include "asm/macroAssembler.inline.hpp"
27 #include "ci/ciFlatArray.hpp"
28 #include "ci/ciInlineKlass.hpp"
29 #include "ci/ciReplay.hpp"
30 #include "classfile/javaClasses.hpp"
31 #include "code/aotCodeCache.hpp"
32 #include "code/exceptionHandlerTable.hpp"
33 #include "code/nmethod.hpp"
34 #include "compiler/compilationFailureInfo.hpp"
35 #include "compiler/compilationMemoryStatistic.hpp"
36 #include "compiler/compileBroker.hpp"
37 #include "compiler/compileLog.hpp"
38 #include "compiler/compiler_globals.hpp"
39 #include "compiler/compilerDefinitions.hpp"
40 #include "compiler/compilerOracle.hpp"
41 #include "compiler/disassembler.hpp"
42 #include "compiler/oopMap.hpp"
43 #include "gc/shared/barrierSet.hpp"
44 #include "gc/shared/c2/barrierSetC2.hpp"
45 #include "jfr/jfrEvents.hpp"
46 #include "jvm_io.h"
47 #include "memory/allocation.hpp"
48 #include "memory/arena.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "opto/addnode.hpp"
51 #include "opto/block.hpp"
52 #include "opto/c2compiler.hpp"
53 #include "opto/callGenerator.hpp"
54 #include "opto/callnode.hpp"
55 #include "opto/castnode.hpp"
56 #include "opto/cfgnode.hpp"
57 #include "opto/chaitin.hpp"
58 #include "opto/compile.hpp"
59 #include "opto/connode.hpp"
60 #include "opto/convertnode.hpp"
61 #include "opto/divnode.hpp"
62 #include "opto/escape.hpp"
63 #include "opto/idealGraphPrinter.hpp"
64 #include "opto/inlinetypenode.hpp"
65 #include "opto/locknode.hpp"
66 #include "opto/loopnode.hpp"
67 #include "opto/machnode.hpp"
68 #include "opto/macro.hpp"
69 #include "opto/matcher.hpp"
70 #include "opto/mathexactnode.hpp"
71 #include "opto/memnode.hpp"
72 #include "opto/movenode.hpp"
73 #include "opto/mulnode.hpp"
74 #include "opto/multnode.hpp"
75 #include "opto/narrowptrnode.hpp"
76 #include "opto/node.hpp"
77 #include "opto/opaquenode.hpp"
78 #include "opto/opcodes.hpp"
79 #include "opto/output.hpp"
80 #include "opto/parse.hpp"
81 #include "opto/phaseX.hpp"
82 #include "opto/rootnode.hpp"
83 #include "opto/runtime.hpp"
84 #include "opto/stringopts.hpp"
85 #include "opto/type.hpp"
86 #include "opto/vector.hpp"
87 #include "opto/vectornode.hpp"
88 #include "runtime/arguments.hpp"
89 #include "runtime/globals_extension.hpp"
90 #include "runtime/sharedRuntime.hpp"
91 #include "runtime/signature.hpp"
92 #include "runtime/stubRoutines.hpp"
93 #include "runtime/timer.hpp"
94 #include "utilities/align.hpp"
95 #include "utilities/copy.hpp"
96 #include "utilities/hashTable.hpp"
97 #include "utilities/macros.hpp"
98
99 // -------------------- Compile::mach_constant_base_node -----------------------
100 // Constant table base node singleton.
101 MachConstantBaseNode* Compile::mach_constant_base_node() {
102 if (_mach_constant_base_node == nullptr) {
103 _mach_constant_base_node = new MachConstantBaseNode();
104 _mach_constant_base_node->add_req(C->root());
105 }
106 return _mach_constant_base_node;
107 }
108
394 // as dead to be conservative about the dead node count at any
395 // given time.
396 if (!dead->is_Con()) {
397 record_dead_node(dead->_idx);
398 }
399 if (dead->is_macro()) {
400 remove_macro_node(dead);
401 }
402 if (dead->is_expensive()) {
403 remove_expensive_node(dead);
404 }
405 if (dead->is_OpaqueTemplateAssertionPredicate()) {
406 remove_template_assertion_predicate_opaque(dead->as_OpaqueTemplateAssertionPredicate());
407 }
408 if (dead->is_ParsePredicate()) {
409 remove_parse_predicate(dead->as_ParsePredicate());
410 }
411 if (dead->for_post_loop_opts_igvn()) {
412 remove_from_post_loop_opts_igvn(dead);
413 }
414 if (dead->is_InlineType()) {
415 remove_inline_type(dead);
416 }
417 if (dead->is_LoadFlat() || dead->is_StoreFlat()) {
418 remove_flat_access(dead);
419 }
420 if (dead->for_merge_stores_igvn()) {
421 remove_from_merge_stores_igvn(dead);
422 }
423 if (dead->is_Call()) {
424 remove_useless_late_inlines( &_late_inlines, dead);
425 remove_useless_late_inlines( &_string_late_inlines, dead);
426 remove_useless_late_inlines( &_boxing_late_inlines, dead);
427 remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
428
429 if (dead->is_CallStaticJava()) {
430 remove_unstable_if_trap(dead->as_CallStaticJava(), false);
431 }
432 }
433 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
434 bs->unregister_potential_barrier_node(dead);
435 }
436
437 // Disconnect all useless nodes by disconnecting those at the boundary.
438 void Compile::disconnect_useless_nodes(Unique_Node_List& useful, Unique_Node_List& worklist, const Unique_Node_List* root_and_safepoints) {
439 uint next = 0;
447 // Use raw traversal of out edges since this code removes out edges
448 int max = n->outcnt();
449 for (int j = 0; j < max; ++j) {
450 Node* child = n->raw_out(j);
451 if (!useful.member(child)) {
452 assert(!child->is_top() || child != top(),
453 "If top is cached in Compile object it is in useful list");
454 // Only need to remove this out-edge to the useless node
455 n->raw_del_out(j);
456 --j;
457 --max;
458 if (child->is_data_proj_of_pure_function(n)) {
459 worklist.push(n);
460 }
461 }
462 }
463 if (n->outcnt() == 1 && n->has_special_unique_user()) {
464 assert(useful.member(n->unique_out()), "do not push a useless node");
465 worklist.push(n->unique_out());
466 }
467 if (n->outcnt() == 0) {
468 worklist.push(n);
469 }
470 }
471
472 remove_useless_nodes(_macro_nodes, useful); // remove useless macro nodes
473 remove_useless_nodes(_parse_predicates, useful); // remove useless Parse Predicate nodes
474 // Remove useless Template Assertion Predicate opaque nodes
475 remove_useless_nodes(_template_assertion_predicate_opaques, useful);
476 remove_useless_nodes(_expensive_nodes, useful); // remove useless expensive nodes
477 remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
478 remove_useless_nodes(_inline_type_nodes, useful); // remove useless inline type nodes
479 remove_useless_nodes(_flat_access_nodes, useful); // remove useless flat access nodes
480 #ifdef ASSERT
481 if (_modified_nodes != nullptr) {
482 _modified_nodes->remove_useless_nodes(useful.member_set());
483 }
484 #endif
485 remove_useless_nodes(_for_merge_stores_igvn, useful); // remove useless node recorded for merge stores IGVN pass
486 remove_useless_unstable_if_traps(useful); // remove useless unstable_if traps
487 remove_useless_coarsened_locks(useful); // remove useless coarsened locks nodes
488 #ifdef ASSERT
489 if (_modified_nodes != nullptr) {
490 _modified_nodes->remove_useless_nodes(useful.member_set());
491 }
492 #endif
493
494 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
495 bs->eliminate_useless_gc_barriers(useful, this);
496 // clean up the late inline lists
497 remove_useless_late_inlines( &_late_inlines, useful);
498 remove_useless_late_inlines( &_string_late_inlines, useful);
499 remove_useless_late_inlines( &_boxing_late_inlines, useful);
500 remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
501 DEBUG_ONLY(verify_graph_edges(true /*check for no_dead_code*/, root_and_safepoints);)
502 }
503
504 // ============================================================================
651
652
653 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
654 Options options, DirectiveSet* directive)
655 : Phase(Compiler),
656 _compile_id(ci_env->compile_id()),
657 _options(options),
658 _method(target),
659 _entry_bci(osr_bci),
660 _ilt(nullptr),
661 _stub_function(nullptr),
662 _stub_name(nullptr),
663 _stub_id(StubId::NO_STUBID),
664 _stub_entry_point(nullptr),
665 _max_node_limit(MaxNodeLimit),
666 _post_loop_opts_phase(false),
667 _merge_stores_phase(false),
668 _allow_macro_nodes(true),
669 _inlining_progress(false),
670 _inlining_incrementally(false),
671 _strength_reduction(false),
672 _do_cleanup(false),
673 _has_reserved_stack_access(target->has_reserved_stack_access()),
674 _has_circular_inline_type(false),
675 #ifndef PRODUCT
676 _igv_idx(0),
677 _trace_opto_output(directive->TraceOptoOutputOption),
678 #endif
679 _clinit_barrier_on_entry(false),
680 _stress_seed(0),
681 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
682 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
683 _env(ci_env),
684 _directive(directive),
685 _log(ci_env->log()),
686 _first_failure_details(nullptr),
687 _intrinsics(comp_arena(), 0, 0, nullptr),
688 _macro_nodes(comp_arena(), 8, 0, nullptr),
689 _parse_predicates(comp_arena(), 8, 0, nullptr),
690 _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
691 _expensive_nodes(comp_arena(), 8, 0, nullptr),
692 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
693 _inline_type_nodes (comp_arena(), 8, 0, nullptr),
694 _flat_access_nodes(comp_arena(), 8, 0, nullptr),
695 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
696 _unstable_if_traps(comp_arena(), 8, 0, nullptr),
697 _coarsened_locks(comp_arena(), 8, 0, nullptr),
698 _congraph(nullptr),
699 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
700 _unique(0),
701 _dead_node_count(0),
702 _dead_node_list(comp_arena()),
703 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
704 _node_arena_two(mtCompiler, Arena::Tag::tag_node),
705 _node_arena(&_node_arena_one),
706 _mach_constant_base_node(nullptr),
707 _Compile_types(mtCompiler, Arena::Tag::tag_type),
708 _initial_gvn(nullptr),
709 _igvn_worklist(nullptr),
710 _types(nullptr),
711 _node_hash(nullptr),
712 _late_inlines(comp_arena(), 2, 0, nullptr),
713 _string_late_inlines(comp_arena(), 2, 0, nullptr),
714 _boxing_late_inlines(comp_arena(), 2, 0, nullptr),
783 #define MINIMUM_NODE_HASH 1023
784
785 // GVN that will be run immediately on new nodes
786 uint estimated_size = method()->code_size()*4+64;
787 estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
788 _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
789 _types = new (comp_arena()) Type_Array(comp_arena());
790 _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
791 PhaseGVN gvn;
792 set_initial_gvn(&gvn);
793
794 { // Scope for timing the parser
795 TracePhase tp(_t_parser);
796
797 // Put top into the hash table ASAP.
798 initial_gvn()->transform(top());
799
800 // Set up tf(), start(), and find a CallGenerator.
801 CallGenerator* cg = nullptr;
802 if (is_osr_compilation()) {
803 init_tf(TypeFunc::make(method(), /* is_osr_compilation = */ true));
804 StartNode* s = new StartOSRNode(root(), tf()->domain_sig());
805 initial_gvn()->set_type_bottom(s);
806 verify_start(s);
807 cg = CallGenerator::for_osr(method(), entry_bci());
808 } else {
809 // Normal case.
810 init_tf(TypeFunc::make(method()));
811 StartNode* s = new StartNode(root(), tf()->domain_cc());
812 initial_gvn()->set_type_bottom(s);
813 verify_start(s);
814 float past_uses = method()->interpreter_invocation_count();
815 float expected_uses = past_uses;
816 cg = CallGenerator::for_inline(method(), expected_uses);
817 }
818 if (failing()) return;
819 if (cg == nullptr) {
820 const char* reason = InlineTree::check_can_parse(method());
821 assert(reason != nullptr, "expect reason for parse failure");
822 stringStream ss;
823 ss.print("cannot parse method: %s", reason);
824 record_method_not_compilable(ss.as_string());
825 return;
826 }
827
828 gvn.set_type(root(), root()->bottom_type());
829
830 JVMState* jvms = build_start_state(start(), tf());
831 if ((jvms = cg->generate(jvms)) == nullptr) {
892 print_ideal_ir("PrintIdeal");
893 }
894 #endif
895
896 #ifdef ASSERT
897 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
898 bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
899 #endif
900
901 // Dump compilation data to replay it.
902 if (directive->DumpReplayOption) {
903 env()->dump_replay_data(_compile_id);
904 }
905 if (directive->DumpInlineOption && (ilt() != nullptr)) {
906 env()->dump_inline_data(_compile_id);
907 }
908
909 // Now that we know the size of all the monitors we can add a fixed slot
910 // for the original deopt pc.
911 int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
912 if (needs_stack_repair()) {
913 // One extra slot for the special stack increment value
914 next_slot += 2;
915 }
916 // TODO 8284443 Only reserve extra slot if needed
917 if (InlineTypeReturnedAsFields) {
918 // One extra slot to hold the null marker for a nullable
919 // inline type return if we run out of registers.
920 next_slot += 2;
921 }
922 set_fixed_slots(next_slot);
923
924 // Compute when to use implicit null checks. Used by matching trap based
925 // nodes and NullCheck optimization.
926 set_allowed_deopt_reasons();
927
928 // Now generate code
929 Code_Gen();
930 }
931
932 //------------------------------Compile----------------------------------------
933 // Compile a runtime stub
934 Compile::Compile(ciEnv* ci_env,
935 TypeFunc_generator generator,
936 address stub_function,
937 const char* stub_name,
938 StubId stub_id,
939 int is_fancy_jump,
940 bool pass_tls,
941 bool return_pc,
942 DirectiveSet* directive)
943 : Phase(Compiler),
944 _compile_id(0),
945 _options(Options::for_runtime_stub()),
946 _method(nullptr),
947 _entry_bci(InvocationEntryBci),
948 _stub_function(stub_function),
949 _stub_name(stub_name),
950 _stub_id(stub_id),
951 _stub_entry_point(nullptr),
952 _max_node_limit(MaxNodeLimit),
953 _post_loop_opts_phase(false),
954 _merge_stores_phase(false),
955 _allow_macro_nodes(true),
956 _inlining_progress(false),
957 _inlining_incrementally(false),
958 _has_reserved_stack_access(false),
959 _has_circular_inline_type(false),
960 #ifndef PRODUCT
961 _igv_idx(0),
962 _trace_opto_output(directive->TraceOptoOutputOption),
963 #endif
964 _clinit_barrier_on_entry(false),
965 _stress_seed(0),
966 _comp_arena(mtCompiler, Arena::Tag::tag_comp),
967 _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
968 _env(ci_env),
969 _directive(directive),
970 _log(ci_env->log()),
971 _first_failure_details(nullptr),
972 _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
973 _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
974 _congraph(nullptr),
975 NOT_PRODUCT(_igv_printer(nullptr) COMMA)
976 _unique(0),
977 _dead_node_count(0),
978 _dead_node_list(comp_arena()),
979 _node_arena_one(mtCompiler, Arena::Tag::tag_node),
1094 _fixed_slots = 0;
1095 set_has_split_ifs(false);
1096 set_has_loops(false); // first approximation
1097 set_has_stringbuilder(false);
1098 set_has_boxed_value(false);
1099 _trap_can_recompile = false; // no traps emitted yet
1100 _major_progress = true; // start out assuming good things will happen
1101 set_has_unsafe_access(false);
1102 set_max_vector_size(0);
1103 set_clear_upper_avx(false); //false as default for clear upper bits of ymm registers
1104 Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1105 set_decompile_count(0);
1106
1107 #ifndef PRODUCT
1108 _phase_counter = 0;
1109 Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1110 #endif
1111
1112 set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1113 _loop_opts_cnt = LoopOptsCount;
1114 _has_flat_accesses = false;
1115 _flat_accesses_share_alias = true;
1116 _scalarize_in_safepoints = false;
1117
1118 set_do_inlining(Inline);
1119 set_max_inline_size(MaxInlineSize);
1120 set_freq_inline_size(FreqInlineSize);
1121 set_do_scheduling(OptoScheduling);
1122
1123 set_do_vector_loop(false);
1124 set_has_monitors(false);
1125 set_has_scoped_access(false);
1126
1127 if (AllowVectorizeOnDemand) {
1128 if (has_method() && _directive->VectorizeOption) {
1129 set_do_vector_loop(true);
1130 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());})
1131 } else if (has_method() && method()->name() != nullptr &&
1132 method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1133 set_do_vector_loop(true);
1134 }
1135 }
1136 set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1137 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());})
1378 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1379 assert(do_aliasing(), "Aliasing should be enabled");
1380 int offset = tj->offset();
1381 TypePtr::PTR ptr = tj->ptr();
1382
1383 // Known instance (scalarizable allocation) alias only with itself.
1384 bool is_known_inst = tj->isa_oopptr() != nullptr &&
1385 tj->is_oopptr()->is_known_instance();
1386
1387 // Process weird unsafe references.
1388 if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1389 assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1390 assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1391 tj = TypeOopPtr::BOTTOM;
1392 ptr = tj->ptr();
1393 offset = tj->offset();
1394 }
1395
1396 // Array pointers need some flattening
1397 const TypeAryPtr* ta = tj->isa_aryptr();
1398 if( ta && is_known_inst ) {
1399 if ( offset != Type::OffsetBot &&
1400 offset > arrayOopDesc::length_offset_in_bytes() ) {
1401 offset = Type::OffsetBot; // Flatten constant access into array body only
1402 tj = ta = ta->
1403 remove_speculative()->
1404 cast_to_ptr_type(ptr)->
1405 with_offset(offset);
1406 }
1407 } else if (ta != nullptr) {
1408 // Common slices
1409 if (offset == arrayOopDesc::length_offset_in_bytes()) {
1410 return TypeAryPtr::RANGE;
1411 } else if (offset == oopDesc::klass_offset_in_bytes()) {
1412 return TypeInstPtr::KLASS;
1413 } else if (offset == oopDesc::mark_offset_in_bytes()) {
1414 return TypeInstPtr::MARK;
1415 }
1416
1417 // Remove size and stability
1418 const TypeAry* normalized_ary = TypeAry::make(ta->elem(), TypeInt::POS, false, ta->is_flat(), ta->is_not_flat(), ta->is_not_null_free(), ta->is_atomic());
1419 // Remove ptr, const_oop, and offset
1420 if (ta->elem() == Type::BOTTOM) {
1421 // Bottom array (meet of int[] and byte[] for example), accesses to it will be done with
1422 // Unsafe. This should alias with all arrays. For now just leave it as it is (this is
1423 // incorrect, see JDK-8331133).
1424 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, false, Type::Offset::bottom);
1425 } else if (ta->elem()->make_oopptr() != nullptr) {
1426 // Object arrays, keep field_offset
1427 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, ta->klass_is_exact(), Type::Offset::bottom, Type::Offset(ta->field_offset()));
1428 } else {
1429 // Primitive arrays
1430 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, ta->exact_klass(), true, Type::Offset::bottom);
1431 }
1432
1433 // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1434 // cannot be distinguished by bytecode alone.
1435 if (ta->elem() == TypeInt::BOOL) {
1436 tj = ta = TypeAryPtr::BYTES;
1437 }
1438
1439 // All arrays of references share the same slice
1440 if (!ta->is_flat() && ta->elem()->make_oopptr() != nullptr) {
1441 const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, TypeInt::POS, false, false, true, true, true);
1442 tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, tary, nullptr, false, Type::Offset::bottom);
1443 }
1444
1445 if (ta->is_flat()) {
1446 if (_flat_accesses_share_alias) {
1447 // Initially all flattened array accesses share a single slice
1448 tj = ta = TypeAryPtr::INLINES;
1449 } else {
1450 // Flat accesses are always exact
1451 tj = ta = ta->cast_to_exactness(true);
1452 }
1453 }
1454 }
1455
1456 // Oop pointers need some flattening
1457 const TypeInstPtr *to = tj->isa_instptr();
1458 if (to && to != TypeOopPtr::BOTTOM) {
1459 ciInstanceKlass* ik = to->instance_klass();
1460 tj = to = to->cast_to_maybe_flat_in_array(); // flatten to maybe flat in array
1461 if( ptr == TypePtr::Constant ) {
1462 if (ik != ciEnv::current()->Class_klass() ||
1463 offset < ik->layout_helper_size_in_bytes()) {
1464 // No constant oop pointers (such as Strings); they alias with
1465 // unknown strings.
1466 assert(!is_known_inst, "not scalarizable allocation");
1467 tj = to = to->
1468 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1469 remove_speculative()->
1470 cast_to_ptr_type(TypePtr::BotPTR)->
1471 cast_to_exactness(false);
1472 }
1473 } else if( is_known_inst ) {
1474 tj = to; // Keep NotNull and klass_is_exact for instance type
1475 } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1476 // During the 2nd round of IterGVN, NotNull castings are removed.
1477 // Make sure the Bottom and NotNull variants alias the same.
1478 // Also, make sure exact and non-exact variants alias the same.
1479 tj = to = to->
1480 remove_speculative()->
1481 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1482 cast_to_ptr_type(TypePtr::BotPTR)->
1483 cast_to_exactness(false);
1484 }
1485 if (to->speculative() != nullptr) {
1486 tj = to = to->remove_speculative();
1487 }
1488 // Canonicalize the holder of this field
1489 if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1490 // First handle header references such as a LoadKlassNode, even if the
1491 // object's klass is unloaded at compile time (4965979).
1492 if (!is_known_inst) { // Do it only for non-instance types
1493 tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, Type::Offset(offset));
1494 }
1495 } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1496 // Static fields are in the space above the normal instance
1497 // fields in the java.lang.Class instance.
1498 if (ik != ciEnv::current()->Class_klass()) {
1499 to = nullptr;
1500 tj = TypeOopPtr::BOTTOM;
1501 offset = tj->offset();
1502 }
1503 } else {
1504 ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1505 assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1506 assert(tj->offset() == offset, "no change to offset expected");
1507 bool xk = to->klass_is_exact();
1508 int instance_id = to->instance_id();
1509
1510 // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1511 // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1512 // its interfaces are included.
1513 if (xk && ik->equals(canonical_holder)) {
1514 assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id,
1515 TypePtr::MaybeFlat), "exact type should be canonical type");
1516 } else {
1517 assert(xk || !is_known_inst, "Known instance should be exact type");
1518 tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id,
1519 TypePtr::MaybeFlat);
1520 }
1521 }
1522 }
1523
1524 // Klass pointers to object array klasses need some flattening
1525 const TypeKlassPtr *tk = tj->isa_klassptr();
1526 if( tk ) {
1527 // If we are referencing a field within a Klass, we need
1528 // to assume the worst case of an Object. Both exact and
1529 // inexact types must flatten to the same alias class so
1530 // use NotNull as the PTR.
1531 if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1532 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1533 env()->Object_klass(),
1534 Type::Offset(offset),
1535 TypePtr::MaybeFlat);
1536 }
1537
1538 if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1539 ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1540 if (!k || !k->is_loaded()) { // Only fails for some -Xcomp runs
1541 tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), Type::Offset(offset), TypePtr::MaybeFlat);
1542 } else {
1543 tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, Type::Offset(offset), tk->is_not_flat(), tk->is_not_null_free(), tk->is_flat(), tk->is_null_free(), tk->is_atomic(), tk->is_aryklassptr()->is_refined_type());
1544 }
1545 }
1546 // Check for precise loads from the primary supertype array and force them
1547 // to the supertype cache alias index. Check for generic array loads from
1548 // the primary supertype array and also force them to the supertype cache
1549 // alias index. Since the same load can reach both, we need to merge
1550 // these 2 disparate memories into the same alias class. Since the
1551 // primary supertype array is read-only, there's no chance of confusion
1552 // where we bypass an array load and an array store.
1553 int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1554 if (offset == Type::OffsetBot ||
1555 (offset >= primary_supers_offset &&
1556 offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1557 offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1558 offset = in_bytes(Klass::secondary_super_cache_offset());
1559 tj = tk = tk->with_offset(offset);
1560 }
1561 }
1562
1563 // Flatten all Raw pointers together.
1564 if (tj->base() == Type::RawPtr)
1565 tj = TypeRawPtr::BOTTOM;
1655 intptr_t key = (intptr_t) adr_type;
1656 key ^= key >> logAliasCacheSize;
1657 return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1658 }
1659
1660
1661 //-----------------------------grow_alias_types--------------------------------
1662 void Compile::grow_alias_types() {
1663 const int old_ats = _max_alias_types; // how many before?
1664 const int new_ats = old_ats; // how many more?
1665 const int grow_ats = old_ats+new_ats; // how many now?
1666 _max_alias_types = grow_ats;
1667 _alias_types = REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1668 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1669 Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1670 for (int i = 0; i < new_ats; i++) _alias_types[old_ats+i] = &ats[i];
1671 }
1672
1673
1674 //--------------------------------find_alias_type------------------------------
1675 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1676 if (!do_aliasing()) {
1677 return alias_type(AliasIdxBot);
1678 }
1679
1680 AliasCacheEntry* ace = nullptr;
1681 if (!uncached) {
1682 ace = probe_alias_cache(adr_type);
1683 if (ace->_adr_type == adr_type) {
1684 return alias_type(ace->_index);
1685 }
1686 }
1687
1688 // Handle special cases.
1689 if (adr_type == nullptr) return alias_type(AliasIdxTop);
1690 if (adr_type == TypePtr::BOTTOM) return alias_type(AliasIdxBot);
1691
1692 // Do it the slow way.
1693 const TypePtr* flat = flatten_alias_type(adr_type);
1694
1695 #ifdef ASSERT
1696 {
1697 ResourceMark rm;
1698 assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1699 Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1700 assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1701 Type::str(adr_type));
1702 if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1703 const TypeOopPtr* foop = flat->is_oopptr();
1704 // Scalarizable allocations have exact klass always.
1705 bool exact = !foop->klass_is_exact() || foop->is_known_instance();
1715 if (alias_type(i)->adr_type() == flat) {
1716 idx = i;
1717 break;
1718 }
1719 }
1720
1721 if (idx == AliasIdxTop) {
1722 if (no_create) return nullptr;
1723 // Grow the array if necessary.
1724 if (_num_alias_types == _max_alias_types) grow_alias_types();
1725 // Add a new alias type.
1726 idx = _num_alias_types++;
1727 _alias_types[idx]->Init(idx, flat);
1728 if (flat == TypeInstPtr::KLASS) alias_type(idx)->set_rewritable(false);
1729 if (flat == TypeAryPtr::RANGE) alias_type(idx)->set_rewritable(false);
1730 if (flat->isa_instptr()) {
1731 if (flat->offset() == java_lang_Class::klass_offset()
1732 && flat->is_instptr()->instance_klass() == env()->Class_klass())
1733 alias_type(idx)->set_rewritable(false);
1734 }
1735 ciField* field = nullptr;
1736 if (flat->isa_aryptr()) {
1737 #ifdef ASSERT
1738 const int header_size_min = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1739 // (T_BYTE has the weakest alignment and size restrictions...)
1740 assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1741 #endif
1742 const Type* elemtype = flat->is_aryptr()->elem();
1743 if (flat->offset() == TypePtr::OffsetBot) {
1744 alias_type(idx)->set_element(elemtype);
1745 }
1746 int field_offset = flat->is_aryptr()->field_offset().get();
1747 if (flat->is_flat() &&
1748 field_offset != Type::OffsetBot) {
1749 ciInlineKlass* vk = elemtype->inline_klass();
1750 field_offset += vk->payload_offset();
1751 field = vk->get_field_by_offset(field_offset, false);
1752 }
1753 }
1754 if (flat->isa_klassptr()) {
1755 if (UseCompactObjectHeaders) {
1756 if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1757 alias_type(idx)->set_rewritable(false);
1758 }
1759 if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1760 alias_type(idx)->set_rewritable(false);
1761 if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1762 alias_type(idx)->set_rewritable(false);
1763 if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1764 alias_type(idx)->set_rewritable(false);
1765 if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1766 alias_type(idx)->set_rewritable(false);
1767 if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1768 alias_type(idx)->set_rewritable(false);
1769 }
1770
1771 if (flat->isa_instklassptr()) {
1772 if (flat->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
1773 alias_type(idx)->set_rewritable(false);
1774 }
1775 }
1776 // %%% (We would like to finalize JavaThread::threadObj_offset(),
1777 // but the base pointer type is not distinctive enough to identify
1778 // references into JavaThread.)
1779
1780 // Check for final fields.
1781 const TypeInstPtr* tinst = flat->isa_instptr();
1782 if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1783 if (tinst->const_oop() != nullptr &&
1784 tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1785 tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1786 // static field
1787 ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1788 field = k->get_field_by_offset(tinst->offset(), true);
1789 } else if (tinst->is_inlinetypeptr()) {
1790 // Inline type field
1791 ciInlineKlass* vk = tinst->inline_klass();
1792 field = vk->get_field_by_offset(tinst->offset(), false);
1793 } else {
1794 ciInstanceKlass *k = tinst->instance_klass();
1795 field = k->get_field_by_offset(tinst->offset(), false);
1796 }
1797 }
1798 assert(field == nullptr ||
1799 original_field == nullptr ||
1800 (field->holder() == original_field->holder() &&
1801 field->offset_in_bytes() == original_field->offset_in_bytes() &&
1802 field->is_static() == original_field->is_static()), "wrong field?");
1803 // Set field() and is_rewritable() attributes.
1804 if (field != nullptr) {
1805 alias_type(idx)->set_field(field);
1806 if (flat->isa_aryptr()) {
1807 // Fields of flat arrays are rewritable although they are declared final
1808 assert(flat->is_flat(), "must be a flat array");
1809 alias_type(idx)->set_rewritable(true);
1810 }
1811 }
1812 }
1813
1814 // Fill the cache for next time.
1815 if (!uncached) {
1816 ace->_adr_type = adr_type;
1817 ace->_index = idx;
1818 assert(alias_type(adr_type) == alias_type(idx), "type must be installed");
1819
1820 // Might as well try to fill the cache for the flattened version, too.
1821 AliasCacheEntry* face = probe_alias_cache(flat);
1822 if (face->_adr_type == nullptr) {
1823 face->_adr_type = flat;
1824 face->_index = idx;
1825 assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1826 }
1827 }
1828
1829 return alias_type(idx);
1830 }
1831
1832
1833 Compile::AliasType* Compile::alias_type(ciField* field) {
1834 const TypeOopPtr* t;
1835 if (field->is_static())
1836 t = TypeInstPtr::make(field->holder()->java_mirror());
1837 else
1838 t = TypeOopPtr::make_from_klass_raw(field->holder());
1839 AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1840 assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1841 return atp;
1842 }
1843
1844
1845 //------------------------------have_alias_type--------------------------------
1846 bool Compile::have_alias_type(const TypePtr* adr_type) {
1928 assert(!C->major_progress(), "not cleared");
1929
1930 if (_for_post_loop_igvn.length() > 0) {
1931 while (_for_post_loop_igvn.length() > 0) {
1932 Node* n = _for_post_loop_igvn.pop();
1933 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1934 igvn._worklist.push(n);
1935 }
1936 igvn.optimize();
1937 if (failing()) return;
1938 assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1939 assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1940
1941 // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1942 if (C->major_progress()) {
1943 C->clear_major_progress(); // ensure that major progress is now clear
1944 }
1945 }
1946 }
1947
1948 void Compile::add_inline_type(Node* n) {
1949 assert(n->is_InlineType(), "unexpected node");
1950 _inline_type_nodes.push(n);
1951 }
1952
1953 void Compile::remove_inline_type(Node* n) {
1954 assert(n->is_InlineType(), "unexpected node");
1955 if (_inline_type_nodes.contains(n)) {
1956 _inline_type_nodes.remove(n);
1957 }
1958 }
1959
1960 // Does the return value keep otherwise useless inline type allocations alive?
1961 static bool return_val_keeps_allocations_alive(Node* ret_val) {
1962 ResourceMark rm;
1963 Unique_Node_List wq;
1964 wq.push(ret_val);
1965 bool some_allocations = false;
1966 for (uint i = 0; i < wq.size(); i++) {
1967 Node* n = wq.at(i);
1968 if (n->outcnt() > 1) {
1969 // Some other use for the allocation
1970 return false;
1971 } else if (n->is_InlineType()) {
1972 wq.push(n->in(1));
1973 } else if (n->is_Phi()) {
1974 for (uint j = 1; j < n->req(); j++) {
1975 wq.push(n->in(j));
1976 }
1977 } else if (n->is_CheckCastPP() &&
1978 n->in(1)->is_Proj() &&
1979 n->in(1)->in(0)->is_Allocate()) {
1980 some_allocations = true;
1981 } else if (n->is_CheckCastPP()) {
1982 wq.push(n->in(1));
1983 }
1984 }
1985 return some_allocations;
1986 }
1987
1988 bool Compile::clear_argument_if_only_used_as_buffer_at_calls(Node* result_cast, PhaseIterGVN& igvn) {
1989 ResourceMark rm;
1990 Unique_Node_List wq;
1991 wq.push(result_cast);
1992 Node_List calls;
1993 for (uint i = 0; i < wq.size(); ++i) {
1994 Node* n = wq.at(i);
1995 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
1996 Node* u = n->fast_out(j);
1997 if (u->is_Phi()) {
1998 wq.push(u);
1999 } else if (u->is_InlineType() && u->as_InlineType()->get_oop() == n) {
2000 wq.push(u);
2001 } else if (u->is_CallJava()) {
2002 CallJavaNode* call = u->as_CallJava();
2003 if (call->method() != nullptr && call->method()->get_Method()->mismatch()) {
2004 return false;
2005 }
2006 uint nargs = call->tf()->domain_cc()->cnt();
2007 for (uint k = TypeFunc::Parms; k < nargs; k++) {
2008 Node* in = call->in(k);
2009 if (in == n && (call->method() == nullptr || !call->method()->is_scalarized_buffer_arg(k - TypeFunc::Parms))) {
2010 return false;
2011 }
2012 }
2013 calls.push(call);
2014 } else if (u->Opcode() == Op_EncodeP) {
2015 wq.push(u);
2016 } else if (u->is_AddP()) {
2017 wq.push(u);
2018 } else if (u->is_Store() && u->in(MemNode::Address) == n) {
2019 // storing to the buffer is fine
2020 } else if (u->is_SafePoint()) {
2021 SafePointNode* sfpt = u->as_SafePoint();
2022 int input = u->find_edge(n);
2023 JVMState* jvms = sfpt->jvms();
2024 if (jvms != nullptr) {
2025 if (input < (int)jvms->debug_start()) {
2026 return false;
2027 }
2028 }
2029 } else {
2030 return false;
2031 }
2032 }
2033 }
2034 for (uint i = 0; i < calls.size(); ++i) {
2035 CallJavaNode* call = calls.at(i)->as_CallJava();
2036 uint nargs = call->tf()->domain_cc()->cnt();
2037 for (uint k = TypeFunc::Parms; k < nargs; k++) {
2038 Node* in = call->in(k);
2039 if (wq.member(in)) {
2040 assert(call->method()->is_scalarized_buffer_arg(k - TypeFunc::Parms), "only buffer argument removed here");
2041 igvn.replace_input_of(call, k, igvn.zerocon(T_OBJECT));
2042 }
2043 }
2044 }
2045 return true;
2046 }
2047
2048 void Compile::process_inline_types(PhaseIterGVN &igvn, bool remove) {
2049 // Make sure that the return value does not keep an otherwise unused allocation alive
2050 if (tf()->returns_inline_type_as_fields()) {
2051 Node* ret = nullptr;
2052 for (uint i = 1; i < root()->req(); i++) {
2053 Node* in = root()->in(i);
2054 if (in->Opcode() == Op_Return) {
2055 assert(ret == nullptr, "only one return");
2056 ret = in;
2057 }
2058 }
2059 if (ret != nullptr) {
2060 Node* ret_val = ret->in(TypeFunc::Parms);
2061 if (igvn.type(ret_val)->isa_oopptr() &&
2062 return_val_keeps_allocations_alive(ret_val)) {
2063 igvn.replace_input_of(ret, TypeFunc::Parms, InlineTypeNode::tagged_klass(igvn.type(ret_val)->inline_klass(), igvn));
2064 assert(ret_val->outcnt() == 0, "should be dead now");
2065 igvn.remove_dead_node(ret_val);
2066 }
2067 }
2068 }
2069 // if a newly allocated object is a value that's only passed as argument to calls as (possibly null) buffers, then
2070 // clear the call argument inputs so the allocation node can be removed
2071 for (int i = 0; i < C->macro_count(); ++i) {
2072 Node* macro_node = C->macro_node(i);
2073 if (macro_node->Opcode() == Op_Allocate) {
2074 AllocateNode* allocate = macro_node->as_Allocate();
2075 Node* result_cast = allocate->result_cast();
2076 if (result_cast != nullptr) {
2077 const Type* result_type = igvn.type(result_cast);
2078 if (result_type->is_inlinetypeptr()) {
2079 clear_argument_if_only_used_as_buffer_at_calls(result_cast, igvn);
2080 }
2081 }
2082 }
2083 }
2084
2085 if (_inline_type_nodes.length() == 0) {
2086 // keep the graph canonical
2087 igvn.optimize();
2088 return;
2089 }
2090 // Scalarize inline types in safepoint debug info.
2091 // Delay this until all inlining is over to avoid getting inconsistent debug info.
2092 set_scalarize_in_safepoints(true);
2093 for (int i = _inline_type_nodes.length()-1; i >= 0; i--) {
2094 InlineTypeNode* vt = _inline_type_nodes.at(i)->as_InlineType();
2095 vt->make_scalar_in_safepoints(&igvn);
2096 igvn.record_for_igvn(vt);
2097 }
2098 if (remove) {
2099 // Remove inline type nodes by replacing them with their oop input
2100 while (_inline_type_nodes.length() > 0) {
2101 InlineTypeNode* vt = _inline_type_nodes.pop()->as_InlineType();
2102 if (vt->outcnt() == 0) {
2103 igvn.remove_dead_node(vt);
2104 continue;
2105 }
2106 for (DUIterator i = vt->outs(); vt->has_out(i); i++) {
2107 DEBUG_ONLY(bool must_be_buffered = false);
2108 Node* u = vt->out(i);
2109 // Check if any users are blackholes. If so, rewrite them to use either the
2110 // allocated buffer, or individual components, instead of the inline type node
2111 // that goes away.
2112 if (u->is_Blackhole()) {
2113 BlackholeNode* bh = u->as_Blackhole();
2114
2115 // Unlink the old input
2116 int idx = bh->find_edge(vt);
2117 assert(idx != -1, "The edge should be there");
2118 bh->del_req(idx);
2119 --i;
2120
2121 if (vt->is_allocated(&igvn)) {
2122 // Already has the allocated instance, blackhole that
2123 bh->add_req(vt->get_oop());
2124 } else {
2125 // Not allocated yet, blackhole the components
2126 for (uint c = 0; c < vt->field_count(); c++) {
2127 bh->add_req(vt->field_value(c));
2128 }
2129 }
2130
2131 // Node modified, record for IGVN
2132 igvn.record_for_igvn(bh);
2133 }
2134 #ifdef ASSERT
2135 // Verify that inline type is buffered when replacing by oop
2136 else if (u->is_InlineType()) {
2137 // InlineType uses don't need buffering because they are about to be replaced as well
2138 } else if (u->is_Phi()) {
2139 // TODO 8302217 Remove this once InlineTypeNodes are reliably pushed through
2140 } else {
2141 must_be_buffered = true;
2142 }
2143 if (must_be_buffered && !vt->is_allocated(&igvn)) {
2144 vt->dump(0);
2145 u->dump(0);
2146 assert(false, "Should have been buffered");
2147 }
2148 #endif
2149 }
2150 igvn.replace_node(vt, vt->get_oop());
2151 }
2152 }
2153 igvn.optimize();
2154 }
2155
2156 void Compile::add_flat_access(Node* n) {
2157 assert(n != nullptr && (n->Opcode() == Op_LoadFlat || n->Opcode() == Op_StoreFlat), "unexpected node %s", n == nullptr ? "nullptr" : n->Name());
2158 assert(!_flat_access_nodes.contains(n), "duplicate insertion");
2159 _flat_access_nodes.push(n);
2160 }
2161
2162 void Compile::remove_flat_access(Node* n) {
2163 assert(n != nullptr && (n->Opcode() == Op_LoadFlat || n->Opcode() == Op_StoreFlat), "unexpected node %s", n == nullptr ? "nullptr" : n->Name());
2164 _flat_access_nodes.remove_if_existing(n);
2165 }
2166
2167 void Compile::process_flat_accesses(PhaseIterGVN& igvn) {
2168 assert(igvn._worklist.size() == 0, "should be empty");
2169 igvn.set_delay_transform(true);
2170 for (int i = _flat_access_nodes.length() - 1; i >= 0; i--) {
2171 Node* n = _flat_access_nodes.at(i);
2172 assert(n != nullptr, "unexpected nullptr");
2173 if (n->is_LoadFlat()) {
2174 LoadFlatNode* loadn = n->as_LoadFlat();
2175 // Expending a flat load atomically means that we get a chunk of memory spanning multiple fields
2176 // that we chop with bitwise operations. That is too subtle for some optimizations, especially
2177 // constant folding when fields are constant. If we can get a constant object from which we are
2178 // flat-loading, we can simply replace the loads at compilation-time by the field of the constant
2179 // object.
2180 ciInstance* loaded_from = nullptr;
2181 if (FoldStableValues) {
2182 const TypeOopPtr* base_type = igvn.type(loadn->base())->is_oopptr();
2183 ciObject* oop = base_type->const_oop();
2184 int off = igvn.type(loadn->ptr())->isa_ptr()->offset();
2185
2186 if (oop != nullptr && oop->is_instance()) {
2187 ciInstance* holder = oop->as_instance();
2188 ciKlass* klass = holder->klass();
2189 ciInstanceKlass* iklass = klass->as_instance_klass();
2190 ciField* field = iklass->get_non_flat_field_by_offset(off);
2191
2192 if (field->is_stable()) {
2193 ciConstant fv = holder->field_value(field);
2194 if (is_reference_type(fv.basic_type()) && fv.as_object()->is_instance()) {
2195 // The field value is an object, not null. We can use stability.
2196 loaded_from = fv.as_object()->as_instance();
2197 }
2198 }
2199 } else if (oop != nullptr && oop->is_array() && off != Type::OffsetBot) {
2200 ciArray* array = oop->as_array();
2201 ciConstant elt = array->element_value_by_offset(off);
2202 const TypeAryPtr* aryptr = base_type->is_aryptr();
2203 if (aryptr->is_stable() && aryptr->is_atomic() && is_reference_type(elt.basic_type()) && elt.as_object()->is_instance()) {
2204 loaded_from = elt.as_object()->as_instance();
2205 }
2206 }
2207 }
2208
2209 if (loaded_from != nullptr) {
2210 loadn->expand_constant(igvn, loaded_from);
2211 } else {
2212 loadn->expand_atomic(igvn);
2213 }
2214 } else {
2215 n->as_StoreFlat()->expand_atomic(igvn);
2216 }
2217 }
2218 _flat_access_nodes.clear_and_deallocate();
2219 igvn.set_delay_transform(false);
2220 igvn.optimize();
2221 }
2222
2223 void Compile::adjust_flat_array_access_aliases(PhaseIterGVN& igvn) {
2224 DEBUG_ONLY(igvn.verify_empty_worklist(nullptr));
2225 if (!_has_flat_accesses) {
2226 return;
2227 }
2228 // Initially, all flat array accesses share the same slice to
2229 // keep dependencies with Object[] array accesses (that could be
2230 // to a flat array) correct. We're done with parsing so we
2231 // now know all flat array accesses in this compile
2232 // unit. Let's move flat array accesses to their own slice,
2233 // one per element field. This should help memory access
2234 // optimizations.
2235 ResourceMark rm;
2236 Unique_Node_List wq;
2237 wq.push(root());
2238
2239 Node_List mergememnodes;
2240 Node_List memnodes;
2241
2242 // Alias index currently shared by all flat memory accesses
2243 int index = get_alias_index(TypeAryPtr::INLINES);
2244
2245 // Find MergeMem nodes and flat array accesses
2246 for (uint i = 0; i < wq.size(); i++) {
2247 Node* n = wq.at(i);
2248 if (n->is_Mem()) {
2249 const TypePtr* adr_type = nullptr;
2250 adr_type = get_adr_type(get_alias_index(n->adr_type()));
2251 if (adr_type == TypeAryPtr::INLINES) {
2252 memnodes.push(n);
2253 }
2254 } else if (n->is_MergeMem()) {
2255 MergeMemNode* mm = n->as_MergeMem();
2256 if (mm->memory_at(index) != mm->base_memory()) {
2257 mergememnodes.push(n);
2258 }
2259 }
2260 for (uint j = 0; j < n->req(); j++) {
2261 Node* m = n->in(j);
2262 if (m != nullptr) {
2263 wq.push(m);
2264 }
2265 }
2266 }
2267
2268 _flat_accesses_share_alias = false;
2269
2270 // We are going to change the slice for the flat array
2271 // accesses so we need to clear the cache entries that refer to
2272 // them.
2273 for (uint i = 0; i < AliasCacheSize; i++) {
2274 AliasCacheEntry* ace = &_alias_cache[i];
2275 if (ace->_adr_type != nullptr &&
2276 ace->_adr_type->is_flat()) {
2277 ace->_adr_type = nullptr;
2278 ace->_index = (i != 0) ? 0 : AliasIdxTop; // Make sure the nullptr adr_type resolves to AliasIdxTop
2279 }
2280 }
2281
2282 #ifdef ASSERT
2283 for (uint i = 0; i < memnodes.size(); i++) {
2284 Node* m = memnodes.at(i);
2285 const TypePtr* adr_type = m->adr_type();
2286 m->as_Mem()->set_adr_type(adr_type);
2287 }
2288 #endif // ASSERT
2289
2290 int start_alias = num_alias_types(); // Start of new aliases
2291 Node_Stack stack(0);
2292 #ifdef ASSERT
2293 VectorSet seen(Thread::current()->resource_area());
2294 #endif
2295 // Now let's fix the memory graph so each flat array access
2296 // is moved to the right slice. Start from the MergeMem nodes.
2297 uint last = unique();
2298 for (uint i = 0; i < mergememnodes.size(); i++) {
2299 MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2300 if (current->outcnt() == 0) {
2301 // This node is killed by a previous iteration
2302 continue;
2303 }
2304
2305 Node* n = current->memory_at(index);
2306 MergeMemNode* mm = nullptr;
2307 do {
2308 // Follow memory edges through memory accesses, phis and
2309 // narrow membars and push nodes on the stack. Once we hit
2310 // bottom memory, we pop element off the stack one at a
2311 // time, in reverse order, and move them to the right slice
2312 // by changing their memory edges.
2313 if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() ||
2314 (n->adr_type() == TypeAryPtr::INLINES && !n->is_NarrowMemProj())) {
2315 assert(!seen.test_set(n->_idx), "");
2316 // Uses (a load for instance) will need to be moved to the
2317 // right slice as well and will get a new memory state
2318 // that we don't know yet. The use could also be the
2319 // backedge of a loop. We put a place holder node between
2320 // the memory node and its uses. We replace that place
2321 // holder with the correct memory state once we know it,
2322 // i.e. when nodes are popped off the stack. Using the
2323 // place holder make the logic work in the presence of
2324 // loops.
2325 if (n->outcnt() > 1) {
2326 Node* place_holder = nullptr;
2327 assert(!n->has_out_with(Op_Node), "");
2328 for (DUIterator k = n->outs(); n->has_out(k); k++) {
2329 Node* u = n->out(k);
2330 if (u != current && u->_idx < last) {
2331 bool success = false;
2332 for (uint l = 0; l < u->req(); l++) {
2333 if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2334 continue;
2335 }
2336 Node* in = u->in(l);
2337 if (in == n) {
2338 if (place_holder == nullptr) {
2339 place_holder = new Node(1);
2340 place_holder->init_req(0, n);
2341 }
2342 igvn.replace_input_of(u, l, place_holder);
2343 success = true;
2344 }
2345 }
2346 if (success) {
2347 --k;
2348 }
2349 }
2350 }
2351 }
2352 if (n->is_Phi()) {
2353 stack.push(n, 1);
2354 n = n->in(1);
2355 } else if (n->is_Mem()) {
2356 stack.push(n, n->req());
2357 n = n->in(MemNode::Memory);
2358 } else {
2359 assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2360 stack.push(n, n->req());
2361 n = n->in(0)->in(TypeFunc::Memory);
2362 }
2363 } else {
2364 assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || n->is_NarrowMemProj(), "");
2365 // Build a new MergeMem node to carry the new memory state
2366 // as we build it. IGVN should fold extraneous MergeMem
2367 // nodes.
2368 if (n->is_NarrowMemProj()) {
2369 // We need 1 NarrowMemProj for each slice of this array
2370 InitializeNode* init = n->in(0)->as_Initialize();
2371 AllocateNode* alloc = init->allocation();
2372 Node* klass_node = alloc->in(AllocateNode::KlassNode);
2373 const TypeAryKlassPtr* klass_type = klass_node->bottom_type()->isa_aryklassptr();
2374 assert(klass_type != nullptr, "must be an array");
2375 assert(klass_type->klass_is_exact(), "must be an exact klass");
2376 ciArrayKlass* klass = klass_type->exact_klass()->as_array_klass();
2377 assert(klass->is_flat_array_klass(), "must be a flat array");
2378 ciInlineKlass* elem_klass = klass->element_klass()->as_inline_klass();
2379 const TypeAryPtr* oop_type = klass_type->as_instance_type()->is_aryptr();
2380 assert(oop_type->klass_is_exact(), "must be an exact klass");
2381
2382 Node* base = alloc->in(TypeFunc::Memory);
2383 assert(base->bottom_type() == Type::MEMORY, "the memory input of AllocateNode must be a memory");
2384 assert(base->adr_type() == TypePtr::BOTTOM, "the memory input of AllocateNode must be a bottom memory");
2385 // Must create a MergeMem with base as the base memory, do not clone if base is a
2386 // MergeMem because it may not be processed yet
2387 mm = MergeMemNode::make(nullptr);
2388 mm->set_base_memory(base);
2389 for (int j = 0; j < elem_klass->nof_nonstatic_fields(); j++) {
2390 int field_offset = elem_klass->nonstatic_field_at(j)->offset_in_bytes() - elem_klass->payload_offset();
2391 const TypeAryPtr* field_ptr = oop_type->with_offset(Type::OffsetBot)->with_field_offset(field_offset);
2392 int field_alias_idx = get_alias_index(field_ptr);
2393 assert(field_ptr == get_adr_type(field_alias_idx), "must match");
2394 Node* new_proj = new NarrowMemProjNode(init, field_ptr);
2395 igvn.register_new_node_with_optimizer(new_proj);
2396 mm->set_memory_at(field_alias_idx, new_proj);
2397 }
2398 if (!klass->is_elem_null_free()) {
2399 int nm_offset = elem_klass->null_marker_offset_in_payload();
2400 const TypeAryPtr* nm_ptr = oop_type->with_offset(Type::OffsetBot)->with_field_offset(nm_offset);
2401 int nm_alias_idx = get_alias_index(nm_ptr);
2402 assert(nm_ptr == get_adr_type(nm_alias_idx), "must match");
2403 Node* new_proj = new NarrowMemProjNode(init, nm_ptr);
2404 igvn.register_new_node_with_optimizer(new_proj);
2405 mm->set_memory_at(nm_alias_idx, new_proj);
2406 }
2407
2408 // Replace all uses of the old NarrowMemProj with the correct state
2409 MergeMemNode* new_n = MergeMemNode::make(mm);
2410 igvn.register_new_node_with_optimizer(new_n);
2411 igvn.replace_node(n, new_n);
2412 } else {
2413 // Must create a MergeMem with n as the base memory, do not clone if n is a MergeMem
2414 // because it may not be processed yet
2415 mm = MergeMemNode::make(nullptr);
2416 mm->set_base_memory(n);
2417 }
2418
2419 igvn.register_new_node_with_optimizer(mm);
2420 while (stack.size() > 0) {
2421 Node* m = stack.node();
2422 uint idx = stack.index();
2423 if (m->is_Mem()) {
2424 // Move memory node to its new slice
2425 const TypePtr* adr_type = m->adr_type();
2426 int alias = get_alias_index(adr_type);
2427 Node* prev = mm->memory_at(alias);
2428 igvn.replace_input_of(m, MemNode::Memory, prev);
2429 mm->set_memory_at(alias, m);
2430 } else if (m->is_Phi()) {
2431 // We need as many new phis as there are new aliases
2432 Node* new_phi_in = MergeMemNode::make(mm);
2433 igvn.register_new_node_with_optimizer(new_phi_in);
2434 igvn.replace_input_of(m, idx, new_phi_in);
2435 if (idx == m->req()-1) {
2436 Node* r = m->in(0);
2437 for (int j = start_alias; j < num_alias_types(); j++) {
2438 const TypePtr* adr_type = get_adr_type(j);
2439 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2440 continue;
2441 }
2442 Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2443 igvn.register_new_node_with_optimizer(phi);
2444 for (uint k = 1; k < m->req(); k++) {
2445 phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2446 }
2447 mm->set_memory_at(j, phi);
2448 }
2449 Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2450 igvn.register_new_node_with_optimizer(base_phi);
2451 for (uint k = 1; k < m->req(); k++) {
2452 base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2453 }
2454 mm->set_base_memory(base_phi);
2455 }
2456 } else {
2457 // This is a MemBarCPUOrder node from
2458 // Parse::array_load()/Parse::array_store(), in the
2459 // branch that handles flat arrays hidden under
2460 // an Object[] array. We also need one new membar per
2461 // new alias to keep the unknown access that the
2462 // membars protect properly ordered with accesses to
2463 // known flat array.
2464 assert(m->is_Proj(), "projection expected");
2465 Node* ctrl = m->in(0)->in(TypeFunc::Control);
2466 igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2467 for (int j = start_alias; j < num_alias_types(); j++) {
2468 const TypePtr* adr_type = get_adr_type(j);
2469 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2470 continue;
2471 }
2472 MemBarNode* mb = new MemBarCPUOrderNode(this, j, nullptr);
2473 igvn.register_new_node_with_optimizer(mb);
2474 Node* mem = mm->memory_at(j);
2475 mb->init_req(TypeFunc::Control, ctrl);
2476 mb->init_req(TypeFunc::Memory, mem);
2477 ctrl = new ProjNode(mb, TypeFunc::Control);
2478 igvn.register_new_node_with_optimizer(ctrl);
2479 mem = new ProjNode(mb, TypeFunc::Memory);
2480 igvn.register_new_node_with_optimizer(mem);
2481 mm->set_memory_at(j, mem);
2482 }
2483 igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2484 }
2485 if (idx < m->req()-1) {
2486 idx += 1;
2487 stack.set_index(idx);
2488 n = m->in(idx);
2489 break;
2490 }
2491 // Take care of place holder nodes
2492 if (m->has_out_with(Op_Node)) {
2493 Node* place_holder = m->find_out_with(Op_Node);
2494 if (place_holder != nullptr) {
2495 Node* mm_clone = mm->clone();
2496 igvn.register_new_node_with_optimizer(mm_clone);
2497 Node* hook = new Node(1);
2498 hook->init_req(0, mm);
2499 igvn.replace_node(place_holder, mm_clone);
2500 hook->destruct(&igvn);
2501 }
2502 assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2503 }
2504 stack.pop();
2505 }
2506 }
2507 } while(stack.size() > 0);
2508 // Fix the memory state at the MergeMem we started from
2509 igvn.rehash_node_delayed(current);
2510 for (int j = start_alias; j < num_alias_types(); j++) {
2511 const TypePtr* adr_type = get_adr_type(j);
2512 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2513 continue;
2514 }
2515 current->set_memory_at(j, mm);
2516 }
2517 current->set_memory_at(index, current->base_memory());
2518 }
2519 igvn.optimize();
2520
2521 #ifdef ASSERT
2522 wq.clear();
2523 wq.push(root());
2524 for (uint i = 0; i < wq.size(); i++) {
2525 Node* n = wq.at(i);
2526 assert(n->adr_type() != TypeAryPtr::INLINES, "should have been removed from the graph");
2527 for (uint j = 0; j < n->req(); j++) {
2528 Node* m = n->in(j);
2529 if (m != nullptr) {
2530 wq.push(m);
2531 }
2532 }
2533 }
2534 #endif
2535
2536 print_method(PHASE_SPLIT_INLINES_ARRAY, 2);
2537 }
2538
2539 void Compile::record_for_merge_stores_igvn(Node* n) {
2540 if (!n->for_merge_stores_igvn()) {
2541 assert(!_for_merge_stores_igvn.contains(n), "duplicate");
2542 n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2543 _for_merge_stores_igvn.append(n);
2544 }
2545 }
2546
2547 void Compile::remove_from_merge_stores_igvn(Node* n) {
2548 n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2549 _for_merge_stores_igvn.remove(n);
2550 }
2551
2552 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
2553 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
2554 // the stores, and we merge the wrong sequence of stores.
2555 // Example:
2556 // StoreI RangeCheck StoreI StoreI RangeCheck StoreI
2557 // Apply MergeStores:
2558 // StoreI RangeCheck [ StoreL ] RangeCheck StoreI
2637 assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
2638 Bytecodes::Code c = iter.cur_bc();
2639 Node* lhs = nullptr;
2640 Node* rhs = nullptr;
2641 if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
2642 lhs = unc->peek_operand(0);
2643 rhs = unc->peek_operand(1);
2644 } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2645 lhs = unc->peek_operand(0);
2646 }
2647
2648 ResourceMark rm;
2649 const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2650 assert(live_locals.is_valid(), "broken liveness info");
2651 int len = (int)live_locals.size();
2652
2653 for (int i = 0; i < len; i++) {
2654 Node* local = unc->local(jvms, i);
2655 // kill local using the liveness of next_bci.
2656 // give up when the local looks like an operand to secure reexecution.
2657 if (!live_locals.at(i) && !local->is_top() && local != lhs && local != rhs) {
2658 uint idx = jvms->locoff() + i;
2659 #ifdef ASSERT
2660 if (PrintOpto && Verbose) {
2661 tty->print("[unstable_if] kill local#%d: ", idx);
2662 local->dump();
2663 tty->cr();
2664 }
2665 #endif
2666 igvn.replace_input_of(unc, idx, top());
2667 modified = true;
2668 }
2669 }
2670 }
2671
2672 // keep the modified trap for late query
2673 if (modified) {
2674 trap->set_modified();
2675 } else {
2676 _unstable_if_traps.delete_at(i);
2677 }
2678 }
2679 igvn.optimize();
2680 }
2681
2682 // StringOpts and late inlining of string methods
2683 void Compile::inline_string_calls(bool parse_time) {
2684 {
2685 // remove useless nodes to make the usage analysis simpler
2686 ResourceMark rm;
2687 PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2688 }
2689
2690 {
2691 ResourceMark rm;
2692 print_method(PHASE_BEFORE_STRINGOPTS, 3);
2884
2885 if (_string_late_inlines.length() > 0) {
2886 assert(has_stringbuilder(), "inconsistent");
2887
2888 inline_string_calls(false);
2889
2890 if (failing()) return;
2891
2892 inline_incrementally_cleanup(igvn);
2893 }
2894
2895 set_inlining_incrementally(false);
2896 }
2897
2898 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2899 // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2900 // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2901 // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2902 // as if "inlining_incrementally() == true" were set.
2903 assert(inlining_incrementally() == false, "not allowed");
2904 set_strength_reduction(true);
2905 #ifdef ASSERT
2906 Unique_Node_List* modified_nodes = _modified_nodes;
2907 _modified_nodes = nullptr;
2908 #endif
2909 assert(_late_inlines.length() > 0, "sanity");
2910
2911 if (StressIncrementalInlining) {
2912 shuffle_late_inlines();
2913 }
2914
2915 while (_late_inlines.length() > 0) {
2916 igvn_worklist()->ensure_empty(); // should be done with igvn
2917
2918 while (inline_incrementally_one()) {
2919 assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2920 }
2921 if (failing()) return;
2922
2923 inline_incrementally_cleanup(igvn);
2924 }
2925 DEBUG_ONLY( _modified_nodes = modified_nodes; )
2926 set_strength_reduction(false);
2927 }
2928
2929 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2930 if (_loop_opts_cnt > 0) {
2931 while (major_progress() && (_loop_opts_cnt > 0)) {
2932 TracePhase tp(_t_idealLoop);
2933 PhaseIdealLoop::optimize(igvn, mode);
2934 _loop_opts_cnt--;
2935 if (failing()) return false;
2936 if (major_progress()) {
2937 print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2938 }
2939 }
2940 }
2941 return true;
2942 }
2943
2944 // Remove edges from "root" to each SafePoint at a backward branch.
2945 // They were inserted during parsing (see add_safepoint()) to make
2946 // infinite loops without calls or exceptions visible to root, i.e.,
3052 print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
3053 }
3054 assert(!has_vbox_nodes(), "sanity");
3055
3056 if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
3057 Compile::TracePhase tp(_t_renumberLive);
3058 igvn_worklist()->ensure_empty(); // should be done with igvn
3059 {
3060 ResourceMark rm;
3061 PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
3062 }
3063 igvn.reset();
3064 igvn.optimize();
3065 if (failing()) return;
3066 }
3067
3068 // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
3069 // safepoints
3070 remove_root_to_sfpts_edges(igvn);
3071
3072 // Process inline type nodes now that all inlining is over
3073 process_inline_types(igvn);
3074
3075 adjust_flat_array_access_aliases(igvn);
3076
3077 if (failing()) return;
3078
3079 if (C->macro_count() > 0) {
3080 // Eliminate some macro nodes before EA to reduce analysis pressure
3081 PhaseMacroExpand mexp(igvn);
3082 mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
3083 if (failing()) {
3084 return;
3085 }
3086 igvn.set_delay_transform(false);
3087 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3088 }
3089
3090 _print_phase_loop_opts = has_loops();
3091 if (_print_phase_loop_opts) {
3092 print_method(PHASE_BEFORE_LOOP_OPTS, 2);
3093 }
3094
3095 // Perform escape analysis
3096 if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
3097 if (has_loops()) {
3098 // Cleanup graph (remove dead nodes).
3099 TracePhase tp(_t_idealLoop);
3100 PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
3101 if (failing()) {
3102 return;
3103 }
3104 print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
3105 if (C->macro_count() > 0) {
3106 // Eliminate some macro nodes before EA to reduce analysis pressure
3107 PhaseMacroExpand mexp(igvn);
3108 mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
3109 if (failing()) {
3110 return;
3111 }
3112 igvn.set_delay_transform(false);
3113 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3114 }
3115 }
3116
3117 bool progress;
3118 do {
3119 ConnectionGraph::do_analysis(this, &igvn);
3120
3121 if (failing()) return;
3122
3123 int mcount = macro_count(); // Record number of allocations and locks before IGVN
3124
3125 // Optimize out fields loads from scalar replaceable allocations.
3126 igvn.optimize();
3127 print_method(PHASE_ITER_GVN_AFTER_EA, 2);
3128
3129 if (failing()) return;
3130
3131 if (congraph() != nullptr && macro_count() > 0) {
3132 TracePhase tp(_t_macroEliminate);
3133 PhaseMacroExpand mexp(igvn);
3134 mexp.eliminate_macro_nodes();
3135 if (failing()) {
3136 return;
3137 }
3138 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
3139
3140 igvn.set_delay_transform(false);
3141 print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3142 }
3143
3144 ConnectionGraph::verify_ram_nodes(this, root());
3145 if (failing()) return;
3146
3147 progress = do_iterative_escape_analysis() &&
3148 (macro_count() < mcount) &&
3149 ConnectionGraph::has_candidates(this);
3150 // Try again if candidates exist and made progress
3151 // by removing some allocations and/or locks.
3152 } while (progress);
3153 }
3154
3155 process_flat_accesses(igvn);
3156 if (failing()) {
3157 return;
3158 }
3159
3160 // Loop transforms on the ideal graph. Range Check Elimination,
3161 // peeling, unrolling, etc.
3162
3163 // Set loop opts counter
3164 if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
3165 {
3166 TracePhase tp(_t_idealLoop);
3167 PhaseIdealLoop::optimize(igvn, LoopOptsDefault);
3168 _loop_opts_cnt--;
3169 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
3170 if (failing()) return;
3171 }
3172 // Loop opts pass if partial peeling occurred in previous pass
3173 if(PartialPeelLoop && major_progress() && (_loop_opts_cnt > 0)) {
3174 TracePhase tp(_t_idealLoop);
3175 PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);
3176 _loop_opts_cnt--;
3177 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
3178 if (failing()) return;
3179 }
3216 // Loop transforms on the ideal graph. Range Check Elimination,
3217 // peeling, unrolling, etc.
3218 if (!optimize_loops(igvn, LoopOptsDefault)) {
3219 return;
3220 }
3221
3222 if (failing()) return;
3223
3224 C->clear_major_progress(); // ensure that major progress is now clear
3225
3226 process_for_post_loop_opts_igvn(igvn);
3227
3228 process_for_merge_stores_igvn(igvn);
3229
3230 if (failing()) return;
3231
3232 #ifdef ASSERT
3233 bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
3234 #endif
3235
3236 assert(_late_inlines.length() == 0 || IncrementalInlineMH || IncrementalInlineVirtual, "not empty");
3237
3238 if (_late_inlines.length() > 0) {
3239 // More opportunities to optimize virtual and MH calls.
3240 // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
3241 process_late_inline_calls_no_inline(igvn);
3242 process_inline_types(igvn);
3243 }
3244
3245 {
3246 TracePhase tp(_t_macroExpand);
3247 PhaseMacroExpand mex(igvn);
3248 // Last attempt to eliminate macro nodes.
3249 mex.eliminate_macro_nodes();
3250 if (failing()) {
3251 return;
3252 }
3253
3254 print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);
3255 // Do not allow new macro nodes once we start to eliminate and expand
3256 C->reset_allow_macro_nodes();
3257 // Last attempt to eliminate macro nodes before expand
3258 mex.eliminate_macro_nodes();
3259 if (failing()) {
3260 return;
3261 }
3262 mex.eliminate_opaque_looplimit_macro_nodes();
3263 if (failing()) {
3264 return;
3265 }
3266 print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
3267 if (mex.expand_macro_nodes()) {
3268 assert(failing(), "must bail out w/ explicit message");
3269 return;
3270 }
3271 print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
3272 }
3273
3274 // Process inline type nodes again and remove them. From here
3275 // on we don't need to keep track of field values anymore.
3276 process_inline_types(igvn, /* remove= */ true);
3277
3278 {
3279 TracePhase tp(_t_barrierExpand);
3280 if (bs->expand_barriers(this, igvn)) {
3281 assert(failing(), "must bail out w/ explicit message");
3282 return;
3283 }
3284 print_method(PHASE_BARRIER_EXPANSION, 2);
3285 }
3286
3287 if (C->max_vector_size() > 0) {
3288 C->optimize_logic_cones(igvn);
3289 igvn.optimize();
3290 if (failing()) return;
3291 }
3292
3293 DEBUG_ONLY( _modified_nodes = nullptr; )
3294 DEBUG_ONLY( _late_inlines.clear(); )
3295
3296 assert(igvn._worklist.size() == 0, "not empty");
3297 } // (End scope of igvn; run destructor if necessary for asserts.)
3298
3299 check_no_dead_use();
3300
3301 // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
3302 // to remove hashes to unlock nodes for modifications.
3303 C->node_hash()->clear();
3304
3305 // A method with only infinite loops has no edges entering loops from root
3306 {
3307 TracePhase tp(_t_graphReshaping);
3308 if (final_graph_reshaping()) {
3309 assert(failing(), "must bail out w/ explicit message");
3310 return;
3311 }
3312 }
3313
3314 print_method(PHASE_OPTIMIZE_FINISHED, 2);
3315 DEBUG_ONLY(set_phase_optimize_finished();)
3316 }
4022 case Op_CmpD3:
4023 case Op_StoreD:
4024 case Op_LoadD:
4025 case Op_LoadD_unaligned:
4026 frc.inc_double_count();
4027 break;
4028 case Op_Opaque1: // Remove Opaque Nodes before matching
4029 n->subsume_by(n->in(1), this);
4030 break;
4031 case Op_CallLeafPure: {
4032 // If the pure call is not supported, then lower to a CallLeaf.
4033 if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
4034 CallNode* call = n->as_Call();
4035 CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
4036 call->_name, TypeRawPtr::BOTTOM);
4037 new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
4038 new_call->init_req(TypeFunc::I_O, C->top());
4039 new_call->init_req(TypeFunc::Memory, C->top());
4040 new_call->init_req(TypeFunc::ReturnAdr, C->top());
4041 new_call->init_req(TypeFunc::FramePtr, C->top());
4042 for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain_sig()->cnt(); i++) {
4043 new_call->init_req(i, call->in(i));
4044 }
4045 n->subsume_by(new_call, this);
4046 }
4047 frc.inc_call_count();
4048 break;
4049 }
4050 case Op_CallStaticJava:
4051 case Op_CallJava:
4052 case Op_CallDynamicJava:
4053 frc.inc_java_call_count(); // Count java call site;
4054 case Op_CallRuntime:
4055 case Op_CallLeaf:
4056 case Op_CallLeafVector:
4057 case Op_CallLeafNoFP: {
4058 assert (n->is_Call(), "");
4059 CallNode *call = n->as_Call();
4060 // Count call sites where the FP mode bit would have to be flipped.
4061 // Do not count uncommon runtime calls:
4062 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
4068 int nop = n->Opcode();
4069 // Clone shared simple arguments to uncommon calls, item (1).
4070 if (n->outcnt() > 1 &&
4071 !n->is_Proj() &&
4072 nop != Op_CreateEx &&
4073 nop != Op_CheckCastPP &&
4074 nop != Op_DecodeN &&
4075 nop != Op_DecodeNKlass &&
4076 !n->is_Mem() &&
4077 !n->is_Phi()) {
4078 Node *x = n->clone();
4079 call->set_req(TypeFunc::Parms, x);
4080 }
4081 }
4082 break;
4083 }
4084 case Op_StoreB:
4085 case Op_StoreC:
4086 case Op_StoreI:
4087 case Op_StoreL:
4088 case Op_StoreLSpecial:
4089 case Op_CompareAndSwapB:
4090 case Op_CompareAndSwapS:
4091 case Op_CompareAndSwapI:
4092 case Op_CompareAndSwapL:
4093 case Op_CompareAndSwapP:
4094 case Op_CompareAndSwapN:
4095 case Op_WeakCompareAndSwapB:
4096 case Op_WeakCompareAndSwapS:
4097 case Op_WeakCompareAndSwapI:
4098 case Op_WeakCompareAndSwapL:
4099 case Op_WeakCompareAndSwapP:
4100 case Op_WeakCompareAndSwapN:
4101 case Op_CompareAndExchangeB:
4102 case Op_CompareAndExchangeS:
4103 case Op_CompareAndExchangeI:
4104 case Op_CompareAndExchangeL:
4105 case Op_CompareAndExchangeP:
4106 case Op_CompareAndExchangeN:
4107 case Op_GetAndAddS:
4108 case Op_GetAndAddB:
4624 k->subsume_by(m, this);
4625 }
4626 }
4627 }
4628 break;
4629 }
4630 case Op_CmpUL: {
4631 if (!Matcher::has_match_rule(Op_CmpUL)) {
4632 // No support for unsigned long comparisons
4633 ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
4634 Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
4635 Node* orl = new OrLNode(n->in(1), sign_bit_mask);
4636 ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
4637 Node* andl = new AndLNode(orl, remove_sign_mask);
4638 Node* cmp = new CmpLNode(andl, n->in(2));
4639 n->subsume_by(cmp, this);
4640 }
4641 break;
4642 }
4643 #ifdef ASSERT
4644 case Op_InlineType: {
4645 n->dump(-1);
4646 assert(false, "inline type node was not removed");
4647 break;
4648 }
4649 case Op_ConNKlass: {
4650 const TypePtr* tp = n->as_Type()->type()->make_ptr();
4651 ciKlass* klass = tp->is_klassptr()->exact_klass();
4652 assert(klass->is_in_encoding_range(), "klass cannot be compressed");
4653 break;
4654 }
4655 #endif
4656 default:
4657 assert(!n->is_Call(), "");
4658 assert(!n->is_Mem(), "");
4659 assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4660 break;
4661 }
4662 }
4663
4664 //------------------------------final_graph_reshaping_walk---------------------
4665 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4666 // requires that the walk visits a node's inputs before visiting the node.
4667 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
4668 Unique_Node_List sfpt;
5004 }
5005 }
5006
5007 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
5008 return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
5009 }
5010
5011 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
5012 return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
5013 }
5014
5015 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
5016 if (holder->is_initialized()) {
5017 return false;
5018 }
5019 if (holder->is_being_initialized()) {
5020 if (accessing_method->holder() == holder) {
5021 // Access inside a class. The barrier can be elided when access happens in <clinit>,
5022 // <init>, or a static method. In all those cases, there was an initialization
5023 // barrier on the holder klass passed.
5024 if (accessing_method->is_class_initializer() ||
5025 accessing_method->is_object_constructor() ||
5026 accessing_method->is_static()) {
5027 return false;
5028 }
5029 } else if (accessing_method->holder()->is_subclass_of(holder)) {
5030 // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
5031 // In case of <init> or a static method, the barrier is on the subclass is not enough:
5032 // child class can become fully initialized while its parent class is still being initialized.
5033 if (accessing_method->is_class_initializer()) {
5034 return false;
5035 }
5036 }
5037 ciMethod* root = method(); // the root method of compilation
5038 if (root != accessing_method) {
5039 return needs_clinit_barrier(holder, root); // check access in the context of compilation root
5040 }
5041 }
5042 return true;
5043 }
5044
5045 #ifndef PRODUCT
5046 //------------------------------verify_bidirectional_edges---------------------
5047 // For each input edge to a node (ie - for each Use-Def edge), verify that
5048 // there is a corresponding Def-Use edge.
5049 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
5050 // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
5051 uint stack_size = live_nodes() >> 4;
5052 Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
5053 if (root_and_safepoints != nullptr) {
5083 if (in != nullptr && !in->is_top()) {
5084 // Count instances of `next`
5085 int cnt = 0;
5086 for (uint idx = 0; idx < in->_outcnt; idx++) {
5087 if (in->_out[idx] == n) {
5088 cnt++;
5089 }
5090 }
5091 assert(cnt > 0, "Failed to find Def-Use edge.");
5092 // Check for duplicate edges
5093 // walk the input array downcounting the input edges to n
5094 for (uint j = 0; j < length; j++) {
5095 if (n->in(j) == in) {
5096 cnt--;
5097 }
5098 }
5099 assert(cnt == 0, "Mismatched edge count.");
5100 } else if (in == nullptr) {
5101 assert(i == 0 || i >= n->req() ||
5102 n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
5103 (n->is_Allocate() && i >= AllocateNode::InlineType) ||
5104 (n->is_Unlock() && i == (n->req() - 1)) ||
5105 (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
5106 "only region, phi, arraycopy, allocate, unlock or membar nodes have null data edges");
5107 } else {
5108 assert(in->is_top(), "sanity");
5109 // Nothing to check.
5110 }
5111 }
5112 }
5113 }
5114
5115 //------------------------------verify_graph_edges---------------------------
5116 // Walk the Graph and verify that there is a one-to-one correspondence
5117 // between Use-Def edges and Def-Use edges in the graph.
5118 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
5119 if (VerifyGraphEdges) {
5120 Unique_Node_List visited;
5121
5122 // Call graph walk to check edges
5123 verify_bidirectional_edges(visited, root_and_safepoints);
5124 if (no_dead_code) {
5125 // Now make sure that no visited node is used by an unvisited node.
5126 bool dead_nodes = false;
5237 // (1) subklass is already limited to a subtype of superklass => always ok
5238 // (2) subklass does not overlap with superklass => always fail
5239 // (3) superklass has NO subtypes and we can check with a simple compare.
5240 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
5241 if (skip) {
5242 return SSC_full_test; // Let caller generate the general case.
5243 }
5244
5245 if (subk->is_java_subtype_of(superk)) {
5246 return SSC_always_true; // (0) and (1) this test cannot fail
5247 }
5248
5249 if (!subk->maybe_java_subtype_of(superk)) {
5250 return SSC_always_false; // (2) true path dead; no dynamic test needed
5251 }
5252
5253 const Type* superelem = superk;
5254 if (superk->isa_aryklassptr()) {
5255 int ignored;
5256 superelem = superk->is_aryklassptr()->base_element_type(ignored);
5257
5258 // Do not fold the subtype check to an array klass pointer comparison for null-able inline type arrays
5259 // because null-free [LMyValue <: null-able [LMyValue but the klasses are different. Perform a full test.
5260 if (!superk->is_aryklassptr()->is_null_free() && superk->is_aryklassptr()->elem()->isa_instklassptr() &&
5261 superk->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->is_inlinetype()) {
5262 return SSC_full_test;
5263 }
5264 }
5265
5266 if (superelem->isa_instklassptr()) {
5267 ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
5268 if (!ik->has_subklass()) {
5269 if (!ik->is_final()) {
5270 // Add a dependency if there is a chance of a later subclass.
5271 dependencies()->assert_leaf_type(ik);
5272 }
5273 if (!superk->maybe_java_subtype_of(subk)) {
5274 return SSC_always_false;
5275 }
5276 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5277 }
5278 } else {
5279 // A primitive array type has no subtypes.
5280 return SSC_easy_test; // (3) caller can do a simple ptr comparison
5281 }
5282
5283 return SSC_full_test;
6076 } else {
6077 _debug_network_printer->update_compiled_method(C->method());
6078 }
6079 tty->print_cr("Method printed over network stream to IGV");
6080 _debug_network_printer->print(name, C->root(), visible_nodes, fr);
6081 }
6082 #endif // !PRODUCT
6083
6084 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
6085 if (type != nullptr && phase->type(value)->higher_equal(type)) {
6086 return value;
6087 }
6088 Node* result = nullptr;
6089 if (bt == T_BYTE) {
6090 result = phase->transform(new LShiftINode(value, phase->intcon(24)));
6091 result = new RShiftINode(result, phase->intcon(24));
6092 } else if (bt == T_BOOLEAN) {
6093 result = new AndINode(value, phase->intcon(0xFF));
6094 } else if (bt == T_CHAR) {
6095 result = new AndINode(value,phase->intcon(0xFFFF));
6096 } else if (bt == T_FLOAT) {
6097 result = new MoveI2FNode(value);
6098 } else {
6099 assert(bt == T_SHORT, "unexpected narrow type");
6100 result = phase->transform(new LShiftINode(value, phase->intcon(16)));
6101 result = new RShiftINode(result, phase->intcon(16));
6102 }
6103 if (transform_res) {
6104 result = phase->transform(result);
6105 }
6106 return result;
6107 }
6108
6109 void Compile::record_method_not_compilable_oom() {
6110 record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
6111 }
6112
6113 #ifndef PRODUCT
6114 // Collects all the control inputs from nodes on the worklist and from their data dependencies
6115 static void find_candidate_control_inputs(Unique_Node_List& worklist, Unique_Node_List& candidates) {
6116 // Follow non-control edges until we reach CFG nodes
6117 for (uint i = 0; i < worklist.size(); i++) {
|