@samitouri / QOS-React-2 / commits / 6bf5a1b396

[codegen] Elide final return stmt if no value

This is a random driveby improvement. I realized that we can eliminate `return` statements if a) they have no value and b) they are in the top-level block. Functions implicitly return at that point — there can't be any succeeding instructions anyway — so we can save bytes in the output.

Joe Savona committed Nov 22, 2022 at 08:03 UTC 6bf5a1b39647c4e4a2ca2874e827073472e259a1
35 files changed +46 -137
compiler/forget/src/HIR/Codegen.ts
+8
@@ -94,9 +94,11 @@ class CodegenVisitor
94 t.SwitchCase
95 >
96 {
97 + depth: number = 0;
98 temp: Map<IdentifierId, t.Expression> = new Map();
99
100 enterBlock(): t.Statement[] {
101 + this.depth++;
102 return [];
103 }
104 visitValue(
@@ -190,6 +192,11 @@ class CodegenVisitor
192 case "return": {
193 if (terminal.value !== null) {
194 return t.returnStatement(terminal.value);
195 + } else if (this.depth === 1) {
196 + // A return at the top-level of a function must be the last instruction,
197 + // and functions implicitly return after the last instruction of the top-level.
198 + // Elide the return.
199 + return t.emptyStatement();
200 } else {
201 return t.returnStatement();
202 }
@@ -223,6 +230,7 @@ class CodegenVisitor
230 }
231 }
232 leaveBlock(block: t.Statement[]): t.Statement {
233 + this.depth--;
234 return t.blockStatement(block);
235 }
236 }
compiler/forget/src/__tests__/fixtures/hir/alias-while.expect.md
+1 -3
@@ -140,9 +140,7 @@ flowchart TB
140 ## Code
141
142 ```javascript
143 -function mutate$0(x$1, y$2) {
144 - return;
145 -}
143 +function mutate$0(x$1, y$2) {}
144
145 ```
146
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md
-2
@@ -60,7 +60,6 @@ function f$0() {
60 x$1 = x$1 + 1;
61 x$1 = x$1 + 1;
62 x$1 = x$1 >>> 1;
63 - return;
63 }
64
65 ```
@@ -100,7 +99,6 @@ flowchart TB
99 function g$0(a$1) {
100 a$1.c.b = a$1.b.c + 1;
101 a$1.c.b = a$1.b.c * 2;
103 - return;
102 }
103
104 ```
compiler/forget/src/__tests__/fixtures/hir/call.expect.md
+1 -3
@@ -38,9 +38,7 @@ flowchart TB
38 ## Code
39
40 ```javascript
41 -function foo$0() {
42 - return;
43 -}
41 +function foo$0() {}
42
43 ```
44 ## HIR
compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md
-1
@@ -83,7 +83,6 @@ function foo$0(a$1, b$2, c$3) {
83 bb7: if (c$3) break;
84 }
85 }
86 - return;
86 }
87
88 ```
compiler/forget/src/__tests__/fixtures/hir/conditional-on-mutable.expect.md
+2 -6
@@ -260,9 +260,7 @@ flowchart TB
260 ## Code
261
262 ```javascript
263 -function Foo$0() {
264 - return;
265 -}
263 +function Foo$0() {}
264
265 ```
266 ## HIR
@@ -288,9 +286,7 @@ flowchart TB
286 ## Code
287
288 ```javascript
291 -function mayMutate$0() {
292 - return;
293 -}
289 +function mayMutate$0() {}
290
291 ```
292
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/constructor.expect.md
+1 -3
@@ -38,9 +38,7 @@ flowchart TB
38 ## Code
39
40 ```javascript
41 -function Foo$0() {
42 - return;
43 -}
41 +function Foo$0() {}
42
43 ```
44 ## HIR
compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md
+2 -7
@@ -52,7 +52,6 @@ function Component$0() {
52 const b$2 = a$1;
53 useFreeze$3(a$1);
54 foo$4(b$2);
55 - return;
55 }
56
57 ```
@@ -79,9 +78,7 @@ flowchart TB
78 ## Code
79
80 ```javascript
82 -function useFreeze$0() {
83 - return;
84 -}
81 +function useFreeze$0() {}
82
83 ```
84 ## HIR
@@ -107,9 +104,7 @@ flowchart TB
104 ## Code
105
106 ```javascript
110 -function foo$0(x$1) {
111 - return;
112 -}
107 +function foo$0(x$1) {}
108
109 ```
110
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md
+2 -6
@@ -42,9 +42,7 @@ flowchart TB
42 ## Code
43
44 ```javascript
45 -function useFreeze$0() {
46 - return;
47 -}
45 +function useFreeze$0() {}
46
47 ```
48 ## HIR
@@ -70,9 +68,7 @@ flowchart TB
68 ## Code
69
70 ```javascript
73 -function foo$0() {
74 - return;
75 -}
71 +function foo$0() {}
72
73 ```
74 ## HIR
compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-arguments.expect.md
+2 -6
@@ -80,9 +80,7 @@ flowchart TB
80 ## Code
81
82 ```javascript
83 -function useFreeze$0(x$1) {
84 - return;
85 -}
83 +function useFreeze$0(x$1) {}
84
85 ```
86 ## HIR
@@ -108,9 +106,7 @@ flowchart TB
106 ## Code
107
108 ```javascript
111 -function call$0(x$1) {
112 - return;
113 -}
109 +function call$0(x$1) {}
110
111 ```
112
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md
+2 -6
@@ -132,9 +132,7 @@ flowchart TB
132 ## Code
133
134 ```javascript
135 -function useFreeze$0(x$1) {
136 - return;
137 -}
135 +function useFreeze$0(x$1) {}
136
137 ```
138 ## HIR
@@ -160,9 +158,7 @@ flowchart TB
158 ## Code
159
160 ```javascript
163 -function call$0(x$1) {
164 - return;
165 -}
161 +function call$0(x$1) {}
162
163 ```
164
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/independent-across-if.expect.md
+4 -12
@@ -56,9 +56,7 @@ flowchart TB
56 ## Code
57
58 ```javascript
59 -function compute$0() {
60 - return;
61 -}
59 +function compute$0() {}
60
61 ```
62 ## HIR
@@ -84,9 +82,7 @@ flowchart TB
82 ## Code
83
84 ```javascript
87 -function mutate$0() {
88 - return;
89 -}
85 +function mutate$0() {}
86
87 ```
88 ## HIR
@@ -112,9 +108,7 @@ flowchart TB
108 ## Code
109
110 ```javascript
115 -function foo$0() {
116 - return;
117 -}
111 +function foo$0() {}
112
113 ```
114 ## HIR
@@ -140,9 +134,7 @@ flowchart TB
134 ## Code
135
136 ```javascript
143 -function Foo$0() {
144 - return;
145 -}
137 +function Foo$0() {}
138
139 ```
140 ## HIR
compiler/forget/src/__tests__/fixtures/hir/independent.expect.md
+3 -9
@@ -85,9 +85,7 @@ flowchart TB
85 ## Code
86
87 ```javascript
88 -function compute$0() {
89 - return;
90 -}
88 +function compute$0() {}
89
90 ```
91 ## HIR
@@ -113,9 +111,7 @@ flowchart TB
111 ## Code
112
113 ```javascript
116 -function foo$0() {
117 - return;
118 -}
114 +function foo$0() {}
115
116 ```
117 ## HIR
@@ -141,9 +137,7 @@ flowchart TB
137 ## Code
138
139 ```javascript
144 -function Foo$0() {
145 - return;
146 -}
140 +function Foo$0() {}
141
142 ```
143
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/interdependent-across-if.expect.md
+3 -9
@@ -50,9 +50,7 @@ flowchart TB
50 ## Code
51
52 ```javascript
53 -function compute$0() {
54 - return;
55 -}
53 +function compute$0() {}
54
55 ```
56 ## HIR
@@ -78,9 +76,7 @@ flowchart TB
76 ## Code
77
78 ```javascript
81 -function foo$0() {
82 - return;
83 -}
79 +function foo$0() {}
80
81 ```
82 ## HIR
@@ -106,9 +102,7 @@ flowchart TB
102 ## Code
103
104 ```javascript
109 -function Foo$0() {
110 - return;
111 -}
105 +function Foo$0() {}
106
107 ```
108 ## HIR
compiler/forget/src/__tests__/fixtures/hir/interdependent.expect.md
+3 -9
@@ -88,9 +88,7 @@ flowchart TB
88 ## Code
89
90 ```javascript
91 -function compute$0() {
92 - return;
93 -}
91 +function compute$0() {}
92
93 ```
94 ## HIR
@@ -116,9 +114,7 @@ flowchart TB
114 ## Code
115
116 ```javascript
119 -function foo$0() {
120 - return;
121 -}
117 +function foo$0() {}
118
119 ```
120 ## HIR
@@ -144,9 +140,7 @@ flowchart TB
140 ## Code
141
142 ```javascript
147 -function Foo$0() {
148 - return;
149 -}
143 +function Foo$0() {}
144
145 ```
146
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/inverted-if.expect.md
-2
@@ -75,8 +75,6 @@ function foo$0(a$1, b$2, c$3) {
75 y$4.push(c$3);
76 }
77 }
78 -
79 - return;
78 }
79
80 ```
compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md
+2 -6
@@ -244,9 +244,7 @@ flowchart TB
244 ## Code
245
246 ```javascript
247 -function f$0() {
248 - return;
249 -}
247 +function f$0() {}
248
249 ```
250 ## HIR
@@ -272,9 +270,7 @@ flowchart TB
270 ## Code
271
272 ```javascript
275 -function g$0() {
276 - return;
277 -}
273 +function g$0() {}
274
275 ```
276
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-loops.expect.md
+2 -7
@@ -62,9 +62,7 @@ flowchart TB
62 ## Code
63
64 ```javascript
65 -function mutate$0(x$1, y$2) {
66 - return;
67 -}
65 +function mutate$0(x$1, y$2) {}
66
67 ```
68 ## HIR
@@ -90,9 +88,7 @@ flowchart TB
88 ## Code
89
90 ```javascript
93 -function cond$0(x$1) {
94 - return;
95 -}
91 +function cond$0(x$1) {}
92
93 ```
94 ## HIR
@@ -247,7 +243,6 @@ function Component$0(props$1) {
243 }
244
245 mutate$7(d$5, null);
250 - return;
246 }
247
248 ```
compiler/forget/src/__tests__/fixtures/hir/mutable-lifetime-with-aliasing.expect.md
+1 -4
@@ -58,9 +58,7 @@ flowchart TB
58 ## Code
59
60 ```javascript
61 -function mutate$0(x$1, y$2) {
62 - return;
63 -}
61 +function mutate$0(x$1, y$2) {}
62
63 ```
64 ## HIR
@@ -174,7 +172,6 @@ function Component$0(props$1) {
172 }
173
174 mutate$8(x$6, null);
177 - return;
175 }
176
177 ```
compiler/forget/src/__tests__/fixtures/hir/mutable-liverange-loop.expect.md
+2 -7
@@ -57,9 +57,7 @@ flowchart TB
57 ## Code
58
59 ```javascript
60 -function mutate$0() {
61 - return;
62 -}
60 +function mutate$0() {}
61
62 ```
63 ## HIR
@@ -85,9 +83,7 @@ flowchart TB
83 ## Code
84
85 ```javascript
88 -function cond$0() {
89 - return;
90 -}
86 +function cond$0() {}
87
88 ```
89 ## HIR
@@ -227,7 +223,6 @@ function Component$0(props$1) {
223 }
224
225 mutate$6(d$5, null);
230 - return;
226 }
227
228 ```
compiler/forget/src/__tests__/fixtures/hir/reverse-postorder.expect.md
-1
@@ -189,7 +189,6 @@ function Component$0(props$1) {
189 }
190
191 x$2;
192 - return;
192 }
193
194 ```
compiler/forget/src/__tests__/fixtures/hir/simple-alias.expect.md
+1 -3
@@ -39,9 +39,7 @@ flowchart TB
39 ## Code
40
41 ```javascript
42 -function mutate$0() {
43 - return;
44 -}
42 +function mutate$0() {}
43
44 ```
45 ## HIR
compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx-2.expect.md
+1 -3
@@ -41,9 +41,7 @@ flowchart TB
41 ## Code
42
43 ```javascript
44 -function foo$0() {
45 - return;
46 -}
44 +function foo$0() {}
45
46 ```
47 ## HIR
compiler/forget/src/__tests__/fixtures/hir/ssa-call-jsx.expect.md
+1 -3
@@ -38,9 +38,7 @@ flowchart TB
38 ## Code
39
40 ```javascript
41 -function foo$0() {
42 - return;
43 -}
41 +function foo$0() {}
42
43 ```
44 ## HIR
compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md
-1
@@ -110,7 +110,6 @@ function foo$0() {
110 }
111
112 y$2 = x$1;
113 - return;
113 }
114
115 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md
-1
@@ -78,7 +78,6 @@ function foo$0() {
78 }
79
80 y$2 = x$1;
81 - return;
81 }
82
83 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md
-2
@@ -83,8 +83,6 @@ function foo$0() {
83 } else {
84 let z$4 = x$1;
85 }
86 -
87 - return;
86 }
87
88 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-newexpression.expect.md
+1 -3
@@ -36,9 +36,7 @@ flowchart TB
36 ## Code
37
38 ```javascript
39 -function Foo$0() {
40 - return;
41 -}
39 +function Foo$0() {}
40
41 ```
42 ## HIR
compiler/forget/src/__tests__/fixtures/hir/ssa-shadowing.expect.md
+1 -4
@@ -40,9 +40,7 @@ flowchart TB
40 ## Code
41
42 ```javascript
43 -function log$0() {
44 - return;
45 -}
43 +function log$0() {}
44
45 ```
46 ## HIR
@@ -119,7 +117,6 @@ function Foo$0(cond$1) {
117 }
118
119 log$4(str$2);
122 - return;
120 }
121
122 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md
-1
@@ -91,7 +91,6 @@ function foo$0() {
91 }
92
93 let x$4 = y$1;
94 - return;
94 }
95
96 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md
-1
@@ -41,7 +41,6 @@ flowchart TB
41 function foo$0() {
42 let x$1 = 1;
43 let y$2 = 2;
44 - return;
44 }
45
46 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md
-2
@@ -67,8 +67,6 @@ function foo$0() {
67 bb1: if (y$2) {
68 let z$3 = x$1 + y$2;
69 }
70 -
71 - return;
70 }
71
72 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md
-1
@@ -134,7 +134,6 @@ function foo$0() {
134 }
135
136 let y$9 = x$1;
137 - return;
137 }
138
139 ```
compiler/forget/src/__tests__/fixtures/hir/switch-with-fallthrough.expect.md
-2
@@ -191,8 +191,6 @@ function foo$0(x$1) {
191 y$2 = 0;
192 }
193 }
194 -
195 - return;
194 }
195
196 ```
compiler/forget/src/__tests__/fixtures/hir/while-conditional-continue.expect.md
-1
@@ -94,7 +94,6 @@ function foo$0(a$1, b$2, c$3, d$4) {
94 }
95
96 d$4();
97 - return;
97 }
98
99 ```