Skip to content

Truly independent web browser

LibJS/JIT: Remove what the last value numbering leaves dead

Andreas Kling4e623c5f96ea
diff --git a/Libraries/LibJS/JIT/ARCHITECTURE.md b/Libraries/LibJS/JIT/ARCHITECTURE.md
index 590d8b2328e..b36304b767e 100644
--- a/Libraries/LibJS/JIT/ARCHITECTURE.md
+++ b/Libraries/LibJS/JIT/ARCHITECTURE.md
@@ -415,7 +415,7 @@ unreachable. Each one keeps the graph valid (`passes/verify.rs`), and IR tests i
| Escape analysis | Allocations that do not escape become their properties' SSA values; frame states refer to `VirtualObject`s. `AssumeLegacyArgumentsUnread` nodes after calls whose frame states have virtual objects among legacy arguments become `AssumeValid` nodes; the others go away. |
| Dead code elimination | Again, for what escape analysis left unused. |
| Branch fusion | A branch on a single-use comparison (int32, float64 or tagged) compares itself, after a block that only branches on a phi of booleans hands the branch to its predecessors. |
-| Refinement removal | Every use of a refined value becomes a use of the value itself; `Refine` nodes go away and checks lose their values; value numbering then merges what differed only in refinements (section 3.2). |
+| Refinement removal | Every use of a refined value becomes a use of the value itself; `Refine` nodes go away and checks lose their values; value numbering then merges what differed only in refinements (section 3.2), and dead code elimination removes what only the merged checks' frame states kept alive. |
| Frame initialization | Moves `InitializeFrame` to the first node that observes the frame's slots on each path, and puts `PublishFrame` before nodes that only need the frame visible (allocations, calls in inlined callees) where it may not be yet. |
| Frame state cleanup | Drops the frame states no node uses. |
| Cold block layout | Moves the cold blocks after all others. |
diff --git a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
index 45d843a4e0c..eb9039555a5 100644
--- a/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/passes/mod.rs
@@ -150,6 +150,8 @@ fn run_passes(graph: &mut Graph, layout: &FrameLayout, verify: bool, observe: &m
// same once the refinements are gone, and nothing moves anymore.
edit::remove_refinements(graph);
gvn::run(graph);
+ // NB: Checks that merged took their frame states along, which may have been the last uses of what those refer to.
+ dce::run(graph);
check(graph, layout, verify, "refinement removal");
observe(graph, "refinement removal");
frame_initialization::run(graph, layout);
diff --git a/Tests/LibJS/JIT/ir/dead-code-after-refinement-removal.js b/Tests/LibJS/JIT/ir/dead-code-after-refinement-removal.js
new file mode 100644
index 00000000000..4ce32d92564
--- /dev/null
+++ b/Tests/LibJS/JIT/ir/dead-code-after-refinement-removal.js
@@ -0,0 +1,23 @@
+// REQUIRES: inline-allocation
+//
+// The multiplication's check of value is the last user of the frame state that refers to the object escape analysis
+// removed, and so of the phi of its property. Only the value numbering after loop invariant code motion merges it with
+// the check hoisted out of the loop, which leaves the phi unused. It must not stay in a register across the call.
+
+function phiOfVirtualObjectProperty(flag, value) {
+ let number = 1;
+ for (let i = 0; i < 4; ++i) number = value;
+ const object = { a: 1 };
+ if (flag) object.a = number;
+ object.b = value * number;
+ return flag / flag;
+}
+jit.prepare(phiOfVirtualObjectProperty);
+for (let i = 0; i < 10; ++i) phiOfVirtualObjectProperty(i & 1 ? {} : null, 1.5);
+jit.compile(phiOfVirtualObjectProperty);
+
+// CHECK-FUNCTION: phiOfVirtualObjectProperty
+// CHECK-PASS: branch fusion
+// CHECK: = Phi (#1, #1,
+// CHECK-PASS: refinement removal
+// CHECK-NOT: = Phi (#1, #1,