Skip to content

Truly independent web browser

LibJS: Let float64 arithmetic in the JIT reuse its left operand register

Andreas Kling2979576d7805
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
index 8495315341b..38b42c4534a 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
@@ -22,6 +22,7 @@ use crate::inline_vec::InlineVec;
use crate::ir::BinaryOp;
use crate::ir::BlockId;
use crate::ir::BranchCondition;
+use crate::ir::Float64UnaryOp;
use crate::ir::Node;
use crate::ir::NodeId;
use crate::ir::Op;
@@ -374,7 +375,18 @@ pub fn lowering_info(node: &Node) -> LoweringInfo {
reuses_first_input: true,
..plain
},
- Op::UnboxDouble | Op::BoxFloat64 | Op::Float64Unary { .. } | Op::Float64Binary { .. } => plain,
+ // NB: Rounding reads its input after writing its output.
+ Op::UnboxDouble
+ | Op::BoxFloat64
+ | Op::Float64Unary {
+ op: Float64UnaryOp::Round,
+ } => plain,
+ // OPTIMIZATION: The SSE instructions of x86 write their result over their first operand, which then needs no
+ // copy where it dies.
+ Op::Float64Unary { .. } | Op::Float64Binary { .. } => LoweringInfo {
+ reuses_first_input: true,
+ ..plain
+ },
Op::Float64Min | Op::Float64Max | Op::CallFloat64Function { .. } | Op::Float64Compare { .. } => plain,
Op::Float64ToInt32 | Op::Float64ToUint8Clamped | Op::CheckFloat64Int32 => plain,
Op::Uint32ShiftRight => plain.with_immediate(1),
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 6473d5a0510..feb85d2ec24 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -669,6 +669,58 @@ fn binary_operations_on_two_operands() {
}
}
+#[test]
+fn double_operations_on_values_that_die_at_them() {
+ // ((a0 * a1 - a1) / a0, then a1 minus that): each product, difference and quotient dies at the next operation,
+ // as its left operand or (last) its right one, whose output then takes its register.
+ let program = assemble(|_| {
+ vec![
+ Instruction::Enter,
+ Instruction::Mul {
+ arith_feedback: 0,
+ dst: r(5),
+ lhs: argument(0),
+ rhs: argument(1),
+ },
+ Instruction::Sub {
+ arith_feedback: 1,
+ dst: r(6),
+ lhs: r(5),
+ rhs: argument(1),
+ },
+ Instruction::Div {
+ arith_feedback: 2,
+ dst: r(7),
+ lhs: r(6),
+ rhs: argument(0),
+ },
+ Instruction::Sub {
+ arith_feedback: 3,
+ dst: r(5),
+ lhs: argument(1),
+ rhs: r(7),
+ },
+ Instruction::Return { value: r(5) },
+ ]
+ });
+ let (snapshot, compiled) = compile_program(&program, |snapshot| {
+ snapshot.executables[0].feedback.arith = vec![crate::snapshot::arith_feedback::DOUBLE; 4];
+ });
+ let numbers = [0.5, -1.25, 3.0, 1e300, -0.0, f64::INFINITY, f64::NAN, 7.75];
+ for lhs in numbers {
+ for rhs in numbers {
+ let (result, _) = instruction_result(run_compiled_program(
+ &snapshot,
+ &compiled,
+ &[double(lhs), double(rhs)],
+ |_| {},
+ ));
+ let expected = box_number(rhs - (lhs * rhs - rhs) / lhs);
+ assert_eq!(result, expected, "{lhs:?} {rhs:?}: got {result:#x}");
+ }
+ }
+}
+
#[test]
fn binary_operations_with_an_int32_operand() {
let values = values();
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
index ef0b3dcd944..49e5ae3b9b9 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
@@ -517,6 +517,60 @@ fn int32_operations_write_over_their_first_input_where_it_dies() {
);
}
+#[test]
+fn float64_operations_write_over_their_first_input_where_it_dies() {
+ // The first product dies at the second, whose output then takes its register, also for non-commutative
+ // operations; the argument stays live, so the first product does not take its register.
+ let program = assemble(|_| {
+ vec![
+ Instruction::Enter,
+ Instruction::Mul {
+ arith_feedback: 0,
+ dst: r(5),
+ lhs: a(0),
+ rhs: a(0),
+ },
+ Instruction::Sub {
+ arith_feedback: 1,
+ dst: r(6),
+ lhs: r(5),
+ rhs: a(0),
+ },
+ Instruction::Return { value: r(6) },
+ ]
+ });
+ let mut snapshot = crate::builder::tests::snapshot_for(&program, crate::bytecode::test_support::test_layout());
+ snapshot.executables[0].feedback.arith = vec![crate::snapshot::arith_feedback::DOUBLE; 2];
+ let graph = crate::builder::tests::optimized_graph(&snapshot);
+ let allocation = allocate_and_verify(&graph);
+ let binaries = graph
+ .nodes
+ .iter()
+ .enumerate()
+ .filter(|(_, node)| matches!(node.op, Op::Float64Binary { .. }))
+ .map(|(index, _)| index)
+ .collect::<Vec<_>>();
+ assert_eq!(binaries.len(), 2, "{}", dump_allocation(&graph, &allocation));
+ let (first, second) = (&allocation.nodes[binaries[0]], &allocation.nodes[binaries[1]]);
+ assert_ne!(
+ first.output,
+ Some(first.inputs[0]),
+ "{}",
+ dump_allocation(&graph, &allocation)
+ );
+ assert_eq!(
+ second.output,
+ Some(second.inputs[0]),
+ "{}",
+ dump_allocation(&graph, &allocation)
+ );
+ assert!(
+ matches!(second.output, Some(Location::Register(register)) if register >= FPR_BASE),
+ "{}",
+ dump_allocation(&graph, &allocation)
+ );
+}
+
#[test]
fn exits_find_constants_registers_and_spilled_values() {
let program = assemble(|_| {