summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp22
-rw-r--r--test/emcc_O2_hello_world.fromasm40
-rw-r--r--test/emcc_O2_hello_world.fromasm.imprecise40
-rw-r--r--test/emcc_hello_world.fromasm40
-rw-r--r--test/emcc_hello_world.fromasm.imprecise40
-rw-r--r--test/memorygrowth.fromasm40
-rw-r--r--test/memorygrowth.fromasm.imprecise40
-rw-r--r--test/unit.fromasm8
-rw-r--r--test/unit.fromasm.imprecise8
9 files changed, 118 insertions, 160 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 638977353..de342b43f 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -36,6 +36,28 @@ struct OptimizeInstructions : public WalkerPass<WasmWalker<OptimizeInstructions>
}
}
}
+ void visitUnary(Unary* curr) {
+ if (curr->op == EqZ) {
+ // fold comparisons that flow into an EqZ
+ auto* child = curr->value->dyn_cast<Binary>();
+ if (child && (child->type == i32 || child->type == i64)) {
+ switch (child->op) {
+ case Eq: child->op = Ne; break;
+ case Ne: child->op = Eq; break;
+ case LtS: child->op = GeS; break;
+ case LtU: child->op = GeU; break;
+ case LeS: child->op = GtS; break;
+ case LeU: child->op = GtU; break;
+ case GtS: child->op = LeS; break;
+ case GtU: child->op = LeU; break;
+ case GeS: child->op = LtS; break;
+ case GeU: child->op = LtU; break;
+ default: return;
+ }
+ replaceCurrent(child);
+ }
+ }
+ }
};
static RegisterPass<OptimizeInstructions> registerPass("optimize-instructions", "optimizes instruction combinations");
diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm
index 6c19b4b2a..8ca7f7436 100644
--- a/test/emcc_O2_hello_world.fromasm
+++ b/test/emcc_O2_hello_world.fromasm
@@ -9587,11 +9587,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $i3)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $i3)
+ (i32.const 4)
)
)
(i32.store
@@ -9624,11 +9622,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $i3)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $i3)
+ (i32.const 0)
)
)
(i32.store8
@@ -9737,11 +9733,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i5)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i5)
)
)
(i32.store8
@@ -9760,11 +9754,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i7)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i7)
)
)
(i32.store
@@ -9783,11 +9775,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i4)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i4)
)
)
(i32.store8
diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise
index 6c19b4b2a..8ca7f7436 100644
--- a/test/emcc_O2_hello_world.fromasm.imprecise
+++ b/test/emcc_O2_hello_world.fromasm.imprecise
@@ -9587,11 +9587,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $i3)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $i3)
+ (i32.const 4)
)
)
(i32.store
@@ -9624,11 +9622,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $i3)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $i3)
+ (i32.const 0)
)
)
(i32.store8
@@ -9737,11 +9733,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i5)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i5)
)
)
(i32.store8
@@ -9760,11 +9754,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i7)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i7)
)
)
(i32.store
@@ -9783,11 +9775,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $i1)
- (get_local $i4)
- )
+ (i32.ge_s
+ (get_local $i1)
+ (get_local $i4)
)
)
(i32.store8
diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm
index 415e66f1d..60394be4f 100644
--- a/test/emcc_hello_world.fromasm
+++ b/test/emcc_hello_world.fromasm
@@ -28144,11 +28144,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $unaligned)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $unaligned)
)
)
(i32.store8
@@ -28167,11 +28165,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop4)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $stop4)
)
)
(i32.store
@@ -28190,11 +28186,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $stop)
)
)
(i32.store8
@@ -28414,11 +28408,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $num)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $num)
+ (i32.const 4)
)
)
(i32.store
@@ -28451,11 +28443,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $num)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $num)
+ (i32.const 0)
)
)
(i32.store8
diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise
index fbc9c0d0e..75ec71e5d 100644
--- a/test/emcc_hello_world.fromasm.imprecise
+++ b/test/emcc_hello_world.fromasm.imprecise
@@ -28142,11 +28142,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $unaligned)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $unaligned)
)
)
(i32.store8
@@ -28165,11 +28163,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop4)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $stop4)
)
)
(i32.store
@@ -28188,11 +28184,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $ptr)
- (get_local $stop)
- )
+ (i32.ge_s
+ (get_local $ptr)
+ (get_local $stop)
)
)
(i32.store8
@@ -28412,11 +28406,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $num)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $num)
+ (i32.const 4)
)
)
(i32.store
@@ -28449,11 +28441,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $num)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $num)
+ (i32.const 0)
)
)
(i32.store8
diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm
index 1692fc8db..872ac6db1 100644
--- a/test/memorygrowth.fromasm
+++ b/test/memorygrowth.fromasm
@@ -9653,11 +9653,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $c)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $c)
+ (i32.const 4)
)
)
(i32.store
@@ -9690,11 +9688,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $c)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $c)
+ (i32.const 0)
)
)
(i32.store8
@@ -9803,11 +9799,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $e)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $e)
)
)
(i32.store8
@@ -9826,11 +9820,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $g)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $g)
)
)
(i32.store
@@ -9849,11 +9841,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $d)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $d)
)
)
(i32.store8
diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise
index 1692fc8db..872ac6db1 100644
--- a/test/memorygrowth.fromasm.imprecise
+++ b/test/memorygrowth.fromasm.imprecise
@@ -9653,11 +9653,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.ge_s
- (get_local $c)
- (i32.const 4)
- )
+ (i32.lt_s
+ (get_local $c)
+ (i32.const 4)
)
)
(i32.store
@@ -9690,11 +9688,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.gt_s
- (get_local $c)
- (i32.const 0)
- )
+ (i32.le_s
+ (get_local $c)
+ (i32.const 0)
)
)
(i32.store8
@@ -9803,11 +9799,9 @@
)
(loop $while-out$0 $while-in$1
(br_if $while-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $e)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $e)
)
)
(i32.store8
@@ -9826,11 +9820,9 @@
)
(loop $while-out$2 $while-in$3
(br_if $while-out$2
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $g)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $g)
)
)
(i32.store
@@ -9849,11 +9841,9 @@
)
(loop $while-out$4 $while-in$5
(br_if $while-out$4
- (i32.eqz
- (i32.lt_s
- (get_local $a)
- (get_local $d)
- )
+ (i32.ge_s
+ (get_local $a)
+ (get_local $d)
)
)
(i32.store8
diff --git a/test/unit.fromasm b/test/unit.fromasm
index 93d654845..3d66f1c28 100644
--- a/test/unit.fromasm
+++ b/test/unit.fromasm
@@ -443,11 +443,9 @@
)
(loop $for-out$0 $for-in$1
(br_if $for-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $i)
- (i32.const 200)
- )
+ (i32.ge_s
+ (get_local $i)
+ (i32.const 200)
)
)
(call_import $h
diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise
index d555a5cf6..0c8da43e9 100644
--- a/test/unit.fromasm.imprecise
+++ b/test/unit.fromasm.imprecise
@@ -439,11 +439,9 @@
)
(loop $for-out$0 $for-in$1
(br_if $for-out$0
- (i32.eqz
- (i32.lt_s
- (get_local $i)
- (i32.const 200)
- )
+ (i32.ge_s
+ (get_local $i)
+ (i32.const 200)
)
)
(call_import $h