Optimize bitwise shift and rotate operations for constant right-hand side values

This commit is contained in:
ownedbywuigi 2026-03-27 19:03:45 +00:00
commit b87795b656

View file

@ -4280,13 +4280,19 @@ BaseCompiler::emitShlI32()
void void
BaseCompiler::emitShlI64() BaseCompiler::emitShlI64()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI64 r = popI64();
masm.lshift64(Imm32(c & 63), r.reg);
pushI64(r);
} else {
RegI64 r0, r1; RegI64 r0, r1;
pop2xI64ForShiftOrRotate(&r0, &r1); pop2xI64ForShiftOrRotate(&r0, &r1);
masm.lshift64(lowPart(r1), r0.reg); masm.lshift64(lowPart(r1), r0.reg);
freeI64(r1); freeI64(r1);
pushI64(r0); pushI64(r0);
} }
}
void void
BaseCompiler::emitShrI32() BaseCompiler::emitShrI32()
@ -4309,13 +4315,19 @@ BaseCompiler::emitShrI32()
void void
BaseCompiler::emitShrI64() BaseCompiler::emitShrI64()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI64 r = popI64();
masm.rshift64Arithmetic(Imm32(c & 63), r.reg);
pushI64(r);
} else {
RegI64 r0, r1; RegI64 r0, r1;
pop2xI64ForShiftOrRotate(&r0, &r1); pop2xI64ForShiftOrRotate(&r0, &r1);
masm.rshift64Arithmetic(lowPart(r1), r0.reg); masm.rshift64Arithmetic(lowPart(r1), r0.reg);
freeI64(r1); freeI64(r1);
pushI64(r0); pushI64(r0);
} }
}
void void
BaseCompiler::emitShrU32() BaseCompiler::emitShrU32()
@ -4338,57 +4350,99 @@ BaseCompiler::emitShrU32()
void void
BaseCompiler::emitShrU64() BaseCompiler::emitShrU64()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI64 r = popI64();
masm.rshift64(Imm32(c & 63), r.reg);
pushI64(r);
} else {
RegI64 r0, r1; RegI64 r0, r1;
pop2xI64ForShiftOrRotate(&r0, &r1); pop2xI64ForShiftOrRotate(&r0, &r1);
masm.rshift64(lowPart(r1), r0.reg); masm.rshift64(lowPart(r1), r0.reg);
freeI64(r1); freeI64(r1);
pushI64(r0); pushI64(r0);
} }
}
void void
BaseCompiler::emitRotrI32() BaseCompiler::emitRotrI32()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI32 r = popI32();
masm.rotateRight(Imm32(c & 31), r.reg, r.reg);
pushI32(r);
} else {
RegI32 r0, r1; RegI32 r0, r1;
pop2xI32ForShiftOrRotate(&r0, &r1); pop2xI32ForShiftOrRotate(&r0, &r1);
masm.rotateRight(r1.reg, r0.reg, r0.reg); masm.rotateRight(r1.reg, r0.reg, r0.reg);
freeI32(r1); freeI32(r1);
pushI32(r0); pushI32(r0);
} }
}
void void
BaseCompiler::emitRotrI64() BaseCompiler::emitRotrI64()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI64 r = popI64();
#ifdef JS_PUNBOX64
masm.rotateRight64(Imm32(c & 63), r.reg, r.reg);
#else
RegI32 temp = needI32();
masm.rotateRight64(Imm32(c & 63), r.reg, r.reg, temp.reg);
freeI32(temp);
#endif
pushI64(r);
} else {
RegI64 r0, r1; RegI64 r0, r1;
pop2xI64ForShiftOrRotate(&r0, &r1); pop2xI64ForShiftOrRotate(&r0, &r1);
masm.rotateRight64(lowPart(r1), r0.reg, r0.reg, maybeHighPart(r1)); masm.rotateRight64(lowPart(r1), r0.reg, r0.reg, maybeHighPart(r1));
freeI64(r1); freeI64(r1);
pushI64(r0); pushI64(r0);
} }
}
void void
BaseCompiler::emitRotlI32() BaseCompiler::emitRotlI32()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI32 r = popI32();
masm.rotateLeft(Imm32(c & 31), r.reg, r.reg);
pushI32(r);
} else {
RegI32 r0, r1; RegI32 r0, r1;
pop2xI32ForShiftOrRotate(&r0, &r1); pop2xI32ForShiftOrRotate(&r0, &r1);
masm.rotateLeft(r1.reg, r0.reg, r0.reg); masm.rotateLeft(r1.reg, r0.reg, r0.reg);
freeI32(r1); freeI32(r1);
pushI32(r0); pushI32(r0);
} }
}
void void
BaseCompiler::emitRotlI64() BaseCompiler::emitRotlI64()
{ {
// TODO / OPTIMIZE: Constant rhs (Bug 1316803) int32_t c;
if (popConstI32(c)) {
RegI64 r = popI64();
#ifdef JS_PUNBOX64
masm.rotateLeft64(Imm32(c & 63), r.reg, r.reg);
#else
RegI32 temp = needI32();
masm.rotateLeft64(Imm32(c & 63), r.reg, r.reg, temp.reg);
freeI32(temp);
#endif
pushI64(r);
} else {
RegI64 r0, r1; RegI64 r0, r1;
pop2xI64ForShiftOrRotate(&r0, &r1); pop2xI64ForShiftOrRotate(&r0, &r1);
masm.rotateLeft64(lowPart(r1), r0.reg, r0.reg, maybeHighPart(r1)); masm.rotateLeft64(lowPart(r1), r0.reg, r0.reg, maybeHighPart(r1));
freeI64(r1); freeI64(r1);
pushI64(r0); pushI64(r0);
} }
}
void void
BaseCompiler::emitEqzI32() BaseCompiler::emitEqzI32()