Truly independent web browser
LibJS: Read sometimes-boolean bitwise operands as int32s in the JIT
Andreas Kling6ff2ca31ff55
diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.confindex 4d9a45df871..908665da28e 100644--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf@@ -138,6 +138,7 @@ const ARITH_FEEDBACK_STRING = 8const ARITH_FEEDBACK_BIGINT = 16const ARITH_FEEDBACK_OTHER = 32const ARITH_FEEDBACK_OBJECT_PAIR = 64+const ARITH_FEEDBACK_BOOLEAN = 128const ARITH_FEEDBACK_INT32_AND_DOUBLE = 3const ARITH_FEEDBACK_INT32_AND_OTHER = 33const CALL_FEEDBACK_TARGET = 0diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flapindex cb2e1573776..c048061101c 100644--- a/Libraries/LibJS/Interpreter/interpreter.flap+++ b/Libraries/LibJS/Interpreter/interpreter.flap@@ -1059,6 +1059,8 @@ inline fn arith_feedback_bits(value: Value) -> i32 {bits = ARITH_FEEDBACK_STRING;} else if tag == BIGINT_TAG {bits = ARITH_FEEDBACK_BIGINT;+ } else if tag == BOOLEAN_TAG {+ bits = ARITH_FEEDBACK_BOOLEAN;}bits}diff --git a/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs b/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rsindex ce464c0fe1a..0b6f37e2dd9 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/intrinsics.rs@@ -212,9 +212,24 @@ impl GraphBuilder<'_> {double: fn(&mut Self, NodeId) -> NodeId,otherwise: impl FnOnce(&mut Self, BranchCondition, Vec<NodeId>) -> NodeId,) -> NodeId {- let (int32_block, other) = self.branch_both_ways(BranchCondition::Int32Value, vec![value]);+ self.on_integer_or_double(BranchCondition::Int32Value, value, repr, int32, double, otherwise)+ }++ /// Like `on_int32_or_double()`, with `integer` deciding the first case:+ /// `BranchCondition::Int32Value`, or `BranchCondition::Int32OrBoolean`,+ /// whose booleans `UnboxInt32` makes 0 or 1 of.+ pub(super) fn on_integer_or_double(+ &mut self,+ integer: BranchCondition,+ value: NodeId,+ repr: Repr,+ int32: fn(&mut Self, NodeId) -> NodeId,+ double: fn(&mut Self, NodeId) -> NodeId,+ otherwise: impl FnOnce(&mut Self, BranchCondition, Vec<NodeId>) -> NodeId,+ ) -> NodeId {+ let (int32_block, other) = self.branch_both_ways(integer, vec![value]);self.block = int32_block;- let refined = self.refine(BranchCondition::Int32Value, true, vec![value]);+ let refined = self.refine(integer, true, vec![value]);let integer = self.emit(Op::UnboxInt32, vec![refined], Some(Repr::Int32));let int32_value = int32(self, integer);let int32_end = self.block;diff --git a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rsindex f9f766be89b..84e5d1db1bf 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/speculation.rs@@ -707,7 +707,7 @@ impl GraphBuilder<'_> {let lhs = if constant_compares_by_identity {lhs} else {- let identity_kinds = arith_feedback::OTHER | arith_feedback::OBJECT_PAIR;+ let identity_kinds = arith_feedback::OTHER | arith_feedback::OBJECT_PAIR | arith_feedback::BOOLEAN;if feedback == 0 || feedback & !identity_kinds != 0 || !self.may_speculate(ExitKind::BadType) {return Ok(None);}@@ -875,13 +875,30 @@ impl GraphBuilder<'_> {)}- /// ToInt32 of `value`, as a `Repr::Int32`, where it is an int32 or, if- /// `doubles`, a double: tests send other values on.- fn int32_of_number_on_path(&mut self, paths: &mut SlowPaths, value: NodeId, doubles: bool) -> NodeId {- if !doubles || self.int32_value_of(value).is_some() || self.int32_constant_of(value).is_some() {+ /// ToInt32 of `value`, as a `Repr::Int32`, where it is an int32, a+ /// boolean if `booleans`, or a double if `doubles`: tests send other+ /// values on.+ fn int32_of_operand_on_path(+ &mut self,+ paths: &mut SlowPaths,+ value: NodeId,+ doubles: bool,+ booleans: bool,+ ) -> NodeId {+ if (!doubles && !booleans) || self.int32_value_of(value).is_some() || self.int32_constant_of(value).is_some() {return self.int32_on_path(paths, value);}- self.on_int32_or_double(+ let integer = if booleans {+ BranchCondition::Int32OrBoolean+ } else {+ BranchCondition::Int32Value+ };+ if !doubles {+ let refined = self.branch_to_next_case(paths, integer, vec![value], true);+ return self.emit(Op::UnboxInt32, vec![refined], Some(Repr::Int32));+ }+ self.on_integer_or_double(+ integer,value,Repr::Int32,|_, integer| integer,@@ -939,17 +956,20 @@ impl GraphBuilder<'_> {let known_int32 = [lhs, rhs].iter().all(|value| self.int32_value_of(*value).is_some() || self.is_boolean(*value));+ // NB: Operands that are sometimes booleans, like a local that+ // holds a comparison or an int32, saw booleans.+ let booleans = feedback & arith_feedback::BOOLEAN != 0;if !known_int32- && (feedback & (arith_feedback::SAW_INT32 | arith_feedback::DOUBLE) == 0- || !self.may_be_number(&[lhs, rhs]))+ && (feedback & (arith_feedback::SAW_INT32 | arith_feedback::DOUBLE | arith_feedback::BOOLEAN) == 0+ || !(booleans || self.may_be_number(&[lhs, rhs]))){return Ok(None);}let (lhs, rhs) = (self.boolean_as_int32(lhs), self.boolean_as_int32(rhs));let doubles = feedback & arith_feedback::DOUBLE != 0 || !self.may_be_int32(&[lhs, rhs]);let mut paths = self.start_slow_paths(Some(Repr::Tagged))?;- let lhs = self.int32_of_number_on_path(&mut paths, lhs, doubles);- let rhs = self.int32_of_number_on_path(&mut paths, rhs, doubles);+ let lhs = self.int32_of_operand_on_path(&mut paths, lhs, doubles, booleans);+ let rhs = self.int32_of_operand_on_path(&mut paths, rhs, doubles, booleans);let result = if op == BinaryOp::UnsignedRightShift && !int32_results {let bits = self.emit(Op::Uint32ShiftRight, vec![lhs, rhs], Some(Repr::Int32));let number = self.emit(Op::Uint32ToFloat64, vec![bits], Some(Repr::Float64));@@ -1068,7 +1088,9 @@ impl GraphBuilder<'_> {) -> Result<Option<SlowPaths>, CompileFailure> {let int32s = feedback & arith_feedback::SAW_INT32 != 0;let doubles = feedback & arith_feedback::DOUBLE != 0;- if !int32s && !doubles {+ // NB: ToInt32 of a boolean is 0 or 1 (see `build_binary_paths()`).+ let booleans = matches!(op, UnaryOp::BitwiseNot | UnaryOp::ToInt32) && feedback & arith_feedback::BOOLEAN != 0;+ if !int32s && !doubles && !booleans {return Ok(None);}let mut paths = self.start_slow_paths(Some(Repr::Tagged))?;@@ -1119,7 +1141,7 @@ impl GraphBuilder<'_> {}}UnaryOp::BitwiseNot | UnaryOp::ToInt32 => {- let integer = self.int32_of_number_on_path(&mut paths, value, doubles);+ let integer = self.int32_of_operand_on_path(&mut paths, value, doubles, booleans);let result = if op == UnaryOp::BitwiseNot {let all_ones = self.int32_constant(-1);self.int32_binary(BinaryOp::BitwiseXor, integer, all_ones)diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex 34a983065e8..f60d8ca9004 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -2440,6 +2440,39 @@ mod arguments {}}+fn dump_bitwise_or(feedback: u8) -> String {+ let program = assemble(|_| {+ vec![+ Instruction::BitwiseOr {+ dst: r(5),+ lhs: a(0),+ rhs: r(6),+ arith_feedback: 0,+ },+ Instruction::Return { value: r(5) },+ ]+ });+ let mut snapshot = snapshot_for(&program, test_layout());+ snapshot.executables[0].feedback.arith = vec![feedback];+ dump(&optimized_graph(&snapshot), &test_layout())+}++#[test]+fn bitwise_operands_that_were_booleans_are_read_as_int32s() {+ use crate::snapshot::arith_feedback::{BOOLEAN, INT32, OTHER};+ let text = dump_bitwise_or(INT32 | BOOLEAN);+ assert_eq!(text.matches("BranchInt32OrBoolean").count(), 2, "{text}");+ assert!(text.contains("Int32BitwiseOr"), "{text}");+ assert_eq!(text.matches("CallSlowPath BitwiseOr").count(), 1, "{text}");+ // Other operands, like undefined, are no booleans.+ let text = dump_bitwise_or(INT32 | OTHER);+ assert!(!text.contains("BranchInt32OrBoolean"), "{text}");+ // Without booleans, int32 speculation checks the operands instead.+ let text = dump_bitwise_or(INT32);+ assert!(!text.contains("BranchInt32OrBoolean"), "{text}");+ assert_eq!(text.matches("CheckInt32").count(), 2, "{text}");+}+#[test]fn numeric_feedback_uses_number_guards_instead_of_type_dispatch() {let program = assemble(|_| {diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex 45d27116b77..8d0dcd291de 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -2245,6 +2245,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {BranchCondition::Int32Value => {self.branch_on_tag(when(Condition::Equal), input, value::INT32_TAG, scratch, target);}+ BranchCondition::Int32OrBoolean => {+ // NB: The int32 tag follows the boolean tag.+ const _: () = assert!(value::INT32_TAG == value::BOOLEAN_TAG + 1);+ self.masm.shr64_imm(scratch, input, value::TAG_SHIFT);+ self.masm.sub32_imm(scratch, scratch, i32::from(value::BOOLEAN_TAG));+ self.masm+ .branch32_imm(when(Condition::BelowOrEqual), scratch, 1, target);+ }BranchCondition::String => {self.branch_on_tag(when(Condition::Equal), input, value::STRING_TAG, scratch, target);}diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rsindex d7b299e867e..d9f9f08cd78 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs@@ -86,6 +86,7 @@ fn condition_text(condition: BranchCondition) -> String {BranchCondition::TaggedEquals { equal: true } => "TaggedEquals".to_string(),BranchCondition::TaggedEquals { equal: false } => "TaggedNotEquals".to_string(),BranchCondition::Int32Value => "Int32Value".to_string(),+ BranchCondition::Int32OrBoolean => "Int32OrBoolean".to_string(),BranchCondition::Double => "Double".to_string(),BranchCondition::String => "String".to_string(),BranchCondition::ResolvedString => "ResolvedString".to_string(),diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rsindex eaed95f3d1d..fdb9e5d4f51 100644--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs@@ -161,6 +161,9 @@ pub enum BranchCondition {TaggedEquals { equal: bool },/// The input is a tagged value: is it an int32?Int32Value,+ /// The input is a tagged value: is it an int32 or a boolean, whose low+ /// 32 bits are its ToInt32 either way?+ Int32OrBoolean,/// The input is a tagged value: is it a double, a number that is no/// int32?Double,@@ -941,7 +944,8 @@ pub enum Op {},// Numbers and strings./// The `Repr::Int32` of the tagged input 0, which must be an int32: a- /// branch on `BranchCondition::Int32Value` decides that first.+ /// branch on `BranchCondition::Int32Value` decides that first. Of a+ /// boolean (see `BranchCondition::Int32OrBoolean`), it is 0 or 1.UnboxInt32,/// The absolute value of the `Repr::Int32` input 0, which must not be/// `i32::MIN`, as a `Repr::Int32`.diff --git a/Libraries/LibJS/JIT/Rust/src/passes/facts.rs b/Libraries/LibJS/JIT/Rust/src/passes/facts.rsindex 9876a33bad9..5f558288a62 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/facts.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/facts.rs@@ -161,10 +161,11 @@ impl Kinds {const DOUBLE: Self = Self(1 << 1);const STRING: Self = Self(1 << 2);const OBJECT: Self = Self(1 << 3);- /// Every other kind of value: `undefined`, `null`, booleans, symbols- /// and big integers.+ /// Every other kind of value: `undefined`, `null`, symbols and big+ /// integers.const OTHER: Self = Self(1 << 4);- const ALL: Self = Self((1 << 5) - 1);+ const BOOLEAN: Self = Self(1 << 5);+ const ALL: Self = Self((1 << 6) - 1);const NUMBER: Self = Self(Self::INT32.0 | Self::DOUBLE.0);fn of(kind: ValueKind) -> Self {@@ -197,6 +198,7 @@ impl Kinds {BranchCondition::Object => exactly(Self::OBJECT),BranchCondition::String => exactly(Self::STRING),BranchCondition::Int32Value => exactly(Self::INT32),+ BranchCondition::Int32OrBoolean => exactly(Self(Self::INT32.0 | Self::BOOLEAN.0)),BranchCondition::Double => exactly(Self::DOUBLE),// NB: These also depend on more than the kind: values of the// kind may fail them.@@ -349,6 +351,7 @@ impl Facts {(Some(Repr::Float64), _) => Kinds::NUMBER,(_, Op::BoxInt32) => self.kinds(value).intersect(Kinds::INT32),(_, Op::BoxFloat64) => self.kinds(value).intersect(Kinds::NUMBER),+ (_, Op::BoxBool) => self.kinds(value).intersect(Kinds::BOOLEAN),_ => self.kinds(value),}}@@ -913,6 +916,8 @@ mod tests {Repr::Tagged,Repr::Tagged,Repr::Int32,+ Repr::Tagged,+ Repr::Tagged,] {graph.add_node(crate::ir::Node {op: crate::ir::Op::LoadSlot { slot: 0 },@@ -950,6 +955,29 @@ mod tests {facts.decides(&graph, NodeId(5), BranchCondition::Int32Value),Some(true));+ assert_eq!(+ facts.decides(&graph, NodeId(5), BranchCondition::Int32OrBoolean),+ Some(true)+ );+ // An int32 or a boolean is neither nullish nor an object, and a value+ // that is neither is no int32.+ let integer_or_boolean = NodeId(6);+ facts.set_passed(integer_or_boolean, BranchCondition::Int32OrBoolean);+ assert_eq!(+ facts.decides(&graph, integer_or_boolean, BranchCondition::Nullish),+ Some(false)+ );+ assert_eq!(+ facts.decides(&graph, integer_or_boolean, BranchCondition::Object),+ Some(false)+ );+ assert_eq!(+ facts.decides(&graph, integer_or_boolean, BranchCondition::Int32Value),+ None+ );+ let neither = NodeId(7);+ facts.set_failed(neither, BranchCondition::Int32OrBoolean);+ assert_eq!(facts.decides(&graph, neither, BranchCondition::Int32Value), Some(false));let object = NodeId(2);facts.set_passed(object, BranchCondition::ElementsKind(ElementsKind::Packed));assert!(facts.is_object(object));diff --git a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rsindex 0b5bc322cca..06c389a307d 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs@@ -245,6 +245,9 @@ fn constant_condition(graph: &Graph, condition: BranchCondition, input: NodeId,if condition == BranchCondition::Int32Value && graph.node(input).op == Op::BoxInt32 {return Some(true);}+ if condition == BranchCondition::Int32OrBoolean && matches!(graph.node(input).op, Op::BoxInt32 | Op::BoxBool) {+ return Some(true);+ }let bits = graph.constant_value(input)?;let tag = value::tag(bits);match condition {@@ -260,6 +263,7 @@ fn constant_condition(graph: &Graph, condition: BranchCondition, input: NodeId,BranchCondition::Object => Some(tag == value::OBJECT_TAG),BranchCondition::NonNegativeInt32 => Some(value::as_int32(bits).is_some_and(|integer| integer >= 0)),BranchCondition::Int32Value => Some(tag == value::INT32_TAG),+ BranchCondition::Int32OrBoolean => Some(tag == value::INT32_TAG || tag == value::BOOLEAN_TAG),BranchCondition::Double => Some(value::is_double(bits)),BranchCondition::String => Some(tag == value::STRING_TAG),// NB: Constant strings are made as such, but strings may becomediff --git a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs b/Libraries/LibJS/JIT/Rust/src/passes/verify.rsindex eacc8e28cbe..9c8f685da7c 100644--- a/Libraries/LibJS/JIT/Rust/src/passes/verify.rs+++ b/Libraries/LibJS/JIT/Rust/src/passes/verify.rs@@ -475,6 +475,7 @@ fn branch_input_reprs(condition: crate::ir::BranchCondition) -> Vec<Repr> {| crate::ir::BranchCondition::Object| crate::ir::BranchCondition::NonNegativeInt32| crate::ir::BranchCondition::Int32Value+ | crate::ir::BranchCondition::Int32OrBoolean| crate::ir::BranchCondition::Double| crate::ir::BranchCondition::String| crate::ir::BranchCondition::ElementsKind(_)diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex af244880171..43b97a59bf3 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -298,7 +298,10 @@ pub mod arith_feedback {pub const OTHER: u8 = 1 << 5;/// Equality comparisons where both operands are objects.pub const OBJECT_PAIR: u8 = 1 << 6;- pub const NON_NUMERIC: u8 = STRING | BIG_INT | OTHER | OBJECT_PAIR;+ /// Boolean operands of arithmetic and bitwise operations, which `OTHER`+ /// does not include there.+ pub const BOOLEAN: u8 = 1 << 7;+ pub const NON_NUMERIC: u8 = STRING | BIG_INT | OTHER | OBJECT_PAIR | BOOLEAN;}/// `value_feedback` bits: the types of the values an instruction produced.diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rsindex b2efd1d36b8..1308abb335c 100644--- a/Libraries/LibJS/Rust/generate/layout_table.rs+++ b/Libraries/LibJS/Rust/generate/layout_table.rs@@ -277,6 +277,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {w.constant("ARITH_FEEDBACK_BIGINT", arith_feedback::BIG_INT);w.constant("ARITH_FEEDBACK_OTHER", arith_feedback::OTHER);w.constant("ARITH_FEEDBACK_OBJECT_PAIR", arith_feedback::OBJECT_PAIR);+ w.constant("ARITH_FEEDBACK_BOOLEAN", arith_feedback::BOOLEAN);w.constant("ARITH_FEEDBACK_INT32_AND_DOUBLE", arith_feedback::INT32 | arith_feedback::DOUBLE);w.constant("ARITH_FEEDBACK_INT32_AND_OTHER", arith_feedback::INT32 | arith_feedback::OTHER);offset!(w, "CALL_FEEDBACK_TARGET", CallFeedback, target);diff --git a/Libraries/LibJS/Rust/src/jit/feedback_dump.rs b/Libraries/LibJS/Rust/src/jit/feedback_dump.rsindex ab74060e893..095d49fbcac 100644--- a/Libraries/LibJS/Rust/src/jit/feedback_dump.rs+++ b/Libraries/LibJS/Rust/src/jit/feedback_dump.rs@@ -200,6 +200,7 @@ pub fn describe_feedback(executable: &Executable) -> Option<String> {(arith_feedback::BIG_INT, "BigInt"),(arith_feedback::OTHER, "Other"),(arith_feedback::OBJECT_PAIR, "ObjectPair"),+ (arith_feedback::BOOLEAN, "Boolean"),],);}diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex be2d9f5689a..08a7527cce6 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -371,6 +371,7 @@ const _: () = {assert!(libjs_jit::snapshot::arith_feedback::BIG_INT == crate::layout::feedback::arith_feedback::BIG_INT);assert!(libjs_jit::snapshot::arith_feedback::OTHER == crate::layout::feedback::arith_feedback::OTHER);assert!(libjs_jit::snapshot::arith_feedback::OBJECT_PAIR == crate::layout::feedback::arith_feedback::OBJECT_PAIR);+ assert!(libjs_jit::snapshot::arith_feedback::BOOLEAN == crate::layout::feedback::arith_feedback::BOOLEAN);assert!(libjs_jit::snapshot::call_feedback_flags::POLYMORPHIC== crate::layout::feedback::call_feedback_flags::POLYMORPHICdiff --git a/Libraries/LibJS/Rust/src/layout/feedback.rs b/Libraries/LibJS/Rust/src/layout/feedback.rsindex 38e4c353c79..0702e196d7b 100644--- a/Libraries/LibJS/Rust/src/layout/feedback.rs+++ b/Libraries/LibJS/Rust/src/layout/feedback.rs@@ -54,6 +54,8 @@ pub mod arith_feedback {pub const OTHER: u8 = 1 << 5;/// Equality comparisons where both operands are objects.pub const OBJECT_PAIR: u8 = 1 << 6;+ /// Boolean operands of arithmetic and bitwise operations, which `OTHER` does not include there.+ pub const BOOLEAN: u8 = 1 << 7;}/// The union of the kinds of values produced by an instruction, as `ValueFeedback` bits. The bit for a boxed value isdiff --git a/Tests/LibJS/Runtime/jit/bitwise-booleans.js b/Tests/LibJS/Runtime/jit/bitwise-booleans.jsindex 723e7feb5e3..c486149f3f7 100644--- a/Tests/LibJS/Runtime/jit/bitwise-booleans.js+++ b/Tests/LibJS/Runtime/jit/bitwise-booleans.js@@ -26,3 +26,33 @@ test("compiled bitwise operations take booleans as 0 and 1", () => {for (let i = 0; i < 5; ++i) expect(combine(heap, i)).toEqual(expected(i));expect(jit.exitCount(combine)).toBe(0);});++test("compiled bitwise operations take operands that are sometimes booleans", () => {+ // NB: Like compiled asm.js code, where a local holds an int32 or a comparison.+ const combine = new Function(+ "a",+ "b",+ `let mixed = 0;+ mixed = a & 1 ? a >= b : mixed;+ return [mixed | (a < b), (b & 3) ^ mixed, mixed & (a > 2), ~mixed, mixed | 0, mixed >>> 0];`+ );+ const expected = (a, b) => {+ let mixed = 0;+ mixed = a & 1 ? a >= b : mixed;+ return [mixed | (a < b), (b & 3) ^ mixed, mixed & (a > 2), ~mixed, mixed | 0, mixed >>> 0];+ };+ jit.prepare(combine);+ for (let i = 0; i < 10; ++i) combine(i, 5);+ expect(jit.compile(combine)).toBe(jit.enabled);+ for (let i = 0; i < 10; ++i) expect(combine(i, 5)).toEqual(expected(i, 5));+ expect(jit.exitCount(combine)).toBe(0);++ // Other values still convert like ToInt32 does.+ for (const value of [1.5, -2.75, 2 ** 32 + 3, undefined, null, "7", {}]) {+ const bitwise = new Function("v", "w", "return [v | w, v & 7, v ^ w, ~v, v | 0];");+ jit.prepare(bitwise);+ for (let i = 0; i < 10; ++i) bitwise(i & 1 ? i > 4 : i, i < 3);+ jit.compile(bitwise);+ expect(bitwise(value, true)).toEqual([value | true, value & 7, value ^ true, ~value, value | 0]);+ }+});