[be] Mutate HIR in-place (shrink/rpo)
Changes `shrink()` and `reversePostorderBlocks()` to modify the HIR in-place rather than return a new function, for consistency with all our other passes which mutate in-place (for performance reasons).
Joe Savona committed
Jan 9, 2023 at 09:13 UTC
3765219933e8d9777e180d4b1172624ce8219e47
1 file changed
+13
-15
compiler/forget/src/HIR/HIRBuilder.ts
+13
-15
@@ -163,15 +163,15 @@ export default class HIRBuilder {
163
};
164
logHIR("Build (pre-shrink)", ir);
165
// First reduce indirections
166
- let shrunk = shrink(ir);
167
- logHIR("Build (shrunk)", shrunk);
166
+ shrink(ir);
167
+ logHIR("Build (shrunk)", ir);
168
// then convert to reverse postorder
169
- const rpo = reversePostorderBlocks(shrunk);
170
- removeUnreachableFallthroughs(rpo);
171
- markInstructionIds(rpo);
172
- markPredecessors(rpo);
169
+ reversePostorderBlocks(ir);
170
+ removeUnreachableFallthroughs(ir);
171
+ markInstructionIds(ir);
172
+ markPredecessors(ir);
173
174
- return rpo;
174
+ return ir;
175
}
176
177
/**
@@ -360,7 +360,7 @@ export default class HIRBuilder {
360
/**
361
* Helper to shrink a CFG eliminate jump-only blocks.
362
*/
363
-function shrink(func: HIR): HIR {
363
+function shrink(func: HIR): void {
364
const gotos = new Map();
365
/**
366
* Given a target block for some terminator, resolves the ideal block that should be
@@ -408,10 +408,10 @@ function shrink(func: HIR): HIR {
408
});
409
}
410
411
- return { blocks, entry: func.entry };
411
+ func.blocks = blocks;
412
}
413
414
-function removeUnreachableFallthroughs(func: HIR) {
414
+function removeUnreachableFallthroughs(func: HIR): void {
415
const visited: Set<BlockId> = new Set();
416
for (const [_, block] of func.blocks) {
417
visited.add(block.id);
@@ -437,7 +437,7 @@ function removeUnreachableFallthroughs(func: HIR) {
437
* Converts the graph to reverse-postorder, with predecessor blocks appearing
438
* before successors except in the case of back links (ie loops).
439
*/
440
-function reversePostorderBlocks(func: HIR): HIR {
440
+function reversePostorderBlocks(func: HIR): void {
441
const visited: Set<BlockId> = new Set();
442
const postorder: Array<BlockId> = [];
443
function visit(blockId: BlockId) {
@@ -520,10 +520,8 @@ function reversePostorderBlocks(func: HIR): HIR {
520
for (const blockId of postorder.reverse()) {
521
blocks.set(blockId, func.blocks.get(blockId)!);
522
}
523
- return {
524
- blocks,
525
- entry: func.entry,
526
- };
523
+
524
+ func.blocks = blocks;
525
}
526
527
function markInstructionIds(func: HIR) {