< prev index next >

src/hotspot/share/opto/compile.cpp

Print this page




  50 #include "opto/divnode.hpp"
  51 #include "opto/escape.hpp"
  52 #include "opto/idealGraphPrinter.hpp"
  53 #include "opto/loopnode.hpp"
  54 #include "opto/machnode.hpp"
  55 #include "opto/macro.hpp"
  56 #include "opto/matcher.hpp"
  57 #include "opto/mathexactnode.hpp"
  58 #include "opto/memnode.hpp"
  59 #include "opto/mulnode.hpp"
  60 #include "opto/narrowptrnode.hpp"
  61 #include "opto/node.hpp"
  62 #include "opto/opcodes.hpp"
  63 #include "opto/output.hpp"
  64 #include "opto/parse.hpp"
  65 #include "opto/phaseX.hpp"
  66 #include "opto/rootnode.hpp"
  67 #include "opto/runtime.hpp"
  68 #include "opto/stringopts.hpp"
  69 #include "opto/type.hpp"

  70 #include "opto/vectornode.hpp"
  71 #include "runtime/arguments.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/stubRoutines.hpp"
  75 #include "runtime/timer.hpp"
  76 #include "utilities/align.hpp"
  77 #include "utilities/copy.hpp"
  78 #include "utilities/macros.hpp"
  79 #if INCLUDE_ZGC
  80 #include "gc/z/c2/zBarrierSetC2.hpp"
  81 #endif
  82 
  83 
  84 // -------------------- Compile::mach_constant_base_node -----------------------
  85 // Constant table base node singleton.
  86 MachConstantBaseNode* Compile::mach_constant_base_node() {
  87   if (_mach_constant_base_node == NULL) {
  88     _mach_constant_base_node = new MachConstantBaseNode();
  89     _mach_constant_base_node->add_req(C->root());


 400   for (int i = range_check_cast_count() - 1; i >= 0; i--) {
 401     Node* cast = range_check_cast_node(i);
 402     if (!useful.member(cast)) {
 403       remove_range_check_cast(cast);
 404     }
 405   }
 406   // Remove useless expensive nodes
 407   for (int i = C->expensive_count()-1; i >= 0; i--) {
 408     Node* n = C->expensive_node(i);
 409     if (!useful.member(n)) {
 410       remove_expensive_node(n);
 411     }
 412   }
 413   // Remove useless Opaque4 nodes
 414   for (int i = opaque4_count() - 1; i >= 0; i--) {
 415     Node* opaq = opaque4_node(i);
 416     if (!useful.member(opaq)) {
 417       remove_opaque4_node(opaq);
 418     }
 419   }




 420   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 421   bs->eliminate_useless_gc_barriers(useful, this);
 422   // clean up the late inline lists
 423   remove_useless_late_inlines(&_string_late_inlines, useful);
 424   remove_useless_late_inlines(&_boxing_late_inlines, useful);
 425   remove_useless_late_inlines(&_late_inlines, useful);
 426   debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
 427 }
 428 
 429 //------------------------------frame_size_in_words-----------------------------
 430 // frame_slots in units of words
 431 int Compile::frame_size_in_words() const {
 432   // shift is 0 in LP32 and 1 in LP64
 433   const int shift = (LogBytesPerWord - LogBytesPerInt);
 434   int words = _frame_slots >> shift;
 435   assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" );
 436   return words;
 437 }
 438 
 439 // To bang the stack of this compiled method we use the stack size


 448 //------------------------------CompileWrapper---------------------------------
 449 class CompileWrapper : public StackObj {
 450   Compile *const _compile;
 451  public:
 452   CompileWrapper(Compile* compile);
 453 
 454   ~CompileWrapper();
 455 };
 456 
 457 CompileWrapper::CompileWrapper(Compile* compile) : _compile(compile) {
 458   // the Compile* pointer is stored in the current ciEnv:
 459   ciEnv* env = compile->env();
 460   assert(env == ciEnv::current(), "must already be a ciEnv active");
 461   assert(env->compiler_data() == NULL, "compile already active?");
 462   env->set_compiler_data(compile);
 463   assert(compile == Compile::current(), "sanity");
 464 
 465   compile->set_type_dict(NULL);
 466   compile->set_clone_map(new Dict(cmpkey, hashkey, _compile->comp_arena()));
 467   compile->clone_map().set_clone_idx(0);
 468   compile->set_type_hwm(NULL);
 469   compile->set_type_last_size(0);
 470   compile->set_last_tf(NULL, NULL);
 471   compile->set_indexSet_arena(NULL);
 472   compile->set_indexSet_free_block_list(NULL);
 473   compile->init_type_arena();
 474   Type::Initialize(compile);
 475   _compile->set_scratch_buffer_blob(NULL);
 476   _compile->begin_method();
 477   _compile->clone_map().set_debug(_compile->has_method() && _compile->directive()->CloneMapDebugOption);
 478 }
 479 CompileWrapper::~CompileWrapper() {
 480   _compile->end_method();
 481   if (_compile->scratch_buffer_blob() != NULL)
 482     BufferBlob::free(_compile->scratch_buffer_blob());
 483   _compile->env()->set_compiler_data(NULL);
 484 }
 485 
 486 
 487 //----------------------------print_compile_messages---------------------------
 488 void Compile::print_compile_messages() {


 525 }
 526 
 527 
 528 //-----------------------init_scratch_buffer_blob------------------------------
 529 // Construct a temporary BufferBlob and cache it for this compile.
 530 void Compile::init_scratch_buffer_blob(int const_size) {
 531   // If there is already a scratch buffer blob allocated and the
 532   // constant section is big enough, use it.  Otherwise free the
 533   // current and allocate a new one.
 534   BufferBlob* blob = scratch_buffer_blob();
 535   if ((blob != NULL) && (const_size <= _scratch_const_size)) {
 536     // Use the current blob.
 537   } else {
 538     if (blob != NULL) {
 539       BufferBlob::free(blob);
 540     }
 541 
 542     ResourceMark rm;
 543     _scratch_const_size = const_size;
 544     int size = C2Compiler::initial_code_buffer_size(const_size);






 545     blob = BufferBlob::create("Compile::scratch_buffer", size);
 546     // Record the buffer blob for next time.
 547     set_scratch_buffer_blob(blob);
 548     // Have we run out of code space?
 549     if (scratch_buffer_blob() == NULL) {
 550       // Let CompilerBroker disable further compilations.
 551       record_failure("Not enough space for scratch buffer in CodeCache");
 552       return;
 553     }
 554   }
 555 
 556   // Initialize the relocation buffers
 557   relocInfo* locs_buf = (relocInfo*) blob->content_end() - MAX_locs_size;
 558   set_scratch_locs_memory(locs_buf);
 559 }
 560 
 561 
 562 //-----------------------scratch_emit_size-------------------------------------
 563 // Helper function that computes size by emitting code
 564 uint Compile::scratch_emit_size(const Node* n) {


 589   int lsize = MAX_locs_size / 3;
 590   buf.consts()->initialize_shared_locs(&locs_buf[lsize * 0], lsize);
 591   buf.insts()->initialize_shared_locs( &locs_buf[lsize * 1], lsize);
 592   buf.stubs()->initialize_shared_locs( &locs_buf[lsize * 2], lsize);
 593   // Mark as scratch buffer.
 594   buf.consts()->set_scratch_emit();
 595   buf.insts()->set_scratch_emit();
 596   buf.stubs()->set_scratch_emit();
 597 
 598   // Do the emission.
 599 
 600   Label fakeL; // Fake label for branch instructions.
 601   Label*   saveL = NULL;
 602   uint save_bnum = 0;
 603   bool is_branch = n->is_MachBranch();
 604   if (is_branch) {
 605     MacroAssembler masm(&buf);
 606     masm.bind(fakeL);
 607     n->as_MachBranch()->save_label(&saveL, &save_bnum);
 608     n->as_MachBranch()->label_set(&fakeL, 0);



 609   }
 610   n->emit(buf, this->regalloc());
 611 
 612   // Emitting into the scratch buffer should not fail
 613   assert (!failing(), "Must not have pending failure. Reason is: %s", failure_reason());
 614 
 615   if (is_branch) // Restore label.

 616     n->as_MachBranch()->label_set(saveL, save_bnum);



 617 
 618   // End scratch_emit_size section.
 619   set_in_scratch_emit_size(false);
 620 
 621   return buf.insts_size();
 622 }
 623 
 624 
 625 // ============================================================================
 626 //------------------------------Compile standard-------------------------------
 627 debug_only( int Compile::_debug_idx = 100000; )
 628 
 629 // Compile a method.  entry_bci is -1 for normal compilations and indicates
 630 // the continuation bci for on stack replacement.
 631 
 632 
 633 Compile::Compile( ciEnv* ci_env, C2Compiler* compiler, ciMethod* target, int osr_bci,
 634                   bool subsume_loads, bool do_escape_analysis, bool eliminate_boxing, DirectiveSet* directive)
 635                 : Phase(Compiler),
 636                   _compile_id(ci_env->compile_id()),
 637                   _save_argument_registers(false),
 638                   _subsume_loads(subsume_loads),
 639                   _do_escape_analysis(do_escape_analysis),
 640                   _eliminate_boxing(eliminate_boxing),
 641                   _method(target),
 642                   _entry_bci(osr_bci),
 643                   _stub_function(NULL),
 644                   _stub_name(NULL),
 645                   _stub_entry_point(NULL),
 646                   _max_node_limit(MaxNodeLimit),
 647                   _orig_pc_slot(0),
 648                   _orig_pc_slot_offset_in_bytes(0),


 649                   _inlining_progress(false),
 650                   _inlining_incrementally(false),
 651                   _do_cleanup(false),
 652                   _has_reserved_stack_access(target->has_reserved_stack_access()),
 653 #ifndef PRODUCT
 654                   _trace_opto_output(directive->TraceOptoOutputOption),
 655 #endif
 656                   _has_method_handle_invokes(false),
 657                   _clinit_barrier_on_entry(false),
 658                   _comp_arena(mtCompiler),
 659                   _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 660                   _env(ci_env),
 661                   _directive(directive),
 662                   _log(ci_env->log()),
 663                   _failure_reason(NULL),
 664                   _congraph(NULL),
 665 #ifndef PRODUCT
 666                   _printer(IdealGraphPrinter::printer()),
 667 #endif
 668                   _dead_node_list(comp_arena()),


 768   print_inlining_init();
 769   { // Scope for timing the parser
 770     TracePhase tp("parse", &timers[_t_parser]);
 771 
 772     // Put top into the hash table ASAP.
 773     initial_gvn()->transform_no_reclaim(top());
 774 
 775     // Set up tf(), start(), and find a CallGenerator.
 776     CallGenerator* cg = NULL;
 777     if (is_osr_compilation()) {
 778       const TypeTuple *domain = StartOSRNode::osr_domain();
 779       const TypeTuple *range = TypeTuple::make_range(method()->signature());
 780       init_tf(TypeFunc::make(domain, range));
 781       StartNode* s = new StartOSRNode(root(), domain);
 782       initial_gvn()->set_type_bottom(s);
 783       init_start(s);
 784       cg = CallGenerator::for_osr(method(), entry_bci());
 785     } else {
 786       // Normal case.
 787       init_tf(TypeFunc::make(method()));
 788       StartNode* s = new StartNode(root(), tf()->domain());
 789       initial_gvn()->set_type_bottom(s);
 790       init_start(s);
 791       if (method()->intrinsic_id() == vmIntrinsics::_Reference_get) {
 792         // With java.lang.ref.reference.get() we must go through the
 793         // intrinsic - even when get() is the root
 794         // method of the compile - so that, if necessary, the value in
 795         // the referent field of the reference object gets recorded by
 796         // the pre-barrier code.
 797         cg = find_intrinsic(method(), false);
 798       }
 799       if (cg == NULL) {
 800         float past_uses = method()->interpreter_invocation_count();
 801         float expected_uses = past_uses;
 802         cg = CallGenerator::for_inline(method(), expected_uses);
 803       }
 804     }
 805     if (failing())  return;
 806     if (cg == NULL) {
 807       record_method_not_compilable("cannot parse method");
 808       return;


 893     }
 894   }
 895 #endif
 896 
 897 #ifdef ASSERT
 898   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 899   bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
 900 #endif
 901 
 902   // Dump compilation data to replay it.
 903   if (directive->DumpReplayOption) {
 904     env()->dump_replay_data(_compile_id);
 905   }
 906   if (directive->DumpInlineOption && (ilt() != NULL)) {
 907     env()->dump_inline_data(_compile_id);
 908   }
 909 
 910   // Now that we know the size of all the monitors we can add a fixed slot
 911   // for the original deopt pc.
 912 
 913   _orig_pc_slot =  fixed_slots();
 914   int next_slot = _orig_pc_slot + (sizeof(address) / VMRegImpl::stack_slot_size);







 915   set_fixed_slots(next_slot);
 916 
 917   // Compute when to use implicit null checks. Used by matching trap based
 918   // nodes and NullCheck optimization.
 919   set_allowed_deopt_reasons();
 920 
 921   // Now generate code
 922   Code_Gen();
 923   if (failing())  return;
 924 
 925   // Check if we want to skip execution of all compiled code.
 926   {
 927 #ifndef PRODUCT
 928     if (OptoNoExecute) {
 929       record_method_not_compilable("+OptoNoExecute");  // Flag as failed
 930       return;
 931     }
 932 #endif
 933     TracePhase tp("install_code", &timers[_t_registerMethod]);
 934 
 935     if (is_osr_compilation()) {
 936       _code_offsets.set_value(CodeOffsets::Verified_Entry, 0);
 937       _code_offsets.set_value(CodeOffsets::OSR_Entry, _first_block_size);
 938     } else {
 939       _code_offsets.set_value(CodeOffsets::Verified_Entry, _first_block_size);









 940       _code_offsets.set_value(CodeOffsets::OSR_Entry, 0);
 941     }
 942 
 943     env()->register_method(_method, _entry_bci,
 944                            &_code_offsets,
 945                            _orig_pc_slot_offset_in_bytes,
 946                            code_buffer(),
 947                            frame_size_in_words(), _oop_map_set,
 948                            &_handler_table, &_inc_table,
 949                            compiler,
 950                            has_unsafe_access(),
 951                            SharedRuntime::is_wide_vector(max_vector_size()),
 952                            rtm_state()
 953                            );
 954 
 955     if (log() != NULL) // Print code cache state into compiler log
 956       log()->code_cache_state();
 957   }
 958 }
 959 


 965                   const char *stub_name,
 966                   int is_fancy_jump,
 967                   bool pass_tls,
 968                   bool save_arg_registers,
 969                   bool return_pc,
 970                   DirectiveSet* directive)
 971   : Phase(Compiler),
 972     _compile_id(0),
 973     _save_argument_registers(save_arg_registers),
 974     _subsume_loads(true),
 975     _do_escape_analysis(false),
 976     _eliminate_boxing(false),
 977     _method(NULL),
 978     _entry_bci(InvocationEntryBci),
 979     _stub_function(stub_function),
 980     _stub_name(stub_name),
 981     _stub_entry_point(NULL),
 982     _max_node_limit(MaxNodeLimit),
 983     _orig_pc_slot(0),
 984     _orig_pc_slot_offset_in_bytes(0),


 985     _inlining_progress(false),
 986     _inlining_incrementally(false),
 987     _has_reserved_stack_access(false),
 988 #ifndef PRODUCT
 989     _trace_opto_output(directive->TraceOptoOutputOption),
 990 #endif
 991     _has_method_handle_invokes(false),
 992     _clinit_barrier_on_entry(false),
 993     _comp_arena(mtCompiler),
 994     _env(ci_env),
 995     _directive(directive),
 996     _log(ci_env->log()),
 997     _failure_reason(NULL),
 998     _congraph(NULL),
 999 #ifndef PRODUCT
