Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 150 additions & 14 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,17 +772,22 @@ struct OptimizeInstructions
return replaceCurrent(ret);
}
}
// bitwise operations
// for and and or, we can potentially conditionalize
if (curr->op == AndInt32 || curr->op == OrInt32) {
// bitwise operations
// for and and or, we can potentially conditionalize
if (auto* ret = conditionalizeExpensiveOnBitwise(curr)) {
return replaceCurrent(ret);
}
}
// for or, we can potentially combine
if (curr->op == OrInt32) {
if (auto* ret = combineOr(curr)) {
return replaceCurrent(ret);
if (curr->op == AndInt32) {
if (auto* ret = combineAnd(curr)) {
return replaceCurrent(ret);
}
}
// for or, we can potentially combine
if (curr->op == OrInt32) {
if (auto* ret = combineOr(curr)) {
return replaceCurrent(ret);
}
}
}
// relation/comparisons allow for math optimizations
Expand Down Expand Up @@ -2486,12 +2491,83 @@ struct OptimizeInstructions
}
}

// We can combine `and` operations, e.g.
// (x == 0) & (y == 0) ==> (x | y) == 0
// (x < 0) & (y < 0) ==> (x & y) < 0
Expression* combineAnd(Binary* curr) {
using namespace Abstract;
using namespace Match;
{
// (i32(x) == 0) & (i32(y) == 0) ==> i32(x | y) == 0
// (i64(x) == 0) & (i64(y) == 0) ==> i64(x | y) == 0
Expression *x, *y;
if (matches(curr,
binary(AndInt32, unary(EqZ, any(&x)), unary(EqZ, any(&y)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Unary>();
inner->value = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, Or), x, y);
return inner;
}
}
{
// (i32(x) < 0) & (i32(y) < 0) ==> i32(x & y) < 0
// (i64(x) < 0) & (i64(y) < 0) ==> i64(x & y) < 0
Expression *x, *y;
if (matches(curr,
binary(AndInt32,
binary(LtS, any(&x), ival(0)),
binary(LtS, any(&y), ival(0)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, And), x, y);
return inner;
}
}
{
// (i32(x) >= 0) & (i32(y) >= 0) => i32(x | y) >= 0
// (i64(x) >= 0) & (i64(y) >= 0) => i64(x | y) >= 0
Expression *x, *y;
if (matches(curr,
binary(AndInt32,
binary(GeS, any(&x), ival(0)),
binary(GeS, any(&y), ival(0)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, Or), x, y);
return inner;
}
}
{
// (i32(x) == -1) & (i32(y) == -1) ==> i32(x & y) == -1
// (i64(x) == -1) & (i64(y) == -1) ==> i64(x & y) == -1
Expression *x, *y;
if (matches(curr,
binary(AndInt32,
binary(Eq, any(&x), ival(-1)),
binary(Eq, any(&y), ival(-1)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, And), x, y);
return inner;
}
}
return nullptr;
}

// We can combine `or` operations, e.g.
// (x > y) | (x == y) ==> x >= y
Expression* combineOr(Binary* binary) {
assert(binary->op == OrInt32);
if (auto* left = binary->left->dynCast<Binary>()) {
if (auto* right = binary->right->dynCast<Binary>()) {
// (x != 0) | (y != 0) ==> (x | y) != 0
Expression* combineOr(Binary* curr) {
using namespace Abstract;
using namespace Match;

assert(curr->op == OrInt32);
if (auto* left = curr->left->dynCast<Binary>()) {
if (auto* right = curr->right->dynCast<Binary>()) {
if (left->op != right->op &&
ExpressionAnalyzer::equal(left->left, right->left) &&
ExpressionAnalyzer::equal(left->right, right->right) &&
Expand All @@ -2512,6 +2588,66 @@ struct OptimizeInstructions
}
}
}
{
// (i32(x) != 0) | (i32(y) != 0) ==> i32(x | y) != 0
// (i64(x) != 0) | (i64(y) != 0) ==> i64(x | y) != 0
Expression *x, *y;
if (matches(curr,
binary(OrInt32,
binary(Ne, any(&x), ival(0)),
binary(Ne, any(&y), ival(0)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, Or), x, y);
return inner;
}
}
{
// (i32(x) < 0) | (i32(y) < 0) ==> i32(x | y) < 0
// (i64(x) < 0) | (i64(y) < 0) ==> i64(x | y) < 0
Expression *x, *y;
if (matches(curr,
binary(OrInt32,
binary(LtS, any(&x), ival(0)),
binary(LtS, any(&y), ival(0)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, Or), x, y);
return inner;
}
}
{
// (i32(x) >= 0) | (i32(y) >= 0) => i32(x & y) >= 0
// (i64(x) >= 0) | (i64(y) >= 0) => i64(x & y) >= 0
Expression *x, *y;
if (matches(curr,
binary(OrInt32,
binary(GeS, any(&x), ival(0)),
binary(GeS, any(&y), ival(0)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, And), x, y);
return inner;
}
}
{
// (i32(x) != -1) | (i32(y) != -1) ==> i32(x & y) != -1
// (i64(x) != -1) | (i64(y) != -1) ==> i64(x & y) != -1
Expression *x, *y;
if (matches(curr,
binary(OrInt32,
binary(Ne, any(&x), ival(-1)),
binary(Ne, any(&y), ival(-1)))) &&
x->type == y->type) {
auto* inner = curr->left->cast<Binary>();
inner->left = Builder(*getModule())
.makeBinary(Abstract::getBinary(x->type, And), x, y);
return inner;
}
}
return nullptr;
}

Expand Down Expand Up @@ -3055,14 +3191,14 @@ struct OptimizeInstructions
}
}
}

using namespace Abstract;
using namespace Match;
// x - y == 0 => x == y
// x - y != 0 => x != y
// unsigned(x - y) > 0 => x != y
// unsigned(x - y) <= 0 => x == y
{
using namespace Abstract;
using namespace Match;

Binary* inner;
// unsigned(x - y) > 0 => x != y
if (matches(curr,
Expand Down
9 changes: 3 additions & 6 deletions test/lit/passes/inlining-optimizing_optimize-level=3.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8849,19 +8849,16 @@
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (local.get $6)
;; CHECK-NEXT: (local.tee $12
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.ne
;; CHECK-NEXT: (i32.ne
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (local.get $13)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.ne
;; CHECK-NEXT: (i32.load offset=4
;; CHECK-NEXT: (local.get $13)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down
Loading