< 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


 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                   _comp_arena(mtCompiler),
 658                   _barrier_set_state(BarrierSet::barrier_set()->barrier_set_c2()->create_barrier_state(comp_arena())),
 659                   _env(ci_env),
 660                   _directive(directive),
 661                   _log(ci_env->log()),
 662                   _failure_reason(NULL),
 663                   _congraph(NULL),
 664 #ifndef PRODUCT
 665                   _printer(IdealGraphPrinter::printer()),
 666 #endif
 667                   _dead_node_list(comp_arena()),
 668                   _dead_node_count(0),


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


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







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









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


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


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


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

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


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

1432     assert(!is_known_inst, "scalarizable allocation should not have unsafe references");
1433     tj = TypeOopPtr::BOTTOM;
1434     ptr = tj->ptr();
1435     offset = tj->offset();
1436   }
1437 
1438   // Array pointers need some flattening
1439   const TypeAryPtr *ta = tj->isa_aryptr();
1440   if (ta && ta->is_stable()) {
1441     // Erase stability property for alias analysis.
1442     tj = ta = ta->cast_to_stable(false);
1443   }
1444   if( ta && is_known_inst ) {
1445     if ( offset != Type::OffsetBot &&
1446          offset > arrayOopDesc::length_offset_in_bytes() ) {
1447       offset = Type::OffsetBot; // Flatten constant access into array body only
1448       tj = ta = TypeAryPtr::make(ptr, ta->ary(), ta->klass(), true, offset, ta->instance_id());
1449     }
1450   } else if( ta && _AliasLevel >= 2 ) {
1451     // For arrays indexed by constant indices, we flatten the alias
1452     // space to include all of the array body.  Only the header, klass
1453     // and array length can be accessed un-aliased.


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


1767     if (alias_type(i)->adr_type() == flat) {
1768       idx = i;
1769       break;
1770     }
1771   }
1772 
1773   if (idx == AliasIdxTop) {
1774     if (no_create)  return NULL;
1775     // Grow the array if necessary.
1776     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1777     // Add a new alias type.
1778     idx = _num_alias_types++;
1779     _alias_types[idx]->Init(idx, flat);
1780     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1781     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1782     if (flat->isa_instptr()) {
1783       if (flat->offset() == java_lang_Class::klass_offset_in_bytes()
1784           && flat->is_instptr()->klass() == env()->Class_klass())
1785         alias_type(idx)->set_rewritable(false);
1786     }

1787     if (flat->isa_aryptr()) {
1788 #ifdef ASSERT
1789       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1790       // (T_BYTE has the weakest alignment and size restrictions...)
1791       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1792 #endif

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






1795       }
1796     }
1797     if (flat->isa_klassptr()) {
1798       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1799         alias_type(idx)->set_rewritable(false);
1800       if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1801         alias_type(idx)->set_rewritable(false);
1802       if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1803         alias_type(idx)->set_rewritable(false);
1804       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1805         alias_type(idx)->set_rewritable(false);
1806     }
1807     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1808     // but the base pointer type is not distinctive enough to identify
1809     // references into JavaThread.)
1810 
1811     // Check for final fields.
1812     const TypeInstPtr* tinst = flat->isa_instptr();
1813     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {
1814       ciField* field;
1815       if (tinst->const_oop() != NULL &&
1816           tinst->klass() == ciEnv::current()->Class_klass() &&
1817           tinst->offset() >= (tinst->klass()->as_instance_klass()->size_helper() * wordSize)) {
1818         // static field
1819         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1820         field = k->get_field_by_offset(tinst->offset(), true);




1821       } else {
1822         ciInstanceKlass *k = tinst->klass()->as_instance_klass();
1823         field = k->get_field_by_offset(tinst->offset(), false);
1824       }

1825       assert(field == NULL ||
1826              original_field == NULL ||
1827              (field->holder() == original_field->holder() &&
1828               field->offset() == original_field->offset() &&
1829               field->is_static() == original_field->is_static()), "wrong field?");
1830       // Set field() and is_rewritable() attributes.
1831       if (field != NULL)  alias_type(idx)->set_field(field);
1832     }
1833   }
1834 
1835   // Fill the cache for next time.
1836   ace->_adr_type = adr_type;
1837   ace->_index    = idx;
1838   assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");
1839 
1840   // Might as well try to fill the cache for the flattened version, too.
1841   AliasCacheEntry* face = probe_alias_cache(flat);
1842   if (face->_adr_type == NULL) {
1843     face->_adr_type = flat;
1844     face->_index    = idx;
1845     assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1846   }
1847 
1848   return alias_type(idx);
1849 }
1850 
1851 
1852 Compile::AliasType* Compile::alias_type(ciField* field) {
1853   const TypeOopPtr* t;


1986   }
1987   assert(range_check_cast_count() == 0, "should be empty");
1988 }
1989 
1990 void Compile::add_opaque4_node(Node* n) {
1991   assert(n->Opcode() == Op_Opaque4, "Opaque4 only");
1992   assert(!_opaque4_nodes->contains(n), "duplicate entry in Opaque4 list");
1993   _opaque4_nodes->append(n);
1994 }
1995 
1996 // Remove all Opaque4 nodes.
1997 void Compile::remove_opaque4_nodes(PhaseIterGVN &igvn) {
1998   for (int i = opaque4_count(); i > 0; i--) {
1999     Node* opaq = opaque4_node(i-1);
2000     assert(opaq->Opcode() == Op_Opaque4, "Opaque4 only");
2001     igvn.replace_node(opaq, opaq->in(2));
2002   }
2003   assert(opaque4_count() == 0, "should be empty");
2004 }
2005 































