-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathDeadCodeElimination.ts
More file actions
426 lines (406 loc) · 13.3 KB
/
DeadCodeElimination.ts
File metadata and controls
426 lines (406 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
BlockId,
Environment,
getHookKind,
HIRFunction,
Identifier,
IdentifierId,
Instruction,
InstructionKind,
InstructionValue,
ObjectPattern,
} from '../HIR';
import {
eachInstructionValueOperand,
eachPatternOperand,
eachTerminalOperand,
} from '../HIR/visitors';
import {assertExhaustive, retainWhere} from '../Utils/utils';
/*
* Implements dead-code elimination, eliminating instructions whose values are unused.
*
* Note that unreachable blocks are already pruned during HIR construction.
*/
export function deadCodeElimination(fn: HIRFunction): void {
/**
* Phase 1: Find/mark all referenced identifiers
* Usages may be visited AFTER declarations if there are circular phi / data dependencies
* between blocks, so we wait to sweep until after fixed point iteration is complete
*/
const state = findReferencedIdentifiers(fn);
/**
* Phase 2: Prune / sweep unreferenced identifiers and instructions
* as possible (subject to HIR structural constraints)
*/
for (const [, block] of fn.body.blocks) {
for (const phi of block.phis) {
if (!state.isIdOrNameUsed(phi.place.identifier)) {
block.phis.delete(phi);
}
}
retainWhere(block.instructions, instr =>
state.isIdOrNameUsed(instr.lvalue.identifier),
);
// Rewrite retained instructions
for (let i = 0; i < block.instructions.length; i++) {
const isBlockValue =
block.kind !== 'block' && i === block.instructions.length - 1;
if (!isBlockValue) {
rewriteInstruction(block.instructions[i], state);
}
}
}
/**
* Constant propagation and DCE may have deleted or rewritten instructions
* that reference context variables.
*/
retainWhere(fn.context, contextVar =>
state.isIdOrNameUsed(contextVar.identifier),
);
}
class State {
env: Environment;
named: Set<string> = new Set();
identifiers: Set<IdentifierId> = new Set();
constructor(env: Environment) {
this.env = env;
}
// Mark the identifier as being referenced (not dead code)
reference(identifier: Identifier): void {
this.identifiers.add(identifier.id);
if (identifier.name !== null) {
this.named.add(identifier.name.value);
}
}
/*
* Check if any version of the given identifier is used somewhere.
* This checks both for usage of this specific identifer id (ssa id)
* and (for named identifiers) for any usages of that identifier name.
*/
isIdOrNameUsed(identifier: Identifier): boolean {
return (
this.identifiers.has(identifier.id) ||
(identifier.name !== null && this.named.has(identifier.name.value))
);
}
/*
* Like `used()`, but only checks for usages of this specific identifier id
* (ssa id).
*/
isIdUsed(identifier: Identifier): boolean {
return this.identifiers.has(identifier.id);
}
get count(): number {
return this.identifiers.size;
}
}
function findReferencedIdentifiers(fn: HIRFunction): State {
/*
* If there are no back-edges the algorithm can terminate after a single iteration
* of the blocks
*/
const hasLoop = hasBackEdge(fn);
const reversedBlocks = [...fn.body.blocks.values()].reverse();
const state = new State(fn.env);
let size = state.count;
do {
size = state.count;
/*
* Iterate blocks in postorder (successors before predecessors, excepting loops)
* to visit usages before declarations
*/
for (const block of reversedBlocks) {
for (const operand of eachTerminalOperand(block.terminal)) {
state.reference(operand.identifier);
}
for (let i = block.instructions.length - 1; i >= 0; i--) {
const instr = block.instructions[i]!;
const isBlockValue =
block.kind !== 'block' && i === block.instructions.length - 1;
if (isBlockValue) {
/**
* The last instr of a value block is never eligible for pruning,
* as that's the block's value. Pessimistically consider all operands
* as used to avoid rewriting the last instruction
*/
state.reference(instr.lvalue.identifier);
for (const place of eachInstructionValueOperand(instr.value)) {
state.reference(place.identifier);
}
} else if (
state.isIdOrNameUsed(instr.lvalue.identifier) ||
!pruneableValue(instr.value, state)
) {
state.reference(instr.lvalue.identifier);
if (instr.value.kind === 'StoreLocal') {
/*
* If this is a Let/Const declaration, mark the initializer as referenced
* only if the ssa'ed lval is also referenced
*/
if (
instr.value.lvalue.kind === InstructionKind.Reassign ||
state.isIdUsed(instr.value.lvalue.place.identifier)
) {
state.reference(instr.value.value.identifier);
}
} else {
for (const operand of eachInstructionValueOperand(instr.value)) {
state.reference(operand.identifier);
}
}
}
}
for (const phi of block.phis) {
if (state.isIdOrNameUsed(phi.place.identifier)) {
for (const [_pred, operand] of phi.operands) {
state.reference(operand.identifier);
}
}
}
}
} while (state.count > size && hasLoop);
return state;
}
function rewriteInstruction(instr: Instruction, state: State): void {
if (instr.value.kind === 'Destructure') {
// Remove unused lvalues
switch (instr.value.lvalue.pattern.kind) {
case 'ArrayPattern': {
/*
* For arrays, we can prune items prior to the end by replacing
* them with a hole. Items at the end can simply be dropped.
*/
let lastEntryIndex = 0;
const items = instr.value.lvalue.pattern.items;
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.kind === 'Identifier') {
if (!state.isIdOrNameUsed(item.identifier)) {
items[i] = {kind: 'Hole'};
} else {
lastEntryIndex = i;
}
} else if (item.kind === 'Spread') {
if (!state.isIdOrNameUsed(item.place.identifier)) {
items[i] = {kind: 'Hole'};
} else {
lastEntryIndex = i;
}
}
}
items.length = lastEntryIndex + 1;
break;
}
case 'ObjectPattern': {
/*
* For objects we can prune any unused properties so long as there is no used rest element
* (`const {x, ...y} = z`). If a rest element exists and is used, then nothing can be pruned
* because it would change the set of properties which are copied into the rest value.
* In the `const {x, ...y} = z` example, removing the `x` property would mean that `y` now
* has an `x` property, changing the semantics.
*/
let nextProperties: ObjectPattern['properties'] | null = null;
for (const property of instr.value.lvalue.pattern.properties) {
if (property.kind === 'ObjectProperty') {
if (state.isIdOrNameUsed(property.place.identifier)) {
nextProperties ??= [];
nextProperties.push(property);
}
} else {
if (state.isIdOrNameUsed(property.place.identifier)) {
nextProperties = null;
break;
}
}
}
if (nextProperties !== null) {
instr.value.lvalue.pattern.properties = nextProperties;
}
break;
}
default: {
assertExhaustive(
instr.value.lvalue.pattern,
`Unexpected pattern kind '${
(instr.value.lvalue.pattern as any).kind
}'`,
);
}
}
} else if (instr.value.kind === 'StoreLocal') {
if (
instr.value.lvalue.kind !== InstructionKind.Reassign &&
!state.isIdUsed(instr.value.lvalue.place.identifier)
) {
/*
* This is a const/let declaration where the variable is accessed later,
* but where the value is always overwritten before being read. Ie the
* initializer value is never read. We rewrite to a DeclareLocal so
* that the initializer value can be DCE'd
*/
instr.value = {
kind: 'DeclareLocal',
lvalue: instr.value.lvalue,
type: instr.value.type,
loc: instr.value.loc,
};
}
}
}
/*
* Returns true if it is safe to prune an instruction with the given value.
* Functions which may have side-
*/
function pruneableValue(value: InstructionValue, state: State): boolean {
switch (value.kind) {
case 'DeclareLocal': {
// Declarations are pruneable only if the named variable is never read later
return !state.isIdOrNameUsed(value.lvalue.place.identifier);
}
case 'StoreLocal': {
if (value.lvalue.kind === InstructionKind.Reassign) {
// Reassignments can be pruned if the specific instance being assigned is never read
return !state.isIdUsed(value.lvalue.place.identifier);
}
// Declarations are pruneable only if the named variable is never read later
return !state.isIdOrNameUsed(value.lvalue.place.identifier);
}
case 'Destructure': {
let isIdOrNameUsed = false;
let isIdUsed = false;
for (const place of eachPatternOperand(value.lvalue.pattern)) {
if (state.isIdUsed(place.identifier)) {
isIdOrNameUsed = true;
isIdUsed = true;
} else if (state.isIdOrNameUsed(place.identifier)) {
isIdOrNameUsed = true;
}
}
if (value.lvalue.kind === InstructionKind.Reassign) {
// Reassignments can be pruned if the specific instance being assigned is never read
return !isIdUsed;
} else {
// Otherwise pruneable only if none of the identifiers are read from later
return !isIdOrNameUsed;
}
}
case 'PostfixUpdate':
case 'PrefixUpdate': {
// Updates are pruneable if the specific instance instance being assigned is never read
return !state.isIdUsed(value.lvalue.identifier);
}
case 'Debugger': {
// explicitly retain debugger statements to not break debugging workflows
return false;
}
case 'CallExpression':
case 'MethodCall': {
if (state.env.outputMode === 'ssr') {
const calleee =
value.kind === 'CallExpression' ? value.callee : value.property;
const hookKind = getHookKind(state.env, calleee.identifier);
switch (hookKind) {
case 'useState':
case 'useReducer':
case 'useRef': {
// unused refs can be removed
return true;
}
}
}
return false;
}
case 'Await':
case 'ComputedDelete':
case 'ComputedStore':
case 'PropertyDelete':
case 'PropertyStore':
case 'StoreGlobal': {
/*
* Mutating instructions are not safe to prune.
* TODO: we could be more precise and make this conditional on whether
* any arguments are actually modified
*/
return false;
}
case 'NewExpression':
case 'UnsupportedNode':
case 'TaggedTemplateExpression': {
// Potentially safe to prune, since they should just be creating new values
return false;
}
case 'GetIterator':
case 'NextPropertyOf':
case 'IteratorNext': {
/*
* Technically a IteratorNext/NextPropertyOf will never be unused because it's
* always used later by another StoreLocal or Destructure instruction, but conceptually
* we can't prune
*/
return false;
}
case 'LoadContext':
case 'DeclareContext':
case 'StoreContext': {
return false;
}
case 'StartMemoize':
case 'FinishMemoize': {
/**
* This instruction is used by the @enablePreserveExistingMemoizationGuarantees feature
* to preserve information about memoization semantics in the original code. We can't
* DCE without losing the memoization guarantees.
*/
return false;
}
case 'RegExpLiteral':
case 'MetaProperty':
case 'LoadGlobal':
case 'ArrayExpression':
case 'BinaryExpression':
case 'ComputedLoad':
case 'ObjectMethod':
case 'FunctionExpression':
case 'LoadLocal':
case 'JsxExpression':
case 'JsxFragment':
case 'JSXText':
case 'ObjectExpression':
case 'Primitive':
case 'PropertyLoad':
case 'TemplateLiteral':
case 'TypeCastExpression':
case 'UnaryExpression': {
// Definitely safe to prune since they are read-only
return true;
}
default: {
assertExhaustive(
value,
`Unexepcted value kind \`${(value as any).kind}\``,
);
}
}
}
export function hasBackEdge(fn: HIRFunction): boolean {
return findBlocksWithBackEdges(fn).size > 0;
}
export function findBlocksWithBackEdges(fn: HIRFunction): Set<BlockId> {
const visited = new Set<BlockId>();
const blocks = new Set<BlockId>();
for (const [blockId, block] of fn.body.blocks) {
for (const predId of block.preds) {
if (!visited.has(predId)) {
blocks.add(blockId);
}
}
visited.add(blockId);
}
return blocks;
}