Skip to content

Fix issue around i64 neg one check in binaryen.js / asmjs #2925

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

Closed
wants to merge 3 commits into from
Closed
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: 5 additions & 5 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,8 @@ struct OptimizeInstructions
auto type = binary->right->type;
auto* right = binary->right->cast<Const>();
if (type.isInteger()) {
auto constRight = right->value.getInteger();
// operations on zero
if (constRight == 0LL) {
if (right->value == Literal::makeFromInt32(0, type)) {
if (binary->op == Abstract::getBinary(type, Abstract::Shl) ||
binary->op == Abstract::getBinary(type, Abstract::ShrU) ||
binary->op == Abstract::getBinary(type, Abstract::ShrS) ||
Expand All @@ -1382,7 +1381,7 @@ struct OptimizeInstructions
}
}
// operations on one
if (constRight == 1LL) {
if (right->value == Literal::makeFromInt32(1, type)) {
// (signed)x % 1 ==> 0
if (binary->op == Abstract::getBinary(type, Abstract::RemS) &&
!EffectAnalyzer(getPassOptions(), features, binary->left)
Expand All @@ -1392,7 +1391,8 @@ struct OptimizeInstructions
}
}
// operations on all 1s
if (constRight == -1LL) {
if (right->value == Literal(int32_t(-1)) ||
right->value == Literal(int64_t(-1))) {
if (binary->op == Abstract::getBinary(type, Abstract::And)) {
// x & -1 ==> x
return binary->left;
Expand Down Expand Up @@ -1447,7 +1447,7 @@ struct OptimizeInstructions
// subtractions than the more common additions).
if (binary->op == Abstract::getBinary(type, Abstract::Add) ||
binary->op == Abstract::getBinary(type, Abstract::Sub)) {
auto value = constRight;
auto value = right->value.getInteger();
if (value == 0x40 || value == 0x2000 || value == 0x100000 ||
value == 0x8000000 || value == 0x400000000LL ||
value == 0x20000000000LL || value == 0x1000000000000LL ||
Expand Down
18 changes: 18 additions & 0 deletions test/passes/optimize-instructions_all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3381,6 +3381,18 @@
)
)
(func $rhs-is-neg-one (param $x i32) (param $y i64) (param $fx f32) (param $fy f64)
(drop
(local.get $x)
)
(drop
(local.get $y)
)
(drop
(i64.and
(local.get $y)
(i64.const 4294967295)
)
)
(drop
(i32.sub
(local.get $x)
Expand Down Expand Up @@ -3422,6 +3434,12 @@
(drop
(i32.const 1)
)
(drop
(i64.le_u
(local.get $y)
(i64.const 4294967295)
)
)
(drop
(i32.le_s
(local.get $x)
Expand Down
16 changes: 16 additions & 0 deletions test/passes/optimize-instructions_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3842,6 +3842,18 @@
)
)
(func $rhs-is-neg-one (param $x i32) (param $y i64) (param $fx f32) (param $fy f64)
(drop (i32.and
(local.get $x)
(i32.const -1)
))
(drop (i64.and
(local.get $y)
(i64.const -1)
))
(drop (i64.and ;; skip
(local.get $y)
(i64.const 4294967295)
))
(drop (i32.sub
(local.get $x)
(i32.const -1)
Expand Down Expand Up @@ -3881,6 +3893,10 @@
(local.get $y)
(i64.const -1)
))
(drop (i64.le_u ;; skip
(local.get $y)
(i64.const 4294967295)
))
(drop (i32.le_s
(local.get $x)
(i32.const -1)
Expand Down