66 #include "runtime/signature.hpp"
67 #include "runtime/stubRoutines.hpp"
68 #include "runtime/timer.hpp"
69 #include "utilities/copy.hpp"
70 #if defined AD_MD_HPP
71 # include AD_MD_HPP
72 #elif defined TARGET_ARCH_MODEL_x86_32
73 # include "adfiles/ad_x86_32.hpp"
74 #elif defined TARGET_ARCH_MODEL_x86_64
75 # include "adfiles/ad_x86_64.hpp"
76 #elif defined TARGET_ARCH_MODEL_aarch64
77 # include "adfiles/ad_aarch64.hpp"
78 #elif defined TARGET_ARCH_MODEL_sparc
79 # include "adfiles/ad_sparc.hpp"
80 #elif defined TARGET_ARCH_MODEL_zero
81 # include "adfiles/ad_zero.hpp"
82 #elif defined TARGET_ARCH_MODEL_ppc_64
83 # include "adfiles/ad_ppc_64.hpp"
84 #endif
85
86 // -------------------- Compile::mach_constant_base_node -----------------------
87 // Constant table base node singleton.
88 MachConstantBaseNode* Compile::mach_constant_base_node() {
89 if (_mach_constant_base_node == NULL) {
90 _mach_constant_base_node = new (C) MachConstantBaseNode();
91 _mach_constant_base_node->add_req(C->root());
92 }
93 return _mach_constant_base_node;
94 }
95
96
97 /// Support for intrinsics.
98
99 // Return the index at which m must be inserted (or already exists).
100 // The sort order is by the address of the ciMethod, with is_virtual as minor key.
101 int Compile::intrinsic_insertion_index(ciMethod* m, bool is_virtual) {
102 #ifdef ASSERT
103 for (int i = 1; i < _intrinsics->length(); i++) {
104 CallGenerator* cg1 = _intrinsics->at(i-1);
105 CallGenerator* cg2 = _intrinsics->at(i);
388 // We're done with a parsing phase. Replaced nodes are not valid
389 // beyond that point.
390 n->as_SafePoint()->delete_replaced_nodes();
391 }
392 // Use raw traversal of out edges since this code removes out edges
393 int max = n->outcnt();
394 for (int j = 0; j < max; ++j) {
395 Node* child = n->raw_out(j);
396 if (! useful.member(child)) {
397 assert(!child->is_top() || child != top(),
398 "If top is cached in Compile object it is in useful list");
399 // Only need to remove this out-edge to the useless node
400 n->raw_del_out(j);
401 --j;
402 --max;
403 }
404 }
405 if (n->outcnt() == 1 && n->has_special_unique_user()) {
406 record_for_igvn(n->unique_out());
407 }
408 }
409 // Remove useless macro and predicate opaq nodes
410 for (int i = C->macro_count()-1; i >= 0; i--) {
411 Node* n = C->macro_node(i);
412 if (!useful.member(n)) {
413 remove_macro_node(n);
414 }
415 }
416 // Remove useless CastII nodes with range check dependency
417 for (int i = range_check_cast_count() - 1; i >= 0; i--) {
418 Node* cast = range_check_cast_node(i);
419 if (!useful.member(cast)) {
420 remove_range_check_cast(cast);
421 }
422 }
423 // Remove useless expensive node
424 for (int i = C->expensive_count()-1; i >= 0; i--) {
425 Node* n = C->expensive_node(i);
426 if (!useful.member(n)) {
427 remove_expensive_node(n);
428 }
429 }
430 // clean up the late inline lists
431 remove_useless_late_inlines(&_string_late_inlines, useful);
432 remove_useless_late_inlines(&_boxing_late_inlines, useful);
433 remove_useless_late_inlines(&_late_inlines, useful);
434 debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
435 }
436
437 //------------------------------frame_size_in_words-----------------------------
438 // frame_slots in units of words
439 int Compile::frame_size_in_words() const {
440 // shift is 0 in LP32 and 1 in LP64
441 const int shift = (LogBytesPerWord - LogBytesPerInt);
442 int words = _frame_slots >> shift;
443 assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" );
444 return words;
445 }
446
447 // To bang the stack of this compiled method we use the stack size
448 // that the interpreter would need in case of a deoptimization. This
449 // removes the need to bang the stack in the deoptimization blob which
753
754 // Put top into the hash table ASAP.
755 initial_gvn()->transform_no_reclaim(top());
756
757 // Set up tf(), start(), and find a CallGenerator.
758 CallGenerator* cg = NULL;
759 if (is_osr_compilation()) {
760 const TypeTuple *domain = StartOSRNode::osr_domain();
761 const TypeTuple *range = TypeTuple::make_range(method()->signature());
762 init_tf(TypeFunc::make(domain, range));
763 StartNode* s = new (this) StartOSRNode(root(), domain);
764 initial_gvn()->set_type_bottom(s);
765 init_start(s);
766 cg = CallGenerator::for_osr(method(), entry_bci());
767 } else {
768 // Normal case.
769 init_tf(TypeFunc::make(method()));
770 StartNode* s = new (this) StartNode(root(), tf()->domain());
771 initial_gvn()->set_type_bottom(s);
772 init_start(s);
773 if (method()->intrinsic_id() == vmIntrinsics::_Reference_get && UseG1GC) {
774 // With java.lang.ref.reference.get() we must go through the
775 // intrinsic when G1 is enabled - even when get() is the root
776 // method of the compile - so that, if necessary, the value in
777 // the referent field of the reference object gets recorded by
778 // the pre-barrier code.
779 // Specifically, if G1 is enabled, the value in the referent
780 // field is recorded by the G1 SATB pre barrier. This will
781 // result in the referent being marked live and the reference
782 // object removed from the list of discovered references during
783 // reference processing.
784 cg = find_intrinsic(method(), false);
785 }
786 if (cg == NULL) {
787 float past_uses = method()->interpreter_invocation_count();
788 float expected_uses = past_uses;
789 cg = CallGenerator::for_inline(method(), expected_uses);
790 }
791 }
792 if (failing()) return;
793 if (cg == NULL) {
1146 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, grow_ats);
1147 Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);
1148 {
1149 for (int i = 0; i < grow_ats; i++) _alias_types[i] = &ats[i];
1150 }
1151 // Initialize the first few types.
1152 _alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);
1153 _alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);
1154 _alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);
1155 _num_alias_types = AliasIdxRaw+1;
1156 // Zero out the alias type cache.
1157 Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));
1158 // A NULL adr_type hits in the cache right away. Preload the right answer.
1159 probe_alias_cache(NULL)->_index = AliasIdxTop;
1160
1161 _intrinsics = NULL;
1162 _macro_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1163 _predicate_opaqs = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1164 _expensive_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1165 _range_check_casts = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1166 register_library_intrinsics();
1167 #ifdef ASSERT
1168 _type_verify_symmetry = true;
1169 #endif
1170 }
1171
1172 //---------------------------init_start----------------------------------------
1173 // Install the StartNode on this compile object.
1174 void Compile::init_start(StartNode* s) {
1175 if (failing())
1176 return; // already failing
1177 assert(s == start(), "");
1178 }
1179
1180 StartNode* Compile::start() const {
1181 assert(!failing(), "");
1182 for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {
1183 Node* start = root()->fast_out(i);
1184 if( start->is_Start() )
1185 return start->as_Start();
2283 PhaseIdealLoop ideal_loop( igvn, true);
2284 loop_opts_cnt--;
2285 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2286 if (failing()) return;
2287 }
2288 }
2289
2290 {
2291 // Verify that all previous optimizations produced a valid graph
2292 // at least to this point, even if no loop optimizations were done.
2293 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2294 PhaseIdealLoop::verify(igvn);
2295 }
2296
2297 if (range_check_cast_count() > 0) {
2298 // No more loop optimizations. Remove all range check dependent CastIINodes.
2299 C->remove_range_check_casts(igvn);
2300 igvn.optimize();
2301 }
2302
2303 {
2304 NOT_PRODUCT( TracePhase t2("macroExpand", &_t_macroExpand, TimeCompiler); )
2305 PhaseMacroExpand mex(igvn);
2306 if (mex.expand_macro_nodes()) {
2307 assert(failing(), "must bail out w/ explicit message");
2308 return;
2309 }
2310 }
2311
2312 } // (End scope of igvn; run destructor if necessary for asserts.)
2313
2314 dump_inlining();
2315 // A method with only infinite loops has no edges entering loops from root
2316 {
2317 NOT_PRODUCT( TracePhase t2("graphReshape", &_t_graphReshaping, TimeCompiler); )
2318 if (final_graph_reshaping()) {
2319 assert(failing(), "must bail out w/ explicit message");
2320 return;
2321 }
2322 }
2323
2324 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2325 }
2326
2327
2328 //------------------------------Code_Gen---------------------------------------
2329 // Given a graph, generate code for it
2330 void Compile::Code_Gen() {
2331 if (failing()) {
2726 // case Op_ConvD2L: // handled by leaf call
2727 case Op_ConD:
2728 case Op_CmpD:
2729 case Op_CmpD3:
2730 frc.inc_double_count();
2731 break;
2732 case Op_Opaque1: // Remove Opaque Nodes before matching
2733 case Op_Opaque2: // Remove Opaque Nodes before matching
2734 case Op_Opaque3:
2735 n->subsume_by(n->in(1), this);
2736 break;
2737 case Op_CallStaticJava:
2738 case Op_CallJava:
2739 case Op_CallDynamicJava:
2740 frc.inc_java_call_count(); // Count java call site;
2741 case Op_CallRuntime:
2742 case Op_CallLeaf:
2743 case Op_CallLeafNoFP: {
2744 assert( n->is_Call(), "" );
2745 CallNode *call = n->as_Call();
2746 // Count call sites where the FP mode bit would have to be flipped.
2747 // Do not count uncommon runtime calls:
2748 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
2749 // _new_Java, _new_typeArray, _new_objArray, _rethrow_Java, ...
2750 if( !call->is_CallStaticJava() || !call->as_CallStaticJava()->_name ) {
2751 frc.inc_call_count(); // Count the call site
2752 } else { // See if uncommon argument is shared
2753 Node *n = call->in(TypeFunc::Parms);
2754 int nop = n->Opcode();
2755 // Clone shared simple arguments to uncommon calls, item (1).
2756 if( n->outcnt() > 1 &&
2757 !n->is_Proj() &&
2758 nop != Op_CreateEx &&
2759 nop != Op_CheckCastPP &&
2760 nop != Op_DecodeN &&
2761 nop != Op_DecodeNKlass &&
2762 !n->is_Mem() ) {
2763 Node *x = n->clone();
2764 call->set_req( TypeFunc::Parms, x );
2765 }
2869 if (nn != NULL) {
2870 // Decode a narrow oop to match address
2871 // [R12 + narrow_oop_reg<<3 + offset]
2872 if (t->isa_oopptr()) {
2873 nn = new (this) DecodeNNode(nn, t);
2874 } else {
2875 nn = new (this) DecodeNKlassNode(nn, t);
2876 }
2877 n->set_req(AddPNode::Base, nn);
2878 n->set_req(AddPNode::Address, nn);
2879 if (addp->outcnt() == 0) {
2880 addp->disconnect_inputs(NULL, this);
2881 }
2882 }
2883 }
2884 }
2885 #endif
2886 break;
2887 }
2888
2889 #ifdef _LP64
2890 case Op_CastPP:
2891 if (n->in(1)->is_DecodeN() && Matcher::gen_narrow_oop_implicit_null_checks()) {
2892 Node* in1 = n->in(1);
2893 const Type* t = n->bottom_type();
2894 Node* new_in1 = in1->clone();
2895 new_in1->as_DecodeN()->set_type(t);
2896
2897 if (!Matcher::narrow_oop_use_complex_address()) {
2898 //
2899 // x86, ARM and friends can handle 2 adds in addressing mode
2900 // and Matcher can fold a DecodeN node into address by using
2901 // a narrow oop directly and do implicit NULL check in address:
2902 //
2903 // [R12 + narrow_oop_reg<<3 + offset]
2904 // NullCheck narrow_oop_reg
2905 //
2906 // On other platforms (Sparc) we have to keep new DecodeN node and
2907 // use it to do implicit NULL check in address:
2908 //
2909 // decode_not_null narrow_oop_reg, base_reg
2910 // [base_reg + offset]
2911 // NullCheck base_reg
2912 //
2913 // Pin the new DecodeN node to non-null path on these platform (Sparc)
2914 // to keep the information to which NULL check the new DecodeN node
2915 // corresponds to use it as value in implicit_null_check().
2916 //
2917 new_in1->set_req(0, n->in(0));
2918 }
2919
2920 n->subsume_by(new_in1, this);
2921 if (in1->outcnt() == 0) {
2922 in1->disconnect_inputs(NULL, this);
2923 }
2924 }
2925 break;
2926
2927 case Op_CmpP:
2928 // Do this transformation here to preserve CmpPNode::sub() and
2929 // other TypePtr related Ideal optimizations (for example, ptr nullness).
2930 if (n->in(1)->is_DecodeNarrowPtr() || n->in(2)->is_DecodeNarrowPtr()) {
2931 Node* in1 = n->in(1);
2932 Node* in2 = n->in(2);
2933 if (!in1->is_DecodeNarrowPtr()) {
2934 in2 = in1;
2935 in1 = n->in(2);
2936 }
2937 assert(in1->is_DecodeNarrowPtr(), "sanity");
2938
2939 Node* new_in2 = NULL;
2940 if (in2->is_DecodeNarrowPtr()) {
2941 assert(in2->Opcode() == in1->Opcode(), "must be same node type");
2942 new_in2 = in2->in(1);
2943 } else if (in2->Opcode() == Op_ConP) {
2944 const Type* t = in2->bottom_type();
2945 if (t == TypePtr::NULL_PTR) {
2946 assert(in1->is_DecodeN(), "compare klass to null?");
3174 }
3175 } else {
3176 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
3177 Node* shift = new (this) AndINode(in2, ConNode::make(this, TypeInt::make(mask)));
3178 n->set_req(2, shift);
3179 }
3180 }
3181 if (in2->outcnt() == 0) { // Remove dead node
3182 in2->disconnect_inputs(NULL, this);
3183 }
3184 }
3185 break;
3186 case Op_MemBarStoreStore:
3187 case Op_MemBarRelease:
3188 // Break the link with AllocateNode: it is no longer useful and
3189 // confuses register allocation.
3190 if (n->req() > MemBarNode::Precedent) {
3191 n->set_req(MemBarNode::Precedent, top());
3192 }
3193 break;
3194 default:
3195 assert( !n->is_Call(), "" );
3196 assert( !n->is_Mem(), "" );
3197 assert( nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3198 break;
3199 }
3200
3201 // Collect CFG split points
3202 if (n->is_MultiBranch())
3203 frc._tests.push(n);
3204 }
3205
3206 //------------------------------final_graph_reshaping_walk---------------------
3207 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3208 // requires that the walk visits a node's inputs before visiting the node.
3209 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
3210 ResourceArea *area = Thread::current()->resource_area();
3211 Unique_Node_List sfpt(area);
3212
3213 frc._visited.set(root->_idx); // first, mark node as visited
3540 if (use->is_Con()) continue; // a dead ConNode is OK
3541 // At this point, we have found a dead node which is DU-reachable.
3542 if (!dead_nodes) {
3543 tty->print_cr("*** Dead nodes reachable via DU edges:");
3544 dead_nodes = true;
3545 }
3546 use->dump(2);
3547 tty->print_cr("---");
3548 checked.push(use); // No repeats; pretend it is now checked.
3549 }
3550 }
3551 assert(!dead_nodes, "using nodes must be reachable from root");
3552 }
3553 }
3554 }
3555
3556 // Verify GC barriers consistency
3557 // Currently supported:
3558 // - G1 pre-barriers (see GraphKit::g1_write_barrier_pre())
3559 void Compile::verify_barriers() {
3560 if (UseG1GC) {
3561 // Verify G1 pre-barriers
3562 const int marking_offset = in_bytes(JavaThread::satb_mark_queue_offset() + PtrQueue::byte_offset_of_active());
3563
3564 ResourceArea *area = Thread::current()->resource_area();
3565 Unique_Node_List visited(area);
3566 Node_List worklist(area);
3567 // We're going to walk control flow backwards starting from the Root
3568 worklist.push(_root);
3569 while (worklist.size() > 0) {
3570 Node* x = worklist.pop();
3571 if (x == NULL || x == top()) continue;
3572 if (visited.member(x)) {
3573 continue;
3574 } else {
3575 visited.push(x);
3576 }
3577
3578 if (x->is_Region()) {
3579 for (uint i = 1; i < x->req(); i++) {
3580 worklist.push(x->in(i));
4064
4065 /**
4066 * Remove the speculative part of types and clean up the graph
4067 */
4068 void Compile::remove_speculative_types(PhaseIterGVN &igvn) {
4069 if (UseTypeSpeculation) {
4070 Unique_Node_List worklist;
4071 worklist.push(root());
4072 int modified = 0;
4073 // Go over all type nodes that carry a speculative type, drop the
4074 // speculative part of the type and enqueue the node for an igvn
4075 // which may optimize it out.
4076 for (uint next = 0; next < worklist.size(); ++next) {
4077 Node *n = worklist.at(next);
4078 if (n->is_Type()) {
4079 TypeNode* tn = n->as_Type();
4080 const Type* t = tn->type();
4081 const Type* t_no_spec = t->remove_speculative();
4082 if (t_no_spec != t) {
4083 bool in_hash = igvn.hash_delete(n);
4084 assert(in_hash, "node should be in igvn hash table");
4085 tn->set_type(t_no_spec);
4086 igvn.hash_insert(n);
4087 igvn._worklist.push(n); // give it a chance to go away
4088 modified++;
4089 }
4090 }
4091 uint max = n->len();
4092 for( uint i = 0; i < max; ++i ) {
4093 Node *m = n->in(i);
4094 if (not_a_node(m)) continue;
4095 worklist.push(m);
4096 }
4097 }
4098 // Drop the speculative part of all types in the igvn's type table
4099 igvn.remove_speculative_types();
4100 if (modified > 0) {
4101 igvn.optimize();
4102 }
4103 #ifdef ASSERT
4104 // Verify that after the IGVN is over no speculative type has resurfaced
4158 // Including count equalizes the chances any candidate is "selected".
4159 // This is useful when we don't have the complete list of candidates to choose
4160 // from uniformly. In this case, we need to adjust the randomicity of the
4161 // selection, or else we will end up biasing the selection towards the latter
4162 // candidates.
4163 //
4164 // Quick back-envelope calculation shows that for the list of n candidates
4165 // the equal probability for the candidate to persist as "best" can be
4166 // achieved by replacing it with "next" k-th candidate with the probability
4167 // of 1/k. It can be easily shown that by the end of the run, the
4168 // probability for any candidate is converged to 1/n, thus giving the
4169 // uniform distribution among all the candidates.
4170 //
4171 // We don't care about the domain size as long as (RANDOMIZED_DOMAIN / count) is large.
4172 #define RANDOMIZED_DOMAIN_POW 29
4173 #define RANDOMIZED_DOMAIN (1 << RANDOMIZED_DOMAIN_POW)
4174 #define RANDOMIZED_DOMAIN_MASK ((1 << (RANDOMIZED_DOMAIN_POW + 1)) - 1)
4175 bool Compile::randomized_select(int count) {
4176 assert(count > 0, "only positive");
4177 return (os::random() & RANDOMIZED_DOMAIN_MASK) < (RANDOMIZED_DOMAIN / count);
4178 }
|
66 #include "runtime/signature.hpp"
67 #include "runtime/stubRoutines.hpp"
68 #include "runtime/timer.hpp"
69 #include "utilities/copy.hpp"
70 #if defined AD_MD_HPP
71 # include AD_MD_HPP
72 #elif defined TARGET_ARCH_MODEL_x86_32
73 # include "adfiles/ad_x86_32.hpp"
74 #elif defined TARGET_ARCH_MODEL_x86_64
75 # include "adfiles/ad_x86_64.hpp"
76 #elif defined TARGET_ARCH_MODEL_aarch64
77 # include "adfiles/ad_aarch64.hpp"
78 #elif defined TARGET_ARCH_MODEL_sparc
79 # include "adfiles/ad_sparc.hpp"
80 #elif defined TARGET_ARCH_MODEL_zero
81 # include "adfiles/ad_zero.hpp"
82 #elif defined TARGET_ARCH_MODEL_ppc_64
83 # include "adfiles/ad_ppc_64.hpp"
84 #endif
85
86 #if INCLUDE_ALL_GCS
87 #include "gc_implementation/shenandoah/shenandoahForwarding.hpp"
88 #include "gc_implementation/shenandoah/c2/shenandoahSupport.hpp"
89 #endif
90
91 // -------------------- Compile::mach_constant_base_node -----------------------
92 // Constant table base node singleton.
93 MachConstantBaseNode* Compile::mach_constant_base_node() {
94 if (_mach_constant_base_node == NULL) {
95 _mach_constant_base_node = new (C) MachConstantBaseNode();
96 _mach_constant_base_node->add_req(C->root());
97 }
98 return _mach_constant_base_node;
99 }
100
101
102 /// Support for intrinsics.
103
104 // Return the index at which m must be inserted (or already exists).
105 // The sort order is by the address of the ciMethod, with is_virtual as minor key.
106 int Compile::intrinsic_insertion_index(ciMethod* m, bool is_virtual) {
107 #ifdef ASSERT
108 for (int i = 1; i < _intrinsics->length(); i++) {
109 CallGenerator* cg1 = _intrinsics->at(i-1);
110 CallGenerator* cg2 = _intrinsics->at(i);
393 // We're done with a parsing phase. Replaced nodes are not valid
394 // beyond that point.
395 n->as_SafePoint()->delete_replaced_nodes();
396 }
397 // Use raw traversal of out edges since this code removes out edges
398 int max = n->outcnt();
399 for (int j = 0; j < max; ++j) {
400 Node* child = n->raw_out(j);
401 if (! useful.member(child)) {
402 assert(!child->is_top() || child != top(),
403 "If top is cached in Compile object it is in useful list");
404 // Only need to remove this out-edge to the useless node
405 n->raw_del_out(j);
406 --j;
407 --max;
408 }
409 }
410 if (n->outcnt() == 1 && n->has_special_unique_user()) {
411 record_for_igvn(n->unique_out());
412 }
413 if (n->Opcode() == Op_AddP && CallLeafNode::has_only_g1_wb_pre_uses(n)) {
414 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
415 record_for_igvn(n->fast_out(i));
416 }
417 }
418 }
419 // Remove useless macro and predicate opaq nodes
420 for (int i = C->macro_count()-1; i >= 0; i--) {
421 Node* n = C->macro_node(i);
422 if (!useful.member(n)) {
423 remove_macro_node(n);
424 }
425 }
426 // Remove useless CastII nodes with range check dependency
427 for (int i = range_check_cast_count() - 1; i >= 0; i--) {
428 Node* cast = range_check_cast_node(i);
429 if (!useful.member(cast)) {
430 remove_range_check_cast(cast);
431 }
432 }
433 // Remove useless expensive node
434 for (int i = C->expensive_count()-1; i >= 0; i--) {
435 Node* n = C->expensive_node(i);
436 if (!useful.member(n)) {
437 remove_expensive_node(n);
438 }
439 }
440 for (int i = C->shenandoah_barriers_count()-1; i >= 0; i--) {
441 ShenandoahLoadReferenceBarrierNode* n = C->shenandoah_barrier(i);
442 if (!useful.member(n)) {
443 remove_shenandoah_barrier(n);
444 }
445 }
446 // clean up the late inline lists
447 remove_useless_late_inlines(&_string_late_inlines, useful);
448 remove_useless_late_inlines(&_boxing_late_inlines, useful);
449 remove_useless_late_inlines(&_late_inlines, useful);
450 debug_only(verify_graph_edges(true/*check for no_dead_code*/);)
451 }
452
453 //------------------------------frame_size_in_words-----------------------------
454 // frame_slots in units of words
455 int Compile::frame_size_in_words() const {
456 // shift is 0 in LP32 and 1 in LP64
457 const int shift = (LogBytesPerWord - LogBytesPerInt);
458 int words = _frame_slots >> shift;
459 assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" );
460 return words;
461 }
462
463 // To bang the stack of this compiled method we use the stack size
464 // that the interpreter would need in case of a deoptimization. This
465 // removes the need to bang the stack in the deoptimization blob which
769
770 // Put top into the hash table ASAP.
771 initial_gvn()->transform_no_reclaim(top());
772
773 // Set up tf(), start(), and find a CallGenerator.
774 CallGenerator* cg = NULL;
775 if (is_osr_compilation()) {
776 const TypeTuple *domain = StartOSRNode::osr_domain();
777 const TypeTuple *range = TypeTuple::make_range(method()->signature());
778 init_tf(TypeFunc::make(domain, range));
779 StartNode* s = new (this) StartOSRNode(root(), domain);
780 initial_gvn()->set_type_bottom(s);
781 init_start(s);
782 cg = CallGenerator::for_osr(method(), entry_bci());
783 } else {
784 // Normal case.
785 init_tf(TypeFunc::make(method()));
786 StartNode* s = new (this) StartNode(root(), tf()->domain());
787 initial_gvn()->set_type_bottom(s);
788 init_start(s);
789 if (method()->intrinsic_id() == vmIntrinsics::_Reference_get && (UseG1GC || UseShenandoahGC)) {
790 // With java.lang.ref.reference.get() we must go through the
791 // intrinsic when G1 is enabled - even when get() is the root
792 // method of the compile - so that, if necessary, the value in
793 // the referent field of the reference object gets recorded by
794 // the pre-barrier code.
795 // Specifically, if G1 is enabled, the value in the referent
796 // field is recorded by the G1 SATB pre barrier. This will
797 // result in the referent being marked live and the reference
798 // object removed from the list of discovered references during
799 // reference processing.
800 cg = find_intrinsic(method(), false);
801 }
802 if (cg == NULL) {
803 float past_uses = method()->interpreter_invocation_count();
804 float expected_uses = past_uses;
805 cg = CallGenerator::for_inline(method(), expected_uses);
806 }
807 }
808 if (failing()) return;
809 if (cg == NULL) {
1162 AliasType* ats = NEW_ARENA_ARRAY(comp_arena(), AliasType, grow_ats);
1163 Copy::zero_to_bytes(ats, sizeof(AliasType)*grow_ats);
1164 {
1165 for (int i = 0; i < grow_ats; i++) _alias_types[i] = &ats[i];
1166 }
1167 // Initialize the first few types.
1168 _alias_types[AliasIdxTop]->Init(AliasIdxTop, NULL);
1169 _alias_types[AliasIdxBot]->Init(AliasIdxBot, TypePtr::BOTTOM);
1170 _alias_types[AliasIdxRaw]->Init(AliasIdxRaw, TypeRawPtr::BOTTOM);
1171 _num_alias_types = AliasIdxRaw+1;
1172 // Zero out the alias type cache.
1173 Copy::zero_to_bytes(_alias_cache, sizeof(_alias_cache));
1174 // A NULL adr_type hits in the cache right away. Preload the right answer.
1175 probe_alias_cache(NULL)->_index = AliasIdxTop;
1176
1177 _intrinsics = NULL;
1178 _macro_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1179 _predicate_opaqs = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1180 _expensive_nodes = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1181 _range_check_casts = new(comp_arena()) GrowableArray<Node*>(comp_arena(), 8, 0, NULL);
1182 _shenandoah_barriers = new(comp_arena()) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena(), 8, 0, NULL);
1183 register_library_intrinsics();
1184 #ifdef ASSERT
1185 _type_verify_symmetry = true;
1186 #endif
1187 }
1188
1189 //---------------------------init_start----------------------------------------
1190 // Install the StartNode on this compile object.
1191 void Compile::init_start(StartNode* s) {
1192 if (failing())
1193 return; // already failing
1194 assert(s == start(), "");
1195 }
1196
1197 StartNode* Compile::start() const {
1198 assert(!failing(), "");
1199 for (DUIterator_Fast imax, i = root()->fast_outs(imax); i < imax; i++) {
1200 Node* start = root()->fast_out(i);
1201 if( start->is_Start() )
1202 return start->as_Start();
2300 PhaseIdealLoop ideal_loop( igvn, true);
2301 loop_opts_cnt--;
2302 if (major_progress()) print_method(PHASE_PHASEIDEALLOOP_ITERATIONS, 2);
2303 if (failing()) return;
2304 }
2305 }
2306
2307 {
2308 // Verify that all previous optimizations produced a valid graph
2309 // at least to this point, even if no loop optimizations were done.
2310 NOT_PRODUCT( TracePhase t2("idealLoopVerify", &_t_idealLoopVerify, TimeCompiler); )
2311 PhaseIdealLoop::verify(igvn);
2312 }
2313
2314 if (range_check_cast_count() > 0) {
2315 // No more loop optimizations. Remove all range check dependent CastIINodes.
2316 C->remove_range_check_casts(igvn);
2317 igvn.optimize();
2318 }
2319
2320 #ifdef ASSERT
2321 if (UseShenandoahGC && ShenandoahVerifyOptoBarriers) {
2322 ShenandoahBarrierC2Support::verify(C->root());
2323 }
2324 #endif
2325
2326 {
2327 NOT_PRODUCT( TracePhase t2("macroExpand", &_t_macroExpand, TimeCompiler); )
2328 PhaseMacroExpand mex(igvn);
2329 if (mex.expand_macro_nodes()) {
2330 assert(failing(), "must bail out w/ explicit message");
2331 return;
2332 }
2333 }
2334
2335 #if INCLUDE_ALL_GCS
2336 if (UseShenandoahGC) {
2337 ShenandoahBarrierC2Support::expand(this, igvn);
2338 }
2339 #endif
2340
2341 } // (End scope of igvn; run destructor if necessary for asserts.)
2342
2343 dump_inlining();
2344 // A method with only infinite loops has no edges entering loops from root
2345 {
2346 NOT_PRODUCT( TracePhase t2("graphReshape", &_t_graphReshaping, TimeCompiler); )
2347 if (final_graph_reshaping()) {
2348 assert(failing(), "must bail out w/ explicit message");
2349 return;
2350 }
2351 }
2352
2353 print_method(PHASE_OPTIMIZE_FINISHED, 2);
2354 }
2355
2356
2357 //------------------------------Code_Gen---------------------------------------
2358 // Given a graph, generate code for it
2359 void Compile::Code_Gen() {
2360 if (failing()) {
2755 // case Op_ConvD2L: // handled by leaf call
2756 case Op_ConD:
2757 case Op_CmpD:
2758 case Op_CmpD3:
2759 frc.inc_double_count();
2760 break;
2761 case Op_Opaque1: // Remove Opaque Nodes before matching
2762 case Op_Opaque2: // Remove Opaque Nodes before matching
2763 case Op_Opaque3:
2764 n->subsume_by(n->in(1), this);
2765 break;
2766 case Op_CallStaticJava:
2767 case Op_CallJava:
2768 case Op_CallDynamicJava:
2769 frc.inc_java_call_count(); // Count java call site;
2770 case Op_CallRuntime:
2771 case Op_CallLeaf:
2772 case Op_CallLeafNoFP: {
2773 assert( n->is_Call(), "" );
2774 CallNode *call = n->as_Call();
2775 if (UseShenandoahGC && call->is_g1_wb_pre_call()) {
2776 uint cnt = OptoRuntime::g1_wb_pre_Type()->domain()->cnt();
2777 if (call->req() > cnt) {
2778 assert(call->req() == cnt+1, "only one extra input");
2779 Node* addp = call->in(cnt);
2780 assert(!CallLeafNode::has_only_g1_wb_pre_uses(addp), "useless address computation?");
2781 call->del_req(cnt);
2782 }
2783 }
2784 // Count call sites where the FP mode bit would have to be flipped.
2785 // Do not count uncommon runtime calls:
2786 // uncommon_trap, _complete_monitor_locking, _complete_monitor_unlocking,
2787 // _new_Java, _new_typeArray, _new_objArray, _rethrow_Java, ...
2788 if( !call->is_CallStaticJava() || !call->as_CallStaticJava()->_name ) {
2789 frc.inc_call_count(); // Count the call site
2790 } else { // See if uncommon argument is shared
2791 Node *n = call->in(TypeFunc::Parms);
2792 int nop = n->Opcode();
2793 // Clone shared simple arguments to uncommon calls, item (1).
2794 if( n->outcnt() > 1 &&
2795 !n->is_Proj() &&
2796 nop != Op_CreateEx &&
2797 nop != Op_CheckCastPP &&
2798 nop != Op_DecodeN &&
2799 nop != Op_DecodeNKlass &&
2800 !n->is_Mem() ) {
2801 Node *x = n->clone();
2802 call->set_req( TypeFunc::Parms, x );
2803 }
2907 if (nn != NULL) {
2908 // Decode a narrow oop to match address
2909 // [R12 + narrow_oop_reg<<3 + offset]
2910 if (t->isa_oopptr()) {
2911 nn = new (this) DecodeNNode(nn, t);
2912 } else {
2913 nn = new (this) DecodeNKlassNode(nn, t);
2914 }
2915 n->set_req(AddPNode::Base, nn);
2916 n->set_req(AddPNode::Address, nn);
2917 if (addp->outcnt() == 0) {
2918 addp->disconnect_inputs(NULL, this);
2919 }
2920 }
2921 }
2922 }
2923 #endif
2924 break;
2925 }
2926
2927 case Op_CastPP: {
2928 // Remove CastPP nodes to gain more freedom during scheduling but
2929 // keep the dependency they encode as control or precedence edges
2930 // (if control is set already) on memory operations. Some CastPP
2931 // nodes don't have a control (don't carry a dependency): skip
2932 // those.
2933 if (n->in(0) != NULL) {
2934 ResourceMark rm;
2935 Unique_Node_List wq;
2936 wq.push(n);
2937 for (uint next = 0; next < wq.size(); ++next) {
2938 Node *m = wq.at(next);
2939 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
2940 Node* use = m->fast_out(i);
2941 if (use->is_Mem() || use->is_EncodeNarrowPtr() || use->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
2942 use->ensure_control_or_add_prec(n->in(0));
2943 } else if (use->in(0) == NULL) {
2944 switch(use->Opcode()) {
2945 case Op_AddP:
2946 case Op_DecodeN:
2947 case Op_DecodeNKlass:
2948 case Op_CheckCastPP:
2949 case Op_CastPP:
2950 wq.push(use);
2951 break;
2952 }
2953 }
2954 }
2955 }
2956 }
2957 const bool is_LP64 = LP64_ONLY(true) NOT_LP64(false);
2958 if (is_LP64 && n->in(1)->is_DecodeN() && Matcher::gen_narrow_oop_implicit_null_checks()) {
2959 Node* in1 = n->in(1);
2960 const Type* t = n->bottom_type();
2961 Node* new_in1 = in1->clone();
2962 new_in1->as_DecodeN()->set_type(t);
2963
2964 if (!Matcher::narrow_oop_use_complex_address()) {
2965 //
2966 // x86, ARM and friends can handle 2 adds in addressing mode
2967 // and Matcher can fold a DecodeN node into address by using
2968 // a narrow oop directly and do implicit NULL check in address:
2969 //
2970 // [R12 + narrow_oop_reg<<3 + offset]
2971 // NullCheck narrow_oop_reg
2972 //
2973 // On other platforms (Sparc) we have to keep new DecodeN node and
2974 // use it to do implicit NULL check in address:
2975 //
2976 // decode_not_null narrow_oop_reg, base_reg
2977 // [base_reg + offset]
2978 // NullCheck base_reg
2979 //
2980 // Pin the new DecodeN node to non-null path on these platform (Sparc)
2981 // to keep the information to which NULL check the new DecodeN node
2982 // corresponds to use it as value in implicit_null_check().
2983 //
2984 new_in1->set_req(0, n->in(0));
2985 }
2986
2987 n->subsume_by(new_in1, this);
2988 if (in1->outcnt() == 0) {
2989 in1->disconnect_inputs(NULL, this);
2990 }
2991 } else {
2992 n->subsume_by(n->in(1), this);
2993 if (n->outcnt() == 0) {
2994 n->disconnect_inputs(NULL, this);
2995 }
2996 }
2997 break;
2998 }
2999 #ifdef _LP64
3000 case Op_CmpP:
3001 // Do this transformation here to preserve CmpPNode::sub() and
3002 // other TypePtr related Ideal optimizations (for example, ptr nullness).
3003 if (n->in(1)->is_DecodeNarrowPtr() || n->in(2)->is_DecodeNarrowPtr()) {
3004 Node* in1 = n->in(1);
3005 Node* in2 = n->in(2);
3006 if (!in1->is_DecodeNarrowPtr()) {
3007 in2 = in1;
3008 in1 = n->in(2);
3009 }
3010 assert(in1->is_DecodeNarrowPtr(), "sanity");
3011
3012 Node* new_in2 = NULL;
3013 if (in2->is_DecodeNarrowPtr()) {
3014 assert(in2->Opcode() == in1->Opcode(), "must be same node type");
3015 new_in2 = in2->in(1);
3016 } else if (in2->Opcode() == Op_ConP) {
3017 const Type* t = in2->bottom_type();
3018 if (t == TypePtr::NULL_PTR) {
3019 assert(in1->is_DecodeN(), "compare klass to null?");
3247 }
3248 } else {
3249 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
3250 Node* shift = new (this) AndINode(in2, ConNode::make(this, TypeInt::make(mask)));
3251 n->set_req(2, shift);
3252 }
3253 }
3254 if (in2->outcnt() == 0) { // Remove dead node
3255 in2->disconnect_inputs(NULL, this);
3256 }
3257 }
3258 break;
3259 case Op_MemBarStoreStore:
3260 case Op_MemBarRelease:
3261 // Break the link with AllocateNode: it is no longer useful and
3262 // confuses register allocation.
3263 if (n->req() > MemBarNode::Precedent) {
3264 n->set_req(MemBarNode::Precedent, top());
3265 }
3266 break;
3267 case Op_ShenandoahLoadReferenceBarrier:
3268 assert(false, "should have been expanded already");
3269 break;
3270 default:
3271 assert( !n->is_Call(), "" );
3272 assert( !n->is_Mem(), "" );
3273 assert( nop != Op_ProfileBoolean, "should be eliminated during IGVN");
3274 break;
3275 }
3276
3277 // Collect CFG split points
3278 if (n->is_MultiBranch())
3279 frc._tests.push(n);
3280 }
3281
3282 //------------------------------final_graph_reshaping_walk---------------------
3283 // Replacing Opaque nodes with their input in final_graph_reshaping_impl(),
3284 // requires that the walk visits a node's inputs before visiting the node.
3285 void Compile::final_graph_reshaping_walk( Node_Stack &nstack, Node *root, Final_Reshape_Counts &frc ) {
3286 ResourceArea *area = Thread::current()->resource_area();
3287 Unique_Node_List sfpt(area);
3288
3289 frc._visited.set(root->_idx); // first, mark node as visited
3616 if (use->is_Con()) continue; // a dead ConNode is OK
3617 // At this point, we have found a dead node which is DU-reachable.
3618 if (!dead_nodes) {
3619 tty->print_cr("*** Dead nodes reachable via DU edges:");
3620 dead_nodes = true;
3621 }
3622 use->dump(2);
3623 tty->print_cr("---");
3624 checked.push(use); // No repeats; pretend it is now checked.
3625 }
3626 }
3627 assert(!dead_nodes, "using nodes must be reachable from root");
3628 }
3629 }
3630 }
3631
3632 // Verify GC barriers consistency
3633 // Currently supported:
3634 // - G1 pre-barriers (see GraphKit::g1_write_barrier_pre())
3635 void Compile::verify_barriers() {
3636 if (UseG1GC || UseShenandoahGC) {
3637 // Verify G1 pre-barriers
3638 const int marking_offset = in_bytes(JavaThread::satb_mark_queue_offset() + PtrQueue::byte_offset_of_active());
3639
3640 ResourceArea *area = Thread::current()->resource_area();
3641 Unique_Node_List visited(area);
3642 Node_List worklist(area);
3643 // We're going to walk control flow backwards starting from the Root
3644 worklist.push(_root);
3645 while (worklist.size() > 0) {
3646 Node* x = worklist.pop();
3647 if (x == NULL || x == top()) continue;
3648 if (visited.member(x)) {
3649 continue;
3650 } else {
3651 visited.push(x);
3652 }
3653
3654 if (x->is_Region()) {
3655 for (uint i = 1; i < x->req(); i++) {
3656 worklist.push(x->in(i));
4140
4141 /**
4142 * Remove the speculative part of types and clean up the graph
4143 */
4144 void Compile::remove_speculative_types(PhaseIterGVN &igvn) {
4145 if (UseTypeSpeculation) {
4146 Unique_Node_List worklist;
4147 worklist.push(root());
4148 int modified = 0;
4149 // Go over all type nodes that carry a speculative type, drop the
4150 // speculative part of the type and enqueue the node for an igvn
4151 // which may optimize it out.
4152 for (uint next = 0; next < worklist.size(); ++next) {
4153 Node *n = worklist.at(next);
4154 if (n->is_Type()) {
4155 TypeNode* tn = n->as_Type();
4156 const Type* t = tn->type();
4157 const Type* t_no_spec = t->remove_speculative();
4158 if (t_no_spec != t) {
4159 bool in_hash = igvn.hash_delete(n);
4160 assert(in_hash || n->hash() == Node::NO_HASH, "node should be in igvn hash table");
4161 tn->set_type(t_no_spec);
4162 igvn.hash_insert(n);
4163 igvn._worklist.push(n); // give it a chance to go away
4164 modified++;
4165 }
4166 }
4167 uint max = n->len();
4168 for( uint i = 0; i < max; ++i ) {
4169 Node *m = n->in(i);
4170 if (not_a_node(m)) continue;
4171 worklist.push(m);
4172 }
4173 }
4174 // Drop the speculative part of all types in the igvn's type table
4175 igvn.remove_speculative_types();
4176 if (modified > 0) {
4177 igvn.optimize();
4178 }
4179 #ifdef ASSERT
4180 // Verify that after the IGVN is over no speculative type has resurfaced
4234 // Including count equalizes the chances any candidate is "selected".
4235 // This is useful when we don't have the complete list of candidates to choose
4236 // from uniformly. In this case, we need to adjust the randomicity of the
4237 // selection, or else we will end up biasing the selection towards the latter
4238 // candidates.
4239 //
4240 // Quick back-envelope calculation shows that for the list of n candidates
4241 // the equal probability for the candidate to persist as "best" can be
4242 // achieved by replacing it with "next" k-th candidate with the probability
4243 // of 1/k. It can be easily shown that by the end of the run, the
4244 // probability for any candidate is converged to 1/n, thus giving the
4245 // uniform distribution among all the candidates.
4246 //
4247 // We don't care about the domain size as long as (RANDOMIZED_DOMAIN / count) is large.
4248 #define RANDOMIZED_DOMAIN_POW 29
4249 #define RANDOMIZED_DOMAIN (1 << RANDOMIZED_DOMAIN_POW)
4250 #define RANDOMIZED_DOMAIN_MASK ((1 << (RANDOMIZED_DOMAIN_POW + 1)) - 1)
4251 bool Compile::randomized_select(int count) {
4252 assert(count > 0, "only positive");
4253 return (os::random() & RANDOMIZED_DOMAIN_MASK) < (RANDOMIZED_DOMAIN / count);
4254 }
4255
4256 void Compile::shenandoah_eliminate_g1_wb_pre(Node* call, PhaseIterGVN* igvn) {
4257 assert(UseShenandoahGC && call->is_g1_wb_pre_call(), "");
4258 Node* c = call->as_Call()->proj_out(TypeFunc::Control);
4259 c = c->unique_ctrl_out();
4260 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
4261 c = c->unique_ctrl_out();
4262 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
4263 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
4264 assert(iff->is_If(), "expect test");
4265 if (!iff->is_shenandoah_marking_if(igvn)) {
4266 c = c->unique_ctrl_out();
4267 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
4268 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
4269 assert(iff->is_shenandoah_marking_if(igvn), "expect marking test");
4270 }
4271 Node* cmpx = iff->in(1)->in(1);
4272 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ));
4273 igvn->rehash_node_delayed(call);
4274 call->del_req(call->req()-1);
4275 }
|