Skip to content

Commit 45d88e3

Browse files
authored
fix optimize-instructions handling of shifts by a zero or of a zero when combining added constants (#1206)
1 parent 2ec15be commit 45d88e3

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,9 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
862862
return;
863863
}
864864
} else if (curr->op == ShlInt32) {
865-
// shifting a 0 is a 0, unless the shift has side effects
866-
if (left && Bits::getEffectiveShifts(left) == 0 && !EffectAnalyzer(passOptions, curr->right).hasSideEffects()) {
865+
// shifting a 0 is a 0, or anything by 0 has no effect, all unless the shift has side effects
866+
if (((left && left->value.geti32() == 0) || (right && Bits::getEffectiveShifts(right) == 0)) &&
867+
!EffectAnalyzer(passOptions, curr->right).hasSideEffects()) {
867868
replaceCurrent(left);
868869
return;
869870
}

test/passes/optimize-instructions.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,4 +2157,26 @@
21572157
(i64.const 255)
21582158
)
21592159
)
2160+
(func $optimizeAddedConstants-filters-through-nonzero (type $2) (result i32)
2161+
(i32.add
2162+
(i32.shl
2163+
(i32.const -536870912)
2164+
(i32.wrap/i64
2165+
(i64.const 0)
2166+
)
2167+
)
2168+
(i32.const -31744)
2169+
)
2170+
)
2171+
(func $optimizeAddedConstants-filters-through-nonzero-b (type $2) (result i32)
2172+
(i32.add
2173+
(i32.shl
2174+
(i32.const -536870912)
2175+
(i32.wrap/i64
2176+
(i64.const -1)
2177+
)
2178+
)
2179+
(i32.const -31744)
2180+
)
2181+
)
21602182
)

test/passes/optimize-instructions.wast

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,4 +2587,32 @@
25872587
(i64.const 255)
25882588
)
25892589
)
2590+
(func $optimizeAddedConstants-filters-through-nonzero (result i32)
2591+
(i32.sub
2592+
(i32.add
2593+
(i32.shl
2594+
(i32.const -536870912)
2595+
(i32.wrap/i64
2596+
(i64.const 0)
2597+
)
2598+
)
2599+
(i32.const -32768)
2600+
)
2601+
(i32.const -1024)
2602+
)
2603+
)
2604+
(func $optimizeAddedConstants-filters-through-nonzero-b (result i32)
2605+
(i32.sub
2606+
(i32.add
2607+
(i32.shl
2608+
(i32.const -536870912)
2609+
(i32.wrap/i64
2610+
(i64.const -1)
2611+
)
2612+
)
2613+
(i32.const -32768)
2614+
)
2615+
(i32.const -1024)
2616+
)
2617+
)
25902618
)

0 commit comments

Comments
 (0)