Skip to content

Commit b0d51f5

Browse files
OptimizeInstructions: Optimize unsigned(x) >= 0 => i32(1) even with side effects (#7429)
Fixes #7425
1 parent 16dbac1 commit b0d51f5

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,15 +553,13 @@ struct OptimizeInstructions
553553
}
554554
{
555555
// unsigned(x) >= 0 => i32(1)
556-
// TODO: Use getDroppedChildrenAndAppend() here, so we can optimize even
557-
// if pure.
558556
Const* c;
559557
Expression* x;
560-
if (matches(curr, binary(GeU, pure(&x), ival(&c))) &&
558+
if (matches(curr, binary(GeU, any(&x), ival(&c))) &&
561559
c->value.isZero()) {
562560
c->value = Literal::makeOne(Type::i32);
563561
c->type = Type::i32;
564-
return replaceCurrent(c);
562+
return replaceCurrent(getDroppedChildrenAndAppend(curr, c));
565563
}
566564
// unsigned(x) < 0 => i32(0)
567565
if (matches(curr, binary(LtU, pure(&x), ival(&c))) &&

test/lit/passes/optimize-instructions-mvp.wast

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11450,9 +11450,29 @@
1145011450
;; CHECK-NEXT: (i32.const 1)
1145111451
;; CHECK-NEXT: )
1145211452
;; CHECK-NEXT: (drop
11453+
;; CHECK-NEXT: (block (result i32)
11454+
;; CHECK-NEXT: (drop
11455+
;; CHECK-NEXT: (i32.load
11456+
;; CHECK-NEXT: (i32.const 0)
11457+
;; CHECK-NEXT: )
11458+
;; CHECK-NEXT: )
11459+
;; CHECK-NEXT: (i32.const 1)
11460+
;; CHECK-NEXT: )
11461+
;; CHECK-NEXT: )
11462+
;; CHECK-NEXT: (drop
1145311463
;; CHECK-NEXT: (i32.const 1)
1145411464
;; CHECK-NEXT: )
1145511465
;; CHECK-NEXT: (drop
11466+
;; CHECK-NEXT: (block (result i32)
11467+
;; CHECK-NEXT: (drop
11468+
;; CHECK-NEXT: (i64.load
11469+
;; CHECK-NEXT: (i32.const 0)
11470+
;; CHECK-NEXT: )
11471+
;; CHECK-NEXT: )
11472+
;; CHECK-NEXT: (i32.const 1)
11473+
;; CHECK-NEXT: )
11474+
;; CHECK-NEXT: )
11475+
;; CHECK-NEXT: (drop
1145611476
;; CHECK-NEXT: (i32.const 0)
1145711477
;; CHECK-NEXT: )
1145811478
;; CHECK-NEXT: (drop
@@ -11659,10 +11679,22 @@
1165911679
(local.get $x)
1166011680
(i32.const 0)
1166111681
))
11682+
(drop (i32.ge_u
11683+
(i32.load
11684+
(i32.const 0)
11685+
)
11686+
(i32.const 0)
11687+
))
1166211688
(drop (i64.ge_u
1166311689
(local.get $y)
1166411690
(i64.const 0)
1166511691
))
11692+
(drop (i64.ge_u
11693+
(i64.load
11694+
(i32.const 0)
11695+
)
11696+
(i64.const 0)
11697+
))
1166611698

1166711699
;; (unsigned)x < 0 => i32(0)
1166811700
(drop (i32.lt_u
@@ -17389,7 +17421,7 @@
1738917421
)
1739017422

1739117423
;; CHECK: (func $skip-added-constants-zero-b (result i32)
17392-
;; CHECK-NEXT: (i32.ge_u
17424+
;; CHECK-NEXT: (drop
1739317425
;; CHECK-NEXT: (i32.add
1739417426
;; CHECK-NEXT: (i32.shr_u
1739517427
;; CHECK-NEXT: (i32.load
@@ -17399,12 +17431,12 @@
1739917431
;; CHECK-NEXT: )
1740017432
;; CHECK-NEXT: (i32.const 1)
1740117433
;; CHECK-NEXT: )
17402-
;; CHECK-NEXT: (i32.const 0)
1740317434
;; CHECK-NEXT: )
17435+
;; CHECK-NEXT: (i32.const 1)
1740417436
;; CHECK-NEXT: )
1740517437
(func $skip-added-constants-zero-b (result i32)
17406-
;; Parallel case to the above, with a zero in the added constant. We do not
17407-
;; optimize.
17438+
;; Parallel case to the above, with a zero in the added constant, but we
17439+
;; do optimize the outer i32.ge_u away.
1740817440
(i32.ge_u
1740917441
(i32.add
1741017442
(i32.shr_u

0 commit comments

Comments
 (0)