< prev index next >

src/hotspot/share/opto/compile.cpp

Print this page

   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/reachability.hpp"
  78 #include "opto/rootnode.hpp"
  79 #include "opto/runtime.hpp"
  80 #include "opto/stringopts.hpp"
  81 #include "opto/type.hpp"
  82 #include "opto/vector.hpp"
  83 #include "opto/vectornode.hpp"

  84 #include "runtime/globals_extension.hpp"
  85 #include "runtime/sharedRuntime.hpp"
  86 #include "runtime/signature.hpp"
  87 #include "runtime/stubRoutines.hpp"
  88 #include "runtime/timer.hpp"
  89 #include "utilities/align.hpp"
  90 #include "utilities/copy.hpp"
  91 #include "utilities/hashTable.hpp"
  92 #include "utilities/macros.hpp"
  93 
  94 // -------------------- Compile::mach_constant_base_node -----------------------
  95 // Constant table base node singleton.
  96 MachConstantBaseNode* Compile::mach_constant_base_node() {
  97   if (_mach_constant_base_node == nullptr) {
  98     _mach_constant_base_node = new MachConstantBaseNode();
  99     _mach_constant_base_node->add_req(C->root());
 100   }
 101   return _mach_constant_base_node;
 102 }
 103 

 392     record_dead_node(dead->_idx);
 393   }
 394   if (dead->is_macro()) {
 395     remove_macro_node(dead);
 396   }
 397   if (dead->is_expensive()) {
 398     remove_expensive_node(dead);
 399   }
 400   if (dead->is_ReachabilityFence()) {
 401     remove_reachability_fence(dead->as_ReachabilityFence());
 402   }
 403   if (dead->is_OpaqueTemplateAssertionPredicate()) {
 404     remove_template_assertion_predicate_opaque(dead->as_OpaqueTemplateAssertionPredicate());
 405   }
 406   if (dead->is_ParsePredicate()) {
 407     remove_parse_predicate(dead->as_ParsePredicate());
 408   }
 409   if (dead->for_post_loop_opts_igvn()) {
 410     remove_from_post_loop_opts_igvn(dead);
 411   }






 412   if (dead->for_merge_stores_igvn()) {
 413     remove_from_merge_stores_igvn(dead);
 414   }
 415   if (dead->is_Call()) {
 416     remove_useless_late_inlines(                &_late_inlines, dead);
 417     remove_useless_late_inlines(         &_string_late_inlines, dead);
 418     remove_useless_late_inlines(         &_boxing_late_inlines, dead);
 419     remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
 420 
 421     if (dead->is_CallStaticJava()) {
 422       remove_unstable_if_trap(dead->as_CallStaticJava(), false);
 423     }
 424   }
 425   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 426   bs->unregister_potential_barrier_node(dead);
 427 }
 428 
 429 // Disconnect all useless nodes by disconnecting those at the boundary.
 430 void Compile::disconnect_useless_nodes(Unique_Node_List& useful, Unique_Node_List& worklist, const Unique_Node_List* root_and_safepoints) {
 431   uint next = 0;

 439     // Use raw traversal of out edges since this code removes out edges
 440     int max = n->outcnt();
 441     for (int j = 0; j < max; ++j) {
 442       Node* child = n->raw_out(j);
 443       if (!useful.member(child)) {
 444         assert(!child->is_top() || child != top(),
 445                "If top is cached in Compile object it is in useful list");
 446         // Only need to remove this out-edge to the useless node
 447         n->raw_del_out(j);
 448         --j;
 449         --max;
 450         if (child->is_data_proj_of_pure_function(n)) {
 451           worklist.push(n);
 452         }
 453       }
 454     }
 455     if (n->outcnt() == 1 && n->has_special_unique_user()) {
 456       assert(useful.member(n->unique_out()), "do not push a useless node");
 457       worklist.push(n->unique_out());
 458     }



 459   }
 460 
 461   remove_useless_nodes(_macro_nodes,        useful); // remove useless macro nodes
 462   remove_useless_nodes(_parse_predicates,   useful); // remove useless Parse Predicate nodes
 463   // Remove useless Template Assertion Predicate opaque nodes
 464   remove_useless_nodes(_template_assertion_predicate_opaques, useful);
 465   remove_useless_nodes(_expensive_nodes,    useful); // remove useless expensive nodes
 466   remove_useless_nodes(_reachability_fences, useful); // remove useless node recorded for post loop opts IGVN pass
 467   remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass







 468   remove_useless_nodes(_for_merge_stores_igvn, useful); // remove useless node recorded for merge stores IGVN pass
 469   remove_useless_unstable_if_traps(useful);          // remove useless unstable_if traps
 470   remove_useless_coarsened_locks(useful);            // remove useless coarsened locks nodes
 471 #ifdef ASSERT
 472   if (_modified_nodes != nullptr) {
 473     _modified_nodes->remove_useless_nodes(useful.member_set());
 474   }
 475 #endif
 476 
 477   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 478   bs->eliminate_useless_gc_barriers(useful, this);
 479   // clean up the late inline lists
 480   remove_useless_late_inlines(                &_late_inlines, useful);
 481   remove_useless_late_inlines(         &_string_late_inlines, useful);
 482   remove_useless_late_inlines(         &_boxing_late_inlines, useful);
 483   remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
 484   DEBUG_ONLY(verify_graph_edges(true /*check for no_dead_code*/, root_and_safepoints);)
 485 }
 486 
 487 // ============================================================================

 635 
 636 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
 637                  Options options, DirectiveSet* directive)
 638     : Phase(Compiler),
 639       _compile_id(ci_env->compile_id()),
 640       _options(options),
 641       _method(target),
 642       _entry_bci(osr_bci),
 643       _ilt(nullptr),
 644       _stub_function(nullptr),
 645       _stub_name(nullptr),
 646       _stub_id(StubId::NO_STUBID),
 647       _stub_entry_point(nullptr),
 648       _max_node_limit(MaxNodeLimit),
 649       _node_count_inlining_cutoff(NodeCountInliningCutoff),
 650       _post_loop_opts_phase(false),
 651       _merge_stores_phase(false),
 652       _allow_macro_nodes(true),
 653       _inlining_progress(false),
 654       _inlining_incrementally(false),

 655       _do_cleanup(false),
 656       _has_reserved_stack_access(target->has_reserved_stack_access()),

 657 #ifndef PRODUCT
 658       _igv_idx(0),
 659       _trace_opto_output(directive->TraceOptoOutputOption),
 660 #endif
 661       _clinit_barrier_on_entry(false),
 662       _stress_seed(0),
 663       _comp_arena(mtCompiler, Arena::Tag::tag_comp),
 664       _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 665       _env(ci_env),
 666       _directive(directive),
 667       _log(ci_env->log()),
 668       _first_failure_details(nullptr),
 669       _intrinsics(comp_arena(), 0, 0, nullptr),
 670       _macro_nodes(comp_arena(), 8, 0, nullptr),
 671       _parse_predicates(comp_arena(), 8, 0, nullptr),
 672       _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
 673       _expensive_nodes(comp_arena(), 8, 0, nullptr),
 674       _reachability_fences(comp_arena(), 8, 0, nullptr),
 675       _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),


 676       _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
 677       _unstable_if_traps(comp_arena(), 8, 0, nullptr),
 678       _coarsened_locks(comp_arena(), 8, 0, nullptr),
 679       _congraph(nullptr),
 680       NOT_PRODUCT(_igv_printer(nullptr) COMMA)
 681       _unique(0),
 682       _dead_node_count(0),
 683       _dead_node_list(comp_arena()),
 684       _node_arena_one(mtCompiler, Arena::Tag::tag_node),
 685       _node_arena_two(mtCompiler, Arena::Tag::tag_node),
 686       _node_arena(&_node_arena_one),
 687       _mach_constant_base_node(nullptr),
 688       _Compile_types(mtCompiler, Arena::Tag::tag_type),
 689       _initial_gvn(nullptr),
 690       _igvn_worklist(nullptr),
 691       _types(nullptr),
 692       _node_hash(nullptr),
 693       _late_inlines(comp_arena(), 2, 0, nullptr),
 694       _string_late_inlines(comp_arena(), 2, 0, nullptr),
 695       _boxing_late_inlines(comp_arena(), 2, 0, nullptr),

 764 #define MINIMUM_NODE_HASH  1023
 765 
 766   // GVN that will be run immediately on new nodes
 767   uint estimated_size = method()->code_size()*4+64;
 768   estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
 769   _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
 770   _types = new (comp_arena()) Type_Array(comp_arena());
 771   _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
 772   PhaseGVN gvn;
 773   set_initial_gvn(&gvn);
 774 
 775   { // Scope for timing the parser
 776     TracePhase tp(_t_parser);
 777 
 778     // Put top into the hash table ASAP.
 779     initial_gvn()->transform(top());
 780 
 781     // Set up tf(), start(), and find a CallGenerator.
 782     CallGenerator* cg = nullptr;
 783     if (is_osr_compilation()) {
 784       const TypeTuple *domain = StartOSRNode::osr_domain();
 785       const TypeTuple *range = TypeTuple::make_range(method()->signature());
 786       init_tf(TypeFunc::make(domain, range));
 787       StartNode* s = new StartOSRNode(root(), domain);
 788       initial_gvn()->set_type_bottom(s);
 789       verify_start(s);
 790       cg = CallGenerator::for_osr(method(), entry_bci());
 791     } else {
 792       // Normal case.
 793       init_tf(TypeFunc::make(method()));
 794       StartNode* s = new StartNode(root(), tf()->domain());
 795       initial_gvn()->set_type_bottom(s);
 796       verify_start(s);
 797       float past_uses = method()->interpreter_invocation_count();
 798       float expected_uses = past_uses;
 799       cg = CallGenerator::for_inline(method(), expected_uses);
 800     }
 801     if (failing())  return;
 802     if (cg == nullptr) {
 803       const char* reason = InlineTree::check_can_parse(method());
 804       assert(reason != nullptr, "expect reason for parse failure");
 805       stringStream ss;
 806       ss.print("cannot parse method: %s", reason);
 807       record_method_not_compilable(ss.as_string());
 808       return;
 809     }
 810 
 811     gvn.set_type(root(), root()->bottom_type());
 812 
 813     JVMState* jvms = build_start_state(start(), tf());
 814     if ((jvms = cg->generate(jvms)) == nullptr) {

 874   if (should_print_ideal()) {
 875     print_ideal_ir("PrintIdeal");
 876   }
 877 #endif
 878 
 879   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 880   bs->final_refinement(this);
 881 
 882 #ifdef ASSERT
 883   bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
 884 #endif
 885 
 886   // Dump compilation data to replay it.
 887   if (directive->DumpReplayOption) {
 888     env()->dump_replay_data(_compile_id);
 889   }
 890   if (directive->DumpInlineOption && (ilt() != nullptr)) {
 891     env()->dump_inline_data(_compile_id);
 892   }
 893 
 894   // Now that we know the size of all the monitors we can add a fixed slot
 895   // for the original deopt pc.
 896   int next_slot = fixed_slots() + (sizeof(address) / VMRegImpl::stack_slot_size);
























 897   set_fixed_slots(next_slot);
 898 
 899   // Compute when to use implicit null checks. Used by matching trap based
 900   // nodes and NullCheck optimization.
 901   set_allowed_deopt_reasons();
 902 
 903   // Now generate code
 904   Code_Gen();
 905 }
 906 
 907 // C2 uses runtime stubs serialized generation to initialize its static tables
 908 // shared by all compilations, like Type::_shared_type_dict.
 909 // At least one stub have to be completely generated to execute intialization
 910 // before we can skip the rest stubs generation by loading AOT cached stubs.
 911 
 912 static bool c2_do_stub_init_complete = false;
 913 
 914 //------------------------------Compile----------------------------------------
 915 // Compile a runtime stub
 916 Compile::Compile(ciEnv* ci_env,

 922                  bool pass_tls,
 923                  bool return_pc,
 924                  DirectiveSet* directive)
 925     : Phase(Compiler),
 926       _compile_id(0),
 927       _options(Options::for_runtime_stub()),
 928       _method(nullptr),
 929       _entry_bci(InvocationEntryBci),
 930       _stub_function(stub_function),
 931       _stub_name(stub_name),
 932       _stub_id(stub_id),
 933       _stub_entry_point(nullptr),
 934       _max_node_limit(MaxNodeLimit),
 935       _node_count_inlining_cutoff(NodeCountInliningCutoff),
 936       _post_loop_opts_phase(false),
 937       _merge_stores_phase(false),
 938       _allow_macro_nodes(true),
 939       _inlining_progress(false),
 940       _inlining_incrementally(false),
 941       _has_reserved_stack_access(false),

 942 #ifndef PRODUCT
 943       _igv_idx(0),
 944       _trace_opto_output(directive->TraceOptoOutputOption),
 945 #endif
 946       _clinit_barrier_on_entry(false),
 947       _stress_seed(0),
 948       _comp_arena(mtCompiler, Arena::Tag::tag_comp),
 949       _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 950       _env(ci_env),
 951       _directive(directive),
 952       _log(ci_env->log()),
 953       _first_failure_details(nullptr),
 954       _reachability_fences(comp_arena(), 8, 0, nullptr),
 955       _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
 956       _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
 957       _congraph(nullptr),
 958       NOT_PRODUCT(_igv_printer(nullptr) COMMA)
 959       _unique(0),
 960       _dead_node_count(0),
 961       _dead_node_list(comp_arena()),

1081   _fixed_slots = 0;
1082   set_has_split_ifs(false);
1083   set_has_loops(false); // first approximation
1084   set_has_stringbuilder(false);
1085   set_has_boxed_value(false);
1086   _trap_can_recompile = false;  // no traps emitted yet
1087   _major_progress = true; // start out assuming good things will happen
1088   set_has_unsafe_access(false);
1089   set_max_vector_size(0);
1090   set_clear_upper_avx(false);  //false as default for clear upper bits of ymm registers
1091   Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1092   set_decompile_count(0);
1093 
1094 #ifndef PRODUCT
1095   _phase_counter = 0;
1096   Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1097 #endif
1098 
1099   set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1100   _loop_opts_cnt = LoopOptsCount;





1101   set_do_inlining(Inline);
1102   set_max_inline_size(MaxInlineSize);
1103   set_freq_inline_size(FreqInlineSize);
1104   set_do_scheduling(OptoScheduling);
1105 
1106   set_do_vector_loop(false);
1107   set_has_monitors(false);
1108   set_has_scoped_access(false);
1109 
1110   if (AllowVectorizeOnDemand) {
1111     if (has_method() && _directive->VectorizeOption) {
1112       set_do_vector_loop(true);
1113       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());})
1114     } else if (has_method() && method()->name() != nullptr &&
1115                method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1116       set_do_vector_loop(true);
1117     }
1118   }
1119   set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1120   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());})

1352   // If this method has already thrown a range-check,
1353   // assume it was because we already tried range smearing
1354   // and it failed.
1355   uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1356   return !already_trapped;
1357 }
1358 
1359 
1360 //------------------------------flatten_alias_type-----------------------------
1361 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1362   assert(do_aliasing(), "Aliasing should be enabled");
1363   int offset = tj->offset();
1364   TypePtr::PTR ptr = tj->ptr();
1365 
1366   // Known instance (scalarizable allocation) alias only with itself.
1367   bool is_known_inst = tj->isa_oopptr() != nullptr &&
1368                        tj->is_oopptr()->is_known_instance();
1369 
1370   // Process weird unsafe references.
1371   if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1372     assert(InlineUnsafeOps || StressReflectiveCode, "indeterminate pointers come only from unsafe ops");
1373     assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1374     tj = TypeOopPtr::BOTTOM;
1375     ptr = tj->ptr();
1376     offset = tj->offset();
1377   }
1378 
1379   // Array pointers need some flattening
1380   const TypeAryPtr* ta = tj->isa_aryptr();
1381   if (ta && ta->is_stable()) {
1382     // Erase stability property for alias analysis.
1383     tj = ta = ta->cast_to_stable(false);
1384   }
1385   if( ta && is_known_inst ) {
1386     if ( offset != Type::OffsetBot &&
1387          offset > arrayOopDesc::length_offset_in_bytes() ) {
1388       offset = Type::OffsetBot; // Flatten constant access into array body only
1389       tj = ta = ta->
1390               remove_speculative()->
1391               cast_to_ptr_type(ptr)->
1392               with_offset(offset);
1393     }
1394   } else if (ta != nullptr) {
1395     // Common slices
1396     if (offset == arrayOopDesc::length_offset_in_bytes()) {
1397       return TypeAryPtr::RANGE;
1398     } else if (offset == oopDesc::klass_offset_in_bytes()) {
1399       return TypeInstPtr::KLASS;
1400     } else if (offset == oopDesc::mark_offset_in_bytes()) {
1401       return TypeInstPtr::MARK;
1402     }
1403 
1404     // Remove size and stability
1405     const TypeAry* normalized_ary = TypeAry::make(ta->elem(), TypeInt::POS, false);
1406     // Remove ptr, const_oop, and offset
1407     if (ta->elem() == Type::BOTTOM) {
1408       // Bottom array (meet of int[] and byte[] for example), accesses to it will be done with
1409       // Unsafe. This should alias with all arrays. For now just leave it as it is (this is
1410       // incorrect, see JDK-8331133).
1411       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, false, Type::OffsetBot);
1412     } else if (ta->elem()->make_oopptr() != nullptr) {
1413       // Object arrays, all of them share the same slice
1414       const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, TypeInt::POS, false);
1415       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, tary, nullptr, false, Type::OffsetBot);
1416     } else {
1417       // Primitive arrays
1418       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, ta->exact_klass(), true, Type::OffsetBot);
1419     }
1420 
1421     // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1422     // cannot be distinguished by bytecode alone.
1423     if (ta->elem() == TypeInt::BOOL) {
1424       tj = ta = TypeAryPtr::BYTES;
1425     }
















