summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-09-24 16:39:18 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-09-24 16:47:52 -0700
commit97cf0d7bf6f115d44636dd52dc7c0036567ca798 (patch)
treec6862019f90650140f86c2c5c02377ba2b6ad3ce
parentf28bba29833bad06a76c7aabf31cf0257e12f5ba (diff)
downloadbinaryen-97cf0d7bf6f115d44636dd52dc7c0036567ca798.tar.gz
binaryen-97cf0d7bf6f115d44636dd52dc7c0036567ca798.tar.bz2
binaryen-97cf0d7bf6f115d44636dd52dc7c0036567ca798.zip
optimize if(const)
-rw-r--r--src/passes/Vacuum.cpp15
-rw-r--r--test/example/c-api-kitchen-sink.txt96
-rw-r--r--test/example/c-api-kitchen-sink.txt.txt48
-rw-r--r--test/passes/precompute.txt7
-rw-r--r--test/passes/precompute.wast2
-rw-r--r--test/passes/vacuum.txt20
-rw-r--r--test/passes/vacuum.wast15
-rw-r--r--test/unit.asm.js12
-rw-r--r--test/unit.fromasm12
-rw-r--r--test/unit.fromasm.imprecise12
-rw-r--r--test/unit.fromasm.imprecise.no-opts12
-rw-r--r--test/unit.fromasm.no-opts12
12 files changed, 96 insertions, 167 deletions
diff --git a/src/passes/Vacuum.cpp b/src/passes/Vacuum.cpp
index 03becb04c..c3fed2328 100644
--- a/src/passes/Vacuum.cpp
+++ b/src/passes/Vacuum.cpp
@@ -186,6 +186,21 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum, Visitor<Vacuum>>
}
void visitIf(If* curr) {
+ // if the condition is a constant, just apply it
+ // we can just return the ifTrue or ifFalse.
+ if (auto* value = curr->condition->dynCast<Const>()) {
+ if (value->value.getInteger()) {
+ replaceCurrent(curr->ifTrue);
+ return;
+ } else {
+ if (curr->ifFalse) {
+ replaceCurrent(curr->ifFalse);
+ } else {
+ ExpressionManipulator::nop(curr);
+ }
+ return;
+ }
+ }
if (curr->ifFalse) {
if (curr->ifFalse->is<Nop>()) {
curr->ifFalse = nullptr;
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 8cc6a0a1c..b66982140 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -1054,29 +1054,12 @@ optimized:
(br $shape$0$continue)
)
)
- (func $split (type $v)
- (call $check
- (i32.const 0)
- )
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
- )
- )
(func $if (type $v)
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 2)
@@ -1086,14 +1069,8 @@ optimized:
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 3)
@@ -1108,10 +1085,7 @@ optimized:
(call $check
(i32.const 1)
)
- (br_if $shape$0$continue
- (i32.const 10)
- )
- (br $block$3$break)
+ (br $shape$0$continue)
)
)
(call $check
@@ -1131,20 +1105,14 @@ optimized:
(call $check
(i32.const 2)
)
- (br_if $block$4$break
- (i32.const -6)
- )
- (br $shape$1$continue)
+ (br $block$4$break)
)
)
(call $check
(i32.const 3)
)
- (if
- (i32.const -10)
- (call $check
- (i32.const 4)
- )
+ (call $check
+ (i32.const 4)
)
(call $check
(i32.const 5)
@@ -3093,29 +3061,12 @@ optimized:
(br $shape$0$continue)
)
)
- (func $split (type $v)
- (call $check
- (i32.const 0)
- )
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
- )
- )
(func $if (type $v)
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 2)
@@ -3125,14 +3076,8 @@ optimized:
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 3)
@@ -3147,10 +3092,7 @@ optimized:
(call $check
(i32.const 1)
)
- (br_if $shape$0$continue
- (i32.const 10)
- )
- (br $block$3$break)
+ (br $shape$0$continue)
)
)
(call $check
@@ -3170,20 +3112,14 @@ optimized:
(call $check
(i32.const 2)
)
- (br_if $block$4$break
- (i32.const -6)
- )
- (br $shape$1$continue)
+ (br $block$4$break)
)
)
(call $check
(i32.const 3)
)
- (if
- (i32.const -10)
- (call $check
- (i32.const 4)
- )
+ (call $check
+ (i32.const 4)
)
(call $check
(i32.const 5)
diff --git a/test/example/c-api-kitchen-sink.txt.txt b/test/example/c-api-kitchen-sink.txt.txt
index 1ce2c903a..b42c1ec34 100644
--- a/test/example/c-api-kitchen-sink.txt.txt
+++ b/test/example/c-api-kitchen-sink.txt.txt
@@ -1047,29 +1047,12 @@
(br $shape$0$continue)
)
)
- (func $split (type $v)
- (call $check
- (i32.const 0)
- )
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
- )
- )
(func $if (type $v)
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 2)
@@ -1079,14 +1062,8 @@
(call $check
(i32.const 0)
)
- (if
- (i32.const 55)
- (call $check
- (i32.const 1)
- )
- (call $check
- (i32.const 2)
- )
+ (call $check
+ (i32.const 1)
)
(call $check
(i32.const 3)
@@ -1101,10 +1078,7 @@
(call $check
(i32.const 1)
)
- (br_if $shape$0$continue
- (i32.const 10)
- )
- (br $block$3$break)
+ (br $shape$0$continue)
)
)
(call $check
@@ -1124,20 +1098,14 @@
(call $check
(i32.const 2)
)
- (br_if $block$4$break
- (i32.const -6)
- )
- (br $shape$1$continue)
+ (br $block$4$break)
)
)
(call $check
(i32.const 3)
)
- (if
- (i32.const -10)
- (call $check
- (i32.const 4)
- )
+ (call $check
+ (i32.const 4)
)
(call $check
(i32.const 5)
diff --git a/test/passes/precompute.txt b/test/passes/precompute.txt
index 9c3640757..cbf0f23a7 100644
--- a/test/passes/precompute.txt
+++ b/test/passes/precompute.txt
@@ -16,13 +16,6 @@
(br $in)
)
(nop)
- (if
- (i32.const 2)
- (call $x
- (i32.const 3)
- )
- )
- (nop)
(block $c
(nop)
(call $x
diff --git a/test/passes/precompute.wast b/test/passes/precompute.wast
index ab7f0e3ad..e57522578 100644
--- a/test/passes/precompute.wast
+++ b/test/passes/precompute.wast
@@ -41,8 +41,6 @@
(loop $in
(br $in)
)
- (if (i32.const 0) (call $x (i32.const 1)))
- (if (i32.const 2) (call $x (i32.const 3)))
(block $b
(br_if $b (i32.const 0))
(br_if $b (i32.const 1))
diff --git a/test/passes/vacuum.txt b/test/passes/vacuum.txt
index 6b5d63b2a..73be2fea4 100644
--- a/test/passes/vacuum.txt
+++ b/test/passes/vacuum.txt
@@ -143,23 +143,24 @@
(unreachable)
)
)
- (func $if-drop (type $0)
+ (func $if-drop (type $3) (result i32)
(block $out
(if
- (i32.const 0)
+ (call $if-drop)
(drop
(call $int)
)
(br $out)
)
(if
- (i32.const 1)
+ (call $if-drop)
(br $out)
(drop
(call $int)
)
)
)
+ (i32.const 1)
)
(func $drop-silly (type $0)
(drop
@@ -210,11 +211,22 @@
(func $if2drops (type $3) (result i32)
(drop
(if
- (i32.const 1)
+ (call $if2drops)
(call $if2drops)
(call $if2drops)
)
)
(i32.const 2)
)
+ (func $if-const (type $1) (param $x i32)
+ (call $if-const
+ (i32.const 3)
+ )
+ (call $if-const
+ (i32.const 5)
+ )
+ (call $if-const
+ (i32.const 7)
+ )
+ )
)
diff --git a/test/passes/vacuum.wast b/test/passes/vacuum.wast
index 3584df3ff..559deb971 100644
--- a/test/passes/vacuum.wast
+++ b/test/passes/vacuum.wast
@@ -299,21 +299,22 @@
(unreachable)
)
)
- (func $if-drop
+ (func $if-drop (result i32)
(block $out
(drop
- (if (i32.const 0)
+ (if (call $if-drop)
(call $int)
(br $out)
)
)
(drop
- (if (i32.const 1)
+ (if (call $if-drop)
(br $out)
(call $int)
)
)
)
+ (i32.const 1)
)
(func $drop-silly
(drop
@@ -414,7 +415,7 @@
)
(func $if2drops (result i32)
(if
- (i32.const 1)
+ (call $if2drops)
(drop
(call $if2drops)
)
@@ -424,4 +425,10 @@
)
(i32.const 2)
)
+ (func $if-const (param $x i32)
+ (if (i32.const 0) (call $if-const (i32.const 1)))
+ (if (i32.const 2) (call $if-const (i32.const 3)))
+ (if (i32.const 0) (call $if-const (i32.const 4)) (call $if-const (i32.const 5)))
+ (if (i32.const 6) (call $if-const (i32.const 7)) (call $if-const (i32.const 8)))
+ )
)
diff --git a/test/unit.asm.js b/test/unit.asm.js
index 86be4cf2f..c3adb2a3a 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -266,7 +266,7 @@ function asm(global, env, buffer) {
function smallIf() {
do {
- if (2) {
+ if (return_int() | 0) {
lb(3) | 0;
} else {
break;
@@ -328,8 +328,8 @@ function asm(global, env, buffer) {
function conditionalTypeFun() {
var x = 0, y = +0;
- x = 1 ? abort(5) | 0 : 2;
- y = 3 ? +abort(7) : 4.5;
+ x = return_int() | 0 ? abort(5) | 0 : 2;
+ y = return_int() | 0 ? +abort(7) : 4.5;
}
function loadSigned(x) {
@@ -358,7 +358,7 @@ function asm(global, env, buffer) {
Int = x;
globalOpts();
x = Int;
- if (1) Int = 20; // but this does interfere
+ if (return_int() | 0) Int = 20; // but this does interfere
Int = x;
globalOpts();
x = Int;
@@ -367,7 +367,7 @@ function asm(global, env, buffer) {
}
function dropCallImport() {
- if (1) return_int() | 0;
+ if (return_int() | 0) return_int() | 0;
}
function loophi(x, y) {
@@ -399,7 +399,7 @@ function asm(global, env, buffer) {
j = 0;
while(1) {
temp = j;
- if (1) {
+ if (return_int() | 0) {
if (temp) {
i$lcssa = i;
break L7;
diff --git a/test/unit.fromasm b/test/unit.fromasm
index 6d0952bd4..e08babd28 100644
--- a/test/unit.fromasm
+++ b/test/unit.fromasm
@@ -509,7 +509,7 @@
)
(func $smallIf
(if
- (i32.const 2)
+ (call $return_int)
(drop
(call $lb
(i32.const 3)
@@ -594,7 +594,7 @@
(func $conditionalTypeFun
(drop
(if
- (i32.const 1)
+ (call $return_int)
(i32.trunc_s/f64
(call $abort
(f64.convert_s/i32
@@ -607,7 +607,7 @@
)
(drop
(if
- (i32.const 3)
+ (call $return_int)
(call $abort
(f64.convert_s/i32
(i32.const 7)
@@ -696,7 +696,7 @@
(get_global $Int)
)
(if
- (i32.const 1)
+ (call $return_int)
(set_global $Int
(i32.const 20)
)
@@ -715,7 +715,7 @@
)
(func $dropCallImport
(if
- (i32.const 1)
+ (call $return_int)
(drop
(call $return_int)
)
@@ -768,7 +768,7 @@
(get_local $0)
)
(if
- (i32.const 1)
+ (call $return_int)
(br_if $label$break$L7
(get_local $2)
)
diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise
index 1e50ad731..f99b02e82 100644
--- a/test/unit.fromasm.imprecise
+++ b/test/unit.fromasm.imprecise
@@ -490,7 +490,7 @@
)
(func $smallIf
(if
- (i32.const 2)
+ (call $return_int)
(drop
(call $lb
(i32.const 3)
@@ -575,7 +575,7 @@
(func $conditionalTypeFun
(drop
(if
- (i32.const 1)
+ (call $return_int)
(i32.trunc_s/f64
(call $abort
(f64.convert_s/i32
@@ -588,7 +588,7 @@
)
(drop
(if
- (i32.const 3)
+ (call $return_int)
(call $abort
(f64.convert_s/i32
(i32.const 7)
@@ -677,7 +677,7 @@
(get_global $Int)
)
(if
- (i32.const 1)
+ (call $return_int)
(set_global $Int
(i32.const 20)
)
@@ -696,7 +696,7 @@
)
(func $dropCallImport
(if
- (i32.const 1)
+ (call $return_int)
(drop
(call $return_int)
)
@@ -749,7 +749,7 @@
(get_local $0)
)
(if
- (i32.const 1)
+ (call $return_int)
(br_if $label$break$L7
(get_local $2)
)
diff --git a/test/unit.fromasm.imprecise.no-opts b/test/unit.fromasm.imprecise.no-opts
index f851b86c9..16a10861b 100644
--- a/test/unit.fromasm.imprecise.no-opts
+++ b/test/unit.fromasm.imprecise.no-opts
@@ -857,7 +857,7 @@
(func $smallIf
(block $do-once$0
(if
- (i32.const 2)
+ (call $return_int)
(drop
(call $lb
(i32.const 3)
@@ -986,7 +986,7 @@
(local $y f64)
(set_local $x
(if
- (i32.const 1)
+ (call $return_int)
(i32.trunc_s/f64
(call $abort
(f64.convert_s/i32
@@ -999,7 +999,7 @@
)
(set_local $y
(if
- (i32.const 3)
+ (call $return_int)
(call $abort
(f64.convert_s/i32
(i32.const 7)
@@ -1131,7 +1131,7 @@
(get_global $Int)
)
(if
- (i32.const 1)
+ (call $return_int)
(set_global $Int
(i32.const 20)
)
@@ -1150,7 +1150,7 @@
)
(func $dropCallImport
(if
- (i32.const 1)
+ (call $return_int)
(drop
(call $return_int)
)
@@ -1219,7 +1219,7 @@
(get_local $j)
)
(if
- (i32.const 1)
+ (call $return_int)
(if
(get_local $temp)
(block
diff --git a/test/unit.fromasm.no-opts b/test/unit.fromasm.no-opts
index a7580f991..58f63e66c 100644
--- a/test/unit.fromasm.no-opts
+++ b/test/unit.fromasm.no-opts
@@ -863,7 +863,7 @@
(func $smallIf
(block $do-once$0
(if
- (i32.const 2)
+ (call $return_int)
(drop
(call $lb
(i32.const 3)
@@ -992,7 +992,7 @@
(local $y f64)
(set_local $x
(if
- (i32.const 1)
+ (call $return_int)
(i32.trunc_s/f64
(call $abort
(f64.convert_s/i32
@@ -1005,7 +1005,7 @@
)
(set_local $y
(if
- (i32.const 3)
+ (call $return_int)
(call $abort
(f64.convert_s/i32
(i32.const 7)
@@ -1137,7 +1137,7 @@
(get_global $Int)
)
(if
- (i32.const 1)
+ (call $return_int)
(set_global $Int
(i32.const 20)
)
@@ -1156,7 +1156,7 @@
)
(func $dropCallImport
(if
- (i32.const 1)
+ (call $return_int)
(drop
(call $return_int)
)
@@ -1225,7 +1225,7 @@
(get_local $j)
)
(if
- (i32.const 1)
+ (call $return_int)
(if
(get_local $temp)
(block