Skip to content

OptimizeInstruction: Optimize any boolean & (No overlap with boolean's LSB) ==> 0 #7505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
55 changes: 55 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ struct OptimizeInstructions
if (auto* ret = combineAnd(curr)) {
return replaceCurrent(ret);
}
if (auto* ret = optimizeAndNoOverlappingBits(curr)) {
return replaceCurrent(ret);
}
}
// for or, we can potentially combine
if (curr->op == OrInt32) {
Expand All @@ -850,6 +853,12 @@ struct OptimizeInstructions
return replaceCurrent(ret);
}
}
if (curr->op == AndInt64) {
if (auto* ret = optimizeAndNoOverlappingBits(curr)) {
return replaceCurrent(ret);
}
}

// relation/comparisons allow for math optimizations
if (curr->isRelational()) {
if (auto* ret = optimizeRelational(curr)) {
Expand Down Expand Up @@ -3549,6 +3558,52 @@ struct OptimizeInstructions
return nullptr;
}

// Bitwise AND of a value with bits in [0, n) and a constant with no bits in
// [0, n) always yields 0. Replace with zero.
Expression* optimizeAndNoOverlappingBits(Binary* curr) {
assert(curr->op == AndInt32 || curr->op == AndInt64);

using namespace Abstract;
using namespace Match;

auto* left = curr->left;
auto* right = curr->right;

// Check right as constant and left's max bits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment here is in the opposite order of the code. I would reverse it, to follow the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check right as constant and left's max bits
// Check right is constant and left's max bits

auto leftMaxBits = Bits::getMaxBits(left, this);
uint64_t maskLeft;
if (leftMaxBits == 64) {
maskLeft = 0xffffffffffffffffULL; // All bits set for 64-bit case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just return nullptr in this case? If we know nothing useful about the bits on the left, we can't optimize.

} else {
maskLeft = (1ULL << leftMaxBits) - 1;
}
if (auto* c = right->dynCast<Const>()) {
uint64_t constantValue = c->value.getInteger();
if ((constantValue & maskLeft) == 0) {
return getDroppedChildrenAndAppend(
curr, LiteralUtils::makeZero(curr->left->type, *getModule()));
}
}

// Check left as constant and right's max bits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should not be needed. We canonicalize code so that constants are on the right, in part to help writing optimizations like this.

So you can assume the constant is on the right. (And let me know if you see a case where that is not true - it is a bug.)

auto rightMaxBits = Bits::getMaxBits(right, this);
uint64_t maskRight;
if (rightMaxBits == 64) {
maskRight = 0xffffffffffffffffULL; // All bits set for 64-bit case
} else {
maskRight = (1ULL << rightMaxBits) - 1;
}
if (auto* c = left->dynCast<Const>()) {
uint64_t constantValue = c->value.getInteger();
if ((constantValue & maskRight) == 0) {
return getDroppedChildrenAndAppend(
curr, LiteralUtils::makeZero(curr->right->type, *getModule()));
}
}

return nullptr;
}

// We can combine `or` operations, e.g.
// (x > y) | (x == y) ==> x >= y
// (x != 0) | (y != 0) ==> (x | y) != 0
Expand Down
157 changes: 137 additions & 20 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,7 @@
;; CHECK-NEXT: (i32.const 100)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const 100)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $and-neg1
Expand All @@ -1393,10 +1390,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const 100)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
Expand Down Expand Up @@ -1442,10 +1436,10 @@
)
;; CHECK: (func $canonicalize-consts-vars (param $x i32) (param $y i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
Expand Down Expand Up @@ -1482,6 +1476,7 @@
;; CHECK-NEXT: )
(func $canonicalize-consts-vars (param $x i32) (param $y i32)
(drop (i32.and (i32.const 1) (i32.const 2)))
(drop (i32.and (i32.const 2) (i32.const 1)))
(drop (i32.and (local.get $x) (i32.const 3)))
(drop (i32.and (i32.const 4) (local.get $x)))
(drop (i32.and (local.get $x) (local.get $y)))
Expand Down Expand Up @@ -2865,18 +2860,18 @@
(i32.const 24)
)
)
;; CHECK: (func $sext-24-and-127-128 (result i32)
;; CHECK: (func $sext-24-and-127-unknown (param $x i32) (result i32)
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 127)
;; CHECK-NEXT: (i32.const 128)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $sext-24-and-127-128 (result i32)
(func $sext-24-and-127-unknown (param $x i32) (result i32)
(i32.shr_s
(i32.shl
(i32.and ;; takes the min, here it is ok
(i32.const 127)
(i32.const 128)
(local.get $x)
)
(i32.const 24)
)
Expand Down Expand Up @@ -6972,7 +6967,7 @@
)
)
)
;; CHECK: (func $de-morgan-2 (param $x i32) (param $y i32)
;; CHECK: (func $de-morgan-2 (param $x i32) (param $y i32) (param $z i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (i32.or
Expand Down Expand Up @@ -7022,7 +7017,9 @@
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (local.get $z)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
Expand All @@ -7031,7 +7028,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $de-morgan-2 (param $x i32) (param $y i32)
(func $de-morgan-2 (param $x i32) (param $y i32) (param $z i64)
(drop
(i32.and (i32.eqz (local.get $x)) (i32.eqz (local.get $y)))
)
Expand All @@ -7048,7 +7045,7 @@
(i32.and (local.get $x) (i32.eqz (local.get $y)))
)
(drop
(i32.and (i32.eqz (local.get $x)) (i32.wrap_i64 (i64.const 2)))
(i32.and (i32.eqz (local.get $x)) (i32.wrap_i64 (local.get $z)))
)
(drop
(i32.and (i32.wrap_i64 (i64.const 1)) (i32.eqz (local.get $y)))
Expand Down Expand Up @@ -17773,4 +17770,124 @@
(i32.const 1)
)
)
;; CHECK: (func $no-overlapping-bits-corner-case (param $x i32) (param $y i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const -2147483647)
;; CHECK-NEXT: (i32.const 2147483647)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (i64.const 2147483649)
;; CHECK-NEXT: (i64.const 2147483647)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $no-overlapping-bits-corner-case (param $x i32) (param $y i64)
;; optimizeAndNoOverlappingBits will simplify AND operations where
;; Bitwise AND of a value with bits in [0, n) and a constant with no bits in
;; [0, n) always yields 0. Replace with zero.
;; Note: after swapping the operands, it also satisfies the commutative law.

;; Unknown, not optimized
(drop
(i32.and
(local.get $x)
(i32.const 1)
)
)
(drop
(i64.and
(local.get $y)
(i64.const 1)
)
)
(drop
(i32.and
(i32.const 1)
(local.get $x)
)
)
(drop
(i64.and
(i64.const 1)
(local.get $y)
)
)
;; No any bit overlapping, optimized
(drop
(i32.and
(i32.const 0x80000000)
(i32.const 0x7fffffff)
)
)
(drop
(i64.and
(i64.const 2)
(i64.const 1)
)
)
(drop
(i64.and
(i64.const 0x80000000)
(i64.const 0x7fffffff)
)
)
(drop
(i64.and
(i64.const 0x8000000000000000)
(i64.const 0x7fffffffffffffff)
)
)
;; Just one bit overlapping, not optimized
(drop
(i32.and
(i32.const 0x80000001)
(i32.const 0x7fffffff)
)
)
(drop
(i64.and
(i64.const 0x80000001)
(i64.const 0x7fffffff)
)
)
)
)
Loading