1000     _printer(NULL),
1001 #endif
1002     _dead_node_list(comp_arena()),
1003     _dead_node_count(0),
1004     _node_arena(mtCompiler),


1114   // Create Debug Information Recorder to record scopes, oopmaps, etc.
1115   env()->set_oop_recorder(new OopRecorder(env()->arena()));
1116   env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));
1117   env()->set_dependencies(new Dependencies(env()));
1118 
1119   _fixed_slots = 0;
1120   set_has_split_ifs(false);
1121   set_has_loops(has_method() && method()->has_loops()); // first approximation
1122   set_has_stringbuilder(false);
1123   set_has_boxed_value(false);
1124   _trap_can_recompile = false;  // no traps emitted yet
1125   _major_progress = true; // start out assuming good things will happen
1126   set_has_unsafe_access(false);
1127   set_max_vector_size(0);
1128   set_clear_upper_avx(false);  //false as default for clear upper bits of ymm registers
1129   Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1130   set_decompile_count(0);
1131 
1132   set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1133   _loop_opts_cnt = LoopOptsCount;



1134   set_do_inlining(Inline);
1135   set_max_inline_size(MaxInlineSize);
1136   set_freq_inline_size(FreqInlineSize);
1137   set_do_scheduling(OptoScheduling);
1138   set_do_count_invocations(false);
1139   set_do_method_data_update(false);
1140 
1141   set_do_vector_loop(false);
1142 
1143   if (AllowVectorizeOnDemand) {
1144     if (has_method() && (_directive->VectorizeOption || _directive->VectorizeDebugOption)) {
1145       set_do_vector_loop(true);
1146       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());})
1147     } else if (has_method() && method()->name() != 0 &&
1148                method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1149       set_do_vector_loop(true);
1150     }
1151   }
1152   set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1153   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());})


1197   Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);
1198   {
1199     for (int i = 0; i < grow_ats; i++)  _alias_types[i] = &ats[i];
1200   }
1201   // Initialize the first few types.
1202   _alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);
1203   _alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);
1204   _alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);
1205   _num_alias_types = AliasIdxRaw+1;
1206   // Zero out the alias type cache.
1207   Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));
1208   // A NULL adr_type hits in the cache right away.  Preload the right answer.
1209   probe_alias_cache(NULL)->_index = AliasIdxTop;
1210 
1211   _intrinsics = NULL;
1212   _macro_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1213   _predicate_opaqs = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1214   _expensive_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1215   _range_check_casts = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1216   _opaque4_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);

1217   register_library_intrinsics();
1218 }
1219 
1220 //---------------------------init_start----------------------------------------
1221 // Install the StartNode on this compile object.
1222 void Compile::init_start(StartNode* s) {
1223   if (failing())
1224     return; // already failing
1225   assert(s == start(), "");
1226 }
1227 
1228 /**
1229  * Return the 'StartNode'. We must not have a pending failure, since the ideal graph
1230  * can be in an inconsistent state, i.e., we can get segmentation faults when traversing
1231  * the ideal graph.
1232  */
1233 StartNode* Compile::start() const {
1234   assert (!failing(), "Must not have pending failure. Reason is: %s", failure_reason());
1235   for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {
1236     Node* start = root()->fast_out(i);


1421 bool Compile::allow_range_check_smearing() const {
1422   // If this method has already thrown a range-check,
1423   // assume it was because we already tried range smearing
1424   // and it failed.
1425   uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1426   return !already_trapped;
1427 }
1428 
1429 
1430 //------------------------------flatten_alias_type-----------------------------
1431 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1432   int offset = tj->offset();
1433   TypePtr::PTR ptr = tj->ptr();
1434 
1435   // Known instance (scalarizable allocation) alias only with itself.
1436   bool is_known_inst = tj->isa_oopptr() != NULL &&
1437                        tj->is_oopptr()->is_known_instance();
1438 
1439   // Process weird unsafe references.
1440   if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1441     assert(InlineUnsafeOps, "indeterminate pointers come only from unsafe ops");

1442     assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1443     tj = TypeOopPtr::BOTTOM;
1444     ptr = tj->ptr();
1445     offset = tj->offset();
1446   }
1447 
1448   // Array pointers need some flattening
1449   const TypeAryPtr *ta = tj->isa_aryptr();
1450   if (ta && ta->is_stable()) {
1451     // Erase stability property for alias analysis.
1452     tj = ta = ta->cast_to_stable(false);
1453   }









1454   if( ta && is_known_inst ) {
1455     if ( offset != Type::OffsetBot &&
1456          offset > arrayOopDesc::length_offset_in_bytes() ) {
1457       offset = Type::OffsetBot; // Flatten constant access into array body only
1458       tj = ta = TypeAryPtr::make(ptr, ta->ary(), ta->klass(), true, offset, ta->instance_id());
1459     }
1460   } else if( ta && _AliasLevel >= 2 ) {
1461     // For arrays indexed by constant indices, we flatten the alias
1462     // space to include all of the array body.  Only the header, klass
1463     // and array length can be accessed un-aliased.


1464     if( offset != Type::OffsetBot ) {
1465       if( ta->const_oop() ) { // MethodData* or Method*
1466         offset = Type::OffsetBot;   // Flatten constant access into array body
1467         tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),ta->ary(),ta->klass(),false,offset);
1468       } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1469         // range is OK as-is.
1470         tj = ta = TypeAryPtr::RANGE;
1471       } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1472         tj = TypeInstPtr::KLASS; // all klass loads look alike
1473         ta = TypeAryPtr::RANGE; // generic ignored junk
1474         ptr = TypePtr::BotPTR;
1475       } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1476         tj = TypeInstPtr::MARK;
1477         ta = TypeAryPtr::RANGE; // generic ignored junk
1478         ptr = TypePtr::BotPTR;
1479       } else if (BarrierSet::barrier_set()->barrier_set_c2()->flatten_gc_alias_type(tj)) {
1480         ta = tj->isa_aryptr();
1481       } else {                  // Random constant offset into array body
1482         offset = Type::OffsetBot;   // Flatten constant access into array body
1483         tj = ta = TypeAryPtr::make(ptr,ta->ary(),ta->klass(),false,offset);
1484       }
1485     }
1486     // Arrays of fixed size alias with arrays of unknown size.
1487     if (ta->size() != TypeInt::POS) {
1488       const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1489       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,ta->klass(),false,offset);
1490     }
1491     // Arrays of known objects become arrays of unknown objects.
1492     if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1493       const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1494       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);
1495     }
1496     if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1497       const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1498       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,offset);





1499     }
1500     // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1501     // cannot be distinguished by bytecode alone.
1502     if (ta->elem() == TypeInt::BOOL) {
1503       const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1504       ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1505       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,offset);
1506     }
1507     // During the 2nd round of IterGVN, NotNull castings are removed.
1508     // Make sure the Bottom and NotNull variants alias the same.
1509     // Also, make sure exact and non-exact variants alias the same.
1510     if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != NULL) {
1511       tj = ta = TypeAryPtr::make(TypePtr::BotPTR,ta->ary(),ta->klass(),false,offset);
1512     }
1513   }
1514 
1515   // Oop pointers need some flattening
1516   const TypeInstPtr *to = tj->isa_instptr();
1517   if( to && _AliasLevel >= 2 && to != TypeOopPtr::BOTTOM ) {
1518     ciInstanceKlass *k = to->klass()->as_instance_klass();
1519     if( ptr == TypePtr::Constant ) {
1520       if (to->klass() != ciEnv::current()->Class_klass() ||
1521           offset < k->size_helper() * wordSize) {
1522         // No constant oop pointers (such as Strings); they alias with
1523         // unknown strings.
1524         assert(!is_known_inst, "not scalarizable allocation");
1525         tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);
1526       }
1527     } else if( is_known_inst ) {
1528       tj = to; // Keep NotNull and klass_is_exact for instance type
1529     } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1530       // During the 2nd round of IterGVN, NotNull castings are removed.
1531       // Make sure the Bottom and NotNull variants alias the same.
1532       // Also, make sure exact and non-exact variants alias the same.
1533       tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,offset);
1534     }
1535     if (to->speculative() != NULL) {
1536       tj = to = TypeInstPtr::make(to->ptr(),to->klass(),to->klass_is_exact(),to->const_oop(),to->offset(), to->instance_id());
1537     }
1538     // Canonicalize the holder of this field
1539     if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1540       // First handle header references such as a LoadKlassNode, even if the
1541       // object's klass is unloaded at compile time (4965979).
1542       if (!is_known_inst) { // Do it only for non-instance types
1543         tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, NULL, offset);
1544       }
1545     } else if (BarrierSet::barrier_set()->barrier_set_c2()->flatten_gc_alias_type(tj)) {
1546       to = tj->is_instptr();
1547     } else if (offset < 0 || offset >= k->size_helper() * wordSize) {
1548       // Static fields are in the space above the normal instance
1549       // fields in the java.lang.Class instance.
1550       if (to->klass() != ciEnv::current()->Class_klass()) {
1551         to = NULL;
1552         tj = TypeOopPtr::BOTTOM;
1553         offset = tj->offset();
1554       }
1555     } else {
1556       ciInstanceKlass *canonical_holder = k->get_canonical_holder(offset);
1557       if (!k->equals(canonical_holder) || tj->offset() != offset) {
1558         if( is_known_inst ) {
1559           tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, NULL, offset, to->instance_id());
1560         } else {
1561           tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, NULL, offset);
1562         }
1563       }
1564     }
1565   }
1566 
1567   // Klass pointers to object array klasses need some flattening
1568   const TypeKlassPtr *tk = tj->isa_klassptr();
1569   if( tk ) {
1570     // If we are referencing a field within a Klass, we need
1571     // to assume the worst case of an Object.  Both exact and
1572     // inexact types must flatten to the same alias class so
1573     // use NotNull as the PTR.
1574     if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1575 
1576       tj = tk = TypeKlassPtr::make(TypePtr::NotNull,
1577                                    TypeKlassPtr::OBJECT->klass(),
1578                                    offset);
1579     }
1580 
1581     ciKlass* klass = tk->klass();
1582     if( klass->is_obj_array_klass() ) {
1583       ciKlass* k = TypeAryPtr::OOPS->klass();
1584       if( !k || !k->is_loaded() )                  // Only fails for some -Xcomp runs
1585         k = TypeInstPtr::BOTTOM->klass();
1586       tj = tk = TypeKlassPtr::make( TypePtr::NotNull, k, offset );
1587     }
1588 
1589     // Check for precise loads from the primary supertype array and force them
1590     // to the supertype cache alias index.  Check for generic array loads from
1591     // the primary supertype array and also force them to the supertype cache
1592     // alias index.  Since the same load can reach both, we need to merge
1593     // these 2 disparate memories into the same alias class.  Since the
1594     // primary supertype array is read-only, there's no chance of confusion
1595     // where we bypass an array load and an array store.
1596     int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1597     if (offset == Type::OffsetBot ||
1598         (offset >= primary_supers_offset &&
1599          offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1600         offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1601       offset = in_bytes(Klass::secondary_super_cache_offset());
1602       tj = tk = TypeKlassPtr::make( TypePtr::NotNull, tk->klass(), offset );
1603     }
1604   }
1605 
1606   // Flatten all Raw pointers together.
1607   if (tj->base() == Type::RawPtr)
1608     tj = TypeRawPtr::BOTTOM;
1609 
1610   if (tj->base() == Type::AnyPtr)
1611     tj = TypePtr::BOTTOM;      // An error, which the caller must check for.
1612 
1613   // Flatten all to bottom for now
1614   switch( _AliasLevel ) {
1615   case 0:
1616     tj = TypePtr::BOTTOM;
1617     break;
1618   case 1:                       // Flatten to: oop, static, field or array
1619     switch (tj->base()) {
1620     //case Type::AryPtr: tj = TypeAryPtr::RANGE;    break;
1621     case Type::RawPtr:   tj = TypeRawPtr::BOTTOM;   break;
1622     case Type::AryPtr:   // do not distinguish arrays at all


1721   intptr_t key = (intptr_t) adr_type;
1722   key ^= key >> logAliasCacheSize;
1723   return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1724 }
1725 
1726 
1727 //-----------------------------grow_alias_types--------------------------------
1728 void Compile::grow_alias_types() {
1729   const int old_ats  = _max_alias_types; // how many before?
1730   const int new_ats  = old_ats;          // how many more?
1731   const int grow_ats = old_ats+new_ats;  // how many now?
1732   _max_alias_types = grow_ats;
1733   _alias_types =  REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1734   AliasType* ats =    NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1735   Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1736   for (int i = 0; i < new_ats; i++)  _alias_types[old_ats+i] = &ats[i];
1737 }
1738 
1739 
1740 //--------------------------------find_alias_type------------------------------
1741 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field) {
1742   if (_AliasLevel == 0)
1743     return alias_type(AliasIdxBot);
1744 
1745   AliasCacheEntry* ace = probe_alias_cache(adr_type);
1746   if (ace->_adr_type == adr_type) {
1747     return alias_type(ace->_index);



1748   }
1749 
1750   // Handle special cases.
1751   if (adr_type == NULL)             return alias_type(AliasIdxTop);
1752   if (adr_type == TypePtr::BOTTOM)  return alias_type(AliasIdxBot);
1753 
1754   // Do it the slow way.
1755   const TypePtr* flat = flatten_alias_type(adr_type);
1756 
1757 #ifdef ASSERT
1758   {
1759     ResourceMark rm;
1760     assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1761            Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1762     assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1763            Type::str(adr_type));
1764     if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1765       const TypeOopPtr* foop = flat->is_oopptr();
1766       // Scalarizable allocations have exact klass always.
1767       bool exact = !foop->klass_is_exact() || foop->is_known_instance();


1777     if (alias_type(i)->adr_type() == flat) {
1778       idx = i;
1779       break;
1780     }
1781   }
1782 
1783   if (idx == AliasIdxTop) {
1784     if (no_create)  return NULL;
1785     // Grow the array if necessary.
1786     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1787     // Add a new alias type.
1788     idx = _num_alias_types++;
1789     _alias_types[idx]->Init(idx, flat);
1790     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1791     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1792     if (flat->isa_instptr()) {
1793       if (flat->offset() == java_lang_Class::klass_offset_in_bytes()
1794           && flat->is_instptr()->klass() == env()->Class_klass())
1795         alias_type(idx)->set_rewritable(false);
1796     }

1797     if (flat->isa_aryptr()) {
1798 #ifdef ASSERT
1799       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1800       // (T_BYTE has the weakest alignment and size restrictions...)
1801       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1802 #endif

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








1805       }
1806     }
1807     if (flat->isa_klassptr()) {
1808       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1809         alias_type(idx)->set_rewritable(false);
1810       if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1811         alias_type(idx)->set_rewritable(false);
1812       if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1813         alias_type(idx)->set_rewritable(false);
1814       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1815         alias_type(idx)->set_rewritable(false);


1816     }
1817     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1818     // but the base pointer type is not distinctive enough to identify
1819     // references into JavaThread.)
1820 
1821     // Check for final fields.
1822     const TypeInstPtr* tinst = flat->isa_instptr();
1823     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1824       ciField* field;
1825       if (tinst->const_oop() != NULL &&
1826           tinst->klass() == ciEnv::current()->Class_klass() &&
1827           tinst->offset() >= (tinst->klass()->as_instance_klass()->size_helper() * wordSize)) {
1828         // static field
1829         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1830         field = k->get_field_by_offset(tinst->offset(), true);




1831       } else {
1832         ciInstanceKlass *k = tinst->klass()->as_instance_klass();
1833         field = k->get_field_by_offset(tinst->offset(), false);
1834       }
1835       assert(field == NULL ||
1836              original_field == NULL ||
1837              (field->holder() == original_field->holder() &&
1838               field->offset() == original_field->offset() &&
1839               field->is_static() == original_field->is_static()), "wrong field?");
1840       // Set field() and is_rewritable() attributes.
1841       if (field != NULL)  alias_type(idx)->set_field(field);







