src/share/vm/opto/reg_split.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 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 "libadt/vectset.hpp"
aoqi@0 27 #include "memory/allocation.inline.hpp"
aoqi@0 28 #include "opto/addnode.hpp"
aoqi@0 29 #include "opto/c2compiler.hpp"
aoqi@0 30 #include "opto/callnode.hpp"
aoqi@0 31 #include "opto/cfgnode.hpp"
aoqi@0 32 #include "opto/chaitin.hpp"
aoqi@0 33 #include "opto/loopnode.hpp"
aoqi@0 34 #include "opto/machnode.hpp"
aoqi@0 35
aoqi@0 36 //------------------------------Split--------------------------------------
aoqi@0 37 // Walk the graph in RPO and for each lrg which spills, propagate reaching
aoqi@0 38 // definitions. During propagation, split the live range around regions of
aoqi@0 39 // High Register Pressure (HRP). If a Def is in a region of Low Register
aoqi@0 40 // Pressure (LRP), it will not get spilled until we encounter a region of
aoqi@0 41 // HRP between it and one of its uses. We will spill at the transition
aoqi@0 42 // point between LRP and HRP. Uses in the HRP region will use the spilled
aoqi@0 43 // Def. The first Use outside the HRP region will generate a SpillCopy to
aoqi@0 44 // hoist the live range back up into a register, and all subsequent uses
aoqi@0 45 // will use that new Def until another HRP region is encountered. Defs in
aoqi@0 46 // HRP regions will get trailing SpillCopies to push the LRG down into the
aoqi@0 47 // stack immediately.
aoqi@0 48 //
aoqi@0 49 // As a side effect, unlink from (hence make dead) coalesced copies.
aoqi@0 50 //
aoqi@0 51
aoqi@0 52 static const char out_of_nodes[] = "out of nodes during split";
aoqi@0 53
aoqi@0 54 //------------------------------get_spillcopy_wide-----------------------------
aoqi@0 55 // Get a SpillCopy node with wide-enough masks. Use the 'wide-mask', the
aoqi@0 56 // wide ideal-register spill-mask if possible. If the 'wide-mask' does
aoqi@0 57 // not cover the input (or output), use the input (or output) mask instead.
aoqi@0 58 Node *PhaseChaitin::get_spillcopy_wide( Node *def, Node *use, uint uidx ) {
aoqi@0 59 // If ideal reg doesn't exist we've got a bad schedule happening
aoqi@0 60 // that is forcing us to spill something that isn't spillable.
aoqi@0 61 // Bail rather than abort
aoqi@0 62 int ireg = def->ideal_reg();
aoqi@0 63 if( ireg == 0 || ireg == Op_RegFlags ) {
aoqi@0 64 assert(false, "attempted to spill a non-spillable item");
aoqi@0 65 C->record_method_not_compilable("attempted to spill a non-spillable item");
aoqi@0 66 return NULL;
aoqi@0 67 }
aoqi@0 68 if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
aoqi@0 69 return NULL;
aoqi@0 70 }
aoqi@0 71 const RegMask *i_mask = &def->out_RegMask();
aoqi@0 72 const RegMask *w_mask = C->matcher()->idealreg2spillmask[ireg];
aoqi@0 73 const RegMask *o_mask = use ? &use->in_RegMask(uidx) : w_mask;
aoqi@0 74 const RegMask *w_i_mask = w_mask->overlap( *i_mask ) ? w_mask : i_mask;
aoqi@0 75 const RegMask *w_o_mask;
aoqi@0 76
aoqi@0 77 int num_regs = RegMask::num_registers(ireg);
aoqi@0 78 bool is_vect = RegMask::is_vector(ireg);
aoqi@0 79 if( w_mask->overlap( *o_mask ) && // Overlap AND
aoqi@0 80 ((num_regs == 1) // Single use or aligned
aoqi@0 81 || is_vect // or vector
aoqi@0 82 || !is_vect && o_mask->is_aligned_pairs()) ) {
aoqi@0 83 assert(!is_vect || o_mask->is_aligned_sets(num_regs), "vectors are aligned");
aoqi@0 84 // Don't come here for mis-aligned doubles
aoqi@0 85 w_o_mask = w_mask;
aoqi@0 86 } else { // wide ideal mask does not overlap with o_mask
aoqi@0 87 // Mis-aligned doubles come here and XMM->FPR moves on x86.
aoqi@0 88 w_o_mask = o_mask; // Must target desired registers
aoqi@0 89 // Does the ideal-reg-mask overlap with o_mask? I.e., can I use
aoqi@0 90 // a reg-reg move or do I need a trip across register classes
aoqi@0 91 // (and thus through memory)?
aoqi@0 92 if( !C->matcher()->idealreg2regmask[ireg]->overlap( *o_mask) && o_mask->is_UP() )
aoqi@0 93 // Here we assume a trip through memory is required.
aoqi@0 94 w_i_mask = &C->FIRST_STACK_mask();
aoqi@0 95 }
aoqi@0 96 return new (C) MachSpillCopyNode( def, *w_i_mask, *w_o_mask );
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 //------------------------------insert_proj------------------------------------
aoqi@0 100 // Insert the spill at chosen location. Skip over any intervening Proj's or
aoqi@0 101 // Phis. Skip over a CatchNode and projs, inserting in the fall-through block
aoqi@0 102 // instead. Update high-pressure indices. Create a new live range.
aoqi@0 103 void PhaseChaitin::insert_proj( Block *b, uint i, Node *spill, uint maxlrg ) {
aoqi@0 104 // Skip intervening ProjNodes. Do not insert between a ProjNode and
aoqi@0 105 // its definer.
aoqi@0 106 while( i < b->number_of_nodes() &&
aoqi@0 107 (b->get_node(i)->is_Proj() ||
aoqi@0 108 b->get_node(i)->is_Phi() ) )
aoqi@0 109 i++;
aoqi@0 110
aoqi@0 111 // Do not insert between a call and his Catch
aoqi@0 112 if( b->get_node(i)->is_Catch() ) {
aoqi@0 113 // Put the instruction at the top of the fall-thru block.
aoqi@0 114 // Find the fall-thru projection
aoqi@0 115 while( 1 ) {
aoqi@0 116 const CatchProjNode *cp = b->get_node(++i)->as_CatchProj();
aoqi@0 117 if( cp->_con == CatchProjNode::fall_through_index )
aoqi@0 118 break;
aoqi@0 119 }
aoqi@0 120 int sidx = i - b->end_idx()-1;
aoqi@0 121 b = b->_succs[sidx]; // Switch to successor block
aoqi@0 122 i = 1; // Right at start of block
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 b->insert_node(spill, i); // Insert node in block
aoqi@0 126 _cfg.map_node_to_block(spill, b); // Update node->block mapping to reflect
aoqi@0 127 // Adjust the point where we go hi-pressure
aoqi@0 128 if( i <= b->_ihrp_index ) b->_ihrp_index++;
aoqi@0 129 if( i <= b->_fhrp_index ) b->_fhrp_index++;
aoqi@0 130
aoqi@0 131 // Assign a new Live Range Number to the SpillCopy and grow
aoqi@0 132 // the node->live range mapping.
aoqi@0 133 new_lrg(spill,maxlrg);
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 //------------------------------split_DEF--------------------------------------
aoqi@0 137 // There are four categories of Split; UP/DOWN x DEF/USE
aoqi@0 138 // Only three of these really occur as DOWN/USE will always color
aoqi@0 139 // Any Split with a DEF cannot CISC-Spill now. Thus we need
aoqi@0 140 // two helper routines, one for Split DEFS (insert after instruction),
aoqi@0 141 // one for Split USES (insert before instruction). DEF insertion
aoqi@0 142 // happens inside Split, where the Leaveblock array is updated.
aoqi@0 143 uint PhaseChaitin::split_DEF( Node *def, Block *b, int loc, uint maxlrg, Node **Reachblock, Node **debug_defs, GrowableArray<uint> splits, int slidx ) {
aoqi@0 144 #ifdef ASSERT
aoqi@0 145 // Increment the counter for this lrg
aoqi@0 146 splits.at_put(slidx, splits.at(slidx)+1);
aoqi@0 147 #endif
aoqi@0 148 // If we are spilling the memory op for an implicit null check, at the
aoqi@0 149 // null check location (ie - null check is in HRP block) we need to do
aoqi@0 150 // the null-check first, then spill-down in the following block.
aoqi@0 151 // (The implicit_null_check function ensures the use is also dominated
aoqi@0 152 // by the branch-not-taken block.)
aoqi@0 153 Node *be = b->end();
aoqi@0 154 if( be->is_MachNullCheck() && be->in(1) == def && def == b->get_node(loc)) {
aoqi@0 155 // Spill goes in the branch-not-taken block
aoqi@0 156 b = b->_succs[b->get_node(b->end_idx()+1)->Opcode() == Op_IfTrue];
aoqi@0 157 loc = 0; // Just past the Region
aoqi@0 158 }
aoqi@0 159 assert( loc >= 0, "must insert past block head" );
aoqi@0 160
aoqi@0 161 // Get a def-side SpillCopy
aoqi@0 162 Node *spill = get_spillcopy_wide(def,NULL,0);
aoqi@0 163 // Did we fail to split?, then bail
aoqi@0 164 if (!spill) {
aoqi@0 165 return 0;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 // Insert the spill at chosen location
aoqi@0 169 insert_proj( b, loc+1, spill, maxlrg++);
aoqi@0 170
aoqi@0 171 // Insert new node into Reaches array
aoqi@0 172 Reachblock[slidx] = spill;
aoqi@0 173 // Update debug list of reaching down definitions by adding this one
aoqi@0 174 debug_defs[slidx] = spill;
aoqi@0 175
aoqi@0 176 // return updated count of live ranges
aoqi@0 177 return maxlrg;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 //------------------------------split_USE--------------------------------------
aoqi@0 181 // Splits at uses can involve redeffing the LRG, so no CISC Spilling there.
aoqi@0 182 // Debug uses want to know if def is already stack enabled.
aoqi@0 183 uint PhaseChaitin::split_USE( Node *def, Block *b, Node *use, uint useidx, uint maxlrg, bool def_down, bool cisc_sp, GrowableArray<uint> splits, int slidx ) {
aoqi@0 184 #ifdef ASSERT
aoqi@0 185 // Increment the counter for this lrg
aoqi@0 186 splits.at_put(slidx, splits.at(slidx)+1);
aoqi@0 187 #endif
aoqi@0 188
aoqi@0 189 // Some setup stuff for handling debug node uses
aoqi@0 190 JVMState* jvms = use->jvms();
aoqi@0 191 uint debug_start = jvms ? jvms->debug_start() : 999999;
aoqi@0 192 uint debug_end = jvms ? jvms->debug_end() : 999999;
aoqi@0 193
aoqi@0 194 //-------------------------------------------
aoqi@0 195 // Check for use of debug info
aoqi@0 196 if (useidx >= debug_start && useidx < debug_end) {
aoqi@0 197 // Actually it's perfectly legal for constant debug info to appear
aoqi@0 198 // just unlikely. In this case the optimizer left a ConI of a 4
aoqi@0 199 // as both inputs to a Phi with only a debug use. It's a single-def
aoqi@0 200 // live range of a rematerializable value. The live range spills,
aoqi@0 201 // rematerializes and now the ConI directly feeds into the debug info.
aoqi@0 202 // assert(!def->is_Con(), "constant debug info already constructed directly");
aoqi@0 203
aoqi@0 204 // Special split handling for Debug Info
aoqi@0 205 // If DEF is DOWN, just hook the edge and return
aoqi@0 206 // If DEF is UP, Split it DOWN for this USE.
aoqi@0 207 if( def->is_Mach() ) {
aoqi@0 208 if( def_down ) {
aoqi@0 209 // DEF is DOWN, so connect USE directly to the DEF
aoqi@0 210 use->set_req(useidx, def);
aoqi@0 211 } else {
aoqi@0 212 // Block and index where the use occurs.
aoqi@0 213 Block *b = _cfg.get_block_for_node(use);
aoqi@0 214 // Put the clone just prior to use
aoqi@0 215 int bindex = b->find_node(use);
aoqi@0 216 // DEF is UP, so must copy it DOWN and hook in USE
aoqi@0 217 // Insert SpillCopy before the USE, which uses DEF as its input,
aoqi@0 218 // and defs a new live range, which is used by this node.
aoqi@0 219 Node *spill = get_spillcopy_wide(def,use,useidx);
aoqi@0 220 // did we fail to split?
aoqi@0 221 if (!spill) {
aoqi@0 222 // Bail
aoqi@0 223 return 0;
aoqi@0 224 }
aoqi@0 225 // insert into basic block
aoqi@0 226 insert_proj( b, bindex, spill, maxlrg++ );
aoqi@0 227 // Use the new split
aoqi@0 228 use->set_req(useidx,spill);
aoqi@0 229 }
aoqi@0 230 // No further split handling needed for this use
aoqi@0 231 return maxlrg;
aoqi@0 232 } // End special splitting for debug info live range
aoqi@0 233 } // If debug info
aoqi@0 234
aoqi@0 235 // CISC-SPILLING
aoqi@0 236 // Finally, check to see if USE is CISC-Spillable, and if so,
aoqi@0 237 // gather_lrg_masks will add the flags bit to its mask, and
aoqi@0 238 // no use side copy is needed. This frees up the live range
aoqi@0 239 // register choices without causing copy coalescing, etc.
aoqi@0 240 if( UseCISCSpill && cisc_sp ) {
aoqi@0 241 int inp = use->cisc_operand();
aoqi@0 242 if( inp != AdlcVMDeps::Not_cisc_spillable )
aoqi@0 243 // Convert operand number to edge index number
aoqi@0 244 inp = use->as_Mach()->operand_index(inp);
aoqi@0 245 if( inp == (int)useidx ) {
aoqi@0 246 use->set_req(useidx, def);
aoqi@0 247 #ifndef PRODUCT
aoqi@0 248 if( TraceCISCSpill ) {
aoqi@0 249 tty->print(" set_split: ");
aoqi@0 250 use->dump();
aoqi@0 251 }
aoqi@0 252 #endif
aoqi@0 253 return maxlrg;
aoqi@0 254 }
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 //-------------------------------------------
aoqi@0 258 // Insert a Copy before the use
aoqi@0 259
aoqi@0 260 // Block and index where the use occurs.
aoqi@0 261 int bindex;
aoqi@0 262 // Phi input spill-copys belong at the end of the prior block
aoqi@0 263 if( use->is_Phi() ) {
aoqi@0 264 b = _cfg.get_block_for_node(b->pred(useidx));
aoqi@0 265 bindex = b->end_idx();
aoqi@0 266 } else {
aoqi@0 267 // Put the clone just prior to use
aoqi@0 268 bindex = b->find_node(use);
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 Node *spill = get_spillcopy_wide( def, use, useidx );
aoqi@0 272 if( !spill ) return 0; // Bailed out
aoqi@0 273 // Insert SpillCopy before the USE, which uses the reaching DEF as
aoqi@0 274 // its input, and defs a new live range, which is used by this node.
aoqi@0 275 insert_proj( b, bindex, spill, maxlrg++ );
aoqi@0 276 // Use the spill/clone
aoqi@0 277 use->set_req(useidx,spill);
aoqi@0 278
aoqi@0 279 // return updated live range count
aoqi@0 280 return maxlrg;
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 //------------------------------clone_node----------------------------
aoqi@0 284 // Clone node with anti dependence check.
aoqi@0 285 Node* clone_node(Node* def, Block *b, Compile* C) {
aoqi@0 286 if (def->needs_anti_dependence_check()) {
aoqi@0 287 #ifdef ASSERT
aoqi@0 288 if (Verbose) {
aoqi@0 289 tty->print_cr("RA attempts to clone node with anti_dependence:");
aoqi@0 290 def->dump(-1); tty->cr();
aoqi@0 291 tty->print_cr("into block:");
aoqi@0 292 b->dump();
aoqi@0 293 }
aoqi@0 294 #endif
aoqi@0 295 if (C->subsume_loads() == true && !C->failing()) {
aoqi@0 296 // Retry with subsume_loads == false
aoqi@0 297 // If this is the first failure, the sentinel string will "stick"
aoqi@0 298 // to the Compile object, and the C2Compiler will see it and retry.
aoqi@0 299 C->record_failure(C2Compiler::retry_no_subsuming_loads());
aoqi@0 300 } else {
aoqi@0 301 // Bailout without retry
aoqi@0 302 C->record_method_not_compilable("RA Split failed: attempt to clone node with anti_dependence");
aoqi@0 303 }
aoqi@0 304 return 0;
aoqi@0 305 }
aoqi@0 306 return def->clone();
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 //------------------------------split_Rematerialize----------------------------
aoqi@0 310 // Clone a local copy of the def.
aoqi@0 311 Node *PhaseChaitin::split_Rematerialize( Node *def, Block *b, uint insidx, uint &maxlrg, GrowableArray<uint> splits, int slidx, uint *lrg2reach, Node **Reachblock, bool walkThru ) {
aoqi@0 312 // The input live ranges will be stretched to the site of the new
aoqi@0 313 // instruction. They might be stretched past a def and will thus
aoqi@0 314 // have the old and new values of the same live range alive at the
aoqi@0 315 // same time - a definite no-no. Split out private copies of
aoqi@0 316 // the inputs.
aoqi@0 317 if( def->req() > 1 ) {
aoqi@0 318 for( uint i = 1; i < def->req(); i++ ) {
aoqi@0 319 Node *in = def->in(i);
aoqi@0 320 uint lidx = _lrg_map.live_range_id(in);
aoqi@0 321 // We do not need this for live ranges that are only defined once.
aoqi@0 322 // However, this is not true for spill copies that are added in this
aoqi@0 323 // Split() pass, since they might get coalesced later on in this pass.
aoqi@0 324 if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_singledef()) {
aoqi@0 325 continue;
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 Block *b_def = _cfg.get_block_for_node(def);
aoqi@0 329 int idx_def = b_def->find_node(def);
aoqi@0 330 Node *in_spill = get_spillcopy_wide( in, def, i );
aoqi@0 331 if( !in_spill ) return 0; // Bailed out
aoqi@0 332 insert_proj(b_def,idx_def,in_spill,maxlrg++);
aoqi@0 333 if( b_def == b )
aoqi@0 334 insidx++;
aoqi@0 335 def->set_req(i,in_spill);
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 Node *spill = clone_node(def, b, C);
aoqi@0 340 if (spill == NULL || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
aoqi@0 341 // Check when generating nodes
aoqi@0 342 return 0;
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 // See if any inputs are currently being spilled, and take the
aoqi@0 346 // latest copy of spilled inputs.
aoqi@0 347 if( spill->req() > 1 ) {
aoqi@0 348 for( uint i = 1; i < spill->req(); i++ ) {
aoqi@0 349 Node *in = spill->in(i);
aoqi@0 350 uint lidx = _lrg_map.find_id(in);
aoqi@0 351
aoqi@0 352 // Walk backwards thru spill copy node intermediates
aoqi@0 353 if (walkThru) {
aoqi@0 354 while (in->is_SpillCopy() && lidx >= _lrg_map.max_lrg_id()) {
aoqi@0 355 in = in->in(1);
aoqi@0 356 lidx = _lrg_map.find_id(in);
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_multidef()) {
aoqi@0 360 // walkThru found a multidef LRG, which is unsafe to use, so
aoqi@0 361 // just keep the original def used in the clone.
aoqi@0 362 in = spill->in(i);
aoqi@0 363 lidx = _lrg_map.find_id(in);
aoqi@0 364 }
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).reg() >= LRG::SPILL_REG) {
aoqi@0 368 Node *rdef = Reachblock[lrg2reach[lidx]];
aoqi@0 369 if (rdef) {
aoqi@0 370 spill->set_req(i, rdef);
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375
aoqi@0 376
aoqi@0 377 assert( spill->out_RegMask().is_UP(), "rematerialize to a reg" );
aoqi@0 378 // Rematerialized op is def->spilled+1
aoqi@0 379 set_was_spilled(spill);
aoqi@0 380 if( _spilled_once.test(def->_idx) )
aoqi@0 381 set_was_spilled(spill);
aoqi@0 382
aoqi@0 383 insert_proj( b, insidx, spill, maxlrg++ );
aoqi@0 384 #ifdef ASSERT
aoqi@0 385 // Increment the counter for this lrg
aoqi@0 386 splits.at_put(slidx, splits.at(slidx)+1);
aoqi@0 387 #endif
aoqi@0 388 // See if the cloned def kills any flags, and copy those kills as well
aoqi@0 389 uint i = insidx+1;
aoqi@0 390 int found_projs = clone_projs( b, i, def, spill, maxlrg);
aoqi@0 391 if (found_projs > 0) {
aoqi@0 392 // Adjust the point where we go hi-pressure
aoqi@0 393 if (i <= b->_ihrp_index) {
aoqi@0 394 b->_ihrp_index += found_projs;
aoqi@0 395 }
aoqi@0 396 if (i <= b->_fhrp_index) {
aoqi@0 397 b->_fhrp_index += found_projs;
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 return spill;
aoqi@0 402 }
aoqi@0 403
aoqi@0 404 //------------------------------is_high_pressure-------------------------------
aoqi@0 405 // Function to compute whether or not this live range is "high pressure"
aoqi@0 406 // in this block - whether it spills eagerly or not.
aoqi@0 407 bool PhaseChaitin::is_high_pressure( Block *b, LRG *lrg, uint insidx ) {
aoqi@0 408 if( lrg->_was_spilled1 ) return true;
aoqi@0 409 // Forced spilling due to conflict? Then split only at binding uses
aoqi@0 410 // or defs, not for supposed capacity problems.
aoqi@0 411 // CNC - Turned off 7/8/99, causes too much spilling
aoqi@0 412 // if( lrg->_is_bound ) return false;
aoqi@0 413
aoqi@0 414 // Use float pressure numbers for vectors.
aoqi@0 415 bool is_float_or_vector = lrg->_is_float || lrg->_is_vector;
aoqi@0 416 // Not yet reached the high-pressure cutoff point, so low pressure
aoqi@0 417 uint hrp_idx = is_float_or_vector ? b->_fhrp_index : b->_ihrp_index;
aoqi@0 418 if( insidx < hrp_idx ) return false;
aoqi@0 419 // Register pressure for the block as a whole depends on reg class
aoqi@0 420 int block_pres = is_float_or_vector ? b->_freg_pressure : b->_reg_pressure;
aoqi@0 421 // Bound live ranges will split at the binding points first;
aoqi@0 422 // Intermediate splits should assume the live range's register set
aoqi@0 423 // got "freed up" and that num_regs will become INT_PRESSURE.
aoqi@0 424 int bound_pres = is_float_or_vector ? FLOATPRESSURE : INTPRESSURE;
aoqi@0 425 // Effective register pressure limit.
aoqi@0 426 int lrg_pres = (lrg->get_invalid_mask_size() > lrg->num_regs())
aoqi@0 427 ? (lrg->get_invalid_mask_size() >> (lrg->num_regs()-1)) : bound_pres;
aoqi@0 428 // High pressure if block pressure requires more register freedom
aoqi@0 429 // than live range has.
aoqi@0 430 return block_pres >= lrg_pres;
aoqi@0 431 }
aoqi@0 432
aoqi@0 433
aoqi@0 434 //------------------------------prompt_use---------------------------------
aoqi@0 435 // True if lidx is used before any real register is def'd in the block
aoqi@0 436 bool PhaseChaitin::prompt_use( Block *b, uint lidx ) {
aoqi@0 437 if (lrgs(lidx)._was_spilled2) {
aoqi@0 438 return false;
aoqi@0 439 }
aoqi@0 440
aoqi@0 441 // Scan block for 1st use.
aoqi@0 442 for( uint i = 1; i <= b->end_idx(); i++ ) {
aoqi@0 443 Node *n = b->get_node(i);
aoqi@0 444 // Ignore PHI use, these can be up or down
aoqi@0 445 if (n->is_Phi()) {
aoqi@0 446 continue;
aoqi@0 447 }
aoqi@0 448 for (uint j = 1; j < n->req(); j++) {
aoqi@0 449 if (_lrg_map.find_id(n->in(j)) == lidx) {
aoqi@0 450 return true; // Found 1st use!
aoqi@0 451 }
aoqi@0 452 }
aoqi@0 453 if (n->out_RegMask().is_NotEmpty()) {
aoqi@0 454 return false;
aoqi@0 455 }
aoqi@0 456 }
aoqi@0 457 return false;
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 //------------------------------Split--------------------------------------
aoqi@0 461 //----------Split Routine----------
aoqi@0 462 // ***** NEW SPLITTING HEURISTIC *****
aoqi@0 463 // DEFS: If the DEF is in a High Register Pressure(HRP) Block, split there.
aoqi@0 464 // Else, no split unless there is a HRP block between a DEF and
aoqi@0 465 // one of its uses, and then split at the HRP block.
aoqi@0 466 //
aoqi@0 467 // USES: If USE is in HRP, split at use to leave main LRG on stack.
aoqi@0 468 // Else, hoist LRG back up to register only (ie - split is also DEF)
aoqi@0 469 // We will compute a new maxlrg as we go
aoqi@0 470 uint PhaseChaitin::Split(uint maxlrg, ResourceArea* split_arena) {
aoqi@0 471 NOT_PRODUCT( Compile::TracePhase t3("regAllocSplit", &_t_regAllocSplit, TimeCompiler); )
aoqi@0 472
aoqi@0 473 // Free thread local resources used by this method on exit.
aoqi@0 474 ResourceMark rm(split_arena);
aoqi@0 475
aoqi@0 476 uint bidx, pidx, slidx, insidx, inpidx, twoidx;
aoqi@0 477 uint non_phi = 1, spill_cnt = 0;
aoqi@0 478 Node *n1, *n2, *n3;
aoqi@0 479 Node_List *defs,*phis;
aoqi@0 480 bool *UPblock;
aoqi@0 481 bool u1, u2, u3;
aoqi@0 482 Block *b, *pred;
aoqi@0 483 PhiNode *phi;
aoqi@0 484 GrowableArray<uint> lidxs(split_arena, maxlrg, 0, 0);
aoqi@0 485
aoqi@0 486 // Array of counters to count splits per live range
aoqi@0 487 GrowableArray<uint> splits(split_arena, maxlrg, 0, 0);
aoqi@0 488
aoqi@0 489 #define NEW_SPLIT_ARRAY(type, size)\
aoqi@0 490 (type*) split_arena->allocate_bytes((size) * sizeof(type))
aoqi@0 491
aoqi@0 492 //----------Setup Code----------
aoqi@0 493 // Create a convenient mapping from lrg numbers to reaches/leaves indices
aoqi@0 494 uint *lrg2reach = NEW_SPLIT_ARRAY(uint, maxlrg);
aoqi@0 495 // Keep track of DEFS & Phis for later passes
aoqi@0 496 defs = new Node_List();
aoqi@0 497 phis = new Node_List();
aoqi@0 498 // Gather info on which LRG's are spilling, and build maps
aoqi@0 499 for (bidx = 1; bidx < maxlrg; bidx++) {
aoqi@0 500 if (lrgs(bidx).alive() && lrgs(bidx).reg() >= LRG::SPILL_REG) {
aoqi@0 501 assert(!lrgs(bidx).mask().is_AllStack(),"AllStack should color");
aoqi@0 502 lrg2reach[bidx] = spill_cnt;
aoqi@0 503 spill_cnt++;
aoqi@0 504 lidxs.append(bidx);
aoqi@0 505 #ifdef ASSERT
aoqi@0 506 // Initialize the split counts to zero
aoqi@0 507 splits.append(0);
aoqi@0 508 #endif
aoqi@0 509 #ifndef PRODUCT
aoqi@0 510 if( PrintOpto && WizardMode && lrgs(bidx)._was_spilled1 )
aoqi@0 511 tty->print_cr("Warning, 2nd spill of L%d",bidx);
aoqi@0 512 #endif
aoqi@0 513 }
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 // Create side arrays for propagating reaching defs info.
aoqi@0 517 // Each block needs a node pointer for each spilling live range for the
aoqi@0 518 // Def which is live into the block. Phi nodes handle multiple input
aoqi@0 519 // Defs by querying the output of their predecessor blocks and resolving
aoqi@0 520 // them to a single Def at the phi. The pointer is updated for each
aoqi@0 521 // Def in the block, and then becomes the output for the block when
aoqi@0 522 // processing of the block is complete. We also need to track whether
aoqi@0 523 // a Def is UP or DOWN. UP means that it should get a register (ie -
aoqi@0 524 // it is always in LRP regions), and DOWN means that it is probably
aoqi@0 525 // on the stack (ie - it crosses HRP regions).
aoqi@0 526 Node ***Reaches = NEW_SPLIT_ARRAY( Node**, _cfg.number_of_blocks() + 1);
aoqi@0 527 bool **UP = NEW_SPLIT_ARRAY( bool*, _cfg.number_of_blocks() + 1);
aoqi@0 528 Node **debug_defs = NEW_SPLIT_ARRAY( Node*, spill_cnt );
aoqi@0 529 VectorSet **UP_entry= NEW_SPLIT_ARRAY( VectorSet*, spill_cnt );
aoqi@0 530
aoqi@0 531 // Initialize Reaches & UP
aoqi@0 532 for (bidx = 0; bidx < _cfg.number_of_blocks() + 1; bidx++) {
aoqi@0 533 Reaches[bidx] = NEW_SPLIT_ARRAY( Node*, spill_cnt );
aoqi@0 534 UP[bidx] = NEW_SPLIT_ARRAY( bool, spill_cnt );
aoqi@0 535 Node **Reachblock = Reaches[bidx];
aoqi@0 536 bool *UPblock = UP[bidx];
aoqi@0 537 for( slidx = 0; slidx < spill_cnt; slidx++ ) {
aoqi@0 538 UPblock[slidx] = true; // Assume they start in registers
aoqi@0 539 Reachblock[slidx] = NULL; // Assume that no def is present
aoqi@0 540 }
aoqi@0 541 }
aoqi@0 542
aoqi@0 543 #undef NEW_SPLIT_ARRAY
aoqi@0 544
aoqi@0 545 // Initialize to array of empty vectorsets
aoqi@0 546 for( slidx = 0; slidx < spill_cnt; slidx++ )
aoqi@0 547 UP_entry[slidx] = new VectorSet(split_arena);
aoqi@0 548
aoqi@0 549 //----------PASS 1----------
aoqi@0 550 //----------Propagation & Node Insertion Code----------
aoqi@0 551 // Walk the Blocks in RPO for DEF & USE info
aoqi@0 552 for( bidx = 0; bidx < _cfg.number_of_blocks(); bidx++ ) {
aoqi@0 553
aoqi@0 554 if (C->check_node_count(spill_cnt, out_of_nodes)) {
aoqi@0 555 return 0;
aoqi@0 556 }
aoqi@0 557
aoqi@0 558 b = _cfg.get_block(bidx);
aoqi@0 559 // Reaches & UP arrays for this block
aoqi@0 560 Node** Reachblock = Reaches[b->_pre_order];
aoqi@0 561 UPblock = UP[b->_pre_order];
aoqi@0 562 // Reset counter of start of non-Phi nodes in block
aoqi@0 563 non_phi = 1;
aoqi@0 564 //----------Block Entry Handling----------
aoqi@0 565 // Check for need to insert a new phi
aoqi@0 566 // Cycle through this block's predecessors, collecting Reaches
aoqi@0 567 // info for each spilled LRG. If they are identical, no phi is
aoqi@0 568 // needed. If they differ, check for a phi, and insert if missing,
aoqi@0 569 // or update edges if present. Set current block's Reaches set to
aoqi@0 570 // be either the phi's or the reaching def, as appropriate.
aoqi@0 571 // If no Phi is needed, check if the LRG needs to spill on entry
aoqi@0 572 // to the block due to HRP.
aoqi@0 573 for( slidx = 0; slidx < spill_cnt; slidx++ ) {
aoqi@0 574 // Grab the live range number
aoqi@0 575 uint lidx = lidxs.at(slidx);
aoqi@0 576 // Do not bother splitting or putting in Phis for single-def
aoqi@0 577 // rematerialized live ranges. This happens alot to constants
aoqi@0 578 // with long live ranges.
aoqi@0 579 if( lrgs(lidx).is_singledef() &&
aoqi@0 580 lrgs(lidx)._def->rematerialize() ) {
aoqi@0 581 // reset the Reaches & UP entries
aoqi@0 582 Reachblock[slidx] = lrgs(lidx)._def;
aoqi@0 583 UPblock[slidx] = true;
aoqi@0 584 // Record following instruction in case 'n' rematerializes and
aoqi@0 585 // kills flags
aoqi@0 586 Block *pred1 = _cfg.get_block_for_node(b->pred(1));
aoqi@0 587 continue;
aoqi@0 588 }
aoqi@0 589
aoqi@0 590 // Initialize needs_phi and needs_split
aoqi@0 591 bool needs_phi = false;
aoqi@0 592 bool needs_split = false;
aoqi@0 593 bool has_phi = false;
aoqi@0 594 // Walk the predecessor blocks to check inputs for that live range
aoqi@0 595 // Grab predecessor block header
aoqi@0 596 n1 = b->pred(1);
aoqi@0 597 // Grab the appropriate reaching def info for inpidx
aoqi@0 598 pred = _cfg.get_block_for_node(n1);
aoqi@0 599 pidx = pred->_pre_order;
aoqi@0 600 Node **Ltmp = Reaches[pidx];
aoqi@0 601 bool *Utmp = UP[pidx];
aoqi@0 602 n1 = Ltmp[slidx];
aoqi@0 603 u1 = Utmp[slidx];
aoqi@0 604 // Initialize node for saving type info
aoqi@0 605 n3 = n1;
aoqi@0 606 u3 = u1;
aoqi@0 607
aoqi@0 608 // Compare inputs to see if a Phi is needed
aoqi@0 609 for( inpidx = 2; inpidx < b->num_preds(); inpidx++ ) {
aoqi@0 610 // Grab predecessor block headers
aoqi@0 611 n2 = b->pred(inpidx);
aoqi@0 612 // Grab the appropriate reaching def info for inpidx
aoqi@0 613 pred = _cfg.get_block_for_node(n2);
aoqi@0 614 pidx = pred->_pre_order;
aoqi@0 615 Ltmp = Reaches[pidx];
aoqi@0 616 Utmp = UP[pidx];
aoqi@0 617 n2 = Ltmp[slidx];
aoqi@0 618 u2 = Utmp[slidx];
aoqi@0 619 // For each LRG, decide if a phi is necessary
aoqi@0 620 if( n1 != n2 ) {
aoqi@0 621 needs_phi = true;
aoqi@0 622 }
aoqi@0 623 // See if the phi has mismatched inputs, UP vs. DOWN
aoqi@0 624 if( n1 && n2 && (u1 != u2) ) {
aoqi@0 625 needs_split = true;
aoqi@0 626 }
aoqi@0 627 // Move n2/u2 to n1/u1 for next iteration
aoqi@0 628 n1 = n2;
aoqi@0 629 u1 = u2;
aoqi@0 630 // Preserve a non-NULL predecessor for later type referencing
aoqi@0 631 if( (n3 == NULL) && (n2 != NULL) ){
aoqi@0 632 n3 = n2;
aoqi@0 633 u3 = u2;
aoqi@0 634 }
aoqi@0 635 } // End for all potential Phi inputs
aoqi@0 636
aoqi@0 637 // check block for appropriate phinode & update edges
aoqi@0 638 for( insidx = 1; insidx <= b->end_idx(); insidx++ ) {
aoqi@0 639 n1 = b->get_node(insidx);
aoqi@0 640 // bail if this is not a phi
aoqi@0 641 phi = n1->is_Phi() ? n1->as_Phi() : NULL;
aoqi@0 642 if( phi == NULL ) {
aoqi@0 643 // Keep track of index of first non-PhiNode instruction in block
aoqi@0 644 non_phi = insidx;
aoqi@0 645 // break out of the for loop as we have handled all phi nodes
aoqi@0 646 break;
aoqi@0 647 }
aoqi@0 648 // must be looking at a phi
aoqi@0 649 if (_lrg_map.find_id(n1) == lidxs.at(slidx)) {
aoqi@0 650 // found the necessary phi
aoqi@0 651 needs_phi = false;
aoqi@0 652 has_phi = true;
aoqi@0 653 // initialize the Reaches entry for this LRG
aoqi@0 654 Reachblock[slidx] = phi;
aoqi@0 655 break;
aoqi@0 656 } // end if found correct phi
aoqi@0 657 } // end for all phi's
aoqi@0 658
aoqi@0 659 // If a phi is needed or exist, check for it
aoqi@0 660 if( needs_phi || has_phi ) {
aoqi@0 661 // add new phinode if one not already found
aoqi@0 662 if( needs_phi ) {
aoqi@0 663 // create a new phi node and insert it into the block
aoqi@0 664 // type is taken from left over pointer to a predecessor
aoqi@0 665 assert(n3,"No non-NULL reaching DEF for a Phi");
aoqi@0 666 phi = new (C) PhiNode(b->head(), n3->bottom_type());
aoqi@0 667 // initialize the Reaches entry for this LRG
aoqi@0 668 Reachblock[slidx] = phi;
aoqi@0 669
aoqi@0 670 // add node to block & node_to_block mapping
aoqi@0 671 insert_proj(b, insidx++, phi, maxlrg++);
aoqi@0 672 non_phi++;
aoqi@0 673 // Reset new phi's mapping to be the spilling live range
aoqi@0 674 _lrg_map.map(phi->_idx, lidx);
aoqi@0 675 assert(_lrg_map.find_id(phi) == lidx, "Bad update on Union-Find mapping");
aoqi@0 676 } // end if not found correct phi
aoqi@0 677 // Here you have either found or created the Phi, so record it
aoqi@0 678 assert(phi != NULL,"Must have a Phi Node here");
aoqi@0 679 phis->push(phi);
aoqi@0 680 // PhiNodes should either force the LRG UP or DOWN depending
aoqi@0 681 // on its inputs and the register pressure in the Phi's block.
aoqi@0 682 UPblock[slidx] = true; // Assume new DEF is UP
aoqi@0 683 // If entering a high-pressure area with no immediate use,
aoqi@0 684 // assume Phi is DOWN
aoqi@0 685 if( is_high_pressure( b, &lrgs(lidx), b->end_idx()) && !prompt_use(b,lidx) )
aoqi@0 686 UPblock[slidx] = false;
aoqi@0 687 // If we are not split up/down and all inputs are down, then we
aoqi@0 688 // are down
aoqi@0 689 if( !needs_split && !u3 )
aoqi@0 690 UPblock[slidx] = false;
aoqi@0 691 } // end if phi is needed
aoqi@0 692
aoqi@0 693 // Do not need a phi, so grab the reaching DEF
aoqi@0 694 else {
aoqi@0 695 // Grab predecessor block header
aoqi@0 696 n1 = b->pred(1);
aoqi@0 697 // Grab the appropriate reaching def info for k
aoqi@0 698 pred = _cfg.get_block_for_node(n1);
aoqi@0 699 pidx = pred->_pre_order;
aoqi@0 700 Node **Ltmp = Reaches[pidx];
aoqi@0 701 bool *Utmp = UP[pidx];
aoqi@0 702 // reset the Reaches & UP entries
aoqi@0 703 Reachblock[slidx] = Ltmp[slidx];
aoqi@0 704 UPblock[slidx] = Utmp[slidx];
aoqi@0 705 } // end else no Phi is needed
aoqi@0 706 } // end for all spilling live ranges
aoqi@0 707 // DEBUG
aoqi@0 708 #ifndef PRODUCT
aoqi@0 709 if(trace_spilling()) {
aoqi@0 710 tty->print("/`\nBlock %d: ", b->_pre_order);
aoqi@0 711 tty->print("Reaching Definitions after Phi handling\n");
aoqi@0 712 for( uint x = 0; x < spill_cnt; x++ ) {
aoqi@0 713 tty->print("Spill Idx %d: UP %d: Node\n",x,UPblock[x]);
aoqi@0 714 if( Reachblock[x] )
aoqi@0 715 Reachblock[x]->dump();
aoqi@0 716 else
aoqi@0 717 tty->print("Undefined\n");
aoqi@0 718 }
aoqi@0 719 }
aoqi@0 720 #endif
aoqi@0 721
aoqi@0 722 //----------Non-Phi Node Splitting----------
aoqi@0 723 // Since phi-nodes have now been handled, the Reachblock array for this
aoqi@0 724 // block is initialized with the correct starting value for the defs which
aoqi@0 725 // reach non-phi instructions in this block. Thus, process non-phi
aoqi@0 726 // instructions normally, inserting SpillCopy nodes for all spill
aoqi@0 727 // locations.
aoqi@0 728
aoqi@0 729 // Memoize any DOWN reaching definitions for use as DEBUG info
aoqi@0 730 for( insidx = 0; insidx < spill_cnt; insidx++ ) {
aoqi@0 731 debug_defs[insidx] = (UPblock[insidx]) ? NULL : Reachblock[insidx];
aoqi@0 732 if( UPblock[insidx] ) // Memoize UP decision at block start
aoqi@0 733 UP_entry[insidx]->set( b->_pre_order );
aoqi@0 734 }
aoqi@0 735
aoqi@0 736 //----------Walk Instructions in the Block and Split----------
aoqi@0 737 // For all non-phi instructions in the block
aoqi@0 738 for( insidx = 1; insidx <= b->end_idx(); insidx++ ) {
aoqi@0 739 Node *n = b->get_node(insidx);
aoqi@0 740 // Find the defining Node's live range index
aoqi@0 741 uint defidx = _lrg_map.find_id(n);
aoqi@0 742 uint cnt = n->req();
aoqi@0 743
aoqi@0 744 if (n->is_Phi()) {
aoqi@0 745 // Skip phi nodes after removing dead copies.
aoqi@0 746 if (defidx < _lrg_map.max_lrg_id()) {
aoqi@0 747 // Check for useless Phis. These appear if we spill, then
aoqi@0 748 // coalesce away copies. Dont touch Phis in spilling live
aoqi@0 749 // ranges; they are busy getting modifed in this pass.
aoqi@0 750 if( lrgs(defidx).reg() < LRG::SPILL_REG ) {
aoqi@0 751 uint i;
aoqi@0 752 Node *u = NULL;
aoqi@0 753 // Look for the Phi merging 2 unique inputs
aoqi@0 754 for( i = 1; i < cnt; i++ ) {
aoqi@0 755 // Ignore repeats and self
aoqi@0 756 if( n->in(i) != u && n->in(i) != n ) {
aoqi@0 757 // Found a unique input
aoqi@0 758 if( u != NULL ) // If it's the 2nd, bail out
aoqi@0 759 break;
aoqi@0 760 u = n->in(i); // Else record it
aoqi@0 761 }
aoqi@0 762 }
aoqi@0 763 assert( u, "at least 1 valid input expected" );
aoqi@0 764 if (i >= cnt) { // Found one unique input
aoqi@0 765 assert(_lrg_map.find_id(n) == _lrg_map.find_id(u), "should be the same lrg");
aoqi@0 766 n->replace_by(u); // Then replace with unique input
aoqi@0 767 n->disconnect_inputs(NULL, C);
aoqi@0 768 b->remove_node(insidx);
aoqi@0 769 insidx--;
aoqi@0 770 b->_ihrp_index--;
aoqi@0 771 b->_fhrp_index--;
aoqi@0 772 }
aoqi@0 773 }
aoqi@0 774 }
aoqi@0 775 continue;
aoqi@0 776 }
aoqi@0 777 assert( insidx > b->_ihrp_index ||
aoqi@0 778 (b->_reg_pressure < (uint)INTPRESSURE) ||
aoqi@0 779 b->_ihrp_index > 4000000 ||
aoqi@0 780 b->_ihrp_index >= b->end_idx() ||
aoqi@0 781 !b->get_node(b->_ihrp_index)->is_Proj(), "" );
aoqi@0 782 assert( insidx > b->_fhrp_index ||
aoqi@0 783 (b->_freg_pressure < (uint)FLOATPRESSURE) ||
aoqi@0 784 b->_fhrp_index > 4000000 ||
aoqi@0 785 b->_fhrp_index >= b->end_idx() ||
aoqi@0 786 !b->get_node(b->_fhrp_index)->is_Proj(), "" );
aoqi@0 787
aoqi@0 788 // ********** Handle Crossing HRP Boundry **********
aoqi@0 789 if( (insidx == b->_ihrp_index) || (insidx == b->_fhrp_index) ) {
aoqi@0 790 for( slidx = 0; slidx < spill_cnt; slidx++ ) {
aoqi@0 791 // Check for need to split at HRP boundary - split if UP
aoqi@0 792 n1 = Reachblock[slidx];
aoqi@0 793 // bail out if no reaching DEF
aoqi@0 794 if( n1 == NULL ) continue;
aoqi@0 795 // bail out if live range is 'isolated' around inner loop
aoqi@0 796 uint lidx = lidxs.at(slidx);
aoqi@0 797 // If live range is currently UP
aoqi@0 798 if( UPblock[slidx] ) {
aoqi@0 799 // set location to insert spills at
aoqi@0 800 // SPLIT DOWN HERE - NO CISC SPILL
aoqi@0 801 if( is_high_pressure( b, &lrgs(lidx), insidx ) &&
aoqi@0 802 !n1->rematerialize() ) {
aoqi@0 803 // If there is already a valid stack definition available, use it
aoqi@0 804 if( debug_defs[slidx] != NULL ) {
aoqi@0 805 Reachblock[slidx] = debug_defs[slidx];
aoqi@0 806 }
aoqi@0 807 else {
aoqi@0 808 // Insert point is just past last use or def in the block
aoqi@0 809 int insert_point = insidx-1;
aoqi@0 810 while( insert_point > 0 ) {
aoqi@0 811 Node *n = b->get_node(insert_point);
aoqi@0 812 // Hit top of block? Quit going backwards
aoqi@0 813 if (n->is_Phi()) {
aoqi@0 814 break;
aoqi@0 815 }
aoqi@0 816 // Found a def? Better split after it.
aoqi@0 817 if (_lrg_map.live_range_id(n) == lidx) {
aoqi@0 818 break;
aoqi@0 819 }
aoqi@0 820 // Look for a use
aoqi@0 821 uint i;
aoqi@0 822 for( i = 1; i < n->req(); i++ ) {
aoqi@0 823 if (_lrg_map.live_range_id(n->in(i)) == lidx) {
aoqi@0 824 break;
aoqi@0 825 }
aoqi@0 826 }
aoqi@0 827 // Found a use? Better split after it.
aoqi@0 828 if (i < n->req()) {
aoqi@0 829 break;
aoqi@0 830 }
aoqi@0 831 insert_point--;
aoqi@0 832 }
aoqi@0 833 uint orig_eidx = b->end_idx();
aoqi@0 834 maxlrg = split_DEF( n1, b, insert_point, maxlrg, Reachblock, debug_defs, splits, slidx);
aoqi@0 835 // If it wasn't split bail
aoqi@0 836 if (!maxlrg) {
aoqi@0 837 return 0;
aoqi@0 838 }
aoqi@0 839 // Spill of NULL check mem op goes into the following block.
aoqi@0 840 if (b->end_idx() > orig_eidx) {
aoqi@0 841 insidx++;
aoqi@0 842 }
aoqi@0 843 }
aoqi@0 844 // This is a new DEF, so update UP
aoqi@0 845 UPblock[slidx] = false;
aoqi@0 846 #ifndef PRODUCT
aoqi@0 847 // DEBUG
aoqi@0 848 if( trace_spilling() ) {
aoqi@0 849 tty->print("\nNew Split DOWN DEF of Spill Idx ");
aoqi@0 850 tty->print("%d, UP %d:\n",slidx,false);
aoqi@0 851 n1->dump();
aoqi@0 852 }
aoqi@0 853 #endif
aoqi@0 854 }
aoqi@0 855 } // end if LRG is UP
aoqi@0 856 } // end for all spilling live ranges
aoqi@0 857 assert( b->get_node(insidx) == n, "got insidx set incorrectly" );
aoqi@0 858 } // end if crossing HRP Boundry
aoqi@0 859
aoqi@0 860 // If the LRG index is oob, then this is a new spillcopy, skip it.
aoqi@0 861 if (defidx >= _lrg_map.max_lrg_id()) {
aoqi@0 862 continue;
aoqi@0 863 }
aoqi@0 864 LRG &deflrg = lrgs(defidx);
aoqi@0 865 uint copyidx = n->is_Copy();
aoqi@0 866 // Remove coalesced copy from CFG
aoqi@0 867 if (copyidx && defidx == _lrg_map.live_range_id(n->in(copyidx))) {
aoqi@0 868 n->replace_by( n->in(copyidx) );
aoqi@0 869 n->set_req( copyidx, NULL );
aoqi@0 870 b->remove_node(insidx--);
aoqi@0 871 b->_ihrp_index--; // Adjust the point where we go hi-pressure
aoqi@0 872 b->_fhrp_index--;
aoqi@0 873 continue;
aoqi@0 874 }
aoqi@0 875
aoqi@0 876 #define DERIVED 0
aoqi@0 877
aoqi@0 878 // ********** Handle USES **********
aoqi@0 879 bool nullcheck = false;
aoqi@0 880 // Implicit null checks never use the spilled value
aoqi@0 881 if( n->is_MachNullCheck() )
aoqi@0 882 nullcheck = true;
aoqi@0 883 if( !nullcheck ) {
aoqi@0 884 // Search all inputs for a Spill-USE
aoqi@0 885 JVMState* jvms = n->jvms();
aoqi@0 886 uint oopoff = jvms ? jvms->oopoff() : cnt;
aoqi@0 887 uint old_last = cnt - 1;
aoqi@0 888 for( inpidx = 1; inpidx < cnt; inpidx++ ) {
aoqi@0 889 // Derived/base pairs may be added to our inputs during this loop.
aoqi@0 890 // If inpidx > old_last, then one of these new inputs is being
aoqi@0 891 // handled. Skip the derived part of the pair, but process
aoqi@0 892 // the base like any other input.
aoqi@0 893 if (inpidx > old_last && ((inpidx - oopoff) & 1) == DERIVED) {
aoqi@0 894 continue; // skip derived_debug added below
aoqi@0 895 }
aoqi@0 896 // Get lidx of input
aoqi@0 897 uint useidx = _lrg_map.find_id(n->in(inpidx));
aoqi@0 898 // Not a brand-new split, and it is a spill use
aoqi@0 899 if (useidx < _lrg_map.max_lrg_id() && lrgs(useidx).reg() >= LRG::SPILL_REG) {
aoqi@0 900 // Check for valid reaching DEF
aoqi@0 901 slidx = lrg2reach[useidx];
aoqi@0 902 Node *def = Reachblock[slidx];
aoqi@0 903 assert( def != NULL, "Using Undefined Value in Split()\n");
aoqi@0 904
aoqi@0 905 // (+++) %%%% remove this in favor of pre-pass in matcher.cpp
aoqi@0 906 // monitor references do not care where they live, so just hook
aoqi@0 907 if ( jvms && jvms->is_monitor_use(inpidx) ) {
aoqi@0 908 // The effect of this clone is to drop the node out of the block,
aoqi@0 909 // so that the allocator does not see it anymore, and therefore
aoqi@0 910 // does not attempt to assign it a register.
aoqi@0 911 def = clone_node(def, b, C);
aoqi@0 912 if (def == NULL || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {
aoqi@0 913 return 0;
aoqi@0 914 }
aoqi@0 915 _lrg_map.extend(def->_idx, 0);
aoqi@0 916 _cfg.map_node_to_block(def, b);
aoqi@0 917 n->set_req(inpidx, def);
aoqi@0 918 continue;
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 // Rematerializable? Then clone def at use site instead
aoqi@0 922 // of store/load
aoqi@0 923 if( def->rematerialize() ) {
aoqi@0 924 int old_size = b->number_of_nodes();
aoqi@0 925 def = split_Rematerialize( def, b, insidx, maxlrg, splits, slidx, lrg2reach, Reachblock, true );
aoqi@0 926 if( !def ) return 0; // Bail out
aoqi@0 927 insidx += b->number_of_nodes()-old_size;
aoqi@0 928 }
aoqi@0 929
aoqi@0 930 MachNode *mach = n->is_Mach() ? n->as_Mach() : NULL;
aoqi@0 931 // Base pointers and oopmap references do not care where they live.
aoqi@0 932 if ((inpidx >= oopoff) ||
aoqi@0 933 (mach && mach->ideal_Opcode() == Op_AddP && inpidx == AddPNode::Base)) {
aoqi@0 934 if (def->rematerialize() && lrgs(useidx)._was_spilled2) {
aoqi@0 935 // This def has been rematerialized a couple of times without
aoqi@0 936 // progress. It doesn't care if it lives UP or DOWN, so
aoqi@0 937 // spill it down now.
aoqi@0 938 maxlrg = split_USE(def,b,n,inpidx,maxlrg,false,false,splits,slidx);
aoqi@0 939 // If it wasn't split bail
aoqi@0 940 if (!maxlrg) {
aoqi@0 941 return 0;
aoqi@0 942 }
aoqi@0 943 insidx++; // Reset iterator to skip USE side split
aoqi@0 944 } else {
aoqi@0 945 // Just hook the def edge
aoqi@0 946 n->set_req(inpidx, def);
aoqi@0 947 }
aoqi@0 948
aoqi@0 949 if (inpidx >= oopoff) {
aoqi@0 950 // After oopoff, we have derived/base pairs. We must mention all
aoqi@0 951 // derived pointers here as derived/base pairs for GC. If the
aoqi@0 952 // derived value is spilling and we have a copy both in Reachblock
aoqi@0 953 // (called here 'def') and debug_defs[slidx] we need to mention
aoqi@0 954 // both in derived/base pairs or kill one.
aoqi@0 955 Node *derived_debug = debug_defs[slidx];
aoqi@0 956 if( ((inpidx - oopoff) & 1) == DERIVED && // derived vs base?
aoqi@0 957 mach && mach->ideal_Opcode() != Op_Halt &&
aoqi@0 958 derived_debug != NULL &&
aoqi@0 959 derived_debug != def ) { // Actual 2nd value appears
aoqi@0 960 // We have already set 'def' as a derived value.
aoqi@0 961 // Also set debug_defs[slidx] as a derived value.
aoqi@0 962 uint k;
aoqi@0 963 for( k = oopoff; k < cnt; k += 2 )
aoqi@0 964 if( n->in(k) == derived_debug )
aoqi@0 965 break; // Found an instance of debug derived
aoqi@0 966 if( k == cnt ) {// No instance of debug_defs[slidx]
aoqi@0 967 // Add a derived/base pair to cover the debug info.
aoqi@0 968 // We have to process the added base later since it is not
aoqi@0 969 // handled yet at this point but skip derived part.
aoqi@0 970 assert(((n->req() - oopoff) & 1) == DERIVED,
aoqi@0 971 "must match skip condition above");
aoqi@0 972 n->add_req( derived_debug ); // this will be skipped above
aoqi@0 973 n->add_req( n->in(inpidx+1) ); // this will be processed
aoqi@0 974 // Increment cnt to handle added input edges on
aoqi@0 975 // subsequent iterations.
aoqi@0 976 cnt += 2;
aoqi@0 977 }
aoqi@0 978 }
aoqi@0 979 }
aoqi@0 980 continue;
aoqi@0 981 }
aoqi@0 982 // Special logic for DEBUG info
aoqi@0 983 if( jvms && b->_freq > BLOCK_FREQUENCY(0.5) ) {
aoqi@0 984 uint debug_start = jvms->debug_start();
aoqi@0 985 // If this is debug info use & there is a reaching DOWN def
aoqi@0 986 if ((debug_start <= inpidx) && (debug_defs[slidx] != NULL)) {
aoqi@0 987 assert(inpidx < oopoff, "handle only debug info here");
aoqi@0 988 // Just hook it in & move on
aoqi@0 989 n->set_req(inpidx, debug_defs[slidx]);
aoqi@0 990 // (Note that this can make two sides of a split live at the
aoqi@0 991 // same time: The debug def on stack, and another def in a
aoqi@0 992 // register. The GC needs to know about both of them, but any
aoqi@0 993 // derived pointers after oopoff will refer to only one of the
aoqi@0 994 // two defs and the GC would therefore miss the other. Thus
aoqi@0 995 // this hack is only allowed for debug info which is Java state
aoqi@0 996 // and therefore never a derived pointer.)
aoqi@0 997 continue;
aoqi@0 998 }
aoqi@0 999 }
aoqi@0 1000 // Grab register mask info
aoqi@0 1001 const RegMask &dmask = def->out_RegMask();
aoqi@0 1002 const RegMask &umask = n->in_RegMask(inpidx);
aoqi@0 1003 bool is_vect = RegMask::is_vector(def->ideal_reg());
aoqi@0 1004 assert(inpidx < oopoff, "cannot use-split oop map info");
aoqi@0 1005
aoqi@0 1006 bool dup = UPblock[slidx];
aoqi@0 1007 bool uup = umask.is_UP();
aoqi@0 1008
aoqi@0 1009 // Need special logic to handle bound USES. Insert a split at this
aoqi@0 1010 // bound use if we can't rematerialize the def, or if we need the
aoqi@0 1011 // split to form a misaligned pair.
aoqi@0 1012 if( !umask.is_AllStack() &&
aoqi@0 1013 (int)umask.Size() <= lrgs(useidx).num_regs() &&
aoqi@0 1014 (!def->rematerialize() ||
aoqi@0 1015 !is_vect && umask.is_misaligned_pair())) {
aoqi@0 1016 // These need a Split regardless of overlap or pressure
aoqi@0 1017 // SPLIT - NO DEF - NO CISC SPILL
aoqi@0 1018 maxlrg = split_USE(def,b,n,inpidx,maxlrg,dup,false, splits,slidx);
aoqi@0 1019 // If it wasn't split bail
aoqi@0 1020 if (!maxlrg) {
aoqi@0 1021 return 0;
aoqi@0 1022 }
aoqi@0 1023 insidx++; // Reset iterator to skip USE side split
aoqi@0 1024 continue;
aoqi@0 1025 }
aoqi@0 1026
aoqi@0 1027 if (UseFPUForSpilling && n->is_MachCall() && !uup && !dup ) {
aoqi@0 1028 // The use at the call can force the def down so insert
aoqi@0 1029 // a split before the use to allow the def more freedom.
aoqi@0 1030 maxlrg = split_USE(def,b,n,inpidx,maxlrg,dup,false, splits,slidx);
aoqi@0 1031 // If it wasn't split bail
aoqi@0 1032 if (!maxlrg) {
aoqi@0 1033 return 0;
aoqi@0 1034 }
aoqi@0 1035 insidx++; // Reset iterator to skip USE side split
aoqi@0 1036 continue;
aoqi@0 1037 }
aoqi@0 1038
aoqi@0 1039 // Here is the logic chart which describes USE Splitting:
aoqi@0 1040 // 0 = false or DOWN, 1 = true or UP
aoqi@0 1041 //
aoqi@0 1042 // Overlap | DEF | USE | Action
aoqi@0 1043 //-------------------------------------------------------
aoqi@0 1044 // 0 | 0 | 0 | Copy - mem -> mem
aoqi@0 1045 // 0 | 0 | 1 | Split-UP - Check HRP
aoqi@0 1046 // 0 | 1 | 0 | Split-DOWN - Debug Info?
aoqi@0 1047 // 0 | 1 | 1 | Copy - reg -> reg
aoqi@0 1048 // 1 | 0 | 0 | Reset Input Edge (no Split)
aoqi@0 1049 // 1 | 0 | 1 | Split-UP - Check HRP
aoqi@0 1050 // 1 | 1 | 0 | Split-DOWN - Debug Info?
aoqi@0 1051 // 1 | 1 | 1 | Reset Input Edge (no Split)
aoqi@0 1052 //
aoqi@0 1053 // So, if (dup == uup), then overlap test determines action,
aoqi@0 1054 // with true being no split, and false being copy. Else,
aoqi@0 1055 // if DEF is DOWN, Split-UP, and check HRP to decide on
aoqi@0 1056 // resetting DEF. Finally if DEF is UP, Split-DOWN, with
aoqi@0 1057 // special handling for Debug Info.
aoqi@0 1058 if( dup == uup ) {
aoqi@0 1059 if( dmask.overlap(umask) ) {
aoqi@0 1060 // Both are either up or down, and there is overlap, No Split
aoqi@0 1061 n->set_req(inpidx, def);
aoqi@0 1062 }
aoqi@0 1063 else { // Both are either up or down, and there is no overlap
aoqi@0 1064 if( dup ) { // If UP, reg->reg copy
aoqi@0 1065 // COPY ACROSS HERE - NO DEF - NO CISC SPILL
aoqi@0 1066 maxlrg = split_USE(def,b,n,inpidx,maxlrg,false,false, splits,slidx);
aoqi@0 1067 // If it wasn't split bail
aoqi@0 1068 if (!maxlrg) {
aoqi@0 1069 return 0;
aoqi@0 1070 }
aoqi@0 1071 insidx++; // Reset iterator to skip USE side split
aoqi@0 1072 }
aoqi@0 1073 else { // DOWN, mem->mem copy
aoqi@0 1074 // COPY UP & DOWN HERE - NO DEF - NO CISC SPILL
aoqi@0 1075 // First Split-UP to move value into Register
aoqi@0 1076 uint def_ideal = def->ideal_reg();
aoqi@0 1077 const RegMask* tmp_rm = Matcher::idealreg2regmask[def_ideal];
aoqi@0 1078 Node *spill = new (C) MachSpillCopyNode(def, dmask, *tmp_rm);
aoqi@0 1079 insert_proj( b, insidx, spill, maxlrg );
aoqi@0 1080 // Then Split-DOWN as if previous Split was DEF
aoqi@0 1081 maxlrg = split_USE(spill,b,n,inpidx,maxlrg,false,false, splits,slidx);
aoqi@0 1082 // If it wasn't split bail
aoqi@0 1083 if (!maxlrg) {
aoqi@0 1084 return 0;
aoqi@0 1085 }
aoqi@0 1086 insidx += 2; // Reset iterator to skip USE side splits
aoqi@0 1087 }
aoqi@0 1088 } // End else no overlap
aoqi@0 1089 } // End if dup == uup
aoqi@0 1090 // dup != uup, so check dup for direction of Split
aoqi@0 1091 else {
aoqi@0 1092 if( dup ) { // If UP, Split-DOWN and check Debug Info
aoqi@0 1093 // If this node is already a SpillCopy, just patch the edge
aoqi@0 1094 // except the case of spilling to stack.
aoqi@0 1095 if( n->is_SpillCopy() ) {
aoqi@0 1096 RegMask tmp_rm(umask);
aoqi@0 1097 tmp_rm.SUBTRACT(Matcher::STACK_ONLY_mask);
aoqi@0 1098 if( dmask.overlap(tmp_rm) ) {
aoqi@0 1099 if( def != n->in(inpidx) ) {
aoqi@0 1100 n->set_req(inpidx, def);
aoqi@0 1101 }
aoqi@0 1102 continue;
aoqi@0 1103 }
aoqi@0 1104 }
aoqi@0 1105 // COPY DOWN HERE - NO DEF - NO CISC SPILL
aoqi@0 1106 maxlrg = split_USE(def,b,n,inpidx,maxlrg,false,false, splits,slidx);
aoqi@0 1107 // If it wasn't split bail
aoqi@0 1108 if (!maxlrg) {
aoqi@0 1109 return 0;
aoqi@0 1110 }
aoqi@0 1111 insidx++; // Reset iterator to skip USE side split
aoqi@0 1112 // Check for debug-info split. Capture it for later
aoqi@0 1113 // debug splits of the same value
aoqi@0 1114 if (jvms && jvms->debug_start() <= inpidx && inpidx < oopoff)
aoqi@0 1115 debug_defs[slidx] = n->in(inpidx);
aoqi@0 1116
aoqi@0 1117 }
aoqi@0 1118 else { // DOWN, Split-UP and check register pressure
aoqi@0 1119 if( is_high_pressure( b, &lrgs(useidx), insidx ) ) {
aoqi@0 1120 // COPY UP HERE - NO DEF - CISC SPILL
aoqi@0 1121 maxlrg = split_USE(def,b,n,inpidx,maxlrg,true,true, splits,slidx);
aoqi@0 1122 // If it wasn't split bail
aoqi@0 1123 if (!maxlrg) {
aoqi@0 1124 return 0;
aoqi@0 1125 }
aoqi@0 1126 insidx++; // Reset iterator to skip USE side split
aoqi@0 1127 } else { // LRP
aoqi@0 1128 // COPY UP HERE - WITH DEF - NO CISC SPILL
aoqi@0 1129 maxlrg = split_USE(def,b,n,inpidx,maxlrg,true,false, splits,slidx);
aoqi@0 1130 // If it wasn't split bail
aoqi@0 1131 if (!maxlrg) {
aoqi@0 1132 return 0;
aoqi@0 1133 }
aoqi@0 1134 // Flag this lift-up in a low-pressure block as
aoqi@0 1135 // already-spilled, so if it spills again it will
aoqi@0 1136 // spill hard (instead of not spilling hard and
aoqi@0 1137 // coalescing away).
aoqi@0 1138 set_was_spilled(n->in(inpidx));
aoqi@0 1139 // Since this is a new DEF, update Reachblock & UP
aoqi@0 1140 Reachblock[slidx] = n->in(inpidx);
aoqi@0 1141 UPblock[slidx] = true;
aoqi@0 1142 insidx++; // Reset iterator to skip USE side split
aoqi@0 1143 }
aoqi@0 1144 } // End else DOWN
aoqi@0 1145 } // End dup != uup
aoqi@0 1146 } // End if Spill USE
aoqi@0 1147 } // End For All Inputs
aoqi@0 1148 } // End If not nullcheck
aoqi@0 1149
aoqi@0 1150 // ********** Handle DEFS **********
aoqi@0 1151 // DEFS either Split DOWN in HRP regions or when the LRG is bound, or
aoqi@0 1152 // just reset the Reaches info in LRP regions. DEFS must always update
aoqi@0 1153 // UP info.
aoqi@0 1154 if( deflrg.reg() >= LRG::SPILL_REG ) { // Spilled?
aoqi@0 1155 uint slidx = lrg2reach[defidx];
aoqi@0 1156 // Add to defs list for later assignment of new live range number
aoqi@0 1157 defs->push(n);
aoqi@0 1158 // Set a flag on the Node indicating it has already spilled.
aoqi@0 1159 // Only do it for capacity spills not conflict spills.
aoqi@0 1160 if( !deflrg._direct_conflict )
aoqi@0 1161 set_was_spilled(n);
aoqi@0 1162 assert(!n->is_Phi(),"Cannot insert Phi into DEFS list");
aoqi@0 1163 // Grab UP info for DEF
aoqi@0 1164 const RegMask &dmask = n->out_RegMask();
aoqi@0 1165 bool defup = dmask.is_UP();
aoqi@0 1166 int ireg = n->ideal_reg();
aoqi@0 1167 bool is_vect = RegMask::is_vector(ireg);
aoqi@0 1168 // Only split at Def if this is a HRP block or bound (and spilled once)
aoqi@0 1169 if( !n->rematerialize() &&
aoqi@0 1170 (((dmask.is_bound(ireg) || !is_vect && dmask.is_misaligned_pair()) &&
aoqi@0 1171 (deflrg._direct_conflict || deflrg._must_spill)) ||
aoqi@0 1172 // Check for LRG being up in a register and we are inside a high
aoqi@0 1173 // pressure area. Spill it down immediately.
aoqi@0 1174 (defup && is_high_pressure(b,&deflrg,insidx))) ) {
aoqi@0 1175 assert( !n->rematerialize(), "" );
aoqi@0 1176 assert( !n->is_SpillCopy(), "" );
aoqi@0 1177 // Do a split at the def site.
aoqi@0 1178 maxlrg = split_DEF( n, b, insidx, maxlrg, Reachblock, debug_defs, splits, slidx );
aoqi@0 1179 // If it wasn't split bail
aoqi@0 1180 if (!maxlrg) {
aoqi@0 1181 return 0;
aoqi@0 1182 }
aoqi@0 1183 // Split DEF's Down
aoqi@0 1184 UPblock[slidx] = 0;
aoqi@0 1185 #ifndef PRODUCT
aoqi@0 1186 // DEBUG
aoqi@0 1187 if( trace_spilling() ) {
aoqi@0 1188 tty->print("\nNew Split DOWN DEF of Spill Idx ");
aoqi@0 1189 tty->print("%d, UP %d:\n",slidx,false);
aoqi@0 1190 n->dump();
aoqi@0 1191 }
aoqi@0 1192 #endif
aoqi@0 1193 }
aoqi@0 1194 else { // Neither bound nor HRP, must be LRP
aoqi@0 1195 // otherwise, just record the def
aoqi@0 1196 Reachblock[slidx] = n;
aoqi@0 1197 // UP should come from the outRegmask() of the DEF
aoqi@0 1198 UPblock[slidx] = defup;
aoqi@0 1199 // Update debug list of reaching down definitions, kill if DEF is UP
aoqi@0 1200 debug_defs[slidx] = defup ? NULL : n;
aoqi@0 1201 #ifndef PRODUCT
aoqi@0 1202 // DEBUG
aoqi@0 1203 if( trace_spilling() ) {
aoqi@0 1204 tty->print("\nNew DEF of Spill Idx ");
aoqi@0 1205 tty->print("%d, UP %d:\n",slidx,defup);
aoqi@0 1206 n->dump();
aoqi@0 1207 }
aoqi@0 1208 #endif
aoqi@0 1209 } // End else LRP
aoqi@0 1210 } // End if spill def
aoqi@0 1211
aoqi@0 1212 // ********** Split Left Over Mem-Mem Moves **********
aoqi@0 1213 // Check for mem-mem copies and split them now. Do not do this
aoqi@0 1214 // to copies about to be spilled; they will be Split shortly.
aoqi@0 1215 if (copyidx) {
aoqi@0 1216 Node *use = n->in(copyidx);
aoqi@0 1217 uint useidx = _lrg_map.find_id(use);
aoqi@0 1218 if (useidx < _lrg_map.max_lrg_id() && // This is not a new split
aoqi@0 1219 OptoReg::is_stack(deflrg.reg()) &&
aoqi@0 1220 deflrg.reg() < LRG::SPILL_REG ) { // And DEF is from stack
aoqi@0 1221 LRG &uselrg = lrgs(useidx);
aoqi@0 1222 if( OptoReg::is_stack(uselrg.reg()) &&
aoqi@0 1223 uselrg.reg() < LRG::SPILL_REG && // USE is from stack
aoqi@0 1224 deflrg.reg() != uselrg.reg() ) { // Not trivially removed
aoqi@0 1225 uint def_ideal_reg = n->bottom_type()->ideal_reg();
aoqi@0 1226 const RegMask &def_rm = *Matcher::idealreg2regmask[def_ideal_reg];
aoqi@0 1227 const RegMask &use_rm = n->in_RegMask(copyidx);
aoqi@0 1228 if( def_rm.overlap(use_rm) && n->is_SpillCopy() ) { // Bug 4707800, 'n' may be a storeSSL
aoqi@0 1229 if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) { // Check when generating nodes
aoqi@0 1230 return 0;
aoqi@0 1231 }
aoqi@0 1232 Node *spill = new (C) MachSpillCopyNode(use,use_rm,def_rm);
aoqi@0 1233 n->set_req(copyidx,spill);
aoqi@0 1234 n->as_MachSpillCopy()->set_in_RegMask(def_rm);
aoqi@0 1235 // Put the spill just before the copy
aoqi@0 1236 insert_proj( b, insidx++, spill, maxlrg++ );
aoqi@0 1237 }
aoqi@0 1238 }
aoqi@0 1239 }
aoqi@0 1240 }
aoqi@0 1241 } // End For All Instructions in Block - Non-PHI Pass
aoqi@0 1242
aoqi@0 1243 // Check if each LRG is live out of this block so as not to propagate
aoqi@0 1244 // beyond the last use of a LRG.
aoqi@0 1245 for( slidx = 0; slidx < spill_cnt; slidx++ ) {
aoqi@0 1246 uint defidx = lidxs.at(slidx);
aoqi@0 1247 IndexSet *liveout = _live->live(b);
aoqi@0 1248 if( !liveout->member(defidx) ) {
aoqi@0 1249 #ifdef ASSERT
aoqi@0 1250 // The index defidx is not live. Check the liveout array to ensure that
aoqi@0 1251 // it contains no members which compress to defidx. Finding such an
aoqi@0 1252 // instance may be a case to add liveout adjustment in compress_uf_map().
aoqi@0 1253 // See 5063219.
aoqi@0 1254 uint member;
aoqi@0 1255 IndexSetIterator isi(liveout);
aoqi@0 1256 while ((member = isi.next()) != 0) {
aoqi@0 1257 assert(defidx != _lrg_map.find_const(member), "Live out member has not been compressed");
aoqi@0 1258 }
aoqi@0 1259 #endif
aoqi@0 1260 Reachblock[slidx] = NULL;
aoqi@0 1261 } else {
aoqi@0 1262 assert(Reachblock[slidx] != NULL,"No reaching definition for liveout value");
aoqi@0 1263 }
aoqi@0 1264 }
aoqi@0 1265 #ifndef PRODUCT
aoqi@0 1266 if( trace_spilling() )
aoqi@0 1267 b->dump();
aoqi@0 1268 #endif
aoqi@0 1269 } // End For All Blocks
aoqi@0 1270
aoqi@0 1271 //----------PASS 2----------
aoqi@0 1272 // Reset all DEF live range numbers here
aoqi@0 1273 for( insidx = 0; insidx < defs->size(); insidx++ ) {
aoqi@0 1274 // Grab the def
aoqi@0 1275 n1 = defs->at(insidx);
aoqi@0 1276 // Set new lidx for DEF
aoqi@0 1277 new_lrg(n1, maxlrg++);
aoqi@0 1278 }
aoqi@0 1279 //----------Phi Node Splitting----------
aoqi@0 1280 // Clean up a phi here, and assign a new live range number
aoqi@0 1281 // Cycle through this block's predecessors, collecting Reaches
aoqi@0 1282 // info for each spilled LRG and update edges.
aoqi@0 1283 // Walk the phis list to patch inputs, split phis, and name phis
aoqi@0 1284 uint lrgs_before_phi_split = maxlrg;
aoqi@0 1285 for( insidx = 0; insidx < phis->size(); insidx++ ) {
aoqi@0 1286 Node *phi = phis->at(insidx);
aoqi@0 1287 assert(phi->is_Phi(),"This list must only contain Phi Nodes");
aoqi@0 1288 Block *b = _cfg.get_block_for_node(phi);
aoqi@0 1289 // Grab the live range number
aoqi@0 1290 uint lidx = _lrg_map.find_id(phi);
aoqi@0 1291 uint slidx = lrg2reach[lidx];
aoqi@0 1292 // Update node to lidx map
aoqi@0 1293 new_lrg(phi, maxlrg++);
aoqi@0 1294 // Get PASS1's up/down decision for the block.
aoqi@0 1295 int phi_up = !!UP_entry[slidx]->test(b->_pre_order);
aoqi@0 1296
aoqi@0 1297 // Force down if double-spilling live range
aoqi@0 1298 if( lrgs(lidx)._was_spilled1 )
aoqi@0 1299 phi_up = false;
aoqi@0 1300
aoqi@0 1301 // When splitting a Phi we an split it normal or "inverted".
aoqi@0 1302 // An inverted split makes the splits target the Phi's UP/DOWN
aoqi@0 1303 // sense inverted; then the Phi is followed by a final def-side
aoqi@0 1304 // split to invert back. It changes which blocks the spill code
aoqi@0 1305 // goes in.
aoqi@0 1306
aoqi@0 1307 // Walk the predecessor blocks and assign the reaching def to the Phi.
aoqi@0 1308 // Split Phi nodes by placing USE side splits wherever the reaching
aoqi@0 1309 // DEF has the wrong UP/DOWN value.
aoqi@0 1310 for( uint i = 1; i < b->num_preds(); i++ ) {
aoqi@0 1311 // Get predecessor block pre-order number
aoqi@0 1312 Block *pred = _cfg.get_block_for_node(b->pred(i));
aoqi@0 1313 pidx = pred->_pre_order;
aoqi@0 1314 // Grab reaching def
aoqi@0 1315 Node *def = Reaches[pidx][slidx];
aoqi@0 1316 Node** Reachblock = Reaches[pidx];
aoqi@0 1317 assert( def, "must have reaching def" );
aoqi@0 1318 // If input up/down sense and reg-pressure DISagree
aoqi@0 1319 if (def->rematerialize()) {
aoqi@0 1320 // Place the rematerialized node above any MSCs created during
aoqi@0 1321 // phi node splitting. end_idx points at the insertion point
aoqi@0 1322 // so look at the node before it.
aoqi@0 1323 int insert = pred->end_idx();
aoqi@0 1324 while (insert >= 1 &&
aoqi@0 1325 pred->get_node(insert - 1)->is_SpillCopy() &&
aoqi@0 1326 _lrg_map.find(pred->get_node(insert - 1)) >= lrgs_before_phi_split) {
aoqi@0 1327 insert--;
aoqi@0 1328 }
aoqi@0 1329 def = split_Rematerialize(def, pred, insert, maxlrg, splits, slidx, lrg2reach, Reachblock, false);
aoqi@0 1330 if (!def) {
aoqi@0 1331 return 0; // Bail out
aoqi@0 1332 }
aoqi@0 1333 }
aoqi@0 1334 // Update the Phi's input edge array
aoqi@0 1335 phi->set_req(i,def);
aoqi@0 1336 // Grab the UP/DOWN sense for the input
aoqi@0 1337 u1 = UP[pidx][slidx];
aoqi@0 1338 if( u1 != (phi_up != 0)) {
aoqi@0 1339 maxlrg = split_USE(def, b, phi, i, maxlrg, !u1, false, splits,slidx);
aoqi@0 1340 // If it wasn't split bail
aoqi@0 1341 if (!maxlrg) {
aoqi@0 1342 return 0;
aoqi@0 1343 }
aoqi@0 1344 }
aoqi@0 1345 } // End for all inputs to the Phi
aoqi@0 1346 } // End for all Phi Nodes
aoqi@0 1347 // Update _maxlrg to save Union asserts
aoqi@0 1348 _lrg_map.set_max_lrg_id(maxlrg);
aoqi@0 1349
aoqi@0 1350
aoqi@0 1351 //----------PASS 3----------
aoqi@0 1352 // Pass over all Phi's to union the live ranges
aoqi@0 1353 for( insidx = 0; insidx < phis->size(); insidx++ ) {
aoqi@0 1354 Node *phi = phis->at(insidx);
aoqi@0 1355 assert(phi->is_Phi(),"This list must only contain Phi Nodes");
aoqi@0 1356 // Walk all inputs to Phi and Union input live range with Phi live range
aoqi@0 1357 for( uint i = 1; i < phi->req(); i++ ) {
aoqi@0 1358 // Grab the input node
aoqi@0 1359 Node *n = phi->in(i);
aoqi@0 1360 assert(n, "node should exist");
aoqi@0 1361 uint lidx = _lrg_map.find(n);
aoqi@0 1362 uint pidx = _lrg_map.find(phi);
aoqi@0 1363 if (lidx < pidx) {
aoqi@0 1364 Union(n, phi);
aoqi@0 1365 }
aoqi@0 1366 else if(lidx > pidx) {
aoqi@0 1367 Union(phi, n);
aoqi@0 1368 }
aoqi@0 1369 } // End for all inputs to the Phi Node
aoqi@0 1370 } // End for all Phi Nodes
aoqi@0 1371 // Now union all two address instructions
aoqi@0 1372 for (insidx = 0; insidx < defs->size(); insidx++) {
aoqi@0 1373 // Grab the def
aoqi@0 1374 n1 = defs->at(insidx);
aoqi@0 1375 // Set new lidx for DEF & handle 2-addr instructions
aoqi@0 1376 if (n1->is_Mach() && ((twoidx = n1->as_Mach()->two_adr()) != 0)) {
aoqi@0 1377 assert(_lrg_map.find(n1->in(twoidx)) < maxlrg,"Assigning bad live range index");
aoqi@0 1378 // Union the input and output live ranges
aoqi@0 1379 uint lr1 = _lrg_map.find(n1);
aoqi@0 1380 uint lr2 = _lrg_map.find(n1->in(twoidx));
aoqi@0 1381 if (lr1 < lr2) {
aoqi@0 1382 Union(n1, n1->in(twoidx));
aoqi@0 1383 }
aoqi@0 1384 else if (lr1 > lr2) {
aoqi@0 1385 Union(n1->in(twoidx), n1);
aoqi@0 1386 }
aoqi@0 1387 } // End if two address
aoqi@0 1388 } // End for all defs
aoqi@0 1389 // DEBUG
aoqi@0 1390 #ifdef ASSERT
aoqi@0 1391 // Validate all live range index assignments
aoqi@0 1392 for (bidx = 0; bidx < _cfg.number_of_blocks(); bidx++) {
aoqi@0 1393 b = _cfg.get_block(bidx);
aoqi@0 1394 for (insidx = 0; insidx <= b->end_idx(); insidx++) {
aoqi@0 1395 Node *n = b->get_node(insidx);
aoqi@0 1396 uint defidx = _lrg_map.find(n);
aoqi@0 1397 assert(defidx < _lrg_map.max_lrg_id(), "Bad live range index in Split");
aoqi@0 1398 assert(defidx < maxlrg,"Bad live range index in Split");
aoqi@0 1399 }
aoqi@0 1400 }
aoqi@0 1401 // Issue a warning if splitting made no progress
aoqi@0 1402 int noprogress = 0;
aoqi@0 1403 for (slidx = 0; slidx < spill_cnt; slidx++) {
aoqi@0 1404 if (PrintOpto && WizardMode && splits.at(slidx) == 0) {
aoqi@0 1405 tty->print_cr("Failed to split live range %d", lidxs.at(slidx));
aoqi@0 1406 //BREAKPOINT;
aoqi@0 1407 }
aoqi@0 1408 else {
aoqi@0 1409 noprogress++;
aoqi@0 1410 }
aoqi@0 1411 }
aoqi@0 1412 if(!noprogress) {
aoqi@0 1413 tty->print_cr("Failed to make progress in Split");
aoqi@0 1414 //BREAKPOINT;
aoqi@0 1415 }
aoqi@0 1416 #endif
aoqi@0 1417 // Return updated count of live ranges
aoqi@0 1418 return maxlrg;
aoqi@0 1419 }

mercurial