@@ -950,63 +950,86 @@ LShiftNode* LShiftNode::make(Node* in1, Node* in2, BasicType bt) {
950
950
return nullptr ;
951
951
}
952
952
953
- // =============================================================================
954
-
955
- static bool const_shift_count (PhaseGVN* phase, Node* shiftNode, int * count) {
956
- const TypeInt* tcount = phase->type (shiftNode->in (2 ))->isa_int ();
953
+ // Returns whether the shift amount is constant. If so, sets count.
954
+ static bool const_shift_count (PhaseGVN* phase, const Node* shift_node, int * count) {
955
+ const TypeInt* tcount = phase->type (shift_node->in (2 ))->isa_int ();
957
956
if (tcount != nullptr && tcount->is_con ()) {
958
957
*count = tcount->get_con ();
959
958
return true ;
960
959
}
961
960
return false ;
962
961
}
963
962
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 ) {
963
+ // Returns whether the shift amount is constant. If so, sets real_shift and masked_shift.
964
+ static bool mask_shift_amount (PhaseGVN* phase, const Node* shift_node, uint nBits, int & real_shift, int & masked_shift) {
965
+ if (const_shift_count (phase, shift_node, &real_shift)) {
966
+ masked_shift = real_shift & (nBits - 1 );
967
+ return true ;
968
+ }
969
+ return false ;
970
+ }
971
+
972
+ // Convenience for when we don't care about the real amount
973
+ static bool mask_shift_amount (PhaseGVN* phase, const Node* shift_node, uint nBits, int & masked_shift) {
974
+ int real_shift;
975
+ return mask_shift_amount (phase, shift_node, nBits, real_shift, masked_shift);
976
+ }
977
+
978
+ // Use this in ::Ideal only with shiftNode == this!
979
+ // Returns the masked shift amount if constant or 0 if not constant.
980
+ static int mask_and_replace_shift_amount (PhaseGVN* phase, Node* shift_node, uint nBits) {
981
+ int real_shift;
982
+ int masked_shift;
983
+ if (mask_shift_amount (phase, shift_node, nBits, real_shift, masked_shift)) {
984
+ if (masked_shift == 0 ) {
969
985
// Let Identity() handle 0 shift count.
970
986
return 0 ;
971
987
}
972
988
973
- if (count != maskedShift) {
974
- shiftNode->set_req (2 , phase->intcon (maskedShift)); // Replace shift count with masked value.
989
+ if (real_shift != masked_shift) {
975
990
PhaseIterGVN* igvn = phase->is_IterGVN ();
976
- if (igvn) {
977
- igvn->rehash_node_delayed (shiftNode );
991
+ if (igvn != nullptr ) {
992
+ igvn->_worklist . push (shift_node );
978
993
}
994
+ shift_node->set_req (2 , phase->intcon (masked_shift)); // Replace shift count with masked value.
979
995
}
980
- return maskedShift ;
996
+ return masked_shift ;
981
997
}
998
+ // Not a shift by a constant.
982
999
return 0 ;
983
1000
}
984
1001
985
1002
// Called with
986
- // outer_shift = (_ << con0 )
1003
+ // outer_shift = (_ << rhs_outer )
987
1004
// 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.
1005
+ // outer_shift = ((X << rhs_inner) << rhs_outer)
1006
+ // where rhs_outer and rhs_inner are constant
1007
+ // we denote inner_shift the nested expression (X << rhs_inner)
1008
+ // con_inner = rhs_inner % nbits and con_outer = rhs_outer % nbits
1009
+ // where nbits is the number of bits of the shifts
992
1010
//
993
1011
// There are 2 cases:
994
- // if con0 + con1 >= nbits => 0
995
- // if con0 + con1 < nbits => X << (con1 + con0 )
996
- static Node* collapse_nested_shift_left (PhaseGVN* phase, Node* outer_shift, int con0 , BasicType bt) {
1012
+ // if con_outer + con_inner >= nbits => 0
1013
+ // if con_outer + con_inner < nbits => X << (con_outer + con_inner )
1014
+ static Node* collapse_nested_shift_left (PhaseGVN* phase, const Node* outer_shift, int con_outer , BasicType bt) {
997
1015
assert (bt == T_LONG || bt == T_INT, " Unexpected type" );
998
- int nbits = static_cast <int >(bits_per_java_integer (bt));
999
- Node* inner_shift = outer_shift->in (1 );
1016
+ const Node* inner_shift = outer_shift->in (1 );
1000
1017
if (inner_shift->Opcode () != Op_LShift (bt)) {
1001
1018
return nullptr ;
1002
1019
}
1003
1020
1004
- int con1 = maskShiftAmount (phase, inner_shift, nbits);
1005
- if (con1 == 0 ) { // Either non-const, or actually 0 (up to mask) and then delegated to Identity()
1021
+ int nbits = static_cast <int >(bits_per_java_integer (bt));
1022
+ int con_inner;
1023
+ if (!mask_shift_amount (phase, inner_shift, nbits, con_inner)) {
1024
+ return nullptr ;
1025
+ }
1026
+
1027
+ if (con_inner == 0 ) {
1028
+ // We let the Identity() of the inner shift do its job.
1006
1029
return nullptr ;
1007
1030
}
1008
1031
1009
- if (con0 + con1 >= nbits) {
1032
+ if (con_outer + con_inner >= nbits) {
1010
1033
// While it might be tempting to use
1011
1034
// phase->zerocon(bt);
1012
1035
// it would be incorrect: zerocon caches nodes, while Ideal is only allowed
@@ -1015,7 +1038,7 @@ static Node* collapse_nested_shift_left(PhaseGVN* phase, Node* outer_shift, int
1015
1038
}
1016
1039
1017
1040
// con0 + con1 < nbits ==> actual shift happens now
1018
- Node* con0_plus_con1 = phase->intcon (con0 + con1 );
1041
+ Node* con0_plus_con1 = phase->intcon (con_outer + con_inner );
1019
1042
return LShiftNode::make (inner_shift->in (1 ), con0_plus_con1, bt);
1020
1043
}
1021
1044
@@ -1036,7 +1059,7 @@ Node* LShiftINode::Identity(PhaseGVN* phase) {
1036
1059
// Also collapse nested left-shifts with constant rhs:
1037
1060
// (X << con1) << con2 ==> X << (con1 + con2)
1038
1061
Node *LShiftINode::Ideal (PhaseGVN *phase, bool can_reshape) {
1039
- int con = maskShiftAmount (phase, this , BitsPerJavaInteger);
1062
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1040
1063
if (con == 0 ) {
1041
1064
return nullptr ;
1042
1065
}
@@ -1222,7 +1245,7 @@ Node* LShiftLNode::Identity(PhaseGVN* phase) {
1222
1245
// Also collapse nested left-shifts with constant rhs:
1223
1246
// (X << con1) << con2 ==> X << (con1 + con2)
1224
1247
Node *LShiftLNode::Ideal (PhaseGVN *phase, bool can_reshape) {
1225
- int con = maskShiftAmount (phase, this , BitsPerJavaLong);
1248
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaLong);
1226
1249
if (con == 0 ) {
1227
1250
return nullptr ;
1228
1251
}
@@ -1443,7 +1466,7 @@ Node* RShiftNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
1443
1466
if (t1 == nullptr ) {
1444
1467
return NodeSentinel; // Left input is an integer
1445
1468
}
1446
- int shift = maskShiftAmount (phase, this , bits_per_java_integer (bt));
1469
+ int shift = mask_and_replace_shift_amount (phase, this , bits_per_java_integer (bt));
1447
1470
if (shift == 0 ) {
1448
1471
return NodeSentinel;
1449
1472
}
@@ -1473,7 +1496,7 @@ Node* RShiftINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1473
1496
if (progress != nullptr ) {
1474
1497
return progress;
1475
1498
}
1476
- int shift = maskShiftAmount (phase, this , BitsPerJavaInteger);
1499
+ int shift = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1477
1500
assert (shift != 0 , " handled by IdealIL" );
1478
1501
1479
1502
// Check for "(short[i] <<16)>>16" which simply sign-extends
@@ -1660,7 +1683,7 @@ Node* URShiftINode::Identity(PhaseGVN* phase) {
1660
1683
1661
1684
// ------------------------------Ideal------------------------------------------
1662
1685
Node *URShiftINode::Ideal (PhaseGVN *phase, bool can_reshape) {
1663
- int con = maskShiftAmount (phase, this , BitsPerJavaInteger);
1686
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaInteger);
1664
1687
if (con == 0 ) {
1665
1688
return nullptr ;
1666
1689
}
@@ -1824,7 +1847,7 @@ Node* URShiftLNode::Identity(PhaseGVN* phase) {
1824
1847
1825
1848
// ------------------------------Ideal------------------------------------------
1826
1849
Node *URShiftLNode::Ideal (PhaseGVN *phase, bool can_reshape) {
1827
- int con = maskShiftAmount (phase, this , BitsPerJavaLong);
1850
+ int con = mask_and_replace_shift_amount (phase, this , BitsPerJavaLong);
1828
1851
if (con == 0 ) {
1829
1852
return nullptr ;
1830
1853
}
0 commit comments