1842     }
1843   }
1844 
1845   // Fill the cache for next time.
1846   ace->_adr_type = adr_type;
1847   ace->_index    = idx;
1848   assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");
1849 
1850   // Might as well try to fill the cache for the flattened version, too.
1851   AliasCacheEntry* face = probe_alias_cache(flat);
1852   if (face->_adr_type == NULL) {
1853     face->_adr_type = flat;
1854     face->_index    = idx;
1855     assert(alias_type(flat) == alias_type(idx), "flat type must work too");


1856   }
1857 
1858   return alias_type(idx);
1859 }
1860 
1861 
1862 Compile::AliasType* Compile::alias_type(ciField* field) {
1863   const TypeOopPtr* t;
1864   if (field->is_static())
1865     t = TypeInstPtr::make(field->holder()->java_mirror());
1866   else
1867     t = TypeOopPtr::make_from_klass_raw(field->holder());
1868   AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1869   assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1870   return atp;
1871 }
1872 
1873 
1874 //------------------------------have_alias_type--------------------------------
1875 bool Compile::have_alias_type(const TypePtr* adr_type) {


1996   }
1997   assert(range_check_cast_count() == 0, "should be empty");
1998 }
1999 
2000 void Compile::add_opaque4_node(Node* n) {
2001   assert(n->Opcode() == Op_Opaque4, "Opaque4 only");
2002   assert(!_opaque4_nodes->contains(n), "duplicate entry in Opaque4 list");
2003   _opaque4_nodes->append(n);
2004 }
2005 
2006 // Remove all Opaque4 nodes.
2007 void Compile::remove_opaque4_nodes(PhaseIterGVN &igvn) {
2008   for (int i = opaque4_count(); i > 0; i--) {
2009     Node* opaq = opaque4_node(i-1);
2010     assert(opaq->Opcode() == Op_Opaque4, "Opaque4 only");
2011     igvn.replace_node(opaq, opaq->in(2));
2012   }
2013   assert(opaque4_count() == 0, "should be empty");
2014 }
2015 





















































































































































































































































































































































2016 // StringOpts and late inlining of string methods
2017 void Compile::inline_string_calls(bool parse_time) {
2018   {
2019     // remove useless nodes to make the usage analysis simpler
2020     ResourceMark rm;
2021     PhaseRemoveUseless pru(initial_gvn(), for_igvn());
2022   }
2023 
2024   {
2025     ResourceMark rm;
2026     print_method(PHASE_BEFORE_STRINGOPTS, 3);
2027     PhaseStringOpts pso(initial_gvn(), for_igvn());
2028     print_method(PHASE_AFTER_STRINGOPTS, 3);
2029   }
2030 
2031   // now inline anything that we skipped the first time around
2032   if (!parse_time) {
2033     _late_inlines_pos = _late_inlines.length();
2034   }
2035 


2270   remove_speculative_types(igvn);
2271 
2272   // No more new expensive nodes will be added to the list from here
2273   // so keep only the actual candidates for optimizations.
2274   cleanup_expensive_nodes(igvn);
2275 
2276   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2277     Compile::TracePhase tp("", &timers[_t_renumberLive]);
2278     initial_gvn()->replace_with(&igvn);
2279     for_igvn()->clear();
2280     Unique_Node_List new_worklist(C->comp_arena());
2281     {
2282       ResourceMark rm;
2283       PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2284     }
2285     set_for_igvn(&new_worklist);
2286     igvn = PhaseIterGVN(initial_gvn());
2287     igvn.optimize();
2288   }
2289 







2290   // Perform escape analysis
2291   if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2292     if (has_loops()) {
2293       // Cleanup graph (remove dead nodes).
2294       TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2295       PhaseIdealLoop::optimize(igvn, LoopOptsNone);
2296       if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2297       if (failing())  return;
2298     }
2299     ConnectionGraph::do_analysis(this, &igvn);
2300 
2301     if (failing())  return;
2302 
2303     // Optimize out fields loads from scalar replaceable allocations.
2304     igvn.optimize();
2305     print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2306 
2307     if (failing())  return;
2308 
2309     if (congraph() != NULL && macro_count() > 0) {


2433     C->remove_opaque4_nodes(igvn);
2434     igvn.optimize();
2435   }
2436 
2437   DEBUG_ONLY( _modified_nodes = NULL; )
2438  } // (End scope of igvn; run destructor if necessary for asserts.)
2439 
2440  process_print_inlining();
2441  // A method with only infinite loops has no edges entering loops from root
2442  {
2443    TracePhase tp("graphReshape", &timers[_t_graphReshaping]);
2444    if (final_graph_reshaping()) {
2445      assert(failing(), "must bail out w/ explicit message");
2446      return;
2447    }
2448  }
2449 
2450  print_method(PHASE_OPTIMIZE_FINISHED, 2);
2451 }
2452 
2453 
2454 //------------------------------Code_Gen---------------------------------------
2455 // Given a graph, generate code for it
2456 void Compile::Code_Gen() {
2457   if (failing()) {
2458     return;
2459   }
2460 
2461   // Perform instruction selection.  You might think we could reclaim Matcher
2462   // memory PDQ, but actually the Matcher is used in generating spill code.
2463   // Internals of the Matcher (including some VectorSets) must remain live
2464   // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
2465   // set a bit in reclaimed memory.
2466 
2467   // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
2468   // nodes.  Mapping is only valid at the root of each matched subtree.
2469   NOT_PRODUCT( verify_graph_edges(); )
2470 
2471   Matcher matcher;
2472   _matcher = &matcher;
2473   {


2762             // Accumulate any precedence edges
2763             if (mem->in(i) != NULL) {
2764               n->add_prec(mem->in(i));
2765             }
2766           }
2767           // Everything above this point has been processed.
2768           done = true;
2769         }
2770         // Eliminate the previous StoreCM
2771         prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
2772         assert(mem->outcnt() == 0, "should be dead");
2773         mem->disconnect_inputs(NULL, this);
2774       } else {
2775         prev = mem;
2776       }
2777       mem = prev->in(MemNode::Memory);
2778     }
2779   }
2780 }
2781 

2782 //------------------------------final_graph_reshaping_impl----------------------
2783 // Implement items 1-5 from final_graph_reshaping below.
2784 void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {
2785 
2786   if ( n->outcnt() == 0 ) return; // dead node
2787   uint nop = n->Opcode();
2788 
2789   // Check for 2-input instruction with "last use" on right input.
2790   // Swap to left input.  Implements item (2).
2791   if( n->req() == 3 &&          // two-input instruction
2792       n->in(1)->outcnt() > 1 && // left use is NOT a last use
2793       (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
2794       n->in(2)->outcnt() == 1 &&// right use IS a last use
2795       !n->in(2)->is_Con() ) {   // right use is not a constant
2796     // Check for commutative opcode
2797     switch( nop ) {
2798     case Op_AddI:  case Op_AddF:  case Op_AddD:  case Op_AddL:
2799     case Op_MaxI:  case Op_MinI:
2800     case Op_MulI:  case Op_MulF:  case Op_MulD:  case Op_MulL:
2801     case Op_AndL:  case Op_XorL:  case Op_OrL:


2984   case Op_LoadUS:
2985   case Op_LoadI:
2986   case Op_LoadKlass:
2987   case Op_LoadNKlass:
2988   case Op_LoadL:
2989   case Op_LoadL_unaligned:
2990   case Op_LoadPLocked:
2991   case Op_LoadP:
2992   case Op_LoadN:
2993   case Op_LoadRange:
2994   case Op_LoadS: {
2995   handle_mem:
2996 #ifdef ASSERT
2997     if( VerifyOptoOopOffsets ) {
2998       MemNode* mem  = n->as_Mem();
2999       // Check to see if address types have grounded out somehow.
3000       const TypeInstPtr *tp = mem->in(MemNode::Address)->bottom_type()->isa_instptr();
3001       assert( !tp || oop_offset_is_sane(tp), "" );
3002     }
3003 #endif







































3004     break;
3005   }
3006 
3007   case Op_AddP: {               // Assert sane base pointers
3008     Node *addp = n->in(AddPNode::Address);
3009     assert( !addp->is_AddP() ||
3010             addp->in(AddPNode::Base)->is_top() || // Top OK for allocation
3011             addp->in(AddPNode::Base) == n->in(AddPNode::Base),
3012             "Base pointers must match (addp %u)", addp->_idx );
3013 #ifdef _LP64
3014     if ((UseCompressedOops || UseCompressedClassPointers) &&
3015         addp->Opcode() == Op_ConP &&
3016         addp == n->in(AddPNode::Base) &&
3017         n->in(AddPNode::Offset)->is_Con()) {
3018       // If the transformation of ConP to ConN+DecodeN is beneficial depends
3019       // on the platform and on the compressed oops mode.
3020       // Use addressing with narrow klass to load with offset on x86.
3021       // Some platforms can use the constant pool to load ConP.
3022       // Do this transformation here since IGVN will convert ConN back to ConP.
3023       const Type* t = addp->bottom_type();


3498           // Replace all nodes with identical edges as m with m
3499           k->subsume_by(m, this);
3500         }
3501       }
3502     }
3503     break;
3504   }
3505   case Op_CmpUL: {
3506     if (!Matcher::has_match_rule(Op_CmpUL)) {
3507       // No support for unsigned long comparisons
3508       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3509       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3510       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3511       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3512       Node* andl = new AndLNode(orl, remove_sign_mask);
3513       Node* cmp = new CmpLNode(andl, n->in(2));
3514       n->subsume_by(cmp, this);
3515     }
3516     break;
3517   }























3518   default:
3519     assert(!n->is_Call(), "");
3520     assert(!n->is_Mem(), "");
3521     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3522     break;
3523   }
3524 }
3525 
3526 //------------------------------final_graph_reshaping_walk---------------------
3527 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3528 // requires that the walk visits a node's inputs before visiting the node.
3529 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
3530   ResourceArea *area = Thread::current()->resource_area();
3531   Unique_Node_List sfpt(area);
3532 
3533   frc._visited.set(root->_idx); // first, mark node as visited
3534   uint cnt = root->req();
3535   Node *n = root;
3536   uint  i = 0;
3537   while (true) {


3844   }
3845 }
3846 
3847 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
3848   return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
3849 }
3850 
3851 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
3852   return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
3853 }
3854 
3855 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
3856   if (holder->is_initialized()) {
3857     return false;
3858   }
3859   if (holder->is_being_initialized()) {
3860     if (accessing_method->holder() == holder) {
3861       // Access inside a class. The barrier can be elided when access happens in <clinit>,
3862       // <init>, or a static method. In all those cases, there was an initialization
3863       // barrier on the holder klass passed.
3864       if (accessing_method->is_static_initializer() ||
3865           accessing_method->is_object_initializer() ||
3866           accessing_method->is_static()) {
3867         return false;
3868       }
3869     } else if (accessing_method->holder()->is_subclass_of(holder)) {
3870       // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
3871       // In case of <init> or a static method, the barrier is on the subclass is not enough:
3872       // child class can become fully initialized while its parent class is still being initialized.
3873       if (accessing_method->is_static_initializer()) {
3874         return false;
3875       }
3876     }
3877     ciMethod* root = method(); // the root method of compilation
3878     if (root != accessing_method) {
3879       return needs_clinit_barrier(holder, root); // check access in the context of compilation root
3880     }
3881   }
3882   return true;
3883 }
3884 
3885 #ifndef PRODUCT
3886 //------------------------------verify_graph_edges---------------------------
3887 // Walk the Graph and verify that there is a one-to-one correspondence
3888 // between Use-Def edges and Def-Use edges in the graph.
3889 void Compile::verify_graph_edges(bool no_dead_code) {
3890   if (VerifyGraphEdges) {
3891     ResourceArea *area = Thread::current()->resource_area();
3892     Unique_Node_List visited(area);
3893     // Call recursive graph walk to check edges


4191   int offset = n->constant_offset() - table_base_offset();
4192 
4193   MacroAssembler _masm(&cb);
4194   address* jump_table_base = (address*) (_masm.code()->consts()->start() + offset);
4195 
4196   for (uint i = 0; i < n->outcnt(); i++) {
4197     address* constant_addr = &jump_table_base[i];
4198     assert(*constant_addr == (((address) n) + i), "all jump-table entries must contain adjusted node pointer: " INTPTR_FORMAT " == " INTPTR_FORMAT, p2i(*constant_addr), p2i(((address) n) + i));
4199     *constant_addr = cb.consts()->target(*labels.at(i), (address) constant_addr);
4200     cb.consts()->relocate((address) constant_addr, relocInfo::internal_word_type);
4201   }
4202 }
4203 
4204 //----------------------------static_subtype_check-----------------------------
4205 // Shortcut important common cases when superklass is exact:
4206 // (0) superklass is java.lang.Object (can occur in reflective code)
4207 // (1) subklass is already limited to a subtype of superklass => always ok
4208 // (2) subklass does not overlap with superklass => always fail
4209 // (3) superklass has NO subtypes and we can check with a simple compare.
4210 int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {
4211   if (StressReflectiveCode) {
4212     return SSC_full_test;       // Let caller generate the general case.
4213   }
4214 
4215   if (superk == env()->Object_klass()) {
4216     return SSC_always_true;     // (0) this test cannot fail
4217   }
4218 
4219   ciType* superelem = superk;
4220   if (superelem->is_array_klass())

4221     superelem = superelem->as_array_klass()->base_element_type();

4222 
4223   if (!subk->is_interface()) {  // cannot trust static interface types yet
4224     if (subk->is_subtype_of(superk)) {
4225       return SSC_always_true;   // (1) false path dead; no dynamic test needed
4226     }
4227     if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&
4228         !superk->is_subtype_of(subk)) {
4229       return SSC_always_false;
4230     }
4231   }
4232 





4233   // If casting to an instance klass, it must have no subtypes
4234   if (superk->is_interface()) {
4235     // Cannot trust interfaces yet.
4236     // %%% S.B. superk->nof_implementors() == 1
4237   } else if (superelem->is_instance_klass()) {
4238     ciInstanceKlass* ik = superelem->as_instance_klass();
4239     if (!ik->has_subklass() && !ik->is_interface()) {
4240       if (!ik->is_final()) {
4241         // Add a dependency if there is a chance of a later subclass.
4242         dependencies()->assert_leaf_type(ik);
4243       }
4244       return SSC_easy_test;     // (3) caller can do a simple ptr comparison
4245     }
4246   } else {
4247     // A primitive array type has no subtypes.
4248     return SSC_easy_test;       // (3) caller can do a simple ptr comparison
4249   }
4250 
4251   return SSC_full_test;
4252 }


4638     worklist.clear();
4639     worklist.push(root());
4640     for (uint next = 0; next < worklist.size(); ++next) {
4641       Node *n  = worklist.at(next);
4642       const Type* t = igvn.type_or_null(n);
4643       assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");
4644       if (n->is_Type()) {
4645         t = n->as_Type()->type();
4646         assert(t == t->remove_speculative(), "no more speculative types");
4647       }
4648       uint max = n->len();
4649       for( uint i = 0; i < max; ++i ) {
4650         Node *m = n->in(i);
4651         if (not_a_node(m))  continue;
4652         worklist.push(m);
4653       }
4654     }
4655     igvn.check_no_speculative_types();
4656 #endif
4657   }





















