import { createGraph, Graph } from "./data/graph"
|
|
|
|
import type { Loc } from "./ir/loc"
|
|
import type { SSA } from "./ir/ssa"
|
|
|
|
export type LivenessInfo = Array<Set<Loc>>
|
|
|
|
export const liveness = (block: Array<SSA>): LivenessInfo => {
|
|
const info: LivenessInfo = []
|
|
|
|
info[block.length] = new Set()
|
|
for (let i = block.length - 1; i >= 0; --i) {
|
|
const insn = block[i]
|
|
const last = info[i + 1]
|
|
|
|
info[i] = new Set()
|
|
|
|
if (typeof insn.source !== "number") {
|
|
info[i].add(insn.source)
|
|
}
|
|
|
|
if ("source1" in insn && typeof insn.source1 !== "number") {
|
|
info[i].add(insn.source1)
|
|
}
|
|
|
|
last.forEach(loc => {
|
|
if (loc.toString() === insn.dest.toString()) {
|
|
return
|
|
}
|
|
|
|
info[i].add(loc)
|
|
})
|
|
}
|
|
|
|
return info
|
|
}
|
|
|
|
export const locations = (block: Array<SSA>): Set<Loc> => {
|
|
const ls: Set<Loc> = new Set()
|
|
|
|
block.forEach(ssa => {
|
|
if ("source1" in ssa && typeof ssa.source1 !== "number") {
|
|
ls.add(ssa.source1)
|
|
} else if (typeof ssa.source !== "number") {
|
|
ls.add(ssa.source)
|
|
}
|
|
})
|
|
|
|
return ls
|
|
}
|
|
|
|
export const interference = (block: Array<SSA>, live: LivenessInfo): Graph<Loc> =>
|
|
createGraph((v, e) => {
|
|
block.forEach((ssa, i) =>
|
|
live[i + 1].forEach(u => {
|
|
if (ssa.dest.toString() !== u.toString()) {
|
|
v(ssa.dest.toString(), ssa.dest)
|
|
v(u.toString(), u)
|
|
e(ssa.dest.toString(), u.toString())
|
|
}
|
|
})
|
|
)
|
|
})
|