Skip to content

Commit db51c2e

Browse files
committed
refactor effective shift size computation
1 parent bd7f7ca commit db51c2e

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/ast/bits.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ struct Bits {
3939
// this is indeed a mask
4040
return 32 - CountLeadingZeroes(mask);
4141
}
42+
43+
// gets the number of effective shifts a shift operation does. In
44+
// wasm, only 5 bits matter for 32-bit shifts, and 6 for 64.
45+
static uint32_t getEffectiveShifts(Const* amount) {
46+
if (amount->type == i32) {
47+
return amount->value.geti32() & 31;
48+
} else if (amount->type == i64) {
49+
return amount->value.geti64() & 63;
50+
}
51+
WASM_UNREACHABLE();
52+
}
4253
};
4354

4455
} // namespace wasm

src/passes/OptimizeInstructions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
188188
case OrInt32: case XorInt32: return std::max(getMaxBits(binary->left, localInfoProvider), getMaxBits(binary->right, localInfoProvider));
189189
case ShlInt32: {
190190
if (auto* shifts = binary->right->dynCast<Const>()) {
191-
return std::min(Index(32), getMaxBits(binary->left, localInfoProvider) + (shifts->value.geti32() & 31));
191+
return std::min(Index(32), getMaxBits(binary->left, localInfoProvider) + Bits::getEffectiveShifts(shifts));
192192
}
193193
return 32;
194194
}
195195
case ShrUInt32: {
196196
if (auto* shift = binary->right->dynCast<Const>()) {
197197
auto maxBits = getMaxBits(binary->left, localInfoProvider);
198-
auto shifts = std::min(Index(shift->value.geti32() & 31), maxBits); // can ignore more shifts than zero us out
198+
auto shifts = std::min(Index(Bits::getEffectiveShifts(shift)), maxBits); // can ignore more shifts than zero us out
199199
return std::max(Index(0), maxBits - shifts);
200200
}
201201
return 32;
@@ -204,7 +204,7 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
204204
if (auto* shift = binary->right->dynCast<Const>()) {
205205
auto maxBits = getMaxBits(binary->left, localInfoProvider);
206206
if (maxBits == 32) return 32;
207-
auto shifts = std::min(Index(shift->value.geti32() & 31), maxBits); // can ignore more shifts than zero us out
207+
auto shifts = std::min(Index(Bits::getEffectiveShifts(shift)), maxBits); // can ignore more shifts than zero us out
208208
return std::max(Index(0), maxBits - shifts);
209209
}
210210
return 32;

0 commit comments

Comments
 (0)