@samitouri / QOS-React-2 / commits / d8d5fe989c

[be] Improvements to shrink/markInstructionIds

Joe Savona committed Jan 9, 2023 at 10:06 UTC d8d5fe989cc7dce1036307ea3bd2ec0007a7f445
1 file changed +19 -14
compiler/forget/src/HIR/HIRBuilder.ts
+19 -14
@@ -387,28 +387,25 @@ function shrink(func: HIR): void {
387 }
388
389 const queue = [func.entry];
390 - const blocks: Map<BlockId, BasicBlock> = new Map();
390 + const reachable = new Set<BlockId>();
391 while (queue.length !== 0) {
392 const blockId = queue.shift()!;
393 - if (blocks.has(blockId)) {
393 + if (reachable.has(blockId)) {
394 continue;
395 }
396 - const { instructions, terminal: prevTerminal } = func.blocks.get(blockId)!;
397 - const terminal = mapTerminalSuccessors(prevTerminal, (prevTarget) => {
396 + reachable.add(blockId);
397 + const block = func.blocks.get(blockId)!;
398 + block.terminal = mapTerminalSuccessors(block.terminal, (prevTarget) => {
399 const target = resolveBlockTarget(prevTarget);
400 queue.push(target);
401 return target;
402 });
402 - blocks.set(blockId, {
403 - id: blockId,
404 - instructions,
405 - terminal,
406 - preds: new Set(),
407 - phis: new Set(),
408 - });
403 }
410 -
411 - func.blocks = blocks;
404 + for (const [blockId] of func.blocks) {
405 + if (!reachable.has(blockId)) {
406 + func.blocks.delete(blockId);
407 + }
408 + }
409 }
410
411 function removeUnreachableFallthroughs(func: HIR): void {
@@ -526,9 +523,14 @@ function reversePostorderBlocks(func: HIR): void {
523
524 function markInstructionIds(func: HIR) {
525 let id = 0;
526 + const visited = new Set<Instruction>();
527 for (const [_, block] of func.blocks) {
528 for (const instr of block.instructions) {
531 - invariant(instr.id === 0, `${printInstruction(instr)} already visited!`);
529 + invariant(
530 + !visited.has(instr),
531 + `${printInstruction(instr)} already visited!`
532 + );
533 + visited.add(instr);
534 instr.id = makeInstructionId(++id);
535 }
536 block.terminal.id = makeInstructionId(++id);
@@ -536,6 +538,9 @@ function markInstructionIds(func: HIR) {
538 }
539
540 function markPredecessors(func: HIR) {
541 + for (const [, block] of func.blocks) {
542 + block.preds.clear();
543 + }
544 const visited: Set<BlockId> = new Set();
545 function visit(blockId: BlockId, prevBlock: BasicBlock | null) {
546 const block = func.blocks.get(blockId)!;