Skip to content

Commit

Permalink
cleanup empty ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
tlively committed Nov 26, 2024
1 parent 0d9d994 commit c243013
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 63 deletions.
18 changes: 15 additions & 3 deletions src/passes/CodeFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ struct CodeFolding
Tail(Block* block) : expr(nullptr), block(block), pointer(nullptr) {}
// For a break
Tail(Expression* expr, Block* block)
: expr(expr), block(block), pointer(nullptr) {
}
: expr(expr), block(block), pointer(nullptr) {}
Tail(Expression* expr, Expression** pointer)
: expr(expr), block(nullptr), pointer(pointer) {}

Expand Down Expand Up @@ -486,7 +485,20 @@ struct CodeFolding
// make a block with curr + the merged code
Builder builder(*getModule());
auto* block = builder.makeBlock();
block->list.push_back(curr);
if constexpr (T::SpecificId == Expression::IfId) {
// If we've moved all the contents out of both arms of the If, then we can
// simplify the output by replacing it entirely with just a drop of the
// condition.
auto* iff = curr->template cast<If>();
if (iff->ifTrue->template cast<Block>()->list.empty() &&
iff->ifFalse->template cast<Block>()->list.empty()) {
block->list.push_back(builder.makeDrop(iff->condition));
} else {
block->list.push_back(curr);
}
} else {
block->list.push_back(curr);
}
while (!mergeable.empty()) {
block->list.push_back(mergeable.back());
mergeable.pop_back();
Expand Down
6 changes: 1 addition & 5 deletions test/lit/passes/code-folding-eh-legacy.wast
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,8 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (block
;; CHECK-NEXT: (if
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (then
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (drop
Expand Down
18 changes: 3 additions & 15 deletions test/lit/passes/code-folding.wast
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@
)

;; CHECK: (func $negative-zero-b (type $1) (result f32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (then
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (f32.const -0)
;; CHECK-NEXT: )
Expand Down Expand Up @@ -747,12 +743,8 @@
)

;; CHECK: (func $unreachable-if (type $0)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (then
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (drop
Expand Down Expand Up @@ -907,12 +899,8 @@
;; CHECK: (func $refined-type-blocks (type $3) (result anyref)
;; CHECK-NEXT: (select (result anyref)
;; CHECK-NEXT: (block (result anyref)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (then
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (ref.null none)
Expand Down
48 changes: 8 additions & 40 deletions test/passes/remove-unused-names_code-folding.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@
)
(func $ifs-blocks
(block
(if
(drop
(i32.const 0)
(then
)
(else
)
)
(nop)
)
Expand Down Expand Up @@ -119,12 +115,8 @@
)
(func $ifs-blocks-big
(block
(if
(drop
(i32.const 0)
(then
)
(else
)
)
(drop
(i32.add
Expand Down Expand Up @@ -512,12 +504,8 @@
(block $out
(block $out2
(block
(if
(drop
(local.get $x)
(then
)
(else
)
)
(br_if $out
(local.get $y)
Expand Down Expand Up @@ -707,12 +695,8 @@
(func $mixture
(block $out
(block
(if
(drop
(i32.const 1)
(then
)
(else
)
)
(drop
(i32.const 2)
Expand Down Expand Up @@ -759,22 +743,14 @@
(block
(block $out3
(block
(if
(drop
(i32.const 1)
(then
)
(else
)
)
(br $out3)
)
(block
(if
(drop
(i32.const 1)
(then
)
(else
)
)
(br $out3)
)
Expand Down Expand Up @@ -1519,12 +1495,8 @@
)
)
(block
(if
(drop
(i32.const 0)
(then
)
(else
)
)
(nop)
)
Expand All @@ -1540,12 +1512,8 @@
(nop)
(drop
(block (result i32)
(if
(drop
(unreachable)
(then
)
(else
)
)
(i32.add
(i32.const 1)
Expand Down

0 comments on commit c243013

Please sign in to comment.