Skip to content

Commit

Permalink
Minor simplification to phi optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Feb 2, 2025
1 parent 6ee7082 commit 2e5240b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 63 deletions.
9 changes: 4 additions & 5 deletions aeneas/src/mach/MachLowering.v3
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@ class SsaGraphNormalizer(context: SsaContext) {
i_old.setInputs(ai_new);
return i_old;
}
def normType(t: Type) -> TypeNorm;
def genSimpleVal(v: Val, tn: TypeNorm) -> Val;
def genValIntoArray(v: Val, tn: TypeNorm, dest: Array<Val>, index: int);
def normType1(t: Type) -> Type {
var tn = normType(t);
return if(tn != null, tn.newType, t);
Expand All @@ -234,7 +231,6 @@ class SsaGraphNormalizer(context: SsaContext) {
var tn = normType(t);
return tn != null && (tn.size != 1 || tn.newType != t);
}

def mapMultiReturn(i_old: SsaInstr, i_new: SsaInstr, tn: TypeNorm) {
if (tn == null || tn.size == 1) return map1(i_old, i_new);
if (tn.size == 0) return map0(i_old);
Expand Down Expand Up @@ -268,6 +264,10 @@ class SsaGraphNormalizer(context: SsaContext) {
}
return expr;
}
// ==== Required overrides ================================================
def normType(t: Type) -> TypeNorm;
def genSimpleVal(v: Val, tn: TypeNorm) -> Val;
def genValIntoArray(v: Val, tn: TypeNorm, dest: Array<Val>, index: int);
}

// Lowers Virgil code to machine level in-place by replacing object-level operations
Expand Down Expand Up @@ -415,7 +415,6 @@ class MachLowering(mach: MachProgram, compiler: Compiler, config: MachLoweringCo
return if(mt != t, TypeNorm.new(t, mt, null));
}
}

def normTypeTuple(t: Type) -> TupleNorm {
// TODO: cache flattened tuples which occur in returns
// flatten tuples
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/main/Version.v3
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

// Updated by VCS scripts. DO NOT EDIT.
component Version {
def version: string = "III-9.1791";
def version: string = "III-9.1792";
var buildData: string;
}
49 changes: 17 additions & 32 deletions aeneas/src/ssa/SsaOptimizer.v3
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,6 @@ class SsaInstrReducer(context: SsaContext) extends SsaInstrMatcher {
i = next;
}
}
def reducePhi(i: SsaPhi) -> bool {
var i0 = i.input0(), facts = i0.facts, same = true;
for (n = 1; n < i.inputs.length; n++) {
var iN = i.inputs[n].dest;
facts &= iN.facts;
if (iN != i0) same = false;
}
if (same) {
// phi(x, x, x...) => x
i.replace(i0);
return false;
}
i.facts |= facts;
return true;
}

def reduceInstr(i: SsaInstr) -> SsaInstr {
prev = i.prev;
match (i) {
Expand Down Expand Up @@ -1428,22 +1412,7 @@ class SsaCfOptimizer(context: SsaContext) {
killInstr(phi);
return true;
}
var facts = i0.facts, reduce = true;
for (i in inputs) {
var d = i.dest;
if (d != phi) {
facts = facts & d.facts;
if (d != i0) reduce = false;
}
}
if (reduce) {
phi.replace(i0); // the phi can be replaced by its (one) input
phi.remove();
return true;
} else {
phi.setFact(facts); // phi facts are the intersection of all input facts
return false;
}
return reducePhi(phi);
}
def optIf(block: SsaBlock, sw: SsaIf) {
var e = sw.inputs[0], v = e.dest;
Expand Down Expand Up @@ -1846,6 +1815,22 @@ class SsaCfOptimizer(context: SsaContext) {
}
class SwitchCmp(val: int, cmp: SsaInstr, end: SsaIf, tSucc: SsaCfEdge, fSucc: SsaCfEdge) { }

def reducePhi(i_phi: SsaPhi) -> bool {
var i0 = i_phi.input0(), facts = i0.facts, same = true;
for (n = 1; n < i_phi.inputs.length; n++) {
var iN = i_phi.inputs[n].dest;
facts &= iN.facts;
if (iN != i0 && iN != i_phi) same = false;
}
if (same) {
// phi(x, phi, x...) => x
i_phi.replace(i0);
i_phi.remove();
return false;
}
i_phi.facts |= facts;
return true;
}

// Performs optimizations on loops.
// 1. eliminate rendundant bounds checks for induction variables.
Expand Down
8 changes: 8 additions & 0 deletions test/core/opt_phi00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@execute 0=0; -11=-11
def main(a: int) -> int {
var sum = a;
for (i < 10) {
sum = a; // phi for sum is recursive but redundant
}
return sum;
}
15 changes: 7 additions & 8 deletions test/core/opt_phi01.v3
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//@execute 1=1; 0=0
component opt_phi01 {
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = a;
while (true) return i;
return i;
}
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = a;
while (true) return i;
return i;
}

15 changes: 7 additions & 8 deletions test/core/opt_phi02.v3
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//@execute 1=3; 0=3
component opt_phi02 {
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = 1;
while (true) return i + 2;
return i + 2;
}
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = 1;
while (true) return i + 2;
return i + 2;
}

16 changes: 7 additions & 9 deletions test/core/opt_phi03.v3
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//@execute 1=1; 0=1
component opt_phi03 {
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = 1;
while (i == 1) break;
if (i == 1) return 1;
return 2;
}
def main(a: int) -> int {
var i = 0;
while (i < 1) i = i + 1;
i = 1;
while (i == 1) break;
if (i == 1) return 1;
return 2;
}
8 changes: 8 additions & 0 deletions test/core/opt_phi04.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@execute 0=0; -11=-11
def main(a: int) -> int {
var sum = a;
for (i < 10) {
sum = sum; // phi for sum is recursive but redundant
}
return sum;
}

0 comments on commit 2e5240b

Please sign in to comment.