8299546: C2: MulLNode::mul_ring() wrongly returns bottom type due to casting errors with large numbers

Reviewed-by: iveresov, kvn, qamai
This commit is contained in:
Christian Hagedorn 2023-03-14 14:57:47 +00:00
parent 55aa122462
commit c466cdf973
4 changed files with 1038 additions and 70 deletions

View File

@ -281,44 +281,115 @@ Node *MulINode::Ideal(PhaseGVN *phase, bool can_reshape) {
return res; // Return final result
}
//------------------------------mul_ring---------------------------------------
// Compute the product type of two integer ranges into this node.
const Type *MulINode::mul_ring(const Type *t0, const Type *t1) const {
const TypeInt *r0 = t0->is_int(); // Handy access
const TypeInt *r1 = t1->is_int();
// Classes to perform mul_ring() for MulI/MulLNode.
//
// This class checks if all cross products of the left and right input of a multiplication have the same "overflow value".
// Without overflow/underflow:
// Product is positive? High signed multiplication result: 0
// Product is negative? High signed multiplication result: -1
//
// We normalize these values (see normalize_overflow_value()) such that we get the same "overflow value" by adding 1 if
// the product is negative. This allows us to compare all the cross product "overflow values". If one is different,
// compared to the others, then we know that this multiplication has a different number of over- or underflows compared
// to the others. In this case, we need to use bottom type and cannot guarantee a better type. Otherwise, we can take
// the min und max of all computed cross products as type of this Mul node.
template<typename IntegerType>
class IntegerMulRing {
using NativeType = std::conditional_t<std::is_same<TypeInt, IntegerType>::value, jint, jlong>;
// Fetch endpoints of all ranges
jint lo0 = r0->_lo;
double a = (double)lo0;
jint hi0 = r0->_hi;
double b = (double)hi0;
jint lo1 = r1->_lo;
double c = (double)lo1;
jint hi1 = r1->_hi;
double d = (double)hi1;
NativeType _lo_left;
NativeType _lo_right;
NativeType _hi_left;
NativeType _hi_right;
NativeType _lo_lo_product;
NativeType _lo_hi_product;
NativeType _hi_lo_product;
NativeType _hi_hi_product;
short _widen_left;
short _widen_right;
// Compute all endpoints & check for overflow
int32_t A = java_multiply(lo0, lo1);
if( (double)A != a*c ) return TypeInt::INT; // Overflow?
int32_t B = java_multiply(lo0, hi1);
if( (double)B != a*d ) return TypeInt::INT; // Overflow?
int32_t C = java_multiply(hi0, lo1);
if( (double)C != b*c ) return TypeInt::INT; // Overflow?
int32_t D = java_multiply(hi0, hi1);
if( (double)D != b*d ) return TypeInt::INT; // Overflow?
static const Type* overflow_type();
static NativeType multiply_high_signed_overflow_value(NativeType x, NativeType y);
if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
else { lo0 = B; hi0 = A; }
if( C < D ) {
if( C < lo0 ) lo0 = C;
if( D > hi0 ) hi0 = D;
} else {
if( D < lo0 ) lo0 = D;
if( C > hi0 ) hi0 = C;
// Pre-compute cross products which are used at several places
void compute_cross_products() {
_lo_lo_product = java_multiply(_lo_left, _lo_right);
_lo_hi_product = java_multiply(_lo_left, _hi_right);
_hi_lo_product = java_multiply(_hi_left, _lo_right);
_hi_hi_product = java_multiply(_hi_left, _hi_right);
}
return TypeInt::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
bool cross_products_not_same_overflow() const {
const NativeType lo_lo_high_product = multiply_high_signed_overflow_value(_lo_left, _lo_right);
const NativeType lo_hi_high_product = multiply_high_signed_overflow_value(_lo_left, _hi_right);
const NativeType hi_lo_high_product = multiply_high_signed_overflow_value(_hi_left, _lo_right);
const NativeType hi_hi_high_product = multiply_high_signed_overflow_value(_hi_left, _hi_right);
return lo_lo_high_product != lo_hi_high_product ||
lo_hi_high_product != hi_lo_high_product ||
hi_lo_high_product != hi_hi_high_product;
}
static NativeType normalize_overflow_value(const NativeType x, const NativeType y, NativeType result) {
return java_multiply(x, y) < 0 ? result + 1 : result;
}
public:
IntegerMulRing(const IntegerType* left, const IntegerType* right) : _lo_left(left->_lo), _lo_right(right->_lo),
_hi_left(left->_hi), _hi_right(right->_hi), _widen_left(left->_widen), _widen_right(right->_widen) {
compute_cross_products();
}
// Compute the product type by multiplying the two input type ranges. We take the minimum and maximum of all possible
// values (requires 4 multiplications of all possible combinations of the two range boundary values). If any of these
// multiplications overflows/underflows, we need to make sure that they all have the same number of overflows/underflows
// If that is not the case, we return the bottom type to cover all values due to the inconsistent overflows/underflows).
const Type* compute() const {
if (cross_products_not_same_overflow()) {
return overflow_type();
}
const NativeType min = MIN4(_lo_lo_product, _lo_hi_product, _hi_lo_product, _hi_hi_product);
const NativeType max = MAX4(_lo_lo_product, _lo_hi_product, _hi_lo_product, _hi_hi_product);
return IntegerType::make(min, max, MAX2(_widen_left, _widen_right));
}
};
template <>
const Type* IntegerMulRing<TypeInt>::overflow_type() {
return TypeInt::INT;
}
template <>
jint IntegerMulRing<TypeInt>::multiply_high_signed_overflow_value(const jint x, const jint y) {
const jlong x_64 = x;
const jlong y_64 = y;
const jlong product = x_64 * y_64;
const jint result = (jint)((uint64_t)product >> 32u);
return normalize_overflow_value(x, y, result);
}
template <>
const Type* IntegerMulRing<TypeLong>::overflow_type() {
return TypeLong::LONG;
}
template <>
jlong IntegerMulRing<TypeLong>::multiply_high_signed_overflow_value(const jlong x, const jlong y) {
const jlong result = multiply_high_signed(x, y);
return normalize_overflow_value(x, y, result);
}
// Compute the product type of two integer ranges into this node.
const Type* MulINode::mul_ring(const Type* type_left, const Type* type_right) const {
const IntegerMulRing<TypeInt> integer_mul_ring(type_left->is_int(), type_right->is_int());
return integer_mul_ring.compute();
}
// Compute the product type of two long ranges into this node.
const Type* MulLNode::mul_ring(const Type* type_left, const Type* type_right) const {
const IntegerMulRing<TypeLong> integer_mul_ring(type_left->is_long(), type_right->is_long());
return integer_mul_ring.compute();
}
//=============================================================================
//------------------------------Ideal------------------------------------------
@ -377,44 +448,6 @@ Node *MulLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
return res; // Return final result
}
//------------------------------mul_ring---------------------------------------
// Compute the product type of two integer ranges into this node.
const Type *MulLNode::mul_ring(const Type *t0, const Type *t1) const {
const TypeLong *r0 = t0->is_long(); // Handy access
const TypeLong *r1 = t1->is_long();
// Fetch endpoints of all ranges
jlong lo0 = r0->_lo;
double a = (double)lo0;
jlong hi0 = r0->_hi;
double b = (double)hi0;
jlong lo1 = r1->_lo;
double c = (double)lo1;
jlong hi1 = r1->_hi;
double d = (double)hi1;
// Compute all endpoints & check for overflow
jlong A = java_multiply(lo0, lo1);
if( (double)A != a*c ) return TypeLong::LONG; // Overflow?
jlong B = java_multiply(lo0, hi1);
if( (double)B != a*d ) return TypeLong::LONG; // Overflow?
jlong C = java_multiply(hi0, lo1);
if( (double)C != b*c ) return TypeLong::LONG; // Overflow?
jlong D = java_multiply(hi0, hi1);
if( (double)D != b*d ) return TypeLong::LONG; // Overflow?
if( A < B ) { lo0 = A; hi0 = B; } // Sort range endpoints
else { lo0 = B; hi0 = A; }
if( C < D ) {
if( C < lo0 ) lo0 = C;
if( D > hi0 ) hi0 = D;
} else {
if( D < lo0 ) lo0 = D;
if( C > hi0 ) hi0 = C;
}
return TypeLong::make(lo0, hi0, MAX2(r0->_widen,r1->_widen));
}
//=============================================================================
//------------------------------mul_ring---------------------------------------
// Compute the product type of two double ranges into this node.

View File

@ -1235,7 +1235,22 @@ inline TYPE NAME (TYPE lhs, jint rhs) { \
JAVA_INTEGER_SHIFT_OP(<<, java_shift_left, jint, juint)
JAVA_INTEGER_SHIFT_OP(<<, java_shift_left, jlong, julong)
// For signed shift right, assume C++ implementation >> sign extends.
//
// C++14 5.8/3: In the description of "E1 >> E2" it says "If E1 has a signed type
// and a negative value, the resulting value is implementation-defined."
//
// However, C++20 7.6.7/3 further defines integral arithmetic, as part of
// requiring two's-complement behavior.
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r3.html
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1236r1.html
// The corresponding C++20 text is "Right-shift on signed integral types is an
// arithmetic right shift, which performs sign-extension."
//
// As discussed in the two's complement proposal, all known modern C++ compilers
// already behave that way. And it is unlikely any would go off and do something
// different now, with C++20 tightening things up.
JAVA_INTEGER_SHIFT_OP(>>, java_shift_right, jint, jint)
JAVA_INTEGER_SHIFT_OP(>>, java_shift_right, jlong, jlong)
// For >>> use C++ unsigned >>.
@ -1266,6 +1281,38 @@ SATURATED_INTEGER_OP(+, saturated_add, uint, uint)
#undef SATURATED_INTEGER_OP
// Taken from rom section 8-2 of Henry S. Warren, Jr., Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174.
inline uint64_t multiply_high_unsigned(const uint64_t x, const uint64_t y) {
const uint64_t x1 = x >> 32u;
const uint64_t x2 = x & 0xFFFFFFFF;
const uint64_t y1 = y >> 32u;
const uint64_t y2 = y & 0xFFFFFFFF;
const uint64_t z2 = x2 * y2;
const uint64_t t = x1 * y2 + (z2 >> 32u);
uint64_t z1 = t & 0xFFFFFFFF;
const uint64_t z0 = t >> 32u;
z1 += x2 * y1;
return x1 * y1 + z0 + (z1 >> 32u);
}
// Taken from java.lang.Math::multiplyHigh which uses the technique from section 8-2 of Henry S. Warren, Jr.,
// Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174 but adapted for signed longs.
inline int64_t multiply_high_signed(const int64_t x, const int64_t y) {
const jlong x1 = java_shift_right((jlong)x, 32);
const jlong x2 = x & 0xFFFFFFFF;
const jlong y1 = java_shift_right((jlong)y, 32);
const jlong y2 = y & 0xFFFFFFFF;
const uint64_t z2 = x2 * y2;
const int64_t t = x1 * y2 + (z2 >> 32u); // Unsigned shift
int64_t z1 = t & 0xFFFFFFFF;
const int64_t z0 = java_shift_right((jlong)t, 32);
z1 += x2 * y1;
return x1 * y1 + z0 + java_shift_right((jlong)z1, 32);
}
// Dereference vptr
// All C++ compilers that we know of have the vtbl pointer in the first
// word. If there are exceptions, this function needs to be made compiler

View File

@ -0,0 +1,820 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* 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.
*/
package compiler.c2.irTests.igvn;
import compiler.lib.ir_framework.*;
import jdk.test.lib.Asserts;
/*
* @test
* @bug 8299546
* @summary Test that IntegerMulRing works correctly and returns correct (and optimized) types.
* @library /test/lib /
* @run driver compiler.c2.irTests.igvn.TestIntegerMulRing
*/
public class TestIntegerMulRing {
public static int iFld, iFld2, iFld3, iFld4;
public static long lFld, lFld2, lFld3, lFld4;
public static void main(String[] args) {
TestFramework.runWithFlags("-XX:-SplitIfBlocks");
}
@Test
@IR(failOn = {IRNode.STORE_I, IRNode.IF}, counts = {IRNode.STORE_L, "2"})
public static void testLongPositive() {
long l = 111111111111111111L;
if (l * 81 == 1L) {
iFld = 23;
}
if (l * 81 == 8999999999999999991L) {
lFld = 23;
}
if (l * 83 == 1L) {
iFld2 = 34;
}
if (l * 83 == 9222222222222222213L) {
lFld2 = 23;
}
}
@Test
@IR(failOn = {IRNode.STORE_I, IRNode.IF}, counts = {IRNode.STORE_L, "2"})
public static void testLongPositive2() {
long l = -111111111111111111L;
if (l * -81 == 1L) {
iFld = 23;
}
if (l * -81 == 8999999999999999991L) {
lFld = 23;
}
if (l * -83 == 1L) {
iFld2 = 34;
}
if (l * -83 == 9222222222222222213L) {
lFld2 = 23;
}
}
@Test
@IR(failOn = {IRNode.STORE_I, IRNode.IF}, counts = {IRNode.STORE_L, "2"})
public static void testLongNegative() {
long l = -111111111111111111L;
if (l * 81 == 1L) {
iFld = 23;
}
if (l * 81 == -8999999999999999991L) {
lFld = 23;
}
if (l * 83 == 1L) {
iFld2 = 34;
}
if (l * 83 == -9222222222222222213L) {
lFld2 = 23;
}
}
@Test
@IR(failOn = {IRNode.STORE_I, IRNode.IF}, counts = {IRNode.STORE_L, "2"})
public static void testLongNegative2() {
long l = 111111111111111111L;
if (l * -81 == 1L) {
iFld = 23;
}
if (l * -81 == -8999999999999999991L) {
lFld = 23;
}
if (l * -83 == 1L) {
iFld2 = 34;
}
if (l * -83 == -9222222222222222213L) {
lFld2 = 23;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongMinValueMinus1(boolean flag, boolean flag2) {
long l = flag ? -1 : Long.MIN_VALUE;
int x = flag2 ? -1 : 0;
if (l * x != 2L) { // Type of multiplication is LONG as Long.MIN_VALUE * -1 does overflow. If cannot be removed.
lFld = 23;
} else {
lFld = 34; // Emits StoreL since warmup is 0 and no UCT will be emitted.
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_L, IRNode.STORE_L}, counts = {IRNode.STORE_I, "1"})
public static void testLongMinValuePlus1(boolean flag, boolean flag2) {
long l = flag ? -1 : Long.MIN_VALUE;
int x = flag2 ? 1 : 0;
if (l * x <= 0L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_L, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testLongMinValueUnderflowOnce(boolean flag, boolean flag2) {
long l = flag ? Long.MIN_VALUE/2 : Long.MIN_VALUE/2 + 1;
int x = flag2 ? 4 : 6;
if (l * x <= 4L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1", IRNode.MUL_L, "1"})
public static void testLongMinValueUnderflowOnceTwice(boolean flag, boolean flag2) {
long l = flag ? Long.MIN_VALUE/2 : Long.MIN_VALUE/2 + 1;
int x = flag2 ? 6 : 8;
if (l * x <= 4L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_L, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testLongMinValueUnderflowTwice(boolean flag, boolean flag2) {
long l = flag ? Long.MIN_VALUE/2 : Long.MIN_VALUE/2 + 1;
int x = flag2 ? 8 : 10;
if (l * x <= 8L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_L, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testLongMaxValueOverflowOnce(boolean flag, boolean flag2) {
long l = flag2 ? Long.MAX_VALUE/2 - 1 : Long.MAX_VALUE/2;
int x = flag ? 4 : 6;
if (l * x >= -8L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1", IRNode.MUL_L, "1"})
public static void testLongMaxValueOverflowOnceTwice(boolean flag, boolean flag2) {
long l = flag2 ? Long.MAX_VALUE/2 - 1 : Long.MAX_VALUE/2;
int x = flag ? 6 : 8;
if (l * x >= -8L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_L, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testLongMaxValueOverflowTwice(boolean flag, boolean flag2) {
long l = flag2 ? Long.MAX_VALUE/2 - 1 : Long.MAX_VALUE/2;
int x = flag ? 8 : 10;
if (l * x >= -16L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_L, counts = {IRNode.STORE_L, "1"})
public static void testLongProductsOverflowOnceAtMin(boolean flag, boolean flag2) {
long l = flag ? Long.MAX_VALUE/2 + 1 : Long.MAX_VALUE/2 + 2;
int x = flag2 ? 2 : 3;
// [MAX_VALUE/2 + 1, MAX_VALUE/2 + 2] * [2,3]: All cross products overflow exactly once.
// Result: [MIN_VALUE, MIN_VALUE/2 + 3] -> 2L outside range and If can be optimized away.
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_L, counts = {IRNode.STORE_L, "1"})
public static void testLongProductsOverflowOnceAtMax(boolean flag, boolean flag2) {
// 88971434439113593 * 311 = Long.MAX_VALUE*3 + 2 --cast to long--> Long.MAX_VALUE
long l = flag ? 88971434439113592L : 88971434439113593L;
int x = flag2 ? 310 : 311;
// All cross products overflow exactly once.
// Result: [y, MAX_VALUE], where y > 2 -> 2L outside range and If can be optimized away.
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_L, counts = {IRNode.STORE_L, "1"})
public static void testLongProductsUnderflowOnceAtMin(boolean flag, boolean flag2) {
long l = flag ? Long.MIN_VALUE/3 - 1 : Long.MIN_VALUE/3 - 2;
int x = flag2 ? 3 : 4;
// [MIN_VALUE/3 - 1, MIN_VALUE/3 - 2] * [3,4]: All cross products underflow exactly once.
// Result: [MAX_VALUE + MIN_VALUE/3 - 5, MAX_VALUE] -> 2L outside range and If can be optimized away.
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_L, counts = {IRNode.STORE_L, "1"})
public static void testLongProductsUnderflowOnceAtMax(boolean flag, boolean flag2) {
// -6917529027641081856 * 4 = Long.MIN_VALUE*3 --cast to long--> Long.MIN_VALUE
long l = flag ? -6917529027641081856L : -6917529027641081855L;
int x = flag2 ? 3 : 4;
// All cross products underflow exactly once.
// Result: [MIN_VALUE, y], where y < 2 -> 2L outside range and If can be optimized away.
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongProductsDifferentNumberOfOverflow(boolean flag, boolean flag2) {
// 88971434439113593 * 311 = Long.MAX_VALUE*3 + 2 --cast to long--> Long.MAX_VALUE // Overflown once
// 88971434439113594 * 311 = (Long.MAX_VALUE*3 + 311) + 2 --cast to long--> Long.MIN_VALUE + 310 // Overflown twice
long l = flag ? 88971434439113593L : 88971434439113594L;
int x = flag2 ? 310 : 311;
// Different number of overflows -> cannot optimize If away
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongProductsDifferentNumberOfUnderflows(boolean flag, boolean flag2) {
// -6917529027641081856 * 4 = Long.MIN_VALUE*3 --cast to long--> Long.MIN_VALUE // Underflown once
// -6917529027641081857 * 4 = (Long.MIN_VALUE*3 - 4) --cast to long--> Long.MAX_VALUE - 3 // Underflown twice
long l = flag ? -6917529027641081856L : -6917529027641081857L;
int x = flag2 ? 3 : 4;
// Different number of underflows -> cannot optimize If away
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongNotSameOverflow1(boolean flag, boolean flag2) {
long l = flag ? 1 : Long.MAX_VALUE;
int x = flag2 ? -1 : 2;
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongNotSameOverflow2(boolean flag, boolean flag2) {
long l = flag ? 1 : Long.MIN_VALUE;
int x = flag2 ? -1 : 2;
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongNotSameOverflow3(boolean flag, boolean flag2) {
long l = flag ? -1 : Long.MIN_VALUE;
long x = flag2 ? Long.MIN_VALUE : -1;
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongNotSameOverflow4(boolean flag, boolean flag2) {
long l = flag ? -1 : Long.MAX_VALUE;
long x = flag2 ? Long.MAX_VALUE : -1;
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_L, "1"})
public static void testLongNotSameOverflow5(boolean flag, boolean flag2) {
long l = flag ? Long.MIN_VALUE : Long.MAX_VALUE;
long x = flag2 ? Long.MAX_VALUE : -1;
if (l * x != 2L) {
lFld = 23;
} else {
lFld = 34;
}
}
// Int cases
@Test
@IR(failOn = IRNode.IF, counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1"})
public static void testIntPositive() {
int i = 26000000;
if (i * 81 == 1) {
iFld = 23;
}
if (i * 81 == 2106000000) {
iFld = 34;
}
if (i * 83 == 1) {
lFld = 23;
}
if (i * 83 == -2136967296) {
lFld = 34;
}
}
@Test
@IR(failOn = IRNode.IF, counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1"})
public static void testIntPositive2() {
int i = -26000000;
if (i * -81 == 1) {
iFld = 23;
}
if (i * -81 == 2106000000) {
iFld = 34;
}
if (i * -83 == 1) {
lFld = 23;
}
if (i * -83 == -2136967296) {
lFld = 34;
}
}
@Test
@IR(failOn = IRNode.IF, counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1"})
public static void testIntNegative() {
int i = 26000000;
if (i * -81 == 1) {
iFld = 23;
}
if (i * -81 == -2106000000) {
iFld = 34;
}
if (i * -83 == 1) {
lFld = 23;
}
if (i * -83 == 2136967296) {
lFld = 34;
}
}
@Test
@IR(failOn = IRNode.IF, counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1"})
public static void testIntNegative2() {
int i = -26000000;
if (i * 81 == 1) {
iFld = 23;
}
if (i * 81 == -2106000000) {
iFld = 34;
}
if (i * 83 == 1) {
lFld = 23;
}
if (i * 83 == 2136967296) {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntMinValueMinus1(boolean flag, boolean flag2) {
int l = flag ? -1 : Integer.MIN_VALUE;
int x = flag2 ? -1 : 0;
if (l * x != 2) { // Type of multiplication is INT as Integer.MIN_VALUE * -1 does overflow. If cannot be removed.
lFld = 23;
} else {
lFld = 34; // Emits StoreL since warmup is 0 and no UCT will be emitted.
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_I, IRNode.STORE_L}, counts = {IRNode.STORE_I, "1"})
public static void testIntMinValuePlus1(boolean flag, boolean flag2) {
int l = flag ? -1 : Integer.MIN_VALUE;
int x = flag2 ? 1 : 0;
if (l * x <= 0) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_I, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testIntMinValueUnderflowOnce(boolean flag, boolean flag2) {
int l = flag ? Integer.MIN_VALUE/2 : Integer.MIN_VALUE/2 + 1;
int x = flag2 ? 4 : 6;
if (l * x <= 4) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1", IRNode.MUL_I, "1"})
public static void testIntMinValueUnderflowOnceTwice(boolean flag, boolean flag2) {
int l = flag ? Integer.MIN_VALUE/2 : Integer.MIN_VALUE/2 + 1;
int x = flag2 ? 6 : 8;
if (l * x <= 4) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_I, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testIntMinValueUnderflowTwice(boolean flag, boolean flag2) {
int l = flag ? Integer.MIN_VALUE/2 : Integer.MIN_VALUE/2 + 1;
int x = flag2 ? 8 : 10;
if (l * x <= 8) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_I, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testIntMaxValueOverflowOnce(boolean flag, boolean flag2) {
int l = flag2 ? Integer.MAX_VALUE/2 - 1 : Integer.MAX_VALUE/2;
int x = flag ? 4 : 6;
if (l * x >= -8) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_I, "1", IRNode.STORE_L, "1", IRNode.MUL_I, "1"})
public static void testIntMaxValueOverflowOnceTwice(boolean flag, boolean flag2) {
int l = flag2 ? Integer.MAX_VALUE/2 - 1 : Integer.MAX_VALUE/2;
int x = flag ? 6 : 8;
if (l * x >= -8) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = {IRNode.MUL_I, IRNode.STORE_L, IRNode.LSHIFT}, counts = {IRNode.STORE_I, "1"})
public static void testIntMaxValueOverflowTwice(boolean flag, boolean flag2) {
int l = flag2 ? Integer.MAX_VALUE/2 - 1 : Integer.MAX_VALUE/2;
int x = flag ? 8 : 10;
if (l * x >= -16L) {
iFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_I, counts = {IRNode.STORE_L, "1"})
public static void testIntProductsOverflowOnceAtMin(boolean flag, boolean flag2) {
int l = flag ? Integer.MAX_VALUE/2 + 1 : Integer.MAX_VALUE/2 + 2;
int x = flag2 ? 2 : 3;
// [MAX_VALUE/2 + 1, MAX_VALUE/2 + 2] * [2,3]: All cross products overflow exactly once.
// Result: [MIN_VALUE, MIN_VALUE/2 + 3] -> 2 outside range and If can be optimized away.
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_I, counts = {IRNode.STORE_L, "1"})
public static void testIntProductsOverflowOnceAtMax(boolean flag, boolean flag2) {
// 63786643 * 101 = Integer.MAX_VALUE*3 + 2 --cast to int--> Integer.MAX_VALUE
int l = flag ? 63786642 : 63786643;
int x = flag2 ? 100 : 101;
// All cross products overflow exactly once.
// Result: [y, MAX_VALUE], where y > 2 -> 2 outside range and If can be optimized away.
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_I, counts = {IRNode.STORE_L, "1"})
public static void testIntProductsUnderflowOnceAtMin(boolean flag, boolean flag2) {
int l = flag ? Integer.MIN_VALUE/3 - 1 : Integer.MIN_VALUE/3 - 2;
int x = flag2 ? 3 : 4;
// [MIN_VALUE/3 - 1, MIN_VALUE/3 - 2] * [3,4]: All cross products underflow exactly once.
// Result: [MAX_VALUE + MIN_VALUE/3 - 5, MAX_VALUE] -> 2 outside range and If can be optimized away.
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(failOn = IRNode.MUL_I, counts = {IRNode.STORE_L, "1"})
public static void testIntProductsUnderflowOnceAtMax(boolean flag, boolean flag2) {
// -1610612736 * 4 = Integer.MIN_VALUE*3 --cast to int--> Integer.MIN_VALUE
int l = flag ? -1610612736 : -1610612735;
int x = flag2 ? 3 : 4;
// All cross products underflow exactly once.
// Result: [MIN_VALUE, y], where y < 2 -> 2 outside range and If can be optimized away.
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntProductsDifferentNumberOfOverflow(boolean flag, boolean flag2) {
// 63786643 * 101 = Integer.MAX_VALUE*3 + 2 --cast to int--> Integer.MAX_VALUE // Overflown once
// 63786644 * 101 = (Integer.MAX_VALUE*3 + 101) + 2 --cast to int--> Integer.MIN_VALUE + 100 // Overflown twice
int l = flag ? 63786643 : 63786644;
int x = flag2 ? 100 : 101;
// Different number of overflows -> cannot optimize If away
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntProductsDifferentNumberOfUnderflows(boolean flag, boolean flag2) {
// -1610612736 * 4 = Integer.MIN_VALUE*3 --cast to int--> Integer.MIN_VALUE // Underflown once
// -1610612737 * 4 = (Integer.MIN_VALUE*3 - 4) --cast to int--> Integer.MAX_VALUE - 3 // Underflown twice
int l = flag ? -1610612736 : -1610612737;
int x = flag2 ? 3 : 4;
// Different number of underflows -> cannot optimize If away
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntNotSameOverflow1(boolean flag, boolean flag2) {
int l = flag ? 1 : Integer.MAX_VALUE;
int x = flag2 ? -1 : 2;
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntNotSameOverflow2(boolean flag, boolean flag2) {
int l = flag ? 1 : Integer.MIN_VALUE;
int x = flag2 ? -1 : 2;
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntNotSameOverflow3(boolean flag, boolean flag2) {
int l = flag ? -1 : Integer.MIN_VALUE;
int x = flag2 ? Integer.MIN_VALUE : -1;
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntNotSameOverflow4(boolean flag, boolean flag2) {
int l = flag ? -1 : Integer.MAX_VALUE;
int x = flag2 ? Integer.MAX_VALUE : -1;
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
@Test
@Warmup(0)
@Arguments({Argument.TRUE, Argument.FALSE})
@IR(counts = {IRNode.STORE_L, "2", IRNode.MUL_I, "1"})
public static void testIntNotSameOverflow5(boolean flag, boolean flag2) {
int l = flag ? Integer.MIN_VALUE : Integer.MAX_VALUE;
int x = flag2 ? Integer.MAX_VALUE : -1;
if (l * x != 2) {
lFld = 23;
} else {
lFld = 34;
}
}
// Just some sanity testing.
@Test
public static void test() {
iFld = 1073741823 * 2;
iFld2 = 1073741824 * 2; // overflow
iFld3 = -1073741824 * 2;
iFld4 = -1073741825 * 2; // underflow
lFld = 4611686018427387903L * 2;
lFld2 = 4611686018427387904L * 2; // overflow
lFld3 = -4611686018427387904L * 2;
lFld4 = -4611686018427387905L * 2; // underflow
}
@Run(test = "test")
public static void run() {
test();
Asserts.assertEQ(iFld, 2147483646);
Asserts.assertEQ(iFld2, -2147483648);
Asserts.assertEQ(iFld3, -2147483648);
Asserts.assertEQ(iFld4, 2147483646);
Asserts.assertEQ(lFld, 9223372036854775806L);
Asserts.assertEQ(lFld2, -9223372036854775808L);
Asserts.assertEQ(lFld3, -9223372036854775808L);
Asserts.assertEQ(lFld4, 9223372036854775806L);
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* 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.
*/
/*
* @test
* @bug 8299546
* @summary Tests that MulL::Value() does not return bottom type and then an optimized type again in CCP.
* @run main/othervm -Xcomp -XX:CompileCommand=compileonly,compiler.ccp.TestMissingMulLOptimization::*
* -XX:CompileCommand=dontinline,compiler.ccp.TestMissingMulLOptimization::*
* compiler.ccp.TestMissingMulLOptimization
*/
package compiler.ccp;
public class TestMissingMulLOptimization {
static int N;
static long x;
public static void main(String[] strArr) {
try {
test();
} catch (RuntimeException e) {
// Expected
}
}
static int test() {
int i6 = 2, i10 = 3, i11, iArr[] = new int[N];
long l = 3151638515L;
double dArr[] = new double[N];
dontInline();
int i;
for (i = 7; i < 221; i++) {
i6 *= i6;
}
for (int j = 9; 83 > j; ) {
for (i11 = 1; i11 < 6; ++i11) {
l *= i;
l += 3;
}
}
x += i6;
return 34;
}
static int dontInline() {
throw new RuntimeException();
}
}