summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2021-10-05 19:06:15 +0300
committerGitHub <noreply@github.com>2021-10-05 16:06:15 +0000
commit653b9d028c12361d3e9e0c4008f8018990b765cb (patch)
tree04fd27a6cfffdbc702a77f62af9c9eef6c3e311f /src/passes/OptimizeInstructions.cpp
parent6feb8838887d21f2412176f7fdc1ab68b0e5c23d (diff)
downloadbinaryen-653b9d028c12361d3e9e0c4008f8018990b765cb.tar.gz
binaryen-653b9d028c12361d3e9e0c4008f8018990b765cb.tar.bz2
binaryen-653b9d028c12361d3e9e0c4008f8018990b765cb.zip
[OptimizeInstructions] Fold select into zero or single expression for some patterns (#4181)
i32(x) ? i32(x) : 0 ==> x i32(x) ? 0 : i32(x) ==> {x, 0} i64(x) == 0 ? 0 : i64(x) ==> x i64(x) != 0 ? i64(x) : 0 ==> x i64(x) == 0 ? i64(x) : 0 ==> {x, 0} i64(x) != 0 ? 0 : i64(x) ==> {x, 0}
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 3153af200..97f1cb756 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -1682,6 +1682,25 @@ private:
return true;
}
+ // Check if two consecutive inputs to an instruction are equal and can be
+ // folded into the first of the two. This identifies reads from the same local
+ // variable when one of them is a "tee" operation.
+ // The inputs here must be consecutive, but it is also ok to have code with no
+ // side effects at all in the middle. For example, a Const in between is ok.
+ bool areConsecutiveInputsEqualAndFoldable(Expression* left,
+ Expression* right) {
+ if (auto* set = left->dynCast<LocalSet>()) {
+ if (auto* get = right->dynCast<LocalGet>()) {
+ if (set->isTee() && get->index == set->index) {
+ return true;
+ }
+ }
+ }
+ // stronger property than we need - we can not only fold
+ // them but remove them entirely.
+ return areConsecutiveInputsEqualAndRemovable(left, right);
+ }
+
// Canonicalizing the order of a symmetric binary helps us
// write more concise pattern matching code elsewhere.
void canonicalize(Binary* binary) {
@@ -1872,6 +1891,44 @@ private:
s->ifTrue = ifFalse;
s->ifFalse = ifTrue;
s->condition = c;
+ return s;
+ }
+ }
+ {
+ // TODO: Remove this after landing SCCP pass. See: #4161
+
+ // i32(x) ? i32(x) : 0 ==> x
+ Expression *x, *y;
+ if (matches(curr, select(any(&x), i32(0), any(&y))) &&
+ areConsecutiveInputsEqualAndFoldable(x, y)) {
+ return curr->ifTrue;
+ }
+ // i32(x) ? 0 : i32(x) ==> { x, 0 }
+ if (matches(curr, select(i32(0), any(&x), any(&y))) &&
+ areConsecutiveInputsEqualAndFoldable(x, y)) {
+ return builder.makeSequence(builder.makeDrop(x), curr->ifTrue);
+ }
+
+ // i64(x) == 0 ? 0 : i64(x) ==> x
+ // i64(x) != 0 ? i64(x) : 0 ==> x
+ if ((matches(curr, select(i64(0), any(&x), unary(EqZInt64, any(&y)))) ||
+ matches(
+ curr,
+ select(any(&x), i64(0), binary(NeInt64, any(&y), i64(0))))) &&
+ areConsecutiveInputsEqualAndFoldable(x, y)) {
+ return curr->condition->is<Unary>() ? curr->ifFalse : curr->ifTrue;
+ }
+
+ // i64(x) == 0 ? i64(x) : 0 ==> { x, 0 }
+ // i64(x) != 0 ? 0 : i64(x) ==> { x, 0 }
+ if ((matches(curr, select(any(&x), i64(0), unary(EqZInt64, any(&y)))) ||
+ matches(
+ curr,
+ select(i64(0), any(&x), binary(NeInt64, any(&y), i64(0))))) &&
+ areConsecutiveInputsEqualAndFoldable(x, y)) {
+ return builder.makeSequence(
+ builder.makeDrop(x),
+ curr->condition->is<Unary>() ? curr->ifFalse : curr->ifTrue);
}
}
{