Skip to content

Commit 79bffe2

Browse files
committed
8349361: C2: RShiftL should support all applicable transformations that RShiftI does
Reviewed-by: epeter, chagedorn, jkarthikeyan
1 parent eef6aef commit 79bffe2

File tree

8 files changed

+315
-135
lines changed

8 files changed

+315
-135
lines changed

src/hotspot/share/opto/mulnode.cpp

Lines changed: 141 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ MulNode* MulNode::make(Node* in1, Node* in2, BasicType bt) {
225225
return nullptr;
226226
}
227227

228+
MulNode* MulNode::make_and(Node* in1, Node* in2, BasicType bt) {
229+
switch (bt) {
230+
case T_INT:
231+
return new AndINode(in1, in2);
232+
case T_LONG:
233+
return new AndLNode(in1, in2);
234+
default:
235+
fatal("Not implemented for %s", type2name(bt));
236+
}
237+
return nullptr;
238+
}
239+
228240

229241
//=============================================================================
230242
//------------------------------Ideal------------------------------------------
@@ -949,7 +961,7 @@ static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) {
949961
return false;
950962
}
951963

952-
static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, int nBits) {
964+
static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, uint nBits) {
953965
int count = 0;
954966
if (const_shift_count(phase, shiftNode, &count)) {
955967
int maskedShift = count & (nBits - 1);
@@ -1378,69 +1390,105 @@ const Type* LShiftLNode::Value(PhaseGVN* phase) const {
13781390
return TypeLong::make( (jlong)r1->get_con() << (jint)shift );
13791391
}
13801392

1393+
RShiftNode* RShiftNode::make(Node* in1, Node* in2, BasicType bt) {
1394+
switch (bt) {
1395+
case T_INT:
1396+
return new RShiftINode(in1, in2);
1397+
case T_LONG:
1398+
return new RShiftLNode(in1, in2);
1399+
default:
1400+
fatal("Not implemented for %s", type2name(bt));
1401+
}
1402+
return nullptr;
1403+
}
1404+
1405+
13811406
//=============================================================================
13821407
//------------------------------Identity---------------------------------------
1383-
Node* RShiftINode::Identity(PhaseGVN* phase) {
1408+
Node* RShiftNode::IdentityIL(PhaseGVN* phase, BasicType bt) {
13841409
int count = 0;
13851410
if (const_shift_count(phase, this, &count)) {
1386-
if ((count & (BitsPerJavaInteger - 1)) == 0) {
1387-
// Shift by a multiple of 32 does nothing
1411+
if ((count & (bits_per_java_integer(bt) - 1)) == 0) {
1412+
// Shift by a multiple of 32/64 does nothing
13881413
return in(1);
13891414
}
13901415
// Check for useless sign-masking
1391-
if (in(1)->Opcode() == Op_LShiftI &&
1416+
if (in(1)->Opcode() == Op_LShift(bt) &&
13921417
in(1)->req() == 3 &&
13931418
in(1)->in(2) == in(2)) {
1394-
count &= BitsPerJavaInteger-1; // semantics of Java shifts
1419+
count &= bits_per_java_integer(bt) - 1; // semantics of Java shifts
13951420
// Compute masks for which this shifting doesn't change
1396-
int lo = (-1 << (BitsPerJavaInteger - ((uint)count)-1)); // FFFF8000
1397-
int hi = ~lo; // 00007FFF
1398-
const TypeInt* t11 = phase->type(in(1)->in(1))->isa_int();
1421+
jlong lo = (CONST64(-1) << (bits_per_java_integer(bt) - ((uint)count)-1)); // FFFF8000
1422+
jlong hi = ~lo; // 00007FFF
1423+
const TypeInteger* t11 = phase->type(in(1)->in(1))->isa_integer(bt);
13991424
if (t11 == nullptr) {
14001425
return this;
14011426
}
14021427
// Does actual value fit inside of mask?
1403-
if (lo <= t11->_lo && t11->_hi <= hi) {
1428+
if (lo <= t11->lo_as_long() && t11->hi_as_long() <= hi) {
14041429
return in(1)->in(1); // Then shifting is a nop
14051430
}
14061431
}
14071432
}
14081433
return this;
14091434
}
14101435

1411-
//------------------------------Ideal------------------------------------------
1412-
Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1436+
Node* RShiftINode::Identity(PhaseGVN* phase) {
1437+
return IdentityIL(phase, T_INT);
1438+
}
1439+
1440+
Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
14131441
// Inputs may be TOP if they are dead.
1414-
const TypeInt *t1 = phase->type(in(1))->isa_int();
1415-
if (!t1) return nullptr; // Left input is an integer
1416-
const TypeInt *t3; // type of in(1).in(2)
1417-
int shift = maskShiftAmount(phase, this, BitsPerJavaInteger);
1442+
const TypeInteger* t1 = phase->type(in(1))->isa_integer(bt);
1443+
if (t1 == nullptr) {
1444+
return NodeSentinel; // Left input is an integer
1445+
}
1446+
int shift = maskShiftAmount(phase, this, bits_per_java_integer(bt));
14181447
if (shift == 0) {
1419-
return nullptr;
1448+
return NodeSentinel;
14201449
}
14211450

14221451
// Check for (x & 0xFF000000) >> 24, whose mask can be made smaller.
1452+
// and convert to (x >> 24) & (0xFF000000 >> 24) = x >> 24
14231453
// Such expressions arise normally from shift chains like (byte)(x >> 24).
1424-
const Node *mask = in(1);
1425-
if( mask->Opcode() == Op_AndI &&
1426-
(t3 = phase->type(mask->in(2))->isa_int()) &&
1427-
t3->is_con() ) {
1428-
Node *x = mask->in(1);
1429-
jint maskbits = t3->get_con();
1454+
const Node* and_node = in(1);
1455+
if (and_node->Opcode() != Op_And(bt)) {
1456+
return nullptr;
1457+
}
1458+
const TypeInteger* mask_t = phase->type(and_node->in(2))->isa_integer(bt);
1459+
if (mask_t != nullptr && mask_t->is_con()) {
1460+
jlong maskbits = mask_t->get_con_as_long(bt);
14301461
// Convert to "(x >> shift) & (mask >> shift)"
1431-
Node *shr_nomask = phase->transform( new RShiftINode(mask->in(1), in(2)) );
1432-
return new AndINode(shr_nomask, phase->intcon( maskbits >> shift));
1462+
Node* shr_nomask = phase->transform(RShiftNode::make(and_node->in(1), in(2), bt));
1463+
return MulNode::make_and(shr_nomask, phase->integercon(maskbits >> shift, bt), bt);
1464+
}
1465+
return nullptr;
1466+
}
1467+
1468+
Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1469+
Node* progress = IdealIL(phase, can_reshape, T_INT);
1470+
if (progress == NodeSentinel) {
1471+
return nullptr;
1472+
}
1473+
if (progress != nullptr) {
1474+
return progress;
14331475
}
1476+
int shift = maskShiftAmount(phase, this, BitsPerJavaInteger);
1477+
assert(shift != 0, "handled by IdealIL");
14341478

14351479
// Check for "(short[i] <<16)>>16" which simply sign-extends
14361480
const Node *shl = in(1);
1437-
if( shl->Opcode() != Op_LShiftI ) return nullptr;
1481+
if (shl->Opcode() != Op_LShiftI) {
1482+
return nullptr;
1483+
}
14381484

1439-
if( shift == 16 &&
1440-
(t3 = phase->type(shl->in(2))->isa_int()) &&
1441-
t3->is_con(16) ) {
1485+
const TypeInt* left_shift_t = phase->type(shl->in(2))->isa_int();
1486+
if (left_shift_t == nullptr) {
1487+
return nullptr;
1488+
}
1489+
if (shift == 16 && left_shift_t->is_con(16)) {
14421490
Node *ld = shl->in(1);
1443-
if( ld->Opcode() == Op_LoadS ) {
1491+
if (ld->Opcode() == Op_LoadS) {
14441492
// Sign extension is just useless here. Return a RShiftI of zero instead
14451493
// returning 'ld' directly. We cannot return an old Node directly as
14461494
// that is the job of 'Identity' calls and Identity calls only work on
@@ -1458,9 +1506,7 @@ Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
14581506
}
14591507

14601508
// Check for "(byte[i] <<24)>>24" which simply sign-extends
1461-
if( shift == 24 &&
1462-
(t3 = phase->type(shl->in(2))->isa_int()) &&
1463-
t3->is_con(24) ) {
1509+
if (shift == 24 && left_shift_t->is_con(24)) {
14641510
Node *ld = shl->in(1);
14651511
if (ld->Opcode() == Op_LoadB) {
14661512
// Sign extension is just useless here
@@ -1473,136 +1519,109 @@ Node *RShiftINode::Ideal(PhaseGVN *phase, bool can_reshape) {
14731519
return nullptr;
14741520
}
14751521

1476-
//------------------------------Value------------------------------------------
1477-
// A RShiftINode shifts its input2 right by input1 amount.
1478-
const Type* RShiftINode::Value(PhaseGVN* phase) const {
1479-
const Type *t1 = phase->type( in(1) );
1480-
const Type *t2 = phase->type( in(2) );
1522+
const Type* RShiftNode::ValueIL(PhaseGVN* phase, BasicType bt) const {
1523+
const Type* t1 = phase->type(in(1));
1524+
const Type* t2 = phase->type(in(2));
14811525
// Either input is TOP ==> the result is TOP
1482-
if( t1 == Type::TOP ) return Type::TOP;
1483-
if( t2 == Type::TOP ) return Type::TOP;
1526+
if (t1 == Type::TOP) {
1527+
return Type::TOP;
1528+
}
1529+
if (t2 == Type::TOP) {
1530+
return Type::TOP;
1531+
}
14841532

14851533
// Left input is ZERO ==> the result is ZERO.
1486-
if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
1534+
if (t1 == TypeInteger::zero(bt)) {
1535+
return TypeInteger::zero(bt);
1536+
}
14871537
// Shift by zero does nothing
1488-
if( t2 == TypeInt::ZERO ) return t1;
1538+
if (t2 == TypeInt::ZERO) {
1539+
return t1;
1540+
}
14891541

14901542
// Either input is BOTTOM ==> the result is BOTTOM
1491-
if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1492-
return TypeInt::INT;
1543+
if (t1 == Type::BOTTOM || t2 == Type::BOTTOM) {
1544+
return TypeInteger::bottom(bt);
1545+
}
14931546

1494-
const TypeInt *r1 = t1->is_int(); // Handy access
1495-
const TypeInt *r2 = t2->is_int(); // Handy access
1547+
const TypeInteger* r1 = t1->isa_integer(bt);
1548+
const TypeInt* r2 = t2->isa_int();
14961549

14971550
// If the shift is a constant, just shift the bounds of the type.
1498-
// For example, if the shift is 31, we just propagate sign bits.
1551+
// For example, if the shift is 31/63, we just propagate sign bits.
14991552
if (!r1->is_con() && r2->is_con()) {
15001553
uint shift = r2->get_con();
1501-
shift &= BitsPerJavaInteger-1; // semantics of Java shifts
1502-
// Shift by a multiple of 32 does nothing:
1503-
if (shift == 0) return t1;
1554+
shift &= bits_per_java_integer(bt) - 1; // semantics of Java shifts
1555+
// Shift by a multiple of 32/64 does nothing:
1556+
if (shift == 0) {
1557+
return t1;
1558+
}
15041559
// Calculate reasonably aggressive bounds for the result.
15051560
// This is necessary if we are to correctly type things
15061561
// like (x<<24>>24) == ((byte)x).
1507-
jint lo = (jint)r1->_lo >> (jint)shift;
1508-
jint hi = (jint)r1->_hi >> (jint)shift;
1562+
jlong lo = r1->lo_as_long() >> (jint)shift;
1563+
jlong hi = r1->hi_as_long() >> (jint)shift;
15091564
assert(lo <= hi, "must have valid bounds");
1510-
const TypeInt* ti = TypeInt::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1565+
#ifdef ASSERT
1566+
if (bt == T_INT) {
1567+
jint lo_verify = checked_cast<jint>(r1->lo_as_long()) >> (jint)shift;
1568+
jint hi_verify = checked_cast<jint>(r1->hi_as_long()) >> (jint)shift;
1569+
assert((checked_cast<jint>(lo) == lo_verify) && (checked_cast<jint>(hi) == hi_verify), "inconsistent");
1570+
}
1571+
#endif
1572+
const TypeInteger* ti = TypeInteger::make(lo, hi, MAX2(r1->_widen,r2->_widen), bt);
15111573
#ifdef ASSERT
15121574
// Make sure we get the sign-capture idiom correct.
1513-
if (shift == BitsPerJavaInteger-1) {
1514-
if (r1->_lo >= 0) assert(ti == TypeInt::ZERO, ">>31 of + is 0");
1515-
if (r1->_hi < 0) assert(ti == TypeInt::MINUS_1, ">>31 of - is -1");
1575+
if (shift == bits_per_java_integer(bt) - 1) {
1576+
if (r1->lo_as_long() >= 0) {
1577+
assert(ti == TypeInteger::zero(bt), ">>31/63 of + is 0");
1578+
}
1579+
if (r1->hi_as_long() < 0) {
1580+
assert(ti == TypeInteger::minus_1(bt), ">>31/63 of - is -1");
1581+
}
15161582
}
15171583
#endif
15181584
return ti;
15191585
}
15201586

15211587
if (!r1->is_con() || !r2->is_con()) {
15221588
// If the left input is non-negative the result must also be non-negative, regardless of what the right input is.
1523-
if (r1->_lo >= 0) {
1524-
return TypeInt::make(0, r1->_hi, MAX2(r1->_widen, r2->_widen));
1589+
if (r1->lo_as_long() >= 0) {
1590+
return TypeInteger::make(0, r1->hi_as_long(), MAX2(r1->_widen, r2->_widen), bt);
15251591
}
15261592

15271593
// Conversely, if the left input is negative then the result must be negative.
1528-
if (r1->_hi <= -1) {
1529-
return TypeInt::make(r1->_lo, -1, MAX2(r1->_widen, r2->_widen));
1594+
if (r1->hi_as_long() <= -1) {
1595+
return TypeInteger::make(r1->lo_as_long(), -1, MAX2(r1->_widen, r2->_widen), bt);
15301596
}
15311597

1532-
return TypeInt::INT;
1598+
return TypeInteger::bottom(bt);
15331599
}
15341600

15351601
// Signed shift right
1536-
return TypeInt::make(r1->get_con() >> (r2->get_con() & 31));
1602+
return TypeInteger::make(r1->get_con_as_long(bt) >> (r2->get_con() & (bits_per_java_integer(bt) - 1)), bt);
1603+
}
1604+
1605+
const Type* RShiftINode::Value(PhaseGVN* phase) const {
1606+
return ValueIL(phase, T_INT);
15371607
}
15381608

15391609
//=============================================================================
15401610
//------------------------------Identity---------------------------------------
15411611
Node* RShiftLNode::Identity(PhaseGVN* phase) {
1542-
const TypeInt *ti = phase->type(in(2))->isa_int(); // Shift count is an int.
1543-
return (ti && ti->is_con() && (ti->get_con() & (BitsPerJavaLong - 1)) == 0) ? in(1) : this;
1612+
return IdentityIL(phase, T_LONG);
15441613
}
15451614

1546-
//------------------------------Value------------------------------------------
1547-
// A RShiftLNode shifts its input2 right by input1 amount.
1548-
const Type* RShiftLNode::Value(PhaseGVN* phase) const {
1549-
const Type *t1 = phase->type( in(1) );
1550-
const Type *t2 = phase->type( in(2) );
1551-
// Either input is TOP ==> the result is TOP
1552-
if( t1 == Type::TOP ) return Type::TOP;
1553-
if( t2 == Type::TOP ) return Type::TOP;
1554-
1555-
// Left input is ZERO ==> the result is ZERO.
1556-
if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1557-
// Shift by zero does nothing
1558-
if( t2 == TypeInt::ZERO ) return t1;
1559-
1560-
// Either input is BOTTOM ==> the result is BOTTOM
1561-
if (t1 == Type::BOTTOM || t2 == Type::BOTTOM)
1562-
return TypeLong::LONG;
1563-
1564-
const TypeLong *r1 = t1->is_long(); // Handy access
1565-
const TypeInt *r2 = t2->is_int (); // Handy access
1566-
1567-
// If the shift is a constant, just shift the bounds of the type.
1568-
// For example, if the shift is 63, we just propagate sign bits.
1569-
if (!r1->is_con() && r2->is_con()) {
1570-
uint shift = r2->get_con();
1571-
shift &= (2*BitsPerJavaInteger)-1; // semantics of Java shifts
1572-
// Shift by a multiple of 64 does nothing:
1573-
if (shift == 0) return t1;
1574-
// Calculate reasonably aggressive bounds for the result.
1575-
// This is necessary if we are to correctly type things
1576-
// like (x<<24>>24) == ((byte)x).
1577-
jlong lo = (jlong)r1->_lo >> (jlong)shift;
1578-
jlong hi = (jlong)r1->_hi >> (jlong)shift;
1579-
assert(lo <= hi, "must have valid bounds");
1580-
const TypeLong* tl = TypeLong::make(lo, hi, MAX2(r1->_widen,r2->_widen));
1581-
#ifdef ASSERT
1582-
// Make sure we get the sign-capture idiom correct.
1583-
if (shift == (2*BitsPerJavaInteger)-1) {
1584-
if (r1->_lo >= 0) assert(tl == TypeLong::ZERO, ">>63 of + is 0");
1585-
if (r1->_hi < 0) assert(tl == TypeLong::MINUS_1, ">>63 of - is -1");
1586-
}
1587-
#endif
1588-
return tl;
1589-
}
1590-
1591-
if (!r1->is_con() || !r2->is_con()) {
1592-
// If the left input is non-negative the result must also be non-negative, regardless of what the right input is.
1593-
if (r1->_lo >= 0) {
1594-
return TypeLong::make(0, r1->_hi, MAX2(r1->_widen, r2->_widen));
1595-
}
1596-
1597-
// Conversely, if the left input is negative then the result must be negative.
1598-
if (r1->_hi <= -1) {
1599-
return TypeLong::make(r1->_lo, -1, MAX2(r1->_widen, r2->_widen));
1600-
}
1601-
1602-
return TypeLong::LONG;
1615+
Node* RShiftLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1616+
Node* progress = IdealIL(phase, can_reshape, T_LONG);
1617+
if (progress == NodeSentinel) {
1618+
return nullptr;
16031619
}
1620+
return progress;
1621+
}
16041622

1605-
return TypeLong::make(r1->get_con() >> (r2->get_con() & 63));
1623+
const Type* RShiftLNode::Value(PhaseGVN* phase) const {
1624+
return ValueIL(phase, T_LONG);
16061625
}
16071626

16081627
//=============================================================================

0 commit comments

Comments
 (0)