dillon1000/react
Commit
Browse files Constant propagation/folding
Implements constant propagation/constant folding for a conservative subset of the language. The approach is described in detail in the comments in the file itself, a key note here is that this pass currently emits what looks like garbage: ``` // input const x = 1; const y = x + 1; // output const x = 1; 2; // <---- you'll see a bunch of lines like this const y = 2; ``` These useless lines occur where previously there was a temporary getting calculated that was used later (so we saved it until it was used), but now it isn't used later so we just emit it in-place. Dead code elimination (DCE) can eliminate these and other useless statements later. Note that a key motivation for implementing this pass is to reduce memoization blocks to what is strictly required for dynamic computations. Why memoize at runtime when we compute at build time?
Changed paths30 files
First-parent comparisoncompiler/forget/src/CompilerPipeline.ts ModifiedM compiler/forget/src/HIR/HIRBuilder.ts ModifiedM compiler/forget/src/HIR/index.ts ModifiedA compiler/forget/src/Optimization/ConstantPropagation.ts AddedA compiler/forget/src/Optimization/index.ts AddedM compiler/forget/src/__tests__/fixtures/hir/_bug_expression-with-assignment.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md ModifiedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-for.expect.md AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-for.js AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-phi.expect.md AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-phi.js AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-while.expect.md AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation-while.js AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation.expect.md AddedA compiler/forget/src/__tests__/fixtures/hir/constant-propagation.js AddedM compiler/forget/src/__tests__/fixtures/hir/object-computed-access-assignment.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-trivial-update.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-multiple-phis.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-nested-loops-no-reassign.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-objectexpression-phi.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-sibling-phis.expect.md ModifiedM compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.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 ModifiedPatch
Files changed
Rendering syntax-highlighted changes…