Prune unnecessary labels in output

This commit is contained in:
Joe Savona
2022-12-14 16:42:54 -08:00
parent de3952352b
commit 6efac28677
59 changed files with 184 additions and 97 deletions
+2
View File
@@ -23,6 +23,7 @@ import { inferTypes } from "./InferTypes";
import { logHIRFunction } from "./logger";
import { printReactiveFunction } from "./PrintReactiveFunction";
import { propagateScopeDependencies } from "./PropagateScopeDependencies";
import { pruneUnusedLabels } from "./PruneUnusedLabels";
export type CompilerFlags = {
eliminateRedundantPhi: boolean;
@@ -89,6 +90,7 @@ export default function (
if (flags.codegen) {
const reactiveFunction = buildReactiveFunction(ir);
pruneUnusedLabels(reactiveFunction);
flattenReactiveLoops(reactiveFunction);
propagateScopeDependencies(reactiveFunction);
const scopes = printReactiveFunction(reactiveFunction);
@@ -0,0 +1,83 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { assertExhaustive } from "../Common/utils";
import {
BlockId,
ReactiveBasicBlock,
ReactiveFunction,
ReactiveTerminal,
} from "./HIR";
/**
* Prunes terminal labels that are never explicitly jumped to.
*/
export function pruneUnusedLabels(fn: ReactiveFunction): void {
const labels: Labels = new Set();
visitBlock(labels, fn.body);
}
type Labels = Set<BlockId>;
function visitBlock(labels: Labels, block: ReactiveBasicBlock): void {
for (const item of block) {
if (item.kind === "terminal") {
// first visit the terminal's contents, which is the only place that can
// reference the terminal's label
visitTerminal(labels, item.terminal);
// if the label wasn't referenced by a break/continue, we can prune it
if (item.label !== null && !labels.has(item.label)) {
item.label = null;
}
} else if (item.kind === "block") {
visitBlock(labels, item.instructions);
}
}
}
function visitTerminal(labels: Labels, terminal: ReactiveTerminal): void {
switch (terminal.kind) {
case "break":
case "continue": {
if (terminal.label !== null) {
labels.add(terminal.label);
}
break;
}
case "for": {
visitBlock(labels, terminal.loop);
break;
}
case "if": {
visitBlock(labels, terminal.consequent);
if (terminal.alternate !== null) {
visitBlock(labels, terminal.alternate);
}
break;
}
case "return":
case "throw": {
break;
}
case "switch": {
for (const case_ of terminal.cases) {
visitBlock(labels, case_.block!);
}
break;
}
case "while": {
visitBlock(labels, terminal.loop);
break;
}
default: {
assertExhaustive(
terminal,
`Unexpected terminal kind '${(terminal as any).kind}'`
);
}
}
}
@@ -69,7 +69,7 @@ function Component$0(props$3) {
const a$4 = [];
a$4.push(props$3.a);
bb2: if (props$3.b) {
if (props$3.b) {
a$4.push(props$3.d);
return a$4;
}
@@ -92,7 +92,7 @@ function foo$0(cond$7) {
b$9 = {};
c$10 = {};
bb2: while (cond$7) {
while (cond$7) {
const z$13 = a$8;
a$8 = b$9;
b$9 = c$10;
@@ -62,9 +62,9 @@ function foo(
```javascript
function foo$0(a$4, b$5, c$6) {
bb1: if (a$4) {
if (a$4) {
while (b$5) {
bb7: if (c$6) {
if (c$6) {
break;
}
}
@@ -189,21 +189,21 @@ function Component$0(props$29) {
} else {
}
bb2: for (let i$36 = 0; i$36 < items$30.length; i$36 = i$36 + 1, i$36) {
for (let i$36 = 0; i$36 < items$30.length; i$36 = i$36 + 1, i$36) {
const item$40 = items$30.at(i$36);
bb9: if (item$40 == null) {
if (item$40 == null) {
} else {
}
bb6: if (seen$33.has(item$40)) {
if (seen$33.has(item$40)) {
continue;
}
seen$33.add(item$40);
renderedItems$32.push(<div>{item$40}</div>);
bb12: if (renderedItems$32.length >= max$35) {
if (renderedItems$32.length >= max$35) {
break;
}
}
@@ -124,7 +124,7 @@ function Component$0(props$4) {
a_DEBUG$5 = [];
a_DEBUG$5.push(props$4.a);
bb1: if (props$4.b) {
if (props$4.b) {
return null;
}
@@ -191,7 +191,7 @@ function Component$0(props$3) {
a$4 = [];
a$4.push(props$3.a);
bb1: if (props$3.b) {
if (props$3.b) {
a$4.push(props$3.c);
}
@@ -262,7 +262,7 @@ function Component$0(props$4) {
a$5 = [];
a$5.push(props$4.a);
bb1: if (props$4.b) {
if (props$4.b) {
a$5.push(props$4.c);
return null;
}
@@ -332,7 +332,7 @@ function Component$0(props$3) {
a$4 = [];
a$4.push(props$3.a);
bb1: if (props$3.b) {
if (props$3.b) {
a$4.push(props$3.c);
return a$4;
}
@@ -400,7 +400,7 @@ function Component$0(props$3) {
const a$4 = [];
a$4.push(props$3.a);
bb2: if (props$3.b) {
if (props$3.b) {
a$4.push(props$3.d);
return a$4;
}
@@ -92,11 +92,11 @@ function Component$0(props$6) {
a$7 = [];
const b$8 = [];
bb1: if (b$8) {
if (b$8) {
a$7.push(props$6.p0);
}
bb3: if (props$6.p1) {
if (props$6.p1) {
b$8.push(props$6.p2);
}
@@ -188,11 +188,11 @@ function Component$0(props$8) {
a$9 = [];
const b$10 = [];
bb1: if (mayMutate$4(b$10)) {
if (mayMutate$4(b$10)) {
a$9.push(props$8.p0);
}
bb3: if (props$8.p1) {
if (props$8.p1) {
b$10.push(props$8.p2);
}
@@ -94,11 +94,11 @@ function foo$0(a$6, b$7) {
if (c_2 || c_3) {
const y$10 = [];
bb1: if (x$8.length) {
if (x$8.length) {
y$10.push(x$8);
}
bb3: if (b$7) {
if (b$7) {
y$10.push(b$7);
}
@@ -88,7 +88,7 @@ function foo$0(x$6, y$7, z$8) {
if (c_3 || c_4) {
items2$10 = [];
bb1: if (x$6) {
if (x$6) {
items2$10.push(y$7);
}
@@ -99,7 +99,7 @@ function foo$0(x$6, y$7, z$8) {
items2$10 = $[5];
}
bb3: if (y$7) {
if (y$7) {
items$9.push(x$6);
}
@@ -87,7 +87,7 @@ function foo$0(a$7, b$8, c$9) {
if (c_0 || c_1 || c_2) {
const x$10 = [];
bb1: if (a$7) {
if (a$7) {
if (b$8) {
if (c$9) {
x$10.push(0);
@@ -101,7 +101,7 @@ function foo$0(a$7, b$8, c$9) {
} else {
}
bb7: if (a$7.length) {
if (a$7.length) {
return a$7;
}
@@ -86,7 +86,7 @@ function Component$0(props$7) {
if (c_0 || c_1) {
a$11 = undefined;
bb1: if (cond$8) {
if (cond$8) {
a$11 = x$9;
} else {
a$11 = [];
@@ -178,7 +178,7 @@ function Component$0(props$8) {
a$9 = compute$3(props$8.a);
const b$10 = compute$3(props$8.b);
bb1: if (props$8.c) {
if (props$8.c) {
mutate$5(a$9);
mutate$5(b$10);
}
@@ -148,7 +148,7 @@ function Component$0(props$8) {
a$9 = compute$3(props$8.a);
b$10 = compute$3(props$8.b);
bb1: if (props$8.c) {
if (props$8.c) {
foo$5(a$9, b$10);
}
@@ -64,7 +64,7 @@ function foo$0(a$5, b$6, c$7) {
if (c_0 || c_1 || c_2) {
const y$8 = [];
bb1: if (a$5) {
if (a$5) {
if (b$6) {
y$8.push(c$7);
}
@@ -79,7 +79,7 @@ function And$0() {
if (c_1) {
t2$6 = undefined;
bb1: if (t0$5) {
if (t0$5) {
t2$6 = g$4();
} else {
t2$6 = t0$5;
@@ -155,7 +155,7 @@ function Or$0() {
if (c_1) {
t2$6 = undefined;
bb1: if (t0$5) {
if (t0$5) {
t2$6 = t0$5;
} else {
t2$6 = g$4();
@@ -236,7 +236,7 @@ function QuestionQuestion$0(props$8) {
if (c_1) {
t2$12 = undefined;
bb1: if (t0$9 != null) {
if (t0$9 != null) {
t2$12 = t0$9;
} else {
t2$12 = g$7();
@@ -184,7 +184,7 @@ function Component$0(props$12) {
let b$20 = {};
let c$22 = {};
let d$24 = {};
bb2: while (true) {
while (true) {
const z$19 = a$18;
a$18 = b$20;
b$20 = c$22;
@@ -192,21 +192,21 @@ function Component$0(props$12) {
d$24 = z$19;
mutate$7(a$18, b$20);
bb4: if (cond$8(a$18)) {
if (cond$8(a$18)) {
break;
}
}
bb7: if (a$18) {
if (a$18) {
}
bb9: if (b$20) {
if (b$20) {
}
bb11: if (c$22) {
if (c$22) {
}
bb13: if (d$24) {
if (d$24) {
}
mutate$7(d$24, null);
@@ -157,19 +157,19 @@ function Component$0(props$10) {
x$15.b = b$12;
const y$16 = mutate$8(x$15, d$14);
bb1: if (a$11) {
if (a$11) {
}
bb3: if (b$12) {
if (b$12) {
}
bb5: if (c$13) {
if (c$13) {
}
bb7: if (d$14) {
if (d$14) {
}
bb9: if (y$16) {
if (y$16) {
}
mutate$8(x$15, null);
@@ -173,24 +173,24 @@ function Component$0(props$11) {
const d$15 = {};
bb2: while (true) {
while (true) {
mutate$6(a$12, b$13);
bb4: if (cond$7(a$12)) {
if (cond$7(a$12)) {
break;
}
}
bb7: if (a$12) {
if (a$12) {
}
bb9: if (b$13) {
if (b$13) {
}
bb11: if (c$14) {
if (c$14) {
}
bb13: if (d$15) {
if (d$15) {
}
mutate$6(d$15, null);
@@ -61,7 +61,7 @@ function foo$0(a$6, b$7, c$8) {
const x$9 = [];
const y$10 = [];
bb1: if (x$9) {
if (x$9) {
}
y$10.push(a$6);
@@ -83,7 +83,7 @@ function foo$0(a$8, b$9, c$10) {
if (c_0 || c_1 || c_2) {
x$11 = [];
bb1: if (a$8) {
if (a$8) {
const c_4 = $[4] !== b$9;
const c_5 = $[5] !== c$10;
let y$12;
@@ -91,7 +91,7 @@ function foo$0(a$8, b$9, c$10) {
if (c_4 || c_5) {
y$12 = [];
bb3: if (b$9) {
if (b$9) {
y$12.push(c$10);
}
@@ -68,7 +68,7 @@ function foo$0(a$6, b$7, c$8) {
const x$9 = [];
const y$10 = [];
bb2: while (c$8) {
while (c$8) {
y$10.push(b$7);
x$9.push(a$6);
}
@@ -75,10 +75,10 @@ function foo$0(a$6, b$7, c$8) {
if (c_0 || c_1 || c_2) {
x$9 = [];
bb1: if (a$6) {
if (a$6) {
const y$10 = [];
bb3: if (b$7) {
if (b$7) {
y$10.push(c$8);
}
@@ -80,7 +80,7 @@ function foo$0(a$8, b$9, c$10) {
if (c_0 || c_1 || c_2) {
x$11 = [];
bb1: if (a$8) {
if (a$8) {
const c_4 = $[4] !== b$9;
let y$12;
@@ -74,7 +74,7 @@ function f$0(a$8, b$9) {
if (c_0 || c_1) {
x$10 = [];
bb1: if (a$8.length === 1) {
if (a$8.length === 1) {
if (b$9) {
x$10.push(b$9);
}
@@ -78,7 +78,7 @@ function Component$0(props$6) {
x$7.push(props$6.p0);
const y$8 = x$7;
bb1: if (props$6.p1) {
if (props$6.p1) {
x$7 = [];
}
@@ -125,7 +125,7 @@ function foo$0(a$13, b$14, c$15) {
if (c_0) {
x$16 = [];
bb1: if (a$13) {
if (a$13) {
x$16.push(a$13);
}
@@ -58,7 +58,7 @@ function foo(
```javascript
function foo$0(x$8, y$9) {
const $ = React.useMemoCache();
bb1: if (x$8) {
if (x$8) {
const c_0 = $[0] !== y$9;
let t1$11;
@@ -109,7 +109,7 @@ function Component$0(props$10) {
} else {
}
bb1: if (foo$4()) {
if (foo$4()) {
const c_2 = $[2] !== a$11;
if (c_2) {
@@ -80,11 +80,11 @@ function foo$0() {
x$7 = 1;
const y$8 = 2;
bb1: if (y$8 === 2) {
if (y$8 === 2) {
x$7 = 3;
}
bb3: if (y$8 === 3) {
if (y$8 === 3) {
x$7 = 5;
}
@@ -63,7 +63,7 @@ function foo$0() {
x$5 = 1;
const y$6 = 2;
bb1: if (y$6 === 2) {
if (y$6 === 2) {
x$5 = 3;
}
@@ -75,7 +75,7 @@ function foo$0() {
if (true) {
x$6 = 1;
bb2: for (const i$7 = 0; i$7 < 10; i$7) {
for (const i$7 = 0; i$7 < 10; i$7) {
x$6 = x$6 + 1;
}
@@ -79,7 +79,7 @@ function foo$0() {
if (true) {
x$7 = 1;
bb2: for (let i$8 = 0; i$8 < 10; i$8 = i$8 + 1, i$8) {
for (let i$8 = 0; i$8 < 10; i$8 = i$8 + 1, i$8) {
x$7 = x$7 + 1;
}
@@ -58,7 +58,7 @@ function foo(
function foo$0() {
const x$5 = 1;
const y$6 = 2;
bb1: if (y$6) {
if (y$6) {
const z$7 = x$5 + y$6;
} else {
const z$8 = x$5;
@@ -119,8 +119,8 @@ function foo$0(a$9, b$10, c$11, d$12) {
if (c_0 || c_1 || c_2 || c_3) {
x$18 = undefined;
bb1: if (true) {
bb3: if (true) {
if (true) {
if (true) {
x$18 = a$9;
} else {
x$18 = b$10;
@@ -128,7 +128,7 @@ function foo$0(a$9, b$10, c$11, d$12) {
x$18;
} else {
bb7: if (true) {
if (true) {
x$18 = c$11;
} else {
x$18 = d$12;
@@ -87,9 +87,9 @@ function foo(
```javascript
function foo$0(a$6, b$7, c$8) {
const x$9 = 0;
bb2: while (a$6) {
bb5: while (b$7) {
bb8: while (c$8) {
while (a$6) {
while (b$7) {
while (c$8) {
x$9 + 1;
}
}
@@ -69,8 +69,8 @@ function foo$0(a$5, b$6, c$7) {
if (c_0 || c_1 || c_2) {
const x$8 = a$5;
bb1: if (b$6) {
bb3: if (c$7) {
if (b$6) {
if (c$7) {
x$8 = c$7;
}
@@ -76,7 +76,7 @@ function foo$0(a$7, b$8, c$9, d$10, e$11) {
if (c_0 || c_1 || c_2 || c_3) {
x$12 = null;
bb1: if (a$7) {
if (a$7) {
x$12 = b$8;
} else {
if (c$9) {
@@ -75,7 +75,7 @@ function foo$0() {
x$6 = 1;
let y$7 = 2;
bb1: if (x$6 > 1) {
if (x$6 > 1) {
x$6 = 2;
} else {
y$7 = 3;
@@ -75,7 +75,7 @@ function foo$0(a$7) {
const b$8 = {};
x$9 = b$8;
bb1: if (a$7) {
if (a$7) {
const y$10 = {};
x$9.y = y$10;
} else {
@@ -72,7 +72,7 @@ function foo$0(a$5) {
if (c_0) {
x$6 = {};
bb1: if (a$5) {
if (a$5) {
if (true) {
const y$7 = {};
} else {
@@ -71,7 +71,7 @@ function foo$0(a$6) {
if (c_0) {
x$7 = {};
bb1: if (a$6) {
if (a$6) {
const y$8 = {};
x$7.y = y$8;
} else {
@@ -73,7 +73,7 @@ function foo$0(a$6) {
if (c_0) {
x$7 = {};
bb1: if (a$6) {
if (a$6) {
const y$8 = {};
x$7.y = y$8;
mutate$4(y$8);
@@ -57,7 +57,7 @@ function foo$0() {
if (true) {
x$4 = 1;
bb1: if (x$4 === 1) {
if (x$4 === 1) {
x$4 = 2;
}
@@ -92,7 +92,7 @@ function Foo$0(cond$5) {
if (c_0) {
str$6 = "";
bb1: if (cond$5) {
if (cond$5) {
const str$7 = "other test";
log$4(str$7);
} else {
@@ -115,7 +115,7 @@ function foo(
function foo$0(a$9, b$10, c$11, d$12) {
const $ = React.useMemoCache();
const x$13 = 0;
bb1: if (true) {
if (true) {
const c_0 = $[0] !== a$9;
const c_1 = $[1] !== b$10;
let x$16;
@@ -123,7 +123,7 @@ function foo$0(a$9, b$10, c$11, d$12) {
if (c_0 || c_1) {
x$16 = undefined;
bb3: if (true) {
if (true) {
x$16 = a$9;
} else {
x$16 = b$10;
@@ -145,7 +145,7 @@ function foo$0(a$9, b$10, c$11, d$12) {
if (c_3 || c_4) {
x$20 = undefined;
bb7: if (true) {
if (true) {
x$20 = c$11;
} else {
x$20 = d$12;
@@ -71,7 +71,7 @@ function foo$0() {
if (true) {
y$8 = undefined;
bb1: if (y$5 > 1) {
if (y$5 > 1) {
y$8 = 1;
} else {
y$8 = 2;
@@ -50,7 +50,7 @@ function foo(
function foo$0() {
const x$4 = 1;
const y$5 = 2;
bb1: if (y$5) {
if (y$5) {
const z$6 = x$4 + y$5;
}
}
@@ -56,7 +56,7 @@ function foo$0() {
if (true) {
x$4 = 1;
bb1: if (x$4 === 1) {
if (x$4 === 1) {
x$4 = 2;
}
@@ -58,7 +58,7 @@ function foo(
```javascript
function foo$0() {
const x$5 = 1;
bb2: while (x$5 < 10) {
while (x$5 < 10) {
x$5 + 1;
}
@@ -64,7 +64,7 @@ function foo$0() {
if (true) {
x$5 = 1;
bb2: while (x$5 < 10) {
while (x$5 < 10) {
x$5 = x$5 + 1;
}
@@ -103,7 +103,7 @@ function Component$0(props$8) {
x$9 = [];
let y$10 = undefined;
bb1: switch (props$8.p0) {
switch (props$8.p0) {
case true: {
x$9.push(props$8.p2);
x$9.push(props$8.p3);
@@ -47,7 +47,7 @@ function component(
```javascript
function component$0(a$5, b$6) {
bb1: if (a$5 > b$6) {
if (a$5 > b$6) {
if (true) {
const m$8 = {};
} else {
@@ -73,7 +73,7 @@ function component$0() {
b$8 = $[1];
}
bb1: if (a$7 > b$8) {
if (a$7 > b$8) {
if (true) {
const m$10 = {};
} else {
@@ -100,7 +100,7 @@ function component$0() {
const u$13 = x$12.u;
const v$14 = x$12.v;
bb1: if (u$13 > v$14) {
if (u$13 > v$14) {
}
const y$16 = x$12.u;
@@ -79,7 +79,7 @@ function component$0() {
y$8 = $[1];
}
bb1: if (x$7 > y$8) {
if (x$7 > y$8) {
if (true) {
const z$10 = {};
} else {
@@ -45,7 +45,7 @@ function foo(
```javascript
function foo$0(a$3, b$4) {
bb2: while (a$3) {
while (a$3) {
break;
}
return b$4;
@@ -66,8 +66,8 @@ function foo(
```javascript
function foo$0(a$5, b$6, c$7, d$8) {
bb2: while (a$5) {
bb4: if (b$6) {
while (a$5) {
if (b$6) {
continue;
}
c$7();
+2
View File
@@ -40,6 +40,7 @@ import { leaveSSA } from "./HIR/LeaveSSA";
import printHIR, { printFunction } from "./HIR/PrintHIR";
import { printReactiveFunction } from "./HIR/PrintReactiveFunction";
import { propagateScopeDependencies } from "./HIR/PropagateScopeDependencies";
import { pruneUnusedLabels } from "./HIR/PruneUnusedLabels";
function parseFunctions(
source: string
@@ -81,6 +82,7 @@ export const HIR = {
printFunction,
printHIR,
printReactiveFunction,
pruneUnusedLabels,
};
export default BabelPlugin;