Skip to content

Truly independent web browser

LibJS/JIT: Fold int32 checks of boxed doubles of int32 values

Andreas Klingcfe33a1abe04
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index 7862ffeaa1c..3746508375d 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -310,10 +310,11 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i
|---|---|
| Trivial phi removal | Phis whose inputs are all one value (or the phi itself) become that value. |
| Truncation | Int32 additions, subtractions and unsigned right shifts whose users only read the low 32 bits of their results (bitwise operations, shifts, wrapping arithmetic, integer typed array stores) and no frame state holds become `Int32WrappingBinary`, which never exits. Comparisons of uint32 values (`x >>> 0`) first become unsigned comparisons of the shifted values. |
-| Simplification | Constant folding and strength reduction; branches on constants become jumps; unboxing a box is the boxed value; int32 operations that leave their left-hand side as it is, like `x \| 0`, become it. |
+| Simplification | Constant folding and strength reduction; branches on constants become jumps; unboxing a box is the boxed value, and so is an int32 check of the boxed double of an int32; int32 operations that leave their left-hand side as it is, like `x \| 0`, become it. |
| Representation selection | Phis of int32, bool or float64 values become unboxed phis, and int32 inputs of float64 phis are converted. Tagged inputs of an int32 phi that the code checks somewhere (or of the phis connected to it) are checked where they enter, exiting to the start of the phi's block, unless such a check exited there before. |
| Simplification | Again, for what unboxing made foldable. |
| Check elimination | A forward dataflow of facts removes checks that passed already and reuses loads whose value is known; may turn shape checks into `AssumeValid` plus a stable shape dependency. |
+| Simplification | Again, for what the branches that check elimination decided left foldable (like the double path of `Math.abs` of an int32). |
| Global value numbering | A CSE-able node computing what a dominating node computed is replaced by it. Memory reads are left to check elimination. |
| Loop invariant code motion | Gives loops entered from several blocks a merged preheader, then moves effect-free nodes with inputs from outside the loop to it; nodes that may exit only from blocks every iteration runs, exiting to the loop header, and not where that kind of exit was taken there before. |
| Bounds check elimination | Bounds checks against constant counts go away where the index is known to be in range: induction variables of loops whose back edge compares the grown counter with a constant bound, merges of values from outside a loop, and int32 arithmetic on them that cannot overflow. The counter an on-stack replacement entry brings is checked against the bound once where it enters, unless such a check exited there before. With the same ranges, `((x << k) + c) >> k` becomes `x + (c >> k)` where nothing overflows and `c` is a multiple of 2^k. Int32 additions, subtractions and multiplications become `Int32WrappingBinary` where the ranges of their operands keep the result an int32 (and not -0), with operands also bounded by the masks and shifts of any int32, and by the dominating bounds checks and in-bounds branches of indices. |
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
index b4662d6362f..2c8aac0ad4c 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
@@ -68,6 +68,10 @@ const PASSES: &[Pass] = &[
name: "check elimination",
run: check_elimination::run,
},
+ Pass {
+ name: "simplification",
+ run: simplify::run_after_check_elimination,
+ },
Pass {
name: "global value numbering",
run: gvn::run,
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
index 23720f56579..85af88eae4f 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/simplify.rs
@@ -296,7 +296,22 @@ fn constant_number(graph: &Graph, node: NodeId) -> Option<f64> {
}
pub fn run(graph: &mut Graph) {
- let dissolvable = dissolvable_checks(graph);
+ simplify(graph, true);
+}
+
+/// Simplification after check elimination, which keeps every int32 check of
+/// a double: check elimination may have relied on what one established, like
+/// the int32 that an unboxing in a branch it decided takes.
+pub fn run_after_check_elimination(graph: &mut Graph) {
+ simplify(graph, false);
+}
+
+fn simplify(graph: &mut Graph, dissolve_checks: bool) {
+ let dissolvable = if dissolve_checks {
+ dissolvable_checks(graph)
+ } else {
+ BitSet::new(0)
+ };
let dissolved_double = |graph: &Graph, value: NodeId| {
// NB: Nodes the pass adds are no checks.
(value.index() < dissolvable.capacity() && dissolvable.contains(value.index()))
@@ -413,6 +428,13 @@ pub fn run(graph: &mut Graph) {
let boolean = if bits != 0 { value::TRUE } else { value::FALSE };
constants.get(graph, boolean)
}),
+ // NB: Boxing the double of an int32 boxes the int32.
+ Op::CheckInt32
+ if let Some(double) = boxed(Op::BoxFloat64, inputs[0])
+ && graph.node(double).op == Op::Int32ToFloat64 =>
+ {
+ Some(graph.node(double).inputs[0])
+ }
Op::CheckInt32 => constant_bits(graph, 0)
.filter(|bits| graph.node(inputs[0]).repr == Some(Repr::Tagged) && value::as_int32(*bits).is_some())
.map(|bits| constants.get_typed(graph, bits & u64::from(u32::MAX), Repr::Int32)),
@@ -599,6 +621,12 @@ pub fn run(graph: &mut Graph) {
// unboxing of a boxing of it, which keep the check. The other users
// of the checks that go took the doubles above, and frame states
// take them now.
+ if dissolvable.is_empty() {
+ if changed_control_flow {
+ edit::clean_up_control_flow(graph);
+ }
+ return;
+ }
let still_dissolvable = dissolvable_checks(graph);
let mut replacements = Replacements::new(graph);
let mut removed = BitSet::new(graph.nodes.len());