4658 }
4659 
4660 // Auxiliary method to support randomized stressing/fuzzing.
4661 //
4662 // This method can be called the arbitrary number of times, with current count
4663 // as the argument. The logic allows selecting a single candidate from the
4664 // running list of candidates as follows:
4665 //    int count = 0;
4666 //    Cand* selected = null;
4667 //    while(cand = cand->next()) {
4668 //      if (randomized_select(++count)) {
4669 //        selected = cand;
4670 //      }
4671 //    }
4672 //
4673 // Including count equalizes the chances any candidate is "selected".
4674 // This is useful when we don't have the complete list of candidates to choose
4675 // from uniformly. In this case, we need to adjust the randomicity of the
4676 // selection, or else we will end up biasing the selection towards the latter
4677 // candidates.




  50 #include "opto/divnode.hpp"
  51 #include "opto/escape.hpp"
  52 #include "opto/idealGraphPrinter.hpp"
  53 #include "opto/loopnode.hpp"
  54 #include "opto/machnode.hpp"
  55 #include "opto/macro.hpp"
  56 #include "opto/matcher.hpp"
  57 #include "opto/mathexactnode.hpp"
  58 #include "opto/memnode.hpp"
  59 #include "opto/mulnode.hpp"
  60 #include "opto/narrowptrnode.hpp"
  61 #include "opto/node.hpp"
  62 #include "opto/opcodes.hpp"
  63 #include "opto/output.hpp"
  64 #include "opto/parse.hpp"
  65 #include "opto/phaseX.hpp"
  66 #include "opto/rootnode.hpp"
  67 #include "opto/runtime.hpp"
  68 #include "opto/stringopts.hpp"
  69 #include "opto/type.hpp"
  70 #include "opto/valuetypenode.hpp"
  71 #include "opto/vectornode.hpp"
  72 #include "runtime/arguments.hpp"
  73 #include "runtime/sharedRuntime.hpp"
  74 #include "runtime/signature.hpp"
  75 #include "runtime/stubRoutines.hpp"
  76 #include "runtime/timer.hpp"
  77 #include "utilities/align.hpp"
  78 #include "utilities/copy.hpp"
  79 #include "utilities/macros.hpp"
  80 #if INCLUDE_ZGC
  81 #include "gc/z/c2/zBarrierSetC2.hpp"
  82 #endif
  83 
  84 
  85 // -------------------- Compile::mach_constant_base_node -----------------------
  86 // Constant table base node singleton.
  87 MachConstantBaseNode* Compile::mach_constant_base_node() {
  88   if (_mach_constant_base_node == NULL) {
  89     _mach_constant_base_node = new MachConstantBaseNode();
  90     _mach_constant_base_node->add_req(C->root());


 401   for (int i = range_check_cast_count() - 1; i >= 0; i--) {
 402     Node* cast = range_check_cast_node(i);
 403     if (!useful.member(cast)) {
 404       remove_range_check_cast(cast);
 405     }
 406   }
 407   // Remove useless expensive nodes
 408   for (int i = C->expensive_count()-1; i >= 0; i--) {
 409     Node* n = C->expensive_node(i);
 410     if (!useful.member(n)) {
 411       remove_expensive_node(n);
 412     }
 413   }
 414   // Remove useless Opaque4 nodes
 415   for (int i = opaque4_count() - 1; i >= 0; i--) {
 416     Node* opaq = opaque4_node(i);
 417     if (!useful.member(opaq)) {
 418       remove_opaque4_node(opaq);
 419     }
 420   }
 421   // Remove useless value type nodes
 422   if (_value_type_nodes != NULL) {
 423     _value_type_nodes->remove_useless_nodes(useful.member_set());
 424   }
 425   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 426   bs->eliminate_useless_gc_barriers(useful, this);
 427   // clean up the late inline lists
 428   remove_useless_late_inlines(&_string_late_inlines, useful);
 429   remove_useless_late_inlines(&_boxing_late_inlines, useful);
 430   remove_useless_late_inlines(&_late_inlines, useful);
 431   debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
 432 }
 433 
 434 //------------------------------frame_size_in_words-----------------------------
 435 // frame_slots in units of words
 436 int Compile::frame_size_in_words() const {
 437   // shift is 0 in LP32 and 1 in LP64
 438   const int shift = (LogBytesPerWord - LogBytesPerInt);
 439   int words = _frame_slots >> shift;
 440   assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" );
 441   return words;
 442 }
 443 
 444 // To bang the stack of this compiled method we use the stack size


 453 //------------------------------CompileWrapper---------------------------------
 454 class CompileWrapper : public StackObj {
 455   Compile *const _compile;
 456  public:
 457   CompileWrapper(Compile* compile);
 458 
 459   ~CompileWrapper();
 460 };
 461 
 462 CompileWrapper::CompileWrapper(Compile* compile) : _compile(compile) {
 463   // the Compile* pointer is stored in the current ciEnv:
 464   ciEnv* env = compile->env();
 465   assert(env == ciEnv::current(), "must already be a ciEnv active");
 466   assert(env->compiler_data() == NULL, "compile already active?");
 467   env->set_compiler_data(compile);
 468   assert(compile == Compile::current(), "sanity");
 469 
 470   compile->set_type_dict(NULL);
 471   compile->set_clone_map(new Dict(cmpkey, hashkey, _compile->comp_arena()));
 472   compile->clone_map().set_clone_idx(0);

 473   compile->set_type_last_size(0);
 474   compile->set_last_tf(NULL, NULL);
 475   compile->set_indexSet_arena(NULL);
 476   compile->set_indexSet_free_block_list(NULL);
 477   compile->init_type_arena();
 478   Type::Initialize(compile);
 479   _compile->set_scratch_buffer_blob(NULL);
 480   _compile->begin_method();
 481   _compile->clone_map().set_debug(_compile->has_method() && _compile->directive()->CloneMapDebugOption);
 482 }
 483 CompileWrapper::~CompileWrapper() {
 484   _compile->end_method();
 485   if (_compile->scratch_buffer_blob() != NULL)
 486     BufferBlob::free(_compile->scratch_buffer_blob());
 487   _compile->env()->set_compiler_data(NULL);
 488 }
 489 
 490 
 491 //----------------------------print_compile_messages---------------------------
 492 void Compile::print_compile_messages() {


 529 }
 530 
 531 
 532 //-----------------------init_scratch_buffer_blob------------------------------
 533 // Construct a temporary BufferBlob and cache it for this compile.
 534 void Compile::init_scratch_buffer_blob(int const_size) {
 535   // If there is already a scratch buffer blob allocated and the
 536   // constant section is big enough, use it.  Otherwise free the
 537   // current and allocate a new one.
 538   BufferBlob* blob = scratch_buffer_blob();
 539   if ((blob != NULL) && (const_size <= _scratch_const_size)) {
 540     // Use the current blob.
 541   } else {
 542     if (blob != NULL) {
 543       BufferBlob::free(blob);
 544     }
 545 
 546     ResourceMark rm;
 547     _scratch_const_size = const_size;
 548     int size = C2Compiler::initial_code_buffer_size(const_size);
 549 #ifdef ASSERT
 550     if (C->has_scalarized_args()) {
 551       // Oop verification for loading object fields from scalarized value types in the new entry point requires lots of space
 552       size += 5120;
 553     }
 554 #endif
 555     blob = BufferBlob::create("Compile::scratch_buffer", size);
 556     // Record the buffer blob for next time.
 557     set_scratch_buffer_blob(blob);
 558     // Have we run out of code space?
 559     if (scratch_buffer_blob() == NULL) {
 560       // Let CompilerBroker disable further compilations.
 561       record_failure("Not enough space for scratch buffer in CodeCache");
 562       return;
 563     }
 564   }
 565 
 566   // Initialize the relocation buffers
 567   relocInfo* locs_buf = (relocInfo*) blob->content_end() - MAX_locs_size;
 568   set_scratch_locs_memory(locs_buf);
 569 }
 570 
 571 
 572 //-----------------------scratch_emit_size-------------------------------------
 573 // Helper function that computes size by emitting code
 574 uint Compile::scratch_emit_size(const Node* n) {


 599   int lsize = MAX_locs_size / 3;
 600   buf.consts()->initialize_shared_locs(&locs_buf[lsize * 0], lsize);
 601   buf.insts()->initialize_shared_locs( &locs_buf[lsize * 1], lsize);
 602   buf.stubs()->initialize_shared_locs( &locs_buf[lsize * 2], lsize);
 603   // Mark as scratch buffer.
 604   buf.consts()->set_scratch_emit();
 605   buf.insts()->set_scratch_emit();
 606   buf.stubs()->set_scratch_emit();
 607 
 608   // Do the emission.
 609 
 610   Label fakeL; // Fake label for branch instructions.
 611   Label*   saveL = NULL;
 612   uint save_bnum = 0;
 613   bool is_branch = n->is_MachBranch();
 614   if (is_branch) {
 615     MacroAssembler masm(&buf);
 616     masm.bind(fakeL);
 617     n->as_MachBranch()->save_label(&saveL, &save_bnum);
 618     n->as_MachBranch()->label_set(&fakeL, 0);
 619   } else if (n->is_MachProlog()) {
 620     saveL = ((MachPrologNode*)n)->_verified_entry;
 621     ((MachPrologNode*)n)->_verified_entry = &fakeL;
 622   }
 623   n->emit(buf, this->regalloc());
 624 
 625   // Emitting into the scratch buffer should not fail
 626   assert (!failing(), "Must not have pending failure. Reason is: %s", failure_reason());
 627 
 628   // Restore label.
 629   if (is_branch) {
 630     n->as_MachBranch()->label_set(saveL, save_bnum);
 631   } else if (n->is_MachProlog()) {
 632     ((MachPrologNode*)n)->_verified_entry = saveL;
 633   }
 634 
 635   // End scratch_emit_size section.
 636   set_in_scratch_emit_size(false);
 637 
 638   return buf.insts_size();
 639 }
 640 
 641 
 642 // ============================================================================
 643 //------------------------------Compile standard-------------------------------
 644 debug_only( int Compile::_debug_idx = 100000; )
 645 
 646 // Compile a method.  entry_bci is -1 for normal compilations and indicates
 647 // the continuation bci for on stack replacement.
 648 
 649 
 650 Compile::Compile( ciEnv* ci_env, C2Compiler* compiler, ciMethod* target, int osr_bci,
 651                   bool subsume_loads, bool do_escape_analysis, bool eliminate_boxing, DirectiveSet* directive)
 652                 : Phase(Compiler),
 653                   _compile_id(ci_env->compile_id()),
 654                   _save_argument_registers(false),
 655                   _subsume_loads(subsume_loads),
 656                   _do_escape_analysis(do_escape_analysis),
 657                   _eliminate_boxing(eliminate_boxing),
 658                   _method(target),
 659                   _entry_bci(osr_bci),
 660                   _stub_function(NULL),
 661                   _stub_name(NULL),
 662                   _stub_entry_point(NULL),
 663                   _max_node_limit(MaxNodeLimit),
 664                   _orig_pc_slot(0),
 665                   _orig_pc_slot_offset_in_bytes(0),
 666                   _sp_inc_slot(0),
 667                   _sp_inc_slot_offset_in_bytes(0),
 668                   _inlining_progress(false),
 669                   _inlining_incrementally(false),
 670                   _do_cleanup(false),
 671                   _has_reserved_stack_access(target->has_reserved_stack_access()),
 672 #ifndef PRODUCT
 673                   _trace_opto_output(directive->TraceOptoOutputOption),
 674 #endif
 675                   _has_method_handle_invokes(false),
 676                   _clinit_barrier_on_entry(false),
 677                   _comp_arena(mtCompiler),
 678                   _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 679                   _env(ci_env),
 680                   _directive(directive),
 681                   _log(ci_env->log()),
 682                   _failure_reason(NULL),
 683                   _congraph(NULL),
 684 #ifndef PRODUCT
 685                   _printer(IdealGraphPrinter::printer()),
 686 #endif
 687                   _dead_node_list(comp_arena()),


 787   print_inlining_init();
 788   { // Scope for timing the parser
 789     TracePhase tp("parse", &timers[_t_parser]);
 790 
 791     // Put top into the hash table ASAP.
 792     initial_gvn()->transform_no_reclaim(top());
 793 
 794     // Set up tf(), start(), and find a CallGenerator.
 795     CallGenerator* cg = NULL;
 796     if (is_osr_compilation()) {
 797       const TypeTuple *domain = StartOSRNode::osr_domain();
 798       const TypeTuple *range = TypeTuple::make_range(method()->signature());
 799       init_tf(TypeFunc::make(domain, range));
 800       StartNode* s = new StartOSRNode(root(), domain);
 801       initial_gvn()->set_type_bottom(s);
 802       init_start(s);
 803       cg = CallGenerator::for_osr(method(), entry_bci());
 804     } else {
 805       // Normal case.
 806       init_tf(TypeFunc::make(method()));
 807       StartNode* s = new StartNode(root(), tf()->domain_cc());
 808       initial_gvn()->set_type_bottom(s);
 809       init_start(s);
 810       if (method()->intrinsic_id() == vmIntrinsics::_Reference_get) {
 811         // With java.lang.ref.reference.get() we must go through the
 812         // intrinsic - even when get() is the root
 813         // method of the compile - so that, if necessary, the value in
 814         // the referent field of the reference object gets recorded by
 815         // the pre-barrier code.
 816         cg = find_intrinsic(method(), false);
 817       }
 818       if (cg == NULL) {
 819         float past_uses = method()->interpreter_invocation_count();
 820         float expected_uses = past_uses;
 821         cg = CallGenerator::for_inline(method(), expected_uses);
 822       }
 823     }
 824     if (failing())  return;
 825     if (cg == NULL) {
 826       record_method_not_compilable("cannot parse method");
 827       return;


 912     }
 913   }
 914 #endif
 915 
 916 #ifdef ASSERT
 917   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 918   bs->verify_gc_barriers(this, BarrierSetC2::BeforeCodeGen);
 919 #endif
 920 
 921   // Dump compilation data to replay it.
 922   if (directive->DumpReplayOption) {
 923     env()->dump_replay_data(_compile_id);
 924   }
 925   if (directive->DumpInlineOption && (ilt() != NULL)) {
 926     env()->dump_inline_data(_compile_id);
 927   }
 928 
 929   // Now that we know the size of all the monitors we can add a fixed slot
 930   // for the original deopt pc.
 931 
 932   _orig_pc_slot = fixed_slots();
 933   int next_slot = _orig_pc_slot + (sizeof(address) / VMRegImpl::stack_slot_size);
 934 
 935   if (needs_stack_repair()) {
 936     // One extra slot for the special stack increment value
 937     _sp_inc_slot = next_slot;
 938     next_slot += 2;
 939   }
 940 
 941   set_fixed_slots(next_slot);
 942 
 943   // Compute when to use implicit null checks. Used by matching trap based
 944   // nodes and NullCheck optimization.
 945   set_allowed_deopt_reasons();
 946 
 947   // Now generate code
 948   Code_Gen();
 949   if (failing())  return;
 950 
 951   // Check if we want to skip execution of all compiled code.
 952   {
 953 #ifndef PRODUCT
 954     if (OptoNoExecute) {
 955       record_method_not_compilable("+OptoNoExecute");  // Flag as failed
 956       return;
 957     }
 958 #endif
 959     TracePhase tp("install_code", &timers[_t_registerMethod]);
 960 
 961     if (is_osr_compilation()) {
 962       _code_offsets.set_value(CodeOffsets::Verified_Entry, 0);
 963       _code_offsets.set_value(CodeOffsets::OSR_Entry, _first_block_size);
 964     } else {
 965       _code_offsets.set_value(CodeOffsets::Verified_Entry, _first_block_size);
 966       if (_code_offsets.value(CodeOffsets::Verified_Value_Entry) == -1) {
 967         _code_offsets.set_value(CodeOffsets::Verified_Value_Entry, _first_block_size);
 968       }
 969       if (_code_offsets.value(CodeOffsets::Verified_Value_Entry_RO) == -1) {
 970         _code_offsets.set_value(CodeOffsets::Verified_Value_Entry_RO, _first_block_size);
 971       }
 972       if (_code_offsets.value(CodeOffsets::Entry) == -1) {
 973         _code_offsets.set_value(CodeOffsets::Entry, _first_block_size);
 974       }
 975       _code_offsets.set_value(CodeOffsets::OSR_Entry, 0);
 976     }
 977 
 978     env()->register_method(_method, _entry_bci,
 979                            &_code_offsets,
 980                            _orig_pc_slot_offset_in_bytes,
 981                            code_buffer(),
 982                            frame_size_in_words(), _oop_map_set,
 983                            &_handler_table, &_inc_table,
 984                            compiler,
 985                            has_unsafe_access(),
 986                            SharedRuntime::is_wide_vector(max_vector_size()),
 987                            rtm_state()
 988                            );
 989 
 990     if (log() != NULL) // Print code cache state into compiler log
 991       log()->code_cache_state();
 992   }
 993 }
 994 


