Skip to content

Truly independent web browser

LibJS/JIT: Store integer constants into elements as immediates

Andreas Klingaed0b0e07d90
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/aarch64/macro_assembler.rs b/Libraries/LibJS/JIT/Rust/src/asm/aarch64/macro_assembler.rs
index b0a3a1de0cf..e756381b657 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/aarch64/macro_assembler.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/aarch64/macro_assembler.rs
@@ -186,6 +186,17 @@ impl MacroAssembler {
self.assembler.load_store(op, rt, operand);
}
+ /// Stores `imm` with the store `op` of 32 bits or fewer, from the zero register where it can.
+ fn store_imm32_with(&mut self, op: MemOp, address: &Address, imm: u32) {
+ let source = if imm == 0 {
+ XZR
+ } else {
+ self.assembler.mov_imm(W32, VALUE_SCRATCH, u64::from(imm));
+ VALUE_SCRATCH
+ };
+ self.load_store(op, source.0, address);
+ }
+
/// `dst = lhs +/- imm`. Either register may be SP.
fn arithmetic_imm(&mut self, op: ArithmeticOp, width: Width, dst: Gpr, lhs: Gpr, imm: i64) {
let (op, magnitude) = if imm < 0 {
@@ -583,14 +594,16 @@ impl PortableMacroAssembler for MacroAssembler {
self.load_store(MemOp::StrX, src.0, address);
}
+ fn store_imm8(&mut self, address: &Address, imm: u8) {
+ self.store_imm32_with(MemOp::Strb, address, u32::from(imm));
+ }
+
+ fn store_imm16(&mut self, address: &Address, imm: u16) {
+ self.store_imm32_with(MemOp::Strh, address, u32::from(imm));
+ }
+
fn store_imm32(&mut self, address: &Address, imm: u32) {
- let source = if imm == 0 {
- XZR
- } else {
- self.assembler.mov_imm(W32, VALUE_SCRATCH, u64::from(imm));
- VALUE_SCRATCH
- };
- self.load_store(MemOp::StrW, source.0, address);
+ self.store_imm32_with(MemOp::StrW, address, imm);
}
fn store_imm64(&mut self, address: &Address, imm: u64) {
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/aarch64/tests.rs b/Libraries/LibJS/JIT/Rust/src/asm/aarch64/tests.rs
index e22bfa4fc11..3eaeed6ebf0 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/aarch64/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/aarch64/tests.rs
@@ -721,6 +721,8 @@ mod macro_assembler {
"ldrsw x3, [x4, #0x4]" => |m| m.load32_sign_extend(X3, &base(X4, 4));
"str xzr, [sp, #0x8]" => |m| m.store_imm64(&base(SP, 8), 0);
"mov w17, #0x5; str w17, [x0]" => |m| m.store_imm32(&base(X0, 0), 5);
+ "mov w17, #0x41; strb w17, [x0, #0x3]" => |m| m.store_imm8(&base(X0, 3), 0x41);
+ "strh wzr, [x0, #0x2]" => |m| m.store_imm16(&base(X0, 2), 0);
"strh w5, [x6, #0x6]" => |m| m.store16(&base(X6, 6), X5);
"add x0, sp, x1, lsl #0x3; add x0, x0, #0x18" => |m| m.load_effective_address(X0, &indexed(SP, X1, Scale::Eight, 24));
"mov x0, sp" => |m| m.move64(X0, SP);
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs b/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
index 7351aeceaa3..9e307441ad8 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/execution_tests.rs
@@ -899,6 +899,8 @@ fn loads_and_stores() {
let code = assemble(|masm| {
masm.store_imm64(&Address::new(RDI, 0), imm);
masm.store_imm32(&Address::new(RDI, 8), imm as u32);
+ masm.store_imm16(&Address::new(RDI, 12), imm as u16);
+ masm.store_imm8(&Address::new(RDI, 14), imm as u8);
masm.ret();
});
// SAFETY: The code implements `Access`.
@@ -907,7 +909,8 @@ fn loads_and_stores() {
function(target.as_mut_ptr(), 0);
assert_eq!(u64::from_le_bytes(target[..8].try_into().unwrap()), imm);
assert_eq!(u32::from_le_bytes(target[8..12].try_into().unwrap()), imm as u32);
- assert_eq!(target[12..], [0xaa; 4]);
+ assert_eq!(u16::from_le_bytes(target[12..14].try_into().unwrap()), imm as u16);
+ assert_eq!(target[14..], [imm as u8, 0xaa]);
}
// Doubles through memory.
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/macro_assembler.rs b/Libraries/LibJS/JIT/Rust/src/asm/macro_assembler.rs
index 713a2c9dde1..7ca8ef519b8 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/macro_assembler.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/macro_assembler.rs
@@ -125,6 +125,8 @@ pub trait PortableMacroAssembler: Sized {
fn store16(&mut self, address: &Address, src: Gpr);
fn store32(&mut self, address: &Address, src: Gpr);
fn store64(&mut self, address: &Address, src: Gpr);
+ fn store_imm8(&mut self, address: &Address, imm: u8);
+ fn store_imm16(&mut self, address: &Address, imm: u16);
fn store_imm32(&mut self, address: &Address, imm: u32);
fn store_imm64(&mut self, address: &Address, imm: u64);
/// Loads the 16 bytes of `imm` (little-endian) into all of `dst`, from
diff --git a/Libraries/LibJS/JIT/Rust/src/asm/x86_64/macro_assembler.rs b/Libraries/LibJS/JIT/Rust/src/asm/x86_64/macro_assembler.rs
index b2e1f0b1eef..67f7bf93990 100644
--- a/Libraries/LibJS/JIT/Rust/src/asm/x86_64/macro_assembler.rs
+++ b/Libraries/LibJS/JIT/Rust/src/asm/x86_64/macro_assembler.rs
@@ -481,6 +481,17 @@ impl PortableMacroAssembler for MacroAssembler {
self.assembler.mov_mr(W64, address, src);
}
+ fn store_imm8(&mut self, address: &Address, imm: u8) {
+ self.assembler.mov8_mi(address, imm);
+ }
+
+ // OPTIMIZATION: A 16-bit immediate after an operand-size prefix stalls the legacy decoders for a few cycles, which
+ // a store of the low half of a register avoids.
+ fn store_imm16(&mut self, address: &Address, imm: u16) {
+ self.assembler.mov_ri32(SCRATCH, u32::from(imm));
+ self.assembler.mov16_mr(address, SCRATCH);
+ }
+
fn store_imm32(&mut self, address: &Address, imm: u32) {
self.assembler.mov_mi(W32, address, imm as i32);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/elements.rs b/Libraries/LibJS/JIT/Rust/src/codegen/elements.rs
index ab6c5012da1..5f1c2af4e47 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/elements.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/elements.rs
@@ -307,6 +307,22 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
}
return;
}
+ if let InputValue::Constant(bits) = self.input_value(node, 2) {
+ let ElementsKind::TypedArray(element) = kind else {
+ self.masm.store_imm64(&address, bits);
+ return;
+ };
+ let integer = (bits as u32).cast_signed();
+ match element {
+ TypedArrayElement::Uint8Clamped => self.masm.store_imm8(&address, integer.clamp(0, 255) as u8),
+ TypedArrayElement::Uint8 | TypedArrayElement::Int8 => self.masm.store_imm8(&address, integer as u8),
+ TypedArrayElement::Uint16 | TypedArrayElement::Int16 => {
+ self.masm.store_imm16(&address, integer as u16);
+ }
+ _ => self.masm.store_imm32(&address, integer.cast_unsigned()),
+ }
+ return;
+ }
let source = self.input(node, 2);
let ElementsKind::TypedArray(element) = kind else {
self.masm.store64(&address, source);
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
index a18b10a3ae7..f2fb28204c9 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/mod.rs
@@ -464,10 +464,18 @@ fn constraints(node: &Node, registers: &RegisterSet) -> Constraints {
constraints.temps = 1;
}
// The elements or the element address.
- Op::LoadElementAt { .. } | Op::StoreElementAt { .. } => {
+ Op::LoadElementAt { .. } => {
constraints.inputs[1] = InputConstraint::RegisterOrConstant;
constraints.temps = 1;
}
+ // NB: Stores of integer and tagged constants store immediates.
+ Op::StoreElementAt { kind } => {
+ constraints.inputs[1] = InputConstraint::RegisterOrConstant;
+ if kind.stored_repr() != Repr::Float64 {
+ constraints.inputs[2] = InputConstraint::RegisterOrConstant;
+ }
+ constraints.temps = 1;
+ }
Op::CheckBounds => {
constraints.inputs[0] = InputConstraint::RegisterOrConstant;
constraints.inputs[1] = InputConstraint::RegisterOrConstant;