@@ -952,7 +952,8 @@ LShiftNode* LShiftNode::make(Node* in1, Node* in2, BasicType bt) {
952
952
953
953
// =============================================================================
954
954
955
- static bool const_shift_count (PhaseGVN* phase, Node* shiftNode, int * count) {
955
+ // Returns whether the shift amount is constant. If so, sets count.
956
+ static bool const_shift_count (PhaseGVN* phase, const Node* shiftNode, int * count) {
956
957
const TypeInt* tcount = phase->type (shiftNode->in (2 ))->isa_int ();
957
958
if (tcount != nullptr && tcount->is_con ()) {
958
959
*count = tcount->get_con ();
@@ -961,53 +962,72 @@ static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) {
961
962
return false ;
962
963
}
963
964
964
- static int maskShiftAmount (PhaseGVN* phase, Node* shiftNode, uint nBits) {
965
- int count = 0 ;
966
- if (const_shift_count (phase, shiftNode, &count)) {
967
- int maskedShift = count & (nBits - 1 );
968
- if (maskedShift == 0 ) {
965
+ // Returns whether the shift amount is constant. If so, sets real_shift and masked_shift.
966
+ static bool mask_shift_amount (PhaseGVN* phase, const Node* shiftNode, uint nBits, int & real_shift, int & masked_shift) {
967
+ if (const_shift_count (phase, shiftNode, &real_shift)) {
968
+ masked_shift = real_shift & (nBits - 1 );
969
+ return true ;
970
+ }
971
+ return false ;
972
+ }
973
+
974
+ // Convenience for when we don't care about the real amount
975
+ static bool mask_shift_amount (PhaseGVN* phase, const Node* shiftNode, uint nBits, int & masked_shift) {
976
+ int real_shift;
977
+ return mask_shift_amount (phase, shiftNode, nBits, real_shift, masked_shift);
978
+ }
979
+
980
+ // Use this in ::Ideal only with shiftNode == this!
981
+ // Returns the masked shift amount if constant or 0 if not constant.
982
+ static int mask_and_replace_shift_amount (PhaseGVN* phase, Node* shiftNode, uint nBits) {
983
+ int real_shift;
984
+ int masked_shift;
985
+ if (mask_shift_amount (phase, shiftNode, nBits, real_shift, masked_shift)) {
986
+ if (masked_shift == 0 ) {
969
987
// Let Identity() handle 0 shift count.
970
988
return 0 ;
971
989
}
972
990
973
- if (count != maskedShift ) {
991
+ if (real_shift != masked_shift ) {
974
992
PhaseIterGVN* igvn = phase->is_IterGVN ();
975
- if (igvn) {
993
+ if (igvn != nullptr ) {
976
994
igvn->rehash_node_delayed (shiftNode);
977
995
}
978
- shiftNode->set_req (2 , phase->intcon (maskedShift )); // Replace shift count with masked value.
996
+ shiftNode->set_req (2 , phase->intcon (masked_shift )); // Replace shift count with masked value.
979
997
}
980
- return maskedShift ;
998
+ return masked_shift ;
981
999
}
1000
+ // Not a shift by a constant.
982
1001
return 0 ;
983
1002
}
984
1003
985
1004
// Called with
986
- // outer_shift = (_ << con0 )
1005
+ // outer_shift = (_ << rhs0 )
987
1006
// We are looking for the pattern:
988
- // outer_shift = ((X << con1) << con0)
989
- // we denote inner_shift the nested expression (X << con1)
990
- //
991
- // con0 and con1 are both in [0..nbits), as they are computed by maskShiftAmount.
1007
+ // outer_shift = ((X << rhs1) << rhs0)
1008
+ // where rhs0 and rhs1 are constant
1009
+ // we denote inner_shift the nested expression (X << rhs1)
1010
+ // con0 = rhs1 % nbits and con0 = rhs1 % nbits
1011
+ // where nbits is the number of bits of the shifts
992
1012
//
993
1013
// There are 2 cases:
994
1014
// if con0 + con1 >= nbits => 0
995
1015
// if con0 + con1 < nbits => X << (con1 + con0)
996
- static Node* collapse_nested_shift_left (PhaseGVN* phase, Node* outer_shift, int con0, BasicType bt) {
1016
+ static Node* collapse_nested_shift_left (PhaseGVN* phase, const Node* outer_shift, int con0, BasicType bt) {
997
1017
assert (bt == T_LONG || bt == T_INT, " Unexpected type" );
998
- Node* inner_shift = outer_shift->in (1 );
1018
+ const Node* inner_shift = outer_shift->in (1 );
999
1019
if (inner_shift->Opcode () != Op_LShift (bt)) {
1000
1020
return nullptr ;
1001
1021
}
1002
1022
1003
- if (!phase->is_IterGVN ()) {
1004
- phase->record_for_igvn (outer_shift);
1023
+ int nbits = static_cast <int >(bits_per_java_integer (bt));
1024
+ int con1;
1025
+ if (!mask_shift_amount (phase, inner_shift, nbits, con1)) {
1005
1026
return nullptr ;
1006
1027
}
1007
1028
1008
- int nbits = static_cast <int >(bits_per_java_integer (bt));
1009
- int con1 = maskShiftAmount (phase, inner_shift, nbits);
1010
- if (con1 == 0 ) { // Either non-const, or actually 0 (up to mask) and then delegated to Identity()
1029
+ if (con1 == 0 ) {
1030
+ // We let the Identity() of the inner shift do its job.
1011
1031
return nullptr ;
1012
1032
}
1013
1033
@@ -1041,7 +1061,7 @@ Node* LShiftINode::Identity(PhaseGVN* phase) {
1041
1061
// Also collapse nested left-shifts with constant rhs:
1042
1062
// (X << con1) << con2 ==> X << (con1 + con2)
1043
1063
Node *LShiftINode::Ideal (PhaseGVN *phase, bool can_reshape) {
1044
- int con = maskShiftAmount (phase, this , BitsPerJavaInteger);
1064
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1045
1065
if (con == 0 ) {
1046
1066
return nullptr ;
1047
1067
}
@@ -1227,7 +1247,7 @@ Node* LShiftLNode::Identity(PhaseGVN* phase) {
1227
1247
// Also collapse nested left-shifts with constant rhs:
1228
1248
// (X << con1) << con2 ==> X << (con1 + con2)
1229
1249
Node *LShiftLNode::Ideal (PhaseGVN *phase, bool can_reshape) {
1230
- int con = maskShiftAmount (phase, this , BitsPerJavaLong);
1250
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaLong);
1231
1251
if (con == 0 ) {
1232
1252
return nullptr ;
1233
1253
}
@@ -1448,7 +1468,7 @@ Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
1448
1468
if (t1 == nullptr ) {
1449
1469
return NodeSentinel; // Left input is an integer
1450
1470
}
1451
- int shift = maskShiftAmount (phase, this , bits_per_java_integer (bt));
1471
+ int shift = mask_and_replace_shift_amount (phase, this , bits_per_java_integer (bt));
1452
1472
if (shift == 0 ) {
1453
1473
return NodeSentinel;
1454
1474
}
@@ -1478,7 +1498,7 @@ Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1478
1498
if (progress != nullptr ) {
1479
1499
return progress;
1480
1500
}
1481
- int shift = maskShiftAmount (phase, this , BitsPerJavaInteger);
1501
+ int shift = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1482
1502
assert (shift != 0 , " handled by IdealIL" );
1483
1503
1484
1504
// Check for "(short[i] <<16)>>16" which simply sign-extends
@@ -1665,7 +1685,7 @@ Node* URShiftINode::Identity(PhaseGVN* phase) {
1665
1685
1666
1686
// ------------------------------Ideal------------------------------------------
1667
1687
Node *URShiftINode::Ideal (PhaseGVN *phase, bool can_reshape) {
1668
- int con = maskShiftAmount (phase, this , BitsPerJavaInteger);
1688
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1669
1689
if (con == 0 ) {
1670
1690
return nullptr ;
1671
1691
}
@@ -1829,7 +1849,7 @@ Node* URShiftLNode::Identity(PhaseGVN* phase) {
1829
1849
1830
1850
// ------------------------------Ideal------------------------------------------
1831
1851
Node *URShiftLNode::Ideal (PhaseGVN *phase, bool can_reshape) {
1832
- int con = maskShiftAmount (phase, this , BitsPerJavaLong);
1852
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaLong);
1833
1853
if (con == 0 ) {
1834
1854
return nullptr ;
1835
1855
}
0 commit comments