1426   }
1427 
1428   // Oop pointers need some flattening
1429   const TypeInstPtr *to = tj->isa_instptr();
1430   if (to && to != TypeOopPtr::BOTTOM) {
1431     ciInstanceKlass* ik = to->instance_klass();

1432     if( ptr == TypePtr::Constant ) {
1433       if (ik != ciEnv::current()->Class_klass() ||
1434           offset < ik->layout_helper_size_in_bytes()) {
1435         // No constant oop pointers (such as Strings); they alias with
1436         // unknown strings.
1437         assert(!is_known_inst, "not scalarizable allocation");
1438         tj = to = to->
1439                 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1440                 remove_speculative()->
1441                 cast_to_ptr_type(TypePtr::BotPTR)->
1442                 cast_to_exactness(false);
1443       }
1444     } else if( is_known_inst ) {
1445       tj = to; // Keep NotNull and klass_is_exact for instance type
1446     } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1447       // During the 2nd round of IterGVN, NotNull castings are removed.
1448       // Make sure the Bottom and NotNull variants alias the same.
1449       // Also, make sure exact and non-exact variants alias the same.
1450       tj = to = to->
1451               remove_speculative()->
1452               cast_to_instance_id(TypeOopPtr::InstanceBot)->
1453               cast_to_ptr_type(TypePtr::BotPTR)->
1454               cast_to_exactness(false);
1455     }
1456     if (to->speculative() != nullptr) {
1457       tj = to = to->remove_speculative();
1458     }
1459     // Canonicalize the holder of this field
1460     if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1461       // First handle header references such as a LoadKlassNode, even if the
1462       // object's klass is unloaded at compile time (4965979).
1463       if (!is_known_inst) { // Do it only for non-instance types
1464         tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, offset);
1465       }
1466     } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1467       // Static fields are in the space above the normal instance
1468       // fields in the java.lang.Class instance.
1469       if (ik != ciEnv::current()->Class_klass()) {
1470         to = nullptr;
1471         tj = TypeOopPtr::BOTTOM;
1472         offset = tj->offset();
1473       }
1474     } else {
1475       ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1476       assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1477       assert(tj->offset() == offset, "no change to offset expected");
1478       bool xk = to->klass_is_exact();
1479       int instance_id = to->instance_id();
1480 
1481       // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1482       // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1483       // its interfaces are included.
1484       if (xk && ik->equals(canonical_holder)) {
1485         assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id), "exact type should be canonical type");

1486       } else {
1487         assert(xk || !is_known_inst, "Known instance should be exact type");
1488         tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, offset, instance_id);

1489       }
1490     }
1491   }
1492 
1493   // Klass pointers to object array klasses need some flattening
1494   const TypeKlassPtr *tk = tj->isa_klassptr();
1495   if( tk ) {
1496     // If we are referencing a field within a Klass, we need
1497     // to assume the worst case of an Object.  Both exact and
1498     // inexact types must flatten to the same alias class so
1499     // use NotNull as the PTR.
1500     if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1501       tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1502                                        env()->Object_klass(),
1503                                        offset);

1504     }
1505 
1506     if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1507       ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1508       if (!k || !k->is_loaded()) {                  // Only fails for some -Xcomp runs
1509         tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), offset);
1510       } else {
1511         tj = tk = TypeAryKlassPtr::make(TypePtr::NotNull, tk->is_aryklassptr()->elem(), k, offset);
1512       }
1513     }
1514 
1515     // Check for precise loads from the primary supertype array and force them
1516     // to the supertype cache alias index.  Check for generic array loads from
1517     // the primary supertype array and also force them to the supertype cache
1518     // alias index.  Since the same load can reach both, we need to merge
1519     // these 2 disparate memories into the same alias class.  Since the
1520     // primary supertype array is read-only, there's no chance of confusion
1521     // where we bypass an array load and an array store.
1522     int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1523     if (offset == Type::OffsetBot ||
1524         (offset >= primary_supers_offset &&
1525          offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1526         offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1527       offset = in_bytes(Klass::secondary_super_cache_offset());
1528       tj = tk = tk->with_offset(offset);
1529     }
1530   }
1531 
1532   // Flatten all Raw pointers together.
1533   if (tj->base() == Type::RawPtr)
1534     tj = TypeRawPtr::BOTTOM;

1624   intptr_t key = (intptr_t) adr_type;
1625   key ^= key >> logAliasCacheSize;
1626   return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1627 }
1628 
1629 
1630 //-----------------------------grow_alias_types--------------------------------
1631 void Compile::grow_alias_types() {
1632   const int old_ats  = _max_alias_types; // how many before?
1633   const int new_ats  = old_ats;          // how many more?
1634   const int grow_ats = old_ats+new_ats;  // how many now?
1635   _max_alias_types = grow_ats;
1636   _alias_types =  REALLOC_ARENA_ARRAY(comp_arena(), _alias_types, old_ats, grow_ats);
1637   AliasType* ats =    NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1638   Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1639   for (int i = 0; i < new_ats; i++)  _alias_types[old_ats+i] = &ats[i];
1640 }
1641 
1642 
1643 //--------------------------------find_alias_type------------------------------
1644 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1645   if (!do_aliasing()) {
1646     return alias_type(AliasIdxBot);
1647   }
1648 
1649   AliasCacheEntry* ace = probe_alias_cache(adr_type);
1650   if (ace->_adr_type == adr_type) {
1651     return alias_type(ace->_index);



1652   }
1653 
1654   // Handle special cases.
1655   if (adr_type == nullptr)          return alias_type(AliasIdxTop);
1656   if (adr_type == TypePtr::BOTTOM)  return alias_type(AliasIdxBot);
1657 
1658   // Do it the slow way.
1659   const TypePtr* flat = flatten_alias_type(adr_type);
1660 
1661 #ifdef ASSERT
1662   {
1663     ResourceMark rm;
1664     assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1665            Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1666     assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1667            Type::str(adr_type));
1668     if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1669       const TypeOopPtr* foop = flat->is_oopptr();
1670       // Scalarizable allocations have exact klass always.
1671       bool exact = !foop->klass_is_exact() || foop->is_known_instance();

1681     if (alias_type(i)->adr_type() == flat) {
1682       idx = i;
1683       break;
1684     }
1685   }
1686 
1687   if (idx == AliasIdxTop) {
1688     if (no_create)  return nullptr;
1689     // Grow the array if necessary.
1690     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1691     // Add a new alias type.
1692     idx = _num_alias_types++;
1693     _alias_types[idx]->Init(idx, flat);
1694     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1695     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1696     if (flat->isa_instptr()) {
1697       if (flat->offset() == java_lang_Class::klass_offset()
1698           && flat->is_instptr()->instance_klass() == env()->Class_klass())
1699         alias_type(idx)->set_rewritable(false);
1700     }

1701     if (flat->isa_aryptr()) {
1702 #ifdef ASSERT
1703       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1704       // (T_BYTE has the weakest alignment and size restrictions...)
1705       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1706 #endif

1707       if (flat->offset() == TypePtr::OffsetBot) {
1708         alias_type(idx)->set_element(flat->is_aryptr()->elem());







1709       }
1710     }
1711     if (flat->isa_klassptr()) {
1712       if (UseCompactObjectHeaders) {
1713         if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1714           alias_type(idx)->set_rewritable(false);
1715       }
1716       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1717         alias_type(idx)->set_rewritable(false);
1718       if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1719         alias_type(idx)->set_rewritable(false);
1720       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1721         alias_type(idx)->set_rewritable(false);


1722       if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1723         alias_type(idx)->set_rewritable(false);
1724     }
1725 
1726     if (flat->isa_instklassptr()) {
1727       if (flat->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
1728         alias_type(idx)->set_rewritable(false);
1729       }
1730     }
1731     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1732     // but the base pointer type is not distinctive enough to identify
1733     // references into JavaThread.)
1734 
1735     // Check for final fields.
1736     const TypeInstPtr* tinst = flat->isa_instptr();
1737     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1738       ciField* field;
1739       if (tinst->const_oop() != nullptr &&
1740           tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1741           tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1742         // static field
1743         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1744         field = k->get_field_by_offset(tinst->offset(), true);




1745       } else {
1746         ciInstanceKlass *k = tinst->instance_klass();
1747         field = k->get_field_by_offset(tinst->offset(), false);
1748       }
1749       assert(field == nullptr ||
1750              original_field == nullptr ||
1751              (field->holder() == original_field->holder() &&
1752               field->offset_in_bytes() == original_field->offset_in_bytes() &&
1753               field->is_static() == original_field->is_static()), "wrong field?");
1754       // Set field() and is_rewritable() attributes.
1755       if (field != nullptr)  alias_type(idx)->set_field(field);







1756     }
1757   }
1758 
1759   // Fill the cache for next time.
1760   ace->_adr_type = adr_type;
1761   ace->_index    = idx;
1762   assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");

1763 
1764   // Might as well try to fill the cache for the flattened version, too.
1765   AliasCacheEntry* face = probe_alias_cache(flat);
1766   if (face->_adr_type == nullptr) {
1767     face->_adr_type = flat;
1768     face->_index    = idx;
1769     assert(alias_type(flat) == alias_type(idx), "flat type must work too");

1770   }
1771 
1772   return alias_type(idx);
1773 }
1774 
1775 
1776 Compile::AliasType* Compile::alias_type(ciField* field) {
1777   const TypeOopPtr* t;
1778   if (field->is_static())
1779     t = TypeInstPtr::make(field->holder()->java_mirror());
1780   else
1781     t = TypeOopPtr::make_from_klass_raw(field->holder());
1782   AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1783   assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1784   return atp;
1785 }
1786 
1787 
1788 //------------------------------have_alias_type--------------------------------
1789 bool Compile::have_alias_type(const TypePtr* adr_type) {

1871   assert(!C->major_progress(), "not cleared");
1872 
1873   if (_for_post_loop_igvn.length() > 0) {
1874     while (_for_post_loop_igvn.length() > 0) {
1875       Node* n = _for_post_loop_igvn.pop();
1876       n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1877       igvn._worklist.push(n);
1878     }
1879     igvn.optimize();
1880     if (failing()) return;
1881     assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1882     assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1883 
1884     // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1885     if (C->major_progress()) {
1886       C->clear_major_progress(); // ensure that major progress is now clear
1887     }
1888   }
1889 }
1890 













































































































































































































































































































































































































































































































































































































1891 void Compile::record_for_merge_stores_igvn(Node* n) {
1892   if (!n->for_merge_stores_igvn()) {
1893     assert(!_for_merge_stores_igvn.contains(n), "duplicate");
1894     n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1895     _for_merge_stores_igvn.append(n);
1896   }
1897 }
1898 
1899 void Compile::remove_from_merge_stores_igvn(Node* n) {
1900   n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
1901   _for_merge_stores_igvn.remove(n);
1902 }
1903 
1904 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
1905 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
1906 // the stores, and we merge the wrong sequence of stores.
1907 // Example:
1908 //   StoreI RangeCheck StoreI StoreI RangeCheck StoreI
1909 // Apply MergeStores:
1910 //   StoreI RangeCheck [   StoreL  ] RangeCheck StoreI

1989       assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
1990       Bytecodes::Code c = iter.cur_bc();
1991       Node* lhs = nullptr;
1992       Node* rhs = nullptr;
1993       if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
1994         lhs = unc->peek_operand(0);
1995         rhs = unc->peek_operand(1);
1996       } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
1997         lhs = unc->peek_operand(0);
1998       }
1999 
2000       ResourceMark rm;
2001       const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2002       assert(live_locals.is_valid(), "broken liveness info");
2003       int len = (int)live_locals.size();
2004 
2005       for (int i = 0; i < len; i++) {
2006         Node* local = unc->local(jvms, i);
2007         // kill local using the liveness of next_bci.
2008         // give up when the local looks like an operand to secure reexecution.
2009         if (!live_locals.at(i) && !local->is_top() && local != lhs && local!= rhs) {
2010           uint idx = jvms->locoff() + i;
2011 #ifdef ASSERT
2012           if (PrintOpto && Verbose) {
2013             tty->print("[unstable_if] kill local#%d: ", idx);
2014             local->dump();
2015             tty->cr();
2016           }
2017 #endif
2018           igvn.replace_input_of(unc, idx, top());
2019           modified = true;
2020         }
2021       }
2022     }
2023 
2024     // keep the mondified trap for late query
2025     if (modified) {
2026       trap->set_modified();
2027     } else {
2028       _unstable_if_traps.delete_at(i);
2029     }
2030   }
2031   igvn.optimize();
2032 }
2033 
2034 // StringOpts and late inlining of string methods
2035 void Compile::inline_string_calls(bool parse_time) {
2036   {
2037     // remove useless nodes to make the usage analysis simpler
2038     ResourceMark rm;
2039     PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2040   }
2041 
2042   {
2043     ResourceMark rm;
2044     print_method(PHASE_BEFORE_STRINGOPTS, 3);

2236 
2237   if (_string_late_inlines.length() > 0) {
2238     assert(has_stringbuilder(), "inconsistent");
2239 
2240     inline_string_calls(false);
2241 
2242     if (failing())  return;
2243 
2244     inline_incrementally_cleanup(igvn);
2245   }
2246 
2247   set_inlining_incrementally(false);
2248 }
2249 
2250 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2251   // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2252   // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2253   // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2254   // as if "inlining_incrementally() == true" were set.
2255   assert(inlining_incrementally() == false, "not allowed");
2256   assert(_modified_nodes == nullptr, "not allowed");




2257   assert(_late_inlines.length() > 0, "sanity");
2258 
2259   if (StressIncrementalInlining) {
2260     shuffle_late_inlines();
2261   }
2262 
2263   while (_late_inlines.length() > 0) {
2264     igvn_worklist()->ensure_empty(); // should be done with igvn
2265 
2266     while (inline_incrementally_one()) {
2267       assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2268     }
2269     if (failing())  return;
2270 
2271     inline_incrementally_cleanup(igvn);
2272   }


2273 }
2274 
2275 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2276   if (_loop_opts_cnt > 0) {
2277     while (major_progress() && (_loop_opts_cnt > 0)) {
2278       TracePhase tp(_t_idealLoop);
2279       PhaseIdealLoop::optimize(igvn, mode);
2280       _loop_opts_cnt--;
2281       if (failing())  return false;
2282       if (major_progress()) {
2283         print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2284       }
2285     }
2286   }
2287   return true;
2288 }
2289 
2290 // Remove edges from "root" to each SafePoint at a backward branch.
2291 // They were inserted during parsing (see add_safepoint()) to make
2292 // infinite loops without calls or exceptions visible to root, i.e.,

2398     print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
2399   }
2400   assert(!has_vbox_nodes(), "sanity");
2401 
2402   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2403     Compile::TracePhase tp(_t_renumberLive);
2404     igvn_worklist()->ensure_empty(); // should be done with igvn
2405     {
2406       ResourceMark rm;
2407       PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
2408     }
2409     igvn.reset();
2410     igvn.optimize(true);
2411     if (failing()) return;
2412   }
2413 
2414   // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
2415   // safepoints
2416   remove_root_to_sfpts_edges(igvn);
2417 





2418   if (failing())  return;
2419 











2420   _print_phase_loop_opts = has_loops();
2421   if (_print_phase_loop_opts) {
2422     print_method(PHASE_BEFORE_LOOP_OPTS, 2);
2423   }
2424 
2425   // Perform escape analysis
2426   if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
2427     if (has_loops()) {
2428       // Cleanup graph (remove dead nodes).
2429       TracePhase tp(_t_idealLoop);
2430       PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
2431       if (failing())  return;













2432     }

2433     bool progress;
2434     print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2435     do {
2436       ConnectionGraph::do_analysis(this, &igvn);
2437 
2438       if (failing())  return;
2439 
2440       int mcount = macro_count(); // Record number of allocations and locks before IGVN
2441 
2442       // Optimize out fields loads from scalar replaceable allocations.
2443       igvn.optimize(true);
2444       print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2445 
2446       if (failing()) return;
2447 
2448       if (congraph() != nullptr && macro_count() > 0) {
2449         TracePhase tp(_t_macroEliminate);
2450         PhaseMacroExpand mexp(igvn);
2451         mexp.eliminate_macro_nodes();
2452         if (failing()) return;


2453         print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2454 
2455         igvn.set_delay_transform(false);
2456         igvn.optimize();
2457         if (failing()) return;
2458 
2459         print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
2460       }
2461 
2462       ConnectionGraph::verify_ram_nodes(this, root());
2463       if (failing())  return;
2464 
2465       progress = do_iterative_escape_analysis() &&
2466                  (macro_count() < mcount) &&
2467                  ConnectionGraph::has_candidates(this);
2468       // Try again if candidates exist and made progress
2469       // by removing some allocations and/or locks.
2470     } while (progress);
2471   }
2472 





