< prev index next >

src/hotspot/share/opto/postaloc.cpp

Print this page

  1 /*
  2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *

249       break; // Failed for some cutout?
250     }
251     x = copy;                   // Progress, try again
252   }
253 
254   // Phis and 2-address instructions cannot change registers so easily - their
255   // outputs must match their input.
256   if( !can_change_regs )
257     return blk_adjust;          // Only check stupid copies!
258 
259   // Loop backedges won't have a value-mapping yet
260   if( &value == NULL ) return blk_adjust;
261 
262   // Skip through all copies to the _value_ being used.  Do not change from
263   // int to pointer.  This attempts to jump through a chain of copies, where
264   // intermediate copies might be illegal, i.e., value is stored down to stack
265   // then reloaded BUT survives in a register the whole way.
266   Node *val = skip_copies(n->in(k));
267   if (val == x) return blk_adjust; // No progress?
268 
269   int n_regs = RegMask::num_registers(val->ideal_reg());
270   uint val_idx = _lrg_map.live_range_id(val);
271   OptoReg::Name val_reg = lrgs(val_idx).reg();

272 
273   // See if it happens to already be in the correct register!
274   // (either Phi's direct register, or the common case of the name
275   // never-clobbered original-def register)
276   if (register_contains_value(val, val_reg, n_regs, value)) {
277     blk_adjust += use_prior_register(n,k,regnd[val_reg],current_block,value,regnd);
278     if( n->in(k) == regnd[val_reg] ) // Success!  Quit trying
279       return blk_adjust;
280   }
281 
282   // See if we can skip the copy by changing registers.  Don't change from
283   // using a register to using the stack unless we know we can remove a
284   // copy-load.  Otherwise we might end up making a pile of Intel cisc-spill
285   // ops reading from memory instead of just loading once and using the
286   // register.
287 
288   // Also handle duplicate copies here.
289   const Type *t = val->is_Con() ? val->bottom_type() : NULL;
290 
291   // Scan all registers to see if this value is around already
292   for( uint reg = 0; reg < (uint)_max_reg; reg++ ) {
293     if (reg == (uint)nk_reg) {
294       // Found ourselves so check if there is only one user of this
295       // copy and keep on searching for a better copy if so.
296       bool ignore_self = true;
297       x = n->in(k);
298       DUIterator_Fast imax, i = x->fast_outs(imax);
299       Node* first = x->fast_out(i); i++;
300       while (i < imax && ignore_self) {
301         Node* use = x->fast_out(i); i++;
302         if (use != first) ignore_self = false;
303       }
304       if (ignore_self) continue;
305     }
306 
307     Node *vv = value[reg];











308     if (n_regs > 1) { // Doubles and vectors check for aligned-adjacent set
309       uint last = (n_regs-1); // Looking for the last part of a set







310       if ((reg&last) != last) continue; // Wrong part of a set
311       if (!register_contains_value(vv, reg, n_regs, value)) continue; // Different value
312     }
313     if( vv == val ||            // Got a direct hit?
314         (t && vv && vv->bottom_type() == t && vv->is_Mach() &&
315          vv->as_Mach()->rule() == val->as_Mach()->rule()) ) { // Or same constant?
316       assert( !n->is_Phi(), "cannot change registers at a Phi so easily" );
317       if( OptoReg::is_stack(nk_reg) || // CISC-loading from stack OR
318           OptoReg::is_reg(reg) || // turning into a register use OR
319           regnd[reg]->outcnt()==1 ) { // last use of a spill-load turns into a CISC use
320         blk_adjust += use_prior_register(n,k,regnd[reg],current_block,value,regnd);
321         if( n->in(k) == regnd[reg] ) // Success!  Quit trying
322           return blk_adjust;
323       } // End of if not degrading to a stack
324     } // End of if found value in another register
325   } // End of scan all machine registers
326   return blk_adjust;
327 }
328 
329 

574       for (j = 1; j < block->num_preds(); j++) {
575         Block* pb = _cfg.get_block_for_node(block->pred(j));
576         if (pb == freed) {
577           continue; // Did self already via freelist
578         }
579         Node_List &p_regnd = *blk2regnd[pb->_pre_order];
580         for( uint k = 0; k < (uint)_max_reg; k++ ) {
581           if( regnd[k] != p_regnd[k] ) { // Conflict on reaching defs?
582             value.map(k,NULL); // Then no value handy
583             regnd.map(k,NULL);
584           }
585         }
586       }
587     }
588 
589     // For all Phi's
590     for (j = 1; j < phi_dex; j++) {
591       uint k;
592       Node *phi = block->get_node(j);
593       uint pidx = _lrg_map.live_range_id(phi);
594       OptoReg::Name preg = lrgs(_lrg_map.live_range_id(phi)).reg();
595 
596       // Remove copies remaining on edges.  Check for junk phi.
597       Node *u = NULL;
598       for (k = 1; k < phi->req(); k++) {
599         Node *x = phi->in(k);
600         if( phi != x && u != x ) // Found a different input
601           u = u ? NodeSentinel : x; // Capture unique input, or NodeSentinel for 2nd input
602       }
603       if (u != NodeSentinel) {    // Junk Phi.  Remove
604         phi->replace_by(u);
605         j -= yank_if_dead(phi, block, &value, &regnd);
606         phi_dex--;
607         continue;
608       }
609       // Note that if value[pidx] exists, then we merged no new values here
610       // and the phi is useless.  This can happen even with the above phi
611       // removal for complex flows.  I cannot keep the better known value here
612       // because locally the phi appears to define a new merged value.  If I
613       // keep the better value then a copy of the phi, being unable to use the
614       // global flow analysis, can't "peek through" the phi to the original
615       // reaching value and so will act like it's defining a new value.  This
616       // can lead to situations where some uses are from the old and some from
617       // the new values.  Not illegal by itself but throws the over-strong
618       // assert in scheduling.
619       if( pidx ) {
620         value.map(preg,phi);
621         regnd.map(preg,phi);
622         int n_regs = RegMask::num_registers(phi->ideal_reg());
623         for (int l = 1; l < n_regs; l++) {
624           OptoReg::Name preg_lo = OptoReg::add(preg,-l);
625           value.map(preg_lo,phi);
626           regnd.map(preg_lo,phi);
627         }
628       }
629     }
630 
631     // For all remaining instructions
632     for (j = phi_dex; j < block->number_of_nodes(); j++) {
633       Node* n = block->get_node(j);
634 
635       if(n->outcnt() == 0 &&   // Dead?
636          n != C->top() &&      // (ignore TOP, it has no du info)
637          !n->is_Proj() ) {     // fat-proj kills
638         j -= yank_if_dead(n, block, &value, &regnd);
639         continue;
640       }
641 
642       // Improve reaching-def info.  Occasionally post-alloc's liveness gives

646       // advantage of this info to set a reaching def for the use-reg.
647       uint k;
648       for (k = 1; k < n->req(); k++) {
649         Node *def = n->in(k);   // n->in(k) is a USE; def is the DEF for this USE
650         guarantee(def != NULL, "no disconnected nodes at this point");
651         uint useidx = _lrg_map.live_range_id(def); // useidx is the live range index for this USE
652 
653         if( useidx ) {
654           OptoReg::Name ureg = lrgs(useidx).reg();
655           if( !value[ureg] ) {
656             int idx;            // Skip occasional useless copy
657             while( (idx=def->is_Copy()) != 0 &&
658                    def->in(idx) != NULL &&  // NULL should not happen
659                    ureg == lrgs(_lrg_map.live_range_id(def->in(idx))).reg())
660               def = def->in(idx);
661             Node *valdef = skip_copies(def); // tighten up val through non-useless copies
662             value.map(ureg,valdef); // record improved reaching-def info
663             regnd.map(ureg,   def);
664             // Record other half of doubles
665             uint def_ideal_reg = def->ideal_reg();
666             int n_regs = RegMask::num_registers(def_ideal_reg);
667             for (int l = 1; l < n_regs; l++) {
668               OptoReg::Name ureg_lo = OptoReg::add(ureg,-l);
669               if (!value[ureg_lo] &&
670                   (!RegMask::can_represent(ureg_lo) ||
671                    lrgs(useidx).mask().Member(ureg_lo))) { // Nearly always adjacent
672                 value.map(ureg_lo,valdef); // record improved reaching-def info
673                 regnd.map(ureg_lo,   def);
674               }
675             }
676           }
677         }
678       }
679 
680       const uint two_adr = n->is_Mach() ? n->as_Mach()->two_adr() : 0;
681 
682       // Remove copies along input edges
683       for (k = 1; k < n->req(); k++) {
684         j -= elide_copy(n, k, block, value, regnd, two_adr != k);
685       }
686 

690         continue;
691       }
692 
693       // Update the register defined by this instruction
694       OptoReg::Name nreg = lrgs(lidx).reg();
695       // Skip through all copies to the _value_ being defined.
696       // Do not change from int to pointer
697       Node *val = skip_copies(n);
698 
699       // Clear out a dead definition before starting so that the
700       // elimination code doesn't have to guard against it.  The
701       // definition could in fact be a kill projection with a count of
702       // 0 which is safe but since those are uninteresting for copy
703       // elimination just delete them as well.
704       if (regnd[nreg] != NULL && regnd[nreg]->outcnt() == 0) {
705         regnd.map(nreg, NULL);
706         value.map(nreg, NULL);
707       }
708 
709       uint n_ideal_reg = n->ideal_reg();
710       int n_regs = RegMask::num_registers(n_ideal_reg);
711       if (n_regs == 1) {
712         // If Node 'n' does not change the value mapped by the register,
713         // then 'n' is a useless copy.  Do not update the register->node
714         // mapping so 'n' will go dead.
715         if( value[nreg] != val ) {
716           if (eliminate_copy_of_constant(val, n, block, value, regnd, nreg, OptoReg::Bad)) {
717             j -= replace_and_yank_if_dead(n, nreg, block, value, regnd);
718           } else {
719             // Update the mapping: record new Node defined by the register
720             regnd.map(nreg,n);
721             // Update mapping for defined *value*, which is the defined
722             // Node after skipping all copies.
723             value.map(nreg,val);
724           }
725         } else if( !may_be_copy_of_callee(n) ) {
726           assert(n->is_Copy(), "");
727           j -= replace_and_yank_if_dead(n, nreg, block, value, regnd);
728         }
729       } else if (RegMask::is_vector(n_ideal_reg)) {
730         // If Node 'n' does not change the value mapped by the register,

  1 /*
  2  * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *

249       break; // Failed for some cutout?
250     }
251     x = copy;                   // Progress, try again
252   }
253 
254   // Phis and 2-address instructions cannot change registers so easily - their
255   // outputs must match their input.
256   if( !can_change_regs )
257     return blk_adjust;          // Only check stupid copies!
258 
259   // Loop backedges won't have a value-mapping yet
260   if( &value == NULL ) return blk_adjust;
261 
262   // Skip through all copies to the _value_ being used.  Do not change from
263   // int to pointer.  This attempts to jump through a chain of copies, where
264   // intermediate copies might be illegal, i.e., value is stored down to stack
265   // then reloaded BUT survives in a register the whole way.
266   Node *val = skip_copies(n->in(k));
267   if (val == x) return blk_adjust; // No progress?
268 

269   uint val_idx = _lrg_map.live_range_id(val);
270   OptoReg::Name val_reg = lrgs(val_idx).reg();
271   int n_regs = RegMask::num_registers(val->ideal_reg(), lrgs(val_idx));
272 
273   // See if it happens to already be in the correct register!
274   // (either Phi's direct register, or the common case of the name
275   // never-clobbered original-def register)
276   if (register_contains_value(val, val_reg, n_regs, value)) {
277     blk_adjust += use_prior_register(n,k,regnd[val_reg],current_block,value,regnd);
278     if( n->in(k) == regnd[val_reg] ) // Success!  Quit trying
279       return blk_adjust;
280   }
281 
282   // See if we can skip the copy by changing registers.  Don't change from
283   // using a register to using the stack unless we know we can remove a
284   // copy-load.  Otherwise we might end up making a pile of Intel cisc-spill
285   // ops reading from memory instead of just loading once and using the
286   // register.
287 
288   // Also handle duplicate copies here.
289   const Type *t = val->is_Con() ? val->bottom_type() : NULL;
290 
291   // Scan all registers to see if this value is around already
292   for( uint reg = 0; reg < (uint)_max_reg; reg++ ) {
293     if (reg == (uint)nk_reg) {
294       // Found ourselves so check if there is only one user of this
295       // copy and keep on searching for a better copy if so.
296       bool ignore_self = true;
297       x = n->in(k);
298       DUIterator_Fast imax, i = x->fast_outs(imax);
299       Node* first = x->fast_out(i); i++;
300       while (i < imax && ignore_self) {
301         Node* use = x->fast_out(i); i++;
302         if (use != first) ignore_self = false;
303       }
304       if (ignore_self) continue;
305     }
306 
307     Node *vv = value[reg];
308     // For scalable register, number of registers may be inconsistent between
309     // "val_reg" and "reg". For example, when "val" resides in register
310     // but "reg" is located in stack.
311     if (lrgs(val_idx).is_scalable()) {
312       assert(val->ideal_reg() == Op_VecA, "scalable vector register");
313       if (OptoReg::is_stack(reg)) {
314         n_regs = lrgs(val_idx).scalable_reg_slots();
315       } else {
316         n_regs = RegMask::SlotsPerVecA;
317       }
318     }
319     if (n_regs > 1) { // Doubles and vectors check for aligned-adjacent set
320       uint last;
321       if (lrgs(val_idx).is_scalable()) {
322         assert(val->ideal_reg() == Op_VecA, "scalable vector register");
323         // For scalable vector register, regmask is always SlotsPerVecA bits aligned
324         last = RegMask::SlotsPerVecA - 1;
325       } else {
326         last = (n_regs-1); // Looking for the last part of a set
327       }
328       if ((reg&last) != last) continue; // Wrong part of a set
329       if (!register_contains_value(vv, reg, n_regs, value)) continue; // Different value
330     }
331     if( vv == val ||            // Got a direct hit?
332         (t && vv && vv->bottom_type() == t && vv->is_Mach() &&
333          vv->as_Mach()->rule() == val->as_Mach()->rule()) ) { // Or same constant?
334       assert( !n->is_Phi(), "cannot change registers at a Phi so easily" );
335       if( OptoReg::is_stack(nk_reg) || // CISC-loading from stack OR
336           OptoReg::is_reg(reg) || // turning into a register use OR
337           regnd[reg]->outcnt()==1 ) { // last use of a spill-load turns into a CISC use
338         blk_adjust += use_prior_register(n,k,regnd[reg],current_block,value,regnd);
339         if( n->in(k) == regnd[reg] ) // Success!  Quit trying
340           return blk_adjust;
341       } // End of if not degrading to a stack
342     } // End of if found value in another register
343   } // End of scan all machine registers
344   return blk_adjust;
345 }
346 
347 

592       for (j = 1; j < block->num_preds(); j++) {
593         Block* pb = _cfg.get_block_for_node(block->pred(j));
594         if (pb == freed) {
595           continue; // Did self already via freelist
596         }
597         Node_List &p_regnd = *blk2regnd[pb->_pre_order];
598         for( uint k = 0; k < (uint)_max_reg; k++ ) {
599           if( regnd[k] != p_regnd[k] ) { // Conflict on reaching defs?
600             value.map(k,NULL); // Then no value handy
601             regnd.map(k,NULL);
602           }
603         }
604       }
605     }
606 
607     // For all Phi's
608     for (j = 1; j < phi_dex; j++) {
609       uint k;
610       Node *phi = block->get_node(j);
611       uint pidx = _lrg_map.live_range_id(phi);
612       OptoReg::Name preg = lrgs(pidx).reg();
613 
614       // Remove copies remaining on edges.  Check for junk phi.
615       Node *u = NULL;
616       for (k = 1; k < phi->req(); k++) {
617         Node *x = phi->in(k);
618         if( phi != x && u != x ) // Found a different input
619           u = u ? NodeSentinel : x; // Capture unique input, or NodeSentinel for 2nd input
620       }
621       if (u != NodeSentinel) {    // Junk Phi.  Remove
622         phi->replace_by(u);
623         j -= yank_if_dead(phi, block, &value, &regnd);
624         phi_dex--;
625         continue;
626       }
627       // Note that if value[pidx] exists, then we merged no new values here
628       // and the phi is useless.  This can happen even with the above phi
629       // removal for complex flows.  I cannot keep the better known value here
630       // because locally the phi appears to define a new merged value.  If I
631       // keep the better value then a copy of the phi, being unable to use the
632       // global flow analysis, can't "peek through" the phi to the original
633       // reaching value and so will act like it's defining a new value.  This
634       // can lead to situations where some uses are from the old and some from
635       // the new values.  Not illegal by itself but throws the over-strong
636       // assert in scheduling.
637       if( pidx ) {
638         value.map(preg,phi);
639         regnd.map(preg,phi);
640         int n_regs = RegMask::num_registers(phi->ideal_reg(), lrgs(pidx));
641         for (int l = 1; l < n_regs; l++) {
642           OptoReg::Name preg_lo = OptoReg::add(preg,-l);
643           value.map(preg_lo,phi);
644           regnd.map(preg_lo,phi);
645         }
646       }
647     }
648 
649     // For all remaining instructions
650     for (j = phi_dex; j < block->number_of_nodes(); j++) {
651       Node* n = block->get_node(j);
652 
653       if(n->outcnt() == 0 &&   // Dead?
654          n != C->top() &&      // (ignore TOP, it has no du info)
655          !n->is_Proj() ) {     // fat-proj kills
656         j -= yank_if_dead(n, block, &value, &regnd);
657         continue;
658       }
659 
660       // Improve reaching-def info.  Occasionally post-alloc's liveness gives

664       // advantage of this info to set a reaching def for the use-reg.
665       uint k;
666       for (k = 1; k < n->req(); k++) {
667         Node *def = n->in(k);   // n->in(k) is a USE; def is the DEF for this USE
668         guarantee(def != NULL, "no disconnected nodes at this point");
669         uint useidx = _lrg_map.live_range_id(def); // useidx is the live range index for this USE
670 
671         if( useidx ) {
672           OptoReg::Name ureg = lrgs(useidx).reg();
673           if( !value[ureg] ) {
674             int idx;            // Skip occasional useless copy
675             while( (idx=def->is_Copy()) != 0 &&
676                    def->in(idx) != NULL &&  // NULL should not happen
677                    ureg == lrgs(_lrg_map.live_range_id(def->in(idx))).reg())
678               def = def->in(idx);
679             Node *valdef = skip_copies(def); // tighten up val through non-useless copies
680             value.map(ureg,valdef); // record improved reaching-def info
681             regnd.map(ureg,   def);
682             // Record other half of doubles
683             uint def_ideal_reg = def->ideal_reg();
684             int n_regs = RegMask::num_registers(def_ideal_reg, lrgs(_lrg_map.live_range_id(def)));
685             for (int l = 1; l < n_regs; l++) {
686               OptoReg::Name ureg_lo = OptoReg::add(ureg,-l);
687               if (!value[ureg_lo] &&
688                   (!RegMask::can_represent(ureg_lo) ||
689                    lrgs(useidx).mask().Member(ureg_lo))) { // Nearly always adjacent
690                 value.map(ureg_lo,valdef); // record improved reaching-def info
691                 regnd.map(ureg_lo,   def);
692               }
693             }
694           }
695         }
696       }
697 
698       const uint two_adr = n->is_Mach() ? n->as_Mach()->two_adr() : 0;
699 
700       // Remove copies along input edges
701       for (k = 1; k < n->req(); k++) {
702         j -= elide_copy(n, k, block, value, regnd, two_adr != k);
703       }
704 

708         continue;
709       }
710 
711       // Update the register defined by this instruction
712       OptoReg::Name nreg = lrgs(lidx).reg();
713       // Skip through all copies to the _value_ being defined.
714       // Do not change from int to pointer
715       Node *val = skip_copies(n);
716 
717       // Clear out a dead definition before starting so that the
718       // elimination code doesn't have to guard against it.  The
719       // definition could in fact be a kill projection with a count of
720       // 0 which is safe but since those are uninteresting for copy
721       // elimination just delete them as well.
722       if (regnd[nreg] != NULL && regnd[nreg]->outcnt() == 0) {
723         regnd.map(nreg, NULL);
724         value.map(nreg, NULL);
725       }
726 
727       uint n_ideal_reg = n->ideal_reg();
728       int n_regs = RegMask::num_registers(n_ideal_reg, lrgs(lidx));
729       if (n_regs == 1) {
730         // If Node 'n' does not change the value mapped by the register,
731         // then 'n' is a useless copy.  Do not update the register->node
732         // mapping so 'n' will go dead.
733         if( value[nreg] != val ) {
734           if (eliminate_copy_of_constant(val, n, block, value, regnd, nreg, OptoReg::Bad)) {
735             j -= replace_and_yank_if_dead(n, nreg, block, value, regnd);
736           } else {
737             // Update the mapping: record new Node defined by the register
738             regnd.map(nreg,n);
739             // Update mapping for defined *value*, which is the defined
740             // Node after skipping all copies.
741             value.map(nreg,val);
742           }
743         } else if( !may_be_copy_of_callee(n) ) {
744           assert(n->is_Copy(), "");
745           j -= replace_and_yank_if_dead(n, nreg, block, value, regnd);
746         }
747       } else if (RegMask::is_vector(n_ideal_reg)) {
748         // If Node 'n' does not change the value mapped by the register,
< prev index next >