/*
* 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 "memory/allocation.inline.hpp"
#include "opto/c2compiler.hpp"
#include "opto/callnode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/machnode.hpp"
#include "opto/runtime.hpp"
#ifdef TARGET_ARCH_MODEL_x86_32
# include "adfiles/ad_x86_32.hpp"
#endif
#ifdef TARGET_ARCH_MODEL_x86_64
# include "adfiles/ad_x86_64.hpp"
#endif
#ifdef TARGET_ARCH_MODEL_sparc
# include "adfiles/ad_sparc.hpp"
#endif
#ifdef TARGET_ARCH_MODEL_zero
# include "adfiles/ad_zero.hpp"
#endif
#ifdef TARGET_ARCH_MODEL_arm
# include "adfiles/ad_arm.hpp"
#endif
#ifdef TARGET_ARCH_MODEL_ppc
# include "adfiles/ad_ppc.hpp"
#endif
// Optimization - Graph Style
//------------------------------implicit_null_check----------------------------
// Detect implicit-null-check opportunities. Basically, find NULL checks
// with suitable memory ops nearby. Use the memory op to do the NULL check.
// I can generate a memory op if there is not one nearby.
// The proj is the control projection for the not-null case.
// The val is the pointer being checked for nullness or
// decodeHeapOop_not_null node if it did not fold into address.
// Assume if null check need for 0 offset then always needed
// Intel solaris doesn't support any null checks yet and no
// mechanism exists (yet) to set the switches at an os_cpu level
// Make sure the ptr-is-null path appears to be uncommon!
if( f > PROB_UNLIKELY_MAG(4) ) return;
// Get the successor block for if the test ptr is non-null
null_block = _succs[0];
} else {
not_null_block = _succs[0];
}
}
// Search the exception block for an uncommon trap.
// (See Parse::do_if and Parse::do_ifnull for the reason
// we need an uncommon trap. Briefly, we need a way to
// detect failure of this optimization, as in 6366351.)
{
bool found_trap = false;
if (nn->is_MachCall() &&
// This uncommon trap is sure to recompile, eventually.
// When that happens, C->too_many_traps will prevent
// this transformation from happening again.
found_trap = true;
}
}
break;
}
}
if (!found_trap) {
// We did not find an uncommon trap.
return;
}
}
// Check for decodeHeapOop_not_null node which did not fold into address
// Search the successor block for a load or store who's base value is also
// the tested value. There may be several.
if( !m->is_Mach() ) continue;
was_store = false;
switch( iop ) {
case Op_LoadB:
case Op_LoadUB:
case Op_LoadUS:
case Op_LoadD:
case Op_LoadF:
case Op_LoadI:
case Op_LoadL:
case Op_LoadP:
case Op_LoadN:
case Op_LoadS:
case Op_LoadKlass:
case Op_LoadNKlass:
case Op_LoadRange:
case Op_LoadD_unaligned:
case Op_LoadL_unaligned:
break;
case Op_StoreB:
case Op_StoreC:
case Op_StoreCM:
case Op_StoreD:
case Op_StoreF:
case Op_StoreI:
case Op_StoreL:
case Op_StoreP:
case Op_StoreN:
was_store = true; // Memory op is a store op
// Stores will have their address in slot 2 (memory in slot 1).
// If the value being nul-checked is in another slot, it means we
// are storing the checked value, which does NOT check the value!
break; // Found a memory op?
case Op_StrComp:
case Op_StrEquals:
case Op_StrIndexOf:
case Op_AryEq:
// Not a legit memory op for implicit null check regardless of
// embedded loads
continue;
default: // Also check for embedded loads
if( !mach->needs_anti_dependence_check() )
continue; // Not an memory op; skip it
if( must_clone[iop] ) {
// Do not move nodes which produce flags because
// RA will try to clone it to place near branch and
// it will cause recompilation, see clone_node().
continue;
}
{
// Check that value is used in memory address in
// instructions with embedded load (CmpP val1,(val2+off)).
continue; // Not an memory op; skip it
}
break; // Found it
} else {
continue; // Skip it
}
}
break;
}
// check if the offset is not too high for implicit exception
{
// Narrow oop address doesn't have base, only index
continue; // Give up if offset is beyond page size
// cannot reason about it; is probably not implicit null exception
} else {
// 32-bits narrow oop can be the base of address expressions
} else {
// only regular oops are expected here
}
// Give up if offset is not a compile-time constant
continue;
continue; // Give up is reference is beyond 4K page size
}
}
// Check ctrl input to see if the null-check dominates the memory op
if( !was_store ) { // Stores can be hoisted only one block
// The non-null-block should dominate the memory op, too. Live
// range spilling will insert a spill in the non-null-block if it is
// needs to spill the memory op for an implicit null check.
if (cb != not_null_block) continue;
}
}
if( cb != this ) continue;
// Found a memory user; see if it can be hoisted to check-block
uint j;
vidx = j;
// Ignore DecodeN val which could be hoisted to where needed.
if( is_decoden ) continue;
}
// Block of memory-op input
Block *b = this; // Start from nul check
b = b->_idom; // search upwards for input
// See if input dominates null check
if( b != inb )
break;
}
if( j > 0 )
continue;
// Hoisting stores requires more checks for the anti-dependence case.
// Give up hoisting if we have to move the store past any load.
if( was_store ) {
// mach use (faulting) trying to hoist
// n might be blocker to hoisting
while( b != this ) {
uint k;
if( n->needs_anti_dependence_check() &&
break; // Found anti-dependent load
}
break; // Found anti-dependent load
// Make sure control does not do a merge (would have to check allpaths)
if( b->num_preds() != 2 ) break;
}
if( b != this ) continue;
}
// Make sure this memory op is not already being used for a NullCheck
continue; // Already being used as a NULL check
// Found a candidate! Pick one with least dom depth - the highest
// in the dom tree should be closest to the null check.
if( !best ||
}
}
// No candidate!
if( !best ) return;
// ---- Found an implicit null check
extern int implicit_null_checks;
if( is_decoden ) {
// Check if we need to hoist decodeHeapOop_not_null first.
// Hoist it up to the end of the test block.
// DecodeN on x86 may kill flags. Check for flag-killing projections
// that also need to be hoisted.
if( n->is_MachProj() ) {
this->add_inst(n);
}
}
}
}
// Hoist the memory candidate up to the end of the test block.
// Move the control dependence
// Check for flag-killing projections that also need to be hoisted
// Should be DU safe because no edge updates.
if( n->is_MachProj() ) {
add_inst(n);
}
}
// proj==Op_True --> ne test; proj==Op_False --> eq test.
// One of two graph shapes got matched:
// (IfTrue (If (Bool NE (CmpP ptr NULL))))
// (IfFalse (If (Bool EQ (CmpP ptr NULL))))
// NULL checks are always branch-if-eq. If we see a IfTrue projection
// then we are replacing a 'ne' test with a 'eq' NULL check test.
// We need to flip the projections to keep the same semantics.
// Swap order of projections in basic block to swap branch targets
}
// Remove the existing null check; use a new implicit null check instead.
// Since schedule-local needs precise def-use info, we need to correct
// it as well.
// Redirect users of old_test to nul_chk
// Clean-up any dead code
}
//------------------------------select-----------------------------------------
// Select a nice fellow from the worklist to schedule next. If there is only
// one choice, then use it. Projections take top priority for correctness
// reasons - if I see a projection, then it is next. There are a number of
// other special cases, for instructions that consume condition codes, et al.
// These are chosen immediately. Some instructions are required to immediately
// precede the last instruction in the block, and these are taken last. Of the
// remaining cases (most), choose the instruction with the greatest latency
// (that is, the most number of pseudo-cycles required to the end of the
// routine). If there is a tie, choose the instruction with the most inputs.
Node *Block::select(PhaseCFG *cfg, Node_List &worklist, GrowableArray<int> &ready_cnt, VectorSet &next_call, uint sched_slot) {
// If only a single entry on the stack, use it
if (cnt == 1) {
return n;
}
// Order in worklist is used to break ties.
// See caller for how this is used to delay scheduling
// of induction variable increments to after the other
// uses of the phi are scheduled.
if( n->is_Proj() || // Projections always win
) {
return n;
}
// Final call in a block must be adjacent to 'catch'
continue;
// Memory op for an implicit null check has to be at the end of the block
continue;
// Schedule IV increment last.
continue;
// See if this instruction is consumed by a branch. If so, then (as the
// branch is the last instruction in the basic block) force it to the
// end of the basic block
if ( must_clone[iop] ) {
// See if any use is a branch
bool found_machif = false;
// The use is a conditional branch, make them adjacent
found_machif = true;
break;
}
// More than this instruction pending for successor to be ready,
// don't choose this if other opportunities are ready
n_choice = 1;
}
// loop terminated, prefer not to use this instruction
if (found_machif)
continue;
}
// See if this has a predecessor that is "must_clone", i.e. sets the
// condition code. If so, choose this first
if (inn) {
n_choice = 3;
break;
}
}
}
// MachTemps should be scheduled last so they are near their uses
if (n->is_MachTemp()) {
n_choice = 1;
}
// Keep best latency found
idx = i; // Also keep index in worklist
}
} // End of for all ready nodes in worklist
return n;
}
//------------------------------set_next_call----------------------------------
if( !m ) continue; // must see all nodes in block that precede call
}
}
//------------------------------needed_for_next_call---------------------------
// Set the flag 'next_call' for each Node that is needed for the next call to
// be scheduled. This flag lets me bias scheduling so Nodes needed for the
// next subroutine call get priority - basically it moves things NOT needed
// for the next call till after the call. This prevents me from trying to
// carry lots of stuff live across a call.
// Find the next control-defining Node in this block
m != this_call && // Not self-start node
m->is_MachCall() )
call = m;
break;
}
// Set next-call for all inputs to this call
}
//------------------------------add_call_kills-------------------------------------
void Block::add_call_kills(MachProjNode *proj, RegMask& regs, const char* save_policy, bool exclude_soe) {
// Fill in the kill mask for the call
// Save-on-call register?
if ((save_policy[r] == 'C') ||
(save_policy[r] == 'A') ||
}
}
}
}
//------------------------------sched_call-------------------------------------
uint Block::sched_call( Matcher &matcher, Block_Array &bbs, uint node_cnt, Node_List &worklist, GrowableArray<int> &ready_cnt, MachCallNode *mcall, VectorSet &next_call ) {
// Schedule all the users of the call right now. All the users are
// projection Nodes, so they must be scheduled next to the call.
// Collect all the defined registers.
// Schedule next to call
// Collect defined registers
// Check for scheduling the next control-definer
// Warm up next pile of heuristic bits
// Children of projections are now all ready
if( m->is_Phi() ) continue;
if( m_cnt == 0 )
}
}
// Act as if the call defines the Frame Pointer.
// Certainly the FP is alive and well after the call.
// Set all registers killed and not already defined by the call.
MachProjNode *proj = new (matcher.C) MachProjNode( mcall, r_cnt+1, RegMask::Empty, MachProjNode::fat_proj );
// Select the right register save policy.
const char * save_policy;
switch (op) {
case Op_CallRuntime:
case Op_CallLeaf:
case Op_CallLeafNoFP:
// Calling C code so use C calling convention
break;
case Op_CallStaticJava:
case Op_CallDynamicJava:
// Calling Java code so use Java calling convention
break;
default:
}
// When using CallRuntime mark SOE registers as killed by the call
// so values that could show up in the RegisterMap aren't live in a
// callee saved register since the register wouldn't know where to
// find them. CallLeaf and CallLeafNoFP are ok because they can't
// have debug info on them. Strictly speaking this only needs to be
// done for oops since idealreg2debugmask takes care of debug info
// references but there no way to handle oops differently than other
// pointers as far as the kill mask goes.
// If the call is a MethodHandle invoke, we need to exclude the
// register which is used to save the SP value over MH invokes from
// the mask. Otherwise this register could be used for
// deoptimization information.
if (op == Op_CallStaticJava) {
}
return node_cnt;
}
//------------------------------schedule_local---------------------------------
// Topological sort within a block. Someday become a real scheduler.
bool Block::schedule_local(PhaseCFG *cfg, Matcher &matcher, GrowableArray<int> &ready_cnt, VectorSet &next_call) {
// Already "sorted" are the block start Node (as the first entry), and
// the block-ending Node and any trailing control projections. We leave
// these alone. PhiNodes and ParmNodes are made to follow the block start
// Node. Everything else gets topo-sorted.
#ifndef PRODUCT
if (cfg->trace_opto_pipelining()) {
}
}
#endif
// RootNode is already sorted
// Move PhiNodes and ParmNodes from 1 to cnt up to the start
uint i;
if( n->is_Phi() || // Found a PhiNode or ParmNode
// Move guy at 'phi_cnt' to the end; makes a hole at phi_cnt
} else { // All others
// Count block-local inputs to 'n'
local++; // One more block-local input
}
#ifdef ASSERT
if( UseConcMarkSweepGC || UseG1GC ) {
// Check the precedence edges
assert(cfg->_bbs[oop_store->_idx]->_dom_depth <= this->_dom_depth, "oop_store must dominate card-mark");
}
}
}
}
#endif
// A few node types require changing a required edge to a precedence edge
// before allocation.
// MemBarAcquire could be created without Precedent edge.
// del_req() replaces the specified edge with the last input edge
// and then removes the last edge. If the specified edge > number of
// edges the last edge will be moved outside of the input edges array
// and the edge will be lost. This is why this code should be
// executed only when Precedent (== TypeFunc::Parms) edge is present.
n->add_prec(x);
}
}
}
// All the prescheduled guys do not hold back internal nodes
}
}
}
// Make a worklist
if (m->is_iteratively_computed()) {
// Push induction variable increments last to allow other uses
// of the phi to be scheduled first. The select() method breaks
// ties in scheduling by worklist order.
// Force the CreateEx to the top of the list so it's processed
// first and ends up at the start of the block.
} else {
}
}
}
}
// Warm up the 'next_call' heuristic bits
#ifndef PRODUCT
if (cfg->trace_opto_pipelining()) {
}
}
#endif
// Pull from worklist and schedule
#ifndef PRODUCT
if (cfg->trace_opto_pipelining()) {
}
}
#endif
// Select and pop a ready guy from worklist
#ifndef PRODUCT
if (cfg->trace_opto_pipelining()) {
n->dump();
if (Verbose) {
}
}
}
#endif
if( n->is_MachCall() ) {
continue;
}
}
// Children are now all ready
if( m->is_Phi() ) continue;
continue;
}
if( m_cnt == 0 )
}
}
// did not schedule all. Retry, Bailout, or Die
if (C->subsume_loads() == true && !C->failing()) {
// Retry with subsume_loads == false
// If this is the first failure, the sentinel string will "stick"
// to the Compile object, and the C2Compiler will see it and retry.
}
// assert( phi_cnt == end_idx(), "did not schedule all" );
return false;
}
#ifndef PRODUCT
if (cfg->trace_opto_pipelining()) {
}
}
#endif
return true;
}
//--------------------------catch_cleanup_fix_all_inputs-----------------------
} else {
l--;
}
}
}
}
//------------------------------catch_cleanup_find_cloned_def------------------
static Node *catch_cleanup_find_cloned_def(Block *use_blk, Node *def, Block *def_blk, Block_Array &bbs, int n_clone_idx) {
// The use is some block below the Catch. Find and return the clone of the def
// that dominates the use. If there is no clone in a dominating block, then
// create a phi for the def in a dominating block.
// Find which successor block dominates this use. The successor
// blocks must all be single-entry (from the Catch only; I will have
// split blocks to make this so), hence they all dominate.
// Find the successor
uint j;
for( j = 0; j < def_blk->_num_succs; j++ )
break;
if( j == def_blk->_num_succs ) {
// Block at same level in dom-tree is not a successor. It needs a
// PhiNode, the PhiNode uses from the def and IT's uses need fixup.
inputs.map(k, catch_cleanup_find_cloned_def(bbs[use_blk->pred(k)->_idx], def, def_blk, bbs, n_clone_idx));
}
// Check to see if the use_blk already has an identical phi inserted.
// If it exists, it will be at the first position since all uses of a
// def are processed together.
// Not a match
break;
}
}
}
// If an existing PhiNode was not found, make a new one.
}
}
} else {
// Found the use just below the Catch. Make it use the clone.
}
return fixup;
}
//--------------------------catch_cleanup_intra_block--------------------------
// Fix all input edges in use that reference "def". The use is in the same
// block as the def and both have been cloned in each successor block.
// Both the use and def have been cloned. For each successor block,
// get the clone of the use, and make its input the clone of the def
// found in that block.
// Get clone in each successor block
// Make use-clone reference the def-clone
}
}
//------------------------------catch_cleanup_inter_block---------------------
// Fix all input edges in use that reference "def". The use is in a different
// block than the def.
static void catch_cleanup_inter_block(Node *use, Block *use_blk, Node *def, Block *def_blk, Block_Array &bbs, int n_clone_idx) {
if( !use_blk ) return; // Can happen if the use is a precedence edge
}
//------------------------------call_catch_cleanup-----------------------------
// If we inserted any instructions between a Call and his CatchNode,
// clone the instructions on all paths below the Catch.
// End of region to clone
// Start of region to clone
beg--;
}
// Range of inserted instructions is [beg, end)
// Clone along all Catch output paths. Clone area between the 'beg' and
// 'end' indices.
for( uint i = 0; i < _num_succs; i++ ) {
// Clone the entire area; ignoring the edge fixup for now.
// It is safe here to clone a node with anti_dependence
// since clones dominate on each path.
}
}
// Fixup edges. Check the def-use info per cloned Node
// Need DU safe iterator because of edge manipulation in calls.
}
}
} else {
if (this == buse) {
} else {
}
}
} // End for all users
} // End of for all Nodes in cloned area
// Remove the now-dead cloned ops
}
// If the successor blocks have a CreateEx node, move it back to the top
// Remove any newly created, but dead, nodes.
if (n->outcnt() == 0 &&
n->disconnect_inputs(NULL, C);
new_cnt--;
}
}
// If any newly created nodes remain, move the CreateEx node to the top
if (new_cnt > 0) {
}
}
}
}