Skip to content
Merged
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
42 changes: 34 additions & 8 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,16 +772,21 @@ struct OptimizeInstructions
return replaceCurrent(ret);
}
}
// bitwise operations
// for and and or, we can potentially conditionalize
if (curr->op == AndInt32 || curr->op == OrInt32) {
if (auto* ret = conditionalizeExpensiveOnBitwise(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)) {
// for or, we can potentially combine
if (curr->op == OrInt32) {
if (auto* ret = combineOr(curr)) {
return replaceCurrent(ret);
}
}
// bitwise operations
// for and and or, we can potentially conditionalize
if (auto* ret = conditionalizeExpensiveOnBitwise(curr)) {
return replaceCurrent(ret);
}
}
Expand Down Expand Up @@ -2486,6 +2491,27 @@ struct OptimizeInstructions
}
}

// We can combine `and` operations, e.g.
// (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;
}
}
return nullptr;
}

// We can combine `or` operations, e.g.
// (x > y) | (x == y) ==> x >= y
Expression* combineOr(Binary* binary) {
Expand Down
31 changes: 31 additions & 0 deletions test/lit/passes/optimize-instructions.wast
Original file line number Diff line number Diff line change
Expand Up @@ -10971,6 +10971,37 @@
)
))
)

;; CHECK: (func $optimize-combined-by-and-equals-to-zero (param $x i32) (param $y i32) (param $a i64) (param $b i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.eqz
;; CHECK-NEXT: (i64.or
;; CHECK-NEXT: (local.get $a)
;; CHECK-NEXT: (local.get $b)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $optimize-combined-by-and-equals-to-zero (param $x i32) (param $y i32) (param $a i64) (param $b i64)
;; (i32(x) == 0) & (i32(y) == 0) ==> i32(x | y) == 0
(drop (i32.and
(i32.eq (local.get $x) (i32.const 0))
(i32.eq (local.get $y) (i32.const 0))
))
;; (i64(x) == 0) & (i64(y) == 0) ==> i64(x | y) == 0
(drop (i32.and
(i64.eq (local.get $a) (i64.const 0))
(i64.eq (local.get $b) (i64.const 0))
))
)
;; CHECK: (func $optimize-relationals (param $x i32) (param $y i32) (param $X i64) (param $Y i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.eq
Expand Down