src/share/vm/opto/postaloc.cpp

Mon, 04 Jan 2010 18:38:08 +0100

author
twisti
date
Mon, 04 Jan 2010 18:38:08 +0100
changeset 1570
e66fd840cb6b
parent 1383
89e0543e1737
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6893081: method handle & invokedynamic code needs additional cleanup (post 6815692, 6858164)
Summary: During the work for 6829187 we have fixed a number of basic bugs which are logically grouped with 6815692 and 6858164 but which must be reviewed and pushed separately.
Reviewed-by: kvn, never

duke@435 1 /*
xdono@1383 2 * Copyright 1998-2009 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_postaloc.cpp.incl"
duke@435 27
duke@435 28 // see if this register kind does not requires two registers
duke@435 29 static bool is_single_register(uint x) {
duke@435 30 #ifdef _LP64
duke@435 31 return (x != Op_RegD && x != Op_RegL && x != Op_RegP);
duke@435 32 #else
duke@435 33 return (x != Op_RegD && x != Op_RegL);
duke@435 34 #endif
duke@435 35 }
duke@435 36
kvn@835 37 //---------------------------may_be_copy_of_callee-----------------------------
duke@435 38 // Check to see if we can possibly be a copy of a callee-save value.
duke@435 39 bool PhaseChaitin::may_be_copy_of_callee( Node *def ) const {
duke@435 40 // Short circuit if there are no callee save registers
duke@435 41 if (_matcher.number_of_saved_registers() == 0) return false;
duke@435 42
duke@435 43 // Expect only a spill-down and reload on exit for callee-save spills.
duke@435 44 // Chains of copies cannot be deep.
duke@435 45 // 5008997 - This is wishful thinking. Register allocator seems to
duke@435 46 // be splitting live ranges for callee save registers to such
duke@435 47 // an extent that in large methods the chains can be very long
duke@435 48 // (50+). The conservative answer is to return true if we don't
twisti@1040 49 // know as this prevents optimizations from occurring.
duke@435 50
duke@435 51 const int limit = 60;
duke@435 52 int i;
duke@435 53 for( i=0; i < limit; i++ ) {
duke@435 54 if( def->is_Proj() && def->in(0)->is_Start() &&
duke@435 55 _matcher.is_save_on_entry(lrgs(n2lidx(def)).reg()) )
duke@435 56 return true; // Direct use of callee-save proj
duke@435 57 if( def->is_Copy() ) // Copies carry value through
duke@435 58 def = def->in(def->is_Copy());
duke@435 59 else if( def->is_Phi() ) // Phis can merge it from any direction
duke@435 60 def = def->in(1);
duke@435 61 else
duke@435 62 break;
duke@435 63 guarantee(def != NULL, "must not resurrect dead copy");
duke@435 64 }
duke@435 65 // If we reached the end and didn't find a callee save proj
duke@435 66 // then this may be a callee save proj so we return true
duke@435 67 // as the conservative answer. If we didn't reach then end
duke@435 68 // we must have discovered that it was not a callee save
duke@435 69 // else we would have returned.
duke@435 70 return i == limit;
duke@435 71 }
duke@435 72
duke@435 73
duke@435 74
duke@435 75 //------------------------------yank_if_dead-----------------------------------
duke@435 76 // Removed an edge from 'old'. Yank if dead. Return adjustment counts to
duke@435 77 // iterators in the current block.
duke@435 78 int PhaseChaitin::yank_if_dead( Node *old, Block *current_block, Node_List *value, Node_List *regnd ) {
duke@435 79 int blk_adjust=0;
duke@435 80 while (old->outcnt() == 0 && old != C->top()) {
duke@435 81 Block *oldb = _cfg._bbs[old->_idx];
duke@435 82 oldb->find_remove(old);
duke@435 83 // Count 1 if deleting an instruction from the current block
duke@435 84 if( oldb == current_block ) blk_adjust++;
duke@435 85 _cfg._bbs.map(old->_idx,NULL);
duke@435 86 OptoReg::Name old_reg = lrgs(n2lidx(old)).reg();
duke@435 87 if( regnd && (*regnd)[old_reg]==old ) { // Instruction is currently available?
duke@435 88 value->map(old_reg,NULL); // Yank from value/regnd maps
duke@435 89 regnd->map(old_reg,NULL); // This register's value is now unknown
duke@435 90 }
never@1358 91 assert(old->req() <= 2, "can't handle more inputs");
duke@435 92 Node *tmp = old->req() > 1 ? old->in(1) : NULL;
duke@435 93 old->disconnect_inputs(NULL);
duke@435 94 if( !tmp ) break;
duke@435 95 old = tmp;
duke@435 96 }
duke@435 97 return blk_adjust;
duke@435 98 }
duke@435 99
duke@435 100 //------------------------------use_prior_register-----------------------------
duke@435 101 // Use the prior value instead of the current value, in an effort to make
duke@435 102 // the current value go dead. Return block iterator adjustment, in case
duke@435 103 // we yank some instructions from this block.
duke@435 104 int PhaseChaitin::use_prior_register( Node *n, uint idx, Node *def, Block *current_block, Node_List &value, Node_List &regnd ) {
duke@435 105 // No effect?
duke@435 106 if( def == n->in(idx) ) return 0;
duke@435 107 // Def is currently dead and can be removed? Do not resurrect
duke@435 108 if( def->outcnt() == 0 ) return 0;
duke@435 109
duke@435 110 // Not every pair of physical registers are assignment compatible,
duke@435 111 // e.g. on sparc floating point registers are not assignable to integer
duke@435 112 // registers.
duke@435 113 const LRG &def_lrg = lrgs(n2lidx(def));
duke@435 114 OptoReg::Name def_reg = def_lrg.reg();
duke@435 115 const RegMask &use_mask = n->in_RegMask(idx);
duke@435 116 bool can_use = ( RegMask::can_represent(def_reg) ? (use_mask.Member(def_reg) != 0)
duke@435 117 : (use_mask.is_AllStack() != 0));
duke@435 118 // Check for a copy to or from a misaligned pair.
duke@435 119 can_use = can_use && !use_mask.is_misaligned_Pair() && !def_lrg.mask().is_misaligned_Pair();
duke@435 120
duke@435 121 if (!can_use)
duke@435 122 return 0;
duke@435 123
duke@435 124 // Capture the old def in case it goes dead...
duke@435 125 Node *old = n->in(idx);
duke@435 126
duke@435 127 // Save-on-call copies can only be elided if the entire copy chain can go
duke@435 128 // away, lest we get the same callee-save value alive in 2 locations at
duke@435 129 // once. We check for the obvious trivial case here. Although it can
duke@435 130 // sometimes be elided with cooperation outside our scope, here we will just
duke@435 131 // miss the opportunity. :-(
duke@435 132 if( may_be_copy_of_callee(def) ) {
duke@435 133 if( old->outcnt() > 1 ) return 0; // We're the not last user
duke@435 134 int idx = old->is_Copy();
duke@435 135 assert( idx, "chain of copies being removed" );
duke@435 136 Node *old2 = old->in(idx); // Chain of copies
duke@435 137 if( old2->outcnt() > 1 ) return 0; // old is not the last user
duke@435 138 int idx2 = old2->is_Copy();
duke@435 139 if( !idx2 ) return 0; // Not a chain of 2 copies
duke@435 140 if( def != old2->in(idx2) ) return 0; // Chain of exactly 2 copies
duke@435 141 }
duke@435 142
duke@435 143 // Use the new def
duke@435 144 n->set_req(idx,def);
duke@435 145 _post_alloc++;
duke@435 146
duke@435 147 // Is old def now dead? We successfully yanked a copy?
duke@435 148 return yank_if_dead(old,current_block,&value,&regnd);
duke@435 149 }
duke@435 150
duke@435 151
duke@435 152 //------------------------------skip_copies------------------------------------
duke@435 153 // Skip through any number of copies (that don't mod oop-i-ness)
duke@435 154 Node *PhaseChaitin::skip_copies( Node *c ) {
duke@435 155 int idx = c->is_Copy();
duke@435 156 uint is_oop = lrgs(n2lidx(c))._is_oop;
duke@435 157 while (idx != 0) {
duke@435 158 guarantee(c->in(idx) != NULL, "must not resurrect dead copy");
duke@435 159 if (lrgs(n2lidx(c->in(idx)))._is_oop != is_oop)
duke@435 160 break; // casting copy, not the same value
duke@435 161 c = c->in(idx);
duke@435 162 idx = c->is_Copy();
duke@435 163 }
duke@435 164 return c;
duke@435 165 }
duke@435 166
duke@435 167 //------------------------------elide_copy-------------------------------------
duke@435 168 // Remove (bypass) copies along Node n, edge k.
duke@435 169 int PhaseChaitin::elide_copy( Node *n, int k, Block *current_block, Node_List &value, Node_List &regnd, bool can_change_regs ) {
duke@435 170 int blk_adjust = 0;
duke@435 171
duke@435 172 uint nk_idx = n2lidx(n->in(k));
duke@435 173 OptoReg::Name nk_reg = lrgs(nk_idx ).reg();
duke@435 174
duke@435 175 // Remove obvious same-register copies
duke@435 176 Node *x = n->in(k);
duke@435 177 int idx;
duke@435 178 while( (idx=x->is_Copy()) != 0 ) {
duke@435 179 Node *copy = x->in(idx);
duke@435 180 guarantee(copy != NULL, "must not resurrect dead copy");
duke@435 181 if( lrgs(n2lidx(copy)).reg() != nk_reg ) break;
duke@435 182 blk_adjust += use_prior_register(n,k,copy,current_block,value,regnd);
duke@435 183 if( n->in(k) != copy ) break; // Failed for some cutout?
duke@435 184 x = copy; // Progress, try again
duke@435 185 }
duke@435 186
duke@435 187 // Phis and 2-address instructions cannot change registers so easily - their
duke@435 188 // outputs must match their input.
duke@435 189 if( !can_change_regs )
duke@435 190 return blk_adjust; // Only check stupid copies!
duke@435 191
duke@435 192 // Loop backedges won't have a value-mapping yet
duke@435 193 if( &value == NULL ) return blk_adjust;
duke@435 194
duke@435 195 // Skip through all copies to the _value_ being used. Do not change from
duke@435 196 // int to pointer. This attempts to jump through a chain of copies, where
duke@435 197 // intermediate copies might be illegal, i.e., value is stored down to stack
duke@435 198 // then reloaded BUT survives in a register the whole way.
duke@435 199 Node *val = skip_copies(n->in(k));
duke@435 200
duke@435 201 if( val == x ) return blk_adjust; // No progress?
duke@435 202
duke@435 203 bool single = is_single_register(val->ideal_reg());
duke@435 204 uint val_idx = n2lidx(val);
duke@435 205 OptoReg::Name val_reg = lrgs(val_idx).reg();
duke@435 206
duke@435 207 // See if it happens to already be in the correct register!
duke@435 208 // (either Phi's direct register, or the common case of the name
duke@435 209 // never-clobbered original-def register)
duke@435 210 if( value[val_reg] == val &&
duke@435 211 // Doubles check both halves
duke@435 212 ( single || value[val_reg-1] == val ) ) {
duke@435 213 blk_adjust += use_prior_register(n,k,regnd[val_reg],current_block,value,regnd);
duke@435 214 if( n->in(k) == regnd[val_reg] ) // Success! Quit trying
duke@435 215 return blk_adjust;
duke@435 216 }
duke@435 217
duke@435 218 // See if we can skip the copy by changing registers. Don't change from
duke@435 219 // using a register to using the stack unless we know we can remove a
duke@435 220 // copy-load. Otherwise we might end up making a pile of Intel cisc-spill
duke@435 221 // ops reading from memory instead of just loading once and using the
duke@435 222 // register.
duke@435 223
duke@435 224 // Also handle duplicate copies here.
duke@435 225 const Type *t = val->is_Con() ? val->bottom_type() : NULL;
duke@435 226
duke@435 227 // Scan all registers to see if this value is around already
duke@435 228 for( uint reg = 0; reg < (uint)_max_reg; reg++ ) {
kvn@835 229 if (reg == (uint)nk_reg) {
kvn@835 230 // Found ourselves so check if there is only one user of this
kvn@835 231 // copy and keep on searching for a better copy if so.
kvn@835 232 bool ignore_self = true;
kvn@835 233 x = n->in(k);
kvn@835 234 DUIterator_Fast imax, i = x->fast_outs(imax);
kvn@835 235 Node* first = x->fast_out(i); i++;
kvn@835 236 while (i < imax && ignore_self) {
kvn@835 237 Node* use = x->fast_out(i); i++;
kvn@835 238 if (use != first) ignore_self = false;
kvn@835 239 }
kvn@835 240 if (ignore_self) continue;
kvn@835 241 }
kvn@835 242
duke@435 243 Node *vv = value[reg];
duke@435 244 if( !single ) { // Doubles check for aligned-adjacent pair
duke@435 245 if( (reg&1)==0 ) continue; // Wrong half of a pair
duke@435 246 if( vv != value[reg-1] ) continue; // Not a complete pair
duke@435 247 }
duke@435 248 if( vv == val || // Got a direct hit?
duke@435 249 (t && vv && vv->bottom_type() == t && vv->is_Mach() &&
duke@435 250 vv->as_Mach()->rule() == val->as_Mach()->rule()) ) { // Or same constant?
duke@435 251 assert( !n->is_Phi(), "cannot change registers at a Phi so easily" );
duke@435 252 if( OptoReg::is_stack(nk_reg) || // CISC-loading from stack OR
duke@435 253 OptoReg::is_reg(reg) || // turning into a register use OR
duke@435 254 regnd[reg]->outcnt()==1 ) { // last use of a spill-load turns into a CISC use
duke@435 255 blk_adjust += use_prior_register(n,k,regnd[reg],current_block,value,regnd);
duke@435 256 if( n->in(k) == regnd[reg] ) // Success! Quit trying
duke@435 257 return blk_adjust;
duke@435 258 } // End of if not degrading to a stack
duke@435 259 } // End of if found value in another register
duke@435 260 } // End of scan all machine registers
duke@435 261 return blk_adjust;
duke@435 262 }
duke@435 263
duke@435 264
duke@435 265 //
duke@435 266 // Check if nreg already contains the constant value val. Normal copy
duke@435 267 // elimination doesn't doesn't work on constants because multiple
duke@435 268 // nodes can represent the same constant so the type and rule of the
duke@435 269 // MachNode must be checked to ensure equivalence.
duke@435 270 //
never@505 271 bool PhaseChaitin::eliminate_copy_of_constant(Node* val, Node* n,
never@505 272 Block *current_block,
duke@435 273 Node_List& value, Node_List& regnd,
duke@435 274 OptoReg::Name nreg, OptoReg::Name nreg2) {
duke@435 275 if (value[nreg] != val && val->is_Con() &&
duke@435 276 value[nreg] != NULL && value[nreg]->is_Con() &&
duke@435 277 (nreg2 == OptoReg::Bad || value[nreg] == value[nreg2]) &&
duke@435 278 value[nreg]->bottom_type() == val->bottom_type() &&
duke@435 279 value[nreg]->as_Mach()->rule() == val->as_Mach()->rule()) {
duke@435 280 // This code assumes that two MachNodes representing constants
duke@435 281 // which have the same rule and the same bottom type will produce
duke@435 282 // identical effects into a register. This seems like it must be
duke@435 283 // objectively true unless there are hidden inputs to the nodes
duke@435 284 // but if that were to change this code would need to updated.
duke@435 285 // Since they are equivalent the second one if redundant and can
duke@435 286 // be removed.
duke@435 287 //
never@505 288 // n will be replaced with the old value but n might have
duke@435 289 // kills projections associated with it so remove them now so that
twisti@1040 290 // yank_if_dead will be able to eliminate the copy once the uses
duke@435 291 // have been transferred to the old[value].
never@505 292 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
never@505 293 Node* use = n->fast_out(i);
duke@435 294 if (use->is_Proj() && use->outcnt() == 0) {
duke@435 295 // Kill projections have no users and one input
duke@435 296 use->set_req(0, C->top());
duke@435 297 yank_if_dead(use, current_block, &value, &regnd);
duke@435 298 --i; --imax;
duke@435 299 }
duke@435 300 }
duke@435 301 _post_alloc++;
duke@435 302 return true;
duke@435 303 }
duke@435 304 return false;
duke@435 305 }
duke@435 306
duke@435 307
duke@435 308 //------------------------------post_allocate_copy_removal---------------------
duke@435 309 // Post-Allocation peephole copy removal. We do this in 1 pass over the
duke@435 310 // basic blocks. We maintain a mapping of registers to Nodes (an array of
duke@435 311 // Nodes indexed by machine register or stack slot number). NULL means that a
duke@435 312 // register is not mapped to any Node. We can (want to have!) have several
duke@435 313 // registers map to the same Node. We walk forward over the instructions
duke@435 314 // updating the mapping as we go. At merge points we force a NULL if we have
duke@435 315 // to merge 2 different Nodes into the same register. Phi functions will give
duke@435 316 // us a new Node if there is a proper value merging. Since the blocks are
duke@435 317 // arranged in some RPO, we will visit all parent blocks before visiting any
duke@435 318 // successor blocks (except at loops).
duke@435 319 //
duke@435 320 // If we find a Copy we look to see if the Copy's source register is a stack
duke@435 321 // slot and that value has already been loaded into some machine register; if
duke@435 322 // so we use machine register directly. This turns a Load into a reg-reg
duke@435 323 // Move. We also look for reloads of identical constants.
duke@435 324 //
duke@435 325 // When we see a use from a reg-reg Copy, we will attempt to use the copy's
duke@435 326 // source directly and make the copy go dead.
duke@435 327 void PhaseChaitin::post_allocate_copy_removal() {
duke@435 328 NOT_PRODUCT( Compile::TracePhase t3("postAllocCopyRemoval", &_t_postAllocCopyRemoval, TimeCompiler); )
duke@435 329 ResourceMark rm;
duke@435 330
duke@435 331 // Need a mapping from basic block Node_Lists. We need a Node_List to
duke@435 332 // map from register number to value-producing Node.
duke@435 333 Node_List **blk2value = NEW_RESOURCE_ARRAY( Node_List *, _cfg._num_blocks+1);
duke@435 334 memset( blk2value, 0, sizeof(Node_List*)*(_cfg._num_blocks+1) );
duke@435 335 // Need a mapping from basic block Node_Lists. We need a Node_List to
duke@435 336 // map from register number to register-defining Node.
duke@435 337 Node_List **blk2regnd = NEW_RESOURCE_ARRAY( Node_List *, _cfg._num_blocks+1);
duke@435 338 memset( blk2regnd, 0, sizeof(Node_List*)*(_cfg._num_blocks+1) );
duke@435 339
duke@435 340 // We keep unused Node_Lists on a free_list to avoid wasting
duke@435 341 // memory.
duke@435 342 GrowableArray<Node_List*> free_list = GrowableArray<Node_List*>(16);
duke@435 343
duke@435 344 // For all blocks
duke@435 345 for( uint i = 0; i < _cfg._num_blocks; i++ ) {
duke@435 346 uint j;
duke@435 347 Block *b = _cfg._blocks[i];
duke@435 348
duke@435 349 // Count of Phis in block
duke@435 350 uint phi_dex;
duke@435 351 for( phi_dex = 1; phi_dex < b->_nodes.size(); phi_dex++ ) {
duke@435 352 Node *phi = b->_nodes[phi_dex];
duke@435 353 if( !phi->is_Phi() )
duke@435 354 break;
duke@435 355 }
duke@435 356
duke@435 357 // If any predecessor has not been visited, we do not know the state
duke@435 358 // of registers at the start. Check for this, while updating copies
duke@435 359 // along Phi input edges
duke@435 360 bool missing_some_inputs = false;
duke@435 361 Block *freed = NULL;
duke@435 362 for( j = 1; j < b->num_preds(); j++ ) {
duke@435 363 Block *pb = _cfg._bbs[b->pred(j)->_idx];
duke@435 364 // Remove copies along phi edges
duke@435 365 for( uint k=1; k<phi_dex; k++ )
duke@435 366 elide_copy( b->_nodes[k], j, b, *blk2value[pb->_pre_order], *blk2regnd[pb->_pre_order], false );
duke@435 367 if( blk2value[pb->_pre_order] ) { // Have a mapping on this edge?
duke@435 368 // See if this predecessor's mappings have been used by everybody
duke@435 369 // who wants them. If so, free 'em.
duke@435 370 uint k;
duke@435 371 for( k=0; k<pb->_num_succs; k++ ) {
duke@435 372 Block *pbsucc = pb->_succs[k];
duke@435 373 if( !blk2value[pbsucc->_pre_order] && pbsucc != b )
duke@435 374 break; // Found a future user
duke@435 375 }
duke@435 376 if( k >= pb->_num_succs ) { // No more uses, free!
duke@435 377 freed = pb; // Record last block freed
duke@435 378 free_list.push(blk2value[pb->_pre_order]);
duke@435 379 free_list.push(blk2regnd[pb->_pre_order]);
duke@435 380 }
duke@435 381 } else { // This block has unvisited (loopback) inputs
duke@435 382 missing_some_inputs = true;
duke@435 383 }
duke@435 384 }
duke@435 385
duke@435 386
duke@435 387 // Extract Node_List mappings. If 'freed' is non-zero, we just popped
duke@435 388 // 'freed's blocks off the list
duke@435 389 Node_List &regnd = *(free_list.is_empty() ? new Node_List() : free_list.pop());
duke@435 390 Node_List &value = *(free_list.is_empty() ? new Node_List() : free_list.pop());
duke@435 391 assert( !freed || blk2value[freed->_pre_order] == &value, "" );
duke@435 392 value.map(_max_reg,NULL);
duke@435 393 regnd.map(_max_reg,NULL);
duke@435 394 // Set mappings as OUR mappings
duke@435 395 blk2value[b->_pre_order] = &value;
duke@435 396 blk2regnd[b->_pre_order] = &regnd;
duke@435 397
duke@435 398 // Initialize value & regnd for this block
duke@435 399 if( missing_some_inputs ) {
duke@435 400 // Some predecessor has not yet been visited; zap map to empty
duke@435 401 for( uint k = 0; k < (uint)_max_reg; k++ ) {
duke@435 402 value.map(k,NULL);
duke@435 403 regnd.map(k,NULL);
duke@435 404 }
duke@435 405 } else {
duke@435 406 if( !freed ) { // Didn't get a freebie prior block
duke@435 407 // Must clone some data
duke@435 408 freed = _cfg._bbs[b->pred(1)->_idx];
duke@435 409 Node_List &f_value = *blk2value[freed->_pre_order];
duke@435 410 Node_List &f_regnd = *blk2regnd[freed->_pre_order];
duke@435 411 for( uint k = 0; k < (uint)_max_reg; k++ ) {
duke@435 412 value.map(k,f_value[k]);
duke@435 413 regnd.map(k,f_regnd[k]);
duke@435 414 }
duke@435 415 }
duke@435 416 // Merge all inputs together, setting to NULL any conflicts.
duke@435 417 for( j = 1; j < b->num_preds(); j++ ) {
duke@435 418 Block *pb = _cfg._bbs[b->pred(j)->_idx];
duke@435 419 if( pb == freed ) continue; // Did self already via freelist
duke@435 420 Node_List &p_regnd = *blk2regnd[pb->_pre_order];
duke@435 421 for( uint k = 0; k < (uint)_max_reg; k++ ) {
duke@435 422 if( regnd[k] != p_regnd[k] ) { // Conflict on reaching defs?
duke@435 423 value.map(k,NULL); // Then no value handy
duke@435 424 regnd.map(k,NULL);
duke@435 425 }
duke@435 426 }
duke@435 427 }
duke@435 428 }
duke@435 429
duke@435 430 // For all Phi's
duke@435 431 for( j = 1; j < phi_dex; j++ ) {
duke@435 432 uint k;
duke@435 433 Node *phi = b->_nodes[j];
duke@435 434 uint pidx = n2lidx(phi);
duke@435 435 OptoReg::Name preg = lrgs(n2lidx(phi)).reg();
duke@435 436
duke@435 437 // Remove copies remaining on edges. Check for junk phi.
duke@435 438 Node *u = NULL;
duke@435 439 for( k=1; k<phi->req(); k++ ) {
duke@435 440 Node *x = phi->in(k);
duke@435 441 if( phi != x && u != x ) // Found a different input
duke@435 442 u = u ? NodeSentinel : x; // Capture unique input, or NodeSentinel for 2nd input
duke@435 443 }
duke@435 444 if( u != NodeSentinel ) { // Junk Phi. Remove
duke@435 445 b->_nodes.remove(j--); phi_dex--;
duke@435 446 _cfg._bbs.map(phi->_idx,NULL);
duke@435 447 phi->replace_by(u);
duke@435 448 phi->disconnect_inputs(NULL);
duke@435 449 continue;
duke@435 450 }
duke@435 451 // Note that if value[pidx] exists, then we merged no new values here
duke@435 452 // and the phi is useless. This can happen even with the above phi
duke@435 453 // removal for complex flows. I cannot keep the better known value here
duke@435 454 // because locally the phi appears to define a new merged value. If I
duke@435 455 // keep the better value then a copy of the phi, being unable to use the
duke@435 456 // global flow analysis, can't "peek through" the phi to the original
duke@435 457 // reaching value and so will act like it's defining a new value. This
duke@435 458 // can lead to situations where some uses are from the old and some from
duke@435 459 // the new values. Not illegal by itself but throws the over-strong
duke@435 460 // assert in scheduling.
duke@435 461 if( pidx ) {
duke@435 462 value.map(preg,phi);
duke@435 463 regnd.map(preg,phi);
duke@435 464 OptoReg::Name preg_lo = OptoReg::add(preg,-1);
duke@435 465 if( !is_single_register(phi->ideal_reg()) ) {
duke@435 466 value.map(preg_lo,phi);
duke@435 467 regnd.map(preg_lo,phi);
duke@435 468 }
duke@435 469 }
duke@435 470 }
duke@435 471
duke@435 472 // For all remaining instructions
duke@435 473 for( j = phi_dex; j < b->_nodes.size(); j++ ) {
duke@435 474 Node *n = b->_nodes[j];
duke@435 475
duke@435 476 if( n->outcnt() == 0 && // Dead?
duke@435 477 n != C->top() && // (ignore TOP, it has no du info)
duke@435 478 !n->is_Proj() ) { // fat-proj kills
duke@435 479 j -= yank_if_dead(n,b,&value,&regnd);
duke@435 480 continue;
duke@435 481 }
duke@435 482
duke@435 483 // Improve reaching-def info. Occasionally post-alloc's liveness gives
duke@435 484 // up (at loop backedges, because we aren't doing a full flow pass).
duke@435 485 // The presence of a live use essentially asserts that the use's def is
duke@435 486 // alive and well at the use (or else the allocator fubar'd). Take
duke@435 487 // advantage of this info to set a reaching def for the use-reg.
duke@435 488 uint k;
duke@435 489 for( k = 1; k < n->req(); k++ ) {
duke@435 490 Node *def = n->in(k); // n->in(k) is a USE; def is the DEF for this USE
duke@435 491 guarantee(def != NULL, "no disconnected nodes at this point");
duke@435 492 uint useidx = n2lidx(def); // useidx is the live range index for this USE
duke@435 493
duke@435 494 if( useidx ) {
duke@435 495 OptoReg::Name ureg = lrgs(useidx).reg();
duke@435 496 if( !value[ureg] ) {
duke@435 497 int idx; // Skip occasional useless copy
duke@435 498 while( (idx=def->is_Copy()) != 0 &&
duke@435 499 def->in(idx) != NULL && // NULL should not happen
duke@435 500 ureg == lrgs(n2lidx(def->in(idx))).reg() )
duke@435 501 def = def->in(idx);
duke@435 502 Node *valdef = skip_copies(def); // tighten up val through non-useless copies
duke@435 503 value.map(ureg,valdef); // record improved reaching-def info
duke@435 504 regnd.map(ureg, def);
duke@435 505 // Record other half of doubles
duke@435 506 OptoReg::Name ureg_lo = OptoReg::add(ureg,-1);
duke@435 507 if( !is_single_register(def->ideal_reg()) &&
duke@435 508 ( !RegMask::can_represent(ureg_lo) ||
duke@435 509 lrgs(useidx).mask().Member(ureg_lo) ) && // Nearly always adjacent
duke@435 510 !value[ureg_lo] ) {
duke@435 511 value.map(ureg_lo,valdef); // record improved reaching-def info
duke@435 512 regnd.map(ureg_lo, def);
duke@435 513 }
duke@435 514 }
duke@435 515 }
duke@435 516 }
duke@435 517
duke@435 518 const uint two_adr = n->is_Mach() ? n->as_Mach()->two_adr() : 0;
duke@435 519
duke@435 520 // Remove copies along input edges
duke@435 521 for( k = 1; k < n->req(); k++ )
duke@435 522 j -= elide_copy( n, k, b, value, regnd, two_adr!=k );
duke@435 523
duke@435 524 // Unallocated Nodes define no registers
duke@435 525 uint lidx = n2lidx(n);
duke@435 526 if( !lidx ) continue;
duke@435 527
duke@435 528 // Update the register defined by this instruction
duke@435 529 OptoReg::Name nreg = lrgs(lidx).reg();
duke@435 530 // Skip through all copies to the _value_ being defined.
duke@435 531 // Do not change from int to pointer
duke@435 532 Node *val = skip_copies(n);
duke@435 533
never@1358 534 // Clear out a dead definition before starting so that the
never@1358 535 // elimination code doesn't have to guard against it. The
never@1358 536 // definition could in fact be a kill projection with a count of
never@1358 537 // 0 which is safe but since those are uninteresting for copy
never@1358 538 // elimination just delete them as well.
never@1358 539 if (regnd[nreg] != NULL && regnd[nreg]->outcnt() == 0) {
never@1358 540 regnd.map(nreg, NULL);
never@1358 541 value.map(nreg, NULL);
never@1358 542 }
never@1358 543
duke@435 544 uint n_ideal_reg = n->ideal_reg();
duke@435 545 if( is_single_register(n_ideal_reg) ) {
duke@435 546 // If Node 'n' does not change the value mapped by the register,
duke@435 547 // then 'n' is a useless copy. Do not update the register->node
duke@435 548 // mapping so 'n' will go dead.
duke@435 549 if( value[nreg] != val ) {
never@505 550 if (eliminate_copy_of_constant(val, n, b, value, regnd, nreg, OptoReg::Bad)) {
never@1358 551 j -= replace_and_yank_if_dead(n, nreg, b, value, regnd);
duke@435 552 } else {
duke@435 553 // Update the mapping: record new Node defined by the register
duke@435 554 regnd.map(nreg,n);
duke@435 555 // Update mapping for defined *value*, which is the defined
duke@435 556 // Node after skipping all copies.
duke@435 557 value.map(nreg,val);
duke@435 558 }
never@1358 559 } else if( !may_be_copy_of_callee(n) ) {
duke@435 560 assert( n->is_Copy(), "" );
never@1358 561 j -= replace_and_yank_if_dead(n, nreg, b, value, regnd);
duke@435 562 }
duke@435 563 } else {
duke@435 564 // If the value occupies a register pair, record same info
duke@435 565 // in both registers.
duke@435 566 OptoReg::Name nreg_lo = OptoReg::add(nreg,-1);
duke@435 567 if( RegMask::can_represent(nreg_lo) && // Either a spill slot, or
duke@435 568 !lrgs(lidx).mask().Member(nreg_lo) ) { // Nearly always adjacent
duke@435 569 // Sparc occasionally has non-adjacent pairs.
duke@435 570 // Find the actual other value
duke@435 571 RegMask tmp = lrgs(lidx).mask();
duke@435 572 tmp.Remove(nreg);
duke@435 573 nreg_lo = tmp.find_first_elem();
duke@435 574 }
duke@435 575 if( value[nreg] != val || value[nreg_lo] != val ) {
never@505 576 if (eliminate_copy_of_constant(val, n, b, value, regnd, nreg, nreg_lo)) {
never@1358 577 j -= replace_and_yank_if_dead(n, nreg, b, value, regnd);
duke@435 578 } else {
duke@435 579 regnd.map(nreg , n );
duke@435 580 regnd.map(nreg_lo, n );
duke@435 581 value.map(nreg ,val);
duke@435 582 value.map(nreg_lo,val);
duke@435 583 }
never@1358 584 } else if( !may_be_copy_of_callee(n) ) {
duke@435 585 assert( n->is_Copy(), "" );
never@1358 586 j -= replace_and_yank_if_dead(n, nreg, b, value, regnd);
duke@435 587 }
duke@435 588 }
duke@435 589
duke@435 590 // Fat projections kill many registers
duke@435 591 if( n_ideal_reg == MachProjNode::fat_proj ) {
duke@435 592 RegMask rm = n->out_RegMask();
duke@435 593 // wow, what an expensive iterator...
duke@435 594 nreg = rm.find_first_elem();
duke@435 595 while( OptoReg::is_valid(nreg)) {
duke@435 596 rm.Remove(nreg);
duke@435 597 value.map(nreg,n);
duke@435 598 regnd.map(nreg,n);
duke@435 599 nreg = rm.find_first_elem();
duke@435 600 }
duke@435 601 }
duke@435 602
duke@435 603 } // End of for all instructions in the block
duke@435 604
duke@435 605 } // End for all blocks
duke@435 606 }

mercurial