2006 // StringOpts and late inlining of string methods
2007 void Compile::inline_string_calls(bool parse_time) {
2008   {
2009     // remove useless nodes to make the usage analysis simpler
2010     ResourceMark rm;
2011     PhaseRemoveUseless pru(initial_gvn(), for_igvn());
2012   }
2013 
2014   {
2015     ResourceMark rm;
2016     print_method(PHASE_BEFORE_STRINGOPTS, 3);
2017     PhaseStringOpts pso(initial_gvn(), for_igvn());
2018     print_method(PHASE_AFTER_STRINGOPTS, 3);
2019   }
2020 
2021   // now inline anything that we skipped the first time around
2022   if (!parse_time) {
2023     _late_inlines_pos = _late_inlines.length();
2024   }
2025 


2260   remove_speculative_types(igvn);
2261 
2262   // No more new expensive nodes will be added to the list from here
2263   // so keep only the actual candidates for optimizations.
2264   cleanup_expensive_nodes(igvn);
2265 
2266   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2267     Compile::TracePhase tp("", &timers[_t_renumberLive]);
2268     initial_gvn()->replace_with(&igvn);
2269     for_igvn()->clear();
2270     Unique_Node_List new_worklist(C->comp_arena());
2271     {
2272       ResourceMark rm;
2273       PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2274     }
2275     set_for_igvn(&new_worklist);
2276     igvn = PhaseIterGVN(initial_gvn());
2277     igvn.optimize();
2278   }
2279 





2280   // Perform escape analysis
2281   if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2282     if (has_loops()) {
2283       // Cleanup graph (remove dead nodes).
2284       TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2285       PhaseIdealLoop::optimize(igvn, LoopOptsNone);
2286       if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2287       if (failing())  return;
2288     }
2289     ConnectionGraph::do_analysis(this, &igvn);
2290 
2291     if (failing())  return;
2292 
2293     // Optimize out fields loads from scalar replaceable allocations.
2294     igvn.optimize();
2295     print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2296 
2297     if (failing())  return;
2298 
2299     if (congraph() != NULL && macro_count() > 0) {


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


2740             // Accumulate any precedence edges
2741             if (mem->in(i) != NULL) {
2742               n->add_prec(mem->in(i));
2743             }
2744           }
2745           // Everything above this point has been processed.
2746           done = true;
2747         }
2748         // Eliminate the previous StoreCM
2749         prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
2750         assert(mem->outcnt() == 0, "should be dead");
2751         mem->disconnect_inputs(NULL, this);
2752       } else {
2753         prev = mem;
2754       }
2755       mem = prev->in(MemNode::Memory);
2756     }
2757   }
2758 }
2759 

2760 //------------------------------final_graph_reshaping_impl----------------------
2761 // Implement items 1-5 from final_graph_reshaping below.
2762 void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {
2763 
2764   if ( n->outcnt() == 0 ) return; // dead node
2765   uint nop = n->Opcode();
2766 
2767   // Check for 2-input instruction with "last use" on right input.
2768   // Swap to left input.  Implements item (2).
2769   if( n->req() == 3 &&          // two-input instruction
2770       n->in(1)->outcnt() > 1 && // left use is NOT a last use
2771       (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
2772       n->in(2)->outcnt() == 1 &&// right use IS a last use
2773       !n->in(2)->is_Con() ) {   // right use is not a constant
2774     // Check for commutative opcode
2775     switch( nop ) {
2776     case Op_AddI:  case Op_AddF:  case Op_AddD:  case Op_AddL:
2777     case Op_MaxI:  case Op_MinI:
2778     case Op_MulI:  case Op_MulF:  case Op_MulD:  case Op_MulL:
2779     case Op_AndL:  case Op_XorL:  case Op_OrL:


3474           // Replace all nodes with identical edges as m with m
3475           k->subsume_by(m, this);
3476         }
3477       }
3478     }
3479     break;
3480   }
3481   case Op_CmpUL: {
3482     if (!Matcher::has_match_rule(Op_CmpUL)) {
3483       // No support for unsigned long comparisons
3484       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3485       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3486       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3487       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3488       Node* andl = new AndLNode(orl, remove_sign_mask);
3489       Node* cmp = new CmpLNode(andl, n->in(2));
3490       n->subsume_by(cmp, this);
3491     }
3492     break;
3493   }








3494   default:
3495     assert(!n->is_Call(), "");
3496     assert(!n->is_Mem(), "");
3497     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3498     break;
3499   }
3500 }
3501 
3502 //------------------------------final_graph_reshaping_walk---------------------
3503 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3504 // requires that the walk visits a node's inputs before visiting the node.
3505 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
3506   ResourceArea *area = Thread::current()->resource_area();
3507   Unique_Node_List sfpt(area);
3508 
3509   frc._visited.set(root->_idx); // first, mark node as visited
3510   uint cnt = root->req();
3511   Node *n = root;
3512   uint  i = 0;
3513   while (true) {


4134   int offset = n->constant_offset() - table_base_offset();
4135 
4136   MacroAssembler _masm(&cb);
4137   address* jump_table_base = (address*) (_masm.code()->consts()->start() + offset);
4138 
4139   for (uint i = 0; i < n->outcnt(); i++) {
4140     address* constant_addr = &jump_table_base[i];
4141     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));
4142     *constant_addr = cb.consts()->target(*labels.at(i), (address) constant_addr);
4143     cb.consts()->relocate((address) constant_addr, relocInfo::internal_word_type);
4144   }
4145 }
4146 
4147 //----------------------------static_subtype_check-----------------------------
4148 // Shortcut important common cases when superklass is exact:
4149 // (0) superklass is java.lang.Object (can occur in reflective code)
4150 // (1) subklass is already limited to a subtype of superklass => always ok
4151 // (2) subklass does not overlap with superklass => always fail
4152 // (3) superklass has NO subtypes and we can check with a simple compare.
4153 int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {
4154   if (StressReflectiveCode) {
4155     return SSC_full_test;       // Let caller generate the general case.
4156   }
4157 
4158   if (superk == env()->Object_klass()) {
4159     return SSC_always_true;     // (0) this test cannot fail
4160   }
4161 
4162   ciType* superelem = superk;
4163   if (superelem->is_array_klass())
4164     superelem = superelem->as_array_klass()->base_element_type();
4165 
4166   if (!subk->is_interface()) {  // cannot trust static interface types yet
4167     if (subk->is_subtype_of(superk)) {
4168       return SSC_always_true;   // (1) false path dead; no dynamic test needed
4169     }
4170     if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&
4171         !superk->is_subtype_of(subk)) {
4172       return SSC_always_false;
4173     }
4174   }


