Skip to content

Commit

Permalink
feat: Statically eliminate logical expression branches (AssemblyScrip…
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored and romdotdog committed Jul 19, 2021
1 parent 15c3e2c commit 028e8eb
Show file tree
Hide file tree
Showing 31 changed files with 772 additions and 3,317 deletions.
55 changes: 39 additions & 16 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4577,15 +4577,26 @@ export class Compiler extends DiagnosticEmitter {

// simplify if only interested in true or false
if (contextualType == Type.bool || contextualType == Type.void) {
rightExpr = this.compileExpression(right, leftType, inheritedConstraints);
rightType = this.currentType;
rightFlow.freeScopedLocals();
leftExpr = this.makeIsTrueish(leftExpr, leftType, left);

// shortcut if lhs is always false
let condKind = this.evaluateCondition(leftExpr);
if (condKind == ConditionKind.FALSE) {
expr = leftExpr;
} else {
rightExpr = this.compileExpression(right, leftType, inheritedConstraints);
rightType = this.currentType;
rightFlow.freeScopedLocals();
rightExpr = this.makeIsTrueish(rightExpr, rightType, right);

// simplify if lhs is always true
if (condKind == ConditionKind.TRUE) {
expr = rightExpr;
} else {
expr = module.if(leftExpr, rightExpr, module.i32(0));
}
}
this.currentFlow = flow;
expr = module.if(
this.makeIsTrueish(leftExpr, leftType, left),
this.makeIsTrueish(rightExpr, rightType, right),
module.i32(0)
);
this.currentType = Type.bool;

} else {
Expand Down Expand Up @@ -4630,15 +4641,26 @@ export class Compiler extends DiagnosticEmitter {

// simplify if only interested in true or false
if (contextualType == Type.bool || contextualType == Type.void) {
rightExpr = this.compileExpression(right, leftType, inheritedConstraints);
rightType = this.currentType;
rightFlow.freeScopedLocals();
leftExpr = this.makeIsTrueish(leftExpr, leftType, left);

// shortcut if lhs is always true
let condKind = this.evaluateCondition(leftExpr);
if (condKind == ConditionKind.TRUE) {
expr = leftExpr;
} else {
rightExpr = this.compileExpression(right, leftType, inheritedConstraints);
rightType = this.currentType;
rightFlow.freeScopedLocals();
rightExpr = this.makeIsTrueish(rightExpr, rightType, right);

// simplify if lhs is always false
if (condKind == ConditionKind.FALSE) {
expr = rightExpr;
} else {
expr = module.if(leftExpr, module.i32(1), rightExpr);
}
}
this.currentFlow = flow;
expr = module.if(
this.makeIsTrueish(leftExpr, leftType, left),
module.i32(1),
this.makeIsTrueish(rightExpr, rightType, right)
);
this.currentType = Type.bool;

} else {
Expand Down Expand Up @@ -10030,6 +10052,7 @@ export class Compiler extends DiagnosticEmitter {

/** Evaluates a boolean condition, determining whether it is TRUE, FALSE or UNKNOWN. */
evaluateCondition(expr: ExpressionRef): ConditionKind {
assert(getExpressionType(expr) == TypeRef.I32);
var module = this.module;
var evaled = module.runExpression(expr, ExpressionRunnerFlags.Default);
if (evaled) {
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/do.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2949,11 +2949,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $start:do
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/empty-exportruntime.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2466,11 +2466,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/for.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2974,11 +2974,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $start:for
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/issues/1225.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2419,11 +2419,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $start:issues/1225
Expand Down
35 changes: 0 additions & 35 deletions tests/compiler/logical.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2434,11 +2434,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down Expand Up @@ -2515,11 +2510,6 @@
i32.const 0
i32.store
i32.const 0
if (result i32)
unreachable
else
i32.const 0
end
drop
f64.const 0
i64.reinterpret_f64
Expand All @@ -2529,18 +2519,8 @@
i64.sub
i64.const -9007199254740994
i64.le_u
if (result i32)
unreachable
else
i32.const 0
end
drop
i32.const 1
if (result i32)
i32.const 1
else
unreachable
end
drop
f64.const 1
i64.reinterpret_f64
Expand All @@ -2550,23 +2530,13 @@
i64.sub
i64.const -9007199254740994
i64.le_u
if (result i32)
i32.const 1
else
unreachable
end
drop
i32.const 1
if (result i32)
i32.const 2
else
i32.const 1
end
if (result i32)
i32.const 1
else
unreachable
end
drop
f64.const 1
i64.reinterpret_f64
Expand All @@ -2588,11 +2558,6 @@
i64.sub
i64.const -9007199254740994
i64.le_u
if (result i32)
i32.const 1
else
unreachable
end
drop
i32.const 1
if (result i32)
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/managed-cast.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2541,11 +2541,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/object-literal.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4278,11 +4278,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/rt/finalize.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2416,11 +2416,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $start:rt/finalize
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/rt/runtime-incremental-export.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2466,11 +2466,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/std-wasi/console.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5317,11 +5317,6 @@
i32.load offset=8
local.set $2
i32.const 1
if (result i32)
i32.const 1
else
i32.const 0
end
drop
local.get $2
local.set $3
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/std-wasi/process.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6985,11 +6985,6 @@
i32.load offset=8
local.set $2
i32.const 1
if (result i32)
i32.const 1
else
i32.const 1
end
drop
local.get $2
local.set $3
Expand Down
5 changes: 0 additions & 5 deletions tests/compiler/std/array-literal.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3872,11 +3872,6 @@
i32.const 0
drop
i32.const 0
if (result i32)
i32.const 1
else
i32.const 0
end
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
Expand Down
Loading

0 comments on commit 028e8eb

Please sign in to comment.