1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/coalesce.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,915 @@ 1.4 +/* 1.5 + * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_coalesce.cpp.incl" 1.30 + 1.31 +//============================================================================= 1.32 +//------------------------------reset_uf_map----------------------------------- 1.33 +void PhaseChaitin::reset_uf_map( uint maxlrg ) { 1.34 + _maxlrg = maxlrg; 1.35 + // Force the Union-Find mapping to be at least this large 1.36 + _uf_map.extend(_maxlrg,0); 1.37 + // Initialize it to be the ID mapping. 1.38 + for( uint i=0; i<_maxlrg; i++ ) 1.39 + _uf_map.map(i,i); 1.40 +} 1.41 + 1.42 +//------------------------------compress_uf_map-------------------------------- 1.43 +// Make all Nodes map directly to their final live range; no need for 1.44 +// the Union-Find mapping after this call. 1.45 +void PhaseChaitin::compress_uf_map_for_nodes( ) { 1.46 + // For all Nodes, compress mapping 1.47 + uint unique = _names.Size(); 1.48 + for( uint i=0; i<unique; i++ ) { 1.49 + uint lrg = _names[i]; 1.50 + uint compressed_lrg = Find(lrg); 1.51 + if( lrg != compressed_lrg ) 1.52 + _names.map(i,compressed_lrg); 1.53 + } 1.54 +} 1.55 + 1.56 +//------------------------------Find------------------------------------------- 1.57 +// Straight out of Tarjan's union-find algorithm 1.58 +uint PhaseChaitin::Find_compress( uint lrg ) { 1.59 + uint cur = lrg; 1.60 + uint next = _uf_map[cur]; 1.61 + while( next != cur ) { // Scan chain of equivalences 1.62 + assert( next < cur, "always union smaller" ); 1.63 + cur = next; // until find a fixed-point 1.64 + next = _uf_map[cur]; 1.65 + } 1.66 + // Core of union-find algorithm: update chain of 1.67 + // equivalences to be equal to the root. 1.68 + while( lrg != next ) { 1.69 + uint tmp = _uf_map[lrg]; 1.70 + _uf_map.map(lrg, next); 1.71 + lrg = tmp; 1.72 + } 1.73 + return lrg; 1.74 +} 1.75 + 1.76 +//------------------------------Find------------------------------------------- 1.77 +// Straight out of Tarjan's union-find algorithm 1.78 +uint PhaseChaitin::Find_compress( const Node *n ) { 1.79 + uint lrg = Find_compress(_names[n->_idx]); 1.80 + _names.map(n->_idx,lrg); 1.81 + return lrg; 1.82 +} 1.83 + 1.84 +//------------------------------Find_const------------------------------------- 1.85 +// Like Find above, but no path compress, so bad asymptotic behavior 1.86 +uint PhaseChaitin::Find_const( uint lrg ) const { 1.87 + if( !lrg ) return lrg; // Ignore the zero LRG 1.88 + // Off the end? This happens during debugging dumps when you got 1.89 + // brand new live ranges but have not told the allocator yet. 1.90 + if( lrg >= _maxlrg ) return lrg; 1.91 + uint next = _uf_map[lrg]; 1.92 + while( next != lrg ) { // Scan chain of equivalences 1.93 + assert( next < lrg, "always union smaller" ); 1.94 + lrg = next; // until find a fixed-point 1.95 + next = _uf_map[lrg]; 1.96 + } 1.97 + return next; 1.98 +} 1.99 + 1.100 +//------------------------------Find------------------------------------------- 1.101 +// Like Find above, but no path compress, so bad asymptotic behavior 1.102 +uint PhaseChaitin::Find_const( const Node *n ) const { 1.103 + if( n->_idx >= _names.Size() ) return 0; // not mapped, usual for debug dump 1.104 + return Find_const( _names[n->_idx] ); 1.105 +} 1.106 + 1.107 +//------------------------------Union------------------------------------------ 1.108 +// union 2 sets together. 1.109 +void PhaseChaitin::Union( const Node *src_n, const Node *dst_n ) { 1.110 + uint src = Find(src_n); 1.111 + uint dst = Find(dst_n); 1.112 + assert( src, "" ); 1.113 + assert( dst, "" ); 1.114 + assert( src < _maxlrg, "oob" ); 1.115 + assert( dst < _maxlrg, "oob" ); 1.116 + assert( src < dst, "always union smaller" ); 1.117 + _uf_map.map(dst,src); 1.118 +} 1.119 + 1.120 +//------------------------------new_lrg---------------------------------------- 1.121 +void PhaseChaitin::new_lrg( const Node *x, uint lrg ) { 1.122 + // Make the Node->LRG mapping 1.123 + _names.extend(x->_idx,lrg); 1.124 + // Make the Union-Find mapping an identity function 1.125 + _uf_map.extend(lrg,lrg); 1.126 +} 1.127 + 1.128 +//------------------------------clone_projs------------------------------------ 1.129 +// After cloning some rematierialized instruction, clone any MachProj's that 1.130 +// follow it. Example: Intel zero is XOR, kills flags. Sparc FP constants 1.131 +// use G3 as an address temp. 1.132 +int PhaseChaitin::clone_projs( Block *b, uint idx, Node *con, Node *copy, uint &maxlrg ) { 1.133 + Block *bcon = _cfg._bbs[con->_idx]; 1.134 + uint cindex = bcon->find_node(con); 1.135 + Node *con_next = bcon->_nodes[cindex+1]; 1.136 + if( con_next->in(0) != con || con_next->Opcode() != Op_MachProj ) 1.137 + return false; // No MachProj's follow 1.138 + 1.139 + // Copy kills after the cloned constant 1.140 + Node *kills = con_next->clone(); 1.141 + kills->set_req( 0, copy ); 1.142 + b->_nodes.insert( idx, kills ); 1.143 + _cfg._bbs.map( kills->_idx, b ); 1.144 + new_lrg( kills, maxlrg++ ); 1.145 + return true; 1.146 +} 1.147 + 1.148 +//------------------------------compact---------------------------------------- 1.149 +// Renumber the live ranges to compact them. Makes the IFG smaller. 1.150 +void PhaseChaitin::compact() { 1.151 + // Current the _uf_map contains a series of short chains which are headed 1.152 + // by a self-cycle. All the chains run from big numbers to little numbers. 1.153 + // The Find() call chases the chains & shortens them for the next Find call. 1.154 + // We are going to change this structure slightly. Numbers above a moving 1.155 + // wave 'i' are unchanged. Numbers below 'j' point directly to their 1.156 + // compacted live range with no further chaining. There are no chains or 1.157 + // cycles below 'i', so the Find call no longer works. 1.158 + uint j=1; 1.159 + uint i; 1.160 + for( i=1; i < _maxlrg; i++ ) { 1.161 + uint lr = _uf_map[i]; 1.162 + // Ignore unallocated live ranges 1.163 + if( !lr ) continue; 1.164 + assert( lr <= i, "" ); 1.165 + _uf_map.map(i, ( lr == i ) ? j++ : _uf_map[lr]); 1.166 + } 1.167 + if( false ) // PrintOptoCompactLiveRanges 1.168 + printf("Compacted %d LRs from %d\n",i-j,i); 1.169 + // Now change the Node->LR mapping to reflect the compacted names 1.170 + uint unique = _names.Size(); 1.171 + for( i=0; i<unique; i++ ) 1.172 + _names.map(i,_uf_map[_names[i]]); 1.173 + 1.174 + // Reset the Union-Find mapping 1.175 + reset_uf_map(j); 1.176 + 1.177 +} 1.178 + 1.179 +//============================================================================= 1.180 +//------------------------------Dump------------------------------------------- 1.181 +#ifndef PRODUCT 1.182 +void PhaseCoalesce::dump( Node *n ) const { 1.183 + // Being a const function means I cannot use 'Find' 1.184 + uint r = _phc.Find(n); 1.185 + tty->print("L%d/N%d ",r,n->_idx); 1.186 +} 1.187 + 1.188 +//------------------------------dump------------------------------------------- 1.189 +void PhaseCoalesce::dump() const { 1.190 + // I know I have a block layout now, so I can print blocks in a loop 1.191 + for( uint i=0; i<_phc._cfg._num_blocks; i++ ) { 1.192 + uint j; 1.193 + Block *b = _phc._cfg._blocks[i]; 1.194 + // Print a nice block header 1.195 + tty->print("B%d: ",b->_pre_order); 1.196 + for( j=1; j<b->num_preds(); j++ ) 1.197 + tty->print("B%d ", _phc._cfg._bbs[b->pred(j)->_idx]->_pre_order); 1.198 + tty->print("-> "); 1.199 + for( j=0; j<b->_num_succs; j++ ) 1.200 + tty->print("B%d ",b->_succs[j]->_pre_order); 1.201 + tty->print(" IDom: B%d/#%d\n", b->_idom ? b->_idom->_pre_order : 0, b->_dom_depth); 1.202 + uint cnt = b->_nodes.size(); 1.203 + for( j=0; j<cnt; j++ ) { 1.204 + Node *n = b->_nodes[j]; 1.205 + dump( n ); 1.206 + tty->print("\t%s\t",n->Name()); 1.207 + 1.208 + // Dump the inputs 1.209 + uint k; // Exit value of loop 1.210 + for( k=0; k<n->req(); k++ ) // For all required inputs 1.211 + if( n->in(k) ) dump( n->in(k) ); 1.212 + else tty->print("_ "); 1.213 + int any_prec = 0; 1.214 + for( ; k<n->len(); k++ ) // For all precedence inputs 1.215 + if( n->in(k) ) { 1.216 + if( !any_prec++ ) tty->print(" |"); 1.217 + dump( n->in(k) ); 1.218 + } 1.219 + 1.220 + // Dump node-specific info 1.221 + n->dump_spec(tty); 1.222 + tty->print("\n"); 1.223 + 1.224 + } 1.225 + tty->print("\n"); 1.226 + } 1.227 +} 1.228 +#endif 1.229 + 1.230 +//------------------------------combine_these_two------------------------------ 1.231 +// Combine the live ranges def'd by these 2 Nodes. N2 is an input to N1. 1.232 +void PhaseCoalesce::combine_these_two( Node *n1, Node *n2 ) { 1.233 + uint lr1 = _phc.Find(n1); 1.234 + uint lr2 = _phc.Find(n2); 1.235 + if( lr1 != lr2 && // Different live ranges already AND 1.236 + !_phc._ifg->test_edge_sq( lr1, lr2 ) ) { // Do not interfere 1.237 + LRG *lrg1 = &_phc.lrgs(lr1); 1.238 + LRG *lrg2 = &_phc.lrgs(lr2); 1.239 + // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK. 1.240 + 1.241 + // Now, why is int->oop OK? We end up declaring a raw-pointer as an oop 1.242 + // and in general that's a bad thing. However, int->oop conversions only 1.243 + // happen at GC points, so the lifetime of the misclassified raw-pointer 1.244 + // is from the CheckCastPP (that converts it to an oop) backwards up 1.245 + // through a merge point and into the slow-path call, and around the 1.246 + // diamond up to the heap-top check and back down into the slow-path call. 1.247 + // The misclassified raw pointer is NOT live across the slow-path call, 1.248 + // and so does not appear in any GC info, so the fact that it is 1.249 + // misclassified is OK. 1.250 + 1.251 + if( (lrg1->_is_oop || !lrg2->_is_oop) && // not an oop->int cast AND 1.252 + // Compatible final mask 1.253 + lrg1->mask().overlap( lrg2->mask() ) ) { 1.254 + // Merge larger into smaller. 1.255 + if( lr1 > lr2 ) { 1.256 + uint tmp = lr1; lr1 = lr2; lr2 = tmp; 1.257 + Node *n = n1; n1 = n2; n2 = n; 1.258 + LRG *ltmp = lrg1; lrg1 = lrg2; lrg2 = ltmp; 1.259 + } 1.260 + // Union lr2 into lr1 1.261 + _phc.Union( n1, n2 ); 1.262 + if (lrg1->_maxfreq < lrg2->_maxfreq) 1.263 + lrg1->_maxfreq = lrg2->_maxfreq; 1.264 + // Merge in the IFG 1.265 + _phc._ifg->Union( lr1, lr2 ); 1.266 + // Combine register restrictions 1.267 + lrg1->AND(lrg2->mask()); 1.268 + } 1.269 + } 1.270 +} 1.271 + 1.272 +//------------------------------coalesce_driver-------------------------------- 1.273 +// Copy coalescing 1.274 +void PhaseCoalesce::coalesce_driver( ) { 1.275 + 1.276 + verify(); 1.277 + // Coalesce from high frequency to low 1.278 + for( uint i=0; i<_phc._cfg._num_blocks; i++ ) 1.279 + coalesce( _phc._blks[i] ); 1.280 + 1.281 +} 1.282 + 1.283 +//------------------------------insert_copy_with_overlap----------------------- 1.284 +// I am inserting copies to come out of SSA form. In the general case, I am 1.285 +// doing a parallel renaming. I'm in the Named world now, so I can't do a 1.286 +// general parallel renaming. All the copies now use "names" (live-ranges) 1.287 +// to carry values instead of the explicit use-def chains. Suppose I need to 1.288 +// insert 2 copies into the same block. They copy L161->L128 and L128->L132. 1.289 +// If I insert them in the wrong order then L128 will get clobbered before it 1.290 +// can get used by the second copy. This cannot happen in the SSA model; 1.291 +// direct use-def chains get me the right value. It DOES happen in the named 1.292 +// model so I have to handle the reordering of copies. 1.293 +// 1.294 +// In general, I need to topo-sort the placed copies to avoid conflicts. 1.295 +// Its possible to have a closed cycle of copies (e.g., recirculating the same 1.296 +// values around a loop). In this case I need a temp to break the cycle. 1.297 +void PhaseAggressiveCoalesce::insert_copy_with_overlap( Block *b, Node *copy, uint dst_name, uint src_name ) { 1.298 + 1.299 + // Scan backwards for the locations of the last use of the dst_name. 1.300 + // I am about to clobber the dst_name, so the copy must be inserted 1.301 + // after the last use. Last use is really first-use on a backwards scan. 1.302 + uint i = b->end_idx()-1; 1.303 + while( 1 ) { 1.304 + Node *n = b->_nodes[i]; 1.305 + // Check for end of virtual copies; this is also the end of the 1.306 + // parallel renaming effort. 1.307 + if( n->_idx < _unique ) break; 1.308 + uint idx = n->is_Copy(); 1.309 + assert( idx || n->is_Con() || n->Opcode() == Op_MachProj, "Only copies during parallel renaming" ); 1.310 + if( idx && _phc.Find(n->in(idx)) == dst_name ) break; 1.311 + i--; 1.312 + } 1.313 + uint last_use_idx = i; 1.314 + 1.315 + // Also search for any kill of src_name that exits the block. 1.316 + // Since the copy uses src_name, I have to come before any kill. 1.317 + uint kill_src_idx = b->end_idx(); 1.318 + // There can be only 1 kill that exits any block and that is 1.319 + // the last kill. Thus it is the first kill on a backwards scan. 1.320 + i = b->end_idx()-1; 1.321 + while( 1 ) { 1.322 + Node *n = b->_nodes[i]; 1.323 + // Check for end of virtual copies; this is also the end of the 1.324 + // parallel renaming effort. 1.325 + if( n->_idx < _unique ) break; 1.326 + assert( n->is_Copy() || n->is_Con() || n->Opcode() == Op_MachProj, "Only copies during parallel renaming" ); 1.327 + if( _phc.Find(n) == src_name ) { 1.328 + kill_src_idx = i; 1.329 + break; 1.330 + } 1.331 + i--; 1.332 + } 1.333 + // Need a temp? Last use of dst comes after the kill of src? 1.334 + if( last_use_idx >= kill_src_idx ) { 1.335 + // Need to break a cycle with a temp 1.336 + uint idx = copy->is_Copy(); 1.337 + Node *tmp = copy->clone(); 1.338 + _phc.new_lrg(tmp,_phc._maxlrg++); 1.339 + // Insert new temp between copy and source 1.340 + tmp ->set_req(idx,copy->in(idx)); 1.341 + copy->set_req(idx,tmp); 1.342 + // Save source in temp early, before source is killed 1.343 + b->_nodes.insert(kill_src_idx,tmp); 1.344 + _phc._cfg._bbs.map( tmp->_idx, b ); 1.345 + last_use_idx++; 1.346 + } 1.347 + 1.348 + // Insert just after last use 1.349 + b->_nodes.insert(last_use_idx+1,copy); 1.350 +} 1.351 + 1.352 +//------------------------------insert_copies---------------------------------- 1.353 +void PhaseAggressiveCoalesce::insert_copies( Matcher &matcher ) { 1.354 + // We do LRGs compressing and fix a liveout data only here since the other 1.355 + // place in Split() is guarded by the assert which we never hit. 1.356 + _phc.compress_uf_map_for_nodes(); 1.357 + // Fix block's liveout data for compressed live ranges. 1.358 + for(uint lrg = 1; lrg < _phc._maxlrg; lrg++ ) { 1.359 + uint compressed_lrg = _phc.Find(lrg); 1.360 + if( lrg != compressed_lrg ) { 1.361 + for( uint bidx = 0; bidx < _phc._cfg._num_blocks; bidx++ ) { 1.362 + IndexSet *liveout = _phc._live->live(_phc._cfg._blocks[bidx]); 1.363 + if( liveout->member(lrg) ) { 1.364 + liveout->remove(lrg); 1.365 + liveout->insert(compressed_lrg); 1.366 + } 1.367 + } 1.368 + } 1.369 + } 1.370 + 1.371 + // All new nodes added are actual copies to replace virtual copies. 1.372 + // Nodes with index less than '_unique' are original, non-virtual Nodes. 1.373 + _unique = C->unique(); 1.374 + 1.375 + for( uint i=0; i<_phc._cfg._num_blocks; i++ ) { 1.376 + Block *b = _phc._cfg._blocks[i]; 1.377 + uint cnt = b->num_preds(); // Number of inputs to the Phi 1.378 + 1.379 + for( uint l = 1; l<b->_nodes.size(); l++ ) { 1.380 + Node *n = b->_nodes[l]; 1.381 + 1.382 + // Do not use removed-copies, use copied value instead 1.383 + uint ncnt = n->req(); 1.384 + for( uint k = 1; k<ncnt; k++ ) { 1.385 + Node *copy = n->in(k); 1.386 + uint cidx = copy->is_Copy(); 1.387 + if( cidx ) { 1.388 + Node *def = copy->in(cidx); 1.389 + if( _phc.Find(copy) == _phc.Find(def) ) 1.390 + n->set_req(k,def); 1.391 + } 1.392 + } 1.393 + 1.394 + // Remove any explicit copies that get coalesced. 1.395 + uint cidx = n->is_Copy(); 1.396 + if( cidx ) { 1.397 + Node *def = n->in(cidx); 1.398 + if( _phc.Find(n) == _phc.Find(def) ) { 1.399 + n->replace_by(def); 1.400 + n->set_req(cidx,NULL); 1.401 + b->_nodes.remove(l); 1.402 + l--; 1.403 + continue; 1.404 + } 1.405 + } 1.406 + 1.407 + if( n->is_Phi() ) { 1.408 + // Get the chosen name for the Phi 1.409 + uint phi_name = _phc.Find( n ); 1.410 + // Ignore the pre-allocated specials 1.411 + if( !phi_name ) continue; 1.412 + // Check for mismatch inputs to Phi 1.413 + for( uint j = 1; j<cnt; j++ ) { 1.414 + Node *m = n->in(j); 1.415 + uint src_name = _phc.Find(m); 1.416 + if( src_name != phi_name ) { 1.417 + Block *pred = _phc._cfg._bbs[b->pred(j)->_idx]; 1.418 + Node *copy; 1.419 + assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach"); 1.420 + // Rematerialize constants instead of copying them 1.421 + if( m->is_Mach() && m->as_Mach()->is_Con() && 1.422 + m->as_Mach()->rematerialize() ) { 1.423 + copy = m->clone(); 1.424 + // Insert the copy in the predecessor basic block 1.425 + pred->add_inst(copy); 1.426 + // Copy any flags as well 1.427 + _phc.clone_projs( pred, pred->end_idx(), m, copy, _phc._maxlrg ); 1.428 + } else { 1.429 + const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()]; 1.430 + copy = new (C) MachSpillCopyNode(m,*rm,*rm); 1.431 + // Find a good place to insert. Kinda tricky, use a subroutine 1.432 + insert_copy_with_overlap(pred,copy,phi_name,src_name); 1.433 + } 1.434 + // Insert the copy in the use-def chain 1.435 + n->set_req( j, copy ); 1.436 + _phc._cfg._bbs.map( copy->_idx, pred ); 1.437 + // Extend ("register allocate") the names array for the copy. 1.438 + _phc._names.extend( copy->_idx, phi_name ); 1.439 + } // End of if Phi names do not match 1.440 + } // End of for all inputs to Phi 1.441 + } else { // End of if Phi 1.442 + 1.443 + // Now check for 2-address instructions 1.444 + uint idx; 1.445 + if( n->is_Mach() && (idx=n->as_Mach()->two_adr()) ) { 1.446 + // Get the chosen name for the Node 1.447 + uint name = _phc.Find( n ); 1.448 + assert( name, "no 2-address specials" ); 1.449 + // Check for name mis-match on the 2-address input 1.450 + Node *m = n->in(idx); 1.451 + if( _phc.Find(m) != name ) { 1.452 + Node *copy; 1.453 + assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach"); 1.454 + // At this point it is unsafe to extend live ranges (6550579). 1.455 + // Rematerialize only constants as we do for Phi above. 1.456 + if( m->is_Mach() && m->as_Mach()->is_Con() && 1.457 + m->as_Mach()->rematerialize() ) { 1.458 + copy = m->clone(); 1.459 + // Insert the copy in the basic block, just before us 1.460 + b->_nodes.insert( l++, copy ); 1.461 + if( _phc.clone_projs( b, l, m, copy, _phc._maxlrg ) ) 1.462 + l++; 1.463 + } else { 1.464 + const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()]; 1.465 + copy = new (C) MachSpillCopyNode( m, *rm, *rm ); 1.466 + // Insert the copy in the basic block, just before us 1.467 + b->_nodes.insert( l++, copy ); 1.468 + } 1.469 + // Insert the copy in the use-def chain 1.470 + n->set_req(idx, copy ); 1.471 + // Extend ("register allocate") the names array for the copy. 1.472 + _phc._names.extend( copy->_idx, name ); 1.473 + _phc._cfg._bbs.map( copy->_idx, b ); 1.474 + } 1.475 + 1.476 + } // End of is two-adr 1.477 + 1.478 + // Insert a copy at a debug use for a lrg which has high frequency 1.479 + if( (b->_freq < OPTO_DEBUG_SPLIT_FREQ) && n->is_MachSafePoint() ) { 1.480 + // Walk the debug inputs to the node and check for lrg freq 1.481 + JVMState* jvms = n->jvms(); 1.482 + uint debug_start = jvms ? jvms->debug_start() : 999999; 1.483 + uint debug_end = jvms ? jvms->debug_end() : 999999; 1.484 + for(uint inpidx = debug_start; inpidx < debug_end; inpidx++) { 1.485 + // Do not split monitors; they are only needed for debug table 1.486 + // entries and need no code. 1.487 + if( jvms->is_monitor_use(inpidx) ) continue; 1.488 + Node *inp = n->in(inpidx); 1.489 + uint nidx = _phc.n2lidx(inp); 1.490 + LRG &lrg = lrgs(nidx); 1.491 + 1.492 + // If this lrg has a high frequency use/def 1.493 + if( lrg._maxfreq >= OPTO_LRG_HIGH_FREQ ) { 1.494 + // If the live range is also live out of this block (like it 1.495 + // would be for a fast/slow idiom), the normal spill mechanism 1.496 + // does an excellent job. If it is not live out of this block 1.497 + // (like it would be for debug info to uncommon trap) splitting 1.498 + // the live range now allows a better allocation in the high 1.499 + // frequency blocks. 1.500 + // Build_IFG_virtual has converted the live sets to 1.501 + // live-IN info, not live-OUT info. 1.502 + uint k; 1.503 + for( k=0; k < b->_num_succs; k++ ) 1.504 + if( _phc._live->live(b->_succs[k])->member( nidx ) ) 1.505 + break; // Live in to some successor block? 1.506 + if( k < b->_num_succs ) 1.507 + continue; // Live out; do not pre-split 1.508 + // Split the lrg at this use 1.509 + const RegMask *rm = C->matcher()->idealreg2spillmask[inp->ideal_reg()]; 1.510 + Node *copy = new (C) MachSpillCopyNode( inp, *rm, *rm ); 1.511 + // Insert the copy in the use-def chain 1.512 + n->set_req(inpidx, copy ); 1.513 + // Insert the copy in the basic block, just before us 1.514 + b->_nodes.insert( l++, copy ); 1.515 + // Extend ("register allocate") the names array for the copy. 1.516 + _phc.new_lrg( copy, _phc._maxlrg++ ); 1.517 + _phc._cfg._bbs.map( copy->_idx, b ); 1.518 + //tty->print_cr("Split a debug use in Aggressive Coalesce"); 1.519 + } // End of if high frequency use/def 1.520 + } // End of for all debug inputs 1.521 + } // End of if low frequency safepoint 1.522 + 1.523 + } // End of if Phi 1.524 + 1.525 + } // End of for all instructions 1.526 + } // End of for all blocks 1.527 +} 1.528 + 1.529 +//============================================================================= 1.530 +//------------------------------coalesce--------------------------------------- 1.531 +// Aggressive (but pessimistic) copy coalescing of a single block 1.532 + 1.533 +// The following coalesce pass represents a single round of aggressive 1.534 +// pessimistic coalesce. "Aggressive" means no attempt to preserve 1.535 +// colorability when coalescing. This occasionally means more spills, but 1.536 +// it also means fewer rounds of coalescing for better code - and that means 1.537 +// faster compiles. 1.538 + 1.539 +// "Pessimistic" means we do not hit the fixed point in one pass (and we are 1.540 +// reaching for the least fixed point to boot). This is typically solved 1.541 +// with a few more rounds of coalescing, but the compiler must run fast. We 1.542 +// could optimistically coalescing everything touching PhiNodes together 1.543 +// into one big live range, then check for self-interference. Everywhere 1.544 +// the live range interferes with self it would have to be split. Finding 1.545 +// the right split points can be done with some heuristics (based on 1.546 +// expected frequency of edges in the live range). In short, it's a real 1.547 +// research problem and the timeline is too short to allow such research. 1.548 +// Further thoughts: (1) build the LR in a pass, (2) find self-interference 1.549 +// in another pass, (3) per each self-conflict, split, (4) split by finding 1.550 +// the low-cost cut (min-cut) of the LR, (5) edges in the LR are weighted 1.551 +// according to the GCM algorithm (or just exec freq on CFG edges). 1.552 + 1.553 +void PhaseAggressiveCoalesce::coalesce( Block *b ) { 1.554 + // Copies are still "virtual" - meaning we have not made them explicitly 1.555 + // copies. Instead, Phi functions of successor blocks have mis-matched 1.556 + // live-ranges. If I fail to coalesce, I'll have to insert a copy to line 1.557 + // up the live-ranges. Check for Phis in successor blocks. 1.558 + uint i; 1.559 + for( i=0; i<b->_num_succs; i++ ) { 1.560 + Block *bs = b->_succs[i]; 1.561 + // Find index of 'b' in 'bs' predecessors 1.562 + uint j=1; 1.563 + while( _phc._cfg._bbs[bs->pred(j)->_idx] != b ) j++; 1.564 + // Visit all the Phis in successor block 1.565 + for( uint k = 1; k<bs->_nodes.size(); k++ ) { 1.566 + Node *n = bs->_nodes[k]; 1.567 + if( !n->is_Phi() ) break; 1.568 + combine_these_two( n, n->in(j) ); 1.569 + } 1.570 + } // End of for all successor blocks 1.571 + 1.572 + 1.573 + // Check _this_ block for 2-address instructions and copies. 1.574 + uint cnt = b->end_idx(); 1.575 + for( i = 1; i<cnt; i++ ) { 1.576 + Node *n = b->_nodes[i]; 1.577 + uint idx; 1.578 + // 2-address instructions have a virtual Copy matching their input 1.579 + // to their output 1.580 + if( n->is_Mach() && (idx = n->as_Mach()->two_adr()) ) { 1.581 + MachNode *mach = n->as_Mach(); 1.582 + combine_these_two( mach, mach->in(idx) ); 1.583 + } 1.584 + } // End of for all instructions in block 1.585 +} 1.586 + 1.587 +//============================================================================= 1.588 +//------------------------------PhaseConservativeCoalesce---------------------- 1.589 +PhaseConservativeCoalesce::PhaseConservativeCoalesce( PhaseChaitin &chaitin ) : PhaseCoalesce(chaitin) { 1.590 + _ulr.initialize(_phc._maxlrg); 1.591 +} 1.592 + 1.593 +//------------------------------verify----------------------------------------- 1.594 +void PhaseConservativeCoalesce::verify() { 1.595 +#ifdef ASSERT 1.596 + _phc.set_was_low(); 1.597 +#endif 1.598 +} 1.599 + 1.600 +//------------------------------union_helper----------------------------------- 1.601 +void PhaseConservativeCoalesce::union_helper( Node *lr1_node, Node *lr2_node, uint lr1, uint lr2, Node *src_def, Node *dst_copy, Node *src_copy, Block *b, uint bindex ) { 1.602 + // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the 1.603 + // union-find tree 1.604 + _phc.Union( lr1_node, lr2_node ); 1.605 + 1.606 + // Single-def live range ONLY if both live ranges are single-def. 1.607 + // If both are single def, then src_def powers one live range 1.608 + // and def_copy powers the other. After merging, src_def powers 1.609 + // the combined live range. 1.610 + lrgs(lr1)._def = (lrgs(lr1)._def == NodeSentinel || 1.611 + lrgs(lr2)._def == NodeSentinel ) 1.612 + ? NodeSentinel : src_def; 1.613 + lrgs(lr2)._def = NULL; // No def for lrg 2 1.614 + lrgs(lr2).Clear(); // Force empty mask for LRG 2 1.615 + //lrgs(lr2)._size = 0; // Live-range 2 goes dead 1.616 + lrgs(lr1)._is_oop |= lrgs(lr2)._is_oop; 1.617 + lrgs(lr2)._is_oop = 0; // In particular, not an oop for GC info 1.618 + 1.619 + if (lrgs(lr1)._maxfreq < lrgs(lr2)._maxfreq) 1.620 + lrgs(lr1)._maxfreq = lrgs(lr2)._maxfreq; 1.621 + 1.622 + // Copy original value instead. Intermediate copies go dead, and 1.623 + // the dst_copy becomes useless. 1.624 + int didx = dst_copy->is_Copy(); 1.625 + dst_copy->set_req( didx, src_def ); 1.626 + // Add copy to free list 1.627 + // _phc.free_spillcopy(b->_nodes[bindex]); 1.628 + assert( b->_nodes[bindex] == dst_copy, "" ); 1.629 + dst_copy->replace_by( dst_copy->in(didx) ); 1.630 + dst_copy->set_req( didx, NULL); 1.631 + b->_nodes.remove(bindex); 1.632 + if( bindex < b->_ihrp_index ) b->_ihrp_index--; 1.633 + if( bindex < b->_fhrp_index ) b->_fhrp_index--; 1.634 + 1.635 + // Stretched lr1; add it to liveness of intermediate blocks 1.636 + Block *b2 = _phc._cfg._bbs[src_copy->_idx]; 1.637 + while( b != b2 ) { 1.638 + b = _phc._cfg._bbs[b->pred(1)->_idx]; 1.639 + _phc._live->live(b)->insert(lr1); 1.640 + } 1.641 +} 1.642 + 1.643 +//------------------------------compute_separating_interferences--------------- 1.644 +// Factored code from copy_copy that computes extra interferences from 1.645 +// lengthening a live range by double-coalescing. 1.646 +uint PhaseConservativeCoalesce::compute_separating_interferences(Node *dst_copy, Node *src_copy, Block *b, uint bindex, RegMask &rm, uint reg_degree, uint rm_size, uint lr1, uint lr2 ) { 1.647 + 1.648 + assert(!lrgs(lr1)._fat_proj, "cannot coalesce fat_proj"); 1.649 + assert(!lrgs(lr2)._fat_proj, "cannot coalesce fat_proj"); 1.650 + Node *prev_copy = dst_copy->in(dst_copy->is_Copy()); 1.651 + Block *b2 = b; 1.652 + uint bindex2 = bindex; 1.653 + while( 1 ) { 1.654 + // Find previous instruction 1.655 + bindex2--; // Chain backwards 1 instruction 1.656 + while( bindex2 == 0 ) { // At block start, find prior block 1.657 + assert( b2->num_preds() == 2, "cannot double coalesce across c-flow" ); 1.658 + b2 = _phc._cfg._bbs[b2->pred(1)->_idx]; 1.659 + bindex2 = b2->end_idx()-1; 1.660 + } 1.661 + // Get prior instruction 1.662 + assert(bindex2 < b2->_nodes.size(), "index out of bounds"); 1.663 + Node *x = b2->_nodes[bindex2]; 1.664 + if( x == prev_copy ) { // Previous copy in copy chain? 1.665 + if( prev_copy == src_copy)// Found end of chain and all interferences 1.666 + break; // So break out of loop 1.667 + // Else work back one in copy chain 1.668 + prev_copy = prev_copy->in(prev_copy->is_Copy()); 1.669 + } else { // Else collect interferences 1.670 + uint lidx = _phc.Find(x); 1.671 + // Found another def of live-range being stretched? 1.672 + if( lidx == lr1 ) return max_juint; 1.673 + if( lidx == lr2 ) return max_juint; 1.674 + 1.675 + // If we attempt to coalesce across a bound def 1.676 + if( lrgs(lidx).is_bound() ) { 1.677 + // Do not let the coalesced LRG expect to get the bound color 1.678 + rm.SUBTRACT( lrgs(lidx).mask() ); 1.679 + // Recompute rm_size 1.680 + rm_size = rm.Size(); 1.681 + //if( rm._flags ) rm_size += 1000000; 1.682 + if( reg_degree >= rm_size ) return max_juint; 1.683 + } 1.684 + if( rm.overlap(lrgs(lidx).mask()) ) { 1.685 + // Insert lidx into union LRG; returns TRUE if actually inserted 1.686 + if( _ulr.insert(lidx) ) { 1.687 + // Infinite-stack neighbors do not alter colorability, as they 1.688 + // can always color to some other color. 1.689 + if( !lrgs(lidx).mask().is_AllStack() ) { 1.690 + // If this coalesce will make any new neighbor uncolorable, 1.691 + // do not coalesce. 1.692 + if( lrgs(lidx).just_lo_degree() ) 1.693 + return max_juint; 1.694 + // Bump our degree 1.695 + if( ++reg_degree >= rm_size ) 1.696 + return max_juint; 1.697 + } // End of if not infinite-stack neighbor 1.698 + } // End of if actually inserted 1.699 + } // End of if live range overlaps 1.700 + } // End of else collect intereferences for 1 node 1.701 + } // End of while forever, scan back for intereferences 1.702 + return reg_degree; 1.703 +} 1.704 + 1.705 +//------------------------------update_ifg------------------------------------- 1.706 +void PhaseConservativeCoalesce::update_ifg(uint lr1, uint lr2, IndexSet *n_lr1, IndexSet *n_lr2) { 1.707 + // Some original neighbors of lr1 might have gone away 1.708 + // because the constrained register mask prevented them. 1.709 + // Remove lr1 from such neighbors. 1.710 + IndexSetIterator one(n_lr1); 1.711 + uint neighbor; 1.712 + LRG &lrg1 = lrgs(lr1); 1.713 + while ((neighbor = one.next()) != 0) 1.714 + if( !_ulr.member(neighbor) ) 1.715 + if( _phc._ifg->neighbors(neighbor)->remove(lr1) ) 1.716 + lrgs(neighbor).inc_degree( -lrg1.compute_degree(lrgs(neighbor)) ); 1.717 + 1.718 + 1.719 + // lr2 is now called (coalesced into) lr1. 1.720 + // Remove lr2 from the IFG. 1.721 + IndexSetIterator two(n_lr2); 1.722 + LRG &lrg2 = lrgs(lr2); 1.723 + while ((neighbor = two.next()) != 0) 1.724 + if( _phc._ifg->neighbors(neighbor)->remove(lr2) ) 1.725 + lrgs(neighbor).inc_degree( -lrg2.compute_degree(lrgs(neighbor)) ); 1.726 + 1.727 + // Some neighbors of intermediate copies now interfere with the 1.728 + // combined live range. 1.729 + IndexSetIterator three(&_ulr); 1.730 + while ((neighbor = three.next()) != 0) 1.731 + if( _phc._ifg->neighbors(neighbor)->insert(lr1) ) 1.732 + lrgs(neighbor).inc_degree( lrg1.compute_degree(lrgs(neighbor)) ); 1.733 +} 1.734 + 1.735 +//------------------------------record_bias------------------------------------ 1.736 +static void record_bias( const PhaseIFG *ifg, int lr1, int lr2 ) { 1.737 + // Tag copy bias here 1.738 + if( !ifg->lrgs(lr1)._copy_bias ) 1.739 + ifg->lrgs(lr1)._copy_bias = lr2; 1.740 + if( !ifg->lrgs(lr2)._copy_bias ) 1.741 + ifg->lrgs(lr2)._copy_bias = lr1; 1.742 +} 1.743 + 1.744 +//------------------------------copy_copy-------------------------------------- 1.745 +// See if I can coalesce a series of multiple copies together. I need the 1.746 +// final dest copy and the original src copy. They can be the same Node. 1.747 +// Compute the compatible register masks. 1.748 +bool PhaseConservativeCoalesce::copy_copy( Node *dst_copy, Node *src_copy, Block *b, uint bindex ) { 1.749 + 1.750 + if( !dst_copy->is_SpillCopy() ) return false; 1.751 + if( !src_copy->is_SpillCopy() ) return false; 1.752 + Node *src_def = src_copy->in(src_copy->is_Copy()); 1.753 + uint lr1 = _phc.Find(dst_copy); 1.754 + uint lr2 = _phc.Find(src_def ); 1.755 + 1.756 + // Same live ranges already? 1.757 + if( lr1 == lr2 ) return false; 1.758 + 1.759 + // Interfere? 1.760 + if( _phc._ifg->test_edge_sq( lr1, lr2 ) ) return false; 1.761 + 1.762 + // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK. 1.763 + if( !lrgs(lr1)._is_oop && lrgs(lr2)._is_oop ) // not an oop->int cast 1.764 + return false; 1.765 + 1.766 + // Coalescing between an aligned live range and a mis-aligned live range? 1.767 + // No, no! Alignment changes how we count degree. 1.768 + if( lrgs(lr1)._fat_proj != lrgs(lr2)._fat_proj ) 1.769 + return false; 1.770 + 1.771 + // Sort; use smaller live-range number 1.772 + Node *lr1_node = dst_copy; 1.773 + Node *lr2_node = src_def; 1.774 + if( lr1 > lr2 ) { 1.775 + uint tmp = lr1; lr1 = lr2; lr2 = tmp; 1.776 + lr1_node = src_def; lr2_node = dst_copy; 1.777 + } 1.778 + 1.779 + // Check for compatibility of the 2 live ranges by 1.780 + // intersecting their allowed register sets. 1.781 + RegMask rm = lrgs(lr1).mask(); 1.782 + rm.AND(lrgs(lr2).mask()); 1.783 + // Number of bits free 1.784 + uint rm_size = rm.Size(); 1.785 + 1.786 + // If we can use any stack slot, then effective size is infinite 1.787 + if( rm.is_AllStack() ) rm_size += 1000000; 1.788 + // Incompatible masks, no way to coalesce 1.789 + if( rm_size == 0 ) return false; 1.790 + 1.791 + // Another early bail-out test is when we are double-coalescing and the 1.792 + // 2 copies are seperated by some control flow. 1.793 + if( dst_copy != src_copy ) { 1.794 + Block *src_b = _phc._cfg._bbs[src_copy->_idx]; 1.795 + Block *b2 = b; 1.796 + while( b2 != src_b ) { 1.797 + if( b2->num_preds() > 2 ){// Found merge-point 1.798 + _phc._lost_opp_cflow_coalesce++; 1.799 + // extra record_bias commented out because Chris believes it is not 1.800 + // productive. Since we can record only 1 bias, we want to choose one 1.801 + // that stands a chance of working and this one probably does not. 1.802 + //record_bias( _phc._lrgs, lr1, lr2 ); 1.803 + return false; // To hard to find all interferences 1.804 + } 1.805 + b2 = _phc._cfg._bbs[b2->pred(1)->_idx]; 1.806 + } 1.807 + } 1.808 + 1.809 + // Union the two interference sets together into '_ulr' 1.810 + uint reg_degree = _ulr.lrg_union( lr1, lr2, rm_size, _phc._ifg, rm ); 1.811 + 1.812 + if( reg_degree >= rm_size ) { 1.813 + record_bias( _phc._ifg, lr1, lr2 ); 1.814 + return false; 1.815 + } 1.816 + 1.817 + // Now I need to compute all the interferences between dst_copy and 1.818 + // src_copy. I'm not willing visit the entire interference graph, so 1.819 + // I limit my search to things in dst_copy's block or in a straight 1.820 + // line of previous blocks. I give up at merge points or when I get 1.821 + // more interferences than my degree. I can stop when I find src_copy. 1.822 + if( dst_copy != src_copy ) { 1.823 + reg_degree = compute_separating_interferences(dst_copy, src_copy, b, bindex, rm, rm_size, reg_degree, lr1, lr2 ); 1.824 + if( reg_degree == max_juint ) { 1.825 + record_bias( _phc._ifg, lr1, lr2 ); 1.826 + return false; 1.827 + } 1.828 + } // End of if dst_copy & src_copy are different 1.829 + 1.830 + 1.831 + // ---- THE COMBINED LRG IS COLORABLE ---- 1.832 + 1.833 + // YEAH - Now coalesce this copy away 1.834 + assert( lrgs(lr1).num_regs() == lrgs(lr2).num_regs(), "" ); 1.835 + 1.836 + IndexSet *n_lr1 = _phc._ifg->neighbors(lr1); 1.837 + IndexSet *n_lr2 = _phc._ifg->neighbors(lr2); 1.838 + 1.839 + // Update the interference graph 1.840 + update_ifg(lr1, lr2, n_lr1, n_lr2); 1.841 + 1.842 + _ulr.remove(lr1); 1.843 + 1.844 + // Uncomment the following code to trace Coalescing in great detail. 1.845 + // 1.846 + //if (false) { 1.847 + // tty->cr(); 1.848 + // tty->print_cr("#######################################"); 1.849 + // tty->print_cr("union %d and %d", lr1, lr2); 1.850 + // n_lr1->dump(); 1.851 + // n_lr2->dump(); 1.852 + // tty->print_cr("resulting set is"); 1.853 + // _ulr.dump(); 1.854 + //} 1.855 + 1.856 + // Replace n_lr1 with the new combined live range. _ulr will use 1.857 + // n_lr1's old memory on the next iteration. n_lr2 is cleared to 1.858 + // send its internal memory to the free list. 1.859 + _ulr.swap(n_lr1); 1.860 + _ulr.clear(); 1.861 + n_lr2->clear(); 1.862 + 1.863 + lrgs(lr1).set_degree( _phc._ifg->effective_degree(lr1) ); 1.864 + lrgs(lr2).set_degree( 0 ); 1.865 + 1.866 + // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the 1.867 + // union-find tree 1.868 + union_helper( lr1_node, lr2_node, lr1, lr2, src_def, dst_copy, src_copy, b, bindex ); 1.869 + // Combine register restrictions 1.870 + lrgs(lr1).set_mask(rm); 1.871 + lrgs(lr1).compute_set_mask_size(); 1.872 + lrgs(lr1)._cost += lrgs(lr2)._cost; 1.873 + lrgs(lr1)._area += lrgs(lr2)._area; 1.874 + 1.875 + // While its uncommon to successfully coalesce live ranges that started out 1.876 + // being not-lo-degree, it can happen. In any case the combined coalesced 1.877 + // live range better Simplify nicely. 1.878 + lrgs(lr1)._was_lo = 1; 1.879 + 1.880 + // kinda expensive to do all the time 1.881 + //tty->print_cr("warning: slow verify happening"); 1.882 + //_phc._ifg->verify( &_phc ); 1.883 + return true; 1.884 +} 1.885 + 1.886 +//------------------------------coalesce--------------------------------------- 1.887 +// Conservative (but pessimistic) copy coalescing of a single block 1.888 +void PhaseConservativeCoalesce::coalesce( Block *b ) { 1.889 + // Bail out on infrequent blocks 1.890 + if( b->is_uncommon(_phc._cfg._bbs) ) 1.891 + return; 1.892 + // Check this block for copies. 1.893 + for( uint i = 1; i<b->end_idx(); i++ ) { 1.894 + // Check for actual copies on inputs. Coalesce a copy into its 1.895 + // input if use and copy's input are compatible. 1.896 + Node *copy1 = b->_nodes[i]; 1.897 + uint idx1 = copy1->is_Copy(); 1.898 + if( !idx1 ) continue; // Not a copy 1.899 + 1.900 + if( copy_copy(copy1,copy1,b,i) ) { 1.901 + i--; // Retry, same location in block 1.902 + PhaseChaitin::_conserv_coalesce++; // Collect stats on success 1.903 + continue; 1.904 + } 1.905 + 1.906 + /* do not attempt pairs. About 1/2 of all pairs can be removed by 1.907 + post-alloc. The other set are too few to bother. 1.908 + Node *copy2 = copy1->in(idx1); 1.909 + uint idx2 = copy2->is_Copy(); 1.910 + if( !idx2 ) continue; 1.911 + if( copy_copy(copy1,copy2,b,i) ) { 1.912 + i--; // Retry, same location in block 1.913 + PhaseChaitin::_conserv_coalesce_pair++; // Collect stats on success 1.914 + continue; 1.915 + } 1.916 + */ 1.917 + } 1.918 +}