summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp6
-rw-r--r--test/lit/passes/optimize-instructions.wast40
2 files changed, 45 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 5eba1e3b9..23a63646e 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -2805,7 +2805,11 @@ private:
// (Y)
// )
// )
- if (curr->type != Type::unreachable) {
+ //
+ // Ignore unreachable code here; leave that for DCE.
+ if (curr->type != Type::unreachable &&
+ curr->ifTrue->type != Type::unreachable &&
+ curr->ifFalse->type != Type::unreachable) {
Unary* un;
Expression* x;
Const* c;
diff --git a/test/lit/passes/optimize-instructions.wast b/test/lit/passes/optimize-instructions.wast
index 3ed0b3115..fd2637373 100644
--- a/test/lit/passes/optimize-instructions.wast
+++ b/test/lit/passes/optimize-instructions.wast
@@ -11814,6 +11814,46 @@
)
)
)
+ ;; CHECK: (func $ternary-no-unreachable-1 (param $x i32) (result i32)
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.eqz
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $ternary-no-unreachable-1 (param $x i32) (result i32)
+ (if (result i32)
+ (local.get $x)
+ ;; one arm is an eqz, the other is 0 or 1, so we can put an eqz on the
+ ;; outside in theory, but we'd need to be careful with the unreachable
+ ;; type here. ignore this case, as DCE is the proper optimization anyhow.
+ (i32.eqz
+ (unreachable)
+ )
+ (i32.const 0)
+ )
+ )
+ ;; CHECK: (func $ternary-no-unreachable-2 (param $x i32) (result i32)
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: (i32.eqz
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $ternary-no-unreachable-2 (param $x i32) (result i32)
+ (if (result i32)
+ (local.get $x)
+ ;; as before, but flipped
+ (i32.const 0)
+ (i32.eqz
+ (unreachable)
+ )
+ )
+ )
;; CHECK: (func $ternary-identical-arms (param $x i32) (param $y i32) (param $z i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.eqz