mulnode.cpp revision 1472
3528N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. 2038N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2038N/A * This code is free software; you can redistribute it and/or modify it 2038N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. 2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT 2038N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2038N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2038N/A * version 2 for more details (a copy is included in the LICENSE file that 2038N/A * You should have received a copy of the GNU General Public License version 2038N/A * 2 along with this work; if not, write to the Free Software Foundation, 2038N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2038N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2038N/A// Portions of code courtesy of Clifford Click 3793N/A#
include "incls/_precompiled.incl" 2038N/A//============================================================================= 3793N/A//------------------------------hash------------------------------------------- 2038N/A// Hash function over MulNodes. Needs to be commutative; i.e., I swap 3793N/A// (commute) inputs to MulNodes willy-nilly so the hash function must return 3793N/A// the same value in the presence of edge swapping. 2038N/A//------------------------------Identity--------------------------------------- 2038N/A// Multiplying a one preserves the other argument 2435N/A//------------------------------Ideal------------------------------------------ 5452N/A// We also canonicalize the Node, moving constants to the right input, 5452N/A// and flatten expressions (so that 1+x+2 becomes x+3). 2038N/A // We are OK if right is a constant, or right is a load and 2038N/A // Otherwise, sort inputs (commutativity) to help value numbering. 2038N/A // If the right input is a constant, and the left input is a product of a 2038N/A // constant, flatten the expression tree. 2040N/A // Compute new constant; check for overflow 2431N/A // The Mul of the flattened expression 5452N/A // If the right input is a constant, and the left input is an add of a 4250N/A // constant, flatten the tree: (X+con1)*con0 ==> X*con0 + con1*con0 4250N/A // Compute new constant; check for overflow 4250N/A // Convert (X+con1)*con0 into X*con0 5452N/A }
// End of is left input an add 4250N/A }
// End of is right input a Mul 2038N/A//------------------------------Value----------------------------------------- 2038N/A // Either input is TOP ==> the result is TOP 2038N/A // Either input is ZERO ==> the result is ZERO. 2038N/A // Not valid for floats or doubles since +0.0 * -0.0 --> +0.0 2038N/A // Either input is BOTTOM ==> the result is the local BOTTOM 2431N/A // Can't trust native compilers to properly fold strict double 2038N/A // multiplication with round-to-zero on this platform. 2038N/A//============================================================================= 2038N/A//------------------------------Ideal------------------------------------------ 2038N/A// Check for power-of-2 multiply, then try the regular MulNode::Ideal 2435N/A // Finish rest of method to use info in 'con' 2038N/A // Now we have a constant Node on the right and the constant in con 2038N/A if(
con == 0 )
return NULL;
// By zero is handled by Value call 2038N/A if(
con ==
1 )
return NULL;
// By one is handled by Identity call 2038N/A // Check for negative constant; if so negate the final result 2431N/A // Get low bit; check for being the only bit 2038N/A // Check for constant with 2 bits set 2038N/A // Sleezy: power-of-2 -1. Next time be generic. 2038N/A//------------------------------mul_ring--------------------------------------- 2038N/A// Compute the product type of two integer ranges into this node. 2040N/A // Fetch endpoints of all ranges 2431N/A // Compute all endpoints & check for overflow 2431N/A if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints 2431N/A//============================================================================= 2431N/A//------------------------------Ideal------------------------------------------ 2431N/A// Check for power-of-2 multiply, then try the regular MulNode::Ideal 3793N/A // Finish rest of method to use info in 'con' 2038N/A // Now we have a constant Node on the right and the constant in con 3012N/A // Check for negative constant; if so negate the final result 2038N/A // Get low bit; check for being the only bit 2038N/A // Check for constant with 2 bits set 2038N/A // Sleezy: power-of-2 -1. Next time be generic. 2038N/A//------------------------------mul_ring--------------------------------------- 2038N/A// Compute the product type of two integer ranges into this node. 2038N/A // Fetch endpoints of all ranges 2038N/A // Compute all endpoints & check for overflow 4183N/A if( A < B ) {
lo0 = A;
hi0 = B; }
// Sort range endpoints 3529N/A//============================================================================= 5455N/A//------------------------------mul_ring--------------------------------------- 3529N/A// Compute the product type of two double ranges into this node. 5455N/A//============================================================================= 3529N/A//------------------------------mul_ring--------------------------------------- 3529N/A// Compute the product type of two double ranges into this node. 3529N/A // We must be multiplying 2 double constants. 5455N/A//============================================================================= 5455N/A//------------------------------Value------------------------------------------ 2038N/A // Either input is TOP ==> the result is TOP 2038N/A // Either input is BOTTOM ==> the result is the local BOTTOM 5452N/A // It is not worth trying to constant fold this stuff! 5452N/A//============================================================================= 5452N/A//------------------------------mul_ring--------------------------------------- 5452N/A// Supplied function returns the product of the inputs IN THE CURRENT RING. 2038N/A// For the logical operations the ring's MUL is really a logical AND function. 2038N/A// This also type-checks the inputs for sanity. Guaranteed never to 2038N/A// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 2038N/A // If either input is a constant, might be able to trim cases 2038N/A // Both constants? Return bits 2038N/A//------------------------------Identity--------------------------------------- 2038N/A// Masking off the high bits of an unsigned load is not required 2431N/A // Masking off high bits which are always zero is useless. 2038N/A // Masking off the high bits of a unsigned-shift-right is not 2038N/A//------------------------------Ideal------------------------------------------ 2431N/A // Special case constant AND mask 2431N/A // Masking bits off of a Character? Hi bits are already zero. 2431N/A (
mask &
0xFFFF0000) )
// Can we make a smaller mask? 2040N/A // Masking bits off of a Short? Loading a Character does some masking 2431N/A // Masking sign bits off of a Byte? Do an unsigned byte load plus 2038N/A // Masking off sign bits? Dont make them! 2038N/A // If the AND'ing of the 2 masks has no bits, then only original shifted 2038N/A // bits survive. NO sign-extension bits survive the maskings. 2038N/A // Use zero-fill shift instead 2038N/A // 'mod 2'. Negate leaves the low order bit unchanged (think: complement 2038N/A // plus 1) and the mask is of the low order bit. Skip the negate. 2038N/A//============================================================================= 2431N/A//------------------------------mul_ring--------------------------------------- 3240N/A// Supplied function returns the product of the inputs IN THE CURRENT RING. 3530N/A// For the logical operations the ring's MUL is really a logical AND function. 2038N/A// This also type-checks the inputs for sanity. Guaranteed never to 3530N/A// be passed a TOP or BOTTOM type, these are filtered out by pre-check. 2431N/A // If either input is a constant, might be able to trim cases 2038N/A // Both constants? Return bits 3240N/A//------------------------------Identity--------------------------------------- 4245N/A// Masking off the high bits of an unsigned load is not required 2038N/A // Masking off high bits which are always zero is useless. 2038N/A // Masking off the high bits of a unsigned-shift-right is not 2038N/A//------------------------------Ideal------------------------------------------ 2038N/A // Special case constant AND mask 2038N/A // Masking sign bits off of an integer? Do an unsigned integer to 2038N/A // NOTE: This check must be *before* we try to convert the AndLNode 2038N/A // to an AndINode and commute it with ConvI2LNode because 2040N/A // 0xFFFFFFFFL masks the whole integer and we get a sign extension, 3530N/A // Are we masking a long that was converted from an int with a mask 3530N/A // that fits in 32-bits? Commute them and use an AndINode. Don't 2038N/A // convert masks which would cause a sign extension of the integer 2431N/A // value. This check includes UI2L masks (0x00000000FFFFFFFF) which 2431N/A // would be optimized away later in Identity. 2431N/A // Masking off sign bits? Dont make them! 2431N/A // If the AND'ing of the 2 masks has no bits, then only original shifted 2431N/A // bits survive. NO sign-extension bits survive the maskings. 2038N/A // Use zero-fill shift instead 2431N/A//============================================================================= 2431N/A//------------------------------Identity--------------------------------------- 2431N/A//------------------------------Ideal------------------------------------------ 2431N/A// If the right input is a constant, and the left input is an add of a 2431N/A// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 2040N/A if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count 3011N/A // Left input is an add of a constant? 3530N/A // Transform is legal, but check for profit. Avoid breaking 'i2s' 2431N/A // Compute X<<con0 + (con1<<con0) 2038N/A // Check for "(x>>c0)<<c0" which just masks off low bits 2431N/A // Convert to "(x & -(1<<c0))" 2038N/A // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits 2038N/A // Convert to "(x & (Y<<c0))" 5452N/A // Check for ((x & ((1<<(32-c0))-1)) << c0) which ANDs off high bits 5452N/A // before shifting them away. 5452N/A//------------------------------Value------------------------------------------ 5452N/A// A LShiftINode shifts its input2 left by input1 amount. 5452N/A // Either input is TOP ==> the result is TOP 5452N/A // Left input is ZERO ==> the result is ZERO. 5452N/A // Shift by zero does nothing 5452N/A // Either input is BOTTOM ==> the result is BOTTOM 2038N/A // Shift by a multiple of 32 does nothing: 2038N/A // If the shift is a constant, shift the bounds of the type, 2038N/A // unless this could lead to an overflow. 2038N/A // No overflow. The range shifts up cleanly. 2038N/A//============================================================================= 2040N/A//------------------------------Identity--------------------------------------- 3240N/A//------------------------------Ideal------------------------------------------ 3530N/A// If the right input is a constant, and the left input is an add of a 2038N/A// constant, flatten the tree: (X+con1)<<con0 ==> X<<con0 + con1<<con0 2038N/A if (
con == 0 )
return NULL;
// let Identity() handle 0 shift count 2038N/A // Left input is an add of a constant? 2038N/A // Avoid dead data cycles from dead loops 2038N/A // Compute X<<con0 + (con1<<con0) 2038N/A // Check for "(x>>c0)<<c0" which just masks off low bits 2038N/A // Convert to "(x & -(1<<c0))" 2038N/A // Check for "((x>>c0) & Y)<<c0" which just masks off more low bits 2038N/A // Convert to "(x & (Y<<c0))" 2038N/A // Check for ((x & ((CONST64(1)<<(64-c0))-1)) << c0) which ANDs off high bits 2038N/A // before shifting them away. 2431N/A//------------------------------Value------------------------------------------ 2431N/A// A LShiftLNode shifts its input2 left by input1 amount. 2431N/A // Either input is TOP ==> the result is TOP 3530N/A // Left input is ZERO ==> the result is ZERO. 3011N/A // Shift by zero does nothing 2038N/A // Either input is BOTTOM ==> the result is BOTTOM 2431N/A // Shift by a multiple of 64 does nothing: 2431N/A // If the shift is a constant, shift the bounds of the type, 2431N/A // unless this could lead to an overflow. 2431N/A // No overflow. The range shifts up cleanly. 2431N/A//============================================================================= 2431N/A//------------------------------Identity--------------------------------------- 2431N/A // Check for useless sign-masking 2431N/A // Compute masks for which this shifting doesn't change 2038N/A // Does actual value fit inside of mask? 2431N/A//------------------------------Ideal------------------------------------------ 2038N/A // Inputs may be TOP if they are dead. 2040N/A // Check for (x & 0xFF000000) >> 24, whose mask can be made smaller. 2040N/A // Such expressions arise normally from shift chains like (byte)(x >> 24). 2040N/A // Convert to "(x >> shift) & (mask >> shift)" 2040N/A // Check for "(short[i] <<16)>>16" which simply sign-extends 2040N/A // Sign extension is just useless here. Return a RShiftI of zero instead 2040N/A // returning 'ld' directly. We cannot return an old Node directly as 2040N/A // that is the job of 'Identity' calls and Identity calls only work on 2040N/A // direct inputs ('ld' is an extra Node removed from 'this'). The 2040N/A // combined optimization requires Identity only return direct inputs. 2040N/A // Replace zero-extension-load with sign-extension-load 2040N/A // Check for "(byte[i] <<24)>>24" which simply sign-extends 2040N/A // Sign extension is just useless here 3530N/A//------------------------------Value------------------------------------------ 2040N/A// A RShiftINode shifts its input2 right by input1 amount. 3530N/A // Either input is TOP ==> the result is TOP 2435N/A // Left input is ZERO ==> the result is ZERO. 2435N/A // Shift by zero does nothing 2435N/A // Either input is BOTTOM ==> the result is BOTTOM 2435N/A // If the shift is a constant, just shift the bounds of the type. 2435N/A // For example, if the shift is 31, we just propagate sign bits. 2435N/A // Shift by a multiple of 32 does nothing: 2038N/A // Calculate reasonably aggressive bounds for the result. 2040N/A // This is necessary if we are to correctly type things 3530N/A // like (x<<24>>24) == ((byte)x). 3530N/A // Make sure we get the sign-capture idiom correct. 3530N/A//============================================================================= 3530N/A//------------------------------Identity--------------------------------------- 3530N/A//------------------------------Value------------------------------------------ 3530N/A// A RShiftLNode shifts its input2 right by input1 amount. 3530N/A // Either input is TOP ==> the result is TOP 3530N/A // Left input is ZERO ==> the result is ZERO. 3530N/A // Shift by zero does nothing 3530N/A // Either input is BOTTOM ==> the result is BOTTOM 2040N/A // If the shift is a constant, just shift the bounds of the type. 3530N/A // For example, if the shift is 63, we just propagate sign bits. 3530N/A // Shift by a multiple of 64 does nothing: 3530N/A // Calculate reasonably aggressive bounds for the result. 3530N/A // This is necessary if we are to correctly type things 3530N/A // like (x<<24>>24) == ((byte)x). 3530N/A // Make sure we get the sign-capture idiom correct. 3530N/A//============================================================================= 3530N/A//------------------------------Identity--------------------------------------- 3530N/A // Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x". 3530N/A // Happens during new-array length computation. 2040N/A // Safe if 'x' is in the range [0..(max_int>>LogBytesPerWord)] 3530N/A // Check that shift_counts are LogBytesPerWord 3530N/A//------------------------------Ideal------------------------------------------ 3530N/A if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count 3530N/A // We'll be wanting the right-shift amount as a mask of that many bits 3530N/A // Check for ((x>>>a)>>>b) and replace with (x>>>(a+b)) when a+b < 32 2040N/A if(
con3 <
32 )
// Only merge shifts if total is < 32 3530N/A // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 3530N/A // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 3530N/A // If Q is "X << z" the rounding is useless. Look for patterns like 3530N/A // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 3530N/A // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 3530N/A // This shortens the mask. Also, if we are extracting a high byte and 3530N/A // storing it to a buffer, the mask will be removed completely. 2435N/A mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) 2435N/A // The negative values are easier to materialize than positive ones. 2435N/A // A typical case from address arithmetic is ((x & ~15) >> 4). 2435N/A // It's better to change that to ((x >> 4) & ~0) versus 2435N/A // ((x >> 4) & 0x0FFFFFFF). The difference is greatest in LP64. 2435N/A // Check for "(X << z ) >>> z" which simply zero-extends 3530N/A//------------------------------Value------------------------------------------ 3530N/A// A URShiftINode shifts its input2 right by input1 amount. 3530N/A // (This is a near clone of RShiftINode::Value.) 2040N/A // Either input is TOP ==> the result is TOP 2038N/A // Left input is ZERO ==> the result is ZERO. 2040N/A // Shift by zero does nothing 2431N/A // Either input is BOTTOM ==> the result is BOTTOM 2431N/A // Shift by a multiple of 32 does nothing: 2431N/A // Calculate reasonably aggressive bounds for the result. 2431N/A // If the type has both negative and positive values, 2038N/A // there are two separate sub-domains to worry about: 2038N/A // The positive half and the negative half. 2040N/A // Make sure we get the sign-capture idiom correct. 2040N/A // Do not support shifted oops in info for GC 2040N/A // else if( t1->base() == Type::InstPtr ) { 2040N/A // const TypeInstPtr *o = t1->is_instptr(); 2040N/A // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); 2431N/A // else if( t1->base() == Type::KlassPtr ) { 3241N/A // const TypeKlassPtr *o = t1->is_klassptr(); 4183N/A // return TypeInt::make( ((uint32)o->const_oop() + o->_offset) >> shift ); 2431N/A//============================================================================= 2431N/A//------------------------------Identity--------------------------------------- 2040N/A//------------------------------Ideal------------------------------------------ 3241N/A if (
con == 0 )
return NULL;
// let Identity() handle a 0 shift count 2040N/A // note: mask computation below does not work for 0 shift count 4183N/A // We'll be wanting the right-shift amount as a mask of that many bits 2040N/A // Check for ((x << z) + Y) >>> z. Replace with x + con>>>z 2040N/A // The idiom for rounding to a power of 2 is "(Q+(2^z-1)) >>> z". 2040N/A // If Q is "X << z" the rounding is useless. Look for patterns like 2040N/A // ((X<<Z) + Y) >>> Z and replace with (X + Y>>>Z) & Z-mask. 2038N/A // Check for (x & mask) >>> z. Replace with (x >>> z) & (mask >>> z) 2038N/A // This shortens the mask. Also, if we are extracting a high byte and 2038N/A // storing it to a buffer, the mask will be removed completely. 2040N/A mask2 >>=
con;
// *signed* shift downward (high-order zeroes do not help) 2038N/A // Check for "(X << z ) >>> z" which simply zero-extends 3011N/A//------------------------------Value------------------------------------------ 3011N/A// A URShiftINode shifts its input2 right by input1 amount. 2038N/A // (This is a near clone of RShiftLNode::Value.) 2038N/A // Either input is TOP ==> the result is TOP 2038N/A // Left input is ZERO ==> the result is ZERO. 2038N/A // Shift by zero does nothing 2038N/A // Either input is BOTTOM ==> the result is BOTTOM 2038N/A // Shift by a multiple of 64 does nothing: 2040N/A // Calculate reasonably aggressive bounds for the result. 2038N/A // If the type has both negative and positive values, 2038N/A // there are two separate sub-domains to worry about: 2038N/A // The positive half and the negative half. 2038N/A //lo = MIN2(neg_lo, pos_lo); // == 0 2038N/A //hi = MAX2(neg_hi, pos_hi); // == -1 >>> shift; 4250N/A // Make sure we get the sign-capture idiom correct.