Tue, 04 Oct 2011 14:30:04 -0700
6865265: JVM crashes with "missing exception handler" error
Summary: Retry the call to fast_exception_handler_bci_for() after it returned with a pending exception. Don't cache the exception handler pc computed by compute_compiled_exc_handler() if the handler is for another (nested) exception.
Reviewed-by: kamg, kvn
Contributed-by: volker.simonis@gmail.com
duke@435 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 27 | #include "opto/block.hpp" |
stefank@2314 | 28 | #include "opto/cfgnode.hpp" |
stefank@2314 | 29 | #include "opto/chaitin.hpp" |
stefank@2314 | 30 | #include "opto/coalesce.hpp" |
stefank@2314 | 31 | #include "opto/connode.hpp" |
stefank@2314 | 32 | #include "opto/indexSet.hpp" |
stefank@2314 | 33 | #include "opto/machnode.hpp" |
stefank@2314 | 34 | #include "opto/matcher.hpp" |
stefank@2314 | 35 | #include "opto/regmask.hpp" |
duke@435 | 36 | |
duke@435 | 37 | //============================================================================= |
duke@435 | 38 | //------------------------------reset_uf_map----------------------------------- |
duke@435 | 39 | void PhaseChaitin::reset_uf_map( uint maxlrg ) { |
duke@435 | 40 | _maxlrg = maxlrg; |
duke@435 | 41 | // Force the Union-Find mapping to be at least this large |
duke@435 | 42 | _uf_map.extend(_maxlrg,0); |
duke@435 | 43 | // Initialize it to be the ID mapping. |
duke@435 | 44 | for( uint i=0; i<_maxlrg; i++ ) |
duke@435 | 45 | _uf_map.map(i,i); |
duke@435 | 46 | } |
duke@435 | 47 | |
duke@435 | 48 | //------------------------------compress_uf_map-------------------------------- |
duke@435 | 49 | // Make all Nodes map directly to their final live range; no need for |
duke@435 | 50 | // the Union-Find mapping after this call. |
duke@435 | 51 | void PhaseChaitin::compress_uf_map_for_nodes( ) { |
duke@435 | 52 | // For all Nodes, compress mapping |
duke@435 | 53 | uint unique = _names.Size(); |
duke@435 | 54 | for( uint i=0; i<unique; i++ ) { |
duke@435 | 55 | uint lrg = _names[i]; |
duke@435 | 56 | uint compressed_lrg = Find(lrg); |
duke@435 | 57 | if( lrg != compressed_lrg ) |
duke@435 | 58 | _names.map(i,compressed_lrg); |
duke@435 | 59 | } |
duke@435 | 60 | } |
duke@435 | 61 | |
duke@435 | 62 | //------------------------------Find------------------------------------------- |
duke@435 | 63 | // Straight out of Tarjan's union-find algorithm |
duke@435 | 64 | uint PhaseChaitin::Find_compress( uint lrg ) { |
duke@435 | 65 | uint cur = lrg; |
duke@435 | 66 | uint next = _uf_map[cur]; |
duke@435 | 67 | while( next != cur ) { // Scan chain of equivalences |
duke@435 | 68 | assert( next < cur, "always union smaller" ); |
duke@435 | 69 | cur = next; // until find a fixed-point |
duke@435 | 70 | next = _uf_map[cur]; |
duke@435 | 71 | } |
duke@435 | 72 | // Core of union-find algorithm: update chain of |
duke@435 | 73 | // equivalences to be equal to the root. |
duke@435 | 74 | while( lrg != next ) { |
duke@435 | 75 | uint tmp = _uf_map[lrg]; |
duke@435 | 76 | _uf_map.map(lrg, next); |
duke@435 | 77 | lrg = tmp; |
duke@435 | 78 | } |
duke@435 | 79 | return lrg; |
duke@435 | 80 | } |
duke@435 | 81 | |
duke@435 | 82 | //------------------------------Find------------------------------------------- |
duke@435 | 83 | // Straight out of Tarjan's union-find algorithm |
duke@435 | 84 | uint PhaseChaitin::Find_compress( const Node *n ) { |
duke@435 | 85 | uint lrg = Find_compress(_names[n->_idx]); |
duke@435 | 86 | _names.map(n->_idx,lrg); |
duke@435 | 87 | return lrg; |
duke@435 | 88 | } |
duke@435 | 89 | |
duke@435 | 90 | //------------------------------Find_const------------------------------------- |
duke@435 | 91 | // Like Find above, but no path compress, so bad asymptotic behavior |
duke@435 | 92 | uint PhaseChaitin::Find_const( uint lrg ) const { |
duke@435 | 93 | if( !lrg ) return lrg; // Ignore the zero LRG |
duke@435 | 94 | // Off the end? This happens during debugging dumps when you got |
duke@435 | 95 | // brand new live ranges but have not told the allocator yet. |
duke@435 | 96 | if( lrg >= _maxlrg ) return lrg; |
duke@435 | 97 | uint next = _uf_map[lrg]; |
duke@435 | 98 | while( next != lrg ) { // Scan chain of equivalences |
duke@435 | 99 | assert( next < lrg, "always union smaller" ); |
duke@435 | 100 | lrg = next; // until find a fixed-point |
duke@435 | 101 | next = _uf_map[lrg]; |
duke@435 | 102 | } |
duke@435 | 103 | return next; |
duke@435 | 104 | } |
duke@435 | 105 | |
duke@435 | 106 | //------------------------------Find------------------------------------------- |
duke@435 | 107 | // Like Find above, but no path compress, so bad asymptotic behavior |
duke@435 | 108 | uint PhaseChaitin::Find_const( const Node *n ) const { |
duke@435 | 109 | if( n->_idx >= _names.Size() ) return 0; // not mapped, usual for debug dump |
duke@435 | 110 | return Find_const( _names[n->_idx] ); |
duke@435 | 111 | } |
duke@435 | 112 | |
duke@435 | 113 | //------------------------------Union------------------------------------------ |
duke@435 | 114 | // union 2 sets together. |
duke@435 | 115 | void PhaseChaitin::Union( const Node *src_n, const Node *dst_n ) { |
duke@435 | 116 | uint src = Find(src_n); |
duke@435 | 117 | uint dst = Find(dst_n); |
duke@435 | 118 | assert( src, "" ); |
duke@435 | 119 | assert( dst, "" ); |
duke@435 | 120 | assert( src < _maxlrg, "oob" ); |
duke@435 | 121 | assert( dst < _maxlrg, "oob" ); |
duke@435 | 122 | assert( src < dst, "always union smaller" ); |
duke@435 | 123 | _uf_map.map(dst,src); |
duke@435 | 124 | } |
duke@435 | 125 | |
duke@435 | 126 | //------------------------------new_lrg---------------------------------------- |
duke@435 | 127 | void PhaseChaitin::new_lrg( const Node *x, uint lrg ) { |
duke@435 | 128 | // Make the Node->LRG mapping |
duke@435 | 129 | _names.extend(x->_idx,lrg); |
duke@435 | 130 | // Make the Union-Find mapping an identity function |
duke@435 | 131 | _uf_map.extend(lrg,lrg); |
duke@435 | 132 | } |
duke@435 | 133 | |
duke@435 | 134 | //------------------------------clone_projs------------------------------------ |
twisti@1040 | 135 | // After cloning some rematerialized instruction, clone any MachProj's that |
duke@435 | 136 | // follow it. Example: Intel zero is XOR, kills flags. Sparc FP constants |
duke@435 | 137 | // use G3 as an address temp. |
duke@435 | 138 | int PhaseChaitin::clone_projs( Block *b, uint idx, Node *con, Node *copy, uint &maxlrg ) { |
duke@435 | 139 | Block *bcon = _cfg._bbs[con->_idx]; |
duke@435 | 140 | uint cindex = bcon->find_node(con); |
duke@435 | 141 | Node *con_next = bcon->_nodes[cindex+1]; |
kvn@3040 | 142 | if( con_next->in(0) != con || !con_next->is_MachProj() ) |
duke@435 | 143 | return false; // No MachProj's follow |
duke@435 | 144 | |
duke@435 | 145 | // Copy kills after the cloned constant |
duke@435 | 146 | Node *kills = con_next->clone(); |
duke@435 | 147 | kills->set_req( 0, copy ); |
duke@435 | 148 | b->_nodes.insert( idx, kills ); |
duke@435 | 149 | _cfg._bbs.map( kills->_idx, b ); |
duke@435 | 150 | new_lrg( kills, maxlrg++ ); |
duke@435 | 151 | return true; |
duke@435 | 152 | } |
duke@435 | 153 | |
duke@435 | 154 | //------------------------------compact---------------------------------------- |
duke@435 | 155 | // Renumber the live ranges to compact them. Makes the IFG smaller. |
duke@435 | 156 | void PhaseChaitin::compact() { |
duke@435 | 157 | // Current the _uf_map contains a series of short chains which are headed |
duke@435 | 158 | // by a self-cycle. All the chains run from big numbers to little numbers. |
duke@435 | 159 | // The Find() call chases the chains & shortens them for the next Find call. |
duke@435 | 160 | // We are going to change this structure slightly. Numbers above a moving |
duke@435 | 161 | // wave 'i' are unchanged. Numbers below 'j' point directly to their |
duke@435 | 162 | // compacted live range with no further chaining. There are no chains or |
duke@435 | 163 | // cycles below 'i', so the Find call no longer works. |
duke@435 | 164 | uint j=1; |
duke@435 | 165 | uint i; |
duke@435 | 166 | for( i=1; i < _maxlrg; i++ ) { |
duke@435 | 167 | uint lr = _uf_map[i]; |
duke@435 | 168 | // Ignore unallocated live ranges |
duke@435 | 169 | if( !lr ) continue; |
duke@435 | 170 | assert( lr <= i, "" ); |
duke@435 | 171 | _uf_map.map(i, ( lr == i ) ? j++ : _uf_map[lr]); |
duke@435 | 172 | } |
duke@435 | 173 | if( false ) // PrintOptoCompactLiveRanges |
duke@435 | 174 | printf("Compacted %d LRs from %d\n",i-j,i); |
duke@435 | 175 | // Now change the Node->LR mapping to reflect the compacted names |
duke@435 | 176 | uint unique = _names.Size(); |
duke@435 | 177 | for( i=0; i<unique; i++ ) |
duke@435 | 178 | _names.map(i,_uf_map[_names[i]]); |
duke@435 | 179 | |
duke@435 | 180 | // Reset the Union-Find mapping |
duke@435 | 181 | reset_uf_map(j); |
duke@435 | 182 | |
duke@435 | 183 | } |
duke@435 | 184 | |
duke@435 | 185 | //============================================================================= |
duke@435 | 186 | //------------------------------Dump------------------------------------------- |
duke@435 | 187 | #ifndef PRODUCT |
duke@435 | 188 | void PhaseCoalesce::dump( Node *n ) const { |
duke@435 | 189 | // Being a const function means I cannot use 'Find' |
duke@435 | 190 | uint r = _phc.Find(n); |
duke@435 | 191 | tty->print("L%d/N%d ",r,n->_idx); |
duke@435 | 192 | } |
duke@435 | 193 | |
duke@435 | 194 | //------------------------------dump------------------------------------------- |
duke@435 | 195 | void PhaseCoalesce::dump() const { |
duke@435 | 196 | // I know I have a block layout now, so I can print blocks in a loop |
duke@435 | 197 | for( uint i=0; i<_phc._cfg._num_blocks; i++ ) { |
duke@435 | 198 | uint j; |
duke@435 | 199 | Block *b = _phc._cfg._blocks[i]; |
duke@435 | 200 | // Print a nice block header |
duke@435 | 201 | tty->print("B%d: ",b->_pre_order); |
duke@435 | 202 | for( j=1; j<b->num_preds(); j++ ) |
duke@435 | 203 | tty->print("B%d ", _phc._cfg._bbs[b->pred(j)->_idx]->_pre_order); |
duke@435 | 204 | tty->print("-> "); |
duke@435 | 205 | for( j=0; j<b->_num_succs; j++ ) |
duke@435 | 206 | tty->print("B%d ",b->_succs[j]->_pre_order); |
duke@435 | 207 | tty->print(" IDom: B%d/#%d\n", b->_idom ? b->_idom->_pre_order : 0, b->_dom_depth); |
duke@435 | 208 | uint cnt = b->_nodes.size(); |
duke@435 | 209 | for( j=0; j<cnt; j++ ) { |
duke@435 | 210 | Node *n = b->_nodes[j]; |
duke@435 | 211 | dump( n ); |
duke@435 | 212 | tty->print("\t%s\t",n->Name()); |
duke@435 | 213 | |
duke@435 | 214 | // Dump the inputs |
duke@435 | 215 | uint k; // Exit value of loop |
duke@435 | 216 | for( k=0; k<n->req(); k++ ) // For all required inputs |
duke@435 | 217 | if( n->in(k) ) dump( n->in(k) ); |
duke@435 | 218 | else tty->print("_ "); |
duke@435 | 219 | int any_prec = 0; |
duke@435 | 220 | for( ; k<n->len(); k++ ) // For all precedence inputs |
duke@435 | 221 | if( n->in(k) ) { |
duke@435 | 222 | if( !any_prec++ ) tty->print(" |"); |
duke@435 | 223 | dump( n->in(k) ); |
duke@435 | 224 | } |
duke@435 | 225 | |
duke@435 | 226 | // Dump node-specific info |
duke@435 | 227 | n->dump_spec(tty); |
duke@435 | 228 | tty->print("\n"); |
duke@435 | 229 | |
duke@435 | 230 | } |
duke@435 | 231 | tty->print("\n"); |
duke@435 | 232 | } |
duke@435 | 233 | } |
duke@435 | 234 | #endif |
duke@435 | 235 | |
duke@435 | 236 | //------------------------------combine_these_two------------------------------ |
duke@435 | 237 | // Combine the live ranges def'd by these 2 Nodes. N2 is an input to N1. |
duke@435 | 238 | void PhaseCoalesce::combine_these_two( Node *n1, Node *n2 ) { |
duke@435 | 239 | uint lr1 = _phc.Find(n1); |
duke@435 | 240 | uint lr2 = _phc.Find(n2); |
duke@435 | 241 | if( lr1 != lr2 && // Different live ranges already AND |
duke@435 | 242 | !_phc._ifg->test_edge_sq( lr1, lr2 ) ) { // Do not interfere |
duke@435 | 243 | LRG *lrg1 = &_phc.lrgs(lr1); |
duke@435 | 244 | LRG *lrg2 = &_phc.lrgs(lr2); |
duke@435 | 245 | // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK. |
duke@435 | 246 | |
duke@435 | 247 | // Now, why is int->oop OK? We end up declaring a raw-pointer as an oop |
duke@435 | 248 | // and in general that's a bad thing. However, int->oop conversions only |
duke@435 | 249 | // happen at GC points, so the lifetime of the misclassified raw-pointer |
duke@435 | 250 | // is from the CheckCastPP (that converts it to an oop) backwards up |
duke@435 | 251 | // through a merge point and into the slow-path call, and around the |
duke@435 | 252 | // diamond up to the heap-top check and back down into the slow-path call. |
duke@435 | 253 | // The misclassified raw pointer is NOT live across the slow-path call, |
duke@435 | 254 | // and so does not appear in any GC info, so the fact that it is |
duke@435 | 255 | // misclassified is OK. |
duke@435 | 256 | |
duke@435 | 257 | if( (lrg1->_is_oop || !lrg2->_is_oop) && // not an oop->int cast AND |
duke@435 | 258 | // Compatible final mask |
duke@435 | 259 | lrg1->mask().overlap( lrg2->mask() ) ) { |
duke@435 | 260 | // Merge larger into smaller. |
duke@435 | 261 | if( lr1 > lr2 ) { |
duke@435 | 262 | uint tmp = lr1; lr1 = lr2; lr2 = tmp; |
duke@435 | 263 | Node *n = n1; n1 = n2; n2 = n; |
duke@435 | 264 | LRG *ltmp = lrg1; lrg1 = lrg2; lrg2 = ltmp; |
duke@435 | 265 | } |
duke@435 | 266 | // Union lr2 into lr1 |
duke@435 | 267 | _phc.Union( n1, n2 ); |
duke@435 | 268 | if (lrg1->_maxfreq < lrg2->_maxfreq) |
duke@435 | 269 | lrg1->_maxfreq = lrg2->_maxfreq; |
duke@435 | 270 | // Merge in the IFG |
duke@435 | 271 | _phc._ifg->Union( lr1, lr2 ); |
duke@435 | 272 | // Combine register restrictions |
duke@435 | 273 | lrg1->AND(lrg2->mask()); |
duke@435 | 274 | } |
duke@435 | 275 | } |
duke@435 | 276 | } |
duke@435 | 277 | |
duke@435 | 278 | //------------------------------coalesce_driver-------------------------------- |
duke@435 | 279 | // Copy coalescing |
duke@435 | 280 | void PhaseCoalesce::coalesce_driver( ) { |
duke@435 | 281 | |
duke@435 | 282 | verify(); |
duke@435 | 283 | // Coalesce from high frequency to low |
duke@435 | 284 | for( uint i=0; i<_phc._cfg._num_blocks; i++ ) |
duke@435 | 285 | coalesce( _phc._blks[i] ); |
duke@435 | 286 | |
duke@435 | 287 | } |
duke@435 | 288 | |
duke@435 | 289 | //------------------------------insert_copy_with_overlap----------------------- |
duke@435 | 290 | // I am inserting copies to come out of SSA form. In the general case, I am |
duke@435 | 291 | // doing a parallel renaming. I'm in the Named world now, so I can't do a |
duke@435 | 292 | // general parallel renaming. All the copies now use "names" (live-ranges) |
duke@435 | 293 | // to carry values instead of the explicit use-def chains. Suppose I need to |
duke@435 | 294 | // insert 2 copies into the same block. They copy L161->L128 and L128->L132. |
duke@435 | 295 | // If I insert them in the wrong order then L128 will get clobbered before it |
duke@435 | 296 | // can get used by the second copy. This cannot happen in the SSA model; |
duke@435 | 297 | // direct use-def chains get me the right value. It DOES happen in the named |
duke@435 | 298 | // model so I have to handle the reordering of copies. |
duke@435 | 299 | // |
duke@435 | 300 | // In general, I need to topo-sort the placed copies to avoid conflicts. |
duke@435 | 301 | // Its possible to have a closed cycle of copies (e.g., recirculating the same |
duke@435 | 302 | // values around a loop). In this case I need a temp to break the cycle. |
duke@435 | 303 | void PhaseAggressiveCoalesce::insert_copy_with_overlap( Block *b, Node *copy, uint dst_name, uint src_name ) { |
duke@435 | 304 | |
duke@435 | 305 | // Scan backwards for the locations of the last use of the dst_name. |
duke@435 | 306 | // I am about to clobber the dst_name, so the copy must be inserted |
duke@435 | 307 | // after the last use. Last use is really first-use on a backwards scan. |
duke@435 | 308 | uint i = b->end_idx()-1; |
duke@435 | 309 | while( 1 ) { |
duke@435 | 310 | Node *n = b->_nodes[i]; |
duke@435 | 311 | // Check for end of virtual copies; this is also the end of the |
duke@435 | 312 | // parallel renaming effort. |
duke@435 | 313 | if( n->_idx < _unique ) break; |
duke@435 | 314 | uint idx = n->is_Copy(); |
kvn@3040 | 315 | assert( idx || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" ); |
duke@435 | 316 | if( idx && _phc.Find(n->in(idx)) == dst_name ) break; |
duke@435 | 317 | i--; |
duke@435 | 318 | } |
duke@435 | 319 | uint last_use_idx = i; |
duke@435 | 320 | |
duke@435 | 321 | // Also search for any kill of src_name that exits the block. |
duke@435 | 322 | // Since the copy uses src_name, I have to come before any kill. |
duke@435 | 323 | uint kill_src_idx = b->end_idx(); |
duke@435 | 324 | // There can be only 1 kill that exits any block and that is |
duke@435 | 325 | // the last kill. Thus it is the first kill on a backwards scan. |
duke@435 | 326 | i = b->end_idx()-1; |
duke@435 | 327 | while( 1 ) { |
duke@435 | 328 | Node *n = b->_nodes[i]; |
duke@435 | 329 | // Check for end of virtual copies; this is also the end of the |
duke@435 | 330 | // parallel renaming effort. |
duke@435 | 331 | if( n->_idx < _unique ) break; |
kvn@3040 | 332 | assert( n->is_Copy() || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" ); |
duke@435 | 333 | if( _phc.Find(n) == src_name ) { |
duke@435 | 334 | kill_src_idx = i; |
duke@435 | 335 | break; |
duke@435 | 336 | } |
duke@435 | 337 | i--; |
duke@435 | 338 | } |
duke@435 | 339 | // Need a temp? Last use of dst comes after the kill of src? |
duke@435 | 340 | if( last_use_idx >= kill_src_idx ) { |
duke@435 | 341 | // Need to break a cycle with a temp |
duke@435 | 342 | uint idx = copy->is_Copy(); |
duke@435 | 343 | Node *tmp = copy->clone(); |
duke@435 | 344 | _phc.new_lrg(tmp,_phc._maxlrg++); |
duke@435 | 345 | // Insert new temp between copy and source |
duke@435 | 346 | tmp ->set_req(idx,copy->in(idx)); |
duke@435 | 347 | copy->set_req(idx,tmp); |
duke@435 | 348 | // Save source in temp early, before source is killed |
duke@435 | 349 | b->_nodes.insert(kill_src_idx,tmp); |
duke@435 | 350 | _phc._cfg._bbs.map( tmp->_idx, b ); |
duke@435 | 351 | last_use_idx++; |
duke@435 | 352 | } |
duke@435 | 353 | |
duke@435 | 354 | // Insert just after last use |
duke@435 | 355 | b->_nodes.insert(last_use_idx+1,copy); |
duke@435 | 356 | } |
duke@435 | 357 | |
duke@435 | 358 | //------------------------------insert_copies---------------------------------- |
duke@435 | 359 | void PhaseAggressiveCoalesce::insert_copies( Matcher &matcher ) { |
duke@435 | 360 | // We do LRGs compressing and fix a liveout data only here since the other |
duke@435 | 361 | // place in Split() is guarded by the assert which we never hit. |
duke@435 | 362 | _phc.compress_uf_map_for_nodes(); |
duke@435 | 363 | // Fix block's liveout data for compressed live ranges. |
duke@435 | 364 | for(uint lrg = 1; lrg < _phc._maxlrg; lrg++ ) { |
duke@435 | 365 | uint compressed_lrg = _phc.Find(lrg); |
duke@435 | 366 | if( lrg != compressed_lrg ) { |
duke@435 | 367 | for( uint bidx = 0; bidx < _phc._cfg._num_blocks; bidx++ ) { |
duke@435 | 368 | IndexSet *liveout = _phc._live->live(_phc._cfg._blocks[bidx]); |
duke@435 | 369 | if( liveout->member(lrg) ) { |
duke@435 | 370 | liveout->remove(lrg); |
duke@435 | 371 | liveout->insert(compressed_lrg); |
duke@435 | 372 | } |
duke@435 | 373 | } |
duke@435 | 374 | } |
duke@435 | 375 | } |
duke@435 | 376 | |
duke@435 | 377 | // All new nodes added are actual copies to replace virtual copies. |
duke@435 | 378 | // Nodes with index less than '_unique' are original, non-virtual Nodes. |
duke@435 | 379 | _unique = C->unique(); |
duke@435 | 380 | |
duke@435 | 381 | for( uint i=0; i<_phc._cfg._num_blocks; i++ ) { |
duke@435 | 382 | Block *b = _phc._cfg._blocks[i]; |
duke@435 | 383 | uint cnt = b->num_preds(); // Number of inputs to the Phi |
duke@435 | 384 | |
duke@435 | 385 | for( uint l = 1; l<b->_nodes.size(); l++ ) { |
duke@435 | 386 | Node *n = b->_nodes[l]; |
duke@435 | 387 | |
duke@435 | 388 | // Do not use removed-copies, use copied value instead |
duke@435 | 389 | uint ncnt = n->req(); |
duke@435 | 390 | for( uint k = 1; k<ncnt; k++ ) { |
duke@435 | 391 | Node *copy = n->in(k); |
duke@435 | 392 | uint cidx = copy->is_Copy(); |
duke@435 | 393 | if( cidx ) { |
duke@435 | 394 | Node *def = copy->in(cidx); |
duke@435 | 395 | if( _phc.Find(copy) == _phc.Find(def) ) |
duke@435 | 396 | n->set_req(k,def); |
duke@435 | 397 | } |
duke@435 | 398 | } |
duke@435 | 399 | |
duke@435 | 400 | // Remove any explicit copies that get coalesced. |
duke@435 | 401 | uint cidx = n->is_Copy(); |
duke@435 | 402 | if( cidx ) { |
duke@435 | 403 | Node *def = n->in(cidx); |
duke@435 | 404 | if( _phc.Find(n) == _phc.Find(def) ) { |
duke@435 | 405 | n->replace_by(def); |
duke@435 | 406 | n->set_req(cidx,NULL); |
duke@435 | 407 | b->_nodes.remove(l); |
duke@435 | 408 | l--; |
duke@435 | 409 | continue; |
duke@435 | 410 | } |
duke@435 | 411 | } |
duke@435 | 412 | |
duke@435 | 413 | if( n->is_Phi() ) { |
duke@435 | 414 | // Get the chosen name for the Phi |
duke@435 | 415 | uint phi_name = _phc.Find( n ); |
duke@435 | 416 | // Ignore the pre-allocated specials |
duke@435 | 417 | if( !phi_name ) continue; |
duke@435 | 418 | // Check for mismatch inputs to Phi |
duke@435 | 419 | for( uint j = 1; j<cnt; j++ ) { |
duke@435 | 420 | Node *m = n->in(j); |
duke@435 | 421 | uint src_name = _phc.Find(m); |
duke@435 | 422 | if( src_name != phi_name ) { |
duke@435 | 423 | Block *pred = _phc._cfg._bbs[b->pred(j)->_idx]; |
duke@435 | 424 | Node *copy; |
duke@435 | 425 | assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach"); |
duke@435 | 426 | // Rematerialize constants instead of copying them |
duke@435 | 427 | if( m->is_Mach() && m->as_Mach()->is_Con() && |
duke@435 | 428 | m->as_Mach()->rematerialize() ) { |
duke@435 | 429 | copy = m->clone(); |
duke@435 | 430 | // Insert the copy in the predecessor basic block |
duke@435 | 431 | pred->add_inst(copy); |
duke@435 | 432 | // Copy any flags as well |
duke@435 | 433 | _phc.clone_projs( pred, pred->end_idx(), m, copy, _phc._maxlrg ); |
duke@435 | 434 | } else { |
duke@435 | 435 | const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()]; |
duke@435 | 436 | copy = new (C) MachSpillCopyNode(m,*rm,*rm); |
duke@435 | 437 | // Find a good place to insert. Kinda tricky, use a subroutine |
duke@435 | 438 | insert_copy_with_overlap(pred,copy,phi_name,src_name); |
duke@435 | 439 | } |
duke@435 | 440 | // Insert the copy in the use-def chain |
duke@435 | 441 | n->set_req( j, copy ); |
duke@435 | 442 | _phc._cfg._bbs.map( copy->_idx, pred ); |
duke@435 | 443 | // Extend ("register allocate") the names array for the copy. |
duke@435 | 444 | _phc._names.extend( copy->_idx, phi_name ); |
duke@435 | 445 | } // End of if Phi names do not match |
duke@435 | 446 | } // End of for all inputs to Phi |
duke@435 | 447 | } else { // End of if Phi |
duke@435 | 448 | |
duke@435 | 449 | // Now check for 2-address instructions |
duke@435 | 450 | uint idx; |
duke@435 | 451 | if( n->is_Mach() && (idx=n->as_Mach()->two_adr()) ) { |
duke@435 | 452 | // Get the chosen name for the Node |
duke@435 | 453 | uint name = _phc.Find( n ); |
duke@435 | 454 | assert( name, "no 2-address specials" ); |
duke@435 | 455 | // Check for name mis-match on the 2-address input |
duke@435 | 456 | Node *m = n->in(idx); |
duke@435 | 457 | if( _phc.Find(m) != name ) { |
duke@435 | 458 | Node *copy; |
duke@435 | 459 | assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach"); |
duke@435 | 460 | // At this point it is unsafe to extend live ranges (6550579). |
duke@435 | 461 | // Rematerialize only constants as we do for Phi above. |
duke@435 | 462 | if( m->is_Mach() && m->as_Mach()->is_Con() && |
duke@435 | 463 | m->as_Mach()->rematerialize() ) { |
duke@435 | 464 | copy = m->clone(); |
duke@435 | 465 | // Insert the copy in the basic block, just before us |
duke@435 | 466 | b->_nodes.insert( l++, copy ); |
duke@435 | 467 | if( _phc.clone_projs( b, l, m, copy, _phc._maxlrg ) ) |
duke@435 | 468 | l++; |
duke@435 | 469 | } else { |
duke@435 | 470 | const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()]; |
duke@435 | 471 | copy = new (C) MachSpillCopyNode( m, *rm, *rm ); |
duke@435 | 472 | // Insert the copy in the basic block, just before us |
duke@435 | 473 | b->_nodes.insert( l++, copy ); |
duke@435 | 474 | } |
duke@435 | 475 | // Insert the copy in the use-def chain |
duke@435 | 476 | n->set_req(idx, copy ); |
duke@435 | 477 | // Extend ("register allocate") the names array for the copy. |
duke@435 | 478 | _phc._names.extend( copy->_idx, name ); |
duke@435 | 479 | _phc._cfg._bbs.map( copy->_idx, b ); |
duke@435 | 480 | } |
duke@435 | 481 | |
duke@435 | 482 | } // End of is two-adr |
duke@435 | 483 | |
duke@435 | 484 | // Insert a copy at a debug use for a lrg which has high frequency |
kvn@1108 | 485 | if( b->_freq < OPTO_DEBUG_SPLIT_FREQ || b->is_uncommon(_phc._cfg._bbs) ) { |
duke@435 | 486 | // Walk the debug inputs to the node and check for lrg freq |
duke@435 | 487 | JVMState* jvms = n->jvms(); |
duke@435 | 488 | uint debug_start = jvms ? jvms->debug_start() : 999999; |
duke@435 | 489 | uint debug_end = jvms ? jvms->debug_end() : 999999; |
duke@435 | 490 | for(uint inpidx = debug_start; inpidx < debug_end; inpidx++) { |
duke@435 | 491 | // Do not split monitors; they are only needed for debug table |
duke@435 | 492 | // entries and need no code. |
duke@435 | 493 | if( jvms->is_monitor_use(inpidx) ) continue; |
duke@435 | 494 | Node *inp = n->in(inpidx); |
duke@435 | 495 | uint nidx = _phc.n2lidx(inp); |
duke@435 | 496 | LRG &lrg = lrgs(nidx); |
duke@435 | 497 | |
duke@435 | 498 | // If this lrg has a high frequency use/def |
kvn@1108 | 499 | if( lrg._maxfreq >= _phc.high_frequency_lrg() ) { |
duke@435 | 500 | // If the live range is also live out of this block (like it |
duke@435 | 501 | // would be for a fast/slow idiom), the normal spill mechanism |
duke@435 | 502 | // does an excellent job. If it is not live out of this block |
duke@435 | 503 | // (like it would be for debug info to uncommon trap) splitting |
duke@435 | 504 | // the live range now allows a better allocation in the high |
duke@435 | 505 | // frequency blocks. |
duke@435 | 506 | // Build_IFG_virtual has converted the live sets to |
duke@435 | 507 | // live-IN info, not live-OUT info. |
duke@435 | 508 | uint k; |
duke@435 | 509 | for( k=0; k < b->_num_succs; k++ ) |
duke@435 | 510 | if( _phc._live->live(b->_succs[k])->member( nidx ) ) |
duke@435 | 511 | break; // Live in to some successor block? |
duke@435 | 512 | if( k < b->_num_succs ) |
duke@435 | 513 | continue; // Live out; do not pre-split |
duke@435 | 514 | // Split the lrg at this use |
duke@435 | 515 | const RegMask *rm = C->matcher()->idealreg2spillmask[inp->ideal_reg()]; |
duke@435 | 516 | Node *copy = new (C) MachSpillCopyNode( inp, *rm, *rm ); |
duke@435 | 517 | // Insert the copy in the use-def chain |
duke@435 | 518 | n->set_req(inpidx, copy ); |
duke@435 | 519 | // Insert the copy in the basic block, just before us |
duke@435 | 520 | b->_nodes.insert( l++, copy ); |
duke@435 | 521 | // Extend ("register allocate") the names array for the copy. |
duke@435 | 522 | _phc.new_lrg( copy, _phc._maxlrg++ ); |
duke@435 | 523 | _phc._cfg._bbs.map( copy->_idx, b ); |
duke@435 | 524 | //tty->print_cr("Split a debug use in Aggressive Coalesce"); |
duke@435 | 525 | } // End of if high frequency use/def |
duke@435 | 526 | } // End of for all debug inputs |
duke@435 | 527 | } // End of if low frequency safepoint |
duke@435 | 528 | |
duke@435 | 529 | } // End of if Phi |
duke@435 | 530 | |
duke@435 | 531 | } // End of for all instructions |
duke@435 | 532 | } // End of for all blocks |
duke@435 | 533 | } |
duke@435 | 534 | |
duke@435 | 535 | //============================================================================= |
duke@435 | 536 | //------------------------------coalesce--------------------------------------- |
duke@435 | 537 | // Aggressive (but pessimistic) copy coalescing of a single block |
duke@435 | 538 | |
duke@435 | 539 | // The following coalesce pass represents a single round of aggressive |
duke@435 | 540 | // pessimistic coalesce. "Aggressive" means no attempt to preserve |
duke@435 | 541 | // colorability when coalescing. This occasionally means more spills, but |
duke@435 | 542 | // it also means fewer rounds of coalescing for better code - and that means |
duke@435 | 543 | // faster compiles. |
duke@435 | 544 | |
duke@435 | 545 | // "Pessimistic" means we do not hit the fixed point in one pass (and we are |
duke@435 | 546 | // reaching for the least fixed point to boot). This is typically solved |
duke@435 | 547 | // with a few more rounds of coalescing, but the compiler must run fast. We |
duke@435 | 548 | // could optimistically coalescing everything touching PhiNodes together |
duke@435 | 549 | // into one big live range, then check for self-interference. Everywhere |
duke@435 | 550 | // the live range interferes with self it would have to be split. Finding |
duke@435 | 551 | // the right split points can be done with some heuristics (based on |
duke@435 | 552 | // expected frequency of edges in the live range). In short, it's a real |
duke@435 | 553 | // research problem and the timeline is too short to allow such research. |
duke@435 | 554 | // Further thoughts: (1) build the LR in a pass, (2) find self-interference |
duke@435 | 555 | // in another pass, (3) per each self-conflict, split, (4) split by finding |
duke@435 | 556 | // the low-cost cut (min-cut) of the LR, (5) edges in the LR are weighted |
duke@435 | 557 | // according to the GCM algorithm (or just exec freq on CFG edges). |
duke@435 | 558 | |
duke@435 | 559 | void PhaseAggressiveCoalesce::coalesce( Block *b ) { |
duke@435 | 560 | // Copies are still "virtual" - meaning we have not made them explicitly |
duke@435 | 561 | // copies. Instead, Phi functions of successor blocks have mis-matched |
duke@435 | 562 | // live-ranges. If I fail to coalesce, I'll have to insert a copy to line |
duke@435 | 563 | // up the live-ranges. Check for Phis in successor blocks. |
duke@435 | 564 | uint i; |
duke@435 | 565 | for( i=0; i<b->_num_succs; i++ ) { |
duke@435 | 566 | Block *bs = b->_succs[i]; |
duke@435 | 567 | // Find index of 'b' in 'bs' predecessors |
duke@435 | 568 | uint j=1; |
duke@435 | 569 | while( _phc._cfg._bbs[bs->pred(j)->_idx] != b ) j++; |
duke@435 | 570 | // Visit all the Phis in successor block |
duke@435 | 571 | for( uint k = 1; k<bs->_nodes.size(); k++ ) { |
duke@435 | 572 | Node *n = bs->_nodes[k]; |
duke@435 | 573 | if( !n->is_Phi() ) break; |
duke@435 | 574 | combine_these_two( n, n->in(j) ); |
duke@435 | 575 | } |
duke@435 | 576 | } // End of for all successor blocks |
duke@435 | 577 | |
duke@435 | 578 | |
duke@435 | 579 | // Check _this_ block for 2-address instructions and copies. |
duke@435 | 580 | uint cnt = b->end_idx(); |
duke@435 | 581 | for( i = 1; i<cnt; i++ ) { |
duke@435 | 582 | Node *n = b->_nodes[i]; |
duke@435 | 583 | uint idx; |
duke@435 | 584 | // 2-address instructions have a virtual Copy matching their input |
duke@435 | 585 | // to their output |
duke@435 | 586 | if( n->is_Mach() && (idx = n->as_Mach()->two_adr()) ) { |
duke@435 | 587 | MachNode *mach = n->as_Mach(); |
duke@435 | 588 | combine_these_two( mach, mach->in(idx) ); |
duke@435 | 589 | } |
duke@435 | 590 | } // End of for all instructions in block |
duke@435 | 591 | } |
duke@435 | 592 | |
duke@435 | 593 | //============================================================================= |
duke@435 | 594 | //------------------------------PhaseConservativeCoalesce---------------------- |
duke@435 | 595 | PhaseConservativeCoalesce::PhaseConservativeCoalesce( PhaseChaitin &chaitin ) : PhaseCoalesce(chaitin) { |
duke@435 | 596 | _ulr.initialize(_phc._maxlrg); |
duke@435 | 597 | } |
duke@435 | 598 | |
duke@435 | 599 | //------------------------------verify----------------------------------------- |
duke@435 | 600 | void PhaseConservativeCoalesce::verify() { |
duke@435 | 601 | #ifdef ASSERT |
duke@435 | 602 | _phc.set_was_low(); |
duke@435 | 603 | #endif |
duke@435 | 604 | } |
duke@435 | 605 | |
duke@435 | 606 | //------------------------------union_helper----------------------------------- |
duke@435 | 607 | 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 ) { |
duke@435 | 608 | // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the |
duke@435 | 609 | // union-find tree |
duke@435 | 610 | _phc.Union( lr1_node, lr2_node ); |
duke@435 | 611 | |
duke@435 | 612 | // Single-def live range ONLY if both live ranges are single-def. |
duke@435 | 613 | // If both are single def, then src_def powers one live range |
duke@435 | 614 | // and def_copy powers the other. After merging, src_def powers |
duke@435 | 615 | // the combined live range. |
never@730 | 616 | lrgs(lr1)._def = (lrgs(lr1).is_multidef() || |
never@730 | 617 | lrgs(lr2).is_multidef() ) |
duke@435 | 618 | ? NodeSentinel : src_def; |
duke@435 | 619 | lrgs(lr2)._def = NULL; // No def for lrg 2 |
duke@435 | 620 | lrgs(lr2).Clear(); // Force empty mask for LRG 2 |
duke@435 | 621 | //lrgs(lr2)._size = 0; // Live-range 2 goes dead |
duke@435 | 622 | lrgs(lr1)._is_oop |= lrgs(lr2)._is_oop; |
duke@435 | 623 | lrgs(lr2)._is_oop = 0; // In particular, not an oop for GC info |
duke@435 | 624 | |
duke@435 | 625 | if (lrgs(lr1)._maxfreq < lrgs(lr2)._maxfreq) |
duke@435 | 626 | lrgs(lr1)._maxfreq = lrgs(lr2)._maxfreq; |
duke@435 | 627 | |
duke@435 | 628 | // Copy original value instead. Intermediate copies go dead, and |
duke@435 | 629 | // the dst_copy becomes useless. |
duke@435 | 630 | int didx = dst_copy->is_Copy(); |
duke@435 | 631 | dst_copy->set_req( didx, src_def ); |
duke@435 | 632 | // Add copy to free list |
duke@435 | 633 | // _phc.free_spillcopy(b->_nodes[bindex]); |
duke@435 | 634 | assert( b->_nodes[bindex] == dst_copy, "" ); |
duke@435 | 635 | dst_copy->replace_by( dst_copy->in(didx) ); |
duke@435 | 636 | dst_copy->set_req( didx, NULL); |
duke@435 | 637 | b->_nodes.remove(bindex); |
duke@435 | 638 | if( bindex < b->_ihrp_index ) b->_ihrp_index--; |
duke@435 | 639 | if( bindex < b->_fhrp_index ) b->_fhrp_index--; |
duke@435 | 640 | |
duke@435 | 641 | // Stretched lr1; add it to liveness of intermediate blocks |
duke@435 | 642 | Block *b2 = _phc._cfg._bbs[src_copy->_idx]; |
duke@435 | 643 | while( b != b2 ) { |
duke@435 | 644 | b = _phc._cfg._bbs[b->pred(1)->_idx]; |
duke@435 | 645 | _phc._live->live(b)->insert(lr1); |
duke@435 | 646 | } |
duke@435 | 647 | } |
duke@435 | 648 | |
duke@435 | 649 | //------------------------------compute_separating_interferences--------------- |
duke@435 | 650 | // Factored code from copy_copy that computes extra interferences from |
duke@435 | 651 | // lengthening a live range by double-coalescing. |
duke@435 | 652 | 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 ) { |
duke@435 | 653 | |
duke@435 | 654 | assert(!lrgs(lr1)._fat_proj, "cannot coalesce fat_proj"); |
duke@435 | 655 | assert(!lrgs(lr2)._fat_proj, "cannot coalesce fat_proj"); |
duke@435 | 656 | Node *prev_copy = dst_copy->in(dst_copy->is_Copy()); |
duke@435 | 657 | Block *b2 = b; |
duke@435 | 658 | uint bindex2 = bindex; |
duke@435 | 659 | while( 1 ) { |
duke@435 | 660 | // Find previous instruction |
duke@435 | 661 | bindex2--; // Chain backwards 1 instruction |
duke@435 | 662 | while( bindex2 == 0 ) { // At block start, find prior block |
duke@435 | 663 | assert( b2->num_preds() == 2, "cannot double coalesce across c-flow" ); |
duke@435 | 664 | b2 = _phc._cfg._bbs[b2->pred(1)->_idx]; |
duke@435 | 665 | bindex2 = b2->end_idx()-1; |
duke@435 | 666 | } |
duke@435 | 667 | // Get prior instruction |
duke@435 | 668 | assert(bindex2 < b2->_nodes.size(), "index out of bounds"); |
duke@435 | 669 | Node *x = b2->_nodes[bindex2]; |
duke@435 | 670 | if( x == prev_copy ) { // Previous copy in copy chain? |
duke@435 | 671 | if( prev_copy == src_copy)// Found end of chain and all interferences |
duke@435 | 672 | break; // So break out of loop |
duke@435 | 673 | // Else work back one in copy chain |
duke@435 | 674 | prev_copy = prev_copy->in(prev_copy->is_Copy()); |
duke@435 | 675 | } else { // Else collect interferences |
duke@435 | 676 | uint lidx = _phc.Find(x); |
duke@435 | 677 | // Found another def of live-range being stretched? |
duke@435 | 678 | if( lidx == lr1 ) return max_juint; |
duke@435 | 679 | if( lidx == lr2 ) return max_juint; |
duke@435 | 680 | |
duke@435 | 681 | // If we attempt to coalesce across a bound def |
duke@435 | 682 | if( lrgs(lidx).is_bound() ) { |
duke@435 | 683 | // Do not let the coalesced LRG expect to get the bound color |
duke@435 | 684 | rm.SUBTRACT( lrgs(lidx).mask() ); |
duke@435 | 685 | // Recompute rm_size |
duke@435 | 686 | rm_size = rm.Size(); |
duke@435 | 687 | //if( rm._flags ) rm_size += 1000000; |
duke@435 | 688 | if( reg_degree >= rm_size ) return max_juint; |
duke@435 | 689 | } |
duke@435 | 690 | if( rm.overlap(lrgs(lidx).mask()) ) { |
duke@435 | 691 | // Insert lidx into union LRG; returns TRUE if actually inserted |
duke@435 | 692 | if( _ulr.insert(lidx) ) { |
duke@435 | 693 | // Infinite-stack neighbors do not alter colorability, as they |
duke@435 | 694 | // can always color to some other color. |
duke@435 | 695 | if( !lrgs(lidx).mask().is_AllStack() ) { |
duke@435 | 696 | // If this coalesce will make any new neighbor uncolorable, |
duke@435 | 697 | // do not coalesce. |
duke@435 | 698 | if( lrgs(lidx).just_lo_degree() ) |
duke@435 | 699 | return max_juint; |
duke@435 | 700 | // Bump our degree |
duke@435 | 701 | if( ++reg_degree >= rm_size ) |
duke@435 | 702 | return max_juint; |
duke@435 | 703 | } // End of if not infinite-stack neighbor |
duke@435 | 704 | } // End of if actually inserted |
duke@435 | 705 | } // End of if live range overlaps |
twisti@1040 | 706 | } // End of else collect interferences for 1 node |
twisti@1040 | 707 | } // End of while forever, scan back for interferences |
duke@435 | 708 | return reg_degree; |
duke@435 | 709 | } |
duke@435 | 710 | |
duke@435 | 711 | //------------------------------update_ifg------------------------------------- |
duke@435 | 712 | void PhaseConservativeCoalesce::update_ifg(uint lr1, uint lr2, IndexSet *n_lr1, IndexSet *n_lr2) { |
duke@435 | 713 | // Some original neighbors of lr1 might have gone away |
duke@435 | 714 | // because the constrained register mask prevented them. |
duke@435 | 715 | // Remove lr1 from such neighbors. |
duke@435 | 716 | IndexSetIterator one(n_lr1); |
duke@435 | 717 | uint neighbor; |
duke@435 | 718 | LRG &lrg1 = lrgs(lr1); |
duke@435 | 719 | while ((neighbor = one.next()) != 0) |
duke@435 | 720 | if( !_ulr.member(neighbor) ) |
duke@435 | 721 | if( _phc._ifg->neighbors(neighbor)->remove(lr1) ) |
duke@435 | 722 | lrgs(neighbor).inc_degree( -lrg1.compute_degree(lrgs(neighbor)) ); |
duke@435 | 723 | |
duke@435 | 724 | |
duke@435 | 725 | // lr2 is now called (coalesced into) lr1. |
duke@435 | 726 | // Remove lr2 from the IFG. |
duke@435 | 727 | IndexSetIterator two(n_lr2); |
duke@435 | 728 | LRG &lrg2 = lrgs(lr2); |
duke@435 | 729 | while ((neighbor = two.next()) != 0) |
duke@435 | 730 | if( _phc._ifg->neighbors(neighbor)->remove(lr2) ) |
duke@435 | 731 | lrgs(neighbor).inc_degree( -lrg2.compute_degree(lrgs(neighbor)) ); |
duke@435 | 732 | |
duke@435 | 733 | // Some neighbors of intermediate copies now interfere with the |
duke@435 | 734 | // combined live range. |
duke@435 | 735 | IndexSetIterator three(&_ulr); |
duke@435 | 736 | while ((neighbor = three.next()) != 0) |
duke@435 | 737 | if( _phc._ifg->neighbors(neighbor)->insert(lr1) ) |
duke@435 | 738 | lrgs(neighbor).inc_degree( lrg1.compute_degree(lrgs(neighbor)) ); |
duke@435 | 739 | } |
duke@435 | 740 | |
duke@435 | 741 | //------------------------------record_bias------------------------------------ |
duke@435 | 742 | static void record_bias( const PhaseIFG *ifg, int lr1, int lr2 ) { |
duke@435 | 743 | // Tag copy bias here |
duke@435 | 744 | if( !ifg->lrgs(lr1)._copy_bias ) |
duke@435 | 745 | ifg->lrgs(lr1)._copy_bias = lr2; |
duke@435 | 746 | if( !ifg->lrgs(lr2)._copy_bias ) |
duke@435 | 747 | ifg->lrgs(lr2)._copy_bias = lr1; |
duke@435 | 748 | } |
duke@435 | 749 | |
duke@435 | 750 | //------------------------------copy_copy-------------------------------------- |
duke@435 | 751 | // See if I can coalesce a series of multiple copies together. I need the |
duke@435 | 752 | // final dest copy and the original src copy. They can be the same Node. |
duke@435 | 753 | // Compute the compatible register masks. |
duke@435 | 754 | bool PhaseConservativeCoalesce::copy_copy( Node *dst_copy, Node *src_copy, Block *b, uint bindex ) { |
duke@435 | 755 | |
duke@435 | 756 | if( !dst_copy->is_SpillCopy() ) return false; |
duke@435 | 757 | if( !src_copy->is_SpillCopy() ) return false; |
duke@435 | 758 | Node *src_def = src_copy->in(src_copy->is_Copy()); |
duke@435 | 759 | uint lr1 = _phc.Find(dst_copy); |
duke@435 | 760 | uint lr2 = _phc.Find(src_def ); |
duke@435 | 761 | |
duke@435 | 762 | // Same live ranges already? |
duke@435 | 763 | if( lr1 == lr2 ) return false; |
duke@435 | 764 | |
duke@435 | 765 | // Interfere? |
duke@435 | 766 | if( _phc._ifg->test_edge_sq( lr1, lr2 ) ) return false; |
duke@435 | 767 | |
duke@435 | 768 | // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK. |
duke@435 | 769 | if( !lrgs(lr1)._is_oop && lrgs(lr2)._is_oop ) // not an oop->int cast |
duke@435 | 770 | return false; |
duke@435 | 771 | |
duke@435 | 772 | // Coalescing between an aligned live range and a mis-aligned live range? |
duke@435 | 773 | // No, no! Alignment changes how we count degree. |
duke@435 | 774 | if( lrgs(lr1)._fat_proj != lrgs(lr2)._fat_proj ) |
duke@435 | 775 | return false; |
duke@435 | 776 | |
duke@435 | 777 | // Sort; use smaller live-range number |
duke@435 | 778 | Node *lr1_node = dst_copy; |
duke@435 | 779 | Node *lr2_node = src_def; |
duke@435 | 780 | if( lr1 > lr2 ) { |
duke@435 | 781 | uint tmp = lr1; lr1 = lr2; lr2 = tmp; |
duke@435 | 782 | lr1_node = src_def; lr2_node = dst_copy; |
duke@435 | 783 | } |
duke@435 | 784 | |
duke@435 | 785 | // Check for compatibility of the 2 live ranges by |
duke@435 | 786 | // intersecting their allowed register sets. |
duke@435 | 787 | RegMask rm = lrgs(lr1).mask(); |
duke@435 | 788 | rm.AND(lrgs(lr2).mask()); |
duke@435 | 789 | // Number of bits free |
duke@435 | 790 | uint rm_size = rm.Size(); |
duke@435 | 791 | |
never@2085 | 792 | if (UseFPUForSpilling && rm.is_AllStack() ) { |
never@2085 | 793 | // Don't coalesce when frequency difference is large |
never@2085 | 794 | Block *dst_b = _phc._cfg._bbs[dst_copy->_idx]; |
never@2085 | 795 | Block *src_def_b = _phc._cfg._bbs[src_def->_idx]; |
never@2085 | 796 | if (src_def_b->_freq > 10*dst_b->_freq ) |
never@2085 | 797 | return false; |
never@2085 | 798 | } |
never@2085 | 799 | |
duke@435 | 800 | // If we can use any stack slot, then effective size is infinite |
duke@435 | 801 | if( rm.is_AllStack() ) rm_size += 1000000; |
duke@435 | 802 | // Incompatible masks, no way to coalesce |
duke@435 | 803 | if( rm_size == 0 ) return false; |
duke@435 | 804 | |
duke@435 | 805 | // Another early bail-out test is when we are double-coalescing and the |
twisti@1040 | 806 | // 2 copies are separated by some control flow. |
duke@435 | 807 | if( dst_copy != src_copy ) { |
duke@435 | 808 | Block *src_b = _phc._cfg._bbs[src_copy->_idx]; |
duke@435 | 809 | Block *b2 = b; |
duke@435 | 810 | while( b2 != src_b ) { |
duke@435 | 811 | if( b2->num_preds() > 2 ){// Found merge-point |
duke@435 | 812 | _phc._lost_opp_cflow_coalesce++; |
duke@435 | 813 | // extra record_bias commented out because Chris believes it is not |
duke@435 | 814 | // productive. Since we can record only 1 bias, we want to choose one |
duke@435 | 815 | // that stands a chance of working and this one probably does not. |
duke@435 | 816 | //record_bias( _phc._lrgs, lr1, lr2 ); |
duke@435 | 817 | return false; // To hard to find all interferences |
duke@435 | 818 | } |
duke@435 | 819 | b2 = _phc._cfg._bbs[b2->pred(1)->_idx]; |
duke@435 | 820 | } |
duke@435 | 821 | } |
duke@435 | 822 | |
duke@435 | 823 | // Union the two interference sets together into '_ulr' |
duke@435 | 824 | uint reg_degree = _ulr.lrg_union( lr1, lr2, rm_size, _phc._ifg, rm ); |
duke@435 | 825 | |
duke@435 | 826 | if( reg_degree >= rm_size ) { |
duke@435 | 827 | record_bias( _phc._ifg, lr1, lr2 ); |
duke@435 | 828 | return false; |
duke@435 | 829 | } |
duke@435 | 830 | |
duke@435 | 831 | // Now I need to compute all the interferences between dst_copy and |
duke@435 | 832 | // src_copy. I'm not willing visit the entire interference graph, so |
duke@435 | 833 | // I limit my search to things in dst_copy's block or in a straight |
duke@435 | 834 | // line of previous blocks. I give up at merge points or when I get |
duke@435 | 835 | // more interferences than my degree. I can stop when I find src_copy. |
duke@435 | 836 | if( dst_copy != src_copy ) { |
duke@435 | 837 | reg_degree = compute_separating_interferences(dst_copy, src_copy, b, bindex, rm, rm_size, reg_degree, lr1, lr2 ); |
duke@435 | 838 | if( reg_degree == max_juint ) { |
duke@435 | 839 | record_bias( _phc._ifg, lr1, lr2 ); |
duke@435 | 840 | return false; |
duke@435 | 841 | } |
duke@435 | 842 | } // End of if dst_copy & src_copy are different |
duke@435 | 843 | |
duke@435 | 844 | |
duke@435 | 845 | // ---- THE COMBINED LRG IS COLORABLE ---- |
duke@435 | 846 | |
duke@435 | 847 | // YEAH - Now coalesce this copy away |
duke@435 | 848 | assert( lrgs(lr1).num_regs() == lrgs(lr2).num_regs(), "" ); |
duke@435 | 849 | |
duke@435 | 850 | IndexSet *n_lr1 = _phc._ifg->neighbors(lr1); |
duke@435 | 851 | IndexSet *n_lr2 = _phc._ifg->neighbors(lr2); |
duke@435 | 852 | |
duke@435 | 853 | // Update the interference graph |
duke@435 | 854 | update_ifg(lr1, lr2, n_lr1, n_lr2); |
duke@435 | 855 | |
duke@435 | 856 | _ulr.remove(lr1); |
duke@435 | 857 | |
duke@435 | 858 | // Uncomment the following code to trace Coalescing in great detail. |
duke@435 | 859 | // |
duke@435 | 860 | //if (false) { |
duke@435 | 861 | // tty->cr(); |
duke@435 | 862 | // tty->print_cr("#######################################"); |
duke@435 | 863 | // tty->print_cr("union %d and %d", lr1, lr2); |
duke@435 | 864 | // n_lr1->dump(); |
duke@435 | 865 | // n_lr2->dump(); |
duke@435 | 866 | // tty->print_cr("resulting set is"); |
duke@435 | 867 | // _ulr.dump(); |
duke@435 | 868 | //} |
duke@435 | 869 | |
duke@435 | 870 | // Replace n_lr1 with the new combined live range. _ulr will use |
duke@435 | 871 | // n_lr1's old memory on the next iteration. n_lr2 is cleared to |
duke@435 | 872 | // send its internal memory to the free list. |
duke@435 | 873 | _ulr.swap(n_lr1); |
duke@435 | 874 | _ulr.clear(); |
duke@435 | 875 | n_lr2->clear(); |
duke@435 | 876 | |
duke@435 | 877 | lrgs(lr1).set_degree( _phc._ifg->effective_degree(lr1) ); |
duke@435 | 878 | lrgs(lr2).set_degree( 0 ); |
duke@435 | 879 | |
duke@435 | 880 | // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the |
duke@435 | 881 | // union-find tree |
duke@435 | 882 | union_helper( lr1_node, lr2_node, lr1, lr2, src_def, dst_copy, src_copy, b, bindex ); |
duke@435 | 883 | // Combine register restrictions |
duke@435 | 884 | lrgs(lr1).set_mask(rm); |
duke@435 | 885 | lrgs(lr1).compute_set_mask_size(); |
duke@435 | 886 | lrgs(lr1)._cost += lrgs(lr2)._cost; |
duke@435 | 887 | lrgs(lr1)._area += lrgs(lr2)._area; |
duke@435 | 888 | |
duke@435 | 889 | // While its uncommon to successfully coalesce live ranges that started out |
duke@435 | 890 | // being not-lo-degree, it can happen. In any case the combined coalesced |
duke@435 | 891 | // live range better Simplify nicely. |
duke@435 | 892 | lrgs(lr1)._was_lo = 1; |
duke@435 | 893 | |
duke@435 | 894 | // kinda expensive to do all the time |
duke@435 | 895 | //tty->print_cr("warning: slow verify happening"); |
duke@435 | 896 | //_phc._ifg->verify( &_phc ); |
duke@435 | 897 | return true; |
duke@435 | 898 | } |
duke@435 | 899 | |
duke@435 | 900 | //------------------------------coalesce--------------------------------------- |
duke@435 | 901 | // Conservative (but pessimistic) copy coalescing of a single block |
duke@435 | 902 | void PhaseConservativeCoalesce::coalesce( Block *b ) { |
duke@435 | 903 | // Bail out on infrequent blocks |
duke@435 | 904 | if( b->is_uncommon(_phc._cfg._bbs) ) |
duke@435 | 905 | return; |
duke@435 | 906 | // Check this block for copies. |
duke@435 | 907 | for( uint i = 1; i<b->end_idx(); i++ ) { |
duke@435 | 908 | // Check for actual copies on inputs. Coalesce a copy into its |
duke@435 | 909 | // input if use and copy's input are compatible. |
duke@435 | 910 | Node *copy1 = b->_nodes[i]; |
duke@435 | 911 | uint idx1 = copy1->is_Copy(); |
duke@435 | 912 | if( !idx1 ) continue; // Not a copy |
duke@435 | 913 | |
duke@435 | 914 | if( copy_copy(copy1,copy1,b,i) ) { |
duke@435 | 915 | i--; // Retry, same location in block |
duke@435 | 916 | PhaseChaitin::_conserv_coalesce++; // Collect stats on success |
duke@435 | 917 | continue; |
duke@435 | 918 | } |
duke@435 | 919 | |
duke@435 | 920 | /* do not attempt pairs. About 1/2 of all pairs can be removed by |
duke@435 | 921 | post-alloc. The other set are too few to bother. |
duke@435 | 922 | Node *copy2 = copy1->in(idx1); |
duke@435 | 923 | uint idx2 = copy2->is_Copy(); |
duke@435 | 924 | if( !idx2 ) continue; |
duke@435 | 925 | if( copy_copy(copy1,copy2,b,i) ) { |
duke@435 | 926 | i--; // Retry, same location in block |
duke@435 | 927 | PhaseChaitin::_conserv_coalesce_pair++; // Collect stats on success |
duke@435 | 928 | continue; |
duke@435 | 929 | } |
duke@435 | 930 | */ |
duke@435 | 931 | } |
duke@435 | 932 | } |