Skip to content

Commit 1c4c99e

Browse files
committed
8256823: C2 compilation fails with "assert(isShiftCount(imm8 >> 1)) failed: illegal shift count"
Reviewed-by: vlivanov, kvn, chagedorn
1 parent 3b3e90e commit 1c4c99e

File tree

5 files changed

+180
-65
lines changed

5 files changed

+180
-65
lines changed

src/hotspot/cpu/x86/assembler_x86.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4905,7 +4905,7 @@ void Assembler::ret(int imm16) {
49054905
}
49064906

49074907
void Assembler::roll(Register dst, int imm8) {
4908-
assert(isShiftCount(imm8 >> 1), "illegal shift count");
4908+
assert(isShiftCount(imm8), "illegal shift count");
49094909
int encode = prefix_and_encode(dst->encoding());
49104910
if (imm8 == 1) {
49114911
emit_int16((unsigned char)0xD1, (0xC0 | encode));
@@ -4920,7 +4920,7 @@ void Assembler::roll(Register dst) {
49204920
}
49214921

49224922
void Assembler::rorl(Register dst, int imm8) {
4923-
assert(isShiftCount(imm8 >> 1), "illegal shift count");
4923+
assert(isShiftCount(imm8), "illegal shift count");
49244924
int encode = prefix_and_encode(dst->encoding());
49254925
if (imm8 == 1) {
49264926
emit_int16((unsigned char)0xD1, (0xC8 | encode));

src/hotspot/share/opto/addnode.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -767,22 +767,22 @@ Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
767767
int ropcode = in(2)->Opcode();
768768
if (Matcher::match_rule_supported(Op_RotateLeft) &&
769769
lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
770-
Node* lshift = in(1)->in(2);
771-
Node* rshift = in(2)->in(2);
772-
Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
773-
if (shift != NULL) {
774-
return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
775-
}
776-
return NULL;
770+
Node* lshift = in(1)->in(2);
771+
Node* rshift = in(2)->in(2);
772+
Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
773+
if (shift != NULL) {
774+
return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
775+
}
776+
return NULL;
777777
}
778778
if (Matcher::match_rule_supported(Op_RotateRight) &&
779779
lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
780-
Node *rshift = in(1)->in(2);
781-
Node *lshift = in(2)->in(2);
782-
Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
783-
if (shift != NULL) {
784-
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
785-
}
780+
Node* rshift = in(1)->in(2);
781+
Node* lshift = in(2)->in(2);
782+
Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
783+
if (shift != NULL) {
784+
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
785+
}
786786
}
787787
return NULL;
788788
}

src/hotspot/share/opto/mulnode.cpp

Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -637,35 +637,44 @@ Node *AndLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
637637

638638
//=============================================================================
639639

640-
static int getShiftCon(PhaseGVN *phase, Node *shiftNode, int retVal) {
641-
const Type *t = phase->type(shiftNode->in(2));
642-
if (t == Type::TOP) return retVal; // Right input is dead.
643-
const TypeInt *t2 = t->isa_int();
644-
if (!t2 || !t2->is_con()) return retVal; // Right input is a constant.
645-
646-
return t2->get_con();
640+
static bool const_shift_count(PhaseGVN* phase, Node* shiftNode, int* count) {
641+
const TypeInt* tcount = phase->type(shiftNode->in(2))->isa_int();
642+
if (tcount != NULL && tcount->is_con()) {
643+
*count = tcount->get_con();
644+
return true;
645+
}
646+
return false;
647647
}
648648

649-
static int maskShiftAmount(PhaseGVN *phase, Node *shiftNode, int nBits) {
650-
int shift = getShiftCon(phase, shiftNode, 0);
651-
int maskedShift = shift & (nBits - 1);
652-
653-
if (maskedShift == 0) return 0; // Let Identity() handle 0 shift count.
649+
static int maskShiftAmount(PhaseGVN* phase, Node* shiftNode, int nBits) {
650+
int count = 0;
651+
if (const_shift_count(phase, shiftNode, &count)) {
652+
int maskedShift = count & (nBits - 1);
653+
if (maskedShift == 0) {
654+
// Let Identity() handle 0 shift count.
655+
return 0;
656+
}
654657

655-
if (shift != maskedShift) {
656-
shiftNode->set_req(2, phase->intcon(maskedShift)); // Replace shift count with masked value.
657-
PhaseIterGVN* igvn = phase->is_IterGVN();
658-
if (igvn) {
659-
igvn->rehash_node_delayed(shiftNode);
658+
if (count != maskedShift) {
659+
shiftNode->set_req(2, phase->intcon(maskedShift)); // Replace shift count with masked value.
660+
PhaseIterGVN* igvn = phase->is_IterGVN();
661+
if (igvn) {
662+
igvn->rehash_node_delayed(shiftNode);
663+
}
660664
}
665+
return maskedShift;
661666
}
662-
663-
return maskedShift;
667+
return 0;
664668
}
665669

666670
//------------------------------Identity---------------------------------------
667671
Node* LShiftINode::Identity(PhaseGVN* phase) {
668-
return ((getShiftCon(phase, this, -1) & (BitsPerJavaInteger - 1)) == 0) ? in(1) : this;
672+
int count = 0;
673+
if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
674+
// Shift by a multiple of 32 does nothing
675+
return in(1);
676+
}
677+
return this;
669678
}
670679

671680
//------------------------------Ideal------------------------------------------
@@ -773,7 +782,12 @@ const Type* LShiftINode::Value(PhaseGVN* phase) const {
773782
//=============================================================================
774783
//------------------------------Identity---------------------------------------
775784
Node* LShiftLNode::Identity(PhaseGVN* phase) {
776-
return ((getShiftCon(phase, this, -1) & (BitsPerJavaLong - 1)) == 0) ? in(1) : this;
785+
int count = 0;
786+
if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
787+
// Shift by a multiple of 64 does nothing
788+
return in(1);
789+
}
790+
return this;
777791
}
778792

779793
//------------------------------Ideal------------------------------------------
@@ -878,26 +892,30 @@ const Type* LShiftLNode::Value(PhaseGVN* phase) const {
878892
//=============================================================================
879893
//------------------------------Identity---------------------------------------
880894
Node* RShiftINode::Identity(PhaseGVN* phase) {
881-
int shift = getShiftCon(phase, this, -1);
882-
if (shift == -1) return this;
883-
if ((shift & (BitsPerJavaInteger - 1)) == 0) return in(1);
884-
885-
// Check for useless sign-masking
886-
if (in(1)->Opcode() == Op_LShiftI &&
887-
in(1)->req() == 3 &&
888-
in(1)->in(2) == in(2)) {
889-
shift &= BitsPerJavaInteger-1; // semantics of Java shifts
890-
// Compute masks for which this shifting doesn't change
891-
int lo = (-1 << (BitsPerJavaInteger - ((uint)shift)-1)); // FFFF8000
892-
int hi = ~lo; // 00007FFF
893-
const TypeInt *t11 = phase->type(in(1)->in(1))->isa_int();
894-
if (!t11) return this;
895-
// Does actual value fit inside of mask?
896-
if (lo <= t11->_lo && t11->_hi <= hi) {
897-
return in(1)->in(1); // Then shifting is a nop
895+
int count = 0;
896+
if (const_shift_count(phase, this, &count)) {
897+
if ((count & (BitsPerJavaInteger - 1)) == 0) {
898+
// Shift by a multiple of 32 does nothing
899+
return in(1);
900+
}
901+
// Check for useless sign-masking
902+
if (in(1)->Opcode() == Op_LShiftI &&
903+
in(1)->req() == 3 &&
904+
in(1)->in(2) == in(2)) {
905+
count &= BitsPerJavaInteger-1; // semantics of Java shifts
906+
// Compute masks for which this shifting doesn't change
907+
int lo = (-1 << (BitsPerJavaInteger - ((uint)count)-1)); // FFFF8000
908+
int hi = ~lo; // 00007FFF
909+
const TypeInt* t11 = phase->type(in(1)->in(1))->isa_int();
910+
if (t11 == NULL) {
911+
return this;
912+
}
913+
// Does actual value fit inside of mask?
914+
if (lo <= t11->_lo && t11->_hi <= hi) {
915+
return in(1)->in(1); // Then shifting is a nop
916+
}
898917
}
899918
}
900-
901919
return this;
902920
}
903921

@@ -1082,8 +1100,11 @@ const Type* RShiftLNode::Value(PhaseGVN* phase) const {
10821100
//=============================================================================
10831101
//------------------------------Identity---------------------------------------
10841102
Node* URShiftINode::Identity(PhaseGVN* phase) {
1085-
int shift = getShiftCon(phase, this, -1);
1086-
if ((shift & (BitsPerJavaInteger - 1)) == 0) return in(1);
1103+
int count = 0;
1104+
if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaInteger - 1)) == 0) {
1105+
// Shift by a multiple of 32 does nothing
1106+
return in(1);
1107+
}
10871108

10881109
// Check for "((x << LogBytesPerWord) + (wordSize-1)) >> LogBytesPerWord" which is just "x".
10891110
// Happens during new-array length computation.
@@ -1266,7 +1287,12 @@ const Type* URShiftINode::Value(PhaseGVN* phase) const {
12661287
//=============================================================================
12671288
//------------------------------Identity---------------------------------------
12681289
Node* URShiftLNode::Identity(PhaseGVN* phase) {
1269-
return ((getShiftCon(phase, this, -1) & (BitsPerJavaLong - 1)) == 0) ? in(1) : this;
1290+
int count = 0;
1291+
if (const_shift_count(phase, this, &count) && (count & (BitsPerJavaLong - 1)) == 0) {
1292+
// Shift by a multiple of 64 does nothing
1293+
return in(1);
1294+
}
1295+
return this;
12701296
}
12711297

12721298
//------------------------------Ideal------------------------------------------
@@ -1444,6 +1470,21 @@ uint MulAddS2INode::hash() const {
14441470

14451471
//------------------------------Rotate Operations ------------------------------
14461472

1473+
Node* RotateLeftNode::Identity(PhaseGVN* phase) {
1474+
const Type* t1 = phase->type(in(1));
1475+
if (t1 == Type::TOP) {
1476+
return this;
1477+
}
1478+
int count = 0;
1479+
assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1480+
int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1481+
if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1482+
// Rotate by a multiple of 32/64 does nothing
1483+
return in(1);
1484+
}
1485+
return this;
1486+
}
1487+
14471488
const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
14481489
const Type* t1 = phase->type(in(1));
14491490
const Type* t2 = phase->type(in(2));
@@ -1460,11 +1501,10 @@ const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
14601501
if (r1 == TypeInt::ZERO) {
14611502
return TypeInt::ZERO;
14621503
}
1463-
// Shift by zero does nothing
1504+
// Rotate by zero does nothing
14641505
if (r2 == TypeInt::ZERO) {
14651506
return r1;
14661507
}
1467-
14681508
if (r1->is_con() && r2->is_con()) {
14691509
int shift = r2->get_con() & (BitsPerJavaInteger - 1); // semantics of Java shifts
14701510
return TypeInt::make((r1->get_con() << shift) | (r1->get_con() >> (32 - shift)));
@@ -1479,11 +1519,10 @@ const Type* RotateLeftNode::Value(PhaseGVN* phase) const {
14791519
if (r1 == TypeLong::ZERO) {
14801520
return TypeLong::ZERO;
14811521
}
1482-
// Shift by zero does nothing
1522+
// Rotate by zero does nothing
14831523
if (r2 == TypeInt::ZERO) {
14841524
return r1;
14851525
}
1486-
14871526
if (r1->is_con() && r2->is_con()) {
14881527
int shift = r2->get_con() & (BitsPerJavaLong - 1); // semantics of Java shifts
14891528
return TypeLong::make((r1->get_con() << shift) | (r1->get_con() >> (64 - shift)));
@@ -1508,6 +1547,21 @@ Node* RotateLeftNode::Ideal(PhaseGVN *phase, bool can_reshape) {
15081547
return NULL;
15091548
}
15101549

1550+
Node* RotateRightNode::Identity(PhaseGVN* phase) {
1551+
const Type* t1 = phase->type(in(1));
1552+
if (t1 == Type::TOP) {
1553+
return this;
1554+
}
1555+
int count = 0;
1556+
assert(t1->isa_int() || t1->isa_long(), "Unexpected type");
1557+
int mask = (t1->isa_int() ? BitsPerJavaInteger : BitsPerJavaLong) - 1;
1558+
if (const_shift_count(phase, this, &count) && (count & mask) == 0) {
1559+
// Rotate by a multiple of 32/64 does nothing
1560+
return in(1);
1561+
}
1562+
return this;
1563+
}
1564+
15111565
const Type* RotateRightNode::Value(PhaseGVN* phase) const {
15121566
const Type* t1 = phase->type(in(1));
15131567
const Type* t2 = phase->type(in(2));
@@ -1524,7 +1578,7 @@ const Type* RotateRightNode::Value(PhaseGVN* phase) const {
15241578
if (r1 == TypeInt::ZERO) {
15251579
return TypeInt::ZERO;
15261580
}
1527-
// Shift by zero does nothing
1581+
// Rotate by zero does nothing
15281582
if (r2 == TypeInt::ZERO) {
15291583
return r1;
15301584
}
@@ -1533,7 +1587,6 @@ const Type* RotateRightNode::Value(PhaseGVN* phase) const {
15331587
return TypeInt::make((r1->get_con() >> shift) | (r1->get_con() << (32 - shift)));
15341588
}
15351589
return TypeInt::INT;
1536-
15371590
} else {
15381591
assert(t1->isa_long(), "Type must be a long");
15391592
const TypeLong* r1 = t1->is_long();
@@ -1542,7 +1595,7 @@ const Type* RotateRightNode::Value(PhaseGVN* phase) const {
15421595
if (r1 == TypeLong::ZERO) {
15431596
return TypeLong::ZERO;
15441597
}
1545-
// Shift by zero does nothing
1598+
// Rotate by zero does nothing
15461599
if (r2 == TypeInt::ZERO) {
15471600
return r1;
15481601
}

src/hotspot/share/opto/mulnode.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class RotateLeftNode : public TypeNode {
220220
init_req(2, in2);
221221
}
222222
virtual int Opcode() const;
223+
virtual Node* Identity(PhaseGVN* phase);
223224
virtual const Type* Value(PhaseGVN* phase) const;
224225
virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
225226
};
@@ -232,6 +233,7 @@ class RotateRightNode : public TypeNode {
232233
init_req(2, in2);
233234
}
234235
virtual int Opcode() const;
236+
virtual Node* Identity(PhaseGVN* phase);
235237
virtual const Type* Value(PhaseGVN* phase) const;
236238
};
237239

0 commit comments

Comments
 (0)