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 22 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 38 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,35 @@ 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);

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

// Check left's max bits and right is constant.
auto leftMaxBits = Bits::getMaxBits(left, this);
uint64_t maskLeft;
if (leftMaxBits == left->type.getByteSize() * 8) {
// If we know nothing useful about the bits on the left,
// we cannot optimize.
return nullptr;
} 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(left->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
207 changes: 198 additions & 9 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1442,9 +1442,12 @@
)
;; CHECK: (func $canonicalize-consts-vars (param $x i32) (param $y i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
Expand Down Expand Up @@ -1482,6 +1485,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 +2869,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 +6976,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 +7026,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 +7037,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 +7054,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 +17779,187 @@
(i32.const 1)
)
)
;; CHECK: (func $no-overlapping-bits-corner-case (param $0 i32) (param $1 i64)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local $y i64)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (i64.const 1)
;; 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: (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: (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 2147483647)
;; CHECK-NEXT: (i64.const 2147483649)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (i64.const 2)
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (i64.const 2147483648)
;; CHECK-NEXT: (i64.const 2147483647)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (i32.const -2147483648)
;; CHECK-NEXT: (i32.const 2147483647)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (i64.const -9223372036854775808)
;; CHECK-NEXT: (i64.const 9223372036854775807)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.and
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.and
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $no-overlapping-bits-corner-case (param $0 i32) (param $1 i64)
;; optimizeAndNoOverlappingBits simplifies AND operations where
;; - the left value covers bits in [0, n)
;; - the right operand is a constant with no bits in [0, n)
;; Result is simplified to zero.
(local $x i32)
(local $y i64)
(local.set $x
(i32.const 1)
)
(local.set $y
(i64.const 1)
)
;; No any bit overlapped, optimized.
(drop
(i32.and
(local.get $x)
(i32.const 2)
)
)
(drop
(i64.and
(local.get $y)
(i64.const 2)
)
)
;; Both-constant cases which meets the condition are also optimized.
(drop
(i32.and
(i32.const 1)
(i32.const 2)
)
)
(drop
(i64.and
(i64.const 1)
(i64.const 2)
)
)
(drop
(i64.and
(i64.const 0x7fffffff)
(i64.const 0x80000000)
)
)
;; One bit overlapped, so we can not optimized.
(drop
(i32.and
(i32.const 0x7fffffff)
(i32.const 0x80000001)
)
)
(drop
(i64.and
(i64.const 0x7fffffff)
(i64.const 0x80000001)
)
)
;; Both-constant cases which does not meets the condition are handled
;; by Precompute, so they are skipped here.
(drop
(i32.and
(i32.const 2)
(i32.const 1)
)
)
(drop
(i64.and
(i64.const 2)
(i64.const 1)
)
)
(drop
(i64.and
(i64.const 0x80000000)
(i64.const 0x7fffffff)
)
)
;; Know nothing useful about the bits on the left, so we can not optimize.
(drop
(i32.and
(i32.const 0x80000000)
(i32.const 0x7fffffff)
)
)
(drop
(i64.and
(i64.const 0x8000000000000000)
(i64.const 0x7fffffffffffffff)
)
)
(drop
(i32.and
(local.get $0)
(i32.const 1)
)
)
(drop
(i64.and
(local.get $1)
(i64.const 1)
)
)
)
)
Loading