2473   // Loop transforms on the ideal graph.  Range Check Elimination,
2474   // peeling, unrolling, etc.
2475 
2476   // Set loop opts counter
2477   if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
2478     {
2479       TracePhase tp(_t_idealLoop);
2480       PhaseIdealLoop::optimize(igvn, LoopOptsDefault);
2481       _loop_opts_cnt--;
2482       if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
2483       if (failing())  return;
2484     }
2485     // Loop opts pass if partial peeling occurred in previous pass
2486     if(PartialPeelLoop && major_progress() && (_loop_opts_cnt > 0)) {
2487       TracePhase tp(_t_idealLoop);
2488       PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);
2489       _loop_opts_cnt--;
2490       if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
2491       if (failing())  return;
2492     }

2540 
2541   // Once loop optimizations are over, it is safe to get rid of all reachability fence nodes and
2542   // migrate reachability edges to safepoints.
2543   if (OptimizeReachabilityFences && _reachability_fences.length() > 0) {
2544     TracePhase tp1(_t_idealLoop);
2545     TracePhase tp2(_t_reachability);
2546     PhaseIdealLoop::optimize(igvn, PostLoopOptsExpandReachabilityFences);
2547     print_method(PHASE_EXPAND_REACHABILITY_FENCES, 2);
2548     if (failing())  return;
2549     assert(_reachability_fences.length() == 0 || PreserveReachabilityFencesOnConstants, "no RF nodes allowed");
2550   }
2551 
2552   process_for_merge_stores_igvn(igvn);
2553 
2554   if (failing())  return;
2555 
2556 #ifdef ASSERT
2557   bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
2558 #endif
2559 














2560   {
2561     TracePhase tp(_t_macroExpand);







2562     print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);
2563     PhaseMacroExpand  mex(igvn);
2564     // Do not allow new macro nodes once we start to eliminate and expand
2565     C->reset_allow_macro_nodes();
2566     // Last attempt to eliminate macro nodes before expand
2567     mex.eliminate_macro_nodes();
2568     if (failing()) {
2569       return;
2570     }
2571     mex.eliminate_opaque_looplimit_macro_nodes();
2572     if (failing()) {
2573       return;
2574     }
2575     print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
2576     if (mex.expand_macro_nodes()) {
2577       assert(failing(), "must bail out w/ explicit message");
2578       return;
2579     }
2580     print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
2581   }
2582 




2583   {
2584     TracePhase tp(_t_barrierExpand);
2585     if (bs->expand_barriers(this, igvn)) {
2586       assert(failing(), "must bail out w/ explicit message");
2587       return;
2588     }
2589     print_method(PHASE_BARRIER_EXPANSION, 2);
2590   }
2591 
2592   if (C->max_vector_size() > 0) {
2593     C->optimize_logic_cones(igvn);
2594     igvn.optimize();
2595     if (failing()) return;
2596   }
2597 
2598   DEBUG_ONLY( _modified_nodes = nullptr; )

2599 
2600   assert(igvn._worklist.size() == 0, "not empty");
2601 
2602   if (_late_inlines.length() > 0) {
2603     // More opportunities to optimize virtual and MH calls.
2604     // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
2605     process_late_inline_calls_no_inline(igvn);
2606     if (failing())  return;
2607   }
2608   assert(_late_inlines.length() == 0, "late inline queue must be drained");
2609  } // (End scope of igvn; run destructor if necessary for asserts.)
2610 
2611  check_no_dead_use();
2612 
2613  // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
2614  // to remove hashes to unlock nodes for modifications.
2615  C->node_hash()->clear();
2616 
2617  // A method with only infinite loops has no edges entering loops from root
2618  {
2619    TracePhase tp(_t_graphReshaping);
2620    if (final_graph_reshaping()) {
2621      assert(failing(), "must bail out w/ explicit message");
2622      return;
2623    }
2624  }
2625 
2626  print_method(PHASE_OPTIMIZE_FINISHED, 2);
2627  DEBUG_ONLY(set_phase_optimize_finished();)
2628 }

3287     n->subsume_by(sub, this);
3288   }
3289 }
3290 
3291 void Compile::final_graph_reshaping_main_switch(Node* n, Final_Reshape_Counts& frc, uint nop, Unique_Node_List& dead_nodes) {
3292   switch( nop ) {
3293   case Op_Opaque1:              // Remove Opaque Nodes before matching
3294     n->subsume_by(n->in(1), this);
3295     break;
3296   case Op_CallLeafPure: {
3297     // If the pure call is not supported, then lower to a CallLeaf.
3298     if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
3299       CallNode* call = n->as_Call();
3300       CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
3301                                             call->_name, TypeRawPtr::BOTTOM);
3302       new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
3303       new_call->init_req(TypeFunc::I_O, C->top());
3304       new_call->init_req(TypeFunc::Memory, C->top());
3305       new_call->init_req(TypeFunc::ReturnAdr, C->top());
3306       new_call->init_req(TypeFunc::FramePtr, C->top());
3307       for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain()->cnt(); i++) {
3308         new_call->init_req(i, call->in(i));
3309       }
3310       n->subsume_by(new_call, this);
3311     }
3312     break;
3313   }
3314   case Op_CallStaticJava:
3315   case Op_CallJava:
3316   case Op_CallDynamicJava:
3317     frc.inc_java_call_count(); // Count java call site;
3318   case Op_CallRuntime:
3319   case Op_CallLeaf:
3320   case Op_CallLeafVector:
3321   case Op_CallLeafNoFP: {
3322     assert (n->is_Call(), "");
3323     CallNode *call = n->as_Call();
3324     // See if uncommon argument is shared
3325     if (call->is_CallStaticJava() && call->as_CallStaticJava()->_name) {
3326       Node *n = call->in(TypeFunc::Parms);
3327       int nop = n->Opcode();

3334           nop != Op_DecodeNKlass &&
3335           !n->is_Mem() &&
3336           !n->is_Phi()) {
3337         Node *x = n->clone();
3338         call->set_req(TypeFunc::Parms, x);
3339       }
3340     }
3341     break;
3342   }
3343 
3344   // Mem nodes need explicit cases to satisfy assert(!n->is_Mem()) in default.
3345   case Op_StoreF:
3346   case Op_LoadF:
3347   case Op_StoreD:
3348   case Op_LoadD:
3349   case Op_LoadD_unaligned:
3350   case Op_StoreB:
3351   case Op_StoreC:
3352   case Op_StoreI:
3353   case Op_StoreL:

3354   case Op_CompareAndSwapB:
3355   case Op_CompareAndSwapS:
3356   case Op_CompareAndSwapI:
3357   case Op_CompareAndSwapL:
3358   case Op_CompareAndSwapP:
3359   case Op_CompareAndSwapN:
3360   case Op_WeakCompareAndSwapB:
3361   case Op_WeakCompareAndSwapS:
3362   case Op_WeakCompareAndSwapI:
3363   case Op_WeakCompareAndSwapL:
3364   case Op_WeakCompareAndSwapP:
3365   case Op_WeakCompareAndSwapN:
3366   case Op_CompareAndExchangeB:
3367   case Op_CompareAndExchangeS:
3368   case Op_CompareAndExchangeI:
3369   case Op_CompareAndExchangeL:
3370   case Op_CompareAndExchangeP:
3371   case Op_CompareAndExchangeN:
3372   case Op_GetAndAddS:
3373   case Op_GetAndAddB:

3865           k->subsume_by(m, this);
3866         }
3867       }
3868     }
3869     break;
3870   }
3871   case Op_CmpUL: {
3872     if (!Matcher::has_match_rule(Op_CmpUL)) {
3873       // No support for unsigned long comparisons
3874       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3875       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3876       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3877       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3878       Node* andl = new AndLNode(orl, remove_sign_mask);
3879       Node* cmp = new CmpLNode(andl, n->in(2));
3880       n->subsume_by(cmp, this);
3881     }
3882     break;
3883   }
3884 #ifdef ASSERT





3885   case Op_ConNKlass: {
3886     const TypePtr* tp = n->as_Type()->type()->make_ptr();
3887     ciKlass* klass = tp->is_klassptr()->exact_klass();
3888     assert(klass->is_in_encoding_range(), "klass cannot be compressed");
3889     break;
3890   }
3891 #endif
3892   default:
3893     assert(!n->is_Call(), "");
3894     assert(!n->is_Mem(), "");
3895     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3896     break;
3897   }
3898 }
3899 
3900 //------------------------------final_graph_reshaping_walk---------------------
3901 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3902 // requires that the walk visits a node's inputs before visiting the node.
3903 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
3904   Unique_Node_List sfpt;