1000                   const char *stub_name,
1001                   int is_fancy_jump,
1002                   bool pass_tls,
1003                   bool save_arg_registers,
1004                   bool return_pc,
1005                   DirectiveSet* directive)
1006   : Phase(Compiler),
1007     _compile_id(0),
1008     _save_argument_registers(save_arg_registers),
1009     _subsume_loads(true),
1010     _do_escape_analysis(false),
1011     _eliminate_boxing(false),
1012     _method(NULL),
1013     _entry_bci(InvocationEntryBci),
1014     _stub_function(stub_function),
1015     _stub_name(stub_name),
1016     _stub_entry_point(NULL),
1017     _max_node_limit(MaxNodeLimit),
1018     _orig_pc_slot(0),
1019     _orig_pc_slot_offset_in_bytes(0),
1020     _sp_inc_slot(0),
1021     _sp_inc_slot_offset_in_bytes(0),
1022     _inlining_progress(false),
1023     _inlining_incrementally(false),
1024     _has_reserved_stack_access(false),
1025 #ifndef PRODUCT
1026     _trace_opto_output(directive->TraceOptoOutputOption),
1027 #endif
1028     _has_method_handle_invokes(false),
1029     _clinit_barrier_on_entry(false),
1030     _comp_arena(mtCompiler),
1031     _env(ci_env),
1032     _directive(directive),
1033     _log(ci_env->log()),
1034     _failure_reason(NULL),
1035     _congraph(NULL),
1036 #ifndef PRODUCT
1037     _printer(NULL),
1038 #endif
1039     _dead_node_list(comp_arena()),
1040     _dead_node_count(0),
1041     _node_arena(mtCompiler),


1151   // Create Debug Information Recorder to record scopes, oopmaps, etc.
1152   env()->set_oop_recorder(new OopRecorder(env()->arena()));
1153   env()->set_debug_info(new DebugInformationRecorder(env()->oop_recorder()));
1154   env()->set_dependencies(new Dependencies(env()));
1155 
1156   _fixed_slots = 0;
1157   set_has_split_ifs(false);
1158   set_has_loops(has_method() && method()->has_loops()); // first approximation
1159   set_has_stringbuilder(false);
1160   set_has_boxed_value(false);
1161   _trap_can_recompile = false;  // no traps emitted yet
1162   _major_progress = true; // start out assuming good things will happen
1163   set_has_unsafe_access(false);
1164   set_max_vector_size(0);
1165   set_clear_upper_avx(false);  //false as default for clear upper bits of ymm registers
1166   Copy::zero_to_bytes(_trap_hist, sizeof(_trap_hist));
1167   set_decompile_count(0);
1168 
1169   set_do_freq_based_layout(_directive->BlockLayoutByFrequencyOption);
1170   _loop_opts_cnt = LoopOptsCount;
1171   _has_flattened_accesses = false;
1172   _flattened_accesses_share_alias = true;
1173 
1174   set_do_inlining(Inline);
1175   set_max_inline_size(MaxInlineSize);
1176   set_freq_inline_size(FreqInlineSize);
1177   set_do_scheduling(OptoScheduling);
1178   set_do_count_invocations(false);
1179   set_do_method_data_update(false);
1180 
1181   set_do_vector_loop(false);
1182 
1183   if (AllowVectorizeOnDemand) {
1184     if (has_method() && (_directive->VectorizeOption || _directive->VectorizeDebugOption)) {
1185       set_do_vector_loop(true);
1186       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());})
1187     } else if (has_method() && method()->name() != 0 &&
1188                method()->intrinsic_id() == vmIntrinsics::_forEachRemaining) {
1189       set_do_vector_loop(true);
1190     }
1191   }
1192   set_use_cmove(UseCMoveUnconditionally /* || do_vector_loop()*/); //TODO: consider do_vector_loop() mandate use_cmove unconditionally
1193   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());})


