Skip to content

Commit 837cf85

Browse files
author
Quan Anh Mai
committed
8312547: Max/Min nodes Value implementation could be improved
Reviewed-by: thartmann, kvn
1 parent 7342f5a commit 837cf85

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

src/hotspot/share/opto/addnode.cpp

+40-27
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,19 @@ Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
216216
// the other input's symbols.
217217
const Type* AddNode::Value(PhaseGVN* phase) const {
218218
// Either input is TOP ==> the result is TOP
219-
const Type *t1 = phase->type( in(1) );
220-
const Type *t2 = phase->type( in(2) );
221-
if( t1 == Type::TOP ) return Type::TOP;
222-
if( t2 == Type::TOP ) return Type::TOP;
223-
224-
// Either input is BOTTOM ==> the result is the local BOTTOM
225-
const Type *bot = bottom_type();
226-
if( (t1 == bot) || (t2 == bot) ||
227-
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
228-
return bot;
219+
const Type* t1 = phase->type(in(1));
220+
const Type* t2 = phase->type(in(2));
221+
if (t1 == Type::TOP || t2 == Type::TOP) {
222+
return Type::TOP;
223+
}
229224

230225
// Check for an addition involving the additive identity
231-
const Type *tadd = add_of_identity( t1, t2 );
232-
if( tadd ) return tadd;
226+
const Type* tadd = add_of_identity(t1, t2);
227+
if (tadd != nullptr) {
228+
return tadd;
229+
}
233230

234-
return add_ring(t1,t2); // Local flavor of type addition
231+
return add_ring(t1, t2); // Local flavor of type addition
235232
}
236233

237234
//------------------------------add_identity-----------------------------------
@@ -513,7 +510,9 @@ const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
513510
// This also type-checks the inputs for sanity. Guaranteed never to
514511
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
515512
const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
516-
// We must be adding 2 float constants.
513+
if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
514+
return bottom_type();
515+
}
517516
return TypeF::make( t0->getf() + t1->getf() );
518517
}
519518

@@ -544,7 +543,9 @@ const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
544543
// This also type-checks the inputs for sanity. Guaranteed never to
545544
// be passed a TOP or BOTTOM type, these are filtered out by pre-check.
546545
const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
547-
// We must be adding 2 double constants.
546+
if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
547+
return bottom_type();
548+
}
548549
return TypeD::make( t0->getd() + t1->getd() );
549550
}
550551

@@ -1367,9 +1368,12 @@ Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
13671368
}
13681369

13691370
//------------------------------add_ring---------------------------------------
1370-
const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1371-
const TypeF *r0 = t0->is_float_constant();
1372-
const TypeF *r1 = t1->is_float_constant();
1371+
const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1372+
const TypeF* r0 = t0->isa_float_constant();
1373+
const TypeF* r1 = t1->isa_float_constant();
1374+
if (r0 == nullptr || r1 == nullptr) {
1375+
return bottom_type();
1376+
}
13731377

13741378
if (r0->is_nan()) {
13751379
return r0;
@@ -1389,9 +1393,12 @@ const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
13891393
}
13901394

13911395
//------------------------------add_ring---------------------------------------
1392-
const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1393-
const TypeD *r0 = t0->is_double_constant();
1394-
const TypeD *r1 = t1->is_double_constant();
1396+
const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1397+
const TypeD* r0 = t0->isa_double_constant();
1398+
const TypeD* r1 = t1->isa_double_constant();
1399+
if (r0 == nullptr || r1 == nullptr) {
1400+
return bottom_type();
1401+
}
13951402

13961403
if (r0->is_nan()) {
13971404
return r0;
@@ -1411,9 +1418,12 @@ const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
14111418
}
14121419

14131420
//------------------------------add_ring---------------------------------------
1414-
const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1415-
const TypeF *r0 = t0->is_float_constant();
1416-
const TypeF *r1 = t1->is_float_constant();
1421+
const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1422+
const TypeF* r0 = t0->isa_float_constant();
1423+
const TypeF* r1 = t1->isa_float_constant();
1424+
if (r0 == nullptr || r1 == nullptr) {
1425+
return bottom_type();
1426+
}
14171427

14181428
if (r0->is_nan()) {
14191429
return r0;
@@ -1433,9 +1443,12 @@ const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
14331443
}
14341444

14351445
//------------------------------add_ring---------------------------------------
1436-
const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1437-
const TypeD *r0 = t0->is_double_constant();
1438-
const TypeD *r1 = t1->is_double_constant();
1446+
const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1447+
const TypeD* r0 = t0->isa_double_constant();
1448+
const TypeD* r1 = t1->isa_double_constant();
1449+
if (r0 == nullptr || r1 == nullptr) {
1450+
return bottom_type();
1451+
}
14391452

14401453
if (r0->is_nan()) {
14411454
return r0;

test/hotspot/jtreg/compiler/c2/irTests/MaxMinINodeIdealizationTests.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/*
3030
* @test
31-
* @bug 8290248
31+
* @bug 8290248 8312547
3232
* @summary Test that Ideal transformations of MaxINode and MinINode are
3333
* being performed as expected.
3434
* @library /test/lib /
@@ -45,9 +45,11 @@ public static void main(String[] args) {
4545
"testMax2L", "testMax2R",
4646
"testMax2LNoLeftAdd",
4747
"testMax3",
48+
"testMax4",
4849
"testMin1",
4950
"testMin2",
50-
"testMin3"})
51+
"testMin3",
52+
"testMin4"})
5153
public void runPositiveTests() {
5254
int a = RunInfo.getRandom().nextInt();
5355
int min = Integer.MIN_VALUE;
@@ -73,10 +75,12 @@ public void assertPositiveResult(int a) {
7375
Asserts.assertEQ(testMax2L(a) , testMax2R(a));
7476
Asserts.assertEQ(Math.max(a >> 1, ((a >> 1) + 11)) , testMax2LNoLeftAdd(a));
7577
Asserts.assertEQ(Math.max(a, a) , testMax3(a));
78+
Asserts.assertEQ(0 , testMax4(a));
7679

7780
Asserts.assertEQ(Math.min(((a >> 1) + 100), Math.min(((a >> 1) + 150), 200)), testMin1(a));
7881
Asserts.assertEQ(Math.min(((a >> 1) + 10), ((a >> 1) + 11)) , testMin2(a));
7982
Asserts.assertEQ(Math.min(a, a) , testMin3(a));
83+
Asserts.assertEQ(0 , testMin4(a));
8084
}
8185

8286
// The transformations in test*1 and test*2 can happen only if the compiler has enough information
@@ -203,6 +207,18 @@ public int testMin3(int i) {
203207
return Math.min(i, i);
204208
}
205209

210+
@Test
211+
@IR(failOn = {IRNode.MAX_I})
212+
public int testMax4(int i) {
213+
return Math.max(i, 0) < 0 ? 1 : 0;
214+
}
215+
216+
@Test
217+
@IR(failOn = {IRNode.MIN_I})
218+
public int testMin4(int i) {
219+
return Math.min(i, 0) > 0 ? 1 : 0;
220+
}
221+
206222
@Run(test = {"testTwoLevelsDifferentXY",
207223
"testTwoLevelsNoLeftConstant",
208224
"testTwoLevelsNoRightConstant",

0 commit comments

Comments
 (0)