4237   }
4238 }
4239 
4240 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4241   return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4242 }
4243 
4244 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4245   return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4246 }
4247 
4248 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4249   if (holder->is_initialized()) {
4250     return false;
4251   }
4252   if (holder->is_being_initialized()) {
4253     if (accessing_method->holder() == holder) {
4254       // Access inside a class. The barrier can be elided when access happens in <clinit>,
4255       // <init>, or a static method. In all those cases, there was an initialization
4256       // barrier on the holder klass passed.
4257       if (accessing_method->is_static_initializer() ||
4258           accessing_method->is_object_initializer() ||
4259           accessing_method->is_static()) {
4260         return false;
4261       }
4262     } else if (accessing_method->holder()->is_subclass_of(holder)) {
4263       // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4264       // In case of <init> or a static method, the barrier is on the subclass is not enough:
4265       // child class can become fully initialized while its parent class is still being initialized.
4266       if (accessing_method->is_static_initializer()) {
4267         return false;
4268       }
4269     }
4270     ciMethod* root = method(); // the root method of compilation
4271     if (root != accessing_method) {
4272       return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4273     }
4274   }
4275   return true;
4276 }
4277 
4278 #ifndef PRODUCT
4279 //------------------------------verify_bidirectional_edges---------------------
4280 // For each input edge to a node (ie - for each Use-Def edge), verify that
4281 // there is a corresponding Def-Use edge.
4282 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
4283   // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
4284   uint stack_size = live_nodes() >> 4;
4285   Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
4286   if (root_and_safepoints != nullptr) {

4316       if (in != nullptr && !in->is_top()) {
4317         // Count instances of `next`
4318         int cnt = 0;
4319         for (uint idx = 0; idx < in->_outcnt; idx++) {
4320           if (in->_out[idx] == n) {
4321             cnt++;
4322           }
4323         }
4324         assert(cnt > 0, "Failed to find Def-Use edge.");
4325         // Check for duplicate edges
4326         // walk the input array downcounting the input edges to n
4327         for (uint j = 0; j < length; j++) {
4328           if (n->in(j) == in) {
4329             cnt--;
4330           }
4331         }
4332         assert(cnt == 0, "Mismatched edge count.");
4333       } else if (in == nullptr) {
4334         assert(i == 0 || i >= n->req() ||
4335                n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||

4336                (n->is_Unlock() && i == (n->req() - 1)) ||
4337                (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
4338               "only region, phi, arraycopy, unlock or membar nodes have null data edges");
4339       } else {
4340         assert(in->is_top(), "sanity");
4341         // Nothing to check.
4342       }
4343     }
4344   }
4345 }
4346 
4347 //------------------------------verify_graph_edges---------------------------
4348 // Walk the Graph and verify that there is a one-to-one correspondence
4349 // between Use-Def edges and Def-Use edges in the graph.
4350 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
4351   if (VerifyGraphEdges) {
4352     Unique_Node_List visited;
4353 
4354     // Call graph walk to check edges
4355     verify_bidirectional_edges(visited, root_and_safepoints);
4356     if (no_dead_code) {
4357       // Now make sure that no visited node is used by an unvisited node.
4358       bool dead_nodes = false;

4469 // (1) subklass is already limited to a subtype of superklass => always ok
4470 // (2) subklass does not overlap with superklass => always fail
4471 // (3) superklass has NO subtypes and we can check with a simple compare.
4472 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
4473   if (skip) {
4474     return SSC_full_test;       // Let caller generate the general case.
4475   }
4476 
4477   if (subk->is_java_subtype_of(superk)) {
4478     return SSC_always_true; // (0) and (1)  this test cannot fail
4479   }
4480 
4481   if (!subk->maybe_java_subtype_of(superk)) {
4482     return SSC_always_false; // (2) true path dead; no dynamic test needed
4483   }
4484 
4485   const Type* superelem = superk;
4486   if (superk->isa_aryklassptr()) {
4487     int ignored;
4488     superelem = superk->is_aryklassptr()->base_element_type(ignored);







4489   }
4490 
4491   if (superelem->isa_instklassptr()) {
4492     ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
4493     if (!ik->has_subklass()) {
4494       if (!ik->is_final()) {
4495         // Add a dependency if there is a chance of a later subclass.
4496         dependencies()->assert_leaf_type(ik);
4497       }
4498       if (!superk->maybe_java_subtype_of(subk)) {
4499         return SSC_always_false;
4500       }
4501       return SSC_easy_test;     // (3) caller can do a simple ptr comparison
4502     }
4503   } else {
4504     // A primitive array type has no subtypes.
4505     return SSC_easy_test;       // (3) caller can do a simple ptr comparison
4506   }
4507 
4508   return SSC_full_test;

5301   } else {
5302     _debug_network_printer->update_compiled_method(C->method());
5303   }
5304   tty->print_cr("Method printed over network stream to IGV");
5305   _debug_network_printer->print(name, C->root(), visible_nodes, fr);
5306 }
5307 #endif // !PRODUCT
5308 
5309 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
5310   if (type != nullptr && phase->type(value)->higher_equal(type)) {
5311     return value;
5312   }
5313   Node* result = nullptr;
5314   if (bt == T_BYTE) {
5315     result = phase->transform(new LShiftINode(value, phase->intcon(24)));
5316     result = new RShiftINode(result, phase->intcon(24));
5317   } else if (bt == T_BOOLEAN) {
5318     result = new AndINode(value, phase->intcon(0xFF));
5319   } else if (bt == T_CHAR) {
5320     result = new AndINode(value,phase->intcon(0xFFFF));


5321   } else {
5322     assert(bt == T_SHORT, "unexpected narrow type");
5323     result = phase->transform(new LShiftINode(value, phase->intcon(16)));
5324     result = new RShiftINode(result, phase->intcon(16));
5325   }
5326   if (transform_res) {
5327     result = phase->transform(result);
5328   }
5329   return result;
5330 }
5331 
5332 void Compile::record_method_not_compilable_oom() {
5333   record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
5334 }
5335 
5336 #ifndef PRODUCT
5337 // Collects all the control inputs from nodes on the worklist and from their data dependencies
5338 static void find_candidate_control_inputs(Unique_Node_List& worklist, Unique_Node_List& candidates) {
5339   // Follow non-control edges until we reach CFG nodes
5340   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/reachability.hpp"
  83 #include "opto/rootnode.hpp"
  84 #include "opto/runtime.hpp"
  85 #include "opto/stringopts.hpp"
  86 #include "opto/type.hpp"
  87 #include "opto/vector.hpp"
  88 #include "opto/vectornode.hpp"
  89 #include "runtime/arguments.hpp"
  90 #include "runtime/globals_extension.hpp"
  91 #include "runtime/sharedRuntime.hpp"
  92 #include "runtime/signature.hpp"
  93 #include "runtime/stubRoutines.hpp"
  94 #include "runtime/timer.hpp"
  95 #include "utilities/align.hpp"
  96 #include "utilities/copy.hpp"
  97 #include "utilities/hashTable.hpp"
  98 #include "utilities/macros.hpp"
  99 
 100 // -------------------- Compile::mach_constant_base_node -----------------------
 101 // Constant table base node singleton.
 102 MachConstantBaseNode* Compile::mach_constant_base_node() {
 103   if (_mach_constant_base_node == nullptr) {
 104     _mach_constant_base_node = new MachConstantBaseNode();
 105     _mach_constant_base_node->add_req(C->root());
 106   }
 107   return _mach_constant_base_node;
 108 }
 109 

 398     record_dead_node(dead->_idx);
 399   }
 400   if (dead->is_macro()) {
 401     remove_macro_node(dead);
 402   }
 403   if (dead->is_expensive()) {
 404     remove_expensive_node(dead);
 405   }
 406   if (dead->is_ReachabilityFence()) {
 407     remove_reachability_fence(dead->as_ReachabilityFence());
 408   }
 409   if (dead->is_OpaqueTemplateAssertionPredicate()) {
 410     remove_template_assertion_predicate_opaque(dead->as_OpaqueTemplateAssertionPredicate());
 411   }
 412   if (dead->is_ParsePredicate()) {
 413     remove_parse_predicate(dead->as_ParsePredicate());
 414   }
 415   if (dead->for_post_loop_opts_igvn()) {
 416     remove_from_post_loop_opts_igvn(dead);
 417   }
 418   if (dead->is_InlineType()) {
 419     remove_inline_type(dead);
 420   }
 421   if (dead->is_LoadFlat() || dead->is_StoreFlat()) {
 422     remove_flat_access(dead);
 423   }
 424   if (dead->for_merge_stores_igvn()) {
 425     remove_from_merge_stores_igvn(dead);
 426   }
 427   if (dead->is_Call()) {
 428     remove_useless_late_inlines(                &_late_inlines, dead);
 429     remove_useless_late_inlines(         &_string_late_inlines, dead);
 430     remove_useless_late_inlines(         &_boxing_late_inlines, dead);
 431     remove_useless_late_inlines(&_vector_reboxing_late_inlines, dead);
 432 
 433     if (dead->is_CallStaticJava()) {
 434       remove_unstable_if_trap(dead->as_CallStaticJava(), false);
 435     }
 436   }
 437   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 438   bs->unregister_potential_barrier_node(dead);
 439 }
 440 
 441 // Disconnect all useless nodes by disconnecting those at the boundary.
 442 void Compile::disconnect_useless_nodes(Unique_Node_List& useful, Unique_Node_List& worklist, const Unique_Node_List* root_and_safepoints) {
 443   uint next = 0;

 451     // Use raw traversal of out edges since this code removes out edges
 452     int max = n->outcnt();
 453     for (int j = 0; j < max; ++j) {
 454       Node* child = n->raw_out(j);
 455       if (!useful.member(child)) {
 456         assert(!child->is_top() || child != top(),
 457                "If top is cached in Compile object it is in useful list");
 458         // Only need to remove this out-edge to the useless node
 459         n->raw_del_out(j);
 460         --j;
 461         --max;
 462         if (child->is_data_proj_of_pure_function(n)) {
 463           worklist.push(n);
 464         }
 465       }
 466     }
 467     if (n->outcnt() == 1 && n->has_special_unique_user()) {
 468       assert(useful.member(n->unique_out()), "do not push a useless node");
 469       worklist.push(n->unique_out());
 470     }
 471     if (n->outcnt() == 0) {
 472       worklist.push(n);
 473     }
 474   }
 475 
 476   remove_useless_nodes(_macro_nodes,        useful); // remove useless macro nodes
 477   remove_useless_nodes(_parse_predicates,   useful); // remove useless Parse Predicate nodes
 478   // Remove useless Template Assertion Predicate opaque nodes
 479   remove_useless_nodes(_template_assertion_predicate_opaques, useful);
 480   remove_useless_nodes(_expensive_nodes,    useful); // remove useless expensive nodes
 481   remove_useless_nodes(_reachability_fences, useful); // remove useless node recorded for post loop opts IGVN pass
 482   remove_useless_nodes(_for_post_loop_igvn, useful); // remove useless node recorded for post loop opts IGVN pass
 483   remove_useless_nodes(_inline_type_nodes,  useful); // remove useless inline type nodes
 484   remove_useless_nodes(_flat_access_nodes, useful);  // remove useless flat access nodes
 485 #ifdef ASSERT
 486   if (_modified_nodes != nullptr) {
 487     _modified_nodes->remove_useless_nodes(useful.member_set());
 488   }
 489 #endif
 490   remove_useless_nodes(_for_merge_stores_igvn, useful); // remove useless node recorded for merge stores IGVN pass
 491   remove_useless_unstable_if_traps(useful);          // remove useless unstable_if traps
 492   remove_useless_coarsened_locks(useful);            // remove useless coarsened locks nodes
 493 #ifdef ASSERT
 494   if (_modified_nodes != nullptr) {
 495     _modified_nodes->remove_useless_nodes(useful.member_set());
 496   }
 497 #endif
 498 
 499   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 500   bs->eliminate_useless_gc_barriers(useful, this);
 501   // clean up the late inline lists
 502   remove_useless_late_inlines(                &_late_inlines, useful);
 503   remove_useless_late_inlines(         &_string_late_inlines, useful);
 504   remove_useless_late_inlines(         &_boxing_late_inlines, useful);
 505   remove_useless_late_inlines(&_vector_reboxing_late_inlines, useful);
 506   DEBUG_ONLY(verify_graph_edges(true /*check for no_dead_code*/, root_and_safepoints);)
 507 }
 508 
 509 // ============================================================================

 657 
 658 Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci,
 659                  Options options, DirectiveSet* directive)
 660     : Phase(Compiler),
 661       _compile_id(ci_env->compile_id()),
 662       _options(options),
 663       _method(target),
 664       _entry_bci(osr_bci),
 665       _ilt(nullptr),
 666       _stub_function(nullptr),
 667       _stub_name(nullptr),
 668       _stub_id(StubId::NO_STUBID),
 669       _stub_entry_point(nullptr),
 670       _max_node_limit(MaxNodeLimit),
 671       _node_count_inlining_cutoff(NodeCountInliningCutoff),
 672       _post_loop_opts_phase(false),
 673       _merge_stores_phase(false),
 674       _allow_macro_nodes(true),
 675       _inlining_progress(false),
 676       _inlining_incrementally(false),
 677       _strength_reduction(false),
 678       _do_cleanup(false),
 679       _has_reserved_stack_access(target->has_reserved_stack_access()),
 680       _has_circular_inline_type(false),
 681 #ifndef PRODUCT
 682       _igv_idx(0),
 683       _trace_opto_output(directive->TraceOptoOutputOption),
 684 #endif
 685       _clinit_barrier_on_entry(false),
 686       _stress_seed(0),
 687       _comp_arena(mtCompiler, Arena::Tag::tag_comp),
 688       _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 689       _env(ci_env),
 690       _directive(directive),
 691       _log(ci_env->log()),
 692       _first_failure_details(nullptr),
 693       _intrinsics(comp_arena(), 0, 0, nullptr),
 694       _macro_nodes(comp_arena(), 8, 0, nullptr),
 695       _parse_predicates(comp_arena(), 8, 0, nullptr),
 696       _template_assertion_predicate_opaques(comp_arena(), 8, 0, nullptr),
 697       _expensive_nodes(comp_arena(), 8, 0, nullptr),
 698       _reachability_fences(comp_arena(), 8, 0, nullptr),
 699       _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
 700       _inline_type_nodes (comp_arena(), 8, 0, nullptr),
 701       _flat_access_nodes(comp_arena(), 8, 0, nullptr),
 702       _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
 703       _unstable_if_traps(comp_arena(), 8, 0, nullptr),
 704       _coarsened_locks(comp_arena(), 8, 0, nullptr),
 705       _congraph(nullptr),
 706       NOT_PRODUCT(_igv_printer(nullptr) COMMA)
 707       _unique(0),
 708       _dead_node_count(0),
 709       _dead_node_list(comp_arena()),
 710       _node_arena_one(mtCompiler, Arena::Tag::tag_node),
 711       _node_arena_two(mtCompiler, Arena::Tag::tag_node),
 712       _node_arena(&_node_arena_one),
 713       _mach_constant_base_node(nullptr),
 714       _Compile_types(mtCompiler, Arena::Tag::tag_type),
 715       _initial_gvn(nullptr),
 716       _igvn_worklist(nullptr),
 717       _types(nullptr),
 718       _node_hash(nullptr),
 719       _late_inlines(comp_arena(), 2, 0, nullptr),
 720       _string_late_inlines(comp_arena(), 2, 0, nullptr),
 721       _boxing_late_inlines(comp_arena(), 2, 0, nullptr),

 790 #define MINIMUM_NODE_HASH  1023
 791 
 792   // GVN that will be run immediately on new nodes
 793   uint estimated_size = method()->code_size()*4+64;
 794   estimated_size = (estimated_size < MINIMUM_NODE_HASH ? MINIMUM_NODE_HASH : estimated_size);
 795   _igvn_worklist = new (comp_arena()) Unique_Node_List(comp_arena());
 796   _types = new (comp_arena()) Type_Array(comp_arena());
 797   _node_hash = new (comp_arena()) NodeHash(comp_arena(), estimated_size);
 798   PhaseGVN gvn;
 799   set_initial_gvn(&gvn);
 800 
 801   { // Scope for timing the parser
 802     TracePhase tp(_t_parser);
 803 
 804     // Put top into the hash table ASAP.
 805     initial_gvn()->transform(top());
 806 
 807     // Set up tf(), start(), and find a CallGenerator.
 808     CallGenerator* cg = nullptr;
 809     if (is_osr_compilation()) {
 810       init_tf(TypeFunc::make(method(), false, /* is_osr_compilation = */ true));
 811       StartNode* s = new StartOSRNode(root(), tf()->domain_sig());


 812       initial_gvn()->set_type_bottom(s);
 813       verify_start(s);
 814       cg = CallGenerator::for_osr(method(), entry_bci());
 815     } else {
 816       // Normal case.
 817       init_tf(TypeFunc::make(method(), false));
 818       StartNode* s = new StartNode(root(), tf()->domain_cc());
 819       initial_gvn()->set_type_bottom(s);
 820       verify_start(s);
 821       float past_uses = method()->interpreter_invocation_count();
 822       float expected_uses = past_uses;
 823       cg = CallGenerator::for_inline(method(), expected_uses);
 824     }
 825     if (failing())  return;
 826     if (cg == nullptr) {
 827       const char* reason = InlineTree::check_can_parse(method());
 828       assert(reason != nullptr, "expect reason for parse failure");
 829       stringStream ss;
 830       ss.print("cannot parse method: %s", reason);
 831       record_method_not_compilable(ss.as_string());
 832       return;
 833     }
 834 
 835     gvn.set_type(root(), root()->bottom_type());
 836 
 837     JVMState* jvms = build_start_state(start(), tf());
 838     if ((jvms = cg->generate(jvms)) == nullptr) {

 898   if (should_print_ideal()) {
 899     print_ideal_ir("PrintIdeal");
 900   }
 901 #endif
 902 
 903   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 904   bs->final_refinement(this);
 905 
 906 #ifdef ASSERT
 907   bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
 908 #endif
 909 
 910   // Dump compilation data to replay it.
 911   if (directive->DumpReplayOption) {
 912     env()->dump_replay_data(_compile_id);
 913   }
 914   if (directive->DumpInlineOption && (ilt() != nullptr)) {
 915     env()->dump_inline_data(_compile_id);
 916   }
 917 
 918   // Now that we know the size of all the monitors we can add fixed slots:
 919   // [...]
 920   // rsp+80: saved fp register
 921   // rsp+76: Fixed slot 7
 922   // rsp+72: Fixed slot 6 (stack increment)
 923   // rsp+68: Fixed slot 5
 924   // rsp+64: Fixed slot 4 (null marker)
 925   // rsp+60: Fixed slot 3
 926   // rsp+56: Fixed slot 2 (original deopt pc)
 927   // rsp+52: Fixed slot 1
 928   // rsp+48: Fixed slot 0 (monitors)
 929   // rsp+44: spill
 930   // [...]
 931 
 932   // One extra slot for the original deopt pc.
 933   int next_slot = fixed_slots();
 934   next_slot += VMRegImpl::slots_per_word;
 935 
 936   // One extra slot for the special stack increment value.
 937   if (needs_stack_repair()) {
 938     next_slot += VMRegImpl::slots_per_word;
 939   }
 940 
 941   // One extra slot to hold the null marker at scalarized returns.
 942   if (needs_nm_slot()) {
 943     next_slot += VMRegImpl::slots_per_word;
 944   }
 945   set_fixed_slots(next_slot);
 946 
 947   // Compute when to use implicit null checks. Used by matching trap based
 948   // nodes and NullCheck optimization.
 949   set_allowed_deopt_reasons();
 950 
 951   // Now generate code
 952   Code_Gen();
 953 }
 954 
 955 // C2 uses runtime stubs serialized generation to initialize its static tables
 956 // shared by all compilations, like Type::_shared_type_dict.
 957 // At least one stub have to be completely generated to execute intialization
 958 // before we can skip the rest stubs generation by loading AOT cached stubs.
 959 
 960 static bool c2_do_stub_init_complete = false;
 961 
 962 //------------------------------Compile----------------------------------------
 963 // Compile a runtime stub
 964 Compile::Compile(ciEnv* ci_env,

 970                  bool pass_tls,
 971                  bool return_pc,
 972                  DirectiveSet* directive)
 973     : Phase(Compiler),
 974       _compile_id(0),
 975       _options(Options::for_runtime_stub()),
 976       _method(nullptr),
 977       _entry_bci(InvocationEntryBci),
 978       _stub_function(stub_function),
 979       _stub_name(stub_name),
 980       _stub_id(stub_id),
 981       _stub_entry_point(nullptr),
 982       _max_node_limit(MaxNodeLimit),
 983       _node_count_inlining_cutoff(NodeCountInliningCutoff),
 984       _post_loop_opts_phase(false),
 985       _merge_stores_phase(false),
 986       _allow_macro_nodes(true),
 987       _inlining_progress(false),
 988       _inlining_incrementally(false),
 989       _has_reserved_stack_access(false),
 990       _has_circular_inline_type(false),
 991 #ifndef PRODUCT
 992       _igv_idx(0),
 993       _trace_opto_output(directive->TraceOptoOutputOption),
 994 #endif
 995       _clinit_barrier_on_entry(false),
 996       _stress_seed(0),
 997       _comp_arena(mtCompiler, Arena::Tag::tag_comp),
 998       _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 999       _env(ci_env),
1000       _directive(directive),
1001       _log(ci_env->log()),
1002       _first_failure_details(nullptr),
1003       _reachability_fences(comp_arena(), 8, 0, nullptr),
1004       _for_post_loop_igvn(comp_arena(), 8, 0, nullptr),
1005       _for_merge_stores_igvn(comp_arena(), 8, 0, nullptr),
1006       _congraph(nullptr),
1007       NOT_PRODUCT(_igv_printer(nullptr) COMMA)
1008       _unique(0),
1009       _dead_node_count(0),
1010       _dead_node_list(comp_arena()),

1130   _fixed_slots = 0;
1131   set_has_split_ifs(false);
1132   set_has_loops(false); // first approximation
1133   set_has_stringbuilder(false);
1134   set_has_boxed_value(false);
1135   _trap_can_recompile = false;  // no traps emitted yet
1136   _major_progress = true; // start out assuming good things will happen
1137   set_has_unsafe_access(false);
1138   set_max_vector_size(0);
1139   set_clear_upper_avx(false);  //false as default for clear upper bits of ymm registers
1140   Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1141   set_decompile_count(0);
1142 
1143 #ifndef PRODUCT
1144   _phase_counter = 0;
1145   Copy::zero_to_bytes(_igv_phase_iter, sizeof(_igv_phase_iter));
1146 #endif
1147 
1148   set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1149   _loop_opts_cnt = LoopOptsCount;
1150   _has_flat_accesses = false;
1151   _flat_accesses_share_alias = true;
1152   _scalarize_in_safepoints = false;
1153   _needs_nm_slot = false;
1154 
1155   set_do_inlining(Inline);
1156   set_max_inline_size(MaxInlineSize);
1157   set_freq_inline_size(FreqInlineSize);
1158   set_do_scheduling(OptoScheduling);
1159 
1160   set_do_vector_loop(false);
1161   set_has_monitors(false);
1162   set_has_scoped_access(false);
1163 
1164   if (AllowVectorizeOnDemand) {
1165     if (has_method() && _directive->VectorizeOption) {
1166       set_do_vector_loop(true);
1167       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());})
1168     } else if (has_method() && method()->name() != nullptr &&
1169                method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1170       set_do_vector_loop(true);
1171     }
1172   }
1173   set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1174   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());})

1406   // If this method has already thrown a range-check,
1407   // assume it was because we already tried range smearing
1408   // and it failed.
1409   uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1410   return !already_trapped;
1411 }
1412 
1413 
1414 //------------------------------flatten_alias_type-----------------------------
1415 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1416   assert(do_aliasing(), "Aliasing should be enabled");
1417   int offset = tj->offset();
1418   TypePtr::PTR ptr = tj->ptr();
1419 
1420   // Known instance (scalarizable allocation) alias only with itself.
1421   bool is_known_inst = tj->isa_oopptr() != nullptr &&
1422                        tj->is_oopptr()->is_known_instance();
1423 
1424   // Process weird unsafe references.
1425   if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1426     assert(InlineUnsafeOps || StressReflectiveCode || UseAcmpFastPath, "indeterminate pointers come only from unsafe ops");
1427     assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1428     tj = TypeOopPtr::BOTTOM;
1429     ptr = tj->ptr();
1430     offset = tj->offset();
1431   }
1432 
1433   // Array pointers need some flattening
1434   const TypeAryPtr* ta = tj->isa_aryptr();