1237   Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);
1238   {
1239     for (int i = 0; i < grow_ats; i++)  _alias_types[i] = &ats[i];
1240   }
1241   // Initialize the first few types.
1242   _alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);
1243   _alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);
1244   _alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);
1245   _num_alias_types = AliasIdxRaw+1;
1246   // Zero out the alias type cache.
1247   Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));
1248   // A NULL adr_type hits in the cache right away.  Preload the right answer.
1249   probe_alias_cache(NULL)->_index = AliasIdxTop;
1250 
1251   _intrinsics = NULL;
1252   _macro_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1253   _predicate_opaqs = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1254   _expensive_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1255   _range_check_casts = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1256   _opaque4_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8,  0, NULL);
1257   _value_type_nodes = new (comp_arena()) Unique_Node_List(comp_arena());
1258   register_library_intrinsics();
1259 }
1260 
1261 //---------------------------init_start----------------------------------------
1262 // Install the StartNode on this compile object.
1263 void Compile::init_start(StartNode* s) {
1264   if (failing())
1265     return; // already failing
1266   assert(s == start(), "");
1267 }
1268 
1269 /**
1270  * Return the 'StartNode'. We must not have a pending failure, since the ideal graph
1271  * can be in an inconsistent state, i.e., we can get segmentation faults when traversing
1272  * the ideal graph.
1273  */
1274 StartNode* Compile::start() const {
1275   assert (!failing(), "Must not have pending failure. Reason is: %s", failure_reason());
1276   for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {
1277     Node* start = root()->fast_out(i);


1462 bool Compile::allow_range_check_smearing() const {
1463   // If this method has already thrown a range-check,
1464   // assume it was because we already tried range smearing
1465   // and it failed.
1466   uint already_trapped = trap_count(Deoptimization::Reason_range_check);
1467   return !already_trapped;
1468 }
1469 
1470 
1471 //------------------------------flatten_alias_type-----------------------------
1472 const TypePtr *Compile::flatten_alias_type( const TypePtr *tj ) const {
1473   int offset = tj->offset();
1474   TypePtr::PTR ptr = tj->ptr();
1475 
1476   // Known instance (scalarizable allocation) alias only with itself.
1477   bool is_known_inst = tj->isa_oopptr() != NULL &&
1478                        tj->is_oopptr()->is_known_instance();
1479 
1480   // Process weird unsafe references.
1481   if (offset == Type::OffsetBot && (tj->isa_instptr() /*|| tj->isa_klassptr()*/)) {
1482     bool default_value_load = EnableValhalla && tj->is_instptr()->klass() == ciEnv::current()->Class_klass();
1483     assert(InlineUnsafeOps || default_value_load, "indeterminate pointers come only from unsafe ops");
1484     assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1485     tj = TypeOopPtr::BOTTOM;
1486     ptr = tj->ptr();
1487     offset = tj->offset();
1488   }
1489 
1490   // Array pointers need some flattening
1491   const TypeAryPtr *ta = tj->isa_aryptr();
1492   if (ta && ta->is_stable()) {
1493     // Erase stability property for alias analysis.
1494     tj = ta = ta->cast_to_stable(false);
1495   }
1496   if (ta && ta->is_not_flat()) {
1497     // Erase not flat property for alias analysis.
1498     tj = ta = ta->cast_to_not_flat(false);
1499   }
1500   if (ta && ta->is_not_null_free()) {
1501     // Erase not null free property for alias analysis.
1502     tj = ta = ta->cast_to_not_null_free(false);
1503   }
1504 
1505   if( ta && is_known_inst ) {
1506     if ( offset != Type::OffsetBot &&
1507          offset > arrayOopDesc::length_offset_in_bytes() ) {
1508       offset = Type::OffsetBot; // Flatten constant access into array body only
1509       tj = ta = TypeAryPtr::make(ptr, ta->ary(), ta->klass(), true, Type::Offset(offset), ta->field_offset(), ta->instance_id());
1510     }
1511   } else if( ta && _AliasLevel >= 2 ) {
1512     // For arrays indexed by constant indices, we flatten the alias
1513     // space to include all of the array body.  Only the header, klass
1514     // and array length can be accessed un-aliased.
1515     // For flattened value type array, each field has its own slice so
1516     // we must include the field offset.
1517     if( offset != Type::OffsetBot ) {
1518       if( ta->const_oop() ) { // MethodData* or Method*
1519         offset = Type::OffsetBot;   // Flatten constant access into array body
1520         tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),ta->ary(),ta->klass(),false,Type::Offset(offset), ta->field_offset());
1521       } else if( offset == arrayOopDesc::length_offset_in_bytes() ) {
1522         // range is OK as-is.
1523         tj = ta = TypeAryPtr::RANGE;
1524       } else if( offset == oopDesc::klass_offset_in_bytes() ) {
1525         tj = TypeInstPtr::KLASS; // all klass loads look alike
1526         ta = TypeAryPtr::RANGE; // generic ignored junk
1527         ptr = TypePtr::BotPTR;
1528       } else if( offset == oopDesc::mark_offset_in_bytes() ) {
1529         tj = TypeInstPtr::MARK;
1530         ta = TypeAryPtr::RANGE; // generic ignored junk
1531         ptr = TypePtr::BotPTR;
1532       } else if (BarrierSet::barrier_set()->barrier_set_c2()->flatten_gc_alias_type(tj)) {
1533         ta = tj->isa_aryptr();
1534       } else {                  // Random constant offset into array body
1535         offset = Type::OffsetBot;   // Flatten constant access into array body
1536         tj = ta = TypeAryPtr::make(ptr,ta->ary(),ta->klass(),false,Type::Offset(offset), ta->field_offset());
1537       }
1538     }
1539     // Arrays of fixed size alias with arrays of unknown size.
1540     if (ta->size() != TypeInt::POS) {
1541       const TypeAry *tary = TypeAry::make(ta->elem(), TypeInt::POS);
1542       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,ta->klass(),false,Type::Offset(offset), ta->field_offset());
1543     }
1544     // Arrays of known objects become arrays of unknown objects.
1545     if (ta->elem()->isa_narrowoop() && ta->elem() != TypeNarrowOop::BOTTOM) {
1546       const TypeAry *tary = TypeAry::make(TypeNarrowOop::BOTTOM, ta->size());
1547       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,Type::Offset(offset), ta->field_offset());
1548     }
1549     if (ta->elem()->isa_oopptr() && ta->elem() != TypeInstPtr::BOTTOM) {
1550       const TypeAry *tary = TypeAry::make(TypeInstPtr::BOTTOM, ta->size());
1551       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,Type::Offset(offset), ta->field_offset());
1552     }
1553     // Initially all flattened array accesses share a single slice
1554     if (ta->elem()->isa_valuetype() && ta->elem() != TypeValueType::BOTTOM && _flattened_accesses_share_alias) {
1555       const TypeAry *tary = TypeAry::make(TypeValueType::BOTTOM, ta->size());
1556       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,NULL,false,Type::Offset(offset), Type::Offset(Type::OffsetBot));
1557     }
1558     // Arrays of bytes and of booleans both use 'bastore' and 'baload' so
1559     // cannot be distinguished by bytecode alone.
1560     if (ta->elem() == TypeInt::BOOL) {
1561       const TypeAry *tary = TypeAry::make(TypeInt::BYTE, ta->size());
1562       ciKlass* aklass = ciTypeArrayKlass::make(T_BYTE);
1563       tj = ta = TypeAryPtr::make(ptr,ta->const_oop(),tary,aklass,false,Type::Offset(offset), ta->field_offset());
1564     }
1565     // During the 2nd round of IterGVN, NotNull castings are removed.
1566     // Make sure the Bottom and NotNull variants alias the same.
1567     // Also, make sure exact and non-exact variants alias the same.
1568     if (ptr == TypePtr::NotNull || ta->klass_is_exact() || ta->speculative() != NULL) {
1569       tj = ta = TypeAryPtr::make(TypePtr::BotPTR,ta->ary(),ta->klass(),false,Type::Offset(offset), ta->field_offset());
1570     }
1571   }
1572 
1573   // Oop pointers need some flattening
1574   const TypeInstPtr *to = tj->isa_instptr();
1575   if( to && _AliasLevel >= 2 && to != TypeOopPtr::BOTTOM ) {
1576     ciInstanceKlass *k = to->klass()->as_instance_klass();
1577     if( ptr == TypePtr::Constant ) {
1578       if (to->klass() != ciEnv::current()->Class_klass() ||
1579           offset < k->size_helper() * wordSize) {
1580         // No constant oop pointers (such as Strings); they alias with
1581         // unknown strings.
1582         assert(!is_known_inst, "not scalarizable allocation");
1583         tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,Type::Offset(offset));
1584       }
1585     } else if( is_known_inst ) {
1586       tj = to; // Keep NotNull and klass_is_exact for instance type
1587     } else if( ptr == TypePtr::NotNull || to->klass_is_exact() ) {
1588       // During the 2nd round of IterGVN, NotNull castings are removed.
1589       // Make sure the Bottom and NotNull variants alias the same.
1590       // Also, make sure exact and non-exact variants alias the same.
1591       tj = to = TypeInstPtr::make(TypePtr::BotPTR,to->klass(),false,0,Type::Offset(offset));
1592     }
1593     if (to->speculative() != NULL) {
1594       tj = to = TypeInstPtr::make(to->ptr(),to->klass(),to->klass_is_exact(),to->const_oop(),Type::Offset(to->offset()), to->instance_id());
1595     }
1596     // Canonicalize the holder of this field
1597     if (offset >= 0 && offset < instanceOopDesc::base_offset_in_bytes()) {
1598       // First handle header references such as a LoadKlassNode, even if the
1599       // object's klass is unloaded at compile time (4965979).
1600       if (!is_known_inst) { // Do it only for non-instance types
1601         tj = to = TypeInstPtr::make(TypePtr::BotPTR, env()->Object_klass(), false, NULL, Type::Offset(offset));
1602       }
1603     } else if (BarrierSet::barrier_set()->barrier_set_c2()->flatten_gc_alias_type(tj)) {
1604       to = tj->is_instptr();
1605     } else if (offset < 0 || offset >= k->size_helper() * wordSize) {
1606       // Static fields are in the space above the normal instance
1607       // fields in the java.lang.Class instance.
1608       if (to->klass() != ciEnv::current()->Class_klass()) {
1609         to = NULL;
1610         tj = TypeOopPtr::BOTTOM;
1611         offset = tj->offset();
1612       }
1613     } else {
1614       ciInstanceKlass *canonical_holder = k->get_canonical_holder(offset);
1615       if (!k->equals(canonical_holder) || tj->offset() != offset) {
1616         if( is_known_inst ) {
1617           tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, true, NULL, Type::Offset(offset), to->instance_id());
1618         } else {
1619           tj = to = TypeInstPtr::make(to->ptr(), canonical_holder, false, NULL, Type::Offset(offset));
1620         }
1621       }
1622     }
1623   }
1624 
1625   // Klass pointers to object array klasses need some flattening
1626   const TypeKlassPtr *tk = tj->isa_klassptr();
1627   if( tk ) {
1628     // If we are referencing a field within a Klass, we need
1629     // to assume the worst case of an Object.  Both exact and
1630     // inexact types must flatten to the same alias class so
1631     // use NotNull as the PTR.
1632     if ( offset == Type::OffsetBot || (offset >= 0 && (size_t)offset < sizeof(Klass)) ) {
1633 
1634       tj = tk = TypeKlassPtr::make(TypePtr::NotNull,
1635                                    TypeKlassPtr::OBJECT->klass(),
1636                                    Type::Offset(offset));
1637     }
1638 
1639     ciKlass* klass = tk->klass();
1640     if (klass != NULL && klass->is_obj_array_klass()) {
1641       ciKlass* k = TypeAryPtr::OOPS->klass();
1642       if( !k || !k->is_loaded() )                  // Only fails for some -Xcomp runs
1643         k = TypeInstPtr::BOTTOM->klass();
1644       tj = tk = TypeKlassPtr::make(TypePtr::NotNull, k, Type::Offset(offset));
1645     }
1646 
1647     // Check for precise loads from the primary supertype array and force them
1648     // to the supertype cache alias index.  Check for generic array loads from
1649     // the primary supertype array and also force them to the supertype cache
1650     // alias index.  Since the same load can reach both, we need to merge
1651     // these 2 disparate memories into the same alias class.  Since the
1652     // primary supertype array is read-only, there's no chance of confusion
1653     // where we bypass an array load and an array store.
1654     int primary_supers_offset = in_bytes(Klass::primary_supers_offset());
1655     if (offset == Type::OffsetBot ||
1656         (offset >= primary_supers_offset &&
1657          offset < (int)(primary_supers_offset + Klass::primary_super_limit() * wordSize)) ||
1658         offset == (int)in_bytes(Klass::secondary_super_cache_offset())) {
1659       offset = in_bytes(Klass::secondary_super_cache_offset());
1660       tj = tk = TypeKlassPtr::make(TypePtr::NotNull, tk->klass(), Type::Offset(offset));
1661     }
1662   }
1663 
1664   // Flatten all Raw pointers together.
1665   if (tj->base() == Type::RawPtr)
1666     tj = TypeRawPtr::BOTTOM;
1667 
1668   if (tj->base() == Type::AnyPtr)
1669     tj = TypePtr::BOTTOM;      // An error, which the caller must check for.
1670 
1671   // Flatten all to bottom for now
1672   switch( _AliasLevel ) {
1673   case 0:
1674     tj = TypePtr::BOTTOM;
1675     break;
1676   case 1:                       // Flatten to: oop, static, field or array
1677     switch (tj->base()) {
1678     //case Type::AryPtr: tj = TypeAryPtr::RANGE;    break;
1679     case Type::RawPtr:   tj = TypeRawPtr::BOTTOM;   break;
1680     case Type::AryPtr:   // do not distinguish arrays at all


1779   intptr_t key = (intptr_t) adr_type;
1780   key ^= key >> logAliasCacheSize;
1781   return &_alias_cache[key & right_n_bits(logAliasCacheSize)];
1782 }
1783 
1784 
1785 //-----------------------------grow_alias_types--------------------------------
1786 void Compile::grow_alias_types() {
1787   const int old_ats  = _max_alias_types; // how many before?
1788   const int new_ats  = old_ats;          // how many more?
1789   const int grow_ats = old_ats+new_ats;  // how many now?
1790   _max_alias_types = grow_ats;
1791   _alias_types =  REALLOC_ARENA_ARRAY(comp_arena(), AliasType*, _alias_types, old_ats, grow_ats);
1792   AliasType* ats =    NEW_ARENA_ARRAY(comp_arena(), AliasType, new_ats);
1793   Copy::zero_to_bytes(ats, sizeof(AliasType)*new_ats);
1794   for (int i = 0; i < new_ats; i++)  _alias_types[old_ats+i] = &ats[i];
1795 }
1796 
1797 
1798 //--------------------------------find_alias_type------------------------------
1799 Compile::AliasType* Compile::find_alias_type(const TypePtr* adr_type, bool no_create, ciField* original_field, bool uncached) {
1800   if (_AliasLevel == 0)
1801     return alias_type(AliasIdxBot);
1802 
1803   AliasCacheEntry* ace = NULL;
1804   if (!uncached) {
1805     ace = probe_alias_cache(adr_type);
1806     if (ace->_adr_type == adr_type) {
1807       return alias_type(ace->_index);
1808     }
1809   }
1810 
1811   // Handle special cases.
1812   if (adr_type == NULL)             return alias_type(AliasIdxTop);
1813   if (adr_type == TypePtr::BOTTOM)  return alias_type(AliasIdxBot);
1814 
1815   // Do it the slow way.
1816   const TypePtr* flat = flatten_alias_type(adr_type);
1817 
1818 #ifdef ASSERT
1819   {
1820     ResourceMark rm;
1821     assert(flat == flatten_alias_type(flat), "not idempotent: adr_type = %s; flat = %s => %s",
1822            Type::str(adr_type), Type::str(flat), Type::str(flatten_alias_type(flat)));
1823     assert(flat != TypePtr::BOTTOM, "cannot alias-analyze an untyped ptr: adr_type = %s",
1824            Type::str(adr_type));
1825     if (flat->isa_oopptr() && !flat->isa_klassptr()) {
1826       const TypeOopPtr* foop = flat->is_oopptr();
1827       // Scalarizable allocations have exact klass always.
1828       bool exact = !foop->klass_is_exact() || foop->is_known_instance();


1838     if (alias_type(i)->adr_type() == flat) {
1839       idx = i;
1840       break;
1841     }
1842   }
1843 
1844   if (idx == AliasIdxTop) {
1845     if (no_create)  return NULL;
1846     // Grow the array if necessary.
1847     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1848     // Add a new alias type.
1849     idx = _num_alias_types++;
1850     _alias_types[idx]->Init(idx, flat);
1851     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1852     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1853     if (flat->isa_instptr()) {
1854       if (flat->offset() == java_lang_Class::klass_offset_in_bytes()
1855           && flat->is_instptr()->klass() == env()->Class_klass())
1856         alias_type(idx)->set_rewritable(false);
1857     }
1858     ciField* field = NULL;
1859     if (flat->isa_aryptr()) {
1860 #ifdef ASSERT
1861       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1862       // (T_BYTE has the weakest alignment and size restrictions...)
1863       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1864 #endif
1865       const Type* elemtype = flat->is_aryptr()->elem();
1866       if (flat->offset() == TypePtr::OffsetBot) {
1867         alias_type(idx)->set_element(elemtype);
1868       }
1869       int field_offset = flat->is_aryptr()->field_offset().get();
1870       if (elemtype->isa_valuetype() &&
1871           elemtype->value_klass() != NULL &&
1872           field_offset != Type::OffsetBot) {
1873         ciValueKlass* vk = elemtype->value_klass();
1874         field_offset += vk->first_field_offset();
1875         field = vk->get_field_by_offset(field_offset, false);
1876       }
1877     }
1878     if (flat->isa_klassptr()) {
1879       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1880         alias_type(idx)->set_rewritable(false);
1881       if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1882         alias_type(idx)->set_rewritable(false);
1883       if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1884         alias_type(idx)->set_rewritable(false);
1885       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1886         alias_type(idx)->set_rewritable(false);
1887       if (flat->offset() == in_bytes(Klass::layout_helper_offset()))
1888         alias_type(idx)->set_rewritable(false);
1889     }
1890     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1891     // but the base pointer type is not distinctive enough to identify
1892     // references into JavaThread.)
1893 
1894     // Check for final fields.
1895     const TypeInstPtr* tinst = flat->isa_instptr();
1896     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {

1897       if (tinst->const_oop() != NULL &&
1898           tinst->klass() == ciEnv::current()->Class_klass() &&
1899           tinst->offset() >= (tinst->klass()->as_instance_klass()->size_helper() * wordSize)) {
1900         // static field
1901         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1902         field = k->get_field_by_offset(tinst->offset(), true);
1903       } else if (tinst->klass()->is_valuetype()) {
1904         // Value type field
1905         ciValueKlass* vk = tinst->value_klass();
1906         field = vk->get_field_by_offset(tinst->offset(), false);
1907       } else {
1908         ciInstanceKlass* k = tinst->klass()->as_instance_klass();
1909         field = k->get_field_by_offset(tinst->offset(), false);
1910       }
1911     }
1912     assert(field == NULL ||
1913            original_field == NULL ||
1914            (field->holder() == original_field->holder() &&
1915             field->offset() == original_field->offset() &&
1916             field->is_static() == original_field->is_static()), "wrong field?");
1917     // Set field() and is_rewritable() attributes.
1918     if (field != NULL) {
1919       alias_type(idx)->set_field(field);
1920       if (flat->isa_aryptr()) {
1921         // Fields of flattened inline type arrays are rewritable although they are declared final
1922         assert(flat->is_aryptr()->elem()->isa_valuetype(), "must be a flattened value array");
1923         alias_type(idx)->set_rewritable(true);
1924       }
1925     }
1926   }
1927 
1928   // Fill the cache for next time.
1929   if (!uncached) {
1930     ace->_adr_type = adr_type;
1931     ace->_index    = idx;
1932     assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");
1933 
1934     // Might as well try to fill the cache for the flattened version, too.
1935     AliasCacheEntry* face = probe_alias_cache(flat);
1936     if (face->_adr_type == NULL) {
1937       face->_adr_type = flat;
1938       face->_index    = idx;
1939       assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1940     }
1941   }
1942 
1943   return alias_type(idx);
1944 }
1945 
1946 
1947 Compile::AliasType* Compile::alias_type(ciField* field) {
1948   const TypeOopPtr* t;
1949   if (field->is_static())
1950     t = TypeInstPtr::make(field->holder()->java_mirror());
1951   else
1952     t = TypeOopPtr::make_from_klass_raw(field->holder());
1953   AliasType* atp = alias_type(t->add_offset(field->offset_in_bytes()), field);
1954   assert((field->is_final() || field->is_stable()) == !atp->is_rewritable(), "must get the rewritable bits correct");
1955   return atp;
1956 }
1957 
1958 
1959 //------------------------------have_alias_type--------------------------------
1960 bool Compile::have_alias_type(const TypePtr* adr_type) {


2081   }
2082   assert(range_check_cast_count() == 0, "should be empty");
2083 }
2084 
2085 void Compile::add_opaque4_node(Node* n) {
2086   assert(n->Opcode() == Op_Opaque4, "Opaque4 only");
2087   assert(!_opaque4_nodes->contains(n), "duplicate entry in Opaque4 list");
2088   _opaque4_nodes->append(n);
2089 }
2090 
2091 // Remove all Opaque4 nodes.
2092 void Compile::remove_opaque4_nodes(PhaseIterGVN &igvn) {
2093   for (int i = opaque4_count(); i > 0; i--) {
2094     Node* opaq = opaque4_node(i-1);
2095     assert(opaq->Opcode() == Op_Opaque4, "Opaque4 only");
2096     igvn.replace_node(opaq, opaq->in(2));
2097   }
2098   assert(opaque4_count() == 0, "should be empty");
2099 }
2100 
2101 void Compile::add_value_type(Node* n) {
2102   assert(n->is_ValueTypeBase(), "unexpected node");
2103   if (_value_type_nodes != NULL) {
2104     _value_type_nodes->push(n);
2105   }
2106 }
2107 
2108 void Compile::remove_value_type(Node* n) {
2109   assert(n->is_ValueTypeBase(), "unexpected node");
2110   if (_value_type_nodes != NULL) {
2111     _value_type_nodes->remove(n);
2112   }
2113 }
2114 
2115 // Does the return value keep otherwise useless value type allocations
2116 // alive?
2117 static bool return_val_keeps_allocations_alive(Node* ret_val) {
2118   ResourceMark rm;
2119   Unique_Node_List wq;
2120   wq.push(ret_val);
2121   bool some_allocations = false;
2122   for (uint i = 0; i < wq.size(); i++) {
2123     Node* n = wq.at(i);
2124     assert(!n->is_ValueTypeBase(), "chain of value type nodes");
2125     if (n->outcnt() > 1) {
2126       // Some other use for the allocation
2127       return false;
2128     } else if (n->is_Phi()) {
2129       for (uint j = 1; j < n->req(); j++) {
2130         wq.push(n->in(j));
2131       }
2132     } else if (n->is_CheckCastPP() &&
2133                n->in(1)->is_Proj() &&
2134                n->in(1)->in(0)->is_Allocate()) {
2135       some_allocations = true;
2136     }
2137   }
2138   return some_allocations;
2139 }
2140 
2141 void Compile::process_value_types(PhaseIterGVN &igvn) {
2142   // Make value types scalar in safepoints
2143   while (_value_type_nodes->size() != 0) {
2144     ValueTypeBaseNode* vt = _value_type_nodes->pop()->as_ValueTypeBase();
2145     vt->make_scalar_in_safepoints(&igvn);
2146     if (vt->is_ValueTypePtr()) {
2147       igvn.replace_node(vt, vt->get_oop());
2148     } else if (vt->outcnt() == 0) {
2149       igvn.remove_dead_node(vt);
2150     }
2151   }
2152   _value_type_nodes = NULL;
2153   if (tf()->returns_value_type_as_fields()) {
2154     Node* ret = NULL;
2155     for (uint i = 1; i < root()->req(); i++){
2156       Node* in = root()->in(i);
2157       if (in->Opcode() == Op_Return) {
2158         assert(ret == NULL, "only one return");
2159         ret = in;
2160       }
2161     }
2162     if (ret != NULL) {
2163       Node* ret_val = ret->in(TypeFunc::Parms);
2164       if (igvn.type(ret_val)->isa_oopptr() &&
2165           return_val_keeps_allocations_alive(ret_val)) {
2166         igvn.replace_input_of(ret, TypeFunc::Parms, ValueTypeNode::tagged_klass(igvn.type(ret_val)->value_klass(), igvn));
2167         assert(ret_val->outcnt() == 0, "should be dead now");
2168         igvn.remove_dead_node(ret_val);
2169       }
2170     }
2171   }
2172   igvn.optimize();
2173 }
2174 
2175 void Compile::adjust_flattened_array_access_aliases(PhaseIterGVN& igvn) {
2176   if (!_has_flattened_accesses) {
2177     return;
2178   }
2179   // Initially, all flattened array accesses share the same slice to
2180   // keep dependencies with Object[] array accesses (that could be
2181   // to a flattened array) correct. We're done with parsing so we
2182   // now know all flattened array accesses in this compile
2183   // unit. Let's move flattened array accesses to their own slice,
2184   // one per element field. This should help memory access
2185   // optimizations.
2186   ResourceMark rm;
2187   Unique_Node_List wq;
2188   wq.push(root());
2189 
2190   Node_List mergememnodes;
2191   Node_List memnodes;
2192 
2193   // Alias index currently shared by all flattened memory accesses
2194   int index = get_alias_index(TypeAryPtr::VALUES);
2195 
2196   // Find MergeMem nodes and flattened array accesses
2197   for (uint i = 0; i < wq.size(); i++) {
2198     Node* n = wq.at(i);
2199     if (n->is_Mem()) {
2200       const TypePtr* adr_type = NULL;
2201       if (n->Opcode() == Op_StoreCM) {
2202         adr_type = get_adr_type(get_alias_index(n->in(MemNode::OopStore)->adr_type()));
2203       } else {
2204         adr_type = get_adr_type(get_alias_index(n->adr_type()));
2205       }
2206       if (adr_type == TypeAryPtr::VALUES) {
2207         memnodes.push(n);
2208       }
2209     } else if (n->is_MergeMem()) {
2210       MergeMemNode* mm = n->as_MergeMem();
2211       if (mm->memory_at(index) != mm->base_memory()) {
2212         mergememnodes.push(n);
2213       }
2214     }
2215     for (uint j = 0; j < n->req(); j++) {
2216       Node* m = n->in(j);
2217       if (m != NULL) {
2218         wq.push(m);
2219       }
2220     }
2221   }
2222 
2223   if (memnodes.size() > 0) {
2224     _flattened_accesses_share_alias = false;
2225 
2226     // We are going to change the slice for the flattened array
2227     // accesses so we need to clear the cache entries that refer to
2228     // them.
2229     for (uint i = 0; i < AliasCacheSize; i++) {
2230       AliasCacheEntry* ace = &_alias_cache[i];
2231       if (ace->_adr_type != NULL &&
2232           ace->_adr_type->isa_aryptr() &&
2233           ace->_adr_type->is_aryptr()->elem()->isa_valuetype()) {
2234         ace->_adr_type = NULL;
2235         ace->_index = 0;
2236       }
2237     }
2238 
2239     // Find what aliases we are going to add
2240     int start_alias = num_alias_types()-1;
2241     int stop_alias = 0;
2242 
2243     for (uint i = 0; i < memnodes.size(); i++) {
2244       Node* m = memnodes.at(i);
2245       const TypePtr* adr_type = NULL;
2246       if (m->Opcode() == Op_StoreCM) {
2247         adr_type = m->in(MemNode::OopStore)->adr_type();
2248         Node* clone = new StoreCMNode(m->in(MemNode::Control), m->in(MemNode::Memory), m->in(MemNode::Address),
2249                                       m->adr_type(), m->in(MemNode::ValueIn), m->in(MemNode::OopStore),
2250                                       get_alias_index(adr_type));
2251         igvn.register_new_node_with_optimizer(clone);
2252         igvn.replace_node(m, clone);
2253       } else {
2254         adr_type = m->adr_type();
2255 #ifdef ASSERT
2256         m->as_Mem()->set_adr_type(adr_type);
2257 #endif
2258       }
2259       int idx = get_alias_index(adr_type);
2260       start_alias = MIN2(start_alias, idx);
2261       stop_alias = MAX2(stop_alias, idx);
2262     }
2263 
2264     assert(stop_alias >= start_alias, "should have expanded aliases");
2265 
2266     Node_Stack stack(0);
2267 #ifdef ASSERT
2268     VectorSet seen(Thread::current()->resource_area());
2269 #endif
2270     // Now let's fix the memory graph so each flattened array access
2271     // is moved to the right slice. Start from the MergeMem nodes.
2272     uint last = unique();
2273     for (uint i = 0; i < mergememnodes.size(); i++) {
2274       MergeMemNode* current = mergememnodes.at(i)->as_MergeMem();
2275       Node* n = current->memory_at(index);
2276       MergeMemNode* mm = NULL;
2277       do {
2278         // Follow memory edges through memory accesses, phis and
2279         // narrow membars and push nodes on the stack. Once we hit
2280         // bottom memory, we pop element off the stack one at a
2281         // time, in reverse order, and move them to the right slice
2282         // by changing their memory edges.
2283         if ((n->is_Phi() && n->adr_type() != TypePtr::BOTTOM) || n->is_Mem() || n->adr_type() == TypeAryPtr::VALUES) {
2284           assert(!seen.test_set(n->_idx), "");
2285           // Uses (a load for instance) will need to be moved to the
2286           // right slice as well and will get a new memory state
2287           // that we don't know yet. The use could also be the
2288           // backedge of a loop. We put a place holder node between
2289           // the memory node and its uses. We replace that place
2290           // holder with the correct memory state once we know it,
2291           // i.e. when nodes are popped off the stack. Using the
2292           // place holder make the logic work in the presence of
2293           // loops.
2294           if (n->outcnt() > 1) {
2295             Node* place_holder = NULL;
2296             assert(!n->has_out_with(Op_Node), "");
2297             for (DUIterator k = n->outs(); n->has_out(k); k++) {
2298               Node* u = n->out(k);
2299               if (u != current && u->_idx < last) {
2300                 bool success = false;
2301                 for (uint l = 0; l < u->req(); l++) {
2302                   if (!stack.is_empty() && u == stack.node() && l == stack.index()) {
2303                     continue;
2304                   }
2305                   Node* in = u->in(l);
2306                   if (in == n) {
2307                     if (place_holder == NULL) {
2308                       place_holder = new Node(1);
2309                       place_holder->init_req(0, n);
2310                     }
2311                     igvn.replace_input_of(u, l, place_holder);
2312                     success = true;
2313                   }
2314                 }
2315                 if (success) {
2316                   --k;
2317                 }
2318               }
2319             }
2320           }
2321           if (n->is_Phi()) {
2322             stack.push(n, 1);
2323             n = n->in(1);
2324           } else if (n->is_Mem()) {
2325             stack.push(n, n->req());
2326             n = n->in(MemNode::Memory);
2327           } else {
2328             assert(n->is_Proj() && n->in(0)->Opcode() == Op_MemBarCPUOrder, "");
2329             stack.push(n, n->req());
2330             n = n->in(0)->in(TypeFunc::Memory);
2331           }
2332         } else {
2333           assert(n->adr_type() == TypePtr::BOTTOM || (n->Opcode() == Op_Node && n->_idx >= last) || (n->is_Proj() && n->in(0)->is_Initialize()), "");
2334           // Build a new MergeMem node to carry the new memory state
2335           // as we build it. IGVN should fold extraneous MergeMem
2336           // nodes.
2337           mm = MergeMemNode::make(n);
2338           igvn.register_new_node_with_optimizer(mm);
2339           while (stack.size() > 0) {
2340             Node* m = stack.node();
2341             uint idx = stack.index();
2342             if (m->is_Mem()) {
2343               // Move memory node to its new slice
2344               const TypePtr* adr_type = m->adr_type();
2345               int alias = get_alias_index(adr_type);
2346               Node* prev = mm->memory_at(alias);
2347               igvn.replace_input_of(m, MemNode::Memory, prev);
2348               mm->set_memory_at(alias, m);
2349             } else if (m->is_Phi()) {
2350               // We need as many new phis as there are new aliases
2351               igvn.replace_input_of(m, idx, mm);
2352               if (idx == m->req()-1) {
2353                 Node* r = m->in(0);
2354                 for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2355                   const Type* adr_type = get_adr_type(j);
2356                   if (!adr_type->isa_aryptr() || !adr_type->is_aryptr()->elem()->isa_valuetype()) {
2357                     continue;
2358                   }
2359                   Node* phi = new PhiNode(r, Type::MEMORY, get_adr_type(j));
2360                   igvn.register_new_node_with_optimizer(phi);
2361                   for (uint k = 1; k < m->req(); k++) {
2362                     phi->init_req(k, m->in(k)->as_MergeMem()->memory_at(j));
2363                   }
2364                   mm->set_memory_at(j, phi);
2365                 }
2366                 Node* base_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2367                 igvn.register_new_node_with_optimizer(base_phi);
2368                 for (uint k = 1; k < m->req(); k++) {
2369                   base_phi->init_req(k, m->in(k)->as_MergeMem()->base_memory());
2370                 }
2371                 mm->set_base_memory(base_phi);
2372               }
2373             } else {
2374               // This is a MemBarCPUOrder node from
2375               // Parse::array_load()/Parse::array_store(), in the
2376               // branch that handles flattened arrays hidden under
2377               // an Object[] array. We also need one new membar per
2378               // new alias to keep the unknown access that the
2379               // membars protect properly ordered with accesses to
2380               // known flattened array.
2381               assert(m->is_Proj(), "projection expected");
2382               Node* ctrl = m->in(0)->in(TypeFunc::Control);
2383               igvn.replace_input_of(m->in(0), TypeFunc::Control, top());
2384               for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2385                 const Type* adr_type = get_adr_type(j);
2386                 if (!adr_type->isa_aryptr() || !adr_type->is_aryptr()->elem()->isa_valuetype()) {
2387                   continue;
2388                 }
2389                 MemBarNode* mb = new MemBarCPUOrderNode(this, j, NULL);
2390                 igvn.register_new_node_with_optimizer(mb);
2391                 Node* mem = mm->memory_at(j);
2392                 mb->init_req(TypeFunc::Control, ctrl);
2393                 mb->init_req(TypeFunc::Memory, mem);
2394                 ctrl = new ProjNode(mb, TypeFunc::Control);
2395                 igvn.register_new_node_with_optimizer(ctrl);
2396                 mem = new ProjNode(mb, TypeFunc::Memory);
2397                 igvn.register_new_node_with_optimizer(mem);
2398                 mm->set_memory_at(j, mem);
2399               }
2400               igvn.replace_node(m->in(0)->as_Multi()->proj_out(TypeFunc::Control), ctrl);
2401             }
2402             if (idx < m->req()-1) {
2403               idx += 1;
2404               stack.set_index(idx);
2405               n = m->in(idx);
2406               break;
2407             }
2408             // Take care of place holder nodes
2409             if (m->has_out_with(Op_Node)) {
2410               Node* place_holder = m->find_out_with(Op_Node);
2411               if (place_holder != NULL) {
2412                 Node* mm_clone = mm->clone();
2413                 igvn.register_new_node_with_optimizer(mm_clone);
2414                 Node* hook = new Node(1);
2415                 hook->init_req(0, mm);
2416                 igvn.replace_node(place_holder, mm_clone);
2417                 hook->destruct();
2418               }
2419               assert(!m->has_out_with(Op_Node), "place holder should be gone now");
2420             }
2421             stack.pop();
2422           }
2423         }
2424       } while(stack.size() > 0);
2425       // Fix the memory state at the MergeMem we started from
2426       igvn.rehash_node_delayed(current);
2427       for (uint j = (uint)start_alias; j <= (uint)stop_alias; j++) {
2428         const Type* adr_type = get_adr_type(j);
2429         if (!adr_type->isa_aryptr() || !adr_type->is_aryptr()->elem()->isa_valuetype()) {
2430           continue;
2431         }
2432         current->set_memory_at(j, mm);
2433       }
2434       current->set_memory_at(index, current->base_memory());
2435     }
2436     igvn.optimize();
2437   }
2438   print_method(PHASE_SPLIT_VALUES_ARRAY, 2);
2439 }
2440 
2441 
2442 // StringOpts and late inlining of string methods
2443 void Compile::inline_string_calls(bool parse_time) {
2444   {
2445     // remove useless nodes to make the usage analysis simpler
2446     ResourceMark rm;
2447     PhaseRemoveUseless pru(initial_gvn(), for_igvn());
2448   }
2449 
2450   {
2451     ResourceMark rm;
2452     print_method(PHASE_BEFORE_STRINGOPTS, 3);
2453     PhaseStringOpts pso(initial_gvn(), for_igvn());
2454     print_method(PHASE_AFTER_STRINGOPTS, 3);
2455   }
2456 
2457   // now inline anything that we skipped the first time around
2458   if (!parse_time) {
2459     _late_inlines_pos = _late_inlines.length();
2460   }
2461 


2696   remove_speculative_types(igvn);
2697 
2698   // No more new expensive nodes will be added to the list from here
2699   // so keep only the actual candidates for optimizations.
2700   cleanup_expensive_nodes(igvn);
2701 
2702   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2703     Compile::TracePhase tp("", &timers[_t_renumberLive]);
2704     initial_gvn()->replace_with(&igvn);
2705     for_igvn()->clear();
2706     Unique_Node_List new_worklist(C->comp_arena());
2707     {
2708       ResourceMark rm;
2709       PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2710     }
2711     set_for_igvn(&new_worklist);
2712     igvn = PhaseIterGVN(initial_gvn());
2713     igvn.optimize();
2714   }
2715 
2716   if (_value_type_nodes->size() > 0) {
2717     // Do this once all inlining is over to avoid getting inconsistent debug info
2718     process_value_types(igvn);
2719   }
2720 
2721   adjust_flattened_array_access_aliases(igvn);
2722 
2723   // Perform escape analysis
2724   if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2725     if (has_loops()) {
2726       // Cleanup graph (remove dead nodes).
2727       TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2728       PhaseIdealLoop::optimize(igvn, LoopOptsNone);
2729       if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2730       if (failing())  return;
2731     }
2732     ConnectionGraph::do_analysis(this, &igvn);
2733 
2734     if (failing())  return;
2735 
2736     // Optimize out fields loads from scalar replaceable allocations.
2737     igvn.optimize();
2738     print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2739 
2740     if (failing())  return;
2741 
2742     if (congraph() != NULL && macro_count() > 0) {


2866     C->remove_opaque4_nodes(igvn);
2867     igvn.optimize();
2868   }
2869 
2870   DEBUG_ONLY( _modified_nodes = NULL; )
2871  } // (End scope of igvn; run destructor if necessary for asserts.)
2872 
2873  process_print_inlining();
2874  // A method with only infinite loops has no edges entering loops from root
2875  {
2876    TracePhase tp("graphReshape", &timers[_t_graphReshaping]);
2877    if (final_graph_reshaping()) {
2878      assert(failing(), "must bail out w/ explicit message");
2879      return;
2880    }
2881  }
2882 
2883  print_method(PHASE_OPTIMIZE_FINISHED, 2);
2884 }
2885 

