Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/ifg.cpp
32285 views
/*1* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "compiler/oopMap.hpp"26#include "memory/allocation.inline.hpp"27#include "opto/addnode.hpp"28#include "opto/block.hpp"29#include "opto/callnode.hpp"30#include "opto/cfgnode.hpp"31#include "opto/chaitin.hpp"32#include "opto/coalesce.hpp"33#include "opto/connode.hpp"34#include "opto/indexSet.hpp"35#include "opto/machnode.hpp"36#include "opto/memnode.hpp"37#include "opto/opcodes.hpp"3839PhaseIFG::PhaseIFG( Arena *arena ) : Phase(Interference_Graph), _arena(arena) {40}4142void PhaseIFG::init( uint maxlrg ) {43_maxlrg = maxlrg;44_yanked = new (_arena) VectorSet(_arena);45_is_square = false;46// Make uninitialized adjacency lists47_adjs = (IndexSet*)_arena->Amalloc(sizeof(IndexSet)*maxlrg);48// Also make empty live range structures49_lrgs = (LRG *)_arena->Amalloc( maxlrg * sizeof(LRG) );50memset(_lrgs,0,sizeof(LRG)*maxlrg);51// Init all to empty52for( uint i = 0; i < maxlrg; i++ ) {53_adjs[i].initialize(maxlrg);54_lrgs[i].Set_All();55}56}5758// Add edge between vertices a & b. These are sorted (triangular matrix),59// then the smaller number is inserted in the larger numbered array.60int PhaseIFG::add_edge( uint a, uint b ) {61lrgs(a).invalid_degree();62lrgs(b).invalid_degree();63// Sort a and b, so that a is bigger64assert( !_is_square, "only on triangular" );65if( a < b ) { uint tmp = a; a = b; b = tmp; }66return _adjs[a].insert( b );67}6869// Add an edge between 'a' and everything in the vector.70void PhaseIFG::add_vector( uint a, IndexSet *vec ) {71// IFG is triangular, so do the inserts where 'a' < 'b'.72assert( !_is_square, "only on triangular" );73IndexSet *adjs_a = &_adjs[a];74if( !vec->count() ) return;7576IndexSetIterator elements(vec);77uint neighbor;78while ((neighbor = elements.next()) != 0) {79add_edge( a, neighbor );80}81}8283// Is there an edge between a and b?84int PhaseIFG::test_edge( uint a, uint b ) const {85// Sort a and b, so that a is larger86assert( !_is_square, "only on triangular" );87if( a < b ) { uint tmp = a; a = b; b = tmp; }88return _adjs[a].member(b);89}9091// Convert triangular matrix to square matrix92void PhaseIFG::SquareUp() {93assert( !_is_square, "only on triangular" );9495// Simple transpose96for( uint i = 0; i < _maxlrg; i++ ) {97IndexSetIterator elements(&_adjs[i]);98uint datum;99while ((datum = elements.next()) != 0) {100_adjs[datum].insert( i );101}102}103_is_square = true;104}105106// Compute effective degree in bulk107void PhaseIFG::Compute_Effective_Degree() {108assert( _is_square, "only on square" );109110for( uint i = 0; i < _maxlrg; i++ )111lrgs(i).set_degree(effective_degree(i));112}113114int PhaseIFG::test_edge_sq( uint a, uint b ) const {115assert( _is_square, "only on square" );116// Swap, so that 'a' has the lesser count. Then binary search is on117// the smaller of a's list and b's list.118if( neighbor_cnt(a) > neighbor_cnt(b) ) { uint tmp = a; a = b; b = tmp; }119//return _adjs[a].unordered_member(b);120return _adjs[a].member(b);121}122123// Union edges of B into A124void PhaseIFG::Union( uint a, uint b ) {125assert( _is_square, "only on square" );126IndexSet *A = &_adjs[a];127IndexSetIterator b_elements(&_adjs[b]);128uint datum;129while ((datum = b_elements.next()) != 0) {130if(A->insert(datum)) {131_adjs[datum].insert(a);132lrgs(a).invalid_degree();133lrgs(datum).invalid_degree();134}135}136}137138// Yank a Node and all connected edges from the IFG. Return a139// list of neighbors (edges) yanked.140IndexSet *PhaseIFG::remove_node( uint a ) {141assert( _is_square, "only on square" );142assert( !_yanked->test(a), "" );143_yanked->set(a);144145// I remove the LRG from all neighbors.146IndexSetIterator elements(&_adjs[a]);147LRG &lrg_a = lrgs(a);148uint datum;149while ((datum = elements.next()) != 0) {150_adjs[datum].remove(a);151lrgs(datum).inc_degree( -lrg_a.compute_degree(lrgs(datum)) );152}153return neighbors(a);154}155156// Re-insert a yanked Node.157void PhaseIFG::re_insert( uint a ) {158assert( _is_square, "only on square" );159assert( _yanked->test(a), "" );160(*_yanked) >>= a;161162IndexSetIterator elements(&_adjs[a]);163uint datum;164while ((datum = elements.next()) != 0) {165_adjs[datum].insert(a);166lrgs(datum).invalid_degree();167}168}169170// Compute the degree between 2 live ranges. If both live ranges are171// aligned-adjacent powers-of-2 then we use the MAX size. If either is172// mis-aligned (or for Fat-Projections, not-adjacent) then we have to173// MULTIPLY the sizes. Inspect Brigg's thesis on register pairs to see why174// this is so.175int LRG::compute_degree( LRG &l ) const {176int tmp;177int num_regs = _num_regs;178int nregs = l.num_regs();179tmp = (_fat_proj || l._fat_proj) // either is a fat-proj?180? (num_regs * nregs) // then use product181: MAX2(num_regs,nregs); // else use max182return tmp;183}184185// Compute effective degree for this live range. If both live ranges are186// aligned-adjacent powers-of-2 then we use the MAX size. If either is187// mis-aligned (or for Fat-Projections, not-adjacent) then we have to188// MULTIPLY the sizes. Inspect Brigg's thesis on register pairs to see why189// this is so.190int PhaseIFG::effective_degree( uint lidx ) const {191int eff = 0;192int num_regs = lrgs(lidx).num_regs();193int fat_proj = lrgs(lidx)._fat_proj;194IndexSet *s = neighbors(lidx);195IndexSetIterator elements(s);196uint nidx;197while((nidx = elements.next()) != 0) {198LRG &lrgn = lrgs(nidx);199int nregs = lrgn.num_regs();200eff += (fat_proj || lrgn._fat_proj) // either is a fat-proj?201? (num_regs * nregs) // then use product202: MAX2(num_regs,nregs); // else use max203}204return eff;205}206207208#ifndef PRODUCT209void PhaseIFG::dump() const {210tty->print_cr("-- Interference Graph --%s--",211_is_square ? "square" : "triangular" );212if( _is_square ) {213for( uint i = 0; i < _maxlrg; i++ ) {214tty->print( (*_yanked)[i] ? "XX " : " ");215tty->print("L%d: { ",i);216IndexSetIterator elements(&_adjs[i]);217uint datum;218while ((datum = elements.next()) != 0) {219tty->print("L%d ", datum);220}221tty->print_cr("}");222223}224return;225}226227// Triangular228for( uint i = 0; i < _maxlrg; i++ ) {229uint j;230tty->print( (*_yanked)[i] ? "XX " : " ");231tty->print("L%d: { ",i);232for( j = _maxlrg; j > i; j-- )233if( test_edge(j - 1,i) ) {234tty->print("L%d ",j - 1);235}236tty->print("| ");237IndexSetIterator elements(&_adjs[i]);238uint datum;239while ((datum = elements.next()) != 0) {240tty->print("L%d ", datum);241}242tty->print("}\n");243}244tty->print("\n");245}246247void PhaseIFG::stats() const {248ResourceMark rm;249int *h_cnt = NEW_RESOURCE_ARRAY(int,_maxlrg*2);250memset( h_cnt, 0, sizeof(int)*_maxlrg*2 );251uint i;252for( i = 0; i < _maxlrg; i++ ) {253h_cnt[neighbor_cnt(i)]++;254}255tty->print_cr("--Histogram of counts--");256for( i = 0; i < _maxlrg*2; i++ )257if( h_cnt[i] )258tty->print("%d/%d ",i,h_cnt[i]);259tty->cr();260}261262void PhaseIFG::verify( const PhaseChaitin *pc ) const {263// IFG is square, sorted and no need for Find264for( uint i = 0; i < _maxlrg; i++ ) {265assert(!((*_yanked)[i]) || !neighbor_cnt(i), "Is removed completely" );266IndexSet *set = &_adjs[i];267IndexSetIterator elements(set);268uint idx;269uint last = 0;270while ((idx = elements.next()) != 0) {271assert(idx != i, "Must have empty diagonal");272assert(pc->_lrg_map.find_const(idx) == idx, "Must not need Find");273assert(_adjs[idx].member(i), "IFG not square");274assert(!(*_yanked)[idx], "No yanked neighbors");275assert(last < idx, "not sorted increasing");276last = idx;277}278assert(!lrgs(i)._degree_valid || effective_degree(i) == lrgs(i).degree(), "degree is valid but wrong");279}280}281#endif282283// Interfere this register with everything currently live. Use the RegMasks284// to trim the set of possible interferences. Return a count of register-only285// interferences as an estimate of register pressure.286void PhaseChaitin::interfere_with_live( uint r, IndexSet *liveout ) {287uint retval = 0;288// Interfere with everything live.289const RegMask &rm = lrgs(r).mask();290// Check for interference by checking overlap of regmasks.291// Only interfere if acceptable register masks overlap.292IndexSetIterator elements(liveout);293uint l;294while( (l = elements.next()) != 0 )295if( rm.overlap( lrgs(l).mask() ) )296_ifg->add_edge( r, l );297}298299// Actually build the interference graph. Uses virtual registers only, no300// physical register masks. This allows me to be very aggressive when301// coalescing copies. Some of this aggressiveness will have to be undone302// later, but I'd rather get all the copies I can now (since unremoved copies303// at this point can end up in bad places). Copies I re-insert later I have304// more opportunity to insert them in low-frequency locations.305void PhaseChaitin::build_ifg_virtual( ) {306307// For all blocks (in any order) do...308for (uint i = 0; i < _cfg.number_of_blocks(); i++) {309Block* block = _cfg.get_block(i);310IndexSet* liveout = _live->live(block);311312// The IFG is built by a single reverse pass over each basic block.313// Starting with the known live-out set, we remove things that get314// defined and add things that become live (essentially executing one315// pass of a standard LIVE analysis). Just before a Node defines a value316// (and removes it from the live-ness set) that value is certainly live.317// The defined value interferes with everything currently live. The318// value is then removed from the live-ness set and it's inputs are319// added to the live-ness set.320for (uint j = block->end_idx() + 1; j > 1; j--) {321Node* n = block->get_node(j - 1);322323// Get value being defined324uint r = _lrg_map.live_range_id(n);325326// Some special values do not allocate327if (r) {328329// Remove from live-out set330liveout->remove(r);331332// Copies do not define a new value and so do not interfere.333// Remove the copies source from the liveout set before interfering.334uint idx = n->is_Copy();335if (idx) {336liveout->remove(_lrg_map.live_range_id(n->in(idx)));337}338339// Interfere with everything live340interfere_with_live(r, liveout);341}342343// Make all inputs live344if (!n->is_Phi()) { // Phi function uses come from prior block345for(uint k = 1; k < n->req(); k++) {346liveout->insert(_lrg_map.live_range_id(n->in(k)));347}348}349350// 2-address instructions always have the defined value live351// on entry to the instruction, even though it is being defined352// by the instruction. We pretend a virtual copy sits just prior353// to the instruction and kills the src-def'd register.354// In other words, for 2-address instructions the defined value355// interferes with all inputs.356uint idx;357if( n->is_Mach() && (idx = n->as_Mach()->two_adr()) ) {358const MachNode *mach = n->as_Mach();359// Sometimes my 2-address ADDs are commuted in a bad way.360// We generally want the USE-DEF register to refer to the361// loop-varying quantity, to avoid a copy.362uint op = mach->ideal_Opcode();363// Check that mach->num_opnds() == 3 to ensure instruction is364// not subsuming constants, effectively excludes addI_cin_imm365// Can NOT swap for instructions like addI_cin_imm since it366// is adding zero to yhi + carry and the second ideal-input367// points to the result of adding low-halves.368// Checking req() and num_opnds() does NOT distinguish addI_cout from addI_cout_imm369if( (op == Op_AddI && mach->req() == 3 && mach->num_opnds() == 3) &&370n->in(1)->bottom_type()->base() == Type::Int &&371// See if the ADD is involved in a tight data loop the wrong way372n->in(2)->is_Phi() &&373n->in(2)->in(2) == n ) {374Node *tmp = n->in(1);375n->set_req( 1, n->in(2) );376n->set_req( 2, tmp );377}378// Defined value interferes with all inputs379uint lidx = _lrg_map.live_range_id(n->in(idx));380for (uint k = 1; k < n->req(); k++) {381uint kidx = _lrg_map.live_range_id(n->in(k));382if (kidx != lidx) {383_ifg->add_edge(r, kidx);384}385}386}387} // End of forall instructions in block388} // End of forall blocks389}390391uint PhaseChaitin::count_int_pressure( IndexSet *liveout ) {392IndexSetIterator elements(liveout);393uint lidx;394uint cnt = 0;395while ((lidx = elements.next()) != 0) {396if( lrgs(lidx).mask().is_UP() &&397lrgs(lidx).mask_size() &&398!lrgs(lidx)._is_float &&399!lrgs(lidx)._is_vector &&400lrgs(lidx).mask().overlap(*Matcher::idealreg2regmask[Op_RegI]) )401cnt += lrgs(lidx).reg_pressure();402}403return cnt;404}405406uint PhaseChaitin::count_float_pressure( IndexSet *liveout ) {407IndexSetIterator elements(liveout);408uint lidx;409uint cnt = 0;410while ((lidx = elements.next()) != 0) {411if( lrgs(lidx).mask().is_UP() &&412lrgs(lidx).mask_size() &&413(lrgs(lidx)._is_float || lrgs(lidx)._is_vector))414cnt += lrgs(lidx).reg_pressure();415}416return cnt;417}418419// Adjust register pressure down by 1. Capture last hi-to-low transition,420static void lower_pressure( LRG *lrg, uint where, Block *b, uint *pressure, uint *hrp_index ) {421if (lrg->mask().is_UP() && lrg->mask_size()) {422if (lrg->_is_float || lrg->_is_vector) {423pressure[1] -= lrg->reg_pressure();424if( pressure[1] == (uint)FLOATPRESSURE ) {425hrp_index[1] = where;426if( pressure[1] > b->_freg_pressure )427b->_freg_pressure = pressure[1]+1;428}429} else if( lrg->mask().overlap(*Matcher::idealreg2regmask[Op_RegI]) ) {430pressure[0] -= lrg->reg_pressure();431if( pressure[0] == (uint)INTPRESSURE ) {432hrp_index[0] = where;433if( pressure[0] > b->_reg_pressure )434b->_reg_pressure = pressure[0]+1;435}436}437}438}439440// Build the interference graph using physical registers when available.441// That is, if 2 live ranges are simultaneously alive but in their acceptable442// register sets do not overlap, then they do not interfere.443uint PhaseChaitin::build_ifg_physical( ResourceArea *a ) {444NOT_PRODUCT( Compile::TracePhase t3("buildIFG", &_t_buildIFGphysical, TimeCompiler); )445446uint must_spill = 0;447448// For all blocks (in any order) do...449for (uint i = 0; i < _cfg.number_of_blocks(); i++) {450Block* block = _cfg.get_block(i);451// Clone (rather than smash in place) the liveout info, so it is alive452// for the "collect_gc_info" phase later.453IndexSet liveout(_live->live(block));454uint last_inst = block->end_idx();455// Compute first nonphi node index456uint first_inst;457for (first_inst = 1; first_inst < last_inst; first_inst++) {458if (!block->get_node(first_inst)->is_Phi()) {459break;460}461}462463// Spills could be inserted before CreateEx node which should be464// first instruction in block after Phis. Move CreateEx up.465for (uint insidx = first_inst; insidx < last_inst; insidx++) {466Node *ex = block->get_node(insidx);467if (ex->is_SpillCopy()) {468continue;469}470if (insidx > first_inst && ex->is_Mach() && ex->as_Mach()->ideal_Opcode() == Op_CreateEx) {471// If the CreateEx isn't above all the MachSpillCopies472// then move it to the top.473block->remove_node(insidx);474block->insert_node(ex, first_inst);475}476// Stop once a CreateEx or any other node is found477break;478}479480// Reset block's register pressure values for each ifg construction481uint pressure[2], hrp_index[2];482pressure[0] = pressure[1] = 0;483hrp_index[0] = hrp_index[1] = last_inst+1;484block->_reg_pressure = block->_freg_pressure = 0;485// Liveout things are presumed live for the whole block. We accumulate486// 'area' accordingly. If they get killed in the block, we'll subtract487// the unused part of the block from the area.488int inst_count = last_inst - first_inst;489double cost = (inst_count <= 0) ? 0.0 : block->_freq * double(inst_count);490assert(!(cost < 0.0), "negative spill cost" );491IndexSetIterator elements(&liveout);492uint lidx;493while ((lidx = elements.next()) != 0) {494LRG &lrg = lrgs(lidx);495lrg._area += cost;496// Compute initial register pressure497if (lrg.mask().is_UP() && lrg.mask_size()) {498if (lrg._is_float || lrg._is_vector) { // Count float pressure499pressure[1] += lrg.reg_pressure();500if (pressure[1] > block->_freg_pressure) {501block->_freg_pressure = pressure[1];502}503// Count int pressure, but do not count the SP, flags504} else if(lrgs(lidx).mask().overlap(*Matcher::idealreg2regmask[Op_RegI])) {505pressure[0] += lrg.reg_pressure();506if (pressure[0] > block->_reg_pressure) {507block->_reg_pressure = pressure[0];508}509}510}511}512assert( pressure[0] == count_int_pressure (&liveout), "" );513assert( pressure[1] == count_float_pressure(&liveout), "" );514515// The IFG is built by a single reverse pass over each basic block.516// Starting with the known live-out set, we remove things that get517// defined and add things that become live (essentially executing one518// pass of a standard LIVE analysis). Just before a Node defines a value519// (and removes it from the live-ness set) that value is certainly live.520// The defined value interferes with everything currently live. The521// value is then removed from the live-ness set and it's inputs are added522// to the live-ness set.523uint j;524for (j = last_inst + 1; j > 1; j--) {525Node* n = block->get_node(j - 1);526527// Get value being defined528uint r = _lrg_map.live_range_id(n);529530// Some special values do not allocate531if(r) {532// A DEF normally costs block frequency; rematerialized values are533// removed from the DEF sight, so LOWER costs here.534lrgs(r)._cost += n->rematerialize() ? 0 : block->_freq;535536// If it is not live, then this instruction is dead. Probably caused537// by spilling and rematerialization. Who cares why, yank this baby.538if( !liveout.member(r) && n->Opcode() != Op_SafePoint ) {539Node *def = n->in(0);540if( !n->is_Proj() ||541// Could also be a flags-projection of a dead ADD or such.542(_lrg_map.live_range_id(def) && !liveout.member(_lrg_map.live_range_id(def)))) {543bool remove = true;544if (n->is_MachProj()) {545// Don't remove KILL projections if their "defining" nodes have546// memory effects (have SCMemProj projection node) -547// they are not dead even when their result is not used.548// For example, compareAndSwapL (and other CAS) and EncodeISOArray nodes.549// The method add_input_to_liveout() keeps such nodes alive (put them on liveout list)550// when it sees SCMemProj node in a block. Unfortunately SCMemProj node could be placed551// in block in such order that KILL MachProj nodes are processed first.552uint cnt = def->outcnt();553for (uint i = 0; i < cnt; i++) {554Node* proj = def->raw_out(i);555if (proj->Opcode() == Op_SCMemProj) {556remove = false;557break;558}559}560}561if (remove) {562block->remove_node(j - 1);563if (lrgs(r)._def == n) {564lrgs(r)._def = 0;565}566n->disconnect_inputs(NULL, C);567_cfg.unmap_node_from_block(n);568n->replace_by(C->top());569// Since yanking a Node from block, high pressure moves up one570hrp_index[0]--;571hrp_index[1]--;572continue;573}574}575576// Fat-projections kill many registers which cannot be used to577// hold live ranges.578if (lrgs(r)._fat_proj) {579// Count the int-only registers580RegMask itmp = lrgs(r).mask();581itmp.AND(*Matcher::idealreg2regmask[Op_RegI]);582int iregs = itmp.Size();583if (pressure[0]+iregs > block->_reg_pressure) {584block->_reg_pressure = pressure[0] + iregs;585}586if (pressure[0] <= (uint)INTPRESSURE && pressure[0] + iregs > (uint)INTPRESSURE) {587hrp_index[0] = j - 1;588}589// Count the float-only registers590RegMask ftmp = lrgs(r).mask();591ftmp.AND(*Matcher::idealreg2regmask[Op_RegD]);592int fregs = ftmp.Size();593if (pressure[1] + fregs > block->_freg_pressure) {594block->_freg_pressure = pressure[1] + fregs;595}596if(pressure[1] <= (uint)FLOATPRESSURE && pressure[1]+fregs > (uint)FLOATPRESSURE) {597hrp_index[1] = j - 1;598}599}600601} else { // Else it is live602// A DEF also ends 'area' partway through the block.603lrgs(r)._area -= cost;604assert(!(lrgs(r)._area < 0.0), "negative spill area" );605606// Insure high score for immediate-use spill copies so they get a color607if( n->is_SpillCopy()608&& lrgs(r).is_singledef() // MultiDef live range can still split609&& n->outcnt() == 1 // and use must be in this block610&& _cfg.get_block_for_node(n->unique_out()) == block) {611// All single-use MachSpillCopy(s) that immediately precede their612// use must color early. If a longer live range steals their613// color, the spill copy will split and may push another spill copy614// further away resulting in an infinite spill-split-retry cycle.615// Assigning a zero area results in a high score() and a good616// location in the simplify list.617//618619Node *single_use = n->unique_out();620assert(block->find_node(single_use) >= j, "Use must be later in block");621// Use can be earlier in block if it is a Phi, but then I should be a MultiDef622623// Find first non SpillCopy 'm' that follows the current instruction624// (j - 1) is index for current instruction 'n'625Node *m = n;626for (uint i = j; i <= last_inst && m->is_SpillCopy(); ++i) {627m = block->get_node(i);628}629if (m == single_use) {630lrgs(r)._area = 0.0;631}632}633634// Remove from live-out set635if( liveout.remove(r) ) {636// Adjust register pressure.637// Capture last hi-to-lo pressure transition638lower_pressure(&lrgs(r), j - 1, block, pressure, hrp_index);639assert( pressure[0] == count_int_pressure (&liveout), "" );640assert( pressure[1] == count_float_pressure(&liveout), "" );641}642643// Copies do not define a new value and so do not interfere.644// Remove the copies source from the liveout set before interfering.645uint idx = n->is_Copy();646if (idx) {647uint x = _lrg_map.live_range_id(n->in(idx));648if (liveout.remove(x)) {649lrgs(x)._area -= cost;650// Adjust register pressure.651lower_pressure(&lrgs(x), j - 1, block, pressure, hrp_index);652assert( pressure[0] == count_int_pressure (&liveout), "" );653assert( pressure[1] == count_float_pressure(&liveout), "" );654}655}656} // End of if live or not657658// Interfere with everything live. If the defined value must659// go in a particular register, just remove that register from660// all conflicting parties and avoid the interference.661662// Make exclusions for rematerializable defs. Since rematerializable663// DEFs are not bound but the live range is, some uses must be bound.664// If we spill live range 'r', it can rematerialize at each use site665// according to its bindings.666const RegMask &rmask = lrgs(r).mask();667if( lrgs(r).is_bound() && !(n->rematerialize()) && rmask.is_NotEmpty() ) {668// Check for common case669int r_size = lrgs(r).num_regs();670OptoReg::Name r_reg = (r_size == 1) ? rmask.find_first_elem() : OptoReg::Physical;671// Smear odd bits672IndexSetIterator elements(&liveout);673uint l;674while ((l = elements.next()) != 0) {675LRG &lrg = lrgs(l);676// If 'l' must spill already, do not further hack his bits.677// He'll get some interferences and be forced to spill later.678if( lrg._must_spill ) continue;679// Remove bound register(s) from 'l's choices680RegMask old = lrg.mask();681uint old_size = lrg.mask_size();682// Remove the bits from LRG 'r' from LRG 'l' so 'l' no683// longer interferes with 'r'. If 'l' requires aligned684// adjacent pairs, subtract out bit pairs.685assert(!lrg._is_vector || !lrg._fat_proj, "sanity");686if (lrg.num_regs() > 1 && !lrg._fat_proj) {687RegMask r2mask = rmask;688// Leave only aligned set of bits.689r2mask.smear_to_sets(lrg.num_regs());690// It includes vector case.691lrg.SUBTRACT( r2mask );692lrg.compute_set_mask_size();693} else if( r_size != 1 ) { // fat proj694lrg.SUBTRACT( rmask );695lrg.compute_set_mask_size();696} else { // Common case: size 1 bound removal697if( lrg.mask().Member(r_reg) ) {698lrg.Remove(r_reg);699lrg.set_mask_size(lrg.mask().is_AllStack() ? LRG::AllStack_size : old_size - 1);700}701}702// If 'l' goes completely dry, it must spill.703if( lrg.not_free() ) {704// Give 'l' some kind of reasonable mask, so he picks up705// interferences (and will spill later).706lrg.set_mask( old );707lrg.set_mask_size(old_size);708must_spill++;709lrg._must_spill = 1;710lrg.set_reg(OptoReg::Name(LRG::SPILL_REG));711}712}713} // End of if bound714715// Now interference with everything that is live and has716// compatible register sets.717interfere_with_live(r,&liveout);718719} // End of if normal register-allocated value720721// Area remaining in the block722inst_count--;723cost = (inst_count <= 0) ? 0.0 : block->_freq * double(inst_count);724725// Make all inputs live726if( !n->is_Phi() ) { // Phi function uses come from prior block727JVMState* jvms = n->jvms();728uint debug_start = jvms ? jvms->debug_start() : 999999;729// Start loop at 1 (skip control edge) for most Nodes.730// SCMemProj's might be the sole use of a StoreLConditional.731// While StoreLConditionals set memory (the SCMemProj use)732// they also def flags; if that flag def is unused the733// allocator sees a flag-setting instruction with no use of734// the flags and assumes it's dead. This keeps the (useless)735// flag-setting behavior alive while also keeping the (useful)736// memory update effect.737for (uint k = ((n->Opcode() == Op_SCMemProj) ? 0:1); k < n->req(); k++) {738Node *def = n->in(k);739uint x = _lrg_map.live_range_id(def);740if (!x) {741continue;742}743LRG &lrg = lrgs(x);744// No use-side cost for spilling debug info745if (k < debug_start) {746// A USE costs twice block frequency (once for the Load, once747// for a Load-delay). Rematerialized uses only cost once.748lrg._cost += (def->rematerialize() ? block->_freq : (block->_freq + block->_freq));749}750// It is live now751if (liveout.insert(x)) {752// Newly live things assumed live from here to top of block753lrg._area += cost;754// Adjust register pressure755if (lrg.mask().is_UP() && lrg.mask_size()) {756if (lrg._is_float || lrg._is_vector) {757pressure[1] += lrg.reg_pressure();758if (pressure[1] > block->_freg_pressure) {759block->_freg_pressure = pressure[1];760}761} else if( lrg.mask().overlap(*Matcher::idealreg2regmask[Op_RegI]) ) {762pressure[0] += lrg.reg_pressure();763if (pressure[0] > block->_reg_pressure) {764block->_reg_pressure = pressure[0];765}766}767}768assert( pressure[0] == count_int_pressure (&liveout), "" );769assert( pressure[1] == count_float_pressure(&liveout), "" );770}771assert(!(lrg._area < 0.0), "negative spill area" );772}773}774} // End of reverse pass over all instructions in block775776// If we run off the top of the block with high pressure and777// never see a hi-to-low pressure transition, just record that778// the whole block is high pressure.779if (pressure[0] > (uint)INTPRESSURE) {780hrp_index[0] = 0;781if (pressure[0] > block->_reg_pressure) {782block->_reg_pressure = pressure[0];783}784}785if (pressure[1] > (uint)FLOATPRESSURE) {786hrp_index[1] = 0;787if (pressure[1] > block->_freg_pressure) {788block->_freg_pressure = pressure[1];789}790}791792// Compute high pressure indice; avoid landing in the middle of projnodes793j = hrp_index[0];794if (j < block->number_of_nodes() && j < block->end_idx() + 1) {795Node* cur = block->get_node(j);796while (cur->is_Proj() || (cur->is_MachNullCheck()) || cur->is_Catch()) {797j--;798cur = block->get_node(j);799}800}801block->_ihrp_index = j;802j = hrp_index[1];803if (j < block->number_of_nodes() && j < block->end_idx() + 1) {804Node* cur = block->get_node(j);805while (cur->is_Proj() || (cur->is_MachNullCheck()) || cur->is_Catch()) {806j--;807cur = block->get_node(j);808}809}810block->_fhrp_index = j;811812#ifndef PRODUCT813// Gather Register Pressure Statistics814if( PrintOptoStatistics ) {815if (block->_reg_pressure > (uint)INTPRESSURE || block->_freg_pressure > (uint)FLOATPRESSURE) {816_high_pressure++;817} else {818_low_pressure++;819}820}821#endif822} // End of for all blocks823824return must_spill;825}826827828