src/share/vm/opto/postaloc.cpp

Mon, 27 May 2013 12:56:34 +0200

author
stefank
date
Mon, 27 May 2013 12:56:34 +0200
changeset 5195
95c00927be11
parent 4949
8373c19be854
child 5509
d1034bd8cefc
permissions
-rw-r--r--

8015428: Remove unused CDS support from StringTable
Summary: The string in StringTable is not used by CDS anymore. Remove the unnecessary code in preparation for 8015422: Large performance hit when the StringTable is walked twice in Parallel Scavenge
Reviewed-by: pliden, tschatzl, coleenp

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

mercurial