4581     worklist.clear();
4582     worklist.push(root());
4583     for (uint next = 0; next < worklist.size(); ++next) {
4584       Node *n  = worklist.at(next);
4585       const Type* t = igvn.type_or_null(n);
4586       assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");
4587       if (n->is_Type()) {
4588         t = n->as_Type()->type();
4589         assert(t == t->remove_speculative(), "no more speculative types");
4590       }
4591       uint max = n->len();
4592       for( uint i = 0; i < max; ++i ) {
4593         Node *m = n->in(i);
4594         if (not_a_node(m))  continue;
4595         worklist.push(m);
4596       }
4597     }
4598     igvn.check_no_speculative_types();
4599 #endif
4600   }





















4601 }
4602 
4603 // Auxiliary method to support randomized stressing/fuzzing.
4604 //
4605 // This method can be called the arbitrary number of times, with current count
4606 // as the argument. The logic allows selecting a single candidate from the
4607 // running list of candidates as follows:
4608 //    int count = 0;
4609 //    Cand* selected = null;
4610 //    while(cand = cand->next()) {
4611 //      if (randomized_select(++count)) {
4612 //        selected = cand;
4613 //      }
4614 //    }
4615 //
4616 // Including count equalizes the chances any candidate is "selected".
4617 // This is useful when we don't have the complete list of candidates to choose
4618 // from uniformly. In this case, we need to adjust the randomicity of the
4619 // selection, or else we will end up biasing the selection towards the latter
4620 // 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


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


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


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


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


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


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


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


1809     if (alias_type(i)->adr_type() == flat) {
1810       idx = i;
1811       break;
1812     }
1813   }
1814 
1815   if (idx == AliasIdxTop) {
1816     if (no_create)  return NULL;
1817     // Grow the array if necessary.
1818     if (_num_alias_types == _max_alias_types)  grow_alias_types();
1819     // Add a new alias type.
1820     idx = _num_alias_types++;
1821     _alias_types[idx]->Init(idx, flat);
1822     if (flat == TypeInstPtr::KLASS)  alias_type(idx)->set_rewritable(false);
1823     if (flat == TypeAryPtr::RANGE)   alias_type(idx)->set_rewritable(false);
1824     if (flat->isa_instptr()) {
1825       if (flat->offset() == java_lang_Class::klass_offset_in_bytes()
1826           && flat->is_instptr()->klass() == env()->Class_klass())
1827         alias_type(idx)->set_rewritable(false);
1828     }
1829     ciField* field = NULL;
1830     if (flat->isa_aryptr()) {
1831 #ifdef ASSERT
1832       const int header_size_min  = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1833       // (T_BYTE has the weakest alignment and size restrictions...)
1834       assert(flat->offset() < header_size_min, "array body reference must be OffsetBot");
1835 #endif
1836       const Type* elemtype = flat->is_aryptr()->elem();
1837       if (flat->offset() == TypePtr::OffsetBot) {
1838         alias_type(idx)->set_element(elemtype);
1839       }
1840       int field_offset = flat->is_aryptr()->field_offset().get();
1841       if (elemtype->isa_valuetype() && field_offset != Type::OffsetBot) {
1842         ciValueKlass* vk = elemtype->is_valuetype()->value_klass();
1843         field_offset += vk->first_field_offset();
1844         field = vk->get_field_by_offset(field_offset, false);
1845       }
1846     }
1847     if (flat->isa_klassptr()) {
1848       if (flat->offset() == in_bytes(Klass::super_check_offset_offset()))
1849         alias_type(idx)->set_rewritable(false);
1850       if (flat->offset() == in_bytes(Klass::modifier_flags_offset()))
1851         alias_type(idx)->set_rewritable(false);
1852       if (flat->offset() == in_bytes(Klass::access_flags_offset()))
1853         alias_type(idx)->set_rewritable(false);
1854       if (flat->offset() == in_bytes(Klass::java_mirror_offset()))
1855         alias_type(idx)->set_rewritable(false);
1856     }
1857     // %%% (We would like to finalize JavaThread::threadObj_offset(),
1858     // but the base pointer type is not distinctive enough to identify
1859     // references into JavaThread.)
1860 
1861     // Check for final fields.
1862     const TypeInstPtr* tinst = flat->isa_instptr();
1863     if (tinst && tinst->offset() >= instanceOopDesc::base_offset_in_bytes()) {

1864       if (tinst->const_oop() != NULL &&
1865           tinst->klass() == ciEnv::current()->Class_klass() &&
1866           tinst->offset() >= (tinst->klass()->as_instance_klass()->size_helper() * wordSize)) {
1867         // static field
1868         ciInstanceKlass* k = tinst->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
1869         field = k->get_field_by_offset(tinst->offset(), true);
1870       } else if (tinst->klass()->is_valuetype()) {
1871         // Value type field
1872         ciValueKlass* vk = tinst->value_klass();
1873         field = vk->get_field_by_offset(tinst->offset(), false);
1874       } else {
1875         ciInstanceKlass* k = tinst->klass()->as_instance_klass();
1876         field = k->get_field_by_offset(tinst->offset(), false);
1877       }
1878     }
1879     assert(field == NULL ||
1880            original_field == NULL ||
1881            (field->holder() == original_field->holder() &&
1882             field->offset() == original_field->offset() &&
1883             field->is_static() == original_field->is_static()), "wrong field?");
1884     // Set field() and is_rewritable() attributes.
1885     if (field != NULL)  alias_type(idx)->set_field(field);
1886   }

1887 
1888   // Fill the cache for next time.
1889   ace->_adr_type = adr_type;
1890   ace->_index    = idx;
1891   assert(alias_type(adr_type) == alias_type(idx),  "type must be installed");
1892 
1893   // Might as well try to fill the cache for the flattened version, too.
1894   AliasCacheEntry* face = probe_alias_cache(flat);
1895   if (face->_adr_type == NULL) {
1896     face->_adr_type = flat;
1897     face->_index    = idx;
1898     assert(alias_type(flat) == alias_type(idx), "flat type must work too");
1899   }
1900 
1901   return alias_type(idx);
1902 }
1903 
1904 
1905 Compile::AliasType* Compile::alias_type(ciField* field) {
1906   const TypeOopPtr* t;


2039   }
2040   assert(range_check_cast_count() == 0, "should be empty");
2041 }
2042 
2043 void Compile::add_opaque4_node(Node* n) {
2044   assert(n->Opcode() == Op_Opaque4, "Opaque4 only");
2045   assert(!_opaque4_nodes->contains(n), "duplicate entry in Opaque4 list");
2046   _opaque4_nodes->append(n);
2047 }
2048 
2049 // Remove all Opaque4 nodes.
2050 void Compile::remove_opaque4_nodes(PhaseIterGVN &igvn) {
2051   for (int i = opaque4_count(); i > 0; i--) {
2052     Node* opaq = opaque4_node(i-1);
2053     assert(opaq->Opcode() == Op_Opaque4, "Opaque4 only");
2054     igvn.replace_node(opaq, opaq->in(2));
2055   }
2056   assert(opaque4_count() == 0, "should be empty");
2057 }
2058 
2059 void Compile::add_value_type(Node* n) {
2060   assert(n->is_ValueTypeBase(), "unexpected node");
2061   if (_value_type_nodes != NULL) {
2062     _value_type_nodes->push(n);
2063   }
2064 }
2065 
2066 void Compile::remove_value_type(Node* n) {
2067   assert(n->is_ValueTypeBase(), "unexpected node");
2068   if (_value_type_nodes != NULL) {
2069     _value_type_nodes->remove(n);
2070   }
2071 }
2072 
2073 void Compile::process_value_types(PhaseIterGVN &igvn) {
2074   // Make value types scalar in safepoints
2075   while (_value_type_nodes->size() != 0) {
2076     ValueTypeBaseNode* vt = _value_type_nodes->pop()->as_ValueTypeBase();
2077     vt->make_scalar_in_safepoints(&igvn);
2078     if (vt->is_ValueTypePtr()) {
2079       igvn.replace_node(vt, vt->get_oop());
2080     } else {
2081       if (vt->outcnt() == 0) {
2082         igvn.remove_dead_node(vt);
2083       }
2084     }
2085   }
2086   _value_type_nodes = NULL;
2087   igvn.optimize();
2088 }
2089 
2090 // StringOpts and late inlining of string methods
2091 void Compile::inline_string_calls(bool parse_time) {
2092   {
2093     // remove useless nodes to make the usage analysis simpler
2094     ResourceMark rm;
2095     PhaseRemoveUseless pru(initial_gvn(), for_igvn());
2096   }
2097 
2098   {
2099     ResourceMark rm;
2100     print_method(PHASE_BEFORE_STRINGOPTS, 3);
2101     PhaseStringOpts pso(initial_gvn(), for_igvn());
2102     print_method(PHASE_AFTER_STRINGOPTS, 3);
2103   }
2104 
2105   // now inline anything that we skipped the first time around
2106   if (!parse_time) {
2107     _late_inlines_pos = _late_inlines.length();
2108   }
2109 


2344   remove_speculative_types(igvn);
2345 
2346   // No more new expensive nodes will be added to the list from here
2347   // so keep only the actual candidates for optimizations.
2348   cleanup_expensive_nodes(igvn);
2349 
2350   if (!failing() && RenumberLiveNodes && live_nodes() + NodeLimitFudgeFactor < unique()) {
2351     Compile::TracePhase tp("", &timers[_t_renumberLive]);
2352     initial_gvn()->replace_with(&igvn);
2353     for_igvn()->clear();
2354     Unique_Node_List new_worklist(C->comp_arena());
2355     {
2356       ResourceMark rm;
2357       PhaseRenumberLive prl = PhaseRenumberLive(initial_gvn(), for_igvn(), &new_worklist);
2358     }
2359     set_for_igvn(&new_worklist);
2360     igvn = PhaseIterGVN(initial_gvn());
2361     igvn.optimize();
2362   }
2363 
2364   if (_value_type_nodes->size() > 0) {
2365     // Do this once all inlining is over to avoid getting inconsistent debug info
2366     process_value_types(igvn);
2367   }
2368 
2369   // Perform escape analysis
2370   if (_do_escape_analysis && ConnectionGraph::has_candidates(this)) {
2371     if (has_loops()) {
2372       // Cleanup graph (remove dead nodes).
2373       TracePhase tp("idealLoop", &timers[_t_idealLoop]);
2374       PhaseIdealLoop::optimize(igvn, LoopOptsNone);
2375       if (major_progress()) print_method(PHASE_PHASEIDEAL_BEFORE_EA, 2);
2376       if (failing())  return;
2377     }
2378     ConnectionGraph::do_analysis(this, &igvn);
2379 
2380     if (failing())  return;
2381 
2382     // Optimize out fields loads from scalar replaceable allocations.
2383     igvn.optimize();
2384     print_method(PHASE_ITER_GVN_AFTER_EA, 2);
2385 
2386     if (failing())  return;
2387 
2388     if (congraph() != NULL && macro_count() > 0) {


2514     C->remove_opaque4_nodes(igvn);
2515     igvn.optimize();
2516   }
2517 
2518   DEBUG_ONLY( _modified_nodes = NULL; )
2519  } // (End scope of igvn; run destructor if necessary for asserts.)
2520 
2521  process_print_inlining();
2522  // A method with only infinite loops has no edges entering loops from root
2523  {
2524    TracePhase tp("graphReshape", &timers[_t_graphReshaping]);
2525    if (final_graph_reshaping()) {
2526      assert(failing(), "must bail out w/ explicit message");
2527      return;
2528    }
2529  }
2530 
2531  print_method(PHASE_OPTIMIZE_FINISHED, 2);
2532 }
2533 

