summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-09-16 17:55:58 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-09-16 18:28:28 -0700
commit0a9df805f688d5eab8be380ab7c9dde115d88852 (patch)
tree23f956226f783d2a72abfce8cf186909c7256367
parentf0546a0c8322a4e2f1777c8a749207a70cdca681 (diff)
downloadbinaryen-0a9df805f688d5eab8be380ab7c9dde115d88852.tar.gz
binaryen-0a9df805f688d5eab8be380ab7c9dde115d88852.tar.bz2
binaryen-0a9df805f688d5eab8be380ab7c9dde115d88852.zip
br_if returns its value
-rw-r--r--src/passes/RemoveUnusedBrs.cpp7
-rw-r--r--src/wasm-builder.h6
-rw-r--r--src/wasm-interpreter.h7
-rw-r--r--src/wasm.h6
-rw-r--r--test/emcc_O2_hello_world.fromasm12
-rw-r--r--test/emcc_O2_hello_world.fromasm.imprecise12
-rw-r--r--test/emcc_hello_world.fromasm64
-rw-r--r--test/emcc_hello_world.fromasm.imprecise64
-rw-r--r--test/example/c-api-kitchen-sink.txt16
-rw-r--r--test/example/c-api-kitchen-sink.txt.txt8
-rw-r--r--test/memorygrowth.fromasm12
-rw-r--r--test/memorygrowth.fromasm.imprecise12
-rw-r--r--test/passes/remove-unused-brs.txt8
-rw-r--r--test/passes/remove-unused-brs.wast8
-rw-r--r--test/unit.fromasm10
-rw-r--r--test/unit.fromasm.imprecise10
16 files changed, 157 insertions, 105 deletions
diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp
index fa8ab2f6c..15482de25 100644
--- a/src/passes/RemoveUnusedBrs.cpp
+++ b/src/passes/RemoveUnusedBrs.cpp
@@ -146,11 +146,10 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
// if without an else. try to reduce if (condition) br => br_if (condition)
Break* br = curr->ifTrue->dynCast<Break>();
if (br && !br->condition) { // TODO: if there is a condition, join them
- // if the br has a value, then if => br_if means we always execute the value, and also the order is value,condition vs condition,value
if (canTurnIfIntoBrIf(curr->condition, br->value)) {
br->condition = curr->condition;
br->finalize();
- replaceCurrent(br);
+ replaceCurrent(Builder(*getModule()).dropIfConcretelyTyped(br));
anotherCycle = true;
}
}
@@ -407,18 +406,18 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
auto* ifTrueBreak = iff->ifTrue->dynCast<Break>();
if (ifTrueBreak && !ifTrueBreak->condition && canTurnIfIntoBrIf(iff->condition, ifTrueBreak->value)) {
// we are an if-else where the ifTrue is a break without a condition, so we can do this
- list[i] = ifTrueBreak;
ifTrueBreak->condition = iff->condition;
ifTrueBreak->finalize();
+ list[i] = Builder(*getModule()).dropIfConcretelyTyped(ifTrueBreak);
ExpressionManipulator::spliceIntoBlock(curr, i + 1, iff->ifFalse);
continue;
}
// otherwise, perhaps we can flip the if
auto* ifFalseBreak = iff->ifFalse->dynCast<Break>();
if (ifFalseBreak && !ifFalseBreak->condition && canTurnIfIntoBrIf(iff->condition, ifFalseBreak->value)) {
- list[i] = ifFalseBreak;
ifFalseBreak->condition = Builder(*getModule()).makeUnary(EqZInt32, iff->condition);
ifFalseBreak->finalize();
+ list[i] = Builder(*getModule()).dropIfConcretelyTyped(ifFalseBreak);
ExpressionManipulator::spliceIntoBlock(curr, i + 1, iff->ifTrue);
continue;
}
diff --git a/src/wasm-builder.h b/src/wasm-builder.h
index f9b2e73f6..546d72391 100644
--- a/src/wasm-builder.h
+++ b/src/wasm-builder.h
@@ -339,6 +339,12 @@ public:
input->finalize();
return ret;
}
+
+ // Drop an expression if it has a concrete type
+ Expression* dropIfConcretelyTyped(Expression* curr) {
+ if (!isConcreteWasmType(curr->type)) return curr;
+ return makeDrop(curr);
+ }
};
} // namespace wasm
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h
index 292d6a521..5d4e7d7b4 100644
--- a/src/wasm-interpreter.h
+++ b/src/wasm-interpreter.h
@@ -155,18 +155,19 @@ public:
Flow visitBreak(Break *curr) {
NOTE_ENTER("Break");
bool condition = true;
- Flow flow(curr->name);
+ Flow flow;
if (curr->value) {
flow = visit(curr->value);
if (flow.breaking()) return flow;
- flow.breakTo = curr->name;
}
if (curr->condition) {
Flow conditionFlow = visit(curr->condition);
if (conditionFlow.breaking()) return conditionFlow;
condition = conditionFlow.value.getInteger() != 0;
+ if (!condition) return flow;
}
- return condition ? flow : Flow();
+ flow.breakTo = curr->name;
+ return flow;
}
Flow visitSwitch(Switch *curr) {
NOTE_ENTER("Switch");
diff --git a/src/wasm.h b/src/wasm.h
index 4d5cf4d70..1bce0bac9 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -1027,7 +1027,11 @@ public:
void finalize() {
if (condition) {
- type = none;
+ if (value) {
+ type = value->type;
+ } else {
+ type = none;
+ }
} else {
type = unreachable;
}
diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm
index b8e25baa5..072d9f97f 100644
--- a/test/emcc_O2_hello_world.fromasm
+++ b/test/emcc_O2_hello_world.fromasm
@@ -4674,11 +4674,13 @@
)
)
(block i32
- (br_if $do-once$67
- (i32.const 31)
- (i32.gt_u
- (get_local $15)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$67
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $15)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise
index f2b2c401c..233a485b8 100644
--- a/test/emcc_O2_hello_world.fromasm.imprecise
+++ b/test/emcc_O2_hello_world.fromasm.imprecise
@@ -4672,11 +4672,13 @@
)
)
(block i32
- (br_if $do-once$67
- (i32.const 31)
- (i32.gt_u
- (get_local $15)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$67
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $15)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm
index 24f6d4f75..a12c7fe56 100644
--- a/test/emcc_hello_world.fromasm
+++ b/test/emcc_hello_world.fromasm
@@ -4607,15 +4607,17 @@
(i32.const 1)
)
(block i32
- (br_if $do-once$64
- (get_local $6)
- (i32.and
- (get_local $16)
+ (drop
+ (br_if $do-once$64
+ (get_local $6)
(i32.and
- (get_local $12)
- (f64.eq
- (get_local $14)
- (f64.const 0)
+ (get_local $16)
+ (i32.and
+ (get_local $12)
+ (f64.eq
+ (get_local $14)
+ (f64.const 0)
+ )
)
)
)
@@ -4965,10 +4967,12 @@
)
)
)
- (br_if $do-once$70
- (get_local $9)
- (i32.eqz
- (get_local $5)
+ (drop
+ (br_if $do-once$70
+ (get_local $9)
+ (i32.eqz
+ (get_local $5)
+ )
)
)
(i32.store
@@ -5175,10 +5179,12 @@
)
)
)
- (br_if $do-once$78
- (get_local $5)
- (i32.eqz
- (get_local $9)
+ (drop
+ (br_if $do-once$78
+ (get_local $5)
+ (i32.eqz
+ (get_local $9)
+ )
)
)
(i32.store
@@ -5513,13 +5519,15 @@
(if
(get_local $30)
(block f64
- (br_if $do-once$90
- (get_local $22)
- (i32.ne
- (i32.load8_s
- (get_local $36)
+ (drop
+ (br_if $do-once$90
+ (get_local $22)
+ (i32.ne
+ (i32.load8_s
+ (get_local $36)
+ )
+ (i32.const 45)
)
- (i32.const 45)
)
)
(set_local $14
@@ -12607,11 +12615,13 @@
)
)
(block i32
- (br_if $do-once$69
- (i32.const 31)
- (i32.gt_u
- (get_local $2)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$69
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $2)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise
index c6123841a..75534025e 100644
--- a/test/emcc_hello_world.fromasm.imprecise
+++ b/test/emcc_hello_world.fromasm.imprecise
@@ -4600,15 +4600,17 @@
(i32.const 1)
)
(block i32
- (br_if $do-once$64
- (get_local $6)
- (i32.and
- (get_local $16)
+ (drop
+ (br_if $do-once$64
+ (get_local $6)
(i32.and
- (get_local $12)
- (f64.eq
- (get_local $14)
- (f64.const 0)
+ (get_local $16)
+ (i32.and
+ (get_local $12)
+ (f64.eq
+ (get_local $14)
+ (f64.const 0)
+ )
)
)
)
@@ -4958,10 +4960,12 @@
)
)
)
- (br_if $do-once$70
- (get_local $9)
- (i32.eqz
- (get_local $5)
+ (drop
+ (br_if $do-once$70
+ (get_local $9)
+ (i32.eqz
+ (get_local $5)
+ )
)
)
(i32.store
@@ -5168,10 +5172,12 @@
)
)
)
- (br_if $do-once$78
- (get_local $5)
- (i32.eqz
- (get_local $9)
+ (drop
+ (br_if $do-once$78
+ (get_local $5)
+ (i32.eqz
+ (get_local $9)
+ )
)
)
(i32.store
@@ -5506,13 +5512,15 @@
(if
(get_local $30)
(block f64
- (br_if $do-once$90
- (get_local $22)
- (i32.ne
- (i32.load8_s
- (get_local $36)
+ (drop
+ (br_if $do-once$90
+ (get_local $22)
+ (i32.ne
+ (i32.load8_s
+ (get_local $36)
+ )
+ (i32.const 45)
)
- (i32.const 45)
)
)
(set_local $14
@@ -12600,11 +12608,13 @@
)
)
(block i32
- (br_if $do-once$69
- (i32.const 31)
- (i32.gt_u
- (get_local $2)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$69
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $2)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 41d64089e..28d7b759b 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -422,9 +422,11 @@ BinaryenFloat64: 4
(i32.const 0)
)
)
- (br_if $the-value
- (i32.const 1)
- (i32.const 0)
+ (drop
+ (br_if $the-value
+ (i32.const 1)
+ (i32.const 0)
+ )
)
(br_if $the-nothing
(i32.const 2)
@@ -2013,9 +2015,11 @@ int main() {
(i32.const 0)
)
)
- (br_if $the-value
- (i32.const 1)
- (i32.const 0)
+ (drop
+ (br_if $the-value
+ (i32.const 1)
+ (i32.const 0)
+ )
)
(br_if $the-nothing
(i32.const 2)
diff --git a/test/example/c-api-kitchen-sink.txt.txt b/test/example/c-api-kitchen-sink.txt.txt
index 10a698717..f66b714eb 100644
--- a/test/example/c-api-kitchen-sink.txt.txt
+++ b/test/example/c-api-kitchen-sink.txt.txt
@@ -417,9 +417,11 @@
(i32.const 0)
)
)
- (br_if $the-value
- (i32.const 1)
- (i32.const 0)
+ (drop
+ (br_if $the-value
+ (i32.const 1)
+ (i32.const 0)
+ )
)
(br_if $the-nothing
(i32.const 2)
diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm
index a07d71fd4..7efde4442 100644
--- a/test/memorygrowth.fromasm
+++ b/test/memorygrowth.fromasm
@@ -4730,11 +4730,13 @@
)
)
(block i32
- (br_if $do-once$67
- (i32.const 31)
- (i32.gt_u
- (get_local $14)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$67
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $14)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise
index ea085352f..1a93b6e60 100644
--- a/test/memorygrowth.fromasm.imprecise
+++ b/test/memorygrowth.fromasm.imprecise
@@ -4728,11 +4728,13 @@
)
)
(block i32
- (br_if $do-once$67
- (i32.const 31)
- (i32.gt_u
- (get_local $14)
- (i32.const 16777215)
+ (drop
+ (br_if $do-once$67
+ (i32.const 31)
+ (i32.gt_u
+ (get_local $14)
+ (i32.const 16777215)
+ )
)
)
(i32.or
diff --git a/test/passes/remove-unused-brs.txt b/test/passes/remove-unused-brs.txt
index 3c51c8f99..da19d7ac8 100644
--- a/test/passes/remove-unused-brs.txt
+++ b/test/passes/remove-unused-brs.txt
@@ -152,9 +152,11 @@
(drop
(i32.const 12)
)
- (br_if $topmost
- (i32.const 1)
- (i32.const 1)
+ (drop
+ (br_if $topmost
+ (i32.const 1)
+ (i32.const 1)
+ )
)
)
(block $block3
diff --git a/test/passes/remove-unused-brs.wast b/test/passes/remove-unused-brs.wast
index 995ab2d3a..50d936dc7 100644
--- a/test/passes/remove-unused-brs.wast
+++ b/test/passes/remove-unused-brs.wast
@@ -158,9 +158,11 @@
(drop
(i32.const 12)
)
- (br_if $topmost
- (i32.const 1)
- (i32.const 1)
+ (drop
+ (br_if $topmost
+ (i32.const 1)
+ (i32.const 1)
+ )
)
)
(block $block3
diff --git a/test/unit.fromasm b/test/unit.fromasm
index ea0ff0843..fe8b616f4 100644
--- a/test/unit.fromasm
+++ b/test/unit.fromasm
@@ -493,10 +493,12 @@
)
(func $phi (result i32)
(block $do-once$0 i32
- (br_if $do-once$0
- (i32.const 0)
- (call $lb
- (i32.const 1)
+ (drop
+ (br_if $do-once$0
+ (i32.const 0)
+ (call $lb
+ (i32.const 1)
+ )
)
)
(i32.const 1)
diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise
index 5ea81aad3..8752f179a 100644
--- a/test/unit.fromasm.imprecise
+++ b/test/unit.fromasm.imprecise
@@ -474,10 +474,12 @@
)
(func $phi (result i32)
(block $do-once$0 i32
- (br_if $do-once$0
- (i32.const 0)
- (call $lb
- (i32.const 1)
+ (drop
+ (br_if $do-once$0
+ (i32.const 0)
+ (call $lb
+ (i32.const 1)
+ )
)
)
(i32.const 1)