@@ -69,7 +69,8 @@ SuperWord::SuperWord(PhaseIdealLoop* phase) :
6969 _nlist(arena(), 8, 0, NULL), // scratch list of nodes
7070 _stk(arena(), 8, 0, NULL), // scratch stack of nodes
7171 _lpt(NULL ), // loop tree node
72- _lp(NULL ), // LoopNode
72+ _lp(NULL ), // CountedLoopNode
73+ _pre_loop_end(NULL ), // Pre loop CountedLoopEndNode
7374 _bb(NULL ), // basic block
7475 _iv(NULL ), // induction var
7576 _race_possible(false ), // cases where SDMU is true
@@ -155,10 +156,15 @@ void SuperWord::transform_loop(IdealLoopTree* lpt, bool do_optimization) {
155156
156157 if (cl->is_main_loop ()) {
157158 // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
158- CountedLoopEndNode* pre_end = get_pre_loop_end (cl);
159- if (pre_end == NULL ) return ;
160- Node *pre_opaq1 = pre_end->limit ();
161- if (pre_opaq1->Opcode () != Op_Opaque1) return ;
159+ CountedLoopEndNode* pre_end = find_pre_loop_end (cl);
160+ if (pre_end == NULL ) {
161+ return ;
162+ }
163+ Node* pre_opaq1 = pre_end->limit ();
164+ if (pre_opaq1->Opcode () != Op_Opaque1) {
165+ return ;
166+ }
167+ set_pre_loop_end (pre_end);
162168 }
163169
164170 init (); // initialize data structures
@@ -911,8 +917,7 @@ bool SuperWord::ref_is_alignable(SWPointer& p) {
911917 if (!p.has_iv ()) {
912918 return true ; // no induction variable
913919 }
914- CountedLoopEndNode* pre_end = get_pre_loop_end (lp ()->as_CountedLoop ());
915- assert (pre_end != NULL , " we must have a correct pre-loop" );
920+ CountedLoopEndNode* pre_end = pre_loop_end ();
916921 assert (pre_end->stride_is_con (), " pre loop stride is constant" );
917922 int preloop_stride = pre_end->stride_con ();
918923
@@ -3431,21 +3436,19 @@ LoadNode::ControlDependency SuperWord::control_dependency(Node_List* p) {
34313436// to align_to_ref will be a position zero in the vector.
34323437// (iv + k) mod vector_align == 0
34333438void SuperWord::align_initial_loop_index (MemNode* align_to_ref) {
3434- CountedLoopNode *main_head = lp ()->as_CountedLoop ();
3435- assert (main_head->is_main_loop (), " " );
3436- CountedLoopEndNode* pre_end = get_pre_loop_end (main_head);
3437- assert (pre_end != NULL , " we must have a correct pre-loop" );
3438- Node *pre_opaq1 = pre_end->limit ();
3439+ assert (lp ()->is_main_loop (), " " );
3440+ CountedLoopEndNode* pre_end = pre_loop_end ();
3441+ Node* pre_opaq1 = pre_end->limit ();
34393442 assert (pre_opaq1->Opcode () == Op_Opaque1, " " );
3440- Opaque1Node * pre_opaq = (Opaque1Node*)pre_opaq1;
3441- Node * lim0 = pre_opaq->in (1 );
3443+ Opaque1Node* pre_opaq = (Opaque1Node*)pre_opaq1;
3444+ Node* lim0 = pre_opaq->in (1 );
34423445
34433446 // Where we put new limit calculations
3444- Node * pre_ctrl = pre_end-> loopnode ()->in (LoopNode::EntryControl);
3447+ Node* pre_ctrl = pre_loop_head ()->in (LoopNode::EntryControl);
34453448
34463449 // Ensure the original loop limit is available from the
34473450 // pre-loop Opaque1 node.
3448- Node * orig_limit = pre_opaq->original_loop_limit ();
3451+ Node* orig_limit = pre_opaq->original_loop_limit ();
34493452 assert (orig_limit != NULL && _igvn.type (orig_limit) != Type::TOP, " " );
34503453
34513454 SWPointer align_to_ref_p (align_to_ref, this , NULL , false );
@@ -3596,7 +3599,7 @@ void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
35963599
35973600// ----------------------------get_pre_loop_end---------------------------
35983601// Find pre loop end from main loop. Returns null if none.
3599- CountedLoopEndNode* SuperWord::get_pre_loop_end (CountedLoopNode* cl) {
3602+ CountedLoopEndNode* SuperWord::find_pre_loop_end (CountedLoopNode* cl) const {
36003603 // The loop cannot be optimized if the graph shape at
36013604 // the loop entry is inappropriate.
36023605 if (!PhaseIdealLoop::is_canonical_loop_entry (cl)) {
@@ -3724,7 +3727,7 @@ SWPointer::SWPointer(MemNode* mem, SuperWord* slp, Node_Stack *nstack, bool anal
37243727 // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
37253728 Node* base = adr->in (AddPNode::Base);
37263729 // The base address should be loop invariant
3727- if (! invariant (base)) {
3730+ if (is_main_loop_member (base)) {
37283731 assert (!valid (), " base address is loop variant" );
37293732 return ;
37303733 }
@@ -3753,7 +3756,7 @@ SWPointer::SWPointer(MemNode* mem, SuperWord* slp, Node_Stack *nstack, bool anal
37533756 break ; // stop looking at addp's
37543757 }
37553758 }
3756- if (! invariant (adr)) {
3759+ if (is_main_loop_member (adr)) {
37573760 assert (!valid (), " adr is loop variant" );
37583761 return ;
37593762 }
@@ -3783,12 +3786,23 @@ SWPointer::SWPointer(SWPointer* p) :
37833786 #endif
37843787{}
37853788
3789+ bool SWPointer::is_main_loop_member (Node* n) const {
3790+ Node* n_c = phase ()->get_ctrl (n);
3791+ return lpt ()->is_member (phase ()->get_loop (n_c));
3792+ }
37863793
3787- bool SWPointer::invariant (Node* n) {
3794+ bool SWPointer::invariant (Node* n) const {
37883795 NOT_PRODUCT (Tracer::Depth dd;)
3789- Node * n_c = phase ()->get_ctrl (n);
3796+ Node* n_c = phase ()->get_ctrl (n);
37903797 NOT_PRODUCT (_tracer.invariant_1 (n, n_c);)
3791- return !lpt ()->is_member (phase ()->get_loop (n_c));
3798+ bool is_not_member = !is_main_loop_member (n);
3799+ if (is_not_member && _slp->lp ()->is_main_loop ()) {
3800+ // Check that n_c dominates the pre loop head node. If it does not, then we cannot use n as invariant for the pre loop
3801+ // CountedLoopEndNode check because n_c is either part of the pre loop or between the pre and the main loop (illegal
3802+ // invariant: Happens, for example, when n_c is a CastII node that prevents data nodes to flow above the main loop).
3803+ return phase ()->is_dominator (n_c, _slp->pre_loop_head ());
3804+ }
3805+ return is_not_member;
37923806}
37933807
37943808// ------------------------scaled_iv_plus_offset--------------------
@@ -3851,7 +3865,7 @@ bool SWPointer::scaled_iv(Node* n) {
38513865 NOT_PRODUCT (_tracer.scaled_iv_3 (n, _scale);)
38523866 return true ;
38533867 }
3854- if (_analyze_only && (invariant (n) == false )) {
3868+ if (_analyze_only && (is_main_loop_member (n))) {
38553869 _nstack->push (n, _stack_idx++);
38563870 }
38573871
@@ -3937,7 +3951,7 @@ bool SWPointer::offset_plus_k(Node* n, bool negate) {
39373951 return false ;
39383952 }
39393953
3940- if (_analyze_only && ( invariant (n) == false )) {
3954+ if (_analyze_only && is_main_loop_member (n )) {
39413955 _nstack->push (n, _stack_idx++);
39423956 }
39433957 if (opc == Op_AddI) {
@@ -3970,20 +3984,27 @@ bool SWPointer::offset_plus_k(Node* n, bool negate) {
39703984 return true ;
39713985 }
39723986 }
3973- if (invariant (n)) {
3987+
3988+ if (!is_main_loop_member (n)) {
3989+ // 'n' is loop invariant. Skip range check dependent CastII nodes before checking if 'n' is dominating the pre loop.
39743990 if (opc == Op_ConvI2L) {
39753991 n = n->in (1 );
39763992 if (n->Opcode () == Op_CastII &&
39773993 n->as_CastII ()->has_range_check ()) {
39783994 // Skip range check dependent CastII nodes
3979- assert (invariant (n), " sanity" );
3995+ assert (! is_main_loop_member (n), " sanity" );
39803996 n = n->in (1 );
39813997 }
3998+
3999+ // Check if 'n' can really be used as invariant (not in main loop and dominating the pre loop).
4000+ if (invariant (n)) {
4001+ _negate_invar = negate;
4002+ _invar = n;
4003+ NOT_PRODUCT (_tracer.offset_plus_k_10 (n, _invar, _negate_invar, _offset);)
4004+ return true ;
4005+ }
39824006 }
3983- _negate_invar = negate;
3984- _invar = n;
3985- NOT_PRODUCT (_tracer.offset_plus_k_10 (n, _invar, _negate_invar, _offset);)
3986- return true ;
4007+ return false ;
39874008 }
39884009
39894010 NOT_PRODUCT (_tracer.offset_plus_k_11 (n);)
@@ -4004,8 +4025,10 @@ void SWPointer::print() {
40044025
40054026// ----------------------------tracing------------------------
40064027#ifndef PRODUCT
4007- void SWPointer::Tracer::print_depth () {
4008- for (int ii = 0 ; ii<_depth; ++ii) tty->print (" " );
4028+ void SWPointer::Tracer::print_depth () const {
4029+ for (int ii = 0 ; ii < _depth; ++ii) {
4030+ tty->print (" " );
4031+ }
40094032}
40104033
40114034void SWPointer::Tracer::ctor_1 (Node* mem) {
@@ -4057,7 +4080,7 @@ void SWPointer::Tracer::ctor_6(Node* mem) {
40574080 }
40584081}
40594082
4060- void SWPointer::Tracer::invariant_1 (Node *n, Node *n_c) {
4083+ void SWPointer::Tracer::invariant_1 (Node *n, Node *n_c) const {
40614084 if (_slp->do_vector_loop () && _slp->is_debug () && _slp->_lpt ->is_member (_slp->_phase ->get_loop (n_c)) != (int )_slp->in_bb (n)) {
40624085 int is_member = _slp->_lpt ->is_member (_slp->_phase ->get_loop (n_c));
40634086 int in_bb = _slp->in_bb (n);
0 commit comments