2534 //------------------------------Code_Gen---------------------------------------
2535 // Given a graph, generate code for it
2536 void Compile::Code_Gen() {
2537   if (failing()) {
2538     return;
2539   }
2540 
2541   // Perform instruction selection.  You might think we could reclaim Matcher
2542   // memory PDQ, but actually the Matcher is used in generating spill code.
2543   // Internals of the Matcher (including some VectorSets) must remain live
2544   // for awhile - thus I cannot reclaim Matcher memory lest a VectorSet usage
2545   // set a bit in reclaimed memory.
2546 
2547   // In debug mode can dump m._nodes.dump() for mapping of ideal to machine
2548   // nodes.  Mapping is only valid at the root of each matched subtree.
2549   NOT_PRODUCT( verify_graph_edges(); )
2550 
2551   Matcher matcher;
2552   _matcher = &matcher;
2553   {


2828             // Accumulate any precedence edges
2829             if (mem->in(i) != NULL) {
2830               n->add_prec(mem->in(i));
2831             }
2832           }
2833           // Everything above this point has been processed.
2834           done = true;
2835         }
2836         // Eliminate the previous StoreCM
2837         prev->set_req(MemNode::Memory, mem->in(MemNode::Memory));
2838         assert(mem->outcnt() == 0, "should be dead");
2839         mem->disconnect_inputs(NULL, this);
2840       } else {
2841         prev = mem;
2842       }
2843       mem = prev->in(MemNode::Memory);
2844     }
2845   }
2846 }
2847 
2848 
2849 //------------------------------final_graph_reshaping_impl----------------------
2850 // Implement items 1-5 from final_graph_reshaping below.
2851 void Compile::final_graph_reshaping_impl( Node *n, Final_Reshape_Counts &frc) {
2852 
2853   if ( n->outcnt() == 0 ) return; // dead node
2854   uint nop = n->Opcode();
2855 
2856   // Check for 2-input instruction with "last use" on right input.
2857   // Swap to left input.  Implements item (2).
2858   if( n->req() == 3 &&          // two-input instruction
2859       n->in(1)->outcnt() > 1 && // left use is NOT a last use
2860       (!n->in(1)->is_Phi() || n->in(1)->in(2) != n) && // it is not data loop
2861       n->in(2)->outcnt() == 1 &&// right use IS a last use
2862       !n->in(2)->is_Con() ) {   // right use is not a constant
2863     // Check for commutative opcode
2864     switch( nop ) {
2865     case Op_AddI:  case Op_AddF:  case Op_AddD:  case Op_AddL:
2866     case Op_MaxI:  case Op_MinI:
2867     case Op_MulI:  case Op_MulF:  case Op_MulD:  case Op_MulL:
2868     case Op_AndL:  case Op_XorL:  case Op_OrL:


3563           // Replace all nodes with identical edges as m with m
3564           k->subsume_by(m, this);
3565         }
3566       }
3567     }
3568     break;
3569   }
3570   case Op_CmpUL: {
3571     if (!Matcher::has_match_rule(Op_CmpUL)) {
3572       // No support for unsigned long comparisons
3573       ConINode* sign_pos = new ConINode(TypeInt::make(BitsPerLong - 1));
3574       Node* sign_bit_mask = new RShiftLNode(n->in(1), sign_pos);
3575       Node* orl = new OrLNode(n->in(1), sign_bit_mask);
3576       ConLNode* remove_sign_mask = new ConLNode(TypeLong::make(max_jlong));
3577       Node* andl = new AndLNode(orl, remove_sign_mask);
3578       Node* cmp = new CmpLNode(andl, n->in(2));
3579       n->subsume_by(cmp, this);
3580     }
3581     break;
3582   }
3583 #ifdef ASSERT
3584   case Op_ValueTypePtr:
3585   case Op_ValueType: {
3586     n->dump(-1);
3587     assert(false, "value type node was not removed");
3588     break;
3589   }
3590 #endif
3591   default:
3592     assert(!n->is_Call(), "");
3593     assert(!n->is_Mem(), "");
3594     assert(nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3595     break;
3596   }
3597 }
3598 
3599 //------------------------------final_graph_reshaping_walk---------------------
3600 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3601 // requires that the walk visits a node's inputs before visiting the node.
3602 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
3603   ResourceArea *area = Thread::current()->resource_area();
3604   Unique_Node_List sfpt(area);
3605 
3606   frc._visited.set(root->_idx); // first, mark node as visited
3607   uint cnt = root->req();
3608   Node *n = root;
3609   uint  i = 0;
3610   while (true) {


4231   int offset = n->constant_offset() - table_base_offset();
4232 
4233   MacroAssembler _masm(&cb);
4234   address* jump_table_base = (address*) (_masm.code()->consts()->start() + offset);
4235 
4236   for (uint i = 0; i < n->outcnt(); i++) {
4237     address* constant_addr = &jump_table_base[i];
4238     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));
4239     *constant_addr = cb.consts()->target(*labels.at(i), (address) constant_addr);
4240     cb.consts()->relocate((address) constant_addr, relocInfo::internal_word_type);
4241   }
4242 }
4243 
4244 //----------------------------static_subtype_check-----------------------------
4245 // Shortcut important common cases when superklass is exact:
4246 // (0) superklass is java.lang.Object (can occur in reflective code)
4247 // (1) subklass is already limited to a subtype of superklass => always ok
4248 // (2) subklass does not overlap with superklass => always fail
4249 // (3) superklass has NO subtypes and we can check with a simple compare.
4250 int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {
4251   if (StressReflectiveCode || superk == NULL || subk == NULL) {
4252     return SSC_full_test;       // Let caller generate the general case.
4253   }
4254 
4255   if (superk == env()->Object_klass()) {
4256     return SSC_always_true;     // (0) this test cannot fail
4257   }
4258 
4259   ciType* superelem = superk;
4260   if (superelem->is_array_klass())
4261     superelem = superelem->as_array_klass()->base_element_type();
4262 
4263   if (!subk->is_interface()) {  // cannot trust static interface types yet
4264     if (subk->is_subtype_of(superk)) {
4265       return SSC_always_true;   // (1) false path dead; no dynamic test needed
4266     }
4267     if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&
4268         !superk->is_subtype_of(subk)) {
4269       return SSC_always_false;
4270     }
4271   }


