Truly independent web browser
LibJS/JIT: Check numbers for operations that only saw int32 operands
Andreas Kling31a4b91c6060
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rsindex 86320fd5d1e..f9f766be89b 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs@@ -419,13 +419,23 @@ impl GraphBuilder<'_> {/// Builds numeric instructions that saw doubles with number guards,/// leaving non-numeric operands to the interpreter before any coercion.+ /// So do operations other than bitwise ones that saw int32 operands, but+ /// that int32 speculation did not build (like divisions, whose results+ /// are rarely int32 values, and operations whose results overflowed).fn try_build_number(&mut self,instruction: &Instruction,operation: Operation,) -> Result<Option<Flow>, CompileFailure> {let feedback = self.arith_feedback(instruction).unwrap_or(0);- if feedback & arith_feedback::DOUBLE == 0+ let is_bitwise = match operation {+ Operation::Binary { op, .. } => op.is_bitwise(),+ Operation::Unary { op, .. } => matches!(op, UnaryOp::BitwiseNot | UnaryOp::ToInt32),+ Operation::Update { .. } | Operation::Compare { .. } => false,+ };+ let saw_numbers =+ feedback & arith_feedback::DOUBLE != 0 || (!is_bitwise && feedback & arith_feedback::SAW_INT32 != 0);+ if !saw_numbers|| feedback & arith_feedback::NON_NUMERIC != 0|| !self.may_speculate(ExitKind::BadType)|| matches!(operation, Operation::Binary { op: BinaryOp::Mod, .. })diff --git a/Tests/LibJS/JIT/ir/number-operators.js b/Tests/LibJS/JIT/ir/number-operators.jsindex ac7bbc21336..d5ede9a601c 100644--- a/Tests/LibJS/JIT/ir/number-operators.js+++ b/Tests/LibJS/JIT/ir/number-operators.js@@ -143,3 +143,21 @@ recompileAfterExit(count, notANumber);// CHECK-NEXT: [[NUMBER:v[0-9]+]]:float64 = CheckNumber ([[COUNT]])// CHECK-NOT: CallSlowPath LessThan// CHECK: Float64LessThan ([[NUMBER]], #0x4025000000000000)++// Divisions of int32 values make numbers that are rarely int32 values, so a+// division whose feedback only saw int32 operands checks that they are numbers+// too, and exits for other operands.++function ratio(a, b) {+ return a / b;+}+jit.prepare(ratio);+for (let i = 1; i < 10; ++i) ratio(i * 6, 3);+jit.compile(ratio);++// CHECK-FUNCTION: ratio+// CHECK-PASS: dead code elimination+// CHECK: [[A:v[0-9]+]]:float64 = CheckNumber+// CHECK-NEXT: [[B:v[0-9]+]]:float64 = CheckNumber+// CHECK-NEXT: Float64Div ([[A]], [[B]])+// CHECK-NOT: CallSlowPath Div