From b01c25b76280a1739073616b4ad541a0f094528d Mon Sep 17 00:00:00 2001 From: Forest Belton Date: Wed, 22 Sep 2021 13:43:01 -0400 Subject: [PATCH] Precompute label indices --- lib/ir/block.ts | 12 ++++++++++++ lib/regalloc.ts | 15 +-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/ir/block.ts b/lib/ir/block.ts index 789773e..08e89a5 100644 --- a/lib/ir/block.ts +++ b/lib/ir/block.ts @@ -3,9 +3,21 @@ import type { Loc } from "./loc" export class BasicBlock { insns: Array + labels: { [label: string]: number } constructor(insns: Array) { this.insns = insns + this.labels = this.insns + .reduce((labels, insn, i) => { + if (insn.type !== "label") { + return labels + } + + return { + ...labels, + [insn.dest]: i, + } + }, {}) } locs(): Set { diff --git a/lib/regalloc.ts b/lib/regalloc.ts index bd20d96..43aa7c6 100644 --- a/lib/regalloc.ts +++ b/lib/regalloc.ts @@ -41,7 +41,7 @@ export const liveness = (block: BasicBlock): LivenessInfo => { continue } - const labelSet = info[getLabelIndex(block, block.insns[i].dest as string)] + const labelSet = info[block.labels[block.insns[i].dest as string]] if (setEquals(info[i], labelSet)) { continue } @@ -54,19 +54,6 @@ export const liveness = (block: BasicBlock): LivenessInfo => { return info } -export const getLabelIndex = (block: BasicBlock, label: string): number => { - let idx = -1 - - for (let i = 0; i < block.insns.length; ++i) { - if (block.insns[i].type === "label" && block.insns[i].dest === label) { - idx = i - break - } - } - - return idx -} - export const interference = (block: BasicBlock, live: LivenessInfo): Graph => createGraph((v, e) => { block.locs().forEach(loc => {