From 7438c6bf294ab340eecaacf01fe9fa7d03449181 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 7 Mar 2022 21:08:44 +0800 Subject: [PATCH] optmize the regAllocator Signed-off-by: hufeng Change-Id: I87cb31e4a5353f3ed7287faf481a0b084d2930a2 --- ts2panda/src/debuginfo.ts | 5 +---- ts2panda/src/pandagen.ts | 4 ++++ ts2panda/src/pass/cacheExpander.ts | 2 +- ts2panda/src/regAllocator.ts | 36 +++++++++++++----------------- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/ts2panda/src/debuginfo.ts b/ts2panda/src/debuginfo.ts index 0519b3ba20..9c482e870b 100644 --- a/ts2panda/src/debuginfo.ts +++ b/ts2panda/src/debuginfo.ts @@ -395,10 +395,7 @@ export class DebugInfo { public static copyDebugInfo(insn: IRNode, expansion: IRNode[]) { - let debugPosInfo = insn.debugPosInfo; - for (let j = 0; j < expansion.length; j++) { - expansion[j].debugPosInfo = debugPosInfo; - } + expansion.forEach(irNode => irNode.debugPosInfo = insn.debugPosInfo); } public static addDebugIns(scope: Scope, pandaGen: PandaGen, isStart: boolean) { diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 22c11bf961..8a098828cb 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -370,6 +370,10 @@ export class PandaGen { return this.insns; } + setInsns(insns: IRNode[]) { + this.insns = insns; + } + printInsns() { LOGE("function " + this.internalName + "() {"); this.getInsns().forEach(ins => { diff --git a/ts2panda/src/pass/cacheExpander.ts b/ts2panda/src/pass/cacheExpander.ts index cfb0137a90..85dc5bc812 100755 --- a/ts2panda/src/pass/cacheExpander.ts +++ b/ts2panda/src/pass/cacheExpander.ts @@ -32,7 +32,7 @@ export class CacheExpander implements Pass { if (item.isNeeded()) { let expander = item.getExpander(); let expansion = expander(pandaGen); - insns.splice(0, 0, ...expansion); + insns.unshift(...expansion); } } } diff --git a/ts2panda/src/regAllocator.ts b/ts2panda/src/regAllocator.ts index 985262132e..dfcc44b3be 100644 --- a/ts2panda/src/regAllocator.ts +++ b/ts2panda/src/regAllocator.ts @@ -32,16 +32,13 @@ const MAX_VREGA = 16; const MAX_VREGB = 256; const MAX_VREGC = 65536; -class VRegWithFlag { - constructor(vreg: VReg) { - this.flag = false; - this.vreg = vreg; - } +interface VRegWithFlag { vreg: VReg; flag: boolean; // indicate whether it is used as a temporary register for spill } class RegAllocator { + private newInsns: IRNode[] = []; private spills: VReg[] = []; private vRegsId: number = 0; private usedVreg: VRegWithFlag[] = []; @@ -54,7 +51,7 @@ class RegAllocator { allocIndexForVreg(vreg: VReg) { let num = this.getFreeVreg(); vreg.num = num; - this.usedVreg[num] = new VRegWithFlag(vreg); + this.usedVreg[num] = {vreg: vreg, flag: false}; } findTmpVreg(level: number): VReg { @@ -121,7 +118,7 @@ class RegAllocator { this.tmpVreg.push(this.usedVreg[num]); } - doRealAdjustment(operands: OperandType[], format: Format, index: number, irNodes: IRNode[]): number { + doRealAdjustment(operands: OperandType[], format: Format, index: number, irNodes: IRNode[]) { let head: IRNode[] = []; let tail: IRNode[] = []; let spills: VReg[] = []; @@ -165,14 +162,12 @@ class RegAllocator { DebugInfo.copyDebugInfo(irNodes[index], head); DebugInfo.copyDebugInfo(irNodes[index], tail); - irNodes.splice(index, 0, ...head); - irNodes.splice(index + head.length + 1, 0, ...tail); + this.newInsns.push(...head, irNodes[index], ...tail); + for (let j = spills.length - 1; j >= 0; --j) { this.freeSpill(spills[j]); } this.clearVregFlags(); - - return (head.length + tail.length); } checkDynRangeInstruction(irNodes: IRNode[], index: number): boolean { @@ -211,7 +206,7 @@ class RegAllocator { return false; } - adjustDynRangeInstruction(irNodes: IRNode[], index: number): number { + adjustDynRangeInstruction(irNodes: IRNode[], index: number) { let head: IRNode[] = []; let tail: IRNode[] = []; let spills: VReg[] = []; @@ -239,14 +234,11 @@ class RegAllocator { DebugInfo.copyDebugInfo(irNodes[index], head); DebugInfo.copyDebugInfo(irNodes[index], tail); - irNodes.splice(index, 0, ...head); - irNodes.splice(index + head.length + 1, 0, ...tail); + this.newInsns.push(...head, irNodes[index], ...tail); for (let i = spills.length - 1; i >= 0; --i) { this.freeSpill(spills[i]); } this.clearVregFlags(); - - return (head.length + tail.length); } adjustInstructionsIfNeeded(irNodes: IRNode[]): void { @@ -255,10 +247,10 @@ class RegAllocator { let formats = irNodes[i].getFormats(); if (isRangeInst(irNodes[i])) { if (this.checkDynRangeInstruction(irNodes, i)) { + this.newInsns.push(irNodes[i]); continue; } - - i += this.adjustDynRangeInstruction(irNodes, i); + this.adjustDynRangeInstruction(irNodes, i); continue; } @@ -272,8 +264,10 @@ class RegAllocator { } } if (min > 0) { - i += this.doRealAdjustment(operands, minFormat, i, irNodes); + this.doRealAdjustment(operands, minFormat, i, irNodes); + continue; } + this.newInsns.push(irNodes[i]); } } @@ -304,8 +298,10 @@ class RegAllocator { for (let i = 0; i < parametersCount; ++i) { let v = new VReg(); this.allocIndexForVreg(v); - irNodes.splice(0, 0, new MovDyn(locals[i], v)); + this.newInsns.unshift(new MovDyn(locals[i], v)); } + + pandaGen.setInsns(this.newInsns); } } -- Gitee