4678     worklist.clear();
4679     worklist.push(root());
4680     for (uint next = 0; next < worklist.size(); ++next) {
4681       Node *n  = worklist.at(next);
4682       const Type* t = igvn.type_or_null(n);
4683       assert((t == NULL) || (t == t->remove_speculative()), "no more speculative types");
4684       if (n->is_Type()) {
4685         t = n->as_Type()->type();
4686         assert(t == t->remove_speculative(), "no more speculative types");
4687       }
4688       uint max = n->len();
4689       for( uint i = 0; i < max; ++i ) {
4690         Node *m = n->in(i);
4691         if (not_a_node(m))  continue;
4692         worklist.push(m);
4693       }
4694     }
4695     igvn.check_no_speculative_types();
4696 #endif
4697   }
4698 }
4699 
4700 Node* Compile::optimize_acmp(PhaseGVN* phase, Node* a, Node* b) {
4701   const TypeInstPtr* ta = phase->type(a)->isa_instptr();
4702   const TypeInstPtr* tb = phase->type(b)->isa_instptr();
4703   if (!EnableValhalla || ta == NULL || tb == NULL ||
4704       ta->is_zero_type() || tb->is_zero_type() ||
4705       !ta->can_be_value_type() || !tb->can_be_value_type()) {
4706     // Use old acmp if one operand is null or not a value type
4707     return new CmpPNode(a, b);
4708   } else if (ta->is_valuetypeptr() || tb->is_valuetypeptr()) {
4709     // We know that one operand is a value type. Therefore,
4710     // new acmp will only return true if both operands are NULL.
4711     // Check if both operands are null by or'ing the oops.
4712     a = phase->transform(new CastP2XNode(NULL, a));
4713     b = phase->transform(new CastP2XNode(NULL, b));
4714     a = phase->transform(new OrXNode(a, b));
4715     return new CmpXNode(a, phase->MakeConX(0));
4716   }
4717   // Use new acmp
4718   return NULL;
4719 }
4720 
4721 // Auxiliary method to support randomized stressing/fuzzing.
4722 //
4723 // This method can be called the arbitrary number of times, with current count
4724 // as the argument. The logic allows selecting a single candidate from the
4725 // running list of candidates as follows:
4726 //    int count = 0;
4727 //    Cand* selected = null;
4728 //    while(cand = cand->next()) {
4729 //      if (randomized_select(++count)) {
4730 //        selected = cand;
4731 //      }
4732 //    }
4733 //
4734 // Including count equalizes the chances any candidate is "selected".
4735 // This is useful when we don't have the complete list of candidates to choose
4736 // from uniformly. In this case, we need to adjust the randomicity of the
4737 // selection, or else we will end up biasing the selection towards the latter
4738 // candidates.


< prev index next >