src/share/vm/opto/coalesce.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial