summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/MergeBlocks.cpp5
-rw-r--r--test/lit/passes/inlining-optimizing_optimize-level=3.wast108
-rw-r--r--test/lit/passes/merge-blocks.wast38
-rw-r--r--test/passes/converge_O3_metrics.bin.txt120
-rw-r--r--test/passes/remove-unused-names_merge-blocks_all-features.txt10
5 files changed, 157 insertions, 124 deletions
diff --git a/src/passes/MergeBlocks.cpp b/src/passes/MergeBlocks.cpp
index b77dbef83..6ba486717 100644
--- a/src/passes/MergeBlocks.cpp
+++ b/src/passes/MergeBlocks.cpp
@@ -520,6 +520,11 @@ struct MergeBlocks
}
}
+ void visitIf(If* curr) {
+ // We can move code out of the condition, but not any of the other children.
+ optimize(curr, curr->condition);
+ }
+
void optimizeTernary(Expression* curr,
Expression*& first,
Expression*& second,
diff --git a/test/lit/passes/inlining-optimizing_optimize-level=3.wast b/test/lit/passes/inlining-optimizing_optimize-level=3.wast
index 4a4f21f94..1ae11b22a 100644
--- a/test/lit/passes/inlining-optimizing_optimize-level=3.wast
+++ b/test/lit/passes/inlining-optimizing_optimize-level=3.wast
@@ -2584,79 +2584,77 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $3
+ ;; CHECK-NEXT: (i32.load8_s
+ ;; CHECK-NEXT: (local.tee $4
+ ;; CHECK-NEXT: (i32.add
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.const 74)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.store8
+ ;; CHECK-NEXT: (local.get $4)
+ ;; CHECK-NEXT: (i32.or
+ ;; CHECK-NEXT: (i32.add
+ ;; CHECK-NEXT: (local.get $3)
+ ;; CHECK-NEXT: (i32.const 255)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (local.get $3)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (local.set $3
;; CHECK-NEXT: (if (result i32)
- ;; CHECK-NEXT: (block (result i32)
- ;; CHECK-NEXT: (local.set $3
- ;; CHECK-NEXT: (i32.load8_s
- ;; CHECK-NEXT: (local.tee $4
- ;; CHECK-NEXT: (i32.add
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (i32.const 74)
- ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (local.tee $3
+ ;; CHECK-NEXT: (i32.load
+ ;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const 8)
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.store8
- ;; CHECK-NEXT: (local.get $4)
- ;; CHECK-NEXT: (i32.or
- ;; CHECK-NEXT: (i32.add
+ ;; CHECK-NEXT: (block (result i32)
+ ;; CHECK-NEXT: (i32.store
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (local.get $3)
- ;; CHECK-NEXT: (i32.const 255)
+ ;; CHECK-NEXT: (i32.const 32)
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (if (result i32)
- ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (block (result i32)
+ ;; CHECK-NEXT: (i32.store offset=8
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.store offset=4
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.store offset=28
+ ;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (local.tee $3
- ;; CHECK-NEXT: (i32.load
+ ;; CHECK-NEXT: (i32.load offset=44
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.const 8)
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (block (result i32)
- ;; CHECK-NEXT: (i32.store
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (i32.or
- ;; CHECK-NEXT: (local.get $3)
- ;; CHECK-NEXT: (i32.const 32)
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.const -1)
+ ;; CHECK-NEXT: (i32.store offset=20
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (block (result i32)
- ;; CHECK-NEXT: (i32.store offset=8
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (i32.const 0)
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.store offset=4
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (i32.const 0)
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.store offset=28
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (local.tee $3
- ;; CHECK-NEXT: (i32.load offset=44
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.store offset=20
- ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.store offset=16
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $3)
- ;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.store offset=16
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: (i32.add
- ;; CHECK-NEXT: (local.get $3)
- ;; CHECK-NEXT: (i32.load offset=48
- ;; CHECK-NEXT: (local.get $2)
- ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.load offset=48
+ ;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
- ;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
diff --git a/test/lit/passes/merge-blocks.wast b/test/lit/passes/merge-blocks.wast
index e6136d4da..3d931690b 100644
--- a/test/lit/passes/merge-blocks.wast
+++ b/test/lit/passes/merge-blocks.wast
@@ -188,4 +188,42 @@
)
)
)
+
+ ;; CHECK: (func $if-condition (result i32)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: (block (result i32)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 2)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const 3)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (block (result i32)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 4)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.const 5)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $if-condition (result i32)
+ ;; Content can be moved out of an if condition, but not anywhere else.
+ (if (result i32)
+ (block (result i32)
+ (drop (i32.const 0))
+ (i32.const 1)
+ )
+ (block (result i32)
+ (drop (i32.const 2))
+ (i32.const 3)
+ )
+ (block (result i32)
+ (drop (i32.const 4))
+ (i32.const 5)
+ )
+ )
+ )
)
diff --git a/test/passes/converge_O3_metrics.bin.txt b/test/passes/converge_O3_metrics.bin.txt
index 5c41b3146..bf24bb5e2 100644
--- a/test/passes/converge_O3_metrics.bin.txt
+++ b/test/passes/converge_O3_metrics.bin.txt
@@ -7,10 +7,10 @@ total
[table-data] : 429
[tables] : 0
[tags] : 0
- [total] : 553
+ [total] : 552
[vars] : 2
Binary : 12
- Block : 7
+ Block : 6
Break : 3
Call : 1
CallIndirect : 4
@@ -69,28 +69,26 @@ total
)
)
)
- (if
- (block (result i32)
- (local.set $0
- (i32.const 10888)
- )
- (loop $label$3
- (br_if $label$3
- (i32.load8_s
- (local.tee $0
- (i32.add
- (local.get $0)
- (i32.const 1)
- )
- )
+ (local.set $0
+ (i32.const 10888)
+ )
+ (loop $label$3
+ (br_if $label$3
+ (i32.load8_s
+ (local.tee $0
+ (i32.add
+ (local.get $0)
+ (i32.const 1)
)
)
)
- (local.tee $0
- (i32.sub
- (local.get $0)
- (i32.const 10888)
- )
+ )
+ )
+ (if
+ (local.tee $0
+ (i32.sub
+ (local.get $0)
+ (i32.const 10888)
)
)
(block $label$2
@@ -237,10 +235,10 @@ total
[table-data] : 429
[tables] : 0
[tags] : 0
- [total] : 549 -4
+ [total] : 548 -4
[vars] : 2
Binary : 12
- Block : 7
+ Block : 6
Break : 3
Call : 1
CallIndirect : 4
@@ -297,28 +295,26 @@ total
)
)
)
- (if
- (block (result i32)
- (local.set $0
- (i32.const 10888)
- )
- (loop $label$3
- (br_if $label$3
- (i32.load8_s
- (local.tee $0
- (i32.add
- (local.get $0)
- (i32.const 1)
- )
- )
+ (local.set $0
+ (i32.const 10888)
+ )
+ (loop $label$3
+ (br_if $label$3
+ (i32.load8_s
+ (local.tee $0
+ (i32.add
+ (local.get $0)
+ (i32.const 1)
)
)
)
- (local.tee $0
- (i32.sub
- (local.get $0)
- (i32.const 10888)
- )
+ )
+ )
+ (if
+ (local.tee $0
+ (i32.sub
+ (local.get $0)
+ (i32.const 10888)
)
)
(block $label$2
@@ -460,10 +456,10 @@ total
[table-data] : 429
[tables] : 0
[tags] : 0
- [total] : 549
+ [total] : 548
[vars] : 2
Binary : 12
- Block : 7
+ Block : 6
Break : 3
Call : 1
CallIndirect : 4
@@ -520,28 +516,26 @@ total
)
)
)
- (if
- (block (result i32)
- (local.set $0
- (i32.const 10888)
- )
- (loop $label$3
- (br_if $label$3
- (i32.load8_s
- (local.tee $0
- (i32.add
- (local.get $0)
- (i32.const 1)
- )
- )
+ (local.set $0
+ (i32.const 10888)
+ )
+ (loop $label$3
+ (br_if $label$3
+ (i32.load8_s
+ (local.tee $0
+ (i32.add
+ (local.get $0)
+ (i32.const 1)
)
)
)
- (local.tee $0
- (i32.sub
- (local.get $0)
- (i32.const 10888)
- )
+ )
+ )
+ (if
+ (local.tee $0
+ (i32.sub
+ (local.get $0)
+ (i32.const 10888)
)
)
(block $label$2
diff --git a/test/passes/remove-unused-names_merge-blocks_all-features.txt b/test/passes/remove-unused-names_merge-blocks_all-features.txt
index 9f15d1319..ab1837ef7 100644
--- a/test/passes/remove-unused-names_merge-blocks_all-features.txt
+++ b/test/passes/remove-unused-names_merge-blocks_all-features.txt
@@ -775,13 +775,11 @@
(func $block-type-change
(local $0 f64)
(local $1 f64)
+ (nop)
(if
- (block (result i32)
- (nop)
- (f64.gt
- (local.get $0)
- (local.get $1)
- )
+ (f64.gt
+ (local.get $0)
+ (local.get $1)
)
(nop)
)