Skip to content

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

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

Merged
merged 11 commits into from
Apr 24, 2025
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
10 changes: 6 additions & 4 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,13 @@ struct OptimizeInstructions
return replaceCurrent(getDroppedChildrenAndAppend(curr, c));
}
// unsigned(x) < 0 => i32(0)
if (matches(curr, binary(LtU, any(&x), ival(&c))) &&
if (curr->op == Abstract::getBinary(curr->left->type, Abstract::LtU) &&
(c = getFallthrough(curr->right)->dynCast<Const>()) &&
c->value.isZero()) {
c->value = Literal::makeZero(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 zero =
Builder(*getModule()).makeConst(Literal::makeZero(Type::i32));
return replaceCurrent(getDroppedChildrenAndAppend(curr, zero));
}
}
}
Expand Down
66 changes: 66 additions & 0 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
)
)
)

;; CHECK: (func $eqz-gt_s (result i32)
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (i32.const 0)
Expand Down Expand Up @@ -11531,6 +11532,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 0)
;; 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 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.ne
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (i32.const 0)
Expand Down Expand Up @@ -11803,6 +11842,33 @@
)
(i64.const 0)
))
(drop (i32.lt_u
(i32.load
(i32.const 0)
)
(block (result i32)
(i32.store
(i32.const 0)
(i32.const 0)
)
(i32.const 0)
)
)
)
(drop (i64.lt_u
(i64.load
(i32.const 0)
)
(block (result i64)
(i64.store
(i32.const 0)
(i64.const 0)
)
(i64.const 0)
)
)
)


;; (unsigned)x > 0 => x != 0
(drop (i32.gt_u
Expand Down
Loading