1435   if( ta && is_known_inst ) {
1436     if ( offset != Type::OffsetBot &&
1437          offset > arrayOopDesc::length_offset_in_bytes() ) {
1438       offset = Type::OffsetBot; // Flatten constant access into array body only
1439       tj = ta = ta->
1440               remove_speculative()->
1441               cast_to_ptr_type(ptr)->
1442               with_offset(offset);
1443     }
1444   } else if (ta != nullptr) {
1445     // Common slices
1446     if (offset == arrayOopDesc::length_offset_in_bytes()) {
1447       return TypeAryPtr::RANGE;
1448     } else if (offset == oopDesc::klass_offset_in_bytes()) {
1449       return TypeInstPtr::KLASS;
1450     } else if (offset == oopDesc::mark_offset_in_bytes()) {
1451       return TypeInstPtr::MARK;
1452     }
1453 
1454     // Remove size and stability
1455     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());
1456     // Remove ptr, const_oop, and offset
1457     if (ta->elem() == Type::BOTTOM) {
1458       // Bottom array (meet of int[] and byte[] for example), accesses to it will be done with
1459       // Unsafe. This should alias with all arrays. For now just leave it as it is (this is
1460       // incorrect, see JDK-8331133).
1461       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, false, Type::Offset::bottom);
1462     } else if (ta->elem()->make_oopptr() != nullptr) {
1463       // Object arrays, keep field_offset
1464       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, nullptr, ta->klass_is_exact(), Type::Offset::bottom, Type::Offset(ta->field_offset()));

1465     } else {
1466       // Primitive arrays
1467       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, normalized_ary, ta->exact_klass(), true, Type::Offset::bottom);
1468     }
1469 
1470     // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1471     // cannot be distinguished by bytecode alone.
1472     if (ta->elem() == TypeInt::BOOL) {
1473       tj = ta = TypeAryPtr::BYTES;
1474     }
1475 
1476     // All arrays of references share the same slice
1477     if (!ta->is_flat() && ta->elem()->make_oopptr() != nullptr) {
1478       const TypeAry* tary = TypeAry::make(TypeInstPtr::BOTTOM, TypeInt::POS, false, false, true, true, true);
1479       tj = ta = TypeAryPtr::make(TypePtr::BotPTR, nullptr, tary, nullptr, false, Type::Offset::bottom);
1480     }
1481 
1482     if (ta->is_flat()) {
1483       if (_flat_accesses_share_alias) {
1484         // Initially all flattened array accesses share a single slice
1485         tj = ta = TypeAryPtr::INLINES;
1486       } else {
1487         // Flat accesses are always exact
1488         tj = ta = ta->cast_to_exactness(true);
1489       }
1490     }
1491   }
1492 
1493   // Oop pointers need some flattening
1494   const TypeInstPtr *to = tj->isa_instptr();
1495   if (to && to != TypeOopPtr::BOTTOM) {
1496     ciInstanceKlass* ik = to->instance_klass();
1497     tj = to = to->cast_to_maybe_flat_in_array(); // flatten to maybe flat in array
1498     if( ptr == TypePtr::Constant ) {
1499       if (ik != ciEnv::current()->Class_klass() ||
1500           offset < ik->layout_helper_size_in_bytes()) {
1501         // No constant oop pointers (such as Strings); they alias with
1502         // unknown strings.
1503         assert(!is_known_inst, "not scalarizable allocation");
1504         tj = to = to->
1505                 cast_to_instance_id(TypeOopPtr::InstanceBot)->
1506                 remove_speculative()->
1507                 cast_to_ptr_type(TypePtr::BotPTR)->
1508                 cast_to_exactness(false);
1509       }
1510     } else if( is_known_inst ) {
1511       tj = to; // Keep NotNull and klass_is_exact for instance type
1512     } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1513       // During the 2nd round of IterGVN, NotNull castings are removed.
1514       // Make sure the Bottom and NotNull variants alias the same.
1515       // Also, make sure exact and non-exact variants alias the same.
1516       tj = to = to->
1517               remove_speculative()->
1518               cast_to_instance_id(TypeOopPtr::InstanceBot)->
1519               cast_to_ptr_type(TypePtr::BotPTR)->
1520               cast_to_exactness(false);
1521     }
1522     if (to->speculative() != nullptr) {
1523       tj = to = to->remove_speculative();
1524     }
1525     // Canonicalize the holder of this field
1526     if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1527       // First handle header references such as a LoadKlassNode, even if the
1528       // object's klass is unloaded at compile time (4965979).
1529       if (!is_known_inst) { // Do it only for non-instance types
1530         tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, nullptr, Type::Offset(offset));
1531       }
1532     } else if (offset < 0 || offset >= ik->layout_helper_size_in_bytes()) {
1533       // Static fields are in the space above the normal instance
1534       // fields in the java.lang.Class instance.
1535       if (ik != ciEnv::current()->Class_klass()) {
1536         to = nullptr;
1537         tj = TypeOopPtr::BOTTOM;
1538         offset = tj->offset();
1539       }
1540     } else {
1541       ciInstanceKlass *canonical_holder = ik->get_canonical_holder(offset);
1542       assert(offset < canonical_holder->layout_helper_size_in_bytes(), "");
1543       assert(tj->offset() == offset, "no change to offset expected");
1544       bool xk = to->klass_is_exact();
1545       int instance_id = to->instance_id();
1546 
1547       // If the input type's class is the holder: if exact, the type only includes interfaces implemented by the holder
1548       // but if not exact, it may include extra interfaces: build new type from the holder class to make sure only
1549       // its interfaces are included.
1550       if (xk && ik->equals(canonical_holder)) {
1551         assert(tj == TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id,
1552                                        TypePtr::MaybeFlat), "exact type should be canonical type");
1553       } else {
1554         assert(xk || !is_known_inst, "Known instance should be exact type");
1555         tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, is_known_inst, nullptr, Type::Offset(offset), instance_id,
1556                                     TypePtr::MaybeFlat);
1557       }
1558     }
1559   }
1560 
1561   // Klass pointers to object array klasses need some flattening
1562   const TypeKlassPtr *tk = tj->isa_klassptr();
1563   if( tk ) {
1564     // If we are referencing a field within a Klass, we need
1565     // to assume the worst case of an Object.  Both exact and
1566     // inexact types must flatten to the same alias class so
1567     // use NotNull as the PTR.
1568     if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1569       tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull,
1570                                        env()->Object_klass(),
1571                                        Type::Offset(offset),
1572                                        TypePtr::MaybeFlat);
1573     }
1574 
1575     if (tk->isa_aryklassptr() && tk->is_aryklassptr()->elem()->isa_klassptr()) {
1576       ciKlass* k = ciObjArrayKlass::make(env()->Object_klass());
1577       if (!k || !k->is_loaded()) {                  // Only fails for some -Xcomp runs
1578         tj = tk = TypeInstKlassPtr::make(TypePtr::NotNull, env()->Object_klass(), Type::Offset(offset), TypePtr::MaybeFlat);
1579       } else {
1580         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());
1581       }
1582     }

1583     // Check for precise loads from the primary supertype array and force them
1584     // to the supertype cache alias index.  Check for generic array loads from
1585     // the primary supertype array and also force them to the supertype cache
1586     // alias index.  Since the same load can reach both, we need to merge
1587     // these 2 disparate memories into the same alias class.  Since the
1588     // primary supertype array is read-only, there's no chance of confusion
1589     // where we bypass an array load and an array store.
1590     int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1591     if (offset == Type::OffsetBot ||
1592         (offset >= primary_supers_offset &&
1593          offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1594         offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1595       offset = in_bytes(Klass::secondary_super_cache_offset());
1596       tj = tk = tk->with_offset(offset);
1597     }
1598   }
1599 
1600   // Flatten all Raw pointers together.
1601   if (tj->base() == Type::RawPtr)
1602     tj = TypeRawPtr::BOTTOM;

1692   intptr_t key = (intptr_t) adr_type;
1693   key ^= key >> logAliasCacheSize;
1694   return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1695 }
1696 
1697 
1698 //-----------------------------grow_alias_types--------------------------------
1699 void Compile::grow_alias_types() {
1700   const int old_ats  = _max_alias_types; // how many before?
1701   const int new_ats  = old_ats;          // how many more?
1702   const int grow_ats = old_ats+new_ats;  // how many now?
1703   _max_alias_types = grow_ats;
1704   _alias_types =  REALLOC_ARENA_ARRAY(comp_arena(), _alias_types, old_ats, grow_ats);
1705   AliasType* ats =    NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1706   Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1707   for (int i = 0; i < new_ats; i++)  _alias_types[old_ats+i] = &ats[i];
1708 }
1709 
1710 
1711 //--------------------------------find_alias_type------------------------------
1712 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1713   if (!do_aliasing()) {
1714     return alias_type(AliasIdxBot);
1715   }
1716 
1717   AliasCacheEntry* ace = nullptr;
1718   if (!uncached) {
1719     ace = probe_alias_cache(adr_type);
1720     if (ace->_adr_type == adr_type) {
1721       return alias_type(ace->_index);
1722     }
1723   }
1724 
1725   // Handle special cases.
1726   if (adr_type == nullptr)          return alias_type(AliasIdxTop);
1727   if (adr_type == TypePtr::BOTTOM)  return alias_type(AliasIdxBot);
1728 
1729   // Do it the slow way.
1730   const TypePtr* flat = flatten_alias_type(adr_type);
1731 
1732 #ifdef ASSERT
1733   {
1734     ResourceMark rm;
1735     assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1736            Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1737     assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1738            Type::str(adr_type));
1739     if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1740       const TypeOopPtr* foop = flat->is_oopptr();
1741       // Scalarizable allocations have exact klass always.
1742       bool exact = !foop->klass_is_exact() || foop->is_known_instance();

