src/share/vm/opto/coalesce.cpp

Thu, 21 Nov 2013 12:30:35 -0800

author
kvn
date
Thu, 21 Nov 2013 12:30:35 -0800
changeset 6485
da862781b584
parent 5639
4b078f877b56
child 6876
710a3c8b516e
child 7295
b8e2e616c1e9
permissions
-rw-r--r--

Merge

duke@435 1 /*
drchase@5285 2 * Copyright (c) 1997, 2013, 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 #ifndef PRODUCT
neliasso@4949 38 void PhaseCoalesce::dump(Node *n) const {
duke@435 39 // Being a const function means I cannot use 'Find'
neliasso@4949 40 uint r = _phc._lrg_map.find(n);
duke@435 41 tty->print("L%d/N%d ",r,n->_idx);
duke@435 42 }
duke@435 43
duke@435 44 void PhaseCoalesce::dump() const {
duke@435 45 // I know I have a block layout now, so I can print blocks in a loop
adlertz@5539 46 for( uint i=0; i<_phc._cfg.number_of_blocks(); i++ ) {
duke@435 47 uint j;
adlertz@5539 48 Block* b = _phc._cfg.get_block(i);
duke@435 49 // Print a nice block header
duke@435 50 tty->print("B%d: ",b->_pre_order);
duke@435 51 for( j=1; j<b->num_preds(); j++ )
adlertz@5509 52 tty->print("B%d ", _phc._cfg.get_block_for_node(b->pred(j))->_pre_order);
duke@435 53 tty->print("-> ");
duke@435 54 for( j=0; j<b->_num_succs; j++ )
duke@435 55 tty->print("B%d ",b->_succs[j]->_pre_order);
duke@435 56 tty->print(" IDom: B%d/#%d\n", b->_idom ? b->_idom->_pre_order : 0, b->_dom_depth);
adlertz@5635 57 uint cnt = b->number_of_nodes();
duke@435 58 for( j=0; j<cnt; j++ ) {
adlertz@5635 59 Node *n = b->get_node(j);
duke@435 60 dump( n );
duke@435 61 tty->print("\t%s\t",n->Name());
duke@435 62
duke@435 63 // Dump the inputs
duke@435 64 uint k; // Exit value of loop
duke@435 65 for( k=0; k<n->req(); k++ ) // For all required inputs
duke@435 66 if( n->in(k) ) dump( n->in(k) );
duke@435 67 else tty->print("_ ");
duke@435 68 int any_prec = 0;
duke@435 69 for( ; k<n->len(); k++ ) // For all precedence inputs
duke@435 70 if( n->in(k) ) {
duke@435 71 if( !any_prec++ ) tty->print(" |");
duke@435 72 dump( n->in(k) );
duke@435 73 }
duke@435 74
duke@435 75 // Dump node-specific info
duke@435 76 n->dump_spec(tty);
duke@435 77 tty->print("\n");
duke@435 78
duke@435 79 }
duke@435 80 tty->print("\n");
duke@435 81 }
duke@435 82 }
duke@435 83 #endif
duke@435 84
duke@435 85 // Combine the live ranges def'd by these 2 Nodes. N2 is an input to N1.
neliasso@4949 86 void PhaseCoalesce::combine_these_two(Node *n1, Node *n2) {
neliasso@4949 87 uint lr1 = _phc._lrg_map.find(n1);
neliasso@4949 88 uint lr2 = _phc._lrg_map.find(n2);
duke@435 89 if( lr1 != lr2 && // Different live ranges already AND
duke@435 90 !_phc._ifg->test_edge_sq( lr1, lr2 ) ) { // Do not interfere
duke@435 91 LRG *lrg1 = &_phc.lrgs(lr1);
duke@435 92 LRG *lrg2 = &_phc.lrgs(lr2);
duke@435 93 // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK.
duke@435 94
duke@435 95 // Now, why is int->oop OK? We end up declaring a raw-pointer as an oop
duke@435 96 // and in general that's a bad thing. However, int->oop conversions only
duke@435 97 // happen at GC points, so the lifetime of the misclassified raw-pointer
duke@435 98 // is from the CheckCastPP (that converts it to an oop) backwards up
duke@435 99 // through a merge point and into the slow-path call, and around the
duke@435 100 // diamond up to the heap-top check and back down into the slow-path call.
duke@435 101 // The misclassified raw pointer is NOT live across the slow-path call,
duke@435 102 // and so does not appear in any GC info, so the fact that it is
duke@435 103 // misclassified is OK.
duke@435 104
duke@435 105 if( (lrg1->_is_oop || !lrg2->_is_oop) && // not an oop->int cast AND
duke@435 106 // Compatible final mask
duke@435 107 lrg1->mask().overlap( lrg2->mask() ) ) {
duke@435 108 // Merge larger into smaller.
duke@435 109 if( lr1 > lr2 ) {
duke@435 110 uint tmp = lr1; lr1 = lr2; lr2 = tmp;
duke@435 111 Node *n = n1; n1 = n2; n2 = n;
duke@435 112 LRG *ltmp = lrg1; lrg1 = lrg2; lrg2 = ltmp;
duke@435 113 }
duke@435 114 // Union lr2 into lr1
duke@435 115 _phc.Union( n1, n2 );
duke@435 116 if (lrg1->_maxfreq < lrg2->_maxfreq)
duke@435 117 lrg1->_maxfreq = lrg2->_maxfreq;
duke@435 118 // Merge in the IFG
duke@435 119 _phc._ifg->Union( lr1, lr2 );
duke@435 120 // Combine register restrictions
duke@435 121 lrg1->AND(lrg2->mask());
duke@435 122 }
duke@435 123 }
duke@435 124 }
duke@435 125
duke@435 126 // Copy coalescing
adlertz@5539 127 void PhaseCoalesce::coalesce_driver() {
duke@435 128 verify();
duke@435 129 // Coalesce from high frequency to low
adlertz@5539 130 for (uint i = 0; i < _phc._cfg.number_of_blocks(); i++) {
adlertz@5539 131 coalesce(_phc._blks[i]);
adlertz@5539 132 }
duke@435 133 }
duke@435 134
duke@435 135 // I am inserting copies to come out of SSA form. In the general case, I am
duke@435 136 // doing a parallel renaming. I'm in the Named world now, so I can't do a
duke@435 137 // general parallel renaming. All the copies now use "names" (live-ranges)
duke@435 138 // to carry values instead of the explicit use-def chains. Suppose I need to
duke@435 139 // insert 2 copies into the same block. They copy L161->L128 and L128->L132.
duke@435 140 // If I insert them in the wrong order then L128 will get clobbered before it
duke@435 141 // can get used by the second copy. This cannot happen in the SSA model;
duke@435 142 // direct use-def chains get me the right value. It DOES happen in the named
duke@435 143 // model so I have to handle the reordering of copies.
duke@435 144 //
duke@435 145 // In general, I need to topo-sort the placed copies to avoid conflicts.
duke@435 146 // Its possible to have a closed cycle of copies (e.g., recirculating the same
duke@435 147 // values around a loop). In this case I need a temp to break the cycle.
duke@435 148 void PhaseAggressiveCoalesce::insert_copy_with_overlap( Block *b, Node *copy, uint dst_name, uint src_name ) {
duke@435 149
duke@435 150 // Scan backwards for the locations of the last use of the dst_name.
duke@435 151 // I am about to clobber the dst_name, so the copy must be inserted
duke@435 152 // after the last use. Last use is really first-use on a backwards scan.
duke@435 153 uint i = b->end_idx()-1;
neliasso@4949 154 while(1) {
adlertz@5635 155 Node *n = b->get_node(i);
duke@435 156 // Check for end of virtual copies; this is also the end of the
duke@435 157 // parallel renaming effort.
neliasso@4949 158 if (n->_idx < _unique) {
neliasso@4949 159 break;
neliasso@4949 160 }
duke@435 161 uint idx = n->is_Copy();
kvn@3040 162 assert( idx || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" );
neliasso@4949 163 if (idx && _phc._lrg_map.find(n->in(idx)) == dst_name) {
neliasso@4949 164 break;
neliasso@4949 165 }
duke@435 166 i--;
duke@435 167 }
duke@435 168 uint last_use_idx = i;
duke@435 169
duke@435 170 // Also search for any kill of src_name that exits the block.
duke@435 171 // Since the copy uses src_name, I have to come before any kill.
duke@435 172 uint kill_src_idx = b->end_idx();
duke@435 173 // There can be only 1 kill that exits any block and that is
duke@435 174 // the last kill. Thus it is the first kill on a backwards scan.
duke@435 175 i = b->end_idx()-1;
neliasso@4949 176 while (1) {
adlertz@5635 177 Node *n = b->get_node(i);
duke@435 178 // Check for end of virtual copies; this is also the end of the
duke@435 179 // parallel renaming effort.
neliasso@4949 180 if (n->_idx < _unique) {
neliasso@4949 181 break;
neliasso@4949 182 }
kvn@3040 183 assert( n->is_Copy() || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" );
neliasso@4949 184 if (_phc._lrg_map.find(n) == src_name) {
duke@435 185 kill_src_idx = i;
duke@435 186 break;
duke@435 187 }
duke@435 188 i--;
duke@435 189 }
duke@435 190 // Need a temp? Last use of dst comes after the kill of src?
neliasso@4949 191 if (last_use_idx >= kill_src_idx) {
duke@435 192 // Need to break a cycle with a temp
duke@435 193 uint idx = copy->is_Copy();
duke@435 194 Node *tmp = copy->clone();
neliasso@4949 195 uint max_lrg_id = _phc._lrg_map.max_lrg_id();
neliasso@4949 196 _phc.new_lrg(tmp, max_lrg_id);
neliasso@4949 197 _phc._lrg_map.set_max_lrg_id(max_lrg_id + 1);
neliasso@4949 198
duke@435 199 // Insert new temp between copy and source
duke@435 200 tmp ->set_req(idx,copy->in(idx));
duke@435 201 copy->set_req(idx,tmp);
duke@435 202 // Save source in temp early, before source is killed
adlertz@5635 203 b->insert_node(tmp, kill_src_idx);
adlertz@5509 204 _phc._cfg.map_node_to_block(tmp, b);
duke@435 205 last_use_idx++;
duke@435 206 }
duke@435 207
duke@435 208 // Insert just after last use
adlertz@5635 209 b->insert_node(copy, last_use_idx + 1);
duke@435 210 }
duke@435 211
duke@435 212 void PhaseAggressiveCoalesce::insert_copies( Matcher &matcher ) {
duke@435 213 // We do LRGs compressing and fix a liveout data only here since the other
duke@435 214 // place in Split() is guarded by the assert which we never hit.
neliasso@4949 215 _phc._lrg_map.compress_uf_map_for_nodes();
duke@435 216 // Fix block's liveout data for compressed live ranges.
neliasso@4949 217 for (uint lrg = 1; lrg < _phc._lrg_map.max_lrg_id(); lrg++) {
neliasso@4949 218 uint compressed_lrg = _phc._lrg_map.find(lrg);
neliasso@4949 219 if (lrg != compressed_lrg) {
adlertz@5539 220 for (uint bidx = 0; bidx < _phc._cfg.number_of_blocks(); bidx++) {
adlertz@5539 221 IndexSet *liveout = _phc._live->live(_phc._cfg.get_block(bidx));
neliasso@4949 222 if (liveout->member(lrg)) {
duke@435 223 liveout->remove(lrg);
duke@435 224 liveout->insert(compressed_lrg);
duke@435 225 }
duke@435 226 }
duke@435 227 }
duke@435 228 }
duke@435 229
duke@435 230 // All new nodes added are actual copies to replace virtual copies.
duke@435 231 // Nodes with index less than '_unique' are original, non-virtual Nodes.
duke@435 232 _unique = C->unique();
duke@435 233
adlertz@5539 234 for (uint i = 0; i < _phc._cfg.number_of_blocks(); i++) {
drchase@5285 235 C->check_node_count(NodeLimitFudgeFactor, "out of nodes in coalesce");
drchase@5285 236 if (C->failing()) return;
adlertz@5539 237 Block *b = _phc._cfg.get_block(i);
duke@435 238 uint cnt = b->num_preds(); // Number of inputs to the Phi
duke@435 239
adlertz@5635 240 for( uint l = 1; l<b->number_of_nodes(); l++ ) {
adlertz@5635 241 Node *n = b->get_node(l);
duke@435 242
duke@435 243 // Do not use removed-copies, use copied value instead
duke@435 244 uint ncnt = n->req();
duke@435 245 for( uint k = 1; k<ncnt; k++ ) {
duke@435 246 Node *copy = n->in(k);
duke@435 247 uint cidx = copy->is_Copy();
duke@435 248 if( cidx ) {
duke@435 249 Node *def = copy->in(cidx);
neliasso@4949 250 if (_phc._lrg_map.find(copy) == _phc._lrg_map.find(def)) {
neliasso@4949 251 n->set_req(k, def);
neliasso@4949 252 }
duke@435 253 }
duke@435 254 }
duke@435 255
duke@435 256 // Remove any explicit copies that get coalesced.
duke@435 257 uint cidx = n->is_Copy();
duke@435 258 if( cidx ) {
duke@435 259 Node *def = n->in(cidx);
neliasso@4949 260 if (_phc._lrg_map.find(n) == _phc._lrg_map.find(def)) {
duke@435 261 n->replace_by(def);
duke@435 262 n->set_req(cidx,NULL);
adlertz@5635 263 b->remove_node(l);
duke@435 264 l--;
duke@435 265 continue;
duke@435 266 }
duke@435 267 }
duke@435 268
neliasso@4949 269 if (n->is_Phi()) {
duke@435 270 // Get the chosen name for the Phi
neliasso@4949 271 uint phi_name = _phc._lrg_map.find(n);
duke@435 272 // Ignore the pre-allocated specials
neliasso@4949 273 if (!phi_name) {
neliasso@4949 274 continue;
neliasso@4949 275 }
duke@435 276 // Check for mismatch inputs to Phi
neliasso@4949 277 for (uint j = 1; j < cnt; j++) {
duke@435 278 Node *m = n->in(j);
neliasso@4949 279 uint src_name = _phc._lrg_map.find(m);
neliasso@4949 280 if (src_name != phi_name) {
adlertz@5509 281 Block *pred = _phc._cfg.get_block_for_node(b->pred(j));
duke@435 282 Node *copy;
duke@435 283 assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach");
duke@435 284 // Rematerialize constants instead of copying them
duke@435 285 if( m->is_Mach() && m->as_Mach()->is_Con() &&
duke@435 286 m->as_Mach()->rematerialize() ) {
duke@435 287 copy = m->clone();
duke@435 288 // Insert the copy in the predecessor basic block
duke@435 289 pred->add_inst(copy);
duke@435 290 // Copy any flags as well
neliasso@4949 291 _phc.clone_projs(pred, pred->end_idx(), m, copy, _phc._lrg_map);
duke@435 292 } else {
duke@435 293 const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
neliasso@4949 294 copy = new (C) MachSpillCopyNode(m, *rm, *rm);
duke@435 295 // Find a good place to insert. Kinda tricky, use a subroutine
duke@435 296 insert_copy_with_overlap(pred,copy,phi_name,src_name);
duke@435 297 }
duke@435 298 // Insert the copy in the use-def chain
neliasso@4949 299 n->set_req(j, copy);
adlertz@5509 300 _phc._cfg.map_node_to_block(copy, pred);
duke@435 301 // Extend ("register allocate") the names array for the copy.
neliasso@4949 302 _phc._lrg_map.extend(copy->_idx, phi_name);
duke@435 303 } // End of if Phi names do not match
duke@435 304 } // End of for all inputs to Phi
duke@435 305 } else { // End of if Phi
duke@435 306
duke@435 307 // Now check for 2-address instructions
duke@435 308 uint idx;
duke@435 309 if( n->is_Mach() && (idx=n->as_Mach()->two_adr()) ) {
duke@435 310 // Get the chosen name for the Node
neliasso@4949 311 uint name = _phc._lrg_map.find(n);
neliasso@4949 312 assert (name, "no 2-address specials");
duke@435 313 // Check for name mis-match on the 2-address input
duke@435 314 Node *m = n->in(idx);
neliasso@4949 315 if (_phc._lrg_map.find(m) != name) {
duke@435 316 Node *copy;
duke@435 317 assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach");
duke@435 318 // At this point it is unsafe to extend live ranges (6550579).
duke@435 319 // Rematerialize only constants as we do for Phi above.
neliasso@4949 320 if(m->is_Mach() && m->as_Mach()->is_Con() &&
neliasso@4949 321 m->as_Mach()->rematerialize()) {
duke@435 322 copy = m->clone();
duke@435 323 // Insert the copy in the basic block, just before us
adlertz@5635 324 b->insert_node(copy, l++);
kvn@5543 325 l += _phc.clone_projs(b, l, m, copy, _phc._lrg_map);
duke@435 326 } else {
duke@435 327 const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
neliasso@4949 328 copy = new (C) MachSpillCopyNode(m, *rm, *rm);
duke@435 329 // Insert the copy in the basic block, just before us
adlertz@5635 330 b->insert_node(copy, l++);
duke@435 331 }
duke@435 332 // Insert the copy in the use-def chain
neliasso@4949 333 n->set_req(idx, copy);
duke@435 334 // Extend ("register allocate") the names array for the copy.
neliasso@4949 335 _phc._lrg_map.extend(copy->_idx, name);
adlertz@5509 336 _phc._cfg.map_node_to_block(copy, b);
duke@435 337 }
duke@435 338
duke@435 339 } // End of is two-adr
duke@435 340
duke@435 341 // Insert a copy at a debug use for a lrg which has high frequency
adlertz@5639 342 if (b->_freq < OPTO_DEBUG_SPLIT_FREQ || _phc._cfg.is_uncommon(b)) {
duke@435 343 // Walk the debug inputs to the node and check for lrg freq
duke@435 344 JVMState* jvms = n->jvms();
duke@435 345 uint debug_start = jvms ? jvms->debug_start() : 999999;
duke@435 346 uint debug_end = jvms ? jvms->debug_end() : 999999;
duke@435 347 for(uint inpidx = debug_start; inpidx < debug_end; inpidx++) {
duke@435 348 // Do not split monitors; they are only needed for debug table
duke@435 349 // entries and need no code.
neliasso@4949 350 if (jvms->is_monitor_use(inpidx)) {
neliasso@4949 351 continue;
neliasso@4949 352 }
duke@435 353 Node *inp = n->in(inpidx);
neliasso@4949 354 uint nidx = _phc._lrg_map.live_range_id(inp);
duke@435 355 LRG &lrg = lrgs(nidx);
duke@435 356
duke@435 357 // If this lrg has a high frequency use/def
kvn@1108 358 if( lrg._maxfreq >= _phc.high_frequency_lrg() ) {
duke@435 359 // If the live range is also live out of this block (like it
duke@435 360 // would be for a fast/slow idiom), the normal spill mechanism
duke@435 361 // does an excellent job. If it is not live out of this block
duke@435 362 // (like it would be for debug info to uncommon trap) splitting
duke@435 363 // the live range now allows a better allocation in the high
duke@435 364 // frequency blocks.
duke@435 365 // Build_IFG_virtual has converted the live sets to
duke@435 366 // live-IN info, not live-OUT info.
duke@435 367 uint k;
duke@435 368 for( k=0; k < b->_num_succs; k++ )
duke@435 369 if( _phc._live->live(b->_succs[k])->member( nidx ) )
duke@435 370 break; // Live in to some successor block?
duke@435 371 if( k < b->_num_succs )
duke@435 372 continue; // Live out; do not pre-split
duke@435 373 // Split the lrg at this use
duke@435 374 const RegMask *rm = C->matcher()->idealreg2spillmask[inp->ideal_reg()];
duke@435 375 Node *copy = new (C) MachSpillCopyNode( inp, *rm, *rm );
duke@435 376 // Insert the copy in the use-def chain
duke@435 377 n->set_req(inpidx, copy );
duke@435 378 // Insert the copy in the basic block, just before us
adlertz@5635 379 b->insert_node(copy, l++);
duke@435 380 // Extend ("register allocate") the names array for the copy.
neliasso@4949 381 uint max_lrg_id = _phc._lrg_map.max_lrg_id();
neliasso@4949 382 _phc.new_lrg(copy, max_lrg_id);
neliasso@4949 383 _phc._lrg_map.set_max_lrg_id(max_lrg_id + 1);
adlertz@5509 384 _phc._cfg.map_node_to_block(copy, b);
duke@435 385 //tty->print_cr("Split a debug use in Aggressive Coalesce");
duke@435 386 } // End of if high frequency use/def
duke@435 387 } // End of for all debug inputs
duke@435 388 } // End of if low frequency safepoint
duke@435 389
duke@435 390 } // End of if Phi
duke@435 391
duke@435 392 } // End of for all instructions
duke@435 393 } // End of for all blocks
duke@435 394 }
duke@435 395
adlertz@5539 396
duke@435 397 // Aggressive (but pessimistic) copy coalescing of a single block
duke@435 398
duke@435 399 // The following coalesce pass represents a single round of aggressive
duke@435 400 // pessimistic coalesce. "Aggressive" means no attempt to preserve
duke@435 401 // colorability when coalescing. This occasionally means more spills, but
duke@435 402 // it also means fewer rounds of coalescing for better code - and that means
duke@435 403 // faster compiles.
duke@435 404
duke@435 405 // "Pessimistic" means we do not hit the fixed point in one pass (and we are
duke@435 406 // reaching for the least fixed point to boot). This is typically solved
duke@435 407 // with a few more rounds of coalescing, but the compiler must run fast. We
duke@435 408 // could optimistically coalescing everything touching PhiNodes together
duke@435 409 // into one big live range, then check for self-interference. Everywhere
duke@435 410 // the live range interferes with self it would have to be split. Finding
duke@435 411 // the right split points can be done with some heuristics (based on
duke@435 412 // expected frequency of edges in the live range). In short, it's a real
duke@435 413 // research problem and the timeline is too short to allow such research.
duke@435 414 // Further thoughts: (1) build the LR in a pass, (2) find self-interference
duke@435 415 // in another pass, (3) per each self-conflict, split, (4) split by finding
duke@435 416 // the low-cost cut (min-cut) of the LR, (5) edges in the LR are weighted
duke@435 417 // according to the GCM algorithm (or just exec freq on CFG edges).
duke@435 418
duke@435 419 void PhaseAggressiveCoalesce::coalesce( Block *b ) {
duke@435 420 // Copies are still "virtual" - meaning we have not made them explicitly
duke@435 421 // copies. Instead, Phi functions of successor blocks have mis-matched
duke@435 422 // live-ranges. If I fail to coalesce, I'll have to insert a copy to line
duke@435 423 // up the live-ranges. Check for Phis in successor blocks.
duke@435 424 uint i;
duke@435 425 for( i=0; i<b->_num_succs; i++ ) {
duke@435 426 Block *bs = b->_succs[i];
duke@435 427 // Find index of 'b' in 'bs' predecessors
duke@435 428 uint j=1;
adlertz@5509 429 while (_phc._cfg.get_block_for_node(bs->pred(j)) != b) {
adlertz@5509 430 j++;
adlertz@5509 431 }
adlertz@5509 432
duke@435 433 // Visit all the Phis in successor block
adlertz@5635 434 for( uint k = 1; k<bs->number_of_nodes(); k++ ) {
adlertz@5635 435 Node *n = bs->get_node(k);
duke@435 436 if( !n->is_Phi() ) break;
duke@435 437 combine_these_two( n, n->in(j) );
duke@435 438 }
duke@435 439 } // End of for all successor blocks
duke@435 440
duke@435 441
duke@435 442 // Check _this_ block for 2-address instructions and copies.
duke@435 443 uint cnt = b->end_idx();
duke@435 444 for( i = 1; i<cnt; i++ ) {
adlertz@5635 445 Node *n = b->get_node(i);
duke@435 446 uint idx;
duke@435 447 // 2-address instructions have a virtual Copy matching their input
duke@435 448 // to their output
neliasso@4949 449 if (n->is_Mach() && (idx = n->as_Mach()->two_adr())) {
duke@435 450 MachNode *mach = n->as_Mach();
neliasso@4949 451 combine_these_two(mach, mach->in(idx));
duke@435 452 }
duke@435 453 } // End of for all instructions in block
duke@435 454 }
duke@435 455
neliasso@4949 456 PhaseConservativeCoalesce::PhaseConservativeCoalesce(PhaseChaitin &chaitin) : PhaseCoalesce(chaitin) {
neliasso@4949 457 _ulr.initialize(_phc._lrg_map.max_lrg_id());
duke@435 458 }
duke@435 459
duke@435 460 void PhaseConservativeCoalesce::verify() {
duke@435 461 #ifdef ASSERT
duke@435 462 _phc.set_was_low();
duke@435 463 #endif
duke@435 464 }
duke@435 465
duke@435 466 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 467 // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the
duke@435 468 // union-find tree
duke@435 469 _phc.Union( lr1_node, lr2_node );
duke@435 470
duke@435 471 // Single-def live range ONLY if both live ranges are single-def.
duke@435 472 // If both are single def, then src_def powers one live range
duke@435 473 // and def_copy powers the other. After merging, src_def powers
duke@435 474 // the combined live range.
never@730 475 lrgs(lr1)._def = (lrgs(lr1).is_multidef() ||
never@730 476 lrgs(lr2).is_multidef() )
duke@435 477 ? NodeSentinel : src_def;
duke@435 478 lrgs(lr2)._def = NULL; // No def for lrg 2
duke@435 479 lrgs(lr2).Clear(); // Force empty mask for LRG 2
duke@435 480 //lrgs(lr2)._size = 0; // Live-range 2 goes dead
duke@435 481 lrgs(lr1)._is_oop |= lrgs(lr2)._is_oop;
duke@435 482 lrgs(lr2)._is_oop = 0; // In particular, not an oop for GC info
duke@435 483
duke@435 484 if (lrgs(lr1)._maxfreq < lrgs(lr2)._maxfreq)
duke@435 485 lrgs(lr1)._maxfreq = lrgs(lr2)._maxfreq;
duke@435 486
duke@435 487 // Copy original value instead. Intermediate copies go dead, and
duke@435 488 // the dst_copy becomes useless.
duke@435 489 int didx = dst_copy->is_Copy();
duke@435 490 dst_copy->set_req( didx, src_def );
duke@435 491 // Add copy to free list
duke@435 492 // _phc.free_spillcopy(b->_nodes[bindex]);
adlertz@5635 493 assert( b->get_node(bindex) == dst_copy, "" );
duke@435 494 dst_copy->replace_by( dst_copy->in(didx) );
duke@435 495 dst_copy->set_req( didx, NULL);
adlertz@5635 496 b->remove_node(bindex);
duke@435 497 if( bindex < b->_ihrp_index ) b->_ihrp_index--;
duke@435 498 if( bindex < b->_fhrp_index ) b->_fhrp_index--;
duke@435 499
duke@435 500 // Stretched lr1; add it to liveness of intermediate blocks
adlertz@5509 501 Block *b2 = _phc._cfg.get_block_for_node(src_copy);
duke@435 502 while( b != b2 ) {
adlertz@5509 503 b = _phc._cfg.get_block_for_node(b->pred(1));
duke@435 504 _phc._live->live(b)->insert(lr1);
duke@435 505 }
duke@435 506 }
duke@435 507
duke@435 508 // Factored code from copy_copy that computes extra interferences from
duke@435 509 // lengthening a live range by double-coalescing.
duke@435 510 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 511
duke@435 512 assert(!lrgs(lr1)._fat_proj, "cannot coalesce fat_proj");
duke@435 513 assert(!lrgs(lr2)._fat_proj, "cannot coalesce fat_proj");
duke@435 514 Node *prev_copy = dst_copy->in(dst_copy->is_Copy());
duke@435 515 Block *b2 = b;
duke@435 516 uint bindex2 = bindex;
duke@435 517 while( 1 ) {
duke@435 518 // Find previous instruction
duke@435 519 bindex2--; // Chain backwards 1 instruction
duke@435 520 while( bindex2 == 0 ) { // At block start, find prior block
duke@435 521 assert( b2->num_preds() == 2, "cannot double coalesce across c-flow" );
adlertz@5509 522 b2 = _phc._cfg.get_block_for_node(b2->pred(1));
duke@435 523 bindex2 = b2->end_idx()-1;
duke@435 524 }
duke@435 525 // Get prior instruction
adlertz@5635 526 assert(bindex2 < b2->number_of_nodes(), "index out of bounds");
adlertz@5635 527 Node *x = b2->get_node(bindex2);
duke@435 528 if( x == prev_copy ) { // Previous copy in copy chain?
duke@435 529 if( prev_copy == src_copy)// Found end of chain and all interferences
duke@435 530 break; // So break out of loop
duke@435 531 // Else work back one in copy chain
duke@435 532 prev_copy = prev_copy->in(prev_copy->is_Copy());
duke@435 533 } else { // Else collect interferences
neliasso@4949 534 uint lidx = _phc._lrg_map.find(x);
duke@435 535 // Found another def of live-range being stretched?
neliasso@4949 536 if(lidx == lr1) {
neliasso@4949 537 return max_juint;
neliasso@4949 538 }
neliasso@4949 539 if(lidx == lr2) {
neliasso@4949 540 return max_juint;
neliasso@4949 541 }
duke@435 542
duke@435 543 // If we attempt to coalesce across a bound def
duke@435 544 if( lrgs(lidx).is_bound() ) {
duke@435 545 // Do not let the coalesced LRG expect to get the bound color
duke@435 546 rm.SUBTRACT( lrgs(lidx).mask() );
duke@435 547 // Recompute rm_size
duke@435 548 rm_size = rm.Size();
duke@435 549 //if( rm._flags ) rm_size += 1000000;
duke@435 550 if( reg_degree >= rm_size ) return max_juint;
duke@435 551 }
duke@435 552 if( rm.overlap(lrgs(lidx).mask()) ) {
duke@435 553 // Insert lidx into union LRG; returns TRUE if actually inserted
duke@435 554 if( _ulr.insert(lidx) ) {
duke@435 555 // Infinite-stack neighbors do not alter colorability, as they
duke@435 556 // can always color to some other color.
duke@435 557 if( !lrgs(lidx).mask().is_AllStack() ) {
duke@435 558 // If this coalesce will make any new neighbor uncolorable,
duke@435 559 // do not coalesce.
duke@435 560 if( lrgs(lidx).just_lo_degree() )
duke@435 561 return max_juint;
duke@435 562 // Bump our degree
duke@435 563 if( ++reg_degree >= rm_size )
duke@435 564 return max_juint;
duke@435 565 } // End of if not infinite-stack neighbor
duke@435 566 } // End of if actually inserted
duke@435 567 } // End of if live range overlaps
twisti@1040 568 } // End of else collect interferences for 1 node
twisti@1040 569 } // End of while forever, scan back for interferences
duke@435 570 return reg_degree;
duke@435 571 }
duke@435 572
duke@435 573 void PhaseConservativeCoalesce::update_ifg(uint lr1, uint lr2, IndexSet *n_lr1, IndexSet *n_lr2) {
duke@435 574 // Some original neighbors of lr1 might have gone away
duke@435 575 // because the constrained register mask prevented them.
duke@435 576 // Remove lr1 from such neighbors.
duke@435 577 IndexSetIterator one(n_lr1);
duke@435 578 uint neighbor;
duke@435 579 LRG &lrg1 = lrgs(lr1);
duke@435 580 while ((neighbor = one.next()) != 0)
duke@435 581 if( !_ulr.member(neighbor) )
duke@435 582 if( _phc._ifg->neighbors(neighbor)->remove(lr1) )
duke@435 583 lrgs(neighbor).inc_degree( -lrg1.compute_degree(lrgs(neighbor)) );
duke@435 584
duke@435 585
duke@435 586 // lr2 is now called (coalesced into) lr1.
duke@435 587 // Remove lr2 from the IFG.
duke@435 588 IndexSetIterator two(n_lr2);
duke@435 589 LRG &lrg2 = lrgs(lr2);
duke@435 590 while ((neighbor = two.next()) != 0)
duke@435 591 if( _phc._ifg->neighbors(neighbor)->remove(lr2) )
duke@435 592 lrgs(neighbor).inc_degree( -lrg2.compute_degree(lrgs(neighbor)) );
duke@435 593
duke@435 594 // Some neighbors of intermediate copies now interfere with the
duke@435 595 // combined live range.
duke@435 596 IndexSetIterator three(&_ulr);
duke@435 597 while ((neighbor = three.next()) != 0)
duke@435 598 if( _phc._ifg->neighbors(neighbor)->insert(lr1) )
duke@435 599 lrgs(neighbor).inc_degree( lrg1.compute_degree(lrgs(neighbor)) );
duke@435 600 }
duke@435 601
duke@435 602 static void record_bias( const PhaseIFG *ifg, int lr1, int lr2 ) {
duke@435 603 // Tag copy bias here
duke@435 604 if( !ifg->lrgs(lr1)._copy_bias )
duke@435 605 ifg->lrgs(lr1)._copy_bias = lr2;
duke@435 606 if( !ifg->lrgs(lr2)._copy_bias )
duke@435 607 ifg->lrgs(lr2)._copy_bias = lr1;
duke@435 608 }
duke@435 609
duke@435 610 // See if I can coalesce a series of multiple copies together. I need the
duke@435 611 // final dest copy and the original src copy. They can be the same Node.
duke@435 612 // Compute the compatible register masks.
neliasso@4949 613 bool PhaseConservativeCoalesce::copy_copy(Node *dst_copy, Node *src_copy, Block *b, uint bindex) {
duke@435 614
neliasso@4949 615 if (!dst_copy->is_SpillCopy()) {
neliasso@4949 616 return false;
neliasso@4949 617 }
neliasso@4949 618 if (!src_copy->is_SpillCopy()) {
neliasso@4949 619 return false;
neliasso@4949 620 }
duke@435 621 Node *src_def = src_copy->in(src_copy->is_Copy());
neliasso@4949 622 uint lr1 = _phc._lrg_map.find(dst_copy);
neliasso@4949 623 uint lr2 = _phc._lrg_map.find(src_def);
duke@435 624
duke@435 625 // Same live ranges already?
neliasso@4949 626 if (lr1 == lr2) {
neliasso@4949 627 return false;
neliasso@4949 628 }
duke@435 629
duke@435 630 // Interfere?
neliasso@4949 631 if (_phc._ifg->test_edge_sq(lr1, lr2)) {
neliasso@4949 632 return false;
neliasso@4949 633 }
duke@435 634
duke@435 635 // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK.
neliasso@4949 636 if (!lrgs(lr1)._is_oop && lrgs(lr2)._is_oop) { // not an oop->int cast
duke@435 637 return false;
neliasso@4949 638 }
duke@435 639
duke@435 640 // Coalescing between an aligned live range and a mis-aligned live range?
duke@435 641 // No, no! Alignment changes how we count degree.
neliasso@4949 642 if (lrgs(lr1)._fat_proj != lrgs(lr2)._fat_proj) {
duke@435 643 return false;
neliasso@4949 644 }
duke@435 645
duke@435 646 // Sort; use smaller live-range number
duke@435 647 Node *lr1_node = dst_copy;
duke@435 648 Node *lr2_node = src_def;
neliasso@4949 649 if (lr1 > lr2) {
duke@435 650 uint tmp = lr1; lr1 = lr2; lr2 = tmp;
duke@435 651 lr1_node = src_def; lr2_node = dst_copy;
duke@435 652 }
duke@435 653
duke@435 654 // Check for compatibility of the 2 live ranges by
duke@435 655 // intersecting their allowed register sets.
duke@435 656 RegMask rm = lrgs(lr1).mask();
duke@435 657 rm.AND(lrgs(lr2).mask());
duke@435 658 // Number of bits free
duke@435 659 uint rm_size = rm.Size();
duke@435 660
never@2085 661 if (UseFPUForSpilling && rm.is_AllStack() ) {
never@2085 662 // Don't coalesce when frequency difference is large
adlertz@5509 663 Block *dst_b = _phc._cfg.get_block_for_node(dst_copy);
adlertz@5509 664 Block *src_def_b = _phc._cfg.get_block_for_node(src_def);
never@2085 665 if (src_def_b->_freq > 10*dst_b->_freq )
never@2085 666 return false;
never@2085 667 }
never@2085 668
duke@435 669 // If we can use any stack slot, then effective size is infinite
duke@435 670 if( rm.is_AllStack() ) rm_size += 1000000;
duke@435 671 // Incompatible masks, no way to coalesce
duke@435 672 if( rm_size == 0 ) return false;
duke@435 673
duke@435 674 // Another early bail-out test is when we are double-coalescing and the
twisti@1040 675 // 2 copies are separated by some control flow.
duke@435 676 if( dst_copy != src_copy ) {
adlertz@5509 677 Block *src_b = _phc._cfg.get_block_for_node(src_copy);
duke@435 678 Block *b2 = b;
duke@435 679 while( b2 != src_b ) {
duke@435 680 if( b2->num_preds() > 2 ){// Found merge-point
duke@435 681 _phc._lost_opp_cflow_coalesce++;
duke@435 682 // extra record_bias commented out because Chris believes it is not
duke@435 683 // productive. Since we can record only 1 bias, we want to choose one
duke@435 684 // that stands a chance of working and this one probably does not.
duke@435 685 //record_bias( _phc._lrgs, lr1, lr2 );
duke@435 686 return false; // To hard to find all interferences
duke@435 687 }
adlertz@5509 688 b2 = _phc._cfg.get_block_for_node(b2->pred(1));
duke@435 689 }
duke@435 690 }
duke@435 691
duke@435 692 // Union the two interference sets together into '_ulr'
duke@435 693 uint reg_degree = _ulr.lrg_union( lr1, lr2, rm_size, _phc._ifg, rm );
duke@435 694
duke@435 695 if( reg_degree >= rm_size ) {
duke@435 696 record_bias( _phc._ifg, lr1, lr2 );
duke@435 697 return false;
duke@435 698 }
duke@435 699
duke@435 700 // Now I need to compute all the interferences between dst_copy and
duke@435 701 // src_copy. I'm not willing visit the entire interference graph, so
duke@435 702 // I limit my search to things in dst_copy's block or in a straight
duke@435 703 // line of previous blocks. I give up at merge points or when I get
duke@435 704 // more interferences than my degree. I can stop when I find src_copy.
duke@435 705 if( dst_copy != src_copy ) {
duke@435 706 reg_degree = compute_separating_interferences(dst_copy, src_copy, b, bindex, rm, rm_size, reg_degree, lr1, lr2 );
duke@435 707 if( reg_degree == max_juint ) {
duke@435 708 record_bias( _phc._ifg, lr1, lr2 );
duke@435 709 return false;
duke@435 710 }
duke@435 711 } // End of if dst_copy & src_copy are different
duke@435 712
duke@435 713
duke@435 714 // ---- THE COMBINED LRG IS COLORABLE ----
duke@435 715
duke@435 716 // YEAH - Now coalesce this copy away
duke@435 717 assert( lrgs(lr1).num_regs() == lrgs(lr2).num_regs(), "" );
duke@435 718
duke@435 719 IndexSet *n_lr1 = _phc._ifg->neighbors(lr1);
duke@435 720 IndexSet *n_lr2 = _phc._ifg->neighbors(lr2);
duke@435 721
duke@435 722 // Update the interference graph
duke@435 723 update_ifg(lr1, lr2, n_lr1, n_lr2);
duke@435 724
duke@435 725 _ulr.remove(lr1);
duke@435 726
duke@435 727 // Uncomment the following code to trace Coalescing in great detail.
duke@435 728 //
duke@435 729 //if (false) {
duke@435 730 // tty->cr();
duke@435 731 // tty->print_cr("#######################################");
duke@435 732 // tty->print_cr("union %d and %d", lr1, lr2);
duke@435 733 // n_lr1->dump();
duke@435 734 // n_lr2->dump();
duke@435 735 // tty->print_cr("resulting set is");
duke@435 736 // _ulr.dump();
duke@435 737 //}
duke@435 738
duke@435 739 // Replace n_lr1 with the new combined live range. _ulr will use
duke@435 740 // n_lr1's old memory on the next iteration. n_lr2 is cleared to
duke@435 741 // send its internal memory to the free list.
duke@435 742 _ulr.swap(n_lr1);
duke@435 743 _ulr.clear();
duke@435 744 n_lr2->clear();
duke@435 745
duke@435 746 lrgs(lr1).set_degree( _phc._ifg->effective_degree(lr1) );
duke@435 747 lrgs(lr2).set_degree( 0 );
duke@435 748
duke@435 749 // Join live ranges. Merge larger into smaller. Union lr2 into lr1 in the
duke@435 750 // union-find tree
duke@435 751 union_helper( lr1_node, lr2_node, lr1, lr2, src_def, dst_copy, src_copy, b, bindex );
duke@435 752 // Combine register restrictions
duke@435 753 lrgs(lr1).set_mask(rm);
duke@435 754 lrgs(lr1).compute_set_mask_size();
duke@435 755 lrgs(lr1)._cost += lrgs(lr2)._cost;
duke@435 756 lrgs(lr1)._area += lrgs(lr2)._area;
duke@435 757
duke@435 758 // While its uncommon to successfully coalesce live ranges that started out
duke@435 759 // being not-lo-degree, it can happen. In any case the combined coalesced
duke@435 760 // live range better Simplify nicely.
duke@435 761 lrgs(lr1)._was_lo = 1;
duke@435 762
duke@435 763 // kinda expensive to do all the time
duke@435 764 //tty->print_cr("warning: slow verify happening");
duke@435 765 //_phc._ifg->verify( &_phc );
duke@435 766 return true;
duke@435 767 }
duke@435 768
duke@435 769 // Conservative (but pessimistic) copy coalescing of a single block
duke@435 770 void PhaseConservativeCoalesce::coalesce( Block *b ) {
duke@435 771 // Bail out on infrequent blocks
adlertz@5639 772 if (_phc._cfg.is_uncommon(b)) {
duke@435 773 return;
adlertz@5509 774 }
duke@435 775 // Check this block for copies.
duke@435 776 for( uint i = 1; i<b->end_idx(); i++ ) {
duke@435 777 // Check for actual copies on inputs. Coalesce a copy into its
duke@435 778 // input if use and copy's input are compatible.
adlertz@5635 779 Node *copy1 = b->get_node(i);
duke@435 780 uint idx1 = copy1->is_Copy();
duke@435 781 if( !idx1 ) continue; // Not a copy
duke@435 782
duke@435 783 if( copy_copy(copy1,copy1,b,i) ) {
duke@435 784 i--; // Retry, same location in block
duke@435 785 PhaseChaitin::_conserv_coalesce++; // Collect stats on success
duke@435 786 continue;
duke@435 787 }
duke@435 788 }
duke@435 789 }

mercurial