Skip to content

Use fallthrough in optimizeInstructions to further optimize (unsigned)x >= 0 ==> i32(0) #7557

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 2 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
11 changes: 6 additions & 5 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,13 @@ struct OptimizeInstructions
{
// unsigned(x) >= 0 => i32(1)
Const* c;
Expression* x;
if (matches(curr, binary(GeU, any(&x), ival(&c))) &&
if (curr->op == Abstract::getBinary(curr->left->type, Abstract::GeU) &&
(c = getFallthrough(curr->right)->dynCast<Const>()) &&
c->value.isZero()) {
c->value = Literal::makeOne(Type::i32);
c->type = Type::i32;
return replaceCurrent(getDroppedChildrenAndAppend(curr, c));
// We could reuse c here, if we checked it had no more uses
auto one =
Builder(*getModule()).makeConst(Literal::makeOne(Type::i32));
return replaceCurrent(getDroppedChildrenAndAppend(curr, one));
}
// unsigned(x) < 0 => i32(0)
if (curr->op == Abstract::getBinary(curr->left->type, Abstract::LtU) &&
Expand Down
64 changes: 64 additions & 0 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -11550,6 +11550,44 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.load
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (i64.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
Expand Down Expand Up @@ -11884,6 +11922,32 @@
)
(i64.const 0)
))
(drop (i32.ge_u
(i32.load
(i32.const 0)
)
(block (result i32)
(i32.store
(i32.const 0)
(i32.const 0)
)
(i32.const 0)
)
)
)
(drop (i64.ge_u
(i64.load
(i32.const 0)
)
(block (result i64)
(i64.store
(i32.const 0)
(i64.const 0)
)
(i64.const 0)
)
)
)

;; (unsigned)x < 0 => i32(0)
(drop (i32.lt_u
Expand Down
Loading