mulnode.cpp revision 2667
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1879N/A// Portions of code courtesy of Clifford Click 0N/A//============================================================================= 0N/A//------------------------------hash------------------------------------------- 0N/A// Hash function over MulNodes. Needs to be commutative; i.e., I swap 0N/A// (commute) inputs to MulNodes willy-nilly so the hash function must return 0N/A// the same value in the presence of edge swapping. 0N/A//------------------------------Identity--------------------------------------- 0N/A// Multiplying a one preserves the other argument 0N/A//------------------------------Ideal------------------------------------------ 0N/A// We also canonicalize the Node, moving constants to the right input, 0N/A// and flatten expressions (so that 1+x+2 becomes x+3). 0N/A // We are OK if right is a constant, or right is a load and 0N/A // left is a non-constant. 0N/A // Otherwise, sort inputs (commutativity) to help value numbering. 0N/A // If the right input is a constant, and the left input is a product of a 0N/A // constant, flatten the expression tree. assert(
false,
"dead loop in MulNode::Ideal");
// Compute new constant; check for overflow // The Mul of the flattened expression // 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 // 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//------------------------------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 // Can't trust native compilers to properly fold strict double // multiplication with round-to-zero on this platform. return mul_ring(
t1,
t2);
// Local flavor of type multiplication //============================================================================= //------------------------------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 if(
con == 0 )
return NULL;
// By zero is handled by Value call if(
con ==
1 )
return NULL;
// By one is handled by Identity call // Check for negative constant; if so negate the final result // Get low bit; check for being the only bit if(
bit1 ==
con ) {
// Found a power of 2? // Check for constant with 2 bits set // Sleezy: power-of-2 -1. Next time be generic. return res;
// Return final result //------------------------------mul_ring--------------------------------------- // Compute the product type of two integer ranges into this node. // Fetch endpoints of all ranges // Compute all endpoints & check for overflow if( (
double)A != a*c )
return TypeInt::
INT;
// Overflow? if( (
double)B != a*d )
return TypeInt::
INT;
// Overflow? if( (
double)C != b*c )
return TypeInt::
INT;
// Overflow? if( (
double)D != b*d )
return TypeInt::
INT;
// Overflow? if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints //============================================================================= //------------------------------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 if(
con ==
CONST64(0) )
return NULL;
// By zero is handled by Value call if(
con ==
CONST64(
1) )
return NULL;
// By one is handled by Identity call // Check for negative constant; if so negate the final result // Get low bit; check for being the only bit if(
bit1 ==
con ) {
// Found a power of 2? // Check for constant with 2 bits set // Sleezy: power-of-2 -1. Next time be generic. return res;
// Return final result //------------------------------mul_ring--------------------------------------- // Compute the product type of two integer ranges into this node. // Fetch endpoints of all ranges // Compute all endpoints & check for overflow if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints //============================================================================= //------------------------------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 // 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 // Masking off high bits which are always zero is useless. // Masking off the high bits of a unsigned-shift-right is not if ((
mask &
con) ==
mask)
// If AND is useless, skip it //------------------------------Ideal------------------------------------------ // Special case constant AND mask // Masking bits off of a Character? Hi bits are already zero. (
mask &
0xFFFF0000) )
// Can we make a smaller mask? // Masking bits off of a Short? Loading a Character does some masking // Masking sign bits off of a Byte? Do an unsigned byte load plus // Masking off sign bits? Dont make them! // If the AND'ing of the 2 masks has no bits, then only original shifted // bits survive. NO sign-extension bits survive the maskings. // Use zero-fill shift instead // Check for 'negate/and-1', a pattern emitted when someone asks for // '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 // Masking off high bits which are always zero is useless. // Masking off the high bits of a unsigned-shift-right is not //------------------------------Ideal------------------------------------------ // Special case constant AND mask // Masking sign bits off of an integer? Do an unsigned integer to // NOTE: This check must be *before* we try to convert the AndLNode // to an AndINode and commute it with ConvI2LNode because // 0xFFFFFFFFL masks the whole integer and we get a sign extension, // 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 the AND'ing of the 2 masks has no bits, then only original shifted // bits survive. NO sign-extension bits survive the maskings. // 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 if( t ==
Type::
TOP )
return NULL;
// Right input is dead if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count // Left input is an add of a constant? if(
t12 &&
t12->
is_con() ){
// Left input is an add of a con? // Transform is legal, but check for profit. Avoid breaking 'i2s' // and 'i2b' patterns which typically fold into 'StoreC/StoreB'. // 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. //------------------------------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 if( t ==
Type::
TOP )
return NULL;
// Right input is dead if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count // Left input is an add of a constant? // Avoid dead data cycles from dead loops if(
t12 &&
t12->
is_con() ){
// Left input is an add of a con? // 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. //------------------------------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--------------------------------------- // Check for useless sign-masking // Compute masks for which this shifting doesn't change int hi = ~
lo;
// 00007FFF // Does actual value fit inside of mask? return in(
1)->
in(
1);
// Then shifting is a nop //------------------------------Ideal------------------------------------------ // Inputs may be TOP if they are dead. if( !
t1 )
return NULL;
// Left input is an integer if (
shift == 0 )
return NULL;
// let Identity() handle 0 shift count // 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 // 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. // Replace zero-extension-load with sign-extension-load // Check for "(byte[i] <<24)>>24" which simply sign-extends // Sign extension is just useless here //------------------------------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). // Make sure we get the sign-capture idiom correct. //============================================================================= //------------------------------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). // Make sure we get the sign-capture idiom correct. //============================================================================= //------------------------------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 //------------------------------Ideal------------------------------------------ const int con =
t2->
get_con() &
31;
// Shift count is always masked if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count // 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 assert(
in(
1) !=
in(
1)->
in(
1),
"dead loop in URShiftINode::Ideal" );
if(
con3 <
32 )
// Only merge shifts if total is < 32 // 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. if(
t3 &&
t3->
is_con() ) {
// Right input is a constant mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) // 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 //------------------------------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. // Make sure we get the sign-capture idiom correct. // Do not support shifted oops in info for GC // else if( t1->base() == Type::InstPtr ) { // const TypeInstPtr *o = t1->is_instptr(); // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); // else if( t1->base() == Type::KlassPtr ) { // const TypeKlassPtr *o = t1->is_klassptr(); // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); //============================================================================= //------------------------------Identity--------------------------------------- //------------------------------Ideal------------------------------------------ if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count // 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. if(
t3 &&
t3->
is_con() ) {
// Right input is a constant mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) // Check for "(X << z ) >>> z" which simply zero-extends //------------------------------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; // Make sure we get the sign-capture idiom correct.