jdk
1/*
2* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*
23*/
24
25#include "precompiled.hpp"26#include "libadt/vectset.hpp"27#include "memory/allocation.inline.hpp"28#include "memory/resourceArea.inline.hpp"29#include "opto/addnode.hpp"30#include "opto/c2compiler.hpp"31#include "opto/callnode.hpp"32#include "opto/cfgnode.hpp"33#include "opto/chaitin.hpp"34#include "opto/loopnode.hpp"35#include "opto/machnode.hpp"36
37//------------------------------Split--------------------------------------
38// Walk the graph in RPO and for each lrg which spills, propagate reaching
39// definitions. During propagation, split the live range around regions of
40// High Register Pressure (HRP). If a Def is in a region of Low Register
41// Pressure (LRP), it will not get spilled until we encounter a region of
42// HRP between it and one of its uses. We will spill at the transition
43// point between LRP and HRP. Uses in the HRP region will use the spilled
44// Def. The first Use outside the HRP region will generate a SpillCopy to
45// hoist the live range back up into a register, and all subsequent uses
46// will use that new Def until another HRP region is encountered. Defs in
47// HRP regions will get trailing SpillCopies to push the LRG down into the
48// stack immediately.
49//
50// As a side effect, unlink from (hence make dead) coalesced copies.
51//
52
53static const char out_of_nodes[] = "out of nodes during split";54
55//------------------------------get_spillcopy_wide-----------------------------
56// Get a SpillCopy node with wide-enough masks. Use the 'wide-mask', the
57// wide ideal-register spill-mask if possible. If the 'wide-mask' does
58// not cover the input (or output), use the input (or output) mask instead.
59Node *PhaseChaitin::get_spillcopy_wide(MachSpillCopyNode::SpillType spill_type, Node *def, Node *use, uint uidx) {60// If ideal reg doesn't exist we've got a bad schedule happening61// that is forcing us to spill something that isn't spillable.62// Bail rather than abort63uint ireg = def->ideal_reg();64if (ireg == 0 || ireg == Op_RegFlags) {65assert(false, "attempted to spill a non-spillable item: %d: %s <- %d: %s, ireg = %u, spill_type: %s",66def->_idx, def->Name(), use->_idx, use->Name(), ireg,67MachSpillCopyNode::spill_type(spill_type));68C->record_method_not_compilable("attempted to spill a non-spillable item");69return nullptr;70}71if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {72return nullptr;73}74const RegMask *i_mask = &def->out_RegMask();75const RegMask *w_mask = C->matcher()->idealreg2spillmask[ireg];76const RegMask *o_mask = use ? &use->in_RegMask(uidx) : w_mask;77const RegMask *w_i_mask = w_mask->overlap( *i_mask ) ? w_mask : i_mask;78const RegMask *w_o_mask;79
80int num_regs = RegMask::num_registers(ireg);81bool is_vect = RegMask::is_vector(ireg);82if( w_mask->overlap( *o_mask ) && // Overlap AND83(num_regs == 1 // Single use or aligned84|| is_vect // or vector85|| (!is_vect && o_mask->is_aligned_pairs())) ) {86assert(!is_vect || o_mask->is_aligned_sets(num_regs), "vectors are aligned");87// Don't come here for mis-aligned doubles88w_o_mask = w_mask;89} else { // wide ideal mask does not overlap with o_mask90// Mis-aligned doubles come here and XMM->FPR moves on x86.91w_o_mask = o_mask; // Must target desired registers92// Does the ideal-reg-mask overlap with o_mask? I.e., can I use93// a reg-reg move or do I need a trip across register classes94// (and thus through memory)?95if( !C->matcher()->idealreg2regmask[ireg]->overlap( *o_mask) && o_mask->is_UP() )96// Here we assume a trip through memory is required.97w_i_mask = &C->FIRST_STACK_mask();98}99return new MachSpillCopyNode(spill_type, def, *w_i_mask, *w_o_mask );100}
101
102//------------------------------insert_proj------------------------------------
103// Insert the spill at chosen location. Skip over any intervening Proj's or
104// Phis. Skip over a CatchNode and projs, inserting in the fall-through block
105// instead. Update high-pressure indices. Create a new live range.
106void PhaseChaitin::insert_proj( Block *b, uint i, Node *spill, uint maxlrg ) {107// Skip intervening ProjNodes. Do not insert between a ProjNode and108// its definer.109while( i < b->number_of_nodes() &&110(b->get_node(i)->is_Proj() ||111b->get_node(i)->is_Phi() ) )112i++;113
114// Do not insert between a call and his Catch115if( b->get_node(i)->is_Catch() ) {116// Put the instruction at the top of the fall-thru block.117// This assumes that the instruction is not used in the other exception118// blocks. Global code motion is responsible for maintaining this invariant.119// Find the fall-thru projection120while( 1 ) {121const CatchProjNode *cp = b->get_node(++i)->as_CatchProj();122if( cp->_con == CatchProjNode::fall_through_index )123break;124}125int sidx = i - b->end_idx()-1;126b = b->_succs[sidx]; // Switch to successor block127i = 1; // Right at start of block128}129
130b->insert_node(spill, i); // Insert node in block131_cfg.map_node_to_block(spill, b); // Update node->block mapping to reflect132// Adjust the point where we go hi-pressure133if( i <= b->_ihrp_index ) b->_ihrp_index++;134if( i <= b->_fhrp_index ) b->_fhrp_index++;135
136// Assign a new Live Range Number to the SpillCopy and grow137// the node->live range mapping.138new_lrg(spill,maxlrg);139}
140
141//------------------------------split_DEF--------------------------------------
142// There are four categories of Split; UP/DOWN x DEF/USE
143// Only three of these really occur as DOWN/USE will always color
144// Any Split with a DEF cannot CISC-Spill now. Thus we need
145// two helper routines, one for Split DEFS (insert after instruction),
146// one for Split USES (insert before instruction). DEF insertion
147// happens inside Split, where the Leaveblock array is updated.
148uint PhaseChaitin::split_DEF( Node *def, Block *b, int loc, uint maxlrg, Node **Reachblock, Node **debug_defs, GrowableArray<uint> splits, int slidx ) {149#ifdef ASSERT150// Increment the counter for this lrg151splits.at_put(slidx, splits.at(slidx)+1);152#endif153// If we are spilling the memory op for an implicit null check, at the154// null check location (ie - null check is in HRP block) we need to do155// the null-check first, then spill-down in the following block.156// (The implicit_null_check function ensures the use is also dominated157// by the branch-not-taken block.)158Node *be = b->end();159if( be->is_MachNullCheck() && be->in(1) == def && def == b->get_node(loc)) {160// Spill goes in the branch-not-taken block161b = b->_succs[b->get_node(b->end_idx()+1)->Opcode() == Op_IfTrue];162loc = 0; // Just past the Region163}164assert( loc >= 0, "must insert past block head" );165
166// Get a def-side SpillCopy167Node *spill = get_spillcopy_wide(MachSpillCopyNode::Definition, def, nullptr, 0);168// Did we fail to split?, then bail169if (!spill) {170return 0;171}172
173// Insert the spill at chosen location174insert_proj( b, loc+1, spill, maxlrg++);175
176// Insert new node into Reaches array177Reachblock[slidx] = spill;178// Update debug list of reaching down definitions by adding this one179debug_defs[slidx] = spill;180
181// return updated count of live ranges182return maxlrg;183}
184
185//------------------------------split_USE--------------------------------------
186// Splits at uses can involve redeffing the LRG, so no CISC Spilling there.
187// Debug uses want to know if def is already stack enabled.
188// Return value
189// -1 : bailout, 0: no spillcopy created, 1: create a new spillcopy
190int PhaseChaitin::split_USE(MachSpillCopyNode::SpillType spill_type, Node *def, Block *b, Node *use, uint useidx, uint maxlrg, bool def_down, bool cisc_sp, GrowableArray<uint> splits, int slidx ) {191#ifdef ASSERT192// Increment the counter for this lrg193splits.at_put(slidx, splits.at(slidx)+1);194#endif195
196// Some setup stuff for handling debug node uses197JVMState* jvms = use->jvms();198uint debug_start = jvms ? jvms->debug_start() : 999999;199uint debug_end = jvms ? jvms->debug_end() : 999999;200
201//-------------------------------------------202// Check for use of debug info203if (useidx >= debug_start && useidx < debug_end) {204// Actually it's perfectly legal for constant debug info to appear205// just unlikely. In this case the optimizer left a ConI of a 4206// as both inputs to a Phi with only a debug use. It's a single-def207// live range of a rematerializable value. The live range spills,208// rematerializes and now the ConI directly feeds into the debug info.209// assert(!def->is_Con(), "constant debug info already constructed directly");210
211// Special split handling for Debug Info212// If DEF is DOWN, just hook the edge and return213// If DEF is UP, Split it DOWN for this USE.214if( def->is_Mach() ) {215if( def_down ) {216// DEF is DOWN, so connect USE directly to the DEF217use->set_req(useidx, def);218return 0;219} else {220// Block and index where the use occurs.221Block *b = _cfg.get_block_for_node(use);222// Put the clone just prior to use223int bindex = b->find_node(use);224// DEF is UP, so must copy it DOWN and hook in USE225// Insert SpillCopy before the USE, which uses DEF as its input,226// and defs a new live range, which is used by this node.227Node *spill = get_spillcopy_wide(spill_type, def,use,useidx);228// did we fail to split?229if (!spill) {230// Bail231return -1;232}233// insert into basic block234insert_proj( b, bindex, spill, maxlrg );235// Use the new split236use->set_req(useidx,spill);237return 1;238}239// No further split handling needed for this use240} // End special splitting for debug info live range241} // If debug info242
243// CISC-SPILLING244// Finally, check to see if USE is CISC-Spillable, and if so,245// gather_lrg_masks will add the flags bit to its mask, and246// no use side copy is needed. This frees up the live range247// register choices without causing copy coalescing, etc.248if( UseCISCSpill && cisc_sp ) {249int inp = use->cisc_operand();250if( inp != AdlcVMDeps::Not_cisc_spillable )251// Convert operand number to edge index number252inp = use->as_Mach()->operand_index(inp);253if( inp == (int)useidx ) {254use->set_req(useidx, def);255#ifndef PRODUCT256if( TraceCISCSpill ) {257tty->print(" set_split: ");258use->dump();259}260#endif261return 0;262}263}264
265//-------------------------------------------266// Insert a Copy before the use267
268// Block and index where the use occurs.269int bindex;270// Phi input spill-copys belong at the end of the prior block271if( use->is_Phi() ) {272b = _cfg.get_block_for_node(b->pred(useidx));273bindex = b->end_idx();274} else {275// Put the clone just prior to use276bindex = b->find_node(use);277}278
279Node *spill = get_spillcopy_wide(spill_type, def, use, useidx );280if( !spill ) return -1; // Bailed out281// Insert SpillCopy before the USE, which uses the reaching DEF as282// its input, and defs a new live range, which is used by this node.283insert_proj( b, bindex, spill, maxlrg );284// Use the spill/clone285use->set_req(useidx,spill);286
287return 1;288}
289
290//------------------------------clone_node----------------------------
291// Clone node with anti dependence check.
292static Node* clone_node(Node* def, Block *b, Compile* C) {293if (def->needs_anti_dependence_check()) {294#ifdef ASSERT295if (PrintOpto && WizardMode) {296tty->print_cr("RA attempts to clone node with anti_dependence:");297def->dump(-1); tty->cr();298tty->print_cr("into block:");299b->dump();300}301#endif302if (C->subsume_loads() == true && !C->failing()) {303// Retry with subsume_loads == false304// If this is the first failure, the sentinel string will "stick"305// to the Compile object, and the C2Compiler will see it and retry.306C->record_failure(C2Compiler::retry_no_subsuming_loads());307} else {308// Bailout without retry309assert(false, "RA Split failed: attempt to clone node with anti_dependence");310C->record_method_not_compilable("RA Split failed: attempt to clone node with anti_dependence");311}312return nullptr;313}314return def->clone();315}
316
317//------------------------------split_Rematerialize----------------------------
318// Clone a local copy of the def.
319Node *PhaseChaitin::split_Rematerialize(Node *def, Block *b, uint insidx, uint &maxlrg,320GrowableArray<uint> splits, int slidx, uint *lrg2reach,321Node **Reachblock, bool walkThru) {322// The input live ranges will be stretched to the site of the new323// instruction. They might be stretched past a def and will thus324// have the old and new values of the same live range alive at the325// same time - a definite no-no. Split out private copies of326// the inputs.327if (def->req() > 1) {328for (uint i = 1; i < def->req(); i++) {329Node *in = def->in(i);330uint lidx = _lrg_map.live_range_id(in);331// We do not need this for live ranges that are only defined once.332// However, this is not true for spill copies that are added in this333// Split() pass, since they might get coalesced later on in this pass.334if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_singledef()) {335continue;336}337
338Block *b_def = _cfg.get_block_for_node(def);339int idx_def = b_def->find_node(def);340// Cannot spill Op_RegFlags.341Node *in_spill;342if (in->ideal_reg() != Op_RegFlags) {343in_spill = get_spillcopy_wide(MachSpillCopyNode::InputToRematerialization, in, def, i);344if (!in_spill) { return nullptr; } // Bailed out345insert_proj(b_def, idx_def, in_spill, maxlrg++);346if (b_def == b) {347insidx++;348}349def->set_req(i, in_spill);350} else {351// The 'in' defines a flag register. Flag registers can not be spilled.352// Register allocation handles live ranges with flag registers353// by rematerializing the def (in this case 'in'). Thus, this is not354// critical if the input can be rematerialized, too.355if (!in->rematerialize()) {356assert(false, "Can not rematerialize %d: %s. Prolongs RegFlags live"357" range and defining node %d: %s may not be rematerialized.",358def->_idx, def->Name(), in->_idx, in->Name());359C->record_method_not_compilable("attempted to spill a non-spillable item with RegFlags input");360return nullptr; // Bailed out361}362}363}364}365
366Node *spill = clone_node(def, b, C);367if (spill == nullptr || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {368// Check when generating nodes369return nullptr;370}371
372// See if any inputs are currently being spilled, and take the373// latest copy of spilled inputs.374if( spill->req() > 1 ) {375for( uint i = 1; i < spill->req(); i++ ) {376Node *in = spill->in(i);377uint lidx = _lrg_map.find_id(in);378
379// Walk backwards thru spill copy node intermediates380if (walkThru) {381while (in->is_SpillCopy() && lidx >= _lrg_map.max_lrg_id()) {382in = in->in(1);383lidx = _lrg_map.find_id(in);384}385
386if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).is_multidef()) {387// walkThru found a multidef LRG, which is unsafe to use, so388// just keep the original def used in the clone.389in = spill->in(i);390lidx = _lrg_map.find_id(in);391}392}393
394if (lidx < _lrg_map.max_lrg_id() && lrgs(lidx).reg() >= LRG::SPILL_REG) {395Node *rdef = Reachblock[lrg2reach[lidx]];396if (rdef) {397spill->set_req(i, rdef);398}399}400}401}402
403
404assert( spill->out_RegMask().is_UP(), "rematerialize to a reg" );405// Rematerialized op is def->spilled+1406set_was_spilled(spill);407if( _spilled_once.test(def->_idx) )408set_was_spilled(spill);409
410insert_proj( b, insidx, spill, maxlrg++ );411#ifdef ASSERT412// Increment the counter for this lrg413splits.at_put(slidx, splits.at(slidx)+1);414#endif415// See if the cloned def kills any flags, and copy those kills as well416uint i = insidx+1;417int found_projs = clone_projs( b, i, def, spill, maxlrg);418if (found_projs > 0) {419// Adjust the point where we go hi-pressure420if (i <= b->_ihrp_index) {421b->_ihrp_index += found_projs;422}423if (i <= b->_fhrp_index) {424b->_fhrp_index += found_projs;425}426}427
428return spill;429}
430
431//------------------------------is_high_pressure-------------------------------
432// Function to compute whether or not this live range is "high pressure"
433// in this block - whether it spills eagerly or not.
434bool PhaseChaitin::is_high_pressure( Block *b, LRG *lrg, uint insidx ) {435if( lrg->_was_spilled1 ) return true;436// Forced spilling due to conflict? Then split only at binding uses437// or defs, not for supposed capacity problems.438// CNC - Turned off 7/8/99, causes too much spilling439// if( lrg->_is_bound ) return false;440
441// Use float pressure numbers for vectors.442bool is_float_or_vector = lrg->_is_float || lrg->_is_vector;443// Not yet reached the high-pressure cutoff point, so low pressure444uint hrp_idx = is_float_or_vector ? b->_fhrp_index : b->_ihrp_index;445if( insidx < hrp_idx ) return false;446// Register pressure for the block as a whole depends on reg class447int block_pres = is_float_or_vector ? b->_freg_pressure : b->_reg_pressure;448// Bound live ranges will split at the binding points first;449// Intermediate splits should assume the live range's register set450// got "freed up" and that num_regs will become INT_PRESSURE.451int bound_pres = is_float_or_vector ? Matcher::float_pressure_limit() : Matcher::int_pressure_limit();452// Effective register pressure limit.453int lrg_pres = (lrg->get_invalid_mask_size() > lrg->num_regs())454? (lrg->get_invalid_mask_size() >> (lrg->num_regs()-1)) : bound_pres;455// High pressure if block pressure requires more register freedom456// than live range has.457return block_pres >= lrg_pres;458}
459
460
461//------------------------------prompt_use---------------------------------
462// True if lidx is used before any real register is def'd in the block
463bool PhaseChaitin::prompt_use( Block *b, uint lidx ) {464if (lrgs(lidx)._was_spilled2) {465return false;466}467
468// Scan block for 1st use.469for( uint i = 1; i <= b->end_idx(); i++ ) {470Node *n = b->get_node(i);471// Ignore PHI use, these can be up or down472if (n->is_Phi()) {473continue;474}475for (uint j = 1; j < n->req(); j++) {476if (_lrg_map.find_id(n->in(j)) == lidx) {477return true; // Found 1st use!478}479}480if (n->out_RegMask().is_NotEmpty()) {481return false;482}483}484return false;485}
486
487//------------------------------Split--------------------------------------
488//----------Split Routine----------
489// ***** NEW SPLITTING HEURISTIC *****
490// DEFS: If the DEF is in a High Register Pressure(HRP) Block, split there.
491// Else, no split unless there is a HRP block between a DEF and
492// one of its uses, and then split at the HRP block.
493//
494// USES: If USE is in HRP, split at use to leave main LRG on stack.
495// Else, hoist LRG back up to register only (ie - split is also DEF)
496// We will compute a new maxlrg as we go
497uint PhaseChaitin::Split(uint maxlrg, ResourceArea* split_arena) {498Compile::TracePhase tp("regAllocSplit", &timers[_t_regAllocSplit]);499
500// Free thread local resources used by this method on exit.501ResourceMark rm(split_arena);502
503uint bidx, pidx, slidx, insidx, inpidx, twoidx;504uint non_phi = 1, spill_cnt = 0;505Node *n1, *n2, *n3;506bool *UPblock;507bool u1, u2, u3;508Block *b, *pred;509PhiNode *phi;510GrowableArray<uint> lidxs(split_arena, maxlrg, 0, 0);511
512// Array of counters to count splits per live range513GrowableArray<uint> splits(split_arena, maxlrg, 0, 0);514
515#define NEW_SPLIT_ARRAY(type, size)\516(type*) split_arena->allocate_bytes((size) * sizeof(type))517
518//----------Setup Code----------519// Create a convenient mapping from lrg numbers to reaches/leaves indices520uint *lrg2reach = NEW_SPLIT_ARRAY(uint, maxlrg);521// Gather info on which LRG's are spilling, and build maps522for (bidx = 1; bidx < maxlrg; bidx++) {523if (lrgs(bidx).alive() && lrgs(bidx).reg() >= LRG::SPILL_REG) {524assert(!lrgs(bidx).mask().is_AllStack(),"AllStack should color");525lrg2reach[bidx] = spill_cnt;526spill_cnt++;527lidxs.append(bidx);528#ifdef ASSERT529// Initialize the split counts to zero530splits.append(0);531#endif532if (PrintOpto && WizardMode && lrgs(bidx)._was_spilled1) {533tty->print_cr("Warning, 2nd spill of L%d",bidx);534}535}536}537
538// Create side arrays for propagating reaching defs info.539// Each block needs a node pointer for each spilling live range for the540// Def which is live into the block. Phi nodes handle multiple input541// Defs by querying the output of their predecessor blocks and resolving542// them to a single Def at the phi. The pointer is updated for each543// Def in the block, and then becomes the output for the block when544// processing of the block is complete. We also need to track whether545// a Def is UP or DOWN. UP means that it should get a register (ie -546// it is always in LRP regions), and DOWN means that it is probably547// on the stack (ie - it crosses HRP regions).548Node ***Reaches = NEW_SPLIT_ARRAY( Node**, _cfg.number_of_blocks() + 1);549bool **UP = NEW_SPLIT_ARRAY( bool*, _cfg.number_of_blocks() + 1);550Node **debug_defs = NEW_SPLIT_ARRAY( Node*, spill_cnt );551VectorSet **UP_entry= NEW_SPLIT_ARRAY( VectorSet*, spill_cnt );552
553// Initialize Reaches & UP554for (bidx = 0; bidx < _cfg.number_of_blocks() + 1; bidx++) {555Reaches[bidx] = NEW_SPLIT_ARRAY( Node*, spill_cnt );556UP[bidx] = NEW_SPLIT_ARRAY( bool, spill_cnt );557Node **Reachblock = Reaches[bidx];558bool *UPblock = UP[bidx];559for( slidx = 0; slidx < spill_cnt; slidx++ ) {560UPblock[slidx] = true; // Assume they start in registers561Reachblock[slidx] = nullptr; // Assume that no def is present562}563}564
565#undef NEW_SPLIT_ARRAY566
567// Initialize to array of empty vectorsets568// Each containing at most spill_cnt * _cfg.number_of_blocks() entries.569for (slidx = 0; slidx < spill_cnt; slidx++) {570UP_entry[slidx] = new(split_arena) VectorSet(split_arena);571}572
573// Keep track of DEFS & Phis for later passes574Node_List defs(split_arena, 8);575Node_List phis(split_arena, 16);576
577//----------PASS 1----------578//----------Propagation & Node Insertion Code----------579// Walk the Blocks in RPO for DEF & USE info580for( bidx = 0; bidx < _cfg.number_of_blocks(); bidx++ ) {581
582if (C->check_node_count(spill_cnt, out_of_nodes)) {583return 0;584}585
586b = _cfg.get_block(bidx);587// Reaches & UP arrays for this block588Node** Reachblock = Reaches[b->_pre_order];589UPblock = UP[b->_pre_order];590// Reset counter of start of non-Phi nodes in block591non_phi = 1;592//----------Block Entry Handling----------593// Check for need to insert a new phi594// Cycle through this block's predecessors, collecting Reaches595// info for each spilled LRG. If they are identical, no phi is596// needed. If they differ, check for a phi, and insert if missing,597// or update edges if present. Set current block's Reaches set to598// be either the phi's or the reaching def, as appropriate.599// If no Phi is needed, check if the LRG needs to spill on entry600// to the block due to HRP.601for( slidx = 0; slidx < spill_cnt; slidx++ ) {602// Grab the live range number603uint lidx = lidxs.at(slidx);604// Do not bother splitting or putting in Phis for single-def605// rematerialized live ranges. This happens a lot to constants606// with long live ranges.607if( lrgs(lidx).is_singledef() &&608lrgs(lidx)._def->rematerialize() ) {609// reset the Reaches & UP entries610Reachblock[slidx] = lrgs(lidx)._def;611UPblock[slidx] = true;612// Record following instruction in case 'n' rematerializes and613// kills flags614Block *pred1 = _cfg.get_block_for_node(b->pred(1));615continue;616}617
618// Initialize needs_phi and needs_split619bool needs_phi = false;620bool needs_split = false;621bool has_phi = false;622// Walk the predecessor blocks to check inputs for that live range623// Grab predecessor block header624n1 = b->pred(1);625// Grab the appropriate reaching def info for inpidx626pred = _cfg.get_block_for_node(n1);627pidx = pred->_pre_order;628Node **Ltmp = Reaches[pidx];629bool *Utmp = UP[pidx];630n1 = Ltmp[slidx];631u1 = Utmp[slidx];632// Initialize node for saving type info633n3 = n1;634u3 = u1;635
636// Compare inputs to see if a Phi is needed637for( inpidx = 2; inpidx < b->num_preds(); inpidx++ ) {638// Grab predecessor block headers639n2 = b->pred(inpidx);640// Grab the appropriate reaching def info for inpidx641pred = _cfg.get_block_for_node(n2);642pidx = pred->_pre_order;643Ltmp = Reaches[pidx];644Utmp = UP[pidx];645n2 = Ltmp[slidx];646u2 = Utmp[slidx];647// For each LRG, decide if a phi is necessary648if( n1 != n2 ) {649needs_phi = true;650}651// See if the phi has mismatched inputs, UP vs. DOWN652if( n1 && n2 && (u1 != u2) ) {653needs_split = true;654}655// Move n2/u2 to n1/u1 for next iteration656n1 = n2;657u1 = u2;658// Preserve a non-null predecessor for later type referencing659if( (n3 == nullptr) && (n2 != nullptr) ){660n3 = n2;661u3 = u2;662}663} // End for all potential Phi inputs664
665// check block for appropriate phinode & update edges666for( insidx = 1; insidx <= b->end_idx(); insidx++ ) {667n1 = b->get_node(insidx);668// bail if this is not a phi669phi = n1->is_Phi() ? n1->as_Phi() : nullptr;670if( phi == nullptr ) {671// Keep track of index of first non-PhiNode instruction in block672non_phi = insidx;673// break out of the for loop as we have handled all phi nodes674break;675}676// must be looking at a phi677if (_lrg_map.find_id(n1) == lidxs.at(slidx)) {678// found the necessary phi679needs_phi = false;680has_phi = true;681// initialize the Reaches entry for this LRG682Reachblock[slidx] = phi;683break;684} // end if found correct phi685} // end for all phi's686
687// If a phi is needed or exist, check for it688if( needs_phi || has_phi ) {689// add new phinode if one not already found690if( needs_phi ) {691// create a new phi node and insert it into the block692// type is taken from left over pointer to a predecessor693guarantee(n3, "No non-null reaching DEF for a Phi");694phi = new PhiNode(b->head(), n3->bottom_type());695// initialize the Reaches entry for this LRG696Reachblock[slidx] = phi;697
698// add node to block & node_to_block mapping699insert_proj(b, insidx++, phi, maxlrg++);700non_phi++;701// Reset new phi's mapping to be the spilling live range702_lrg_map.map(phi->_idx, lidx);703assert(_lrg_map.find_id(phi) == lidx, "Bad update on Union-Find mapping");704} // end if not found correct phi705// Here you have either found or created the Phi, so record it706assert(phi != nullptr,"Must have a Phi Node here");707phis.push(phi);708// PhiNodes should either force the LRG UP or DOWN depending709// on its inputs and the register pressure in the Phi's block.710UPblock[slidx] = true; // Assume new DEF is UP711// If entering a high-pressure area with no immediate use,712// assume Phi is DOWN713if( is_high_pressure( b, &lrgs(lidx), b->end_idx()) && !prompt_use(b,lidx) )714UPblock[slidx] = false;715// If we are not split up/down and all inputs are down, then we716// are down717if( !needs_split && !u3 )718UPblock[slidx] = false;719} // end if phi is needed720
721// Do not need a phi, so grab the reaching DEF722else {723// Grab predecessor block header724n1 = b->pred(1);725// Grab the appropriate reaching def info for k726pred = _cfg.get_block_for_node(n1);727pidx = pred->_pre_order;728Node **Ltmp = Reaches[pidx];729bool *Utmp = UP[pidx];730// reset the Reaches & UP entries731Reachblock[slidx] = Ltmp[slidx];732UPblock[slidx] = Utmp[slidx];733} // end else no Phi is needed734} // end for all spilling live ranges735// DEBUG736#ifndef PRODUCT737if(trace_spilling()) {738tty->print("/`\nBlock %d: ", b->_pre_order);739tty->print("Reaching Definitions after Phi handling\n");740for( uint x = 0; x < spill_cnt; x++ ) {741tty->print("Spill Idx %d: UP %d: Node\n",x,UPblock[x]);742if( Reachblock[x] )743Reachblock[x]->dump();744else745tty->print("Undefined\n");746}747}748#endif749
750//----------Non-Phi Node Splitting----------751// Since phi-nodes have now been handled, the Reachblock array for this752// block is initialized with the correct starting value for the defs which753// reach non-phi instructions in this block. Thus, process non-phi754// instructions normally, inserting SpillCopy nodes for all spill755// locations.756
757// Memoize any DOWN reaching definitions for use as DEBUG info758for( insidx = 0; insidx < spill_cnt; insidx++ ) {759debug_defs[insidx] = (UPblock[insidx]) ? nullptr : Reachblock[insidx];760if( UPblock[insidx] ) // Memoize UP decision at block start761UP_entry[insidx]->set( b->_pre_order );762}763
764//----------Walk Instructions in the Block and Split----------765// For all non-phi instructions in the block766for( insidx = 1; insidx <= b->end_idx(); insidx++ ) {767Node *n = b->get_node(insidx);768// Find the defining Node's live range index769uint defidx = _lrg_map.find_id(n);770uint cnt = n->req();771
772if (n->is_Phi()) {773// Skip phi nodes after removing dead copies.774if (defidx < _lrg_map.max_lrg_id()) {775// Check for useless Phis. These appear if we spill, then776// coalesce away copies. Dont touch Phis in spilling live777// ranges; they are busy getting modified in this pass.778if( lrgs(defidx).reg() < LRG::SPILL_REG ) {779uint i;780Node *u = nullptr;781// Look for the Phi merging 2 unique inputs782for( i = 1; i < cnt; i++ ) {783// Ignore repeats and self784if( n->in(i) != u && n->in(i) != n ) {785// Found a unique input786if( u != nullptr ) // If it's the 2nd, bail out787break;788u = n->in(i); // Else record it789}790}791assert( u, "at least 1 valid input expected" );792if (i >= cnt) { // Found one unique input793assert(_lrg_map.find_id(n) == _lrg_map.find_id(u), "should be the same lrg");794n->replace_by(u); // Then replace with unique input795n->disconnect_inputs(C);796b->remove_node(insidx);797insidx--;798b->_ihrp_index--;799b->_fhrp_index--;800}801}802}803continue;804}805assert( insidx > b->_ihrp_index ||806(b->_reg_pressure < Matcher::int_pressure_limit()) ||807b->_ihrp_index > 4000000 ||808b->_ihrp_index >= b->end_idx() ||809!b->get_node(b->_ihrp_index)->is_Proj(), "" );810assert( insidx > b->_fhrp_index ||811(b->_freg_pressure < Matcher::float_pressure_limit()) ||812b->_fhrp_index > 4000000 ||813b->_fhrp_index >= b->end_idx() ||814!b->get_node(b->_fhrp_index)->is_Proj(), "" );815
816// ********** Handle Crossing HRP Boundary **********817if( (insidx == b->_ihrp_index) || (insidx == b->_fhrp_index) ) {818for( slidx = 0; slidx < spill_cnt; slidx++ ) {819// Check for need to split at HRP boundary - split if UP820n1 = Reachblock[slidx];821// bail out if no reaching DEF822if( n1 == nullptr ) continue;823// bail out if live range is 'isolated' around inner loop824uint lidx = lidxs.at(slidx);825// If live range is currently UP826if( UPblock[slidx] ) {827// set location to insert spills at828// SPLIT DOWN HERE - NO CISC SPILL829if( is_high_pressure( b, &lrgs(lidx), insidx ) &&830!n1->rematerialize() ) {831// If there is already a valid stack definition available, use it832if( debug_defs[slidx] != nullptr ) {833Reachblock[slidx] = debug_defs[slidx];834}835else {836// Insert point is just past last use or def in the block837int insert_point = insidx-1;838while( insert_point > 0 ) {839Node *n = b->get_node(insert_point);840// Hit top of block? Quit going backwards841if (n->is_Phi()) {842break;843}844// Found a def? Better split after it.845if (_lrg_map.live_range_id(n) == lidx) {846break;847}848// Look for a use849uint i;850for( i = 1; i < n->req(); i++ ) {851if (_lrg_map.live_range_id(n->in(i)) == lidx) {852break;853}854}855// Found a use? Better split after it.856if (i < n->req()) {857break;858}859insert_point--;860}861uint orig_eidx = b->end_idx();862maxlrg = split_DEF( n1, b, insert_point, maxlrg, Reachblock, debug_defs, splits, slidx);863// If it wasn't split bail864if (!maxlrg) {865return 0;866}867// Spill of null check mem op goes into the following block.868if (b->end_idx() > orig_eidx) {869insidx++;870}871}872// This is a new DEF, so update UP873UPblock[slidx] = false;874#ifndef PRODUCT875// DEBUG876if( trace_spilling() ) {877tty->print("\nNew Split DOWN DEF of Spill Idx ");878tty->print("%d, UP %d:\n",slidx,false);879n1->dump();880}881#endif882}883} // end if LRG is UP884} // end for all spilling live ranges885assert( b->get_node(insidx) == n, "got insidx set incorrectly" );886} // end if crossing HRP Boundary887
888// If the LRG index is oob, then this is a new spillcopy, skip it.889if (defidx >= _lrg_map.max_lrg_id()) {890continue;891}892LRG &deflrg = lrgs(defidx);893uint copyidx = n->is_Copy();894// Remove coalesced copy from CFG895if (copyidx && defidx == _lrg_map.live_range_id(n->in(copyidx))) {896n->replace_by( n->in(copyidx) );897n->set_req( copyidx, nullptr );898b->remove_node(insidx--);899b->_ihrp_index--; // Adjust the point where we go hi-pressure900b->_fhrp_index--;901continue;902}903
904#define DERIVED 0905
906// ********** Handle USES **********907bool nullcheck = false;908// Implicit null checks never use the spilled value909if( n->is_MachNullCheck() )910nullcheck = true;911if( !nullcheck ) {912// Search all inputs for a Spill-USE913JVMState* jvms = n->jvms();914uint oopoff = jvms ? jvms->oopoff() : cnt;915uint old_last = cnt - 1;916for( inpidx = 1; inpidx < cnt; inpidx++ ) {917// Derived/base pairs may be added to our inputs during this loop.918// If inpidx > old_last, then one of these new inputs is being919// handled. Skip the derived part of the pair, but process920// the base like any other input.921if (inpidx > old_last && ((inpidx - oopoff) & 1) == DERIVED) {922continue; // skip derived_debug added below923}924// Get lidx of input925uint useidx = _lrg_map.find_id(n->in(inpidx));926// Not a brand-new split, and it is a spill use927if (useidx < _lrg_map.max_lrg_id() && lrgs(useidx).reg() >= LRG::SPILL_REG) {928// Check for valid reaching DEF929slidx = lrg2reach[useidx];930Node *def = Reachblock[slidx];931assert( def != nullptr, "Using Undefined Value in Split()\n");932
933// (+++) %%%% remove this in favor of pre-pass in matcher.cpp934// monitor references do not care where they live, so just hook935if ( jvms && jvms->is_monitor_use(inpidx) ) {936// The effect of this clone is to drop the node out of the block,937// so that the allocator does not see it anymore, and therefore938// does not attempt to assign it a register.939def = clone_node(def, b, C);940if (def == nullptr || C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) {941return 0;942}943_lrg_map.extend(def->_idx, 0);944_cfg.map_node_to_block(def, b);945n->set_req(inpidx, def);946continue;947}948
949// Rematerializable? Then clone def at use site instead950// of store/load951if( def->rematerialize() ) {952int old_size = b->number_of_nodes();953def = split_Rematerialize( def, b, insidx, maxlrg, splits, slidx, lrg2reach, Reachblock, true );954if( !def ) return 0; // Bail out955insidx += b->number_of_nodes()-old_size;956}957
958MachNode *mach = n->is_Mach() ? n->as_Mach() : nullptr;959// Base pointers and oopmap references do not care where they live.960if ((inpidx >= oopoff) ||961(mach && mach->ideal_Opcode() == Op_AddP && inpidx == AddPNode::Base)) {962if (def->rematerialize() && lrgs(useidx)._was_spilled2) {963// This def has been rematerialized a couple of times without964// progress. It doesn't care if it lives UP or DOWN, so965// spill it down now.966int delta = split_USE(MachSpillCopyNode::BasePointerToMem, def,b,n,inpidx,maxlrg,false,false,splits,slidx);967// If it wasn't split bail968if (delta < 0) {969return 0;970}971maxlrg += delta;972insidx += delta; // Reset iterator to skip USE side split973} else {974// Just hook the def edge975n->set_req(inpidx, def);976}977
978if (inpidx >= oopoff) {979// After oopoff, we have derived/base pairs. We must mention all980// derived pointers here as derived/base pairs for GC. If the981// derived value is spilling and we have a copy both in Reachblock982// (called here 'def') and debug_defs[slidx] we need to mention983// both in derived/base pairs or kill one.984Node *derived_debug = debug_defs[slidx];985if( ((inpidx - oopoff) & 1) == DERIVED && // derived vs base?986mach && mach->ideal_Opcode() != Op_Halt &&987derived_debug != nullptr &&988derived_debug != def ) { // Actual 2nd value appears989// We have already set 'def' as a derived value.990// Also set debug_defs[slidx] as a derived value.991uint k;992for( k = oopoff; k < cnt; k += 2 )993if( n->in(k) == derived_debug )994break; // Found an instance of debug derived995if( k == cnt ) {// No instance of debug_defs[slidx]996// Add a derived/base pair to cover the debug info.997// We have to process the added base later since it is not998// handled yet at this point but skip derived part.999assert(((n->req() - oopoff) & 1) == DERIVED,1000"must match skip condition above");1001n->add_req( derived_debug ); // this will be skipped above1002n->add_req( n->in(inpidx+1) ); // this will be processed1003// Increment cnt to handle added input edges on1004// subsequent iterations.1005cnt += 2;1006}1007}1008}1009continue;1010}1011// Special logic for DEBUG info1012if( jvms && b->_freq > BLOCK_FREQUENCY(0.5) ) {1013uint debug_start = jvms->debug_start();1014// If this is debug info use & there is a reaching DOWN def1015if ((debug_start <= inpidx) && (debug_defs[slidx] != nullptr)) {1016assert(inpidx < oopoff, "handle only debug info here");1017// Just hook it in & move on1018n->set_req(inpidx, debug_defs[slidx]);1019// (Note that this can make two sides of a split live at the1020// same time: The debug def on stack, and another def in a1021// register. The GC needs to know about both of them, but any1022// derived pointers after oopoff will refer to only one of the1023// two defs and the GC would therefore miss the other. Thus1024// this hack is only allowed for debug info which is Java state1025// and therefore never a derived pointer.)1026continue;1027}1028}1029// Grab register mask info1030const RegMask &dmask = def->out_RegMask();1031const RegMask &umask = n->in_RegMask(inpidx);1032bool is_vect = RegMask::is_vector(def->ideal_reg());1033assert(inpidx < oopoff, "cannot use-split oop map info");1034
1035bool dup = UPblock[slidx];1036bool uup = umask.is_UP();1037
1038// Need special logic to handle bound USES. Insert a split at this1039// bound use if we can't rematerialize the def, or if we need the1040// split to form a misaligned pair.1041if( !umask.is_AllStack() &&1042(int)umask.Size() <= lrgs(useidx).num_regs() &&1043(!def->rematerialize() ||1044(!is_vect && umask.is_misaligned_pair()))) {1045// These need a Split regardless of overlap or pressure1046// SPLIT - NO DEF - NO CISC SPILL1047int delta = split_USE(MachSpillCopyNode::Bound, def,b,n,inpidx,maxlrg,dup,false, splits,slidx);1048// If it wasn't split bail1049if (delta < 0) {1050return 0;1051}1052maxlrg += delta;1053insidx += delta; // Reset iterator to skip USE side split1054continue;1055}1056
1057if (UseFPUForSpilling && n->is_MachCall() && !uup && !dup ) {1058// The use at the call can force the def down so insert1059// a split before the use to allow the def more freedom.1060int delta = split_USE(MachSpillCopyNode::CallUse, def,b,n,inpidx,maxlrg,dup,false, splits,slidx);1061// If it wasn't split bail1062if (delta < 0) {1063return 0;1064}1065maxlrg += delta;1066insidx += delta; // Reset iterator to skip USE side split1067continue;1068}1069
1070// Here is the logic chart which describes USE Splitting:1071// 0 = false or DOWN, 1 = true or UP1072//1073// Overlap | DEF | USE | Action1074//-------------------------------------------------------1075// 0 | 0 | 0 | Copy - mem -> mem1076// 0 | 0 | 1 | Split-UP - Check HRP1077// 0 | 1 | 0 | Split-DOWN - Debug Info?1078// 0 | 1 | 1 | Copy - reg -> reg1079// 1 | 0 | 0 | Reset Input Edge (no Split)1080// 1 | 0 | 1 | Split-UP - Check HRP1081// 1 | 1 | 0 | Split-DOWN - Debug Info?1082// 1 | 1 | 1 | Reset Input Edge (no Split)1083//1084// So, if (dup == uup), then overlap test determines action,1085// with true being no split, and false being copy. Else,1086// if DEF is DOWN, Split-UP, and check HRP to decide on1087// resetting DEF. Finally if DEF is UP, Split-DOWN, with1088// special handling for Debug Info.1089if( dup == uup ) {1090if( dmask.overlap(umask) ) {1091// Both are either up or down, and there is overlap, No Split1092n->set_req(inpidx, def);1093}1094else { // Both are either up or down, and there is no overlap1095if( dup ) { // If UP, reg->reg copy1096// COPY ACROSS HERE - NO DEF - NO CISC SPILL1097int delta = split_USE(MachSpillCopyNode::RegToReg, def,b,n,inpidx,maxlrg,false,false, splits,slidx);1098// If it wasn't split bail1099if (delta < 0) {1100return 0;1101}1102maxlrg += delta;1103insidx += delta; // Reset iterator to skip USE side split1104}1105else { // DOWN, mem->mem copy1106// COPY UP & DOWN HERE - NO DEF - NO CISC SPILL1107// First Split-UP to move value into Register1108uint def_ideal = def->ideal_reg();1109const RegMask* tmp_rm = Matcher::idealreg2regmask[def_ideal];1110Node *spill = new MachSpillCopyNode(MachSpillCopyNode::MemToReg, def, dmask, *tmp_rm);1111insert_proj( b, insidx, spill, maxlrg );1112maxlrg++; insidx++;1113// Then Split-DOWN as if previous Split was DEF1114int delta = split_USE(MachSpillCopyNode::RegToMem, spill,b,n,inpidx,maxlrg,false,false, splits,slidx);1115// If it wasn't split bail1116if (delta < 0) {1117return 0;1118}1119maxlrg += delta;1120insidx += delta; // Reset iterator to skip USE side splits1121}1122} // End else no overlap1123} // End if dup == uup1124// dup != uup, so check dup for direction of Split1125else {1126if( dup ) { // If UP, Split-DOWN and check Debug Info1127// If this node is already a SpillCopy, just patch the edge1128// except the case of spilling to stack.1129if( n->is_SpillCopy() ) {1130RegMask tmp_rm(umask);1131tmp_rm.SUBTRACT(Matcher::STACK_ONLY_mask);1132if( dmask.overlap(tmp_rm) ) {1133if( def != n->in(inpidx) ) {1134n->set_req(inpidx, def);1135}1136continue;1137}1138}1139// COPY DOWN HERE - NO DEF - NO CISC SPILL1140int delta = split_USE(MachSpillCopyNode::RegToMem, def,b,n,inpidx,maxlrg,false,false, splits,slidx);1141// If it wasn't split bail1142if (delta < 0) {1143return 0;1144}1145maxlrg += delta;1146insidx += delta; // Reset iterator to skip USE side split1147// Check for debug-info split. Capture it for later1148// debug splits of the same value1149if (jvms && jvms->debug_start() <= inpidx && inpidx < oopoff)1150debug_defs[slidx] = n->in(inpidx);1151
1152}1153else { // DOWN, Split-UP and check register pressure1154if( is_high_pressure( b, &lrgs(useidx), insidx ) ) {1155// COPY UP HERE - NO DEF - CISC SPILL1156int delta = split_USE(MachSpillCopyNode::MemToReg, def,b,n,inpidx,maxlrg,true,true, splits,slidx);1157// If it wasn't split bail1158if (delta < 0) {1159return 0;1160}1161maxlrg += delta;1162insidx += delta; // Reset iterator to skip USE side split1163} else { // LRP1164// COPY UP HERE - WITH DEF - NO CISC SPILL1165int delta = split_USE(MachSpillCopyNode::MemToReg, def,b,n,inpidx,maxlrg,true,false, splits,slidx);1166// If it wasn't split bail1167if (delta < 0) {1168return 0;1169}1170// Flag this lift-up in a low-pressure block as1171// already-spilled, so if it spills again it will1172// spill hard (instead of not spilling hard and1173// coalescing away).1174set_was_spilled(n->in(inpidx));1175// Since this is a new DEF, update Reachblock & UP1176Reachblock[slidx] = n->in(inpidx);1177UPblock[slidx] = true;1178maxlrg += delta;1179insidx += delta; // Reset iterator to skip USE side split1180}1181} // End else DOWN1182} // End dup != uup1183} // End if Spill USE1184} // End For All Inputs1185} // End If not nullcheck1186
1187// ********** Handle DEFS **********1188// DEFS either Split DOWN in HRP regions or when the LRG is bound, or1189// just reset the Reaches info in LRP regions. DEFS must always update1190// UP info.1191if( deflrg.reg() >= LRG::SPILL_REG ) { // Spilled?1192uint slidx = lrg2reach[defidx];1193// Add to defs list for later assignment of new live range number1194defs.push(n);1195// Set a flag on the Node indicating it has already spilled.1196// Only do it for capacity spills not conflict spills.1197if( !deflrg._direct_conflict )1198set_was_spilled(n);1199assert(!n->is_Phi(),"Cannot insert Phi into DEFS list");1200// Grab UP info for DEF1201const RegMask &dmask = n->out_RegMask();1202bool defup = dmask.is_UP();1203uint ireg = n->ideal_reg();1204bool is_vect = RegMask::is_vector(ireg);1205// Only split at Def if this is a HRP block or bound (and spilled once)1206if( !n->rematerialize() &&1207(((dmask.is_bound(ireg) || (!is_vect && dmask.is_misaligned_pair())) &&1208(deflrg._direct_conflict || deflrg._must_spill)) ||1209// Check for LRG being up in a register and we are inside a high1210// pressure area. Spill it down immediately.1211(defup && is_high_pressure(b,&deflrg,insidx) && !n->is_SpillCopy())) ) {1212assert( !n->rematerialize(), "" );1213// Do a split at the def site.1214maxlrg = split_DEF( n, b, insidx, maxlrg, Reachblock, debug_defs, splits, slidx );1215// If it wasn't split bail1216if (!maxlrg) {1217return 0;1218}1219// Split DEF's Down1220UPblock[slidx] = 0;1221#ifndef PRODUCT1222// DEBUG1223if( trace_spilling() ) {1224tty->print("\nNew Split DOWN DEF of Spill Idx ");1225tty->print("%d, UP %d:\n",slidx,false);1226n->dump();1227}1228#endif1229}1230else { // Neither bound nor HRP, must be LRP1231// otherwise, just record the def1232Reachblock[slidx] = n;1233// UP should come from the outRegmask() of the DEF1234UPblock[slidx] = defup;1235// Update debug list of reaching down definitions, kill if DEF is UP1236debug_defs[slidx] = defup ? nullptr : n;1237#ifndef PRODUCT1238// DEBUG1239if( trace_spilling() ) {1240tty->print("\nNew DEF of Spill Idx ");1241tty->print("%d, UP %d:\n",slidx,defup);1242n->dump();1243}1244#endif1245} // End else LRP1246} // End if spill def1247
1248// ********** Split Left Over Mem-Mem Moves **********1249// Check for mem-mem copies and split them now. Do not do this1250// to copies about to be spilled; they will be Split shortly.1251if (copyidx) {1252Node *use = n->in(copyidx);1253uint useidx = _lrg_map.find_id(use);1254if (useidx < _lrg_map.max_lrg_id() && // This is not a new split1255OptoReg::is_stack(deflrg.reg()) &&1256deflrg.reg() < LRG::SPILL_REG ) { // And DEF is from stack1257LRG &uselrg = lrgs(useidx);1258if( OptoReg::is_stack(uselrg.reg()) &&1259uselrg.reg() < LRG::SPILL_REG && // USE is from stack1260deflrg.reg() != uselrg.reg() ) { // Not trivially removed1261uint def_ideal_reg = n->bottom_type()->ideal_reg();1262const RegMask &def_rm = *Matcher::idealreg2regmask[def_ideal_reg];1263const RegMask &use_rm = n->in_RegMask(copyidx);1264if( def_rm.overlap(use_rm) && n->is_SpillCopy() ) { // Bug 4707800, 'n' may be a storeSSL1265if (C->check_node_count(NodeLimitFudgeFactor, out_of_nodes)) { // Check when generating nodes1266return 0;1267}1268Node *spill = new MachSpillCopyNode(MachSpillCopyNode::MemToReg, use,use_rm,def_rm);1269n->set_req(copyidx,spill);1270n->as_MachSpillCopy()->set_in_RegMask(def_rm);1271// Put the spill just before the copy1272insert_proj( b, insidx++, spill, maxlrg++ );1273}1274}1275}1276}1277} // End For All Instructions in Block - Non-PHI Pass1278
1279// Check if each LRG is live out of this block so as not to propagate1280// beyond the last use of a LRG.1281for( slidx = 0; slidx < spill_cnt; slidx++ ) {1282uint defidx = lidxs.at(slidx);1283IndexSet *liveout = _live->live(b);1284if( !liveout->member(defidx) ) {1285#ifdef ASSERT1286if (VerifyRegisterAllocator) {1287// The index defidx is not live. Check the liveout array to ensure that1288// it contains no members which compress to defidx. Finding such an1289// instance may be a case to add liveout adjustment in compress_uf_map().1290// See 5063219.1291if (!liveout->is_empty()) {1292uint member;1293IndexSetIterator isi(liveout);1294while ((member = isi.next()) != 0) {1295assert(defidx != _lrg_map.find_const(member), "Live out member has not been compressed");1296}1297}1298}1299#endif1300Reachblock[slidx] = nullptr;1301} else {1302assert(Reachblock[slidx] != nullptr,"No reaching definition for liveout value");1303}1304}1305#ifndef PRODUCT1306if( trace_spilling() )1307b->dump();1308#endif1309} // End For All Blocks1310
1311//----------PASS 2----------1312// Reset all DEF live range numbers here1313for( insidx = 0; insidx < defs.size(); insidx++ ) {1314// Grab the def1315n1 = defs.at(insidx);1316// Set new lidx for DEF1317new_lrg(n1, maxlrg++);1318}1319//----------Phi Node Splitting----------1320// Clean up a phi here, and assign a new live range number1321// Cycle through this block's predecessors, collecting Reaches1322// info for each spilled LRG and update edges.1323// Walk the phis list to patch inputs, split phis, and name phis1324uint lrgs_before_phi_split = maxlrg;1325for( insidx = 0; insidx < phis.size(); insidx++ ) {1326Node *phi = phis.at(insidx);1327assert(phi->is_Phi(),"This list must only contain Phi Nodes");1328Block *b = _cfg.get_block_for_node(phi);1329// Grab the live range number1330uint lidx = _lrg_map.find_id(phi);1331uint slidx = lrg2reach[lidx];1332// Update node to lidx map1333new_lrg(phi, maxlrg++);1334// Get PASS1's up/down decision for the block.1335int phi_up = !!UP_entry[slidx]->test(b->_pre_order);1336
1337// Force down if double-spilling live range1338if( lrgs(lidx)._was_spilled1 )1339phi_up = false;1340
1341// When splitting a Phi we an split it normal or "inverted".1342// An inverted split makes the splits target the Phi's UP/DOWN1343// sense inverted; then the Phi is followed by a final def-side1344// split to invert back. It changes which blocks the spill code1345// goes in.1346
1347// Walk the predecessor blocks and assign the reaching def to the Phi.1348// Split Phi nodes by placing USE side splits wherever the reaching1349// DEF has the wrong UP/DOWN value.1350for( uint i = 1; i < b->num_preds(); i++ ) {1351// Get predecessor block pre-order number1352Block *pred = _cfg.get_block_for_node(b->pred(i));1353pidx = pred->_pre_order;1354// Grab reaching def1355Node *def = Reaches[pidx][slidx];1356Node** Reachblock = Reaches[pidx];1357assert( def, "must have reaching def" );1358// If input up/down sense and reg-pressure DISagree1359if (def->rematerialize()) {1360// Place the rematerialized node above any MSCs created during1361// phi node splitting. end_idx points at the insertion point1362// so look at the node before it.1363int insert = pred->end_idx();1364while (insert >= 1 &&1365pred->get_node(insert - 1)->is_SpillCopy() &&1366_lrg_map.find(pred->get_node(insert - 1)) >= lrgs_before_phi_split) {1367insert--;1368}1369def = split_Rematerialize(def, pred, insert, maxlrg, splits, slidx, lrg2reach, Reachblock, false);1370if (!def) {1371return 0; // Bail out1372}1373}1374// Update the Phi's input edge array1375phi->set_req(i,def);1376// Grab the UP/DOWN sense for the input1377u1 = UP[pidx][slidx];1378if( u1 != (phi_up != 0)) {1379int delta = split_USE(MachSpillCopyNode::PhiLocationDifferToInputLocation, def, b, phi, i, maxlrg, !u1, false, splits,slidx);1380// If it wasn't split bail1381if (delta < 0) {1382return 0;1383}1384maxlrg += delta;1385}1386} // End for all inputs to the Phi1387} // End for all Phi Nodes1388// Update _maxlrg to save Union asserts1389_lrg_map.set_max_lrg_id(maxlrg);1390
1391
1392//----------PASS 3----------1393// Pass over all Phi's to union the live ranges1394for( insidx = 0; insidx < phis.size(); insidx++ ) {1395Node *phi = phis.at(insidx);1396assert(phi->is_Phi(),"This list must only contain Phi Nodes");1397// Walk all inputs to Phi and Union input live range with Phi live range1398for( uint i = 1; i < phi->req(); i++ ) {1399// Grab the input node1400Node *n = phi->in(i);1401assert(n, "node should exist");1402uint lidx = _lrg_map.find(n);1403uint pidx = _lrg_map.find(phi);1404if (lidx < pidx) {1405Union(n, phi);1406}1407else if(lidx > pidx) {1408Union(phi, n);1409}1410} // End for all inputs to the Phi Node1411} // End for all Phi Nodes1412// Now union all two address instructions1413for (insidx = 0; insidx < defs.size(); insidx++) {1414// Grab the def1415n1 = defs.at(insidx);1416// Set new lidx for DEF & handle 2-addr instructions1417if (n1->is_Mach() && ((twoidx = n1->as_Mach()->two_adr()) != 0)) {1418assert(_lrg_map.find(n1->in(twoidx)) < maxlrg,"Assigning bad live range index");1419// Union the input and output live ranges1420uint lr1 = _lrg_map.find(n1);1421uint lr2 = _lrg_map.find(n1->in(twoidx));1422if (lr1 < lr2) {1423Union(n1, n1->in(twoidx));1424}1425else if (lr1 > lr2) {1426Union(n1->in(twoidx), n1);1427}1428} // End if two address1429} // End for all defs1430// DEBUG1431#ifdef ASSERT1432// Validate all live range index assignments1433for (bidx = 0; bidx < _cfg.number_of_blocks(); bidx++) {1434b = _cfg.get_block(bidx);1435for (insidx = 0; insidx <= b->end_idx(); insidx++) {1436Node *n = b->get_node(insidx);1437uint defidx = _lrg_map.find(n);1438assert(defidx < _lrg_map.max_lrg_id(), "Bad live range index in Split");1439assert(defidx < maxlrg,"Bad live range index in Split");1440}1441}1442// Issue a warning if splitting made no progress1443int noprogress = 0;1444for (slidx = 0; slidx < spill_cnt; slidx++) {1445if (PrintOpto && WizardMode && splits.at(slidx) == 0) {1446tty->print_cr("Failed to split live range %d", lidxs.at(slidx));1447//BREAKPOINT;1448}1449else {1450noprogress++;1451}1452}1453if(!noprogress) {1454tty->print_cr("Failed to make progress in Split");1455//BREAKPOINT;1456}1457#endif1458// Return updated count of live ranges1459return maxlrg;1460}
1461