1752     if (alias_type(i)->adr_type() == flat) {
1753       idx = i;
1754       break;
1755     }
1756   }
1757 
1758   if (idx == AliasIdxTop) {
1759     if (no_create)  return nullptr;
1760     // Grow the array if necessary.
1761     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1762     // Add a new alias type.
1763     idx = _num_alias_types++;
1764     _alias_types[idx]->Init(idx, flat);
1765     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1766     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1767     if (flat->isa_instptr()) {
1768       if (flat->offset() == java_lang_Class::klass_offset()
1769           && flat->is_instptr()->instance_klass() == env()->Class_klass())
1770         alias_type(idx)->set_rewritable(false);
1771     }
1772     ciField* field = nullptr;
1773     if (flat->isa_aryptr()) {
1774 #ifdef ASSERT
1775       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1776       // (T_BYTE has the weakest alignment and size restrictions...)
1777       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1778 #endif
1779       const Type* elemtype = flat->is_aryptr()->elem();
1780       if (flat->offset() == TypePtr::OffsetBot) {
1781         alias_type(idx)->set_element(elemtype);
1782       }
1783       int field_offset = flat->is_aryptr()->field_offset().get();
1784       if (flat->is_flat() &&
1785           field_offset != Type::OffsetBot) {
1786         ciInlineKlass* vk = elemtype->inline_klass();
1787         field_offset += vk->payload_offset();
1788         field = vk->get_field_by_offset(field_offset, false);
1789       }
1790     }
1791     if (flat->isa_klassptr()) {
1792       if (UseCompactObjectHeaders) {
1793         if (flat->offset() == in_bytes(Klass::prototype_header_offset()))
1794           alias_type(idx)->set_rewritable(false);
1795       }
1796       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1797         alias_type(idx)->set_rewritable(false);
1798       if (flat->offset() == in_bytes(Klass::misc_flags_offset()))
1799         alias_type(idx)->set_rewritable(false);
1800       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1801         alias_type(idx)->set_rewritable(false);
1802       if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1803         alias_type(idx)->set_rewritable(false);
1804       if (flat->offset() == in_bytes(Klass::secondary_super_cache_offset()))
1805         alias_type(idx)->set_rewritable(false);
1806     }
1807 
1808     if (flat->isa_instklassptr()) {
1809       if (flat->offset() == in_bytes(InstanceKlass::access_flags_offset())) {
1810         alias_type(idx)->set_rewritable(false);
1811       }
1812     }
1813     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1814     // but the base pointer type is not distinctive enough to identify
1815     // references into JavaThread.)
1816 
1817     // Check for final fields.
1818     const TypeInstPtr* tinst = flat->isa_instptr();
1819     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {

1820       if (tinst->const_oop() != nullptr &&
1821           tinst->instance_klass() == ciEnv::current()->Class_klass() &&
1822           tinst->offset() >= (tinst->instance_klass()->layout_helper_size_in_bytes())) {
1823         // static field
1824         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1825         field = k->get_field_by_offset(tinst->offset(), true);
1826       } else if (tinst->is_inlinetypeptr()) {
1827         // Inline type field
1828         ciInlineKlass* vk = tinst->inline_klass();
1829         field = vk->get_field_by_offset(tinst->offset(), false);
1830       } else {
1831         ciInstanceKlass *k = tinst->instance_klass();
1832         field = k->get_field_by_offset(tinst->offset(), false);
1833       }
1834     }
1835     assert(field == nullptr ||
1836            original_field == nullptr ||
1837            (field->holder() == original_field->holder() &&
1838             field->offset_in_bytes() == original_field->offset_in_bytes() &&
1839             field->is_static() == original_field->is_static()), "wrong field?");
1840     // Set field() and is_rewritable() attributes.
1841     if (field != nullptr) {
1842       alias_type(idx)->set_field(field);
1843       if (flat->isa_aryptr()) {
1844         // Fields of flat arrays are rewritable although they are declared final
1845         assert(flat->is_flat(), "must be a flat array");
1846         alias_type(idx)->set_rewritable(true);
1847       }
1848     }
1849   }
1850 
1851   // Fill the cache for next time.
1852   if (!uncached) {
1853     ace->_adr_type = adr_type;
1854     ace->_index    = idx;
1855     assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");
1856 
1857     // Might as well try to fill the cache for the flattened version, too.
1858     AliasCacheEntry* face = probe_alias_cache(flat);
1859     if (face->_adr_type == nullptr) {
1860       face->_adr_type = flat;
1861       face->_index    = idx;
1862       assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1863     }
1864   }
1865 
1866   return alias_type(idx);
1867 }
1868 
1869 
1870 Compile::AliasType* Compile::alias_type(ciField* field) {
1871   const TypeOopPtr* t;
1872   if (field->is_static())
1873     t = TypeInstPtr::make(field->holder()->java_mirror());
1874   else
1875     t = TypeOopPtr::make_from_klass_raw(field->holder());
1876   AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1877   assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1878   return atp;
1879 }
1880 
1881 
1882 //------------------------------have_alias_type--------------------------------
1883 bool Compile::have_alias_type(const TypePtr* adr_type) {

1965   assert(!C->major_progress(), "not cleared");
1966 
1967   if (_for_post_loop_igvn.length() > 0) {
1968     while (_for_post_loop_igvn.length() > 0) {
1969       Node* n = _for_post_loop_igvn.pop();
1970       n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
1971       igvn._worklist.push(n);
1972     }
1973     igvn.optimize();
1974     if (failing()) return;
1975     assert(_for_post_loop_igvn.length() == 0, "no more delayed nodes allowed");
1976     assert(C->parse_predicate_count() == 0, "all parse predicates should have been removed now");
1977 
1978     // Sometimes IGVN sets major progress (e.g., when processing loop nodes).
1979     if (C->major_progress()) {
1980       C->clear_major_progress(); // ensure that major progress is now clear
1981     }
1982   }
1983 }
1984 
1985 void Compile::add_inline_type(Node* n) {
1986   assert(n->is_InlineType(), "unexpected node");
1987   _inline_type_nodes.push(n);
1988 }
1989 
1990 void Compile::remove_inline_type(Node* n) {
1991   assert(n->is_InlineType(), "unexpected node");
1992   if (_inline_type_nodes.contains(n)) {
1993     _inline_type_nodes.remove(n);
1994   }
1995 }
1996 
1997 // Does the return value keep otherwise useless inline type allocations alive?
1998 static bool return_val_keeps_allocations_alive(Node* ret_val) {
1999   ResourceMark rm;
2000   Unique_Node_List wq;
2001   wq.push(ret_val);
2002   bool some_allocations = false;
2003   for (uint i = 0; i < wq.size(); i++) {
2004     Node* n = wq.at(i);
2005     if (n->outcnt() > 1) {
2006       // Some other use for the allocation
2007       return false;
2008     } else if (n->is_InlineType()) {
2009       wq.push(n->in(1));
2010     } else if (n->is_Phi()) {
2011       for (uint j = 1; j < n->req(); j++) {
2012         wq.push(n->in(j));
2013       }
2014     } else if (n->is_CheckCastPP() &&
2015                n->in(1)->is_Proj() &&
2016                n->in(1)->in(0)->is_Allocate()) {
2017       some_allocations = true;
2018     } else if (n->is_CheckCastPP() || n->is_CastPP()) {
2019       wq.push(n->in(1));
2020     }
2021   }
2022   return some_allocations;
2023 }
2024 
2025 bool Compile::clear_argument_if_only_used_as_buffer_at_calls(Node* result_cast, PhaseIterGVN& igvn) {
2026   ResourceMark rm;
2027   Unique_Node_List wq;
2028   wq.push(result_cast);
2029   Node_List calls;
2030   for (uint i = 0; i < wq.size(); ++i) {
2031     Node* n = wq.at(i);
2032     for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
2033       Node* u = n->fast_out(j);
2034       if (u->is_Phi()) {
2035         wq.push(u);
2036       } else if (u->is_InlineType() && u->as_InlineType()->get_oop() == n) {
2037         wq.push(u);
2038       } else if (u->is_CallJava()) {
2039         CallJavaNode* call = u->as_CallJava();
2040         if (call->method() != nullptr && call->method()->mismatch()) {
2041           return false;
2042         }
2043         uint nargs = call->tf()->domain_cc()->cnt();
2044         for (uint k = TypeFunc::Parms; k < nargs; k++) {
2045           Node* in = call->in(k);
2046           if (in == n && (call->method() == nullptr || !call->method()->is_scalarized_buffer_arg(k - TypeFunc::Parms))) {
2047             return false;
2048           }
2049         }
2050         calls.push(call);
2051       } else if (u->Opcode() == Op_EncodeP) {
2052         wq.push(u);
2053       } else if (u->is_AddP()) {
2054         wq.push(u);
2055       } else if (u->is_Store() && u->in(MemNode::Address) == n) {
2056         // storing to the buffer is fine
2057       } else if (u->is_SafePoint()) {
2058         SafePointNode* sfpt = u->as_SafePoint();
2059         int input = u->find_edge(n);
2060         JVMState* jvms = sfpt->jvms();
2061         if (jvms != nullptr) {
2062           if (input < (int)jvms->debug_start()) {
2063             return false;
2064           }
2065         }
2066       } else {
2067         return false;
2068       }
2069     }
2070   }
2071   for (uint i = 0; i < calls.size(); ++i) {
2072     CallJavaNode* call = calls.at(i)->as_CallJava();
2073     uint nargs = call->tf()->domain_cc()->cnt();
2074     for (uint k = TypeFunc::Parms; k < nargs; k++) {
2075       Node* in = call->in(k);
2076       if (wq.member(in)) {
2077         assert(call->method()->is_scalarized_buffer_arg(k - TypeFunc::Parms), "only buffer argument removed here");
2078         igvn.replace_input_of(call, k, igvn.zerocon(T_OBJECT));
2079       }
2080     }
2081   }
2082   return true;
2083 }
2084 
2085 void Compile::process_inline_types(PhaseIterGVN &igvn, bool remove) {
2086   // Make sure that the return value does not keep an otherwise unused allocation alive
2087   if (tf()->returns_inline_type_as_fields()) {
2088     Node* ret = nullptr;
2089     for (uint i = 1; i < root()->req(); i++) {
2090       Node* in = root()->in(i);
2091       if (in->Opcode() == Op_Return) {
2092         assert(ret == nullptr, "only one return");
2093         ret = in;
2094       }
2095     }
2096     if (ret != nullptr) {
2097       Node* ret_val = ret->in(TypeFunc::Parms);
2098       if (igvn.type(ret_val)->isa_oopptr() &&
2099           return_val_keeps_allocations_alive(ret_val)) {
2100         igvn.replace_input_of(ret, TypeFunc::Parms, InlineTypeNode::tagged_klass(igvn.type(ret_val)->inline_klass(), igvn));
2101         assert(ret_val->outcnt() == 0, "should be dead now");
2102         igvn.remove_dead_node(ret_val, PhaseIterGVN::NodeOrigin::Graph);
2103       }
2104     }
2105   }
2106   // if a newly allocated object is a value that's only passed as argument to calls as (possibly null) buffers, then
2107   // clear the call argument inputs so the allocation node can be removed
2108   for (int i = 0; i < C->macro_count(); ++i) {
2109     Node* macro_node = C->macro_node(i);
2110     if (macro_node->Opcode() == Op_Allocate) {
2111       AllocateNode* allocate = macro_node->as_Allocate();
2112       Node* result_cast = allocate->result_cast();
2113       if (result_cast != nullptr) {
2114         const Type* result_type = igvn.type(result_cast);
2115         if (result_type->is_inlinetypeptr()) {
2116           clear_argument_if_only_used_as_buffer_at_calls(result_cast, igvn);
2117         }
2118       }
2119     }
2120   }
2121 
2122   if (_inline_type_nodes.length() == 0) {
2123     // keep the graph canonical
2124     igvn.optimize();
2125     return;
2126   }
2127   // Scalarize inline types in safepoint debug info.
2128   // Delay this until all inlining is over to avoid getting inconsistent debug info.
2129   set_scalarize_in_safepoints(true);
2130   for (int i = _inline_type_nodes.length()-1; i >= 0; i--) {
2131     InlineTypeNode* vt = _inline_type_nodes.at(i)->as_InlineType();
2132     vt->make_scalar_in_safepoints(&igvn);
2133     igvn.record_for_igvn(vt);
2134   }
2135   if (remove) {
2136     // Remove inline type nodes by replacing them with their oop input
2137     while (_inline_type_nodes.length() > 0) {
2138       InlineTypeNode* vt = _inline_type_nodes.pop()->as_InlineType();
2139       if (vt->outcnt() == 0) {
2140         igvn.remove_dead_node(vt, PhaseIterGVN::NodeOrigin::Graph);
2141         continue;
2142       }
2143       for (DUIterator i = vt->outs(); vt->has_out(i); i++) {
2144         DEBUG_ONLY(bool must_be_buffered = false);
2145         Node* u = vt->out(i);
2146         // Check if any users are blackholes. If so, rewrite them to use either the
2147         // allocated buffer, or individual components, instead of the inline type node
2148         // that goes away.
2149         if (u->is_Blackhole()) {
2150           BlackholeNode* bh = u->as_Blackhole();
2151 
2152           // Unlink the old input
2153           int idx = bh->find_edge(vt);
2154           assert(idx != -1, "The edge should be there");
2155           bh->del_req(idx);
2156           --i;
2157 
2158           if (vt->is_allocated(&igvn)) {
2159             // Already has the allocated instance, blackhole that
2160             bh->add_req(vt->get_oop());
2161           } else {
2162             // Not allocated yet, blackhole the components
2163             for (uint c = 0; c < vt->field_count(); c++) {
2164               bh->add_req(vt->field_value(c));
2165             }
2166           }
2167 
2168           // Node modified, record for IGVN
2169           igvn.record_for_igvn(bh);
2170         }
2171 #ifdef ASSERT
2172         // Verify that inline type is buffered when replacing by oop
2173         else if (u->is_InlineType()) {
2174           // InlineType uses don't need buffering because they are about to be replaced as well
2175         } else {
2176           must_be_buffered = true;
2177         }
2178         if (must_be_buffered && !vt->is_allocated(&igvn)) {
2179           vt->dump(0);
2180           u->dump(0);
2181           assert(false, "Should have been buffered");
2182         }
2183 #endif
2184       }
2185       igvn.replace_node(vt, vt->get_oop());
2186     }
2187   }
2188   igvn.optimize();
2189 }
2190 
2191 void Compile::add_flat_access(Node* n) {
2192   assert(n != nullptr && (n->Opcode() == Op_LoadFlat || n->Opcode() == Op_StoreFlat), "unexpected node %s", n == nullptr ? "nullptr" : n->Name());
2193   assert(!_flat_access_nodes.contains(n), "duplicate insertion");
2194   _flat_access_nodes.push(n);
2195 }
2196 
2197 void Compile::remove_flat_access(Node* n) {
2198   assert(n != nullptr && (n->Opcode() == Op_LoadFlat || n->Opcode() == Op_StoreFlat), "unexpected node %s", n == nullptr ? "nullptr" : n->Name());
2199   _flat_access_nodes.remove_if_existing(n);
2200 }
2201 
2202 void Compile::process_flat_accesses(PhaseIterGVN& igvn) {
2203   assert(igvn._worklist.size() == 0, "should be empty");
2204   igvn.set_delay_transform(true);
2205   for (int i = _flat_access_nodes.length() - 1; i >= 0; i--) {
2206     Node* n = _flat_access_nodes.at(i);
2207     assert(n != nullptr, "unexpected nullptr");
2208     if (n->is_LoadFlat()) {
2209       LoadFlatNode* loadn = n->as_LoadFlat();
2210       // Expending a flat load atomically means that we get a chunk of memory spanning multiple fields
2211       // that we chop with bitwise operations. That is too subtle for some optimizations, especially
2212       // constant folding when fields are constant. If we can get a constant object from which we are
2213       // flat-loading, we can simply replace the loads at compilation-time by the field of the constant
2214       // object.
2215       ciInstance* loaded_from = nullptr;
2216       if (FoldStableValues) {
2217         const TypeOopPtr* base_type = igvn.type(loadn->base())->is_oopptr();
2218         ciObject* oop = base_type->const_oop();
2219         int off = igvn.type(loadn->ptr())->isa_ptr()->offset();
2220 
2221         if (oop != nullptr && oop->is_instance()) {
2222           ciInstance* holder = oop->as_instance();
2223           ciKlass* klass = holder->klass();
2224           ciInstanceKlass* iklass = klass->as_instance_klass();
2225           ciField* field = iklass->get_non_flat_field_by_offset(off);
2226 
2227           if (field->is_stable()) {
2228             ciConstant fv = holder->field_value(field);
2229             if (is_reference_type(fv.basic_type()) && fv.as_object()->is_instance()) {
2230               // The field value is an object, not null. We can use stability.
2231               loaded_from = fv.as_object()->as_instance();
2232             }
2233           }
2234         } else if (oop != nullptr && oop->is_array() && off != Type::OffsetBot) {
2235           ciArray* array = oop->as_array();
2236           ciConstant elt = array->element_value_by_offset(off);
2237           const TypeAryPtr* aryptr = base_type->is_aryptr();
2238           if (aryptr->is_stable() && aryptr->is_atomic() && is_reference_type(elt.basic_type()) && elt.as_object()->is_instance()) {
2239             loaded_from = elt.as_object()->as_instance();
2240           }
2241         }
2242       }
2243 
2244       if (loaded_from != nullptr) {
2245         loadn->expand_constant(igvn, loaded_from);
2246       } else {
2247         loadn->expand_atomic(igvn);
2248       }
2249     } else {
2250       n->as_StoreFlat()->expand_atomic(igvn);
2251     }
2252   }
2253   _flat_access_nodes.clear_and_deallocate();
2254   igvn.set_delay_transform(false);
2255   igvn.optimize();
2256 }
2257 
2258 void Compile::adjust_flat_array_access_aliases(PhaseIterGVN& igvn) {
2259   DEBUG_ONLY(igvn.verify_empty_worklist(nullptr));
2260   if (!_has_flat_accesses) {
2261     return;
2262   }
2263   // Initially, all flat array accesses share the same slice to
2264   // keep dependencies with Object[] array accesses (that could be
2265   // to a flat array) correct. We're done with parsing so we
2266   // now know all flat array accesses in this compile
2267   // unit. Let's move flat array accesses to their own slice,
2268   // one per element field. This should help memory access
2269   // optimizations.
2270   ResourceMark rm;
2271   Unique_Node_List wq;
2272   wq.push(root());
2273 
2274   Node_List mergememnodes;
2275   Node_List memnodes;
2276 
2277   // Alias index currently shared by all flat memory accesses
2278   int index = get_alias_index(TypeAryPtr::INLINES);
2279 
2280   // Find MergeMem nodes and flat array accesses
2281   for (uint i = 0; i < wq.size(); i++) {
2282     Node* n = wq.at(i);
2283     if (n->is_Mem()) {
2284       const TypePtr* adr_type = nullptr;
2285       adr_type = get_adr_type(get_alias_index(n->adr_type()));
2286       if (adr_type == TypeAryPtr::INLINES) {
2287         memnodes.push(n);
2288       }
2289     } else if (n->is_MergeMem()) {
2290       MergeMemNode* mm = n->as_MergeMem();
2291       if (mm->memory_at(index) != mm->base_memory()) {
2292         mergememnodes.push(n);
2293       }
2294     }
2295     for (uint j = 0; j < n->req(); j++) {
2296       Node* m = n->in(j);
2297       if (m != nullptr) {
2298         wq.push(m);
2299       }
2300     }
2301   }
2302 
2303   _flat_accesses_share_alias = false;
2304 
2305   // We are going to change the slice for the flat array
2306   // accesses so we need to clear the cache entries that refer to
2307   // them.
2308   for (uint i = 0; i < AliasCacheSize; i++) {
2309     AliasCacheEntry* ace = &_alias_cache[i];
2310     if (ace->_adr_type != nullptr &&
2311         ace->_adr_type->is_flat()) {
2312       ace->_adr_type = nullptr;
2313       ace->_index = (i != 0) ? 0 : AliasIdxTop; // Make sure the nullptr adr_type resolves to AliasIdxTop
2314     }
2315   }
2316 
2317 #ifdef ASSERT
2318   for (uint i = 0; i < memnodes.size(); i++) {
2319     Node* m = memnodes.at(i);
2320     const TypePtr* adr_type = m->adr_type();
2321     m->as_Mem()->set_adr_type(adr_type);
2322   }
2323 #endif // ASSERT
2324 
2325   int start_alias = num_alias_types(); // Start of new aliases
2326   Node_Stack stack(0);
2327 #ifdef ASSERT
2328   VectorSet seen(Thread::current()->resource_area());
2329 #endif
2330   // Now let's fix the memory graph so each flat array access
2331   // is moved to the right slice. Start from the MergeMem nodes.
2332   uint last = unique();
2333   for (uint i = 0; i < mergememnodes.size(); i++) {
2334     MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2335     if (current->outcnt() == 0) {
2336       // This node is killed by a previous iteration
2337       continue;
2338     }
2339 
2340     Node* n = current->memory_at(index);
2341     MergeMemNode* mm = nullptr;
2342     do {
2343       // Follow memory edges through memory accesses, phis and
2344       // narrow membars and push nodes on the stack. Once we hit
2345       // bottom memory, we pop element off the stack one at a
2346       // time, in reverse order, and move them to the right slice
2347       // by changing their memory edges.
2348       if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() ||
2349           (n->adr_type() == TypeAryPtr::INLINES && !n->is_NarrowMemProj())) {
2350         assert(!seen.test_set(n->_idx), "");
2351         // Uses (a load for instance) will need to be moved to the
2352         // right slice as well and will get a new memory state
2353         // that we don't know yet. The use could also be the
2354         // backedge of a loop. We put a place holder node between
2355         // the memory node and its uses. We replace that place
2356         // holder with the correct memory state once we know it,
2357         // i.e. when nodes are popped off the stack. Using the
2358         // place holder make the logic work in the presence of
2359         // loops.
2360         if (n->outcnt() > 1) {
2361           Node* place_holder = nullptr;
2362           assert(!n->has_out_with(Op_Node), "");
2363           for (DUIterator k = n->outs(); n->has_out(k); k++) {
2364             Node* u = n->out(k);
2365             if (u != current && u->_idx < last) {
2366               bool success = false;
2367               for (uint l = 0; l < u->req(); l++) {
2368                 if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2369                   continue;
2370                 }
2371                 Node* in = u->in(l);
2372                 if (in == n) {
2373                   if (place_holder == nullptr) {
2374                     place_holder = new Node(1);
2375                     place_holder->init_req(0, n);
2376                   }
2377                   igvn.replace_input_of(u, l, place_holder);
2378                   success = true;
2379                 }
2380               }
2381               if (success) {
2382                 --k;
2383               }
2384             }
2385           }
2386         }
2387         if (n->is_Phi()) {
2388           stack.push(n, 1);
2389           n = n->in(1);
2390         } else if (n->is_Mem()) {
2391           stack.push(n, n->req());
2392           n = n->in(MemNode::Memory);
2393         } else {
2394           assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2395           stack.push(n, n->req());
2396           n = n->in(0)->in(TypeFunc::Memory);
2397         }
2398       } else {
2399         assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || n->is_NarrowMemProj(), "");
2400         // Build a new MergeMem node to carry the new memory state
2401         // as we build it. IGVN should fold extraneous MergeMem
2402         // nodes.
2403         if (n->is_NarrowMemProj()) {
2404           // We need 1 NarrowMemProj for each slice of this array
2405           InitializeNode* init = n->in(0)->as_Initialize();
2406           AllocateNode* alloc = init->allocation();
2407           Node* klass_node = alloc->in(AllocateNode::KlassNode);
2408           const TypeAryKlassPtr* klass_type = klass_node->bottom_type()->isa_aryklassptr();
2409           assert(klass_type != nullptr, "must be an array");
2410           assert(klass_type->klass_is_exact(), "must be an exact klass");
2411           ciArrayKlass* klass = klass_type->exact_klass()->as_array_klass();
2412           assert(klass->is_flat_array_klass(), "must be a flat array");
2413           ciInlineKlass* elem_klass = klass->element_klass()->as_inline_klass();
2414           const TypeAryPtr* oop_type = klass_type->as_instance_type()->is_aryptr();
2415           assert(oop_type->klass_is_exact(), "must be an exact klass");
2416 
2417           Node* base = alloc->in(TypeFunc::Memory);
2418           assert(base->bottom_type() == Type::MEMORY, "the memory input of AllocateNode must be a memory");
2419           assert(base->adr_type() == TypePtr::BOTTOM, "the memory input of AllocateNode must be a bottom memory");
2420           // Must create a MergeMem with base as the base memory, do not clone if base is a
2421           // MergeMem because it may not be processed yet
2422           mm = MergeMemNode::make(nullptr);
2423           mm->set_base_memory(base);
2424           for (int j = 0; j < elem_klass->nof_nonstatic_fields(); j++) {
2425             int field_offset = elem_klass->nonstatic_field_at(j)->offset_in_bytes() - elem_klass->payload_offset();
2426             const TypeAryPtr* field_ptr = oop_type->with_offset(Type::OffsetBot)->with_field_offset(field_offset);
2427             int field_alias_idx = get_alias_index(field_ptr);
2428             assert(field_ptr == get_adr_type(field_alias_idx), "must match");
2429             Node* new_proj = new NarrowMemProjNode(init, field_ptr);
2430             igvn.register_new_node_with_optimizer(new_proj);
2431             mm->set_memory_at(field_alias_idx, new_proj);
2432           }
2433           if (!klass->is_elem_null_free()) {
2434             int nm_offset = elem_klass->null_marker_offset_in_payload();
2435             const TypeAryPtr* nm_ptr = oop_type->with_offset(Type::OffsetBot)->with_field_offset(nm_offset);
2436             int nm_alias_idx = get_alias_index(nm_ptr);
2437             assert(nm_ptr == get_adr_type(nm_alias_idx), "must match");
2438             Node* new_proj = new NarrowMemProjNode(init, nm_ptr);
2439             igvn.register_new_node_with_optimizer(new_proj);
2440             mm->set_memory_at(nm_alias_idx, new_proj);
2441           }
2442 
2443           // Replace all uses of the old NarrowMemProj with the correct state
2444           MergeMemNode* new_n = MergeMemNode::make(mm);
2445           igvn.register_new_node_with_optimizer(new_n);
2446           igvn.replace_node(n, new_n);
2447         } else {
2448           // Must create a MergeMem with n as the base memory, do not clone if n is a MergeMem
2449           // because it may not be processed yet
2450           mm = MergeMemNode::make(nullptr);
2451           mm->set_base_memory(n);
2452         }
2453 
2454         igvn.register_new_node_with_optimizer(mm);
2455         while (stack.size() > 0) {
2456           Node* m = stack.node();
2457           uint idx = stack.index();
2458           if (m->is_Mem()) {
2459             // Move memory node to its new slice
2460             const TypePtr* adr_type = m->adr_type();
2461             int alias = get_alias_index(adr_type);
2462             Node* prev = mm->memory_at(alias);
2463             igvn.replace_input_of(m, MemNode::Memory, prev);
2464             mm->set_memory_at(alias, m);
2465           } else if (m->is_Phi()) {
2466             // We need as many new phis as there are new aliases
2467             Node* new_phi_in = MergeMemNode::make(mm);
2468             igvn.register_new_node_with_optimizer(new_phi_in);
2469             igvn.replace_input_of(m, idx, new_phi_in);
2470             if (idx == m->req()-1) {
2471               Node* r = m->in(0);
2472               for (int j = start_alias; j < num_alias_types(); j++) {
2473                 const TypePtr* adr_type = get_adr_type(j);
2474                 if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2475                   continue;
2476                 }
2477                 Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2478                 igvn.register_new_node_with_optimizer(phi);
2479                 for (uint k = 1; k < m->req(); k++) {
2480                   phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2481                 }
2482                 mm->set_memory_at(j, phi);
2483               }
2484               Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2485               igvn.register_new_node_with_optimizer(base_phi);
2486               for (uint k = 1; k < m->req(); k++) {
2487                 base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2488               }
2489               mm->set_base_memory(base_phi);
2490             }
2491           } else {
2492             // This is a MemBarCPUOrder node from
2493             // Parse::array_load()/Parse::array_store(), in the
2494             // branch that handles flat arrays hidden under
2495             // an Object[] array. We also need one new membar per
2496             // new alias to keep the unknown access that the
2497             // membars protect properly ordered with accesses to
2498             // known flat array.
2499             assert(m->is_Proj(), "projection expected");
2500             Node* ctrl = m->in(0)->in(TypeFunc::Control);
2501             igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2502             for (int j = start_alias; j < num_alias_types(); j++) {
2503               const TypePtr* adr_type = get_adr_type(j);
2504               if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2505                 continue;
2506               }
2507               MemBarNode* mb = new MemBarCPUOrderNode(this, j, nullptr);
2508               igvn.register_new_node_with_optimizer(mb);
2509               Node* mem = mm->memory_at(j);
2510               mb->init_req(TypeFunc::Control, ctrl);
2511               mb->init_req(TypeFunc::Memory, mem);
2512               ctrl = new ProjNode(mb, TypeFunc::Control);
2513               igvn.register_new_node_with_optimizer(ctrl);
2514               mem = new ProjNode(mb, TypeFunc::Memory);
2515               igvn.register_new_node_with_optimizer(mem);
2516               mm->set_memory_at(j, mem);
2517             }
2518             igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2519           }
2520           if (idx < m->req()-1) {
2521             idx += 1;
2522             stack.set_index(idx);
2523             n = m->in(idx);
2524             break;
2525           }
2526           // Take care of place holder nodes
2527           if (m->has_out_with(Op_Node)) {
2528             Node* place_holder = m->find_out_with(Op_Node);
2529             if (place_holder != nullptr) {
2530               Node* mm_clone = mm->clone();
2531               igvn.register_new_node_with_optimizer(mm_clone);
2532               Node* hook = new Node(1);
2533               hook->init_req(0, mm);
2534               igvn.replace_node(place_holder, mm_clone);
2535               hook->destruct(&igvn);
2536             }
2537             assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2538           }
2539           stack.pop();
2540         }
2541       }
2542     } while(stack.size() > 0);
2543     // Fix the memory state at the MergeMem we started from
2544     igvn.rehash_node_delayed(current);
2545     for (int j = start_alias; j < num_alias_types(); j++) {
2546       const TypePtr* adr_type = get_adr_type(j);
2547       if (!adr_type->isa_aryptr() || !adr_type->is_flat()) {
2548         continue;
2549       }
2550       current->set_memory_at(j, mm);
2551     }
2552     current->set_memory_at(index, current->base_memory());
2553   }
2554   igvn.optimize();
2555 
2556 #ifdef ASSERT
2557   wq.clear();
2558   wq.push(root());
2559   for (uint i = 0; i < wq.size(); i++) {
2560     Node* n = wq.at(i);
2561     assert(n->adr_type() != TypeAryPtr::INLINES, "should have been removed from the graph");
2562     for (uint j = 0; j < n->req(); j++) {
2563       Node* m = n->in(j);
2564       if (m != nullptr) {
2565         wq.push(m);
2566       }
2567     }
2568   }
2569 #endif
2570 
2571   print_method(PHASE_SPLIT_INLINES_ARRAY, 2);
2572 }
2573 
2574 void Compile::record_for_merge_stores_igvn(Node* n) {
2575   if (!n->for_merge_stores_igvn()) {
2576     assert(!_for_merge_stores_igvn.contains(n), "duplicate");
2577     n->add_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2578     _for_merge_stores_igvn.append(n);
2579   }
2580 }
2581 
2582 void Compile::remove_from_merge_stores_igvn(Node* n) {
2583   n->remove_flag(Node::NodeFlags::Flag_for_merge_stores_igvn);
2584   _for_merge_stores_igvn.remove(n);
2585 }
2586 
2587 // We need to wait with merging stores until RangeCheck smearing has removed the RangeChecks during
2588 // the post loops IGVN phase. If we do it earlier, then there may still be some RangeChecks between
2589 // the stores, and we merge the wrong sequence of stores.
2590 // Example:
2591 //   StoreI RangeCheck StoreI StoreI RangeCheck StoreI
2592 // Apply MergeStores:
2593 //   StoreI RangeCheck [   StoreL  ] RangeCheck StoreI

2672       assert(next_bci == iter.next_bci() || next_bci == iter.get_dest(), "wrong next_bci at unstable_if");
2673       Bytecodes::Code c = iter.cur_bc();
2674       Node* lhs = nullptr;
2675       Node* rhs = nullptr;
2676       if (c == Bytecodes::_if_acmpeq || c == Bytecodes::_if_acmpne) {
2677         lhs = unc->peek_operand(0);
2678         rhs = unc->peek_operand(1);
2679       } else if (c == Bytecodes::_ifnull || c == Bytecodes::_ifnonnull) {
2680         lhs = unc->peek_operand(0);
2681       }
2682 
2683       ResourceMark rm;
2684       const MethodLivenessResult& live_locals = method->liveness_at_bci(next_bci);
2685       assert(live_locals.is_valid(), "broken liveness info");
2686       int len = (int)live_locals.size();
2687 
2688       for (int i = 0; i < len; i++) {
2689         Node* local = unc->local(jvms, i);
2690         // kill local using the liveness of next_bci.
2691         // give up when the local looks like an operand to secure reexecution.
2692         if (!live_locals.at(i) && !local->is_top() && local != lhs && local != rhs) {
2693           uint idx = jvms->locoff() + i;
2694 #ifdef ASSERT
2695           if (PrintOpto && Verbose) {
2696             tty->print("[unstable_if] kill local#%d: ", idx);
2697             local->dump();
2698             tty->cr();
2699           }
2700 #endif
2701           igvn.replace_input_of(unc, idx, top());
2702           modified = true;
2703         }
2704       }
2705     }
2706 
2707     // keep the modified trap for late query
2708     if (modified) {
2709       trap->set_modified();
2710     } else {
2711       _unstable_if_traps.delete_at(i);
2712     }
2713   }
2714   igvn.optimize();
2715 }
2716 
2717 // StringOpts and late inlining of string methods
2718 void Compile::inline_string_calls(bool parse_time) {
2719   {
2720     // remove useless nodes to make the usage analysis simpler
2721     ResourceMark rm;
2722     PhaseRemoveUseless pru(initial_gvn(), *igvn_worklist());
2723   }
2724 
2725   {
2726     ResourceMark rm;
2727     print_method(PHASE_BEFORE_STRINGOPTS, 3);

2919 
2920   if (_string_late_inlines.length() > 0) {
2921     assert(has_stringbuilder(), "inconsistent");
2922 
2923     inline_string_calls(false);
2924 
2925     if (failing())  return;
2926 
2927     inline_incrementally_cleanup(igvn);
2928   }
2929 
2930   set_inlining_incrementally(false);
2931 }
2932 
2933 void Compile::process_late_inline_calls_no_inline(PhaseIterGVN& igvn) {
2934   // "inlining_incrementally() == false" is used to signal that no inlining is allowed
2935   // (see LateInlineVirtualCallGenerator::do_late_inline_check() for details).
2936   // Tracking and verification of modified nodes is disabled by setting "_modified_nodes == nullptr"
2937   // as if "inlining_incrementally() == true" were set.
2938   assert(inlining_incrementally() == false, "not allowed");
2939   set_strength_reduction(true);
2940 #ifdef ASSERT
2941   Unique_Node_List* modified_nodes = _modified_nodes;
2942   _modified_nodes = nullptr;
2943 #endif
2944   assert(_late_inlines.length() > 0, "sanity");
2945 
2946   if (StressIncrementalInlining) {
2947     shuffle_late_inlines();
2948   }
2949 
2950   while (_late_inlines.length() > 0) {
2951     igvn_worklist()->ensure_empty(); // should be done with igvn
2952 
2953     while (inline_incrementally_one()) {
2954       assert(!failing_internal() || failure_is_artificial(), "inconsistent");
2955     }
2956     if (failing())  return;
2957 
2958     inline_incrementally_cleanup(igvn);
2959   }
2960   DEBUG_ONLY( _modified_nodes = modified_nodes; )
2961   set_strength_reduction(false);
2962 }
2963 
2964 bool Compile::optimize_loops(PhaseIterGVN& igvn, LoopOptsMode mode) {
2965   if (_loop_opts_cnt > 0) {
2966     while (major_progress() && (_loop_opts_cnt > 0)) {
2967       TracePhase tp(_t_idealLoop);
2968       PhaseIdealLoop::optimize(igvn, mode);
2969       _loop_opts_cnt--;
2970       if (failing())  return false;
2971       if (major_progress()) {
2972         print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2973       }
2974     }
2975   }
2976   return true;
2977 }
2978 
2979 // Remove edges from "root" to each SafePoint at a backward branch.
2980 // They were inserted during parsing (see add_safepoint()) to make
2981 // infinite loops without calls or exceptions visible to root, i.e.,

3087     print_method(PHASE_ITER_GVN_AFTER_VECTOR, 2);
3088   }
3089   assert(!has_vbox_nodes(), "sanity");
3090 
3091   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
3092     Compile::TracePhase tp(_t_renumberLive);
3093     igvn_worklist()->ensure_empty(); // should be done with igvn
3094     {
3095       ResourceMark rm;
3096       PhaseRenumberLive prl(initial_gvn(), *igvn_worklist());
3097     }
3098     igvn.reset();
3099     igvn.optimize(true);
3100     if (failing()) return;
3101   }
3102 
3103   // Now that all inlining is over and no PhaseRemoveUseless will run, cut edge from root to loop
3104   // safepoints
3105   remove_root_to_sfpts_edges(igvn);
3106 
3107   // Process inline type nodes now that all inlining is over
3108   process_inline_types(igvn);
3109 
3110   adjust_flat_array_access_aliases(igvn);
3111 
3112   if (failing())  return;
3113 
3114   if (C->macro_count() > 0) {
3115     // Eliminate some macro nodes before EA to reduce analysis pressure
3116     PhaseMacroExpand mexp(igvn);
3117     mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
3118     if (failing()) {
3119       return;
3120     }
3121     igvn.set_delay_transform(false);
3122     print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3123   }
3124 
3125   _print_phase_loop_opts = has_loops();
3126   if (_print_phase_loop_opts) {
3127     print_method(PHASE_BEFORE_LOOP_OPTS, 2);
3128   }
3129 
3130   // Perform escape analysis
3131   if (do_escape_analysis() && ConnectionGraph::has_candidates(this)) {
3132     if (has_loops()) {
3133       // Cleanup graph (remove dead nodes).
3134       TracePhase tp(_t_idealLoop);
3135       PhaseIdealLoop::optimize(igvn, LoopOptsMaxUnroll);
3136       if (failing()) {
3137         return;
3138       }
3139       print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
3140       if (C->macro_count() > 0) {
3141         // Eliminate some macro nodes before EA to reduce analysis pressure
3142         PhaseMacroExpand mexp(igvn);
3143         mexp.eliminate_macro_nodes(/* eliminate_locks= */ false);
3144         if (failing()) {
3145           return;
3146         }
3147         igvn.set_delay_transform(false);
3148         print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3149       }
3150     }
3151 
3152     bool progress;

3153     do {
3154       ConnectionGraph::do_analysis(this, &igvn);
3155 
3156       if (failing())  return;
3157 
3158       int mcount = macro_count(); // Record number of allocations and locks before IGVN
3159 
3160       // Optimize out fields loads from scalar replaceable allocations.
3161       igvn.optimize(true);
3162       print_method(PHASE_ITER_GVN_AFTER_EA, 2);
3163 
3164       if (failing()) return;
3165 
3166       if (congraph() != nullptr && macro_count() > 0) {
3167         TracePhase tp(_t_macroEliminate);
3168         PhaseMacroExpand mexp(igvn);
3169         mexp.eliminate_macro_nodes();
3170         if (failing()) {
3171           return;
3172         }
3173         print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
3174 
3175         igvn.set_delay_transform(false);



3176         print_method(PHASE_ITER_GVN_AFTER_ELIMINATION, 2);
3177       }
3178 
3179       ConnectionGraph::verify_ram_nodes(this, root());
3180       if (failing())  return;
3181 
3182       progress = do_iterative_escape_analysis() &&
3183                  (macro_count() < mcount) &&
3184                  ConnectionGraph::has_candidates(this);
3185       // Try again if candidates exist and made progress
3186       // by removing some allocations and/or locks.
3187     } while (progress);
3188   }
3189 
3190   process_flat_accesses(igvn);
3191   if (failing()) {
3192     return;
3193   }
3194 
3195   // Loop transforms on the ideal graph.  Range Check Elimination,
3196   // peeling, unrolling, etc.
3197 
3198   // Set loop opts counter
3199   if((_loop_opts_cnt > 0) && (has_loops() || has_split_ifs())) {
3200     {
3201       TracePhase tp(_t_idealLoop);
3202       PhaseIdealLoop::optimize(igvn, LoopOptsDefault);
3203       _loop_opts_cnt--;
3204       if (major_progress()) print_method(PHASE_PHASEIDEALLOOP1, 2);
3205       if (failing())  return;
3206     }
3207     // Loop opts pass if partial peeling occurred in previous pass
3208     if(PartialPeelLoop && major_progress() && (_loop_opts_cnt > 0)) {
3209       TracePhase tp(_t_idealLoop);
3210       PhaseIdealLoop::optimize(igvn, LoopOptsSkipSplitIf);
3211       _loop_opts_cnt--;
3212       if (major_progress()) print_method(PHASE_PHASEIDEALLOOP2, 2);
3213       if (failing())  return;
3214     }

3262 
3263   // Once loop optimizations are over, it is safe to get rid of all reachability fence nodes and
3264   // migrate reachability edges to safepoints.
3265   if (OptimizeReachabilityFences && _reachability_fences.length() > 0) {
3266     TracePhase tp1(_t_idealLoop);
3267     TracePhase tp2(_t_reachability);
3268     PhaseIdealLoop::optimize(igvn, PostLoopOptsExpandReachabilityFences);
3269     print_method(PHASE_EXPAND_REACHABILITY_FENCES, 2);
3270     if (failing())  return;
3271     assert(_reachability_fences.length() == 0 || PreserveReachabilityFencesOnConstants, "no RF nodes allowed");
3272   }
3273 
3274   process_for_merge_stores_igvn(igvn);
3275 
3276   if (failing())  return;
3277 
3278 #ifdef ASSERT
3279   bs->verify_gc_barriers(this, BarrierSetC2::BeforeMacroExpand);
3280 #endif
3281 
3282   if (_late_inlines.length() > 0) {
3283     // More opportunities to optimize virtual and MH calls.
3284     // Though it's maybe too late to perform inlining, strength-reducing them to direct calls is still an option.
3285     process_late_inline_calls_no_inline(igvn);
3286     if (failing()) {
3287       return;
3288     }
3289   }
3290   assert(_late_inlines.length() == 0, "late inline queue must be drained");
3291 
3292   // Process inline types before macro expansion. Otherwise, we will not be able to
3293   // remove unused allocations because it cannot match the expanded allocation.
3294   process_inline_types(igvn);
3295 
3296   {
3297     TracePhase tp(_t_macroExpand);
3298     PhaseMacroExpand mex(igvn);
3299     // Last attempt to eliminate macro nodes.
3300     mex.eliminate_macro_nodes();
3301     if (failing()) {
3302       return;
3303     }
3304 
3305     print_method(PHASE_BEFORE_MACRO_EXPANSION, 3);

3306     // Do not allow new macro nodes once we start to eliminate and expand
3307     C->reset_allow_macro_nodes();
3308     // Last attempt to eliminate macro nodes before expand
3309     mex.eliminate_macro_nodes();
3310     if (failing()) {
3311       return;
3312     }
3313     mex.eliminate_opaque_looplimit_macro_nodes();
3314     if (failing()) {
3315       return;
3316     }
3317     print_method(PHASE_AFTER_MACRO_ELIMINATION, 2);
3318     if (mex.expand_macro_nodes()) {
3319       assert(failing(), "must bail out w/ explicit message");
3320       return;
3321     }
3322     print_method(PHASE_AFTER_MACRO_EXPANSION, 2);
3323   }
3324 
3325   // Process inline type nodes again and remove them. From here
3326   // on we don't need to keep track of field values anymore.
3327   process_inline_types(igvn, /* remove= */ true);
3328 
3329   {
3330     TracePhase tp(_t_barrierExpand);
3331     if (bs->expand_barriers(this, igvn)) {
3332       assert(failing(), "must bail out w/ explicit message");
3333       return;
3334     }
3335     print_method(PHASE_BARRIER_EXPANSION, 2);
3336   }
3337 
3338   if (C->max_vector_size() > 0) {
3339     C->optimize_logic_cones(igvn);
3340     igvn.optimize();
3341     if (failing()) return;
3342   }
3343 
3344   DEBUG_ONLY( _modified_nodes = nullptr; )
3345   DEBUG_ONLY( _late_inlines.clear(); )
3346 
3347   assert(igvn._worklist.size() == 0, "not empty");








3348  } // (End scope of igvn; run destructor if necessary for asserts.)
3349 
3350  check_no_dead_use();
3351 
3352  // We will never use the NodeHash table any more. Clear it so that final_graph_reshaping does not have
3353  // to remove hashes to unlock nodes for modifications.
3354  C->node_hash()->clear();
3355 
3356  // A method with only infinite loops has no edges entering loops from root
3357  {
3358    TracePhase tp(_t_graphReshaping);
3359    if (final_graph_reshaping()) {
3360      assert(failing(), "must bail out w/ explicit message");
3361      return;
3362    }
3363  }
3364 
3365  print_method(PHASE_OPTIMIZE_FINISHED, 2);
3366  DEBUG_ONLY(set_phase_optimize_finished();)
3367 }

4026     n->subsume_by(sub, this);
4027   }
4028 }
4029 
4030 void Compile::final_graph_reshaping_main_switch(Node* n, Final_Reshape_Counts& frc, uint nop, Unique_Node_List& dead_nodes) {
4031   switch( nop ) {
4032   case Op_Opaque1:              // Remove Opaque Nodes before matching
4033     n->subsume_by(n->in(1), this);
4034     break;
4035   case Op_CallLeafPure: {
4036     // If the pure call is not supported, then lower to a CallLeaf.
4037     if (!Matcher::match_rule_supported(Op_CallLeafPure)) {
4038       CallNode* call = n->as_Call();
4039       CallNode* new_call = new CallLeafNode(call->tf(), call->entry_point(),
4040                                             call->_name, TypeRawPtr::BOTTOM);
4041       new_call->init_req(TypeFunc::Control, call->in(TypeFunc::Control));
4042       new_call->init_req(TypeFunc::I_O, C->top());
4043       new_call->init_req(TypeFunc::Memory, C->top());
4044       new_call->init_req(TypeFunc::ReturnAdr, C->top());
4045       new_call->init_req(TypeFunc::FramePtr, C->top());
4046       for (unsigned int i = TypeFunc::Parms; i < call->tf()->domain_sig()->cnt(); i++) {
4047         new_call->init_req(i, call->in(i));
4048       }
4049       n->subsume_by(new_call, this);
4050     }
4051     break;
4052   }
4053   case Op_CallStaticJava:
4054   case Op_CallJava:
4055   case Op_CallDynamicJava:
4056     frc.inc_java_call_count(); // Count java call site;
4057   case Op_CallRuntime:
4058   case Op_CallLeaf:
4059   case Op_CallLeafVector:
4060   case Op_CallLeafNoFP: {
4061     assert (n->is_Call(), "");
4062     CallNode *call = n->as_Call();
4063     // See if uncommon argument is shared
4064     if (call->is_CallStaticJava() && call->as_CallStaticJava()->_name) {
4065       Node *n = call->in(TypeFunc::Parms);
4066       int nop = n->Opcode();

4073           nop != Op_DecodeNKlass &&
4074           !n->is_Mem() &&
4075           !n->is_Phi()) {
4076         Node *x = n->clone();
4077         call->set_req(TypeFunc::Parms, x);
4078       }
4079     }
4080     break;
4081   }
4082 
4083   // Mem nodes need explicit cases to satisfy assert(!n->is_Mem()) in default.
4084   case Op_StoreF:
4085   case Op_LoadF:
4086   case Op_StoreD:
4087   case Op_LoadD:
4088   case Op_LoadD_unaligned:
4089   case Op_StoreB:
4090   case Op_StoreC:
4091   case Op_StoreI:
4092   case Op_StoreL:
4093   case Op_StoreLSpecial:
4094   case Op_CompareAndSwapB:
4095   case Op_CompareAndSwapS:
4096   case Op_CompareAndSwapI:
4097   case Op_CompareAndSwapL:
4098   case Op_CompareAndSwapP:
4099   case Op_CompareAndSwapN:
4100   case Op_WeakCompareAndSwapB:
4101   case Op_WeakCompareAndSwapS:
4102   case Op_WeakCompareAndSwapI:
4103   case Op_WeakCompareAndSwapL:
4104   case Op_WeakCompareAndSwapP:
4105   case Op_WeakCompareAndSwapN:
4106   case Op_CompareAndExchangeB:
4107   case Op_CompareAndExchangeS:
4108   case Op_CompareAndExchangeI:
4109   case Op_CompareAndExchangeL:
4110   case Op_CompareAndExchangeP:
4111   case Op_CompareAndExchangeN:
4112   case Op_GetAndAddS:
4113   case Op_GetAndAddB:

4605           k->subsume_by(m, this);
4606         }
4607       }
4608     }
4609     break;
4610   }
4611   case Op_CmpUL: {
4612     if (!Matcher::has_match_rule(Op_CmpUL)) {
4613       // No support for unsigned long comparisons
4614       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
4615       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
4616       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
4617       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
4618       Node* andl = new AndLNode(orl, remove_sign_mask);
4619       Node* cmp = new CmpLNode(andl, n->in(2));
4620       n->subsume_by(cmp, this);
4621     }
4622     break;
4623   }
4624 #ifdef ASSERT
4625   case Op_InlineType: {
4626     n->dump(-1);
4627     assert(false, "inline type node was not removed");
4628     break;
4629   }
4630   case Op_ConNKlass: {
4631     const TypePtr* tp = n->as_Type()->type()->make_ptr();
4632     ciKlass* klass = tp->is_klassptr()->exact_klass();
4633     assert(klass->is_in_encoding_range(), "klass cannot be compressed");
4634     break;
4635   }
4636 #endif
4637   default:
4638     assert(!n->is_Call(), "");
4639     assert(!n->is_Mem(), "");
4640     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4641     break;
4642   }
4643 }
4644 
4645 //------------------------------final_graph_reshaping_walk---------------------
4646 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4647 // requires that the walk visits a node's inputs before visiting the node.
4648 void Compile::final_graph_reshaping_walk(Node_Stack& nstack, Node* root, Final_Reshape_Counts& frc, Unique_Node_List& dead_nodes) {
4649   Unique_Node_List sfpt;

4982   }
4983 }
4984 
4985 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4986   return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4987 }
4988 
4989 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4990   return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4991 }
4992 
4993 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4994   if (holder->is_initialized()) {
4995     return false;
4996   }
4997   if (holder->is_being_initialized()) {
4998     if (accessing_method->holder() == holder) {
4999       // Access inside a class. The barrier can be elided when access happens in <clinit>,
5000       // <init>, or a static method. In all those cases, there was an initialization
5001       // barrier on the holder klass passed.
5002       if (accessing_method->is_class_initializer() ||
5003           accessing_method->is_object_constructor() ||
5004           accessing_method->is_static()) {
5005         return false;
5006       }
5007     } else if (accessing_method->holder()->is_subclass_of(holder)) {
5008       // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
5009       // In case of <init> or a static method, the barrier is on the subclass is not enough:
5010       // child class can become fully initialized while its parent class is still being initialized.
5011       if (accessing_method->is_class_initializer()) {
5012         return false;
5013       }
5014     }
5015     ciMethod* root = method(); // the root method of compilation
5016     if (root != accessing_method) {
5017       return needs_clinit_barrier(holder, root); // check access in the context of compilation root
5018     }
5019   }
5020   return true;
5021 }
5022 
5023 #ifndef PRODUCT
5024 //------------------------------verify_bidirectional_edges---------------------
5025 // For each input edge to a node (ie - for each Use-Def edge), verify that
5026 // there is a corresponding Def-Use edge.
5027 void Compile::verify_bidirectional_edges(Unique_Node_List& visited, const Unique_Node_List* root_and_safepoints) const {
5028   // Allocate stack of size C->live_nodes()/16 to avoid frequent realloc
5029   uint stack_size = live_nodes() >> 4;
5030   Node_List nstack(MAX2(stack_size, (uint) OptoNodeListSize));
5031   if (root_and_safepoints != nullptr) {

5061       if (in != nullptr && !in->is_top()) {
5062         // Count instances of `next`
5063         int cnt = 0;
5064         for (uint idx = 0; idx < in->_outcnt; idx++) {
5065           if (in->_out[idx] == n) {
5066             cnt++;
5067           }
5068         }
5069         assert(cnt > 0, "Failed to find Def-Use edge.");
5070         // Check for duplicate edges
5071         // walk the input array downcounting the input edges to n
5072         for (uint j = 0; j < length; j++) {
5073           if (n->in(j) == in) {
5074             cnt--;
5075           }
5076         }
5077         assert(cnt == 0, "Mismatched edge count.");
5078       } else if (in == nullptr) {
5079         assert(i == 0 || i >= n->req() ||
5080                n->is_Region() || n->is_Phi() || n->is_ArrayCopy() ||
5081                (n->is_Allocate() && i >= AllocateNode::InlineType) ||
5082                (n->is_Unlock() && i == (n->req() - 1)) ||
5083                (n->is_MemBar() && i == 5), // the precedence edge to a membar can be removed during macro node expansion
5084               "only region, phi, arraycopy, allocate, unlock or membar nodes have null data edges");
5085       } else {
5086         assert(in->is_top(), "sanity");
5087         // Nothing to check.
5088       }
5089     }
5090   }
5091 }
5092 
5093 //------------------------------verify_graph_edges---------------------------
5094 // Walk the Graph and verify that there is a one-to-one correspondence
5095 // between Use-Def edges and Def-Use edges in the graph.
5096 void Compile::verify_graph_edges(bool no_dead_code, const Unique_Node_List* root_and_safepoints) const {
5097   if (VerifyGraphEdges) {
5098     Unique_Node_List visited;
5099 
5100     // Call graph walk to check edges
5101     verify_bidirectional_edges(visited, root_and_safepoints);
5102     if (no_dead_code) {
5103       // Now make sure that no visited node is used by an unvisited node.
5104       bool dead_nodes = false;

5215 // (1) subklass is already limited to a subtype of superklass => always ok
5216 // (2) subklass does not overlap with superklass => always fail
5217 // (3) superklass has NO subtypes and we can check with a simple compare.
5218 Compile::SubTypeCheckResult Compile::static_subtype_check(const TypeKlassPtr* superk, const TypeKlassPtr* subk, bool skip) {
5219   if (skip) {
5220     return SSC_full_test;       // Let caller generate the general case.
5221   }
5222 
5223   if (subk->is_java_subtype_of(superk)) {
5224     return SSC_always_true; // (0) and (1)  this test cannot fail
5225   }
5226 
5227   if (!subk->maybe_java_subtype_of(superk)) {
5228     return SSC_always_false; // (2) true path dead; no dynamic test needed
5229   }
5230 
5231   const Type* superelem = superk;
5232   if (superk->isa_aryklassptr()) {
5233     int ignored;
5234     superelem = superk->is_aryklassptr()->base_element_type(ignored);
5235 
5236     // Do not fold the subtype check to an array klass pointer comparison for null-able inline type arrays
5237     // because null-free [LMyValue <: null-able [LMyValue but the klasses are different. Perform a full test.
5238     if (!superk->is_aryklassptr()->is_null_free() && superk->is_aryklassptr()->elem()->isa_instklassptr() &&
5239         superk->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->is_inlinetype()) {
5240       return SSC_full_test;
5241     }
5242   }
5243 
5244   if (superelem->isa_instklassptr()) {
5245     ciInstanceKlass* ik = superelem->is_instklassptr()->instance_klass();
5246     if (!ik->has_subklass()) {
5247       if (!ik->is_final()) {
5248         // Add a dependency if there is a chance of a later subclass.
5249         dependencies()->assert_leaf_type(ik);
5250       }
5251       if (!superk->maybe_java_subtype_of(subk)) {
5252         return SSC_always_false;
5253       }
5254       return SSC_easy_test;     // (3) caller can do a simple ptr comparison
5255     }
5256   } else {
5257     // A primitive array type has no subtypes.
5258     return SSC_easy_test;       // (3) caller can do a simple ptr comparison
5259   }
5260 
5261   return SSC_full_test;

6054   } else {
6055     _debug_network_printer->update_compiled_method(C->method());
6056   }
6057   tty->print_cr("Method printed over network stream to IGV");
6058   _debug_network_printer->print(name, C->root(), visible_nodes, fr);
6059 }
6060 #endif // !PRODUCT
6061 
6062 Node* Compile::narrow_value(BasicType bt, Node* value, const Type* type, PhaseGVN* phase, bool transform_res) {
6063   if (type != nullptr && phase->type(value)->higher_equal(type)) {
6064     return value;
6065   }
6066   Node* result = nullptr;
6067   if (bt == T_BYTE) {
6068     result = phase->transform(new LShiftINode(value, phase->intcon(24)));
6069     result = new RShiftINode(result, phase->intcon(24));
6070   } else if (bt == T_BOOLEAN) {
6071     result = new AndINode(value, phase->intcon(0xFF));
6072   } else if (bt == T_CHAR) {
6073     result = new AndINode(value,phase->intcon(0xFFFF));
6074   } else if (bt == T_FLOAT) {
6075     result = new MoveI2FNode(value);
6076   } else {
6077     assert(bt == T_SHORT, "unexpected narrow type");
6078     result = phase->transform(new LShiftINode(value, phase->intcon(16)));
6079     result = new RShiftINode(result, phase->intcon(16));
6080   }
6081   if (transform_res) {
6082     result = phase->transform(result);
6083   }
6084   return result;
6085 }
6086 
6087 void Compile::record_method_not_compilable_oom() {
6088   record_method_not_compilable(CompilationMemoryStatistic::failure_reason_memlimit());
6089 }
6090 
6091 #ifndef PRODUCT
6092 // Collects all the control inputs from nodes on the worklist and from their data dependencies
6093 static void find_candidate_control_inputs(Unique_Node_List& worklist, Unique_Node_List& candidates) {
6094   // Follow non-control edges until we reach CFG nodes
6095   for (uint i = 0; i < worklist.size(); i++) {
< prev index next >