1# deadCodeElimination23## File4`src/Optimization/DeadCodeElimination.ts`56## Purpose7Eliminates instructions whose values are unused, reducing generated code size. The pass performs mark-and-sweep analysis to identify and remove dead code while preserving side effects and program semantics.89## Input Invariants10- Must run after `InferMutationAliasingEffects` because "dead" code may still affect effect inference11- HIR is in SSA form with phi nodes12- Unreachable blocks are already pruned during HIR construction1314## Output Guarantees15- All instructions with unused lvalues (that are safe to prune) are removed16- Unused phi nodes are deleted17- Unused context variables are removed from `fn.context`18- Destructuring patterns are rewritten to remove unused bindings19- `StoreLocal` instructions with unused initializers are converted to `DeclareLocal`2021## Algorithm22Two-phase mark-and-sweep with fixed-point iteration for loops:2324**Phase 1: Mark (findReferencedIdentifiers)**251. Detect if function has back-edges (loops)262. Iterate blocks in reverse postorder (successors before predecessors) to visit usages before declarations273. For each block:28 - Mark all terminal operands as referenced29 - Process instructions in reverse order:30 - If lvalue is used OR instruction is not pruneable, mark the lvalue and all operands as referenced31 - Special case for `StoreLocal`: only mark initializer if the SSA lvalue is actually read32 - Mark phi operands if the phi result is used334. If loops exist and new identifiers were marked, repeat until fixed point3435**Phase 2: Sweep**361. Remove unused phi nodes from each block372. Remove instructions with unused lvalues using `retainWhere`383. Rewrite retained instructions:39 - **Array destructuring**: Replace unused elements with holes, truncate trailing holes40 - **Object destructuring**: Remove unused properties (only if rest element is unused or absent)41 - **StoreLocal**: Convert to `DeclareLocal` if initializer value is never read424. Remove unused context variables4344## Key Data Structures45- **State class**: Tracks referenced identifiers46 - `identifiers: Set<IdentifierId>` - SSA-specific usages47 - `named: Set<string>` - Named variable usages (any version)48 - `isIdOrNameUsed()` - Checks if identifier or any version of named variable is used49 - `isIdUsed()` - Checks if specific SSA id is used50- **hasBackEdge/findBlocksWithBackEdges**: Detect loops requiring fixed-point iteration5152## Edge Cases53- **Preserved even if unused:**54 - `debugger` statements (to not break debugging workflows)55 - Call expressions and method calls (may have side effects)56 - Await expressions57 - Store operations (ComputedStore, PropertyStore, StoreGlobal)58 - Delete operations (ComputedDelete, PropertyDelete)59 - Iterator operations (GetIterator, IteratorNext, NextPropertyOf)60 - Context operations (LoadContext, DeclareContext, StoreContext)61 - Memoization markers (StartMemoize, FinishMemoize)6263- **SSR mode special case:**64 - In SSR mode, unused `useState`, `useReducer`, and `useRef` hooks can be removed6566- **Object destructuring with rest:**67 - Cannot remove unused properties if rest element is used (would change rest's value)6869- **Block value instructions:**70 - Last instruction of value blocks (not 'block' kind) is never pruned as it's the block's value7172## TODOs73- "TODO: we could be more precise and make this conditional on whether any arguments are actually modified" (for mutating instructions)7475## Example7677**Input:**78```javascript79function Component(props) {80 const _ = 42;81 return props.value;82}83```8485**After DeadCodeElimination:**86The `const _ = 42` assignment is removed since `_` is never used:87```javascript88function Component(props) {89 return props.value;90}91```9293**Array destructuring example:**9495Input:96```javascript97function foo(props) {98 const [x, unused, y] = props.a;99 return x + y;100}101```102103Output (middle element becomes a hole):104```javascript105function foo(props) {106 const [x, , y] = props.a;107 return x + y;108}109```
Findings
✓ No findings reported for this file.