Skip to content

Commit 04418d5

Browse files
committed
use effective shifts in more places in optimize-instructions
1 parent 5a07a93 commit 04418d5

5 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/ast/bits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct Bits {
5151
WASM_UNREACHABLE();
5252
}
5353

54-
static Index getEffectiveShifts(Const* amount) {
54+
static Index getEffectiveShifts(Expression* expr) {
55+
auto* amount = expr->cast<Const>();
5556
if (amount->type == i32) {
5657
return getEffectiveShifts(amount->value.geti32(), i32);
5758
} else if (amount->type == i64) {

src/ast/properties.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct Properties {
7979

8080
// gets the size of the sign-extended value
8181
static Index getSignExtBits(Expression* curr) {
82-
return 32 - curr->cast<Binary>()->right->cast<Const>()->value.geti32();
82+
return 32 - Bits::getEffectiveShifts(curr->cast<Binary>()->right);
8383
}
8484

8585
// Check if an expression is almost a sign-extend: perhaps the inner shift
@@ -93,7 +93,7 @@ struct Properties {
9393
if (auto* inner = outer->left->dynCast<Binary>()) {
9494
if (inner->op == ShlInt32) {
9595
if (auto* innerConst = inner->right->dynCast<Const>()) {
96-
if (outerConst->value.leU(innerConst->value).geti32()) {
96+
if (Bits::getEffectiveShifts(outerConst) <= Bits::getEffectiveShifts(innerConst)) {
9797
return inner->left;
9898
}
9999
}
@@ -109,8 +109,8 @@ struct Properties {
109109
// gets the size of the almost sign-extended value, as well as the
110110
// extra shifts, if any
111111
static Index getAlmostSignExtBits(Expression* curr, Index& extraShifts) {
112-
extraShifts = curr->cast<Binary>()->left->cast<Binary>()->right->cast<Const>()->value.geti32() -
113-
curr->cast<Binary>()->right->cast<Const>()->value.geti32();
112+
extraShifts = Bits::getEffectiveShifts(curr->cast<Binary>()->left->cast<Binary>()->right) -
113+
Bits::getEffectiveShifts(curr->cast<Binary>()->right);
114114
return getSignExtBits(curr);
115115
}
116116

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
779779
return;
780780
} else if (binary->op == ShlInt32) {
781781
if (auto* c = binary->right->dynCast<Const>()) {
782-
seek(binary->left, mul * Pow2(c->value.geti32()));
782+
seek(binary->left, mul * Pow2(Bits::getEffectiveShifts(c)));
783783
return;
784784
}
785785
} else if (binary->op == MulInt32) {
@@ -836,7 +836,7 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
836836
}
837837
} else if (curr->op == ShlInt32) {
838838
// shifting a 0 is a 0, unless the shift has side effects
839-
if (left && left->value.geti32() == 0 && !EffectAnalyzer(passOptions, curr->right).hasSideEffects()) {
839+
if (left && Bits::getEffectiveShifts(left) == 0 && !EffectAnalyzer(passOptions, curr->right).hasSideEffects()) {
840840
replaceCurrent(left);
841841
return;
842842
}

test/passes/optimize-instructions.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,4 +2061,19 @@
20612061
(i32.const 9)
20622062
)
20632063
)
2064+
(func $mix-shifts (type $2) (result i32)
2065+
(i32.shr_s
2066+
(i32.shl
2067+
(i32.const 23)
2068+
(i32.const -61)
2069+
)
2070+
(i32.const 168)
2071+
)
2072+
)
2073+
(func $actually-no-shifts (type $2) (result i32)
2074+
(i32.const 33)
2075+
)
2076+
(func $less-shifts-than-it-seems (type $3) (param $x i32) (result i32)
2077+
(i32.const 4800)
2078+
)
20642079
)

test/passes/optimize-instructions.wast

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,4 +2496,34 @@
24962496
(i32.const 4098) ;; 2 bits effectively
24972497
)
24982498
)
2499+
(func $mix-shifts (result i32)
2500+
(i32.shr_s
2501+
(i32.shl
2502+
(i32.const 23)
2503+
(i32.const -61)
2504+
)
2505+
(i32.const 168)
2506+
)
2507+
)
2508+
(func $actually-no-shifts (result i32)
2509+
(i32.add
2510+
(i32.shl
2511+
(i32.const 23)
2512+
(i32.const 32) ;; really 0
2513+
)
2514+
(i32.const 10)
2515+
)
2516+
)
2517+
(func $less-shifts-than-it-seems (param $x i32) (result i32)
2518+
(i32.add
2519+
(i32.shl
2520+
(i32.const 200)
2521+
(i32.const 36) ;; really 4
2522+
)
2523+
(i32.shl
2524+
(i32.const 100)
2525+
(i32.const 4)
2526+
)
2527+
)
2528+
)
24992529
)

0 commit comments

Comments
 (0)