2886 //------------------------------Code_Gen---------------------------------------
2887 // Given a graph, generate code for it
2888 void Compile::Code_Gen() {
2889   if (failing()) {
2890     return;
2891   }
2892 
2893   // Perform instruction selection.  You might think we could reclaim Matcher
2894   // memory PDQ, but actually the Matcher is used in generating spill code.
2895   // Internals of the Matcher (including some VectorSets) must remain live
2896   // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
2897   // set a bit in reclaimed memory.
2898 
2899   // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
2900   // nodes.  Mapping is only valid at the root of each matched subtree.
2901   NOT_PRODUCT( verify_graph_edges(); )
2902 
2903   Matcher matcher;
2904   _matcher = &matcher;
2905   {


3194             // Accumulate any precedence edges
3195             if (mem->in(i) != NULL) {
3196               n->add_prec(mem->in(i));
3197             }
3198           }
3199           // Everything above this point has been processed.
3200           done = true;
3201         }
3202         // Eliminate the previous StoreCM
3203         prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
3204         assert(mem->outcnt() == 0, "should be dead");
3205         mem->disconnect_inputs(NULL, this);
3206       } else {
3207         prev = mem;
3208       }
3209       mem = prev->in(MemNode::Memory);
3210     }
3211   }
3212 }
3213 
3214 
3215 //------------------------------final_graph_reshaping_impl----------------------
3216 // Implement items 1-5 from final_graph_reshaping below.
3217 void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {
3218 
3219   if ( n->outcnt() == 0 ) return; // dead node
3220   uint nop = n->Opcode();
3221 
3222   // Check for 2-input instruction with "last use" on right input.
3223   // Swap to left input.  Implements item (2).
3224   if( n->req() == 3 &&          // two-input instruction
3225       n->in(1)->outcnt() > 1 && // left use is NOT a last use
3226       (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
3227       n->in(2)->outcnt() == 1 &&// right use IS a last use
3228       !n->in(2)->is_Con() ) {   // right use is not a constant
3229     // Check for commutative opcode
3230     switch( nop ) {
3231     case Op_AddI:  case Op_AddF:  case Op_AddD:  case Op_AddL:
3232     case Op_MaxI:  case Op_MinI:
3233     case Op_MulI:  case Op_MulF:  case Op_MulD:  case Op_MulL:
3234     case Op_AndL:  case Op_XorL:  case Op_OrL:


3417   case Op_LoadUS:
3418   case Op_LoadI:
3419   case Op_LoadKlass:
3420   case Op_LoadNKlass:
3421   case Op_LoadL:
3422   case Op_LoadL_unaligned:
3423   case Op_LoadPLocked:
3424   case Op_LoadP:
3425   case Op_LoadN:
3426   case Op_LoadRange:
3427   case Op_LoadS: {
3428   handle_mem:
3429 #ifdef ASSERT
3430     if( VerifyOptoOopOffsets ) {
3431       MemNode* mem  = n->as_Mem();
3432       // Check to see if address types have grounded out somehow.
3433       const TypeInstPtr *tp = mem->in(MemNode::Address)->bottom_type()->isa_instptr();
3434       assert( !tp || oop_offset_is_sane(tp), "" );
3435     }
3436 #endif
3437     if (EnableValhalla && (nop == Op_LoadKlass || nop == Op_LoadNKlass)) {
3438       const TypeKlassPtr* tk = n->bottom_type()->make_ptr()->is_klassptr();
3439       assert(!tk->klass_is_exact(), "should have been folded");
3440       if (tk->klass()->can_be_value_array_klass()) {
3441         // Array load klass needs to filter out property bits (but not
3442         // GetNullFreePropertyNode which needs to extract the null free bits)
3443         uint last = unique();
3444         Node* pointer = NULL;
3445         if (nop == Op_LoadKlass) {
3446           Node* cast = new CastP2XNode(NULL, n);
3447           Node* masked = new LShiftXNode(cast, new ConINode(TypeInt::make(oopDesc::storage_props_nof_bits)));
3448           masked = new RShiftXNode(masked, new ConINode(TypeInt::make(oopDesc::storage_props_nof_bits)));
3449           pointer = new CastX2PNode(masked);
3450           pointer = new CheckCastPPNode(NULL, pointer, n->bottom_type());
3451         } else {
3452           Node* cast = new CastN2INode(n);
3453           Node* masked = new AndINode(cast, new ConINode(TypeInt::make(oopDesc::compressed_klass_mask())));
3454           pointer = new CastI2NNode(masked, n->bottom_type());
3455         }
3456         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3457           Node* u = n->fast_out(i);
3458           if (u->_idx < last && u->Opcode() != Op_GetNullFreeProperty) {
3459             // If user is a comparison with a klass that can't be a value type
3460             // array klass, we don't need to clear the storage property bits.
3461             Node* cmp = (u->is_DecodeNKlass() && u->outcnt() == 1) ? u->unique_out() : u;
3462             if (cmp->is_Cmp()) {
3463               const TypeKlassPtr* kp1 = cmp->in(1)->bottom_type()->make_ptr()->isa_klassptr();
3464               const TypeKlassPtr* kp2 = cmp->in(2)->bottom_type()->make_ptr()->isa_klassptr();
3465               if ((kp1 != NULL && !kp1->klass()->can_be_value_array_klass()) ||
3466                   (kp2 != NULL && !kp2->klass()->can_be_value_array_klass())) {
3467                 continue;
3468               }
3469             }
3470             int nb = u->replace_edge(n, pointer);
3471             --i, imax -= nb;
3472           }
3473         }
3474       }
3475     }
3476     break;
3477   }
3478 
3479   case Op_AddP: {               // Assert sane base pointers
3480     Node *addp = n->in(AddPNode::Address);
3481     assert( !addp->is_AddP() ||
3482             addp->in(AddPNode::Base)->is_top() || // Top OK for allocation
3483             addp->in(AddPNode::Base) == n->in(AddPNode::Base),
3484             "Base pointers must match (addp %u)", addp->_idx );
3485 #ifdef _LP64
3486     if ((UseCompressedOops || UseCompressedClassPointers) &&
3487         addp->Opcode() == Op_ConP &&
3488         addp == n->in(AddPNode::Base) &&
3489         n->in(AddPNode::Offset)->is_Con()) {
3490       // If the transformation of ConP to ConN+DecodeN is beneficial depends
3491       // on the platform and on the compressed oops mode.
3492       // Use addressing with narrow klass to load with offset on x86.
3493       // Some platforms can use the constant pool to load ConP.
3494       // Do this transformation here since IGVN will convert ConN back to ConP.
3495       const Type* t = addp->bottom_type();


3970           // Replace all nodes with identical edges as m with m
3971           k->subsume_by(m, this);
3972         }
3973       }
3974     }
3975     break;
3976   }
3977   case Op_CmpUL: {
3978     if (!Matcher::has_match_rule(Op_CmpUL)) {
3979       // No support for unsigned long comparisons
3980       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3981       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3982       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3983       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3984       Node* andl = new AndLNode(orl, remove_sign_mask);
3985       Node* cmp = new CmpLNode(andl, n->in(2));
3986       n->subsume_by(cmp, this);
3987     }
3988     break;
3989   }
3990 #ifdef ASSERT
3991   case Op_ValueTypePtr:
3992   case Op_ValueType: {
3993     n->dump(-1);
3994     assert(false, "value type node was not removed");
3995     break;
3996   }
3997 #endif
3998   case Op_GetNullFreeProperty: {
3999     // Extract the null free bits
4000     uint last = unique();
4001     Node* null_free = NULL;
4002     if (n->in(1)->Opcode() == Op_LoadKlass) {
4003       Node* cast = new CastP2XNode(NULL, n->in(1));
4004       null_free = new AndLNode(cast, new ConLNode(TypeLong::make(((jlong)1)<<(oopDesc::wide_storage_props_shift + ArrayStorageProperties::null_free_bit))));
4005     } else {
4006       assert(n->in(1)->Opcode() == Op_LoadNKlass, "not a compressed klass?");
4007       Node* cast = new CastN2INode(n->in(1));
4008       null_free = new AndINode(cast, new ConINode(TypeInt::make(1<<(oopDesc::narrow_storage_props_shift + ArrayStorageProperties::null_free_bit))));
4009     }
4010     n->replace_by(null_free);
4011     break;
4012   }
4013   default:
4014     assert(!n->is_Call(), "");
4015     assert(!n->is_Mem(), "");
4016     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
4017     break;
4018   }
4019 }
4020 
4021 //------------------------------final_graph_reshaping_walk---------------------
4022 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
4023 // requires that the walk visits a node's inputs before visiting the node.
4024 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
4025   ResourceArea *area = Thread::current()->resource_area();
4026   Unique_Node_List sfpt(area);
4027 
4028   frc._visited.set(root->_idx); // first, mark node as visited
4029   uint cnt = root->req();
4030   Node *n = root;
4031   uint  i = 0;
4032   while (true) {


4339   }
4340 }
4341 
4342 bool Compile::needs_clinit_barrier(ciMethod* method, ciMethod* accessing_method) {
4343   return method->is_static() && needs_clinit_barrier(method->holder(), accessing_method);
4344 }
4345 
4346 bool Compile::needs_clinit_barrier(ciField* field, ciMethod* accessing_method) {
4347   return field->is_static() && needs_clinit_barrier(field->holder(), accessing_method);
4348 }
4349 
4350 bool Compile::needs_clinit_barrier(ciInstanceKlass* holder, ciMethod* accessing_method) {
4351   if (holder->is_initialized()) {
4352     return false;
4353   }
4354   if (holder->is_being_initialized()) {
4355     if (accessing_method->holder() == holder) {
4356       // Access inside a class. The barrier can be elided when access happens in <clinit>,
4357       // <init>, or a static method. In all those cases, there was an initialization
4358       // barrier on the holder klass passed.
4359       if (accessing_method->is_class_initializer() ||
4360           accessing_method->is_object_constructor() ||
4361           accessing_method->is_static()) {
4362         return false;
4363       }
4364     } else if (accessing_method->holder()->is_subclass_of(holder)) {
4365       // Access from a subclass. The barrier can be elided only when access happens in <clinit>.
4366       // In case of <init> or a static method, the barrier is on the subclass is not enough:
4367       // child class can become fully initialized while its parent class is still being initialized.
4368       if (accessing_method->is_class_initializer()) {
4369         return false;
4370       }
4371     }
4372     ciMethod* root = method(); // the root method of compilation
4373     if (root != accessing_method) {
4374       return needs_clinit_barrier(holder, root); // check access in the context of compilation root
4375     }
4376   }
4377   return true;
4378 }
4379 
4380 #ifndef PRODUCT
4381 //------------------------------verify_graph_edges---------------------------
4382 // Walk the Graph and verify that there is a one-to-one correspondence
4383 // between Use-Def edges and Def-Use edges in the graph.
4384 void Compile::verify_graph_edges(bool no_dead_code) {
4385   if (VerifyGraphEdges) {
4386     ResourceArea *area = Thread::current()->resource_area();
4387     Unique_Node_List visited(area);
4388     // Call recursive graph walk to check edges


4686   int offset = n->constant_offset() - table_base_offset();
4687 
4688   MacroAssembler _masm(&cb);
4689   address* jump_table_base = (address*) (_masm.code()->consts()->start() + offset);
4690 
4691   for (uint i = 0; i < n->outcnt(); i++) {
4692     address* constant_addr = &jump_table_base[i];
4693     assert(*constant_addr == (((address) n) + i), "all jump-table entries must contain adjusted node pointer: " INTPTR_FORMAT " == " INTPTR_FORMAT, p2i(*constant_addr), p2i(((address) n) + i));
4694     *constant_addr = cb.consts()->target(*labels.at(i), (address) constant_addr);
4695     cb.consts()->relocate((address) constant_addr, relocInfo::internal_word_type);
4696   }
4697 }
4698 
4699 //----------------------------static_subtype_check-----------------------------
4700 // Shortcut important common cases when superklass is exact:
4701 // (0) superklass is java.lang.Object (can occur in reflective code)
4702 // (1) subklass is already limited to a subtype of superklass => always ok
4703 // (2) subklass does not overlap with superklass => always fail
4704 // (3) superklass has NO subtypes and we can check with a simple compare.
4705 int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {
4706   if (StressReflectiveCode || superk == NULL || subk == NULL) {
4707     return SSC_full_test;       // Let caller generate the general case.
4708   }
4709 
4710   if (superk == env()->Object_klass()) {
4711     return SSC_always_true;     // (0) this test cannot fail
4712   }
4713 
4714   ciType* superelem = superk;
4715   if (superelem->is_array_klass()) {
4716     ciArrayKlass* ak = superelem->as_array_klass();
4717     superelem = superelem->as_array_klass()->base_element_type();
4718   }
4719 
4720   if (!subk->is_interface()) {  // cannot trust static interface types yet
4721     if (subk->is_subtype_of(superk)) {
4722       return SSC_always_true;   // (1) false path dead; no dynamic test needed
4723     }
4724     if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&
4725         !superk->is_subtype_of(subk)) {
4726       return SSC_always_false;
4727     }
4728   }
4729 
4730   // Do not fold the subtype check to an array klass pointer comparison for [V? arrays.
4731   // [V is a subtype of [V? but the klass for [V is not equal to the klass for [V?. Perform a full test.
4732   if (superk->is_obj_array_klass() && !superk->as_array_klass()->storage_properties().is_null_free() && superk->as_array_klass()->element_klass()->is_valuetype()) {
4733     return SSC_full_test;
4734   }
4735   // If casting to an instance klass, it must have no subtypes
4736   if (superk->is_interface()) {
4737     // Cannot trust interfaces yet.
4738     // %%% S.B. superk->nof_implementors() == 1
4739   } else if (superelem->is_instance_klass()) {
4740     ciInstanceKlass* ik = superelem->as_instance_klass();
4741     if (!ik->has_subklass() && !ik->is_interface()) {
4742       if (!ik->is_final()) {
4743         // Add a dependency if there is a chance of a later subclass.
4744         dependencies()->assert_leaf_type(ik);
4745       }
4746       return SSC_easy_test;     // (3) caller can do a simple ptr comparison
4747     }
4748   } else {
4749     // A primitive array type has no subtypes.
4750     return SSC_easy_test;       // (3) caller can do a simple ptr comparison
4751   }
4752 
4753   return SSC_full_test;
4754 }


