Skip to content

Commit df5a49f

Browse files
committed
[JSC] B3 ReduceStrength should not fold Trunc(SShr(Shl(SExt32(x), c), c)) to x for a negative c
https://bugs.webkit.org/show_bug.cgi?id=328174 rdar://2 Reviewed by Tadeu Zagallo. Previously this conversion only checks constant is < 31. But when it is negative number, since it is masked with width of value (64 - 1 etc.), it can be larger than 31 still, but it passed. This patch fixes it by adding non-negative check. We also do this masking generally for shift operations. Tests: Source/JavaScriptCore/b3/testb3_1.cpp Source/JavaScriptCore/b3/testb3_7.cpp * Source/JavaScriptCore/b3/B3ReduceStrength.cpp: * Source/JavaScriptCore/b3/testb3.h: * Source/JavaScriptCore/b3/testb3_1.cpp: (run): * Source/JavaScriptCore/b3/testb3_7.cpp: (testTruncSShrShlSExt32): Canonical link: https://commits.webkit.org/324247@main
1 parent 7d12474 commit df5a49f

4 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎Source/JavaScriptCore/b3/B3ReduceStrength.cpp‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ class IntRange {
394394
template<typename T>
395395
IntRange LEGACY_NODELETE shl(int32_t shiftAmount)
396396
{
397+
shiftAmount &= static_cast<int32_t>(sizeof(T) * 8 - 1);
397398
T newMin = static_cast<T>(m_min) << static_cast<T>(shiftAmount);
398399
T newMax = static_cast<T>(m_max) << static_cast<T>(shiftAmount);
399400

@@ -422,6 +423,7 @@ class IntRange {
422423
template<typename T>
423424
IntRange LEGACY_NODELETE sShr(int32_t shiftAmount)
424425
{
426+
shiftAmount &= static_cast<int32_t>(sizeof(T) * 8 - 1);
425427
T newMin = static_cast<T>(m_min) >> static_cast<T>(shiftAmount);
426428
T newMax = static_cast<T>(m_max) >> static_cast<T>(shiftAmount);
427429

@@ -444,6 +446,8 @@ class IntRange {
444446
template<typename T>
445447
IntRange LEGACY_NODELETE zShr(int32_t shiftAmount)
446448
{
449+
shiftAmount &= static_cast<int32_t>(sizeof(T) * 8 - 1);
450+
447451
// This is an awkward corner case for all of the other logic.
448452
if (!shiftAmount)
449453
return *this;
@@ -1960,6 +1964,8 @@ class ReduceStrength {
19601964
break;
19611965

19621966
case Shl:
1967+
maskConstantShiftAmount();
1968+
19631969
// Turn this: Shl(constant1, constant2)
19641970
// Into this: constant1 << constant2
19651971
if (Value* constant = m_value->child(0)->shlConstant(m_proc, m_value->child(1))) {
@@ -1984,6 +1990,8 @@ class ReduceStrength {
19841990
break;
19851991

19861992
case SShr:
1993+
maskConstantShiftAmount();
1994+
19871995
// Turn this: SShr(constant1, constant2)
19881996
// Into this: constant1 >> constant2
19891997
if (Value* constant = m_value->child(0)->sShrConstant(m_proc, m_value->child(1))) {
@@ -2079,6 +2087,8 @@ class ReduceStrength {
20792087
break;
20802088

20812089
case ZShr:
2090+
maskConstantShiftAmount();
2091+
20822092
// Turn this: ZShr(constant1, constant2)
20832093
// Into this: (unsigned)constant1 >> constant2
20842094
if (Value* constant = m_value->child(0)->zShrConstant(m_proc, m_value->child(1))) {
@@ -2149,6 +2159,8 @@ class ReduceStrength {
21492159
break;
21502160

21512161
case RotR:
2162+
maskConstantShiftAmount();
2163+
21522164
// Turn this: RotR(constant1, constant2)
21532165
// Into this: (constant1 >> constant2) | (constant1 << sizeof(constant1) * 8 - constant2)
21542166
if (Value* constant = m_value->child(0)->rotRConstant(m_proc, m_value->child(1))) {
@@ -2160,6 +2172,8 @@ class ReduceStrength {
21602172
break;
21612173

21622174
case RotL:
2175+
maskConstantShiftAmount();
2176+
21632177
// Turn this: RotL(constant1, constant2)
21642178
// Into this: (constant1 << constant2) | (constant1 >> sizeof(constant1) * 8 - constant2)
21652179
if (Value* constant = m_value->child(0)->rotLConstant(m_proc, m_value->child(1))) {
@@ -2563,13 +2577,18 @@ class ReduceStrength {
25632577
int32_t shiftAmountConstant = m_value->child(0)->child(1)->asInt32();
25642578
auto sshrArg0 = m_value->child(0)->child(0);
25652579

2566-
// Turn this: Trunc(SShr(Shl(SExt32(@a), $12), $12))
2580+
// Turn this: Trunc(SShr(Shl(SExt32(@a), $c), $c)) where (c & 63) <= 32
25672581
// Into this: @a
2582+
// SExt32 makes the top 33 bits copies of the sign bit, so shifting out up to 32 of them
2583+
// and shifting back in loses nothing.
25682584
if (sshrArg0->opcode() == Shl && sshrArg0->child(1)->hasInt32()
2569-
&& sshrArg0->child(1)->asInt32() == shiftAmountConstant && shiftAmountConstant < 31
25702585
&& sshrArg0->child(0)->opcode() == SExt32) {
2571-
replaceWithIdentity(sshrArg0->child(0)->child(0));
2572-
break;
2586+
unsigned shlAmount = sshrArg0->child(1)->asInt32() & 63;
2587+
unsigned sshrAmount = shiftAmountConstant & 63;
2588+
if (shlAmount == sshrAmount && sshrAmount <= 32) {
2589+
replaceWithIdentity(sshrArg0->child(0)->child(0));
2590+
break;
2591+
}
25732592
}
25742593

25752594
// Shl(SExt32(@a), $12)
@@ -4665,6 +4684,20 @@ class ReduceStrength {
46654684
m_changed = true;
46664685
}
46674686

4687+
// Shifts and rotates use their amount modulo the operand width. Reducing a constant amount to
4688+
// that range up front lets the shift patterns compare and compute with it directly.
4689+
void maskConstantShiftAmount()
4690+
{
4691+
if (!m_value->child(1)->hasInt32())
4692+
return;
4693+
int32_t amount = m_value->child(1)->asInt32();
4694+
int32_t maskedAmount = amount & static_cast<int32_t>(sizeofType(m_value->type()) * 8 - 1);
4695+
if (amount == maskedAmount)
4696+
return;
4697+
m_value->child(1) = m_insertionSet.insert<Const32Value>(m_index, m_value->origin(), maskedAmount);
4698+
m_changed = true;
4699+
}
4700+
46684701
void handleShiftAmount()
46694702
{
46704703
// Shift anything by zero is identity.

‎Source/JavaScriptCore/b3/testb3.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ void testSShrCompare64(int64_t);
15001500
void testInt52RoundTripUnary(int32_t);
15011501
void testInt52RoundTripBinary();
15021502
void testTruncSShrAddUnalignedConstant();
1503+
void testTruncSShrShlSExt32(int32_t);
15031504

15041505
void testMulHigh32();
15051506
void testMulHigh64();

‎Source/JavaScriptCore/b3/testb3_1.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,19 @@ void run(const TestConfig* config)
640640
RUN_UNARY(testInt52RoundTripUnary, int32Operands());
641641
RUN(testInt52RoundTripBinary());
642642
RUN(testTruncSShrAddUnalignedConstant());
643+
RUN(testTruncSShrShlSExt32(-64));
644+
RUN(testTruncSShrShlSExt32(-33));
645+
RUN(testTruncSShrShlSExt32(-32));
646+
RUN(testTruncSShrShlSExt32(-31));
647+
RUN(testTruncSShrShlSExt32(-1));
648+
RUN(testTruncSShrShlSExt32(0));
649+
RUN(testTruncSShrShlSExt32(1));
650+
RUN(testTruncSShrShlSExt32(12));
651+
RUN(testTruncSShrShlSExt32(30));
652+
RUN(testTruncSShrShlSExt32(31));
653+
RUN(testTruncSShrShlSExt32(32));
654+
RUN(testTruncSShrShlSExt32(63));
655+
RUN(testTruncSShrShlSExt32(64));
643656

644657
RUN_UNARY(testCheckAddRemoveCheckWithSExt8, int8Operands());
645658
RUN_UNARY(testCheckAddRemoveCheckWithSExt16, int16Operands());

‎Source/JavaScriptCore/b3/testb3_7.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,6 +3864,24 @@ void testTruncSShrAddUnalignedConstant()
38643864
CHECK_EQ(invoke<int32_t>(*code, static_cast<int64_t>(-2048)), static_cast<int32_t>((-2048LL + constant) >> 12));
38653865
}
38663866

3867+
void testTruncSShrShlSExt32(int32_t amount)
3868+
{
3869+
Procedure proc;
3870+
BasicBlock* root = proc.addBlock();
3871+
auto arguments = cCallArgumentValues<int32_t>(proc, root);
3872+
Value* extended = root->appendNew<Value>(proc, SExt32, Origin(), arguments[0]);
3873+
Value* shl = root->appendNew<Value>(proc, Shl, Origin(), extended, root->appendNew<Const32Value>(proc, Origin(), amount));
3874+
Value* sshr = root->appendNew<Value>(proc, SShr, Origin(), shl, root->appendNew<Const32Value>(proc, Origin(), amount));
3875+
root->appendNew<Value>(proc, Return, Origin(), root->appendNew<Value>(proc, Trunc, Origin(), sshr));
3876+
auto code = compileProc(proc);
3877+
3878+
unsigned maskedAmount = amount & 63;
3879+
for (auto operand : int32Operands()) {
3880+
int64_t shifted = static_cast<int64_t>(static_cast<uint64_t>(static_cast<int64_t>(operand.value)) << maskedAmount);
3881+
CHECK_EQ(invoke<int32_t>(*code, operand.value), static_cast<int32_t>(shifted >> maskedAmount));
3882+
}
3883+
}
3884+
38673885
// Test XOR-and-rotate-right pattern matching for XAR instruction (SHA3).
38683886
// Pattern: VectorOr(VectorShl(VectorXor(a, b), shlConst), VectorShr(VectorXor(a, b), shrConst))
38693887
// Should be folded into a single XAR instruction on ARM64 with SHA3 support.

0 commit comments

Comments
 (0)