Skip to content

Truly independent web browser

LibJS/JIT: Let passes move and share calls that only compute

Andreas Klingb60155635476
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index e633f668775..7b6c2f86269 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -63,7 +63,9 @@ from them alone:
- **Exits.** `Op::can_eager_exit()`, whether the op has an exit kind (`Op::exit_kind()`): may exit
before its side effects, resuming at the current bytecode (needs the eager frame state).
`can_lazy_exit`: may need to exit after it returns, resuming after the current bytecode.
-- **Call.** `is_call`: calls out of compiled code and clobbers every caller-saved register.
+- **Call.** `is_call`: calls out of compiled code and clobbers every caller-saved register. That
+ is no effect: a call without any of the other effects only computes its value, and passes treat
+ it like any other node that does.
- **Memory.** `reads` and `writes`: sets of abstract locations (`ir::Locations`): `FRAME`,
`NAMED_SLOTS` (property values), `SHAPES` (shapes and prototypes), `ELEMENTS` (element values),
`ELEMENT_COUNTS` (element storage kind, length, capacity and the storage offset of typed arrays), `GLOBAL_BINDINGS` and `BINDINGS` (the
@@ -339,7 +341,7 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i
| Check elimination | A forward dataflow of facts removes checks that passed already, decides branches that a branch on the same values decided before, 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 | Again, for the nodes that check elimination and simplification made the same. |
-| 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 too, exiting to the loop header, and not where that kind of exit was taken there before. Those from blocks that not every iteration runs, but for cold ones, move speculatively: they exit with `Hoisted`, and stay in the loop where such an exit was taken there before. |
+| 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 (but calls in cold blocks); nodes that may exit too, exiting to the loop header, and not where that kind of exit was taken there before. Those from blocks that not every iteration runs, but for cold ones, move speculatively: they exit with `Hoisted`, and stay in the loop where such an 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. |
| Exit unboxing | Frame states take the unboxed value of an int32, float64 or bool boxing that only frame states use, so that exits box it instead of the fast path. |
| Dead code elimination | Removes nodes without uses and without effects; frame states count as uses. Branches whose edges lead to the same place doing the same thing become jumps. |
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
index 8f731875c21..1c41cd3d4c0 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/dce.rs
@@ -24,7 +24,6 @@ fn is_removable(op: &Op) -> bool {
!op.is_control()
&& !op.can_eager_exit()
&& !properties.can_lazy_exit
- && !properties.is_call
&& !properties.allocates
&& properties.writes.is_empty()
&& !properties.runs_code
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs b/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
index e9fec6d6298..4eb5840319d 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/gvn.rs
@@ -34,7 +34,6 @@ fn is_numbered(op: &Op) -> bool {
&& !properties.allocates
&& !properties.runs_code
&& !properties.can_lazy_exit
- && !properties.is_call
&& !matches!(op, Op::Constant(_) | Op::Phi)
&& !op.is_control()
}
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
index 23add0135ce..2b3461ded5a 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/licm.rs
@@ -216,9 +216,11 @@ pub fn run(graph: &mut Graph) {
.inputs
.iter()
.all(|input| block_of[input.index()].is_none_or(|block| !loop_.blocks.contains(block)));
- // NB: Refinements belong to the edge into their block.
+ // NB: Refinements belong to the edge into their block. Calls in
+ // cold blocks stay there, where they rarely run, rather
+ // than run whenever the loop is entered.
let movable = !matches!(data.op, Op::Refine { .. })
- && !properties.is_call
+ && !(properties.is_call && is_cold)
&& !properties.can_lazy_exit
&& !properties.allocates
&& properties.writes.is_empty()
diff --git a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
index dc58d8c9ba4..2b6f932631a 100644
--- a/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/regalloc/tests.rs
@@ -225,7 +225,7 @@ fn swapping_loop_phis_go_through_a_temp_slot() {
Instruction::Mov { dst: l(0), src: c(0) },
Instruction::Mov { dst: l(1), src: c(1) },
Instruction::JumpIf {
- condition: a(0),
+ condition: l(0),
true_target: label(4),
false_target: label(8),
},
@@ -246,41 +246,40 @@ fn swapping_loop_phis_go_through_a_temp_slot() {
assert_eq!(moves.len(), 3, "{moves:?}");
assert!(moves.iter().any(|m| matches!(m.to, Location::Stack(_))), "{moves:?}");
// The loop header starts with the registers of the block entering the
- // loop: the condition stays in r0, with no reload in the loop.
+ // loop: the condition, a phi, stays in r0, with no reload in the loop.
assert_text(
&dump_allocation(&graph, &allocation),
"
b0:
v0 Jump
b1:
- v9 LoadSlot -> r0 spill s0
- moves #0 -> r1, #10 -> r2
+ moves #0 -> r0, #10 -> r1
v6 Jump
b2:
- v7 Phi -> r1
- v8 Phi -> r2
- v10 BranchTruthy (r0)
+ v7 Phi -> r0
+ v8 Phi -> r1
+ v9 BranchTruthy (r0)
b3:
- v11 Jump
+ v10 Jump
b4:
- v12 Jump
+ v11 Jump
b5:
moves r0 -> r1
- v13 ToBoolean (r1) -> r0
- v14 Branch (r0)
+ v12 ToBoolean (r1) -> r0
+ v13 Branch (r0)
b6:
- moves s0 -> r0, s1 -> r1, s2 -> r2
- v15 Jump
+ moves s0 -> r0, s1 -> r1
+ v14 Jump
b7:
- moves s2 -> r2
- v16 Jump
+ moves s1 -> r1
+ v15 Jump
b8:
- moves r2 -> r0
- v17 Return (r0)
+ moves r1 -> r0
+ v16 Return (r0)
b9:
- moves r1 -> s3, r2 -> r1, s3 -> r2
- v18 Jump
- spill slots: 4
+ moves r0 -> s2, r1 -> r0, s2 -> r1
+ v17 Jump
+ spill slots: 3
",
);
}
@@ -309,7 +308,7 @@ fn loop_phis_share_spill_slots_with_their_inputs() {
dst: r(6),
},
Instruction::JumpIf {
- condition: a(0),
+ condition: l(0),
true_target: label(8),
false_target: label(12),
},
diff --git a/Tests/LibJS/JIT/ir/math-and-string-builtins.js b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
index d542a169976..d901f199bcd 100644
--- a/Tests/LibJS/JIT/ir/math-and-string-builtins.js
+++ b/Tests/LibJS/JIT/ir/math-and-string-builtins.js
@@ -103,7 +103,8 @@ jit.compile(absAndSqrt);
console.log(absAndSqrt(-9), absAndSqrt(16));
// Math.exp, log, sin, cos, tan and pow of numbers call the runtime's functions
-// of doubles, which only compute: the code after them keeps what it knew.
+// of doubles, which only compute: the code after them keeps what it knew, and
+// a call of loop invariant numbers moves out of the loop.
function bias(base, values) {
let sum = 0;
@@ -119,6 +120,11 @@ jit.compile(bias);
// CHECK: CallFloat64Log
// CHECK: CallFloat64Pow
// CHECK-NOT: CallSlowPath CallBuiltinMath
+// CHECK-PASS: loop invariant code motion
+// CHECK: CallFloat64Log
+// CHECK: loop:
+// CHECK-NOT: CallFloat64Log
+// CHECK: CallFloat64Pow
function trigonometry(x) {
return Math.sin(x) + Math.cos(x) + Math.tan(x) + Math.exp(x);