5140     worklist.clear();
5141     worklist.push(root());
5142     for (uint next = 0; next < worklist.size(); ++next) {
5143       Node *n  = worklist.at(next);
5144       const Type* t = igvn.type_or_null(n);
5145       assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");
5146       if (n->is_Type()) {
5147         t = n->as_Type()->type();
5148         assert(t == t->remove_speculative(), "no more speculative types");
5149       }
5150       uint max = n->len();
5151       for( uint i = 0; i < max; ++i ) {
5152         Node *m = n->in(i);
5153         if (not_a_node(m))  continue;
5154         worklist.push(m);
5155       }
5156     }
5157     igvn.check_no_speculative_types();
5158 #endif
5159   }
5160 }
5161 
5162 Node* Compile::optimize_acmp(PhaseGVN* phase, Node* a, Node* b) {
5163   const TypeInstPtr* ta = phase->type(a)->isa_instptr();
5164   const TypeInstPtr* tb = phase->type(b)->isa_instptr();
5165   if (!EnableValhalla || ta == NULL || tb == NULL ||
5166       ta->is_zero_type() || tb->is_zero_type() ||
5167       !ta->can_be_value_type() || !tb->can_be_value_type()) {
5168     // Use old acmp if one operand is null or not a value type
5169     return new CmpPNode(a, b);
5170   } else if (ta->is_valuetypeptr() || tb->is_valuetypeptr()) {
5171     // We know that one operand is a value type. Therefore,
5172     // new acmp will only return true if both operands are NULL.
5173     // Check if both operands are null by or'ing the oops.
5174     a = phase->transform(new CastP2XNode(NULL, a));
5175     b = phase->transform(new CastP2XNode(NULL, b));
5176     a = phase->transform(new OrXNode(a, b));
5177     return new CmpXNode(a, phase->MakeConX(0));
5178   }
5179   // Use new acmp
5180   return NULL;
5181 }
5182 
5183 // Auxiliary method to support randomized stressing/fuzzing.
5184 //
5185 // This method can be called the arbitrary number of times, with current count
5186 // as the argument. The logic allows selecting a single candidate from the
5187 // running list of candidates as follows:
5188 //    int count = 0;
5189 //    Cand* selected = null;
5190 //    while(cand = cand->next()) {
5191 //      if (randomized_select(++count)) {
5192 //        selected = cand;
5193 //      }
5194 //    }
5195 //
5196 // Including count equalizes the chances any candidate is "selected".
5197 // This is useful when we don't have the complete list of candidates to choose
5198 // from uniformly. In this case, we need to adjust the randomicity of the
5199 // selection, or else we will end up biasing the selection towards the latter
5200 // candidates.


< prev index next >