dillon1000/react
Commit
Browse files Distinguish identifier id and ssa id
I noticed on @kassens's #771 that despite running LeaveSSA there are still cases
where we still reassign to a unique identifier: functions that have reassignment
but no phi nodes, such as:
```javascript
function foo() {
let x$1 = 0;
x$2 = x$1 + 1;
}
```
Here SSA form rewrote the second statement's LHS, but bc there's no phi node we
can't recover what the original was supposed to be (`x = x + 1`). This was my
oversight when suggesting the simpler LeaveSSA algorithm, it works for
eliminating phis but not other reassignments. The only alternative to removing
SSA form is to add assignment statements, which we obviously don't want to do
since that generates bloat.
This PR addresses the issue by adding an additional, optional property to
`Identifier` called `preSsaId` that starts off null. When entering SSA we save
the original id in this property and update id to a new SSA value. LeaveSSA does
the inverse, setting id = preSsaId and nulling out the latter. This means that
an identifier can always be uniquely identified by its `id` value at any point
in the compiler, while it's trivial to correctly undo SSA form.
```typescript
type Identifier = {
// Unique value for each original identifier
id: IdentifierId;
// The original, un-mangled variable name if this was a variable present in the
source (null if it's generated)
name: string | null;
// When in SSA mode, this is set to the original, pre-SSA `id` value
preSsaId: IdentifierId | null;
}
```Changed paths58 files
First-parent comparisoncompiler/forget/src/HIR/EliminateRedundantPhi.ts ModifiedM compiler/forget/src/HIR/EnterSSA.ts ModifiedM compiler/forget/src/HIR/HIR.ts ModifiedM compiler/forget/src/HIR/HIRBuilder.ts ModifiedM compiler/forget/src/HIR/LeaveSSA.ts ModifiedM compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/call.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/component.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/conditional-break.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/independent.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/jsx-fragment.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/property-assignment.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/simple.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-arrayexpression.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-for-of.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-property-call.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-property.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-while-no-reassign.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/switch.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/while-break.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md ModifiedPatch
Files changed
Rendering syntax-highlighted changes…