Skip to content

Commit 8cd3ad2

Browse files
authored
Fix pow2 util and avoid pow2 for left shifting in ZeroRemover (#3293)
Fixes a fuzz bug that was triggered by #3015 (comment) but was actually a pre-existing bug in pow2, that that PR just happened to uncover.
1 parent 5f7d263 commit 8cd3ad2

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,8 @@ struct OptimizeInstructions
11131113

11141114
struct SeekState {
11151115
Expression* curr;
1116-
int64_t mul;
1117-
SeekState(Expression* curr, int64_t mul) : curr(curr), mul(mul) {}
1116+
uint64_t mul;
1117+
SeekState(Expression* curr, uint64_t mul) : curr(curr), mul(mul) {}
11181118
};
11191119
std::vector<SeekState> seekStack;
11201120
seekStack.emplace_back(binary, 1);
@@ -1124,8 +1124,8 @@ struct OptimizeInstructions
11241124
auto curr = state.curr;
11251125
auto mul = state.mul;
11261126
if (auto* c = curr->dynCast<Const>()) {
1127-
int64_t value = c->value.getInteger();
1128-
if (value != 0LL) {
1127+
uint64_t value = c->value.getInteger();
1128+
if (value != 0ULL) {
11291129
constant += value * mul;
11301130
constants.push_back(c);
11311131
}
@@ -1147,17 +1147,19 @@ struct OptimizeInstructions
11471147
} else if (binary->op ==
11481148
Abstract::getBinary(binary->type, Abstract::Shl)) {
11491149
if (auto* c = binary->right->dynCast<Const>()) {
1150-
seekStack.emplace_back(
1151-
binary->left, mul * Bits::pow2(Bits::getEffectiveShifts(c)));
1150+
seekStack.emplace_back(binary->left,
1151+
mul << Bits::getEffectiveShifts(c));
11521152
continue;
11531153
}
11541154
} else if (binary->op ==
11551155
Abstract::getBinary(binary->type, Abstract::Mul)) {
11561156
if (auto* c = binary->left->dynCast<Const>()) {
1157-
seekStack.emplace_back(binary->right, mul * c->value.getInteger());
1157+
seekStack.emplace_back(binary->right,
1158+
mul * (uint64_t)c->value.getInteger());
11581159
continue;
11591160
} else if (auto* c = binary->right->dynCast<Const>()) {
1160-
seekStack.emplace_back(binary->left, mul * c->value.getInteger());
1161+
seekStack.emplace_back(binary->left,
1162+
mul * (uint64_t)c->value.getInteger());
11611163
continue;
11621164
}
11631165
}

src/support/bits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ uint32_t log2(uint32_t v) {
196196
return 31 - countLeadingZeroes(v);
197197
}
198198

199-
uint32_t pow2(uint32_t v) { return 1 << (v & 31); }
199+
uint32_t pow2(uint32_t v) { return v < 32 ? 1 << v : 0; }
200200

201201
} // namespace Bits
202202

test/passes/optimize-instructions_all-features.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $i32_i32_=>_none (func (param i32 i32)))
3-
(type $i32_=>_i32 (func (param i32) (result i32)))
43
(type $none_=>_i32 (func (result i32)))
4+
(type $i32_=>_i32 (func (param i32) (result i32)))
55
(type $i32_i64_=>_none (func (param i32 i64)))
66
(type $none_=>_none (func))
77
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
@@ -2362,6 +2362,15 @@
23622362
)
23632363
)
23642364
)
2365+
(func $zero-ops-64-special (result i32)
2366+
(return
2367+
(i32.wrap_i64
2368+
(i64.popcnt
2369+
(i64.const 7377)
2370+
)
2371+
)
2372+
)
2373+
)
23652374
(func $sign-ext-1-and-ne (result i32)
23662375
(i32.ne
23672376
(i32.and

test/passes/optimize-instructions_all-features.wast

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,21 @@
26832683
)
26842684
)
26852685
)
2686+
(func $zero-ops-64-special (result i32)
2687+
(return
2688+
(i32.wrap_i64
2689+
(i64.popcnt
2690+
(i64.sub
2691+
(i64.shl
2692+
(i64.const 4294783828)
2693+
(i64.const 17179869183)
2694+
)
2695+
(i64.const -7377)
2696+
)
2697+
)
2698+
)
2699+
)
2700+
)
26862701
(func $sign-ext-1-and-ne (result i32)
26872702
(select
26882703
(i32.ne

0 commit comments

Comments
 (0)