/*
* 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/addnode.hpp"
#include "opto/connode.hpp"
#include "opto/memnode.hpp"
#include "opto/mulnode.hpp"
#include "opto/phaseX.hpp"
#include "opto/subnode.hpp"
// Portions of code courtesy of Clifford Click
//=============================================================================
//------------------------------hash-------------------------------------------
// Hash function over MulNodes. Needs to be commutative; i.e., I swap
// (commute) inputs to MulNodes willy-nilly so the hash function must return
// the same value in the presence of edge swapping.
}
//------------------------------Identity---------------------------------------
// Multiplying a one preserves the other argument
return this;
}
//------------------------------Ideal------------------------------------------
// We also canonicalize the Node, moving constants to the right input,
// and flatten expressions (so that 1+x+2 becomes x+3).
// We are OK if right is a constant, or right is a load and
// left is a non-constant.
// Otherwise, sort inputs (commutativity) to help value numbering.
t2 = t;
progress = this; // Made progress
}
}
// If the right input is a constant, and the left input is a product of a
// constant, flatten the expression tree.
#ifdef ASSERT
// Check for dead loop
assert(false, "dead loop in MulNode::Ideal");
#endif
// Mul of a constant?
// Compute new constant; check for overflow
// The Mul of the flattened expression
progress = this; // Made progress
}
}
}
// If the right input is a constant, and the left input is an add of a
// constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0
// Add of a constant?
// Compute new constant; check for overflow
// Convert (X+con1)*con0 into X*con0
}
}
} // End of is left input an add
} // End of is right input a Mul
return progress;
}
//------------------------------Value-----------------------------------------
// Either input is TOP ==> the result is TOP
// Either input is ZERO ==> the result is ZERO.
// Not valid for floats or doubles since +0.0 * -0.0 --> +0.0
}
// Either input is BOTTOM ==> the result is the local BOTTOM
return bottom_type();
#if defined(IA32)
// Can't trust native compilers to properly fold strict double
// multiplication with round-to-zero on this platform.
}
#endif
}
//=============================================================================
//------------------------------Ideal------------------------------------------
// Check for power-of-2 multiply, then try the regular MulNode::Ideal
// Swap constant to right
// Finish rest of method to use info in 'con'
}
// Now we have a constant Node on the right and the constant in con
// Check for negative constant; if so negate the final result
bool sign_flip = false;
if( con < 0 ) {
sign_flip = true;
}
// Get low bit; check for being the only bit
} else {
// Check for constant with 2 bits set
Node *n1 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(bit1)) ) );
Node *n2 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(bit2)) ) );
// Sleezy: power-of-2 -1. Next time be generic.
Node *n1 = phase->transform( new (phase->C) LShiftINode( in(1), phase->intcon(log2_intptr(temp)) ) );
} else {
}
}
if( sign_flip ) { // Need to negate result?
}
return res; // Return final result
}
//------------------------------mul_ring---------------------------------------
// Compute the product type of two integer ranges into this node.
// Fetch endpoints of all ranges
double a = (double)lo0;
double b = (double)hi0;
double c = (double)lo1;
double d = (double)hi1;
// Compute all endpoints & check for overflow
if( C < D ) {
} else {
}
}
//=============================================================================
//------------------------------Ideal------------------------------------------
// Check for power-of-2 multiply, then try the regular MulNode::Ideal
// Swap constant to right
// Finish rest of method to use info in 'con'
}
// Now we have a constant Node on the right and the constant in con
// Check for negative constant; if so negate the final result
bool sign_flip = false;
if( con < 0 ) {
sign_flip = true;
}
// Get low bit; check for being the only bit
} else {
// Check for constant with 2 bits set
// Sleezy: power-of-2 -1. Next time be generic.
} else {
}
}
if( sign_flip ) { // Need to negate result?
}
return res; // Return final result
}
//------------------------------mul_ring---------------------------------------
// Compute the product type of two integer ranges into this node.
// Fetch endpoints of all ranges
double a = (double)lo0;
double b = (double)hi0;
double c = (double)lo1;
double d = (double)hi1;
// Compute all endpoints & check for overflow
if( C < D ) {
} else {
}
}
//=============================================================================
//------------------------------mul_ring---------------------------------------
// Compute the product type of two double ranges into this node.
}
//=============================================================================
//------------------------------mul_ring---------------------------------------
// Compute the product type of two double ranges into this node.
// We must be multiplying 2 double constants.
}
//=============================================================================
//------------------------------Value------------------------------------------
// Either input is TOP ==> the result is TOP
// Either input is BOTTOM ==> the result is the local BOTTOM
return bot;
// It is not worth trying to constant fold this stuff!
}
//=============================================================================
//------------------------------mul_ring---------------------------------------
// Supplied function returns the product of the inputs IN THE CURRENT RING.
// For the logical operations the ring's MUL is really a logical AND function.
// This also type-checks the inputs for sanity. Guaranteed never to
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
// If either input is a constant, might be able to trim cases
// Both constants? Return bits
}
}
//------------------------------Identity---------------------------------------
// Masking off the high bits of an unsigned load is not required
// x & x => x
// Masking off high bits which are always zero is useless.
return in1;
}
// Masking off the high bits of a unsigned-shift-right is not
// needed either.
if (op == Op_URShiftI) {
return in1;
}
}
}
}
//------------------------------Ideal------------------------------------------
// Special case constant AND mask
// Masking bits off of a Character? Hi bits are already zero.
// Masking bits off of a Short? Loading a Character does some masking
if (can_reshape &&
}
// Masking sign bits off of a Byte? Do an unsigned byte load plus
// an and.
}
}
// Masking off sign bits? Dont make them!
if( lop == Op_RShiftI ) {
// If the AND'ing of the 2 masks has no bits, then only original shifted
// bits survive. NO sign-extension bits survive the maskings.
if( (sign_bits_mask & mask) == 0 ) {
// Use zero-fill shift instead
}
}
}
// 'mod 2'. Negate leaves the low order bit unchanged (think: complement
// plus 1) and the mask is of the low order bit. Skip the negate.
}
//=============================================================================
//------------------------------mul_ring---------------------------------------
// Supplied function returns the product of the inputs IN THE CURRENT RING.
// For the logical operations the ring's MUL is really a logical AND function.
// This also type-checks the inputs for sanity. Guaranteed never to
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
// If either input is a constant, might be able to trim cases
// Both constants? Return bits
}
//------------------------------Identity---------------------------------------
// Masking off the high bits of an unsigned load is not required
// x & x => x
// Masking off high bits which are always zero is useless.
return usr;
}
// Masking off the high bits of a unsigned-shift-right is not
// needed either.
if( lop == Op_URShiftL ) {
return usr;
}
}
}
}
//------------------------------Ideal------------------------------------------
// Special case constant AND mask
// Are we masking a long that was converted from an int with a mask
// that fits in 32-bits? Commute them and use an AndINode. Don't
// convert masks which would cause a sign extension of the integer
// value. This check includes UI2L masks (0x00000000FFFFFFFF) which
// would be optimized away later in Identity.
}
// Masking off sign bits? Dont make them!
if (op == Op_RShiftL) {
// If the AND'ing of the 2 masks has no bits, then only original shifted
// bits survive. NO sign-extension bits survive the maskings.
if( (sign_bits_mask & mask) == 0 ) {
// Use zero-fill shift instead
}
}
}
}
//=============================================================================
//------------------------------Identity---------------------------------------
}
//------------------------------Ideal------------------------------------------
// If the right input is a constant, and the left input is an add of a
// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
// Left input is an add of a constant?
// Transform is legal, but check for profit. Avoid breaking 'i2s'
if( con < 16 ) {
// Compute X << con0
// Compute X<<con0 + (con1<<con0)
}
}
}
// Check for "(x>>c0)<<c0" which just masks off low bits
// Convert to "(x & -(1<<c0))"
// Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
// Convert to "(x & (Y<<c0))"
}
}
// Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits
// before shifting them away.
return NULL;
}
//------------------------------Value------------------------------------------
// A LShiftINode shifts its input2 left by input1 amount.
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// Shift by a multiple of 32 does nothing:
// If the shift is a constant, shift the bounds of the type,
// unless this could lead to an overflow.
// No overflow. The range shifts up cleanly.
}
}
}
//=============================================================================
//------------------------------Identity---------------------------------------
}
//------------------------------Ideal------------------------------------------
// If the right input is a constant, and the left input is an add of a
// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0
// Left input is an add of a constant?
// Avoid dead data cycles from dead loops
// Compute X << con0
// Compute X<<con0 + (con1<<con0)
}
}
// Check for "(x>>c0)<<c0" which just masks off low bits
// Convert to "(x & -(1<<c0))"
// Check for "((x>>c0) & Y)<<c0" which just masks off more low bits
// Convert to "(x & (Y<<c0))"
}
}
// Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits
// before shifting them away.
return NULL;
}
//------------------------------Value------------------------------------------
// A LShiftLNode shifts its input2 left by input1 amount.
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// Shift by a multiple of 64 does nothing:
// If the shift is a constant, shift the bounds of the type,
// unless this could lead to an overflow.
// No overflow. The range shifts up cleanly.
}
}
}
//=============================================================================
//------------------------------Identity---------------------------------------
if( !t2 ) return this;
return in(1);
// Check for useless sign-masking
// Compute masks for which this shifting doesn't change
if( !t11 ) return this;
// Does actual value fit inside of mask?
}
return this;
}
//------------------------------Ideal------------------------------------------
// Inputs may be TOP if they are dead.
// Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
// Such expressions arise normally from shift chains like (byte)(x >> 24).
// Convert to "(x >> shift) & (mask >> shift)"
}
// Check for "(short[i] <<16)>>16" which simply sign-extends
if( shift == 16 &&
// Sign extension is just useless here. Return a RShiftI of zero instead
// returning 'ld' directly. We cannot return an old Node directly as
// that is the job of 'Identity' calls and Identity calls only work on
// direct inputs ('ld' is an extra Node removed from 'this'). The
// combined optimization requires Identity only return direct inputs.
return this;
}
else if( can_reshape &&
// Replace zero-extension-load with sign-extension-load
}
// Check for "(byte[i] <<24)>>24" which simply sign-extends
if( shift == 24 &&
// Sign extension is just useless here
return this;
}
}
return NULL;
}
//------------------------------Value------------------------------------------
// A RShiftINode shifts its input2 right by input1 amount.
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// If the shift is a constant, just shift the bounds of the type.
// For example, if the shift is 31, we just propagate sign bits.
// Shift by a multiple of 32 does nothing:
// Calculate reasonably aggressive bounds for the result.
// This is necessary if we are to correctly type things
// like (x<<24>>24) == ((byte)x).
#ifdef ASSERT
// Make sure we get the sign-capture idiom correct.
}
#endif
return ti;
}
// Signed shift right
}
//=============================================================================
//------------------------------Identity---------------------------------------
}
//------------------------------Value------------------------------------------
// A RShiftLNode shifts its input2 right by input1 amount.
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// If the shift is a constant, just shift the bounds of the type.
// For example, if the shift is 63, we just propagate sign bits.
// Shift by a multiple of 64 does nothing:
// Calculate reasonably aggressive bounds for the result.
// This is necessary if we are to correctly type things
// like (x<<24>>24) == ((byte)x).
#ifdef ASSERT
// Make sure we get the sign-capture idiom correct.
}
#endif
return tl;
}
}
//=============================================================================
//------------------------------Identity---------------------------------------
// Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
// Happens during new-array length computation.
// Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)]
// Check that shift_counts are LogBytesPerWord
return x;
}
}
}
}
}
//------------------------------Ideal------------------------------------------
// We'll be wanting the right-shift amount as a mask of that many bits
// Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32
if( in1_op == Op_URShiftI ) {
}
}
// Check for ((x << z) + Y) >>> z. Replace with x + con>>>z
// The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
// If Q is "X << z" the rounding is useless. Look for patterns like
// ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask.
}
}
// Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z)
// This shortens the mask. Also, if we are extracting a high byte and
// storing it to a buffer, the mask will be removed completely.
// The negative values are easier to materialize than positive ones.
// A typical case from address arithmetic is ((x & ~15) >> 4).
// It's better to change that to ((x >> 4) & ~0) versus
// ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64.
}
}
// Check for "(X << z ) >>> z" which simply zero-extends
if( in1_op == Op_LShiftI &&
return NULL;
}
//------------------------------Value------------------------------------------
// A URShiftINode shifts its input2 right by input1 amount.
// (This is a near clone of RShiftINode::Value.)
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// Shift by a multiple of 32 does nothing:
// Calculate reasonably aggressive bounds for the result.
// If the type has both negative and positive values,
// there are two separate sub-domains to worry about:
// The positive half and the negative half.
}
#ifdef ASSERT
// Make sure we get the sign-capture idiom correct.
}
#endif
return ti;
}
//
// Do not support shifted oops in info for GC
//
// else if( t1->base() == Type::InstPtr ) {
//
// const TypeInstPtr *o = t1->is_instptr();
// if( t1->singleton() )
// return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
// }
// else if( t1->base() == Type::KlassPtr ) {
// const TypeKlassPtr *o = t1->is_klassptr();
// if( t1->singleton() )
// return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift );
// }
}
//=============================================================================
//------------------------------Identity---------------------------------------
}
//------------------------------Ideal------------------------------------------
// note: mask computation below does not work for 0 shift count
// We'll be wanting the right-shift amount as a mask of that many bits
// Check for ((x << z) + Y) >>> z. Replace with x + con>>>z
// The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z".
// If Q is "X << z" the rounding is useless. Look for patterns like
// ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask.
}
}
// Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z)
// This shortens the mask. Also, if we are extracting a high byte and
// storing it to a buffer, the mask will be removed completely.
}
}
// Check for "(X << z ) >>> z" which simply zero-extends
return NULL;
}
//------------------------------Value------------------------------------------
// A URShiftINode shifts its input2 right by input1 amount.
// (This is a near clone of RShiftLNode::Value.)
// Either input is TOP ==> the result is TOP
// Left input is ZERO ==> the result is ZERO.
// Shift by zero does nothing
// Either input is BOTTOM ==> the result is BOTTOM
// Shift by a multiple of 64 does nothing:
// Calculate reasonably aggressive bounds for the result.
// If the type has both negative and positive values,
// there are two separate sub-domains to worry about:
// The positive half and the negative half.
//lo = MIN2(neg_lo, pos_lo); // == 0
//hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift;
}
#ifdef ASSERT
// Make sure we get the sign-capture idiom correct.
}
#endif
return tl;
}
}