src/hotspot/share/opto/ifg.cpp
changeset 59081 95a99e617f28
parent 58962 2dcfc28a314d
equal deleted inserted replaced
59075:355f4f42dda5 59081:95a99e617f28
    78 // Convert triangular matrix to square matrix
    78 // Convert triangular matrix to square matrix
    79 void PhaseIFG::SquareUp() {
    79 void PhaseIFG::SquareUp() {
    80   assert( !_is_square, "only on triangular" );
    80   assert( !_is_square, "only on triangular" );
    81 
    81 
    82   // Simple transpose
    82   // Simple transpose
    83   for( uint i = 0; i < _maxlrg; i++ ) {
    83   for(uint i = 0; i < _maxlrg; i++ ) {
    84     IndexSetIterator elements(&_adjs[i]);
    84     if (!_adjs[i].is_empty()) {
    85     uint datum;
    85       IndexSetIterator elements(&_adjs[i]);
    86     while ((datum = elements.next()) != 0) {
    86       uint datum;
    87       _adjs[datum].insert( i );
    87       while ((datum = elements.next()) != 0) {
       
    88         _adjs[datum].insert(i);
       
    89       }
    88     }
    90     }
    89   }
    91   }
    90   _is_square = true;
    92   _is_square = true;
    91 }
    93 }
    92 
    94 
   106   //return _adjs[a].unordered_member(b);
   108   //return _adjs[a].unordered_member(b);
   107   return _adjs[a].member(b);
   109   return _adjs[a].member(b);
   108 }
   110 }
   109 
   111 
   110 // Union edges of B into A
   112 // Union edges of B into A
   111 void PhaseIFG::Union( uint a, uint b ) {
   113 void PhaseIFG::Union(uint a, uint b) {
   112   assert( _is_square, "only on square" );
   114   assert( _is_square, "only on square" );
   113   IndexSet *A = &_adjs[a];
   115   IndexSet *A = &_adjs[a];
   114   IndexSetIterator b_elements(&_adjs[b]);
   116   if (!_adjs[b].is_empty()) {
   115   uint datum;
   117     IndexSetIterator b_elements(&_adjs[b]);
   116   while ((datum = b_elements.next()) != 0) {
   118     uint datum;
   117     if(A->insert(datum)) {
   119     while ((datum = b_elements.next()) != 0) {
   118       _adjs[datum].insert(a);
   120       if (A->insert(datum)) {
   119       lrgs(a).invalid_degree();
   121         _adjs[datum].insert(a);
   120       lrgs(datum).invalid_degree();
   122         lrgs(a).invalid_degree();
       
   123         lrgs(datum).invalid_degree();
       
   124       }
   121     }
   125     }
   122   }
   126   }
   123 }
   127 }
   124 
   128 
   125 // Yank a Node and all connected edges from the IFG.  Return a
   129 // Yank a Node and all connected edges from the IFG.  Return a
   128   assert( _is_square, "only on square" );
   132   assert( _is_square, "only on square" );
   129   assert( !_yanked->test(a), "" );
   133   assert( !_yanked->test(a), "" );
   130   _yanked->set(a);
   134   _yanked->set(a);
   131 
   135 
   132   // I remove the LRG from all neighbors.
   136   // I remove the LRG from all neighbors.
   133   IndexSetIterator elements(&_adjs[a]);
       
   134   LRG &lrg_a = lrgs(a);
   137   LRG &lrg_a = lrgs(a);
   135   uint datum;
   138 
   136   while ((datum = elements.next()) != 0) {
   139   if (!_adjs[a].is_empty()) {
   137     _adjs[datum].remove(a);
   140     IndexSetIterator elements(&_adjs[a]);
   138     lrgs(datum).inc_degree( -lrg_a.compute_degree(lrgs(datum)) );
   141     uint datum;
       
   142     while ((datum = elements.next()) != 0) {
       
   143       _adjs[datum].remove(a);
       
   144       lrgs(datum).inc_degree(-lrg_a.compute_degree(lrgs(datum)));
       
   145     }
   139   }
   146   }
   140   return neighbors(a);
   147   return neighbors(a);
   141 }
   148 }
   142 
   149 
   143 // Re-insert a yanked Node.
   150 // Re-insert a yanked Node.
   144 void PhaseIFG::re_insert(uint a) {
   151 void PhaseIFG::re_insert(uint a) {
   145   assert( _is_square, "only on square" );
   152   assert( _is_square, "only on square" );
   146   assert( _yanked->test(a), "" );
   153   assert( _yanked->test(a), "" );
   147   _yanked->remove(a);
   154   _yanked->remove(a);
   148 
   155 
       
   156   if (_adjs[a].is_empty()) return;
       
   157 
   149   IndexSetIterator elements(&_adjs[a]);
   158   IndexSetIterator elements(&_adjs[a]);
   150   uint datum;
   159   uint datum;
   151   while ((datum = elements.next()) != 0) {
   160   while ((datum = elements.next()) != 0) {
   152     _adjs[datum].insert(a);
   161     _adjs[datum].insert(a);
   153     lrgs(datum).invalid_degree();
   162     lrgs(datum).invalid_degree();
   157 // Compute the degree between 2 live ranges.  If both live ranges are
   166 // Compute the degree between 2 live ranges.  If both live ranges are
   158 // aligned-adjacent powers-of-2 then we use the MAX size.  If either is
   167 // aligned-adjacent powers-of-2 then we use the MAX size.  If either is
   159 // mis-aligned (or for Fat-Projections, not-adjacent) then we have to
   168 // mis-aligned (or for Fat-Projections, not-adjacent) then we have to
   160 // MULTIPLY the sizes.  Inspect Brigg's thesis on register pairs to see why
   169 // MULTIPLY the sizes.  Inspect Brigg's thesis on register pairs to see why
   161 // this is so.
   170 // this is so.
   162 int LRG::compute_degree( LRG &l ) const {
   171 int LRG::compute_degree(LRG &l) const {
   163   int tmp;
   172   int tmp;
   164   int num_regs = _num_regs;
   173   int num_regs = _num_regs;
   165   int nregs = l.num_regs();
   174   int nregs = l.num_regs();
   166   tmp =  (_fat_proj || l._fat_proj)     // either is a fat-proj?
   175   tmp =  (_fat_proj || l._fat_proj)     // either is a fat-proj?
   167     ? (num_regs * nregs)                // then use product
   176     ? (num_regs * nregs)                // then use product
   172 // Compute effective degree for this live range.  If both live ranges are
   181 // Compute effective degree for this live range.  If both live ranges are
   173 // aligned-adjacent powers-of-2 then we use the MAX size.  If either is
   182 // aligned-adjacent powers-of-2 then we use the MAX size.  If either is
   174 // mis-aligned (or for Fat-Projections, not-adjacent) then we have to
   183 // mis-aligned (or for Fat-Projections, not-adjacent) then we have to
   175 // MULTIPLY the sizes.  Inspect Brigg's thesis on register pairs to see why
   184 // MULTIPLY the sizes.  Inspect Brigg's thesis on register pairs to see why
   176 // this is so.
   185 // this is so.
   177 int PhaseIFG::effective_degree( uint lidx ) const {
   186 int PhaseIFG::effective_degree(uint lidx) const {
       
   187   IndexSet *s = neighbors(lidx);
       
   188   if (s->is_empty()) return 0;
   178   int eff = 0;
   189   int eff = 0;
   179   int num_regs = lrgs(lidx).num_regs();
   190   int num_regs = lrgs(lidx).num_regs();
   180   int fat_proj = lrgs(lidx)._fat_proj;
   191   int fat_proj = lrgs(lidx)._fat_proj;
   181   IndexSet *s = neighbors(lidx);
       
   182   IndexSetIterator elements(s);
   192   IndexSetIterator elements(s);
   183   uint nidx;
   193   uint nidx;
   184   while((nidx = elements.next()) != 0) {
   194   while ((nidx = elements.next()) != 0) {
   185     LRG &lrgn = lrgs(nidx);
   195     LRG &lrgn = lrgs(nidx);
   186     int nregs = lrgn.num_regs();
   196     int nregs = lrgn.num_regs();
   187     eff += (fat_proj || lrgn._fat_proj) // either is a fat-proj?
   197     eff += (fat_proj || lrgn._fat_proj) // either is a fat-proj?
   188       ? (num_regs * nregs)              // then use product
   198       ? (num_regs * nregs)              // then use product
   189       : MAX2(num_regs,nregs);           // else use max
   199       : MAX2(num_regs,nregs);           // else use max
   194 
   204 
   195 #ifndef PRODUCT
   205 #ifndef PRODUCT
   196 void PhaseIFG::dump() const {
   206 void PhaseIFG::dump() const {
   197   tty->print_cr("-- Interference Graph --%s--",
   207   tty->print_cr("-- Interference Graph --%s--",
   198                 _is_square ? "square" : "triangular" );
   208                 _is_square ? "square" : "triangular" );
   199   if( _is_square ) {
   209   if (_is_square) {
   200     for( uint i = 0; i < _maxlrg; i++ ) {
   210     for (uint i = 0; i < _maxlrg; i++) {
   201       tty->print(_yanked->test(i) ? "XX " : "  ");
   211       tty->print(_yanked->test(i) ? "XX " : "  ");
   202       tty->print("L%d: { ",i);
   212       tty->print("L%d: { ",i);
   203       IndexSetIterator elements(&_adjs[i]);
   213       if (!_adjs[i].is_empty()) {
   204       uint datum;
   214         IndexSetIterator elements(&_adjs[i]);
   205       while ((datum = elements.next()) != 0) {
   215         uint datum;
   206         tty->print("L%d ", datum);
   216         while ((datum = elements.next()) != 0) {
       
   217           tty->print("L%d ", datum);
       
   218         }
   207       }
   219       }
   208       tty->print_cr("}");
   220       tty->print_cr("}");
   209 
   221 
   210     }
   222     }
   211     return;
   223     return;
   219     for( j = _maxlrg; j > i; j-- )
   231     for( j = _maxlrg; j > i; j-- )
   220       if( test_edge(j - 1,i) ) {
   232       if( test_edge(j - 1,i) ) {
   221         tty->print("L%d ",j - 1);
   233         tty->print("L%d ",j - 1);
   222       }
   234       }
   223     tty->print("| ");
   235     tty->print("| ");
   224     IndexSetIterator elements(&_adjs[i]);
   236     if (!_adjs[i].is_empty()) {
   225     uint datum;
   237       IndexSetIterator elements(&_adjs[i]);
   226     while ((datum = elements.next()) != 0) {
   238       uint datum;
   227       tty->print("L%d ", datum);
   239       while ((datum = elements.next()) != 0) {
       
   240         tty->print("L%d ", datum);
       
   241       }
   228     }
   242     }
   229     tty->print("}\n");
   243     tty->print("}\n");
   230   }
   244   }
   231   tty->print("\n");
   245   tty->print("\n");
   232 }
   246 }
   249 void PhaseIFG::verify( const PhaseChaitin *pc ) const {
   263 void PhaseIFG::verify( const PhaseChaitin *pc ) const {
   250   // IFG is square, sorted and no need for Find
   264   // IFG is square, sorted and no need for Find
   251   for( uint i = 0; i < _maxlrg; i++ ) {
   265   for( uint i = 0; i < _maxlrg; i++ ) {
   252     assert(!_yanked->test(i) || !neighbor_cnt(i), "Is removed completely" );
   266     assert(!_yanked->test(i) || !neighbor_cnt(i), "Is removed completely" );
   253     IndexSet *set = &_adjs[i];
   267     IndexSet *set = &_adjs[i];
   254     IndexSetIterator elements(set);
   268     if (!set->is_empty()) {
   255     uint idx;
   269       IndexSetIterator elements(set);
   256     uint last = 0;
   270       uint idx;
   257     while ((idx = elements.next()) != 0) {
   271       uint last = 0;
   258       assert(idx != i, "Must have empty diagonal");
   272       while ((idx = elements.next()) != 0) {
   259       assert(pc->_lrg_map.find_const(idx) == idx, "Must not need Find");
   273         assert(idx != i, "Must have empty diagonal");
   260       assert(_adjs[idx].member(i), "IFG not square");
   274         assert(pc->_lrg_map.find_const(idx) == idx, "Must not need Find");
   261       assert(!_yanked->test(idx), "No yanked neighbors");
   275         assert(_adjs[idx].member(i), "IFG not square");
   262       assert(last < idx, "not sorted increasing");
   276         assert(!_yanked->test(idx), "No yanked neighbors");
   263       last = idx;
   277         assert(last < idx, "not sorted increasing");
       
   278         last = idx;
       
   279       }
   264     }
   280     }
   265     assert(!lrgs(i)._degree_valid || effective_degree(i) == lrgs(i).degree(), "degree is valid but wrong");
   281     assert(!lrgs(i)._degree_valid || effective_degree(i) == lrgs(i).degree(), "degree is valid but wrong");
   266   }
   282   }
   267 }
   283 }
   268 #endif
   284 #endif
   271  * Interfere this register with everything currently live.
   287  * Interfere this register with everything currently live.
   272  * Check for interference by checking overlap of regmasks.
   288  * Check for interference by checking overlap of regmasks.
   273  * Only interfere if acceptable register masks overlap.
   289  * Only interfere if acceptable register masks overlap.
   274  */
   290  */
   275 void PhaseChaitin::interfere_with_live(uint lid, IndexSet* liveout) {
   291 void PhaseChaitin::interfere_with_live(uint lid, IndexSet* liveout) {
   276   LRG& lrg = lrgs(lid);
   292   if (!liveout->is_empty()) {
   277   const RegMask& rm = lrg.mask();
   293     LRG& lrg = lrgs(lid);
   278   IndexSetIterator elements(liveout);
   294     const RegMask &rm = lrg.mask();
   279   uint interfering_lid = elements.next();
   295     IndexSetIterator elements(liveout);
   280   while (interfering_lid != 0) {
   296     uint interfering_lid = elements.next();
   281     LRG& interfering_lrg = lrgs(interfering_lid);
   297     while (interfering_lid != 0) {
   282     if (rm.overlap(interfering_lrg.mask())) {
   298       LRG& interfering_lrg = lrgs(interfering_lid);
   283       _ifg->add_edge(lid, interfering_lid);
   299       if (rm.overlap(interfering_lrg.mask())) {
   284     }
   300         _ifg->add_edge(lid, interfering_lid);
   285     interfering_lid = elements.next();
   301       }
       
   302       interfering_lid = elements.next();
       
   303     }
   286   }
   304   }
   287 }
   305 }
   288 
   306 
   289 // Actually build the interference graph.  Uses virtual registers only, no
   307 // Actually build the interference graph.  Uses virtual registers only, no
   290 // physical register masks.  This allows me to be very aggressive when
   308 // physical register masks.  This allows me to be very aggressive when
   379   } // End of forall blocks
   397   } // End of forall blocks
   380 }
   398 }
   381 
   399 
   382 #ifdef ASSERT
   400 #ifdef ASSERT
   383 uint PhaseChaitin::count_int_pressure(IndexSet* liveout) {
   401 uint PhaseChaitin::count_int_pressure(IndexSet* liveout) {
       
   402   if (liveout->is_empty()) {
       
   403     return 0;
       
   404   }
   384   IndexSetIterator elements(liveout);
   405   IndexSetIterator elements(liveout);
   385   uint lidx = elements.next();
   406   uint lidx = elements.next();
   386   uint cnt = 0;
   407   uint cnt = 0;
   387   while (lidx != 0) {
   408   while (lidx != 0) {
   388     LRG& lrg = lrgs(lidx);
   409     LRG& lrg = lrgs(lidx);
   395   }
   416   }
   396   return cnt;
   417   return cnt;
   397 }
   418 }
   398 
   419 
   399 uint PhaseChaitin::count_float_pressure(IndexSet* liveout) {
   420 uint PhaseChaitin::count_float_pressure(IndexSet* liveout) {
       
   421   if (liveout->is_empty()) {
       
   422     return 0;
       
   423   }
   400   IndexSetIterator elements(liveout);
   424   IndexSetIterator elements(liveout);
   401   uint lidx = elements.next();
   425   uint lidx = elements.next();
   402   uint cnt = 0;
   426   uint cnt = 0;
   403   while (lidx != 0) {
   427   while (lidx != 0) {
   404     LRG& lrg = lrgs(lidx);
   428     LRG& lrg = lrgs(lidx);
   492  * We add the cost for the whole block to the area of the live ranges initially.
   516  * We add the cost for the whole block to the area of the live ranges initially.
   493  * If a live range gets killed in the block, we'll subtract the unused part of
   517  * If a live range gets killed in the block, we'll subtract the unused part of
   494  * the block from the area.
   518  * the block from the area.
   495  */
   519  */
   496 void PhaseChaitin::compute_initial_block_pressure(Block* b, IndexSet* liveout, Pressure& int_pressure, Pressure& float_pressure, double cost) {
   520 void PhaseChaitin::compute_initial_block_pressure(Block* b, IndexSet* liveout, Pressure& int_pressure, Pressure& float_pressure, double cost) {
   497   IndexSetIterator elements(liveout);
   521   if (!liveout->is_empty()) {
   498   uint lid = elements.next();
   522     IndexSetIterator elements(liveout);
   499   while (lid != 0) {
   523     uint lid = elements.next();
   500     LRG& lrg = lrgs(lid);
   524     while (lid != 0) {
   501     lrg._area += cost;
   525       LRG &lrg = lrgs(lid);
   502     raise_pressure(b, lrg, int_pressure, float_pressure);
   526       lrg._area += cost;
   503     lid = elements.next();
   527       raise_pressure(b, lrg, int_pressure, float_pressure);
       
   528       lid = elements.next();
       
   529     }
   504   }
   530   }
   505   assert(int_pressure.current_pressure() == count_int_pressure(liveout), "the int pressure is incorrect");
   531   assert(int_pressure.current_pressure() == count_int_pressure(liveout), "the int pressure is incorrect");
   506   assert(float_pressure.current_pressure() == count_float_pressure(liveout), "the float pressure is incorrect");
   532   assert(float_pressure.current_pressure() == count_float_pressure(liveout), "the float pressure is incorrect");
   507 }
   533 }
   508 
   534 
   510 * Computes the entry register pressure of a block, looking at all live
   536 * Computes the entry register pressure of a block, looking at all live
   511 * ranges in the livein. The register pressure is computed for both float
   537 * ranges in the livein. The register pressure is computed for both float
   512 * and int/pointer registers.
   538 * and int/pointer registers.
   513 */
   539 */
   514 void PhaseChaitin::compute_entry_block_pressure(Block* b) {
   540 void PhaseChaitin::compute_entry_block_pressure(Block* b) {
   515   IndexSet* livein = _live->livein(b);
   541   IndexSet *livein = _live->livein(b);
   516   IndexSetIterator elements(livein);
   542   if (!livein->is_empty()) {
   517   uint lid = elements.next();
   543     IndexSetIterator elements(livein);
   518   while (lid != 0) {
   544     uint lid = elements.next();
   519     LRG& lrg = lrgs(lid);
   545     while (lid != 0) {
   520     raise_pressure(b, lrg, _sched_int_pressure, _sched_float_pressure);
   546       LRG &lrg = lrgs(lid);
   521     lid = elements.next();
   547       raise_pressure(b, lrg, _sched_int_pressure, _sched_float_pressure);
       
   548       lid = elements.next();
       
   549     }
   522   }
   550   }
   523   // Now check phis for locally defined inputs
   551   // Now check phis for locally defined inputs
   524   for (uint j = 0; j < b->number_of_nodes(); j++) {
   552   for (uint j = 0; j < b->number_of_nodes(); j++) {
   525     Node* n = b->get_node(j);
   553     Node* n = b->get_node(j);
   526     if (n->is_Phi()) {
   554     if (n->is_Phi()) {
   544 * Computes the exit register pressure of a block, looking at all live
   572 * Computes the exit register pressure of a block, looking at all live
   545 * ranges in the liveout. The register pressure is computed for both float
   573 * ranges in the liveout. The register pressure is computed for both float
   546 * and int/pointer registers.
   574 * and int/pointer registers.
   547 */
   575 */
   548 void PhaseChaitin::compute_exit_block_pressure(Block* b) {
   576 void PhaseChaitin::compute_exit_block_pressure(Block* b) {
       
   577 
   549   IndexSet* livein = _live->live(b);
   578   IndexSet* livein = _live->live(b);
   550   IndexSetIterator elements(livein);
       
   551   _sched_int_pressure.set_current_pressure(0);
   579   _sched_int_pressure.set_current_pressure(0);
   552   _sched_float_pressure.set_current_pressure(0);
   580   _sched_float_pressure.set_current_pressure(0);
   553   uint lid = elements.next();
   581   if (!livein->is_empty()) {
   554   while (lid != 0) {
   582     IndexSetIterator elements(livein);
   555     LRG& lrg = lrgs(lid);
   583     uint lid = elements.next();
   556     raise_pressure(b, lrg, _sched_int_pressure, _sched_float_pressure);
   584     while (lid != 0) {
   557     lid = elements.next();
   585       LRG &lrg = lrgs(lid);
       
   586       raise_pressure(b, lrg, _sched_int_pressure, _sched_float_pressure);
       
   587       lid = elements.next();
       
   588     }
   558   }
   589   }
   559 }
   590 }
   560 
   591 
   561 /*
   592 /*
   562  * Remove dead node if it's not used.
   593  * Remove dead node if it's not used.
   652 /*
   683 /*
   653  * The defined value must go in a particular register. Remove that register from
   684  * The defined value must go in a particular register. Remove that register from
   654  * all conflicting parties and avoid the interference.
   685  * all conflicting parties and avoid the interference.
   655  */
   686  */
   656 void PhaseChaitin::remove_bound_register_from_interfering_live_ranges(LRG& lrg, IndexSet* liveout, uint& must_spill) {
   687 void PhaseChaitin::remove_bound_register_from_interfering_live_ranges(LRG& lrg, IndexSet* liveout, uint& must_spill) {
       
   688   if (liveout->is_empty()) return;
   657   // Check for common case
   689   // Check for common case
   658   const RegMask& rm = lrg.mask();
   690   const RegMask& rm = lrg.mask();
   659   int r_size = lrg.num_regs();
   691   int r_size = lrg.num_regs();
   660   // Smear odd bits
   692   // Smear odd bits
   661   IndexSetIterator elements(liveout);
   693   IndexSetIterator elements(liveout);
   831 
   863 
   832     for (uint location = last_inst; location > 0; location--) {
   864     for (uint location = last_inst; location > 0; location--) {
   833       Node* n = block->get_node(location);
   865       Node* n = block->get_node(location);
   834       uint lid = _lrg_map.live_range_id(n);
   866       uint lid = _lrg_map.live_range_id(n);
   835 
   867 
   836       if(lid) {
   868       if (lid) {
   837         LRG& lrg = lrgs(lid);
   869         LRG& lrg = lrgs(lid);
   838 
   870 
   839         // A DEF normally costs block frequency; rematerialized values are
   871         // A DEF normally costs block frequency; rematerialized values are
   840         // removed from the DEF sight, so LOWER costs here.
   872         // removed from the DEF sight, so LOWER costs here.
   841         lrg._cost += n->rematerialize() ? 0 : block->_freq;
   873         lrg._cost += n->rematerialize() ? 0 : block->_freq;