Skip to content

Commit 1de1204

Browse files
committed
fix shift computation in getMaxBits - in wasm only the lower 5 bits matter for a 32-bit shift
1 parent 1d67ab0 commit 1de1204

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
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()), maxBits); // can ignore more shifts than zero us out
198+
auto shifts = std::min(Index(shift->value.geti32() & 31), 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()), maxBits); // can ignore more shifts than zero us out
207+
auto shifts = std::min(Index(shift->value.geti32() & 31), maxBits); // can ignore more shifts than zero us out
208208
return std::max(Index(0), maxBits - shifts);
209209
}
210210
return 32;

test/passes/optimize-instructions.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@
805805
(i32.const -1)
806806
(i32.const 2147483647)
807807
)
808-
(i32.const 32)
808+
(i32.const 31)
809809
)
810810
)
811811
(drop
@@ -2006,4 +2006,13 @@
20062006
(i32.const 1)
20072007
)
20082008
)
2009+
(func $neg-shifts-and-255 (type $2) (result i32)
2010+
(i32.and
2011+
(i32.shr_u
2012+
(i32.const -99)
2013+
(i32.const -32)
2014+
)
2015+
(i32.const 255)
2016+
)
2017+
)
20092018
)

test/passes/optimize-instructions.wast

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@
842842
(i32.const -1)
843843
(i32.const 2147483647)
844844
)
845-
(i32.const 32)
845+
(i32.const 31) ;; adjusted after we fixed shift computation to just look at lower 5 bits
846846
)
847847
(i32.const 24)
848848
)
@@ -2433,4 +2433,13 @@
24332433
(i32.const 1)
24342434
)
24352435
)
2436+
(func $neg-shifts-and-255 (result i32)
2437+
(i32.and
2438+
(i32.shr_u
2439+
(i32.const -99)
2440+
(i32.const -32) ;; this shift does nothing
2441+
)
2442+
(i32.const 255)
2443+
)
2444+
)
24362445
)

0 commit comments

Comments
 (0)