Skip to content

Commit 6d686bd

Browse files
committed
fix off-by-one error in clz/ctz/popcount used bits computation
1 parent 114ed43 commit 6d686bd

3 files changed

Lines changed: 95 additions & 11 deletions

File tree

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
228228
}
229229
} else if (auto* unary = curr->dynCast<Unary>()) {
230230
switch (unary->op) {
231-
case ClzInt32: case CtzInt32: case PopcntInt32: return 5;
232-
case ClzInt64: case CtzInt64: case PopcntInt64: return 6;
231+
case ClzInt32: case CtzInt32: case PopcntInt32: return 6;
232+
case ClzInt64: case CtzInt64: case PopcntInt64: return 7;
233233
case EqZInt32: case EqZInt64: return 1;
234234
case WrapInt64: return std::min(Index(32), getMaxBits(unary->value, localInfoProvider));
235235
default: {}

test/passes/optimize-instructions.txt

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
(type $5 (func (param i32)))
88
(type $6 (func (param i32 i32) (result i32)))
99
(type $7 (func (param i64) (result i64)))
10+
(type $8 (func (result i64)))
1011
(memory $0 0)
1112
(export "load-off-2" (func $load-off-2))
1213
(func $f (type $0) (param $i1 i32) (param $i2 i64)
@@ -827,11 +828,14 @@
827828
)
828829
)
829830
(drop
830-
(i32.shl
831-
(i32.clz
832-
(i32.const 0)
831+
(i32.shr_s
832+
(i32.shl
833+
(i32.clz
834+
(i32.const 0)
835+
)
836+
(i32.const 26)
833837
)
834-
(i32.const 2)
838+
(i32.const 24)
835839
)
836840
)
837841
(drop
@@ -853,13 +857,16 @@
853857
)
854858
)
855859
(drop
856-
(i32.shl
857-
(i32.wrap/i64
858-
(i64.clz
859-
(i64.const 0)
860+
(i32.shr_s
861+
(i32.shl
862+
(i32.wrap/i64
863+
(i64.clz
864+
(i64.const 0)
865+
)
860866
)
867+
(i32.const 25)
861868
)
862-
(i32.const 1)
869+
(i32.const 24)
863870
)
864871
)
865872
(drop
@@ -2076,4 +2083,41 @@
20762083
(func $less-shifts-than-it-seems (type $3) (param $x i32) (result i32)
20772084
(i32.const 4800)
20782085
)
2086+
(func $and-popcount32 (type $2) (result i32)
2087+
(i32.and
2088+
(i32.popcnt
2089+
(i32.const -1)
2090+
)
2091+
(i32.const 31)
2092+
)
2093+
)
2094+
(func $and-popcount32-big (type $2) (result i32)
2095+
(i32.popcnt
2096+
(i32.const -1)
2097+
)
2098+
)
2099+
(func $and-popcount64 (type $8) (result i64)
2100+
(i64.and
2101+
(i64.popcnt
2102+
(i64.const -1)
2103+
)
2104+
(i64.const 63)
2105+
)
2106+
)
2107+
(func $and-popcount64-big (type $8) (result i64)
2108+
(i64.and
2109+
(i64.popcnt
2110+
(i64.const -1)
2111+
)
2112+
(i64.const 127)
2113+
)
2114+
)
2115+
(func $and-popcount64-bigger (type $8) (result i64)
2116+
(i64.and
2117+
(i64.popcnt
2118+
(i64.const -1)
2119+
)
2120+
(i64.const 255)
2121+
)
2122+
)
20792123
)

test/passes/optimize-instructions.wast

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,4 +2526,44 @@
25262526
)
25272527
)
25282528
)
2529+
(func $and-popcount32 (result i32)
2530+
(i32.and
2531+
(i32.popcnt
2532+
(i32.const -1)
2533+
)
2534+
(i32.const 31)
2535+
)
2536+
)
2537+
(func $and-popcount32-big (result i32)
2538+
(i32.and
2539+
(i32.popcnt
2540+
(i32.const -1)
2541+
)
2542+
(i32.const 63)
2543+
)
2544+
)
2545+
(func $and-popcount64 (result i64) ;; these are TODOs
2546+
(i64.and
2547+
(i64.popcnt
2548+
(i64.const -1)
2549+
)
2550+
(i64.const 63)
2551+
)
2552+
)
2553+
(func $and-popcount64-big (result i64)
2554+
(i64.and
2555+
(i64.popcnt
2556+
(i64.const -1)
2557+
)
2558+
(i64.const 127)
2559+
)
2560+
)
2561+
(func $and-popcount64-bigger (result i64)
2562+
(i64.and
2563+
(i64.popcnt
2564+
(i64.const -1)
2565+
)
2566+
(i64.const 255)
2567+
)
2568+
)
25292569
)

0 commit comments

Comments
 (0)