Skip to content

Commit

Permalink
[opt] Improve load elimination and SsaOptimizer (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer authored Sep 23, 2024
1 parent c486482 commit f1395dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
6 changes: 6 additions & 0 deletions aeneas/src/ir/SsaNormalizer.v3
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class SsaRaNormalizer extends SsaRebuilder {
var opt = SsaOptimizer.new(context);
opt.optGraph();
context.printSsa("Norm Optimized");
if (context.compiler.LoadOptimize) {
// Perform load elimination.
if (SsaLoadOptimizer.new(context).optimize()) {
context.printSsa("Load Optimized");
}
}
//TODO SsaGraphVerifier.new(context).verify();
}
}
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-7.1760";
def version: string = "III-7.1761";
var buildData: string;
}
28 changes: 22 additions & 6 deletions aeneas/src/ssa/SsaOptimizer.v3
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class SsaInstrReducer(context: SsaContext) extends SsaInstrMatcher {
var optimize_nullchecks = true; // activates null check elimination
var optimize_inits = false; // activates init elimination
var optimize_bounds = true;
var remove_pure_ops = true;
var graph: SsaGraph;
var prev: SsaLink; // instruction before the current instruction
var next: SsaLink; // instruction after the current instruction
Expand Down Expand Up @@ -375,6 +376,7 @@ class SsaInstrReducer(context: SsaContext) extends SsaInstrMatcher {
def PURE_LOAD = Fact.O_NO_NULL_CHECK | Fact.O_PURE;
def ZERO_NON_ZERO = Fact.V_ZERO | Fact.V_NON_ZERO;
def ABOVE_BELOW_ZERO = Fact.V_NON_ZERO | Fact.V_NON_NEGATIVE | Fact.V_BELOW_ZERO;
if (remove_pure_ops && i.facts.O_PURE && i.useList == null) return killUnused(i);
match (i.op.opcode) {
BoolEq,
IntEq,
Expand Down Expand Up @@ -730,6 +732,7 @@ class SsaInstrReducer(context: SsaContext) extends SsaInstrMatcher {
}
i.setFactIf(Fact.O_NO_NULL_CHECK, Fact.O_PURE);
i.facts |= Fact.F_VALUE;
if (remove_pure_ops && i.facts.O_PURE && i.useList == null) return killUnused(i);
return lookupField(i, receiver, field);
}
}
Expand Down Expand Up @@ -1232,6 +1235,11 @@ class SsaInstrReducer(context: SsaContext) extends SsaInstrMatcher {
prev = i;
return i;
}
def killUnused(i_old: SsaInstr) -> SsaInstr {
i_old.kill();
i_old.remove();
return graph.nop();
}
}

// Performs control flow optimizations on a completed SsaGraph.
Expand Down Expand Up @@ -1911,15 +1919,23 @@ class SsaLoadOptimizer(context: SsaContext) {
pure.length = 0;
impure.length = 0;
var i = block.next;
for (i = block.next; SsaInstr.?(i); ()) {
var next = i.next;
match (i) {
instr: SsaApplyOp => {
var repl = optInstr(instr);
if (repl != null) {
any = true;
instr.replace(repl);
instr.remove();
}
}
}
i = next;
}
while (SsaApplyOp.?(i)) {
var instr = SsaApplyOp.!(i);
var next = instr.next;
var repl = optInstr(instr);
if (repl != null) {
any = true;
instr.replace(repl);
instr.remove();
}
i = next;
}
}
Expand Down
1 change: 1 addition & 0 deletions aeneas/test/SsaInstrReducerTest.v3
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ private class SsaInstrReducerTester(t: Tester) {
opt.optimize_nullchecks = optimize_nullchecks;
opt.optimize_inits = optimize_inits;
opt.optimize_bounds = optimize_bounds;
opt.remove_pure_ops = false; // TODO: test with removal of pure operations
return opt;
}
def recordConst(rtype: Type, vals: Array<Val>) -> SsaConst {
Expand Down
10 changes: 10 additions & 0 deletions test/core/opt_load12.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@execute -1=33; 2=55
class Foo(x: (int, int)) { }

var f1 = Foo.new(33, 44);
var f2 = Foo.new(55, 66);

def main(a: int) -> int {
var f = if(a < 0, f1, f2);
return f.x.0;
}

0 comments on commit f1395dc

Please sign in to comment.