mulnode.cpp revision 404
196N/A * Copyright 1997-2008 Sun Microsystems, Inc. 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. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A// Portions of code courtesy of Clifford Click 0N/A#
include "incls/_precompiled.incl" 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. 0N/A // Check for dead loop 0N/A assert(
false,
"dead loop in MulNode::Ideal");
0N/A // Mul of a constant? 0N/A // Compute new constant; check for overflow 0N/A // The Mul of the flattened expression 0N/A // If the right input is a constant, and the left input is an add of a 0N/A // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0 0N/A // Add of a constant? 0N/A // Compute new constant; check for overflow 0N/A // Convert (X+con1)*con0 into X*con0 0N/A }
// End of is left input an add 0N/A }
// End of is right input a Mul 0N/A//------------------------------Value----------------------------------------- 0N/A // Either input is TOP ==> the result is TOP 0N/A // Either input is ZERO ==> the result is ZERO. 0N/A // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0 0N/A // Either input is BOTTOM ==> the result is the local BOTTOM 404N/A // Can't trust native compilers to properly fold strict double 404N/A // multiplication with round-to-zero on this platform. 0N/A//============================================================================= 0N/A//------------------------------Ideal------------------------------------------ 0N/A// Check for power-of-2 multiply, then try the regular MulNode::Ideal 0N/A // Swap constant to right 0N/A // Finish rest of method to use info in 'con' 0N/A // Now we have a constant Node on the right and the constant in con 0N/A if(
con == 0 )
return NULL;
// By zero is handled by Value call 0N/A if(
con ==
1 )
return NULL;
// By one is handled by Identity call 0N/A // Check for negative constant; if so negate the final result 0N/A // Get low bit; check for being the only bit 0N/A // Check for constant with 2 bits set 0N/A // Sleezy: power-of-2 -1. Next time be generic. 0N/A return res;
// Return final result 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Compute the product type of two integer ranges into this node. 0N/A // Fetch endpoints of all ranges 0N/A // Compute all endpoints & check for overflow 0N/A if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints 0N/A//============================================================================= 0N/A//------------------------------Ideal------------------------------------------ 0N/A// Check for power-of-2 multiply, then try the regular MulNode::Ideal 0N/A // Swap constant to right 0N/A // Finish rest of method to use info in 'con' 0N/A // Now we have a constant Node on the right and the constant in con 0N/A // Check for negative constant; if so negate the final result 0N/A // Get low bit; check for being the only bit 0N/A // Check for constant with 2 bits set 0N/A // Sleezy: power-of-2 -1. Next time be generic. 0N/A return res;
// Return final result 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Compute the product type of two integer ranges into this node. 0N/A // Fetch endpoints of all ranges 0N/A // Compute all endpoints & check for overflow 0N/A if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints 0N/A//============================================================================= 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Compute the product type of two double ranges into this node. 0N/A//============================================================================= 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Compute the product type of two double ranges into this node. 404N/A // We must be multiplying 2 double constants. 0N/A//============================================================================= 145N/A//------------------------------Value------------------------------------------ 145N/A // Either input is TOP ==> the result is TOP 145N/A // Either input is BOTTOM ==> the result is the local BOTTOM 145N/A // It is not worth trying to constant fold this stuff! 145N/A//============================================================================= 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Supplied function returns the product of the inputs IN THE CURRENT RING. 0N/A// For the logical operations the ring's MUL is really a logical AND function. 0N/A// This also type-checks the inputs for sanity. Guaranteed never to 0N/A// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 0N/A // If either input is a constant, might be able to trim cases 0N/A // Both constants? Return bits 0N/A//------------------------------Identity--------------------------------------- 0N/A// Masking off the high bits of an unsigned load is not required 0N/A // Masking off high bits which are always zero is useless. 0N/A con ==
0x0000FFFF )
// Already zero-extended 0N/A // Masking off the high bits of a unsigned-shift-right is not 0N/A//------------------------------Ideal------------------------------------------ 0N/A // Special case constant AND mask 0N/A // Masking bits off of a Character? Hi bits are already zero. 0N/A (
mask &
0xFFFF0000) )
// Can we make a smaller mask? 0N/A // Masking bits off of a Short? Loading a Character does some masking 0N/A // Masking sign bits off of a Byte? Let the matcher use an unsigned load 0N/A // Associate this node with the LoadB, so the matcher can see them together. 0N/A // If we don't do this, it is common for the LoadB to have one control 0N/A // edge, and the store or call containing this AndI to have a different 0N/A // control edge. This will cause Label_Root to group the AndI with 0N/A // the encoding store or call, so the matcher has no chance to match 0N/A // this AndI together with the LoadB. Setting the control edge here 0N/A // prevents Label_Root from grouping the AndI with the store or call, 0N/A // if it has a control edge that is inconsistent with the LoadB. 0N/A // Masking off sign bits? Dont make them! 0N/A // If the AND'ing of the 2 masks has no bits, then only original shifted 0N/A // bits survive. NO sign-extension bits survive the maskings. 0N/A // Use zero-fill shift instead 0N/A // 'mod 2'. Negate leaves the low order bit unchanged (think: complement 0N/A // plus 1) and the mask is of the low order bit. Skip the negate. 0N/A//============================================================================= 0N/A//------------------------------mul_ring--------------------------------------- 0N/A// Supplied function returns the product of the inputs IN THE CURRENT RING. 0N/A// For the logical operations the ring's MUL is really a logical AND function. 0N/A// This also type-checks the inputs for sanity. Guaranteed never to 0N/A// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 0N/A // If either input is a constant, might be able to trim cases 0N/A // Both constants? Return bits 0N/A//------------------------------Identity--------------------------------------- 0N/A// Masking off the high bits of an unsigned load is not required 0N/A // Masking off high bits which are always zero is useless. 0N/A // Masking off the high bits of a unsigned-shift-right is not 0N/A//------------------------------Ideal------------------------------------------ 0N/A // Special case constant AND mask 0N/A // Masking off sign bits? Dont make them! 0N/A // If the AND'ing of the 2 masks has no bits, then only original shifted 0N/A // bits survive. NO sign-extension bits survive the maskings. 0N/A // Use zero-fill shift instead 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A//------------------------------Ideal------------------------------------------ 0N/A// If the right input is a constant, and the left input is an add of a 0N/A// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 0N/A if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count 0N/A // Left input is an add of a constant? 0N/A // Transform is legal, but check for profit. Avoid breaking 'i2s' 0N/A // Compute X << con0 0N/A // Compute X<<con0 + (con1<<con0) 0N/A // Check for "(x>>c0)<<c0" which just masks off low bits 0N/A // Convert to "(x & -(1<<c0))" 0N/A // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits 0N/A // Convert to "(x & (Y<<c0))" 0N/A // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits 0N/A // before shifting them away. 0N/A//------------------------------Value------------------------------------------ 0N/A// A LShiftINode shifts its input2 left by input1 amount. 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // Shift by a multiple of 32 does nothing: 0N/A // If the shift is a constant, shift the bounds of the type, 0N/A // unless this could lead to an overflow. 0N/A // No overflow. The range shifts up cleanly. 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A//------------------------------Ideal------------------------------------------ 0N/A// If the right input is a constant, and the left input is an add of a 0N/A// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 0N/A if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count 0N/A // Left input is an add of a constant? 0N/A // Avoid dead data cycles from dead loops 0N/A // Compute X << con0 0N/A // Compute X<<con0 + (con1<<con0) 0N/A // Check for "(x>>c0)<<c0" which just masks off low bits 0N/A // Convert to "(x & -(1<<c0))" 0N/A // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits 0N/A // Convert to "(x & (Y<<c0))" 0N/A // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits 0N/A // before shifting them away. 0N/A//------------------------------Value------------------------------------------ 0N/A// A LShiftLNode shifts its input2 left by input1 amount. 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // Shift by a multiple of 64 does nothing: 0N/A // If the shift is a constant, shift the bounds of the type, 0N/A // unless this could lead to an overflow. 0N/A // No overflow. The range shifts up cleanly. 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A // Check for useless sign-masking 0N/A // Compute masks for which this shifting doesn't change 0N/A // Does actual value fit inside of mask? 0N/A return in(
1)->
in(
1);
// Then shifting is a nop 0N/A//------------------------------Ideal------------------------------------------ 0N/A // Inputs may be TOP if they are dead. 0N/A if( !
t1 )
return NULL;
// Left input is an integer 0N/A if (
shift == 0 )
return NULL;
// let Identity() handle 0 shift count 0N/A // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller. 0N/A // Such expressions arise normally from shift chains like (byte)(x >> 24). 0N/A // Convert to "(x >> shift) & (mask >> shift)" 0N/A // Check for "(short[i] <<16)>>16" which simply sign-extends 0N/A // Sign extension is just useless here. Return a RShiftI of zero instead 0N/A // returning 'ld' directly. We cannot return an old Node directly as 0N/A // that is the job of 'Identity' calls and Identity calls only work on 0N/A // direct inputs ('ld' is an extra Node removed from 'this'). The 0N/A // combined optimization requires Identity only return direct inputs. 0N/A // Replace zero-extension-load with sign-extension-load 0N/A // Check for "(byte[i] <<24)>>24" which simply sign-extends 0N/A // Sign extension is just useless here 0N/A//------------------------------Value------------------------------------------ 0N/A// A RShiftINode shifts its input2 right by input1 amount. 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // If the shift is a constant, just shift the bounds of the type. 0N/A // For example, if the shift is 31, we just propagate sign bits. 0N/A // Shift by a multiple of 32 does nothing: 0N/A // Calculate reasonably aggressive bounds for the result. 0N/A // This is necessary if we are to correctly type things 0N/A // like (x<<24>>24) == ((byte)x). 0N/A // Make sure we get the sign-capture idiom correct. 0N/A // Signed shift right 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A//------------------------------Value------------------------------------------ 0N/A// A RShiftLNode shifts its input2 right by input1 amount. 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // If the shift is a constant, just shift the bounds of the type. 0N/A // For example, if the shift is 63, we just propagate sign bits. 0N/A // Shift by a multiple of 64 does nothing: 0N/A // Calculate reasonably aggressive bounds for the result. 0N/A // This is necessary if we are to correctly type things 0N/A // like (x<<24>>24) == ((byte)x). 0N/A // Make sure we get the sign-capture idiom correct. 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x". 0N/A // Happens during new-array length computation. 0N/A // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)] 0N/A // Check that shift_counts are LogBytesPerWord 0N/A//------------------------------Ideal------------------------------------------ 0N/A if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count 0N/A // We'll be wanting the right-shift amount as a mask of that many bits 0N/A // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32 0N/A if(
con3 <
32 )
// Only merge shifts if total is < 32 0N/A // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 0N/A // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 0N/A // If Q is "X << z" the rounding is useless. Look for patterns like 0N/A // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 0N/A // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 0N/A // This shortens the mask. Also, if we are extracting a high byte and 0N/A // storing it to a buffer, the mask will be removed completely. 0N/A mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) 0N/A // The negative values are easier to materialize than positive ones. 0N/A // A typical case from address arithmetic is ((x & ~15) >> 4). 0N/A // It's better to change that to ((x >> 4) & ~0) versus 0N/A // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64. 0N/A // Check for "(X << z ) >>> z" which simply zero-extends 0N/A//------------------------------Value------------------------------------------ 0N/A// A URShiftINode shifts its input2 right by input1 amount. 0N/A // (This is a near clone of RShiftINode::Value.) 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // Shift by a multiple of 32 does nothing: 0N/A // Calculate reasonably aggressive bounds for the result. 0N/A // If the type has both negative and positive values, 0N/A // there are two separate sub-domains to worry about: 0N/A // The positive half and the negative half. 0N/A // Make sure we get the sign-capture idiom correct. 0N/A // Do not support shifted oops in info for GC 0N/A // else if( t1->base() == Type::InstPtr ) { 0N/A // const TypeInstPtr *o = t1->is_instptr(); 0N/A // if( t1->singleton() ) 0N/A // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); 0N/A // else if( t1->base() == Type::KlassPtr ) { 0N/A // const TypeKlassPtr *o = t1->is_klassptr(); 0N/A // if( t1->singleton() ) 0N/A // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); 0N/A//============================================================================= 0N/A//------------------------------Identity--------------------------------------- 0N/A//------------------------------Ideal------------------------------------------ 0N/A if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count 0N/A // note: mask computation below does not work for 0 shift count 0N/A // We'll be wanting the right-shift amount as a mask of that many bits 0N/A // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 0N/A // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 0N/A // If Q is "X << z" the rounding is useless. Look for patterns like 0N/A // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 0N/A // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 0N/A // This shortens the mask. Also, if we are extracting a high byte and 0N/A // storing it to a buffer, the mask will be removed completely. 0N/A mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) 0N/A // Check for "(X << z ) >>> z" which simply zero-extends 0N/A//------------------------------Value------------------------------------------ 0N/A// A URShiftINode shifts its input2 right by input1 amount. 0N/A // (This is a near clone of RShiftLNode::Value.) 0N/A // Either input is TOP ==> the result is TOP 0N/A // Left input is ZERO ==> the result is ZERO. 0N/A // Shift by zero does nothing 0N/A // Either input is BOTTOM ==> the result is BOTTOM 0N/A // Shift by a multiple of 64 does nothing: 0N/A // Calculate reasonably aggressive bounds for the result. 0N/A // If the type has both negative and positive values, 0N/A // there are two separate sub-domains to worry about: 0N/A // The positive half and the negative half. 0N/A //lo = MIN2(neg_lo, pos_lo); // == 0 0N/A //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 0N/A // Make sure we get the sign-capture idiom correct.