summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-04-09 10:46:26 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-04-09 10:46:26 -0700
commit98a76440fa47c784b3e425aa3ae4d3a3bad05f25 (patch)
tree805df540a21b31a3a73d1e1f65d4b48b601ab69b
parentb7fbe0910cddf984426505de51200ed263d834d8 (diff)
downloadbinaryen-98a76440fa47c784b3e425aa3ae4d3a3bad05f25.tar.gz
binaryen-98a76440fa47c784b3e425aa3ae4d3a3bad05f25.tar.bz2
binaryen-98a76440fa47c784b3e425aa3ae4d3a3bad05f25.zip
optimize asm2wasm loop branches using eqz
-rw-r--r--src/asm2wasm.h14
-rw-r--r--test/emcc_O2_hello_world.fromasm72
-rw-r--r--test/emcc_O2_hello_world.fromasm.imprecise72
-rw-r--r--test/emcc_hello_world.fromasm72
-rw-r--r--test/emcc_hello_world.fromasm.imprecise72
-rw-r--r--test/memorygrowth.fromasm72
-rw-r--r--test/memorygrowth.fromasm.imprecise72
-rw-r--r--test/unit.fromasm12
-rw-r--r--test/unit.fromasm.imprecise12
9 files changed, 235 insertions, 235 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 1d3723869..72a8b9048 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -133,6 +133,8 @@ class Asm2WasmBuilder {
MixedArena &allocator;
+ Builder builder;
+
// globals
unsigned nextGlobal; // next place to put a global
@@ -267,6 +269,7 @@ public:
Asm2WasmBuilder(AllocatingModule& wasm, bool memoryGrowth, bool debug, bool imprecise)
: wasm(wasm),
allocator(wasm.allocator),
+ builder(wasm),
nextGlobal(8),
maxGlobal(1000),
memoryGrowth(memoryGrowth),
@@ -783,7 +786,6 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
Name x64("x64"), y64("y64");
func->locals.emplace_back(x64, i64);
func->locals.emplace_back(y64, i64);
- Builder builder(wasm);
auto* body = allocator.alloc<Block>();
auto recreateI64 = [&](Name target, Name low, Name high) {
return builder.makeSetLocal(
@@ -1393,9 +1395,8 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
Break *breakOut = allocator.alloc<Break>();
breakOut->name = out;
If *condition = allocator.alloc<If>();
- condition->condition = process(ast[1]);
- condition->ifTrue = allocator.alloc<Nop>();
- condition->ifFalse = breakOut;
+ condition->condition = builder.makeUnary(EqZ, process(ast[1]));
+ condition->ifTrue = breakOut;
auto body = allocator.alloc<Block>();
body->list.push_back(condition);
body->list.push_back(process(ast[2]));
@@ -1491,9 +1492,8 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
Break *breakOut = allocator.alloc<Break>();
breakOut->name = out;
If *condition = allocator.alloc<If>();
- condition->condition = process(fcond);
- condition->ifTrue = allocator.alloc<Nop>();
- condition->ifFalse = breakOut;
+ condition->condition = builder.makeUnary(EqZ, process(fcond));
+ condition->ifTrue = breakOut;
auto body = allocator.alloc<Block>();
body->list.push_back(condition);
body->list.push_back(process(fbody));
diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm
index 62eb5d4a9..6c19b4b2a 100644
--- a/test/emcc_O2_hello_world.fromasm
+++ b/test/emcc_O2_hello_world.fromasm
@@ -9543,13 +9543,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $i1)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $i1)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eqz
@@ -9586,13 +9586,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $i3)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $i3)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $i1)
@@ -9623,13 +9623,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $i3)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $i3)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $i1)
@@ -9736,13 +9736,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i5)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i5)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $i1)
@@ -9759,13 +9759,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i7)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i7)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $i1)
@@ -9782,13 +9782,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i4)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i4)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $i1)
diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise
index 62eb5d4a9..6c19b4b2a 100644
--- a/test/emcc_O2_hello_world.fromasm.imprecise
+++ b/test/emcc_O2_hello_world.fromasm.imprecise
@@ -9543,13 +9543,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $i1)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $i1)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eqz
@@ -9586,13 +9586,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $i3)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $i3)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $i1)
@@ -9623,13 +9623,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $i3)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $i3)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $i1)
@@ -9736,13 +9736,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i5)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i5)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $i1)
@@ -9759,13 +9759,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i7)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i7)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $i1)
@@ -9782,13 +9782,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $i1)
- (get_local $i4)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i1)
+ (get_local $i4)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $i1)
diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm
index 381e53407..415e66f1d 100644
--- a/test/emcc_hello_world.fromasm
+++ b/test/emcc_hello_world.fromasm
@@ -28143,13 +28143,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $unaligned)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $unaligned)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $ptr)
@@ -28166,13 +28166,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $stop4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $ptr)
@@ -28189,13 +28189,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $stop)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $ptr)
@@ -28369,13 +28369,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $dest)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $dest)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eq
@@ -28413,13 +28413,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $num)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $num)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $dest)
@@ -28450,13 +28450,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $num)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $num)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $dest)
diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise
index ec2f98964..fbc9c0d0e 100644
--- a/test/emcc_hello_world.fromasm.imprecise
+++ b/test/emcc_hello_world.fromasm.imprecise
@@ -28141,13 +28141,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $unaligned)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $unaligned)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $ptr)
@@ -28164,13 +28164,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $stop4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $ptr)
@@ -28187,13 +28187,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $ptr)
+ (get_local $stop)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $ptr)
@@ -28367,13 +28367,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $dest)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $dest)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eq
@@ -28411,13 +28411,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $num)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $num)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $dest)
@@ -28448,13 +28448,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $num)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $num)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $dest)
diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm
index 99ae1f394..1692fc8db 100644
--- a/test/memorygrowth.fromasm
+++ b/test/memorygrowth.fromasm
@@ -9609,13 +9609,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $a)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $a)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eqz
@@ -9652,13 +9652,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $c)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $c)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $a)
@@ -9689,13 +9689,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $c)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $c)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $a)
@@ -9802,13 +9802,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $e)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $e)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $a)
@@ -9825,13 +9825,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $g)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $g)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $a)
@@ -9848,13 +9848,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $d)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $d)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $a)
diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise
index 99ae1f394..1692fc8db 100644
--- a/test/memorygrowth.fromasm.imprecise
+++ b/test/memorygrowth.fromasm.imprecise
@@ -9609,13 +9609,13 @@
)
(block
(loop $while-out$0 $while-in$1
- (if
- (i32.and
- (get_local $a)
- (i32.const 3)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.and
+ (get_local $a)
+ (i32.const 3)
+ )
)
- (nop)
- (br $while-out$0)
)
(if
(i32.eqz
@@ -9652,13 +9652,13 @@
(br $while-in$1)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.ge_s
- (get_local $c)
- (i32.const 4)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.ge_s
+ (get_local $c)
+ (i32.const 4)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $a)
@@ -9689,13 +9689,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.gt_s
- (get_local $c)
- (i32.const 0)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.gt_s
+ (get_local $c)
+ (i32.const 0)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $a)
@@ -9802,13 +9802,13 @@
)
)
(loop $while-out$0 $while-in$1
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $e)
+ (br_if $while-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $e)
+ )
)
- (nop)
- (br $while-out$0)
)
(i32.store8
(get_local $a)
@@ -9825,13 +9825,13 @@
)
)
(loop $while-out$2 $while-in$3
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $g)
+ (br_if $while-out$2
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $g)
+ )
)
- (nop)
- (br $while-out$2)
)
(i32.store
(get_local $a)
@@ -9848,13 +9848,13 @@
)
)
(loop $while-out$4 $while-in$5
- (if
- (i32.lt_s
- (get_local $a)
- (get_local $d)
+ (br_if $while-out$4
+ (i32.eqz
+ (i32.lt_s
+ (get_local $a)
+ (get_local $d)
+ )
)
- (nop)
- (br $while-out$4)
)
(i32.store8
(get_local $a)
diff --git a/test/unit.fromasm b/test/unit.fromasm
index 8a8b2ebd9..93d654845 100644
--- a/test/unit.fromasm
+++ b/test/unit.fromasm
@@ -442,13 +442,13 @@
(i32.const 1)
)
(loop $for-out$0 $for-in$1
- (if
- (i32.lt_s
- (get_local $i)
- (i32.const 200)
+ (br_if $for-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i)
+ (i32.const 200)
+ )
)
- (nop)
- (br $for-out$0)
)
(call_import $h
(get_local $i)
diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise
index 677336c09..d555a5cf6 100644
--- a/test/unit.fromasm.imprecise
+++ b/test/unit.fromasm.imprecise
@@ -438,13 +438,13 @@
(i32.const 1)
)
(loop $for-out$0 $for-in$1
- (if
- (i32.lt_s
- (get_local $i)
- (i32.const 200)
+ (br_if $for-out$0
+ (i32.eqz
+ (i32.lt_s
+ (get_local $i)
+ (i32.const 200)
+ )
)
- (nop)
- (br $for-out$0)
)
(call_import $h
(get_local $i)