/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "compiler/oopMap.hpp"
#include "opto/addnode.hpp"
#include "opto/callnode.hpp"
#include "opto/compile.hpp"
#include "opto/machnode.hpp"
#include "opto/matcher.hpp"
#include "opto/regalloc.hpp"
#include "opto/rootnode.hpp"
#ifdef TARGET_ARCH_x86
# include "vmreg_x86.inline.hpp"
#endif
#ifdef TARGET_ARCH_sparc
# include "vmreg_sparc.inline.hpp"
#endif
#ifdef TARGET_ARCH_zero
# include "vmreg_zero.inline.hpp"
#endif
#ifdef TARGET_ARCH_arm
# include "vmreg_arm.inline.hpp"
#endif
#ifdef TARGET_ARCH_ppc
# include "vmreg_ppc.inline.hpp"
#endif
// The functions in this file builds OopMaps after all scheduling is done.
//
// OopMaps contain a list of all registers and stack-slots containing oops (so
// they can be updated by GC). OopMaps also contain a list of derived-pointer
// base-pointer pairs. When the base is moved, the derived pointer moves to
// follow it. Finally, any registers holding callee-save values are also
// recorded. These might contain oops, but only the caller knows.
//
// BuildOopMaps implements a simple forward reaching-defs solution. At each
// GC point we'll have the reaching-def Nodes. If the reaching Nodes are
// typed as pointers (no offset), then they are oops. Pointers+offsets are
// derived pointers, and bases can be found from them. Finally, we'll also
// track reaching callee-save values. Note that a copy of a callee-save value
// "kills" it's source, so that only 1 copy of a callee-save value is alive at
// a time.
//
// We run a simple bitvector liveness pass to help trim out dead oops. Due to
// irreducible loops, we can have a reaching def of an oop that only reaches
// along one path and no way to know if it's valid or not on the other path.
// The bitvectors are quite dense and the liveness pass is fast.
//
// At GC points, we consult this information to build OopMaps. All reaching
// defs typed as oops are added to the OopMap. Only 1 instance of a
// callee-save register can be recorded. For derived pointers, we'll have to
// find and record the register holding the base.
//
// The reaching def's is a simple 1-pass worklist approach. I tried a clever
// breadth-first approach but it was worse (showed O(n^2) in the
// pick-next-block code).
//
// The relevant data is kept in a struct of arrays (it could just as well be
// an array of structs, but the struct-of-arrays is generally a little more
// efficient). The arrays are indexed by register number (including
// stack-slots as registers) and so is bounded by 200 to 300 elements in
// practice. One array will map to a reaching def Node (or NULL for
// OptoReg::Bad for not-callee-saved.
//------------------------------OopFlow----------------------------------------
// Structure to pass around
// OopFlow structs, when not being actively modified, describe the _end_ of
// this block.
Compile* C;
// Given reaching-defs for this block start, compute it for this block end
// Merge these two OopFlows into the 'this' pointer.
// Copy a 'flow' over an existing flow
// Make a new OopFlow from scratch
// Build an oopmap from the current flow info
};
//------------------------------compute_reach----------------------------------
// Given reaching-defs for this block start, compute it for this block end
if( n->jvms() ) { // Build an OopMap here?
// no map needed for leaf calls
if( n->is_MachSafePoint() && !n->is_MachCallLeaf() ) {
}
}
// Assign new reaching def's.
// Note that I padded the _defs and _callees arrays so it's legal
// to index at _defs[OptoReg::Bad].
// Pass callee-save info around copies
if( idx ) { // Copies move callee-save info
} else if( n->is_Phi() ) { // Phis do not mod callee-saves
} else {
// Find base case for callee saves
}
}
}
}
//------------------------------merge------------------------------------------
// Merge the given flow into the 'this' flow
// Do the merge. If there are any differences, drop to 'bottom' which
// is OptoReg::Bad or NULL depending.
for( int i=0; i<max_reg; i++ ) {
// Merge the callee-save's
// Merge the reaching defs
}
}
//------------------------------clone------------------------------------------
}
//------------------------------make-------------------------------------------
return flow;
}
//------------------------------bit twiddlers----------------------------------
//------------------------------build_oop_map----------------------------------
// Build an oopmap from the current flow info
// For all registers do...
continue; // Ignore if not live
// %%% C2 can use 2 OptoRegs when the physical register is only one 64bit
// register in that case we'll get an non-concrete register for the second
// half. We only need to tell the map the register once!
//
// However for the moment we disable this change and leave things as they
// were.
if (false && r->is_reg() && !r->is_concrete()) {
continue;
}
// See if dead (no reaching def).
// Classify the reaching def as oop, derived, callee-save, dead, or other
if( t->isa_oop_ptr() ) { // Oop or derived?
#ifdef _LP64
// 64-bit pointers record oop-ishness on 2 aligned adjacent registers.
// Make sure both are record from the same reaching def, but do not
// put both into the oopmap.
continue; // Do not record high parts in oopmap
}
#endif
// Check for a legal reg name in the oopMap and bailout if it is not.
if (!omap->legal_vm_reg_name(r)) {
continue;
}
if( mcall ) {
// Outgoing argument GC mask responsibility belongs to the callee,
// not the caller. Inspect the inputs to the call, to see if
// this live-range is one of them.
uint j;
break; // reaching def is an argument oop
if( j < cnt ) // arg oops dont go in GC map
continue; // Continue on to the next register
}
} else { // Else it's derived.
// Find the base of the derived value.
uint i;
// Fast, common case, scan
if( i == n->req() ) { // Missed, try a more generous scan
// Scan again, but this time peek through copies
while( 1 ) {
while( 1 ) { // Follow copies of reaching def to end
if( m == d ) goto found; // breaks 3 loops
if( !idx ) break;
}
if( !idx ) break;
}
}
}
found: ;
// I record liveness at safepoints BEFORE I make the inputs
// live. This is because argument oops are NOT live at a
// safepoint (or at least they cannot appear in the oopmap).
// liveness data but they need to appear in the oopmap.
// Flag it, so next derived pointer won't re-insert into oopmap
// Already missed our turn?
if (b->is_stack() || b->is_concrete() || true ) {
}
}
}
if (b->is_stack() || b->is_concrete() || true ) {
omap->set_derived_oop( r, b);
}
}
} else if( t->isa_narrowoop() ) {
// Check for a legal reg name in the oopMap and bailout if it is not.
if (!omap->legal_vm_reg_name(r)) {
continue;
}
if( mcall ) {
// Outgoing argument GC mask responsibility belongs to the callee,
// not the caller. Inspect the inputs to the call, to see if
// this live-range is one of them.
uint j;
break; // reaching def is an argument oop
if( j < cnt ) // arg oops dont go in GC map
continue; // Continue on to the next register
}
omap->set_narrowoop(r);
// It's a callee-save value
if ( callee->is_concrete() || true ) {
}
} else {
// Other - some reaching non-oop value
#ifdef ASSERT
n->dump();
assert(false, "there should be a oop in OopMap instead of a live raw oop at safepoint");
}
#endif
}
}
#ifdef ASSERT
/* Nice, Intel-only assert
int cnt_callee_saves=0;
int reg2 = 0;
while (OptoReg::is_reg(reg2)) {
if( dup_check[reg2] != 0) cnt_callee_saves++;
assert( cnt_callee_saves==3 || cnt_callee_saves==5, "missed some callee-save" );
reg2++;
}
*/
#endif
#ifdef ASSERT
bool found = false;
found = true;
break;
}
}
}
#endif
return omap;
}
//------------------------------do_liveness------------------------------------
// Compute backwards liveness on registers
static void do_liveness( PhaseRegAlloc *regalloc, PhaseCFG *cfg, Block_List *worklist, int max_reg_ints, Arena *A, Dict *safehash ) {
// On CISC platforms, get the node representing the stack pointer that regalloc
// used for spills
}
// Push preds onto worklist
// ZKM.jar includes tiny infinite loops which are unreached from below.
// If we missed any blocks, we'll retry here after pushing all missed
// blocks on the worklist. Normally this outer loop never trips more
// than once.
while( 1 ) {
// Copy first successor into my tmp_live space
for( int i=0; i<max_reg_ints; i++ )
tmp_live[i] = t[i];
// OR in the remaining live registers
for( int i=0; i<max_reg_ints; i++ )
tmp_live[i] |= t[i];
}
// Now walk tmp_live up the block backwards, computing live
// KILL def'd bits
// Check if m is potentially a CISC alternate instruction (i.e, possibly
// synthesized by RegAlloc from a conventional instruction and a
// spilled input)
bool is_cisc_alternate = false;
if (UseCISCSpill && m) {
is_cisc_alternate = m->is_cisc_alternate();
}
// GEN use'd bits
// If we use the stack pointer in a cisc-alternative instruction,
// check for use as a memory operand. Then reconstruct the RegName
// for this stack location, and set the appropriate bit in the
// live vector 4987749.
if (base == NodeSentinel) {
// Machnode has multiple memory inputs. We are unable to reason
// with these, but are presuming (with trepidation) that not any of
// them are oops. This can be fixed by making get_base_and_disp()
// look at a specific input instead of all inputs.
// Do nothing: the fp operand is either not from a memory use
// (base == NULL) OR the fp is used in a non-memory context
// (base is some other register) OR the offset is not constant,
// so it is not a stack slot.
} else {
} else {
assert(false, "stack_reg not on stack?");
}
}
}
}
if( n->jvms() ) { // Record liveness at safepoint
// This placement of this stanza means inputs to calls are
// considered live at the callsite's OopMap. Argument oops are
// hence live, but NOT included in the oopmap. See cutout in
// build_oop_map. Debug oops are live (and in OopMap).
for( int l=0; l<max_reg_ints; l++ )
}
}
// Now at block top, see if we have any changes. If so, propagate
// to prior blocks.
int l;
for( l=0; l<max_reg_ints; l++ )
break;
if( l<max_reg_ints ) { // Change!
// Copy in new value
for( l=0; l<max_reg_ints; l++ )
// Push preds onto worklist
for( l=1; l<(int)b->num_preds(); l++ )
}
}
// Scan for any missing safepoints. Happens to infinite loops
// ala ZKM.jar
uint i;
uint j;
break;
}
if( i == cfg->_num_blocks )
break; // Got 'em all
#ifndef PRODUCT
#endif
// Force the issue (expensively): recheck everybody
}
}
//------------------------------BuildOopMaps-----------------------------------
// Collect GC mask info - where are all the OOPs?
// Can't resource-mark because I need to leave all those OopMaps around,
// or else I need to resource-mark some arena other than the default.
// ResourceMark rm; // Reclaim all OopFlows when done
// Compute a backwards liveness per register. Needs a bitarray of
// #blocks x (#registers, rounded up to ints)
// Array mapping blocks to completed oopflows
// Do the first block 'by hand' to prime the worklist
// Initialize to 'bottom' (not 'top')
// Do the first block 'by hand' to prime the worklist
// Now worklist contains blocks which have some, but perhaps not all,
// predecessors visited.
// Scan for a block with all predecessors visited, or any randoms slob
// otherwise. All-preds-visited order allows me to recycle OopFlow
// structures rapidly and cut down on the memory footprint.
// Note: not all predecessors might be visited yet (must happen for
// irreducible loops). This is OK, since every live value must have the
// SAME reaching def for the block, so any reaching def is OK.
uint i;
// Ignore root block
// Block is already done? Happens if block has several predecessors,
// he can get on the worklist more than once.
if( flows[b->_pre_order] ) continue;
// If this block has a visited predecessor AND that predecessor has this
// last block as his only undone child, we can move the OopFlow from the
// pred to this block. Otherwise we have to grab a new OopFlow.
uint j;
// Scan this block's preds to find a done predecessor
for( j=1; j<b->num_preds(); j++ ) {
if( p_flow ) { // Predecessor is done
pred = p; // Record some predecessor
// If all successors of p are done except for 'b', then we can carry
// p_flow forward to 'b' without copying, otherwise we have to draw
// from the free_list and clone data.
uint k;
for( k=0; k<p->_num_succs; k++ )
p->_succs[k] != b )
break;
// Either carry-forward the now-unused OopFlow for b's use
// or draw a new one from the free list
if( k==p->_num_succs ) {
break; // Found an ideal pred, use him
}
}
}
if( flow ) {
// We have an OopFlow that's the last-use of a predecessor.
// Carry it forward.
} else { // Draw a new OopFlow from the freelist
if( !free_list )
}
// Mark flow for block. Blocks can only be flowed over once,
// because after the first time they are guarded from entering
// this code again.
// Now push flow forward
// Now push children onto worklist
for( i=0; i<b->_num_succs; i++ )
}
}