Skip to content

Commit 3be188b

Browse files
OptimizeInstructions: Optimize eqz of fallthrough const (#7558)
Precompute doesn't do math computations like (i32.eqz (local.tee $x (i32.const 1) ) ) It could emit 0 on the outside, but it would repeat the operation each time the pass is run, which we don't want Instead, handle this in OptimizeInstructions. Fixes: #7492
1 parent 0934d6c commit 3be188b

File tree

3 files changed

+339
-16
lines changed

3 files changed

+339
-16
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,12 @@ struct OptimizeInstructions
28912891
}
28922892
}
28932893
}
2894+
if (unary->op == EqZInt32 || unary->op == EqZInt64) {
2895+
if (auto* c = getFallthrough(unary->value)->dynCast<Const>()) {
2896+
return getDroppedChildrenAndAppend(
2897+
unary, Literal::makeFromInt32(c->value.isZero(), Type::i32));
2898+
}
2899+
}
28942900
}
28952901
} else if (auto* binary = boolean->dynCast<Binary>()) {
28962902
if (binary->op == SubInt32) {

test/lit/passes/optimize-instructions-ignore-traps.wast

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@
688688

689689
;; CHECK: (func $conditionalize-if-type-change (type $3) (result f64)
690690
;; CHECK-NEXT: (local $0 i32)
691+
;; CHECK-NEXT: (local $1 i64)
691692
;; CHECK-NEXT: (drop
692693
;; CHECK-NEXT: (loop $label$1 (result f32)
693694
;; CHECK-NEXT: (block $label$2 (result f32)
@@ -709,7 +710,7 @@
709710
;; CHECK-NEXT: (i64.const 58)
710711
;; CHECK-NEXT: (i64.const -982757)
711712
;; CHECK-NEXT: (i64.eqz
712-
;; CHECK-NEXT: (i64.const 0)
713+
;; CHECK-NEXT: (local.get $1)
713714
;; CHECK-NEXT: )
714715
;; CHECK-NEXT: )
715716
;; CHECK-NEXT: )
@@ -725,6 +726,7 @@
725726
;; CHECK-NEXT: )
726727
(func $conditionalize-if-type-change (result f64)
727728
(local $0 i32)
729+
(local $1 i64)
728730
(drop
729731
(loop $label$1 (result f32)
730732
(block $label$2 (result f32)
@@ -746,7 +748,7 @@
746748
(i64.const 58)
747749
(i64.const -982757)
748750
(i64.eqz
749-
(i64.const 0)
751+
(local.get $1)
750752
)
751753
)
752754
)

0 commit comments

Comments
 (0)