Skip to content

Commit 26ef829

Browse files
authored
remove unnecessary constraint on remove-unused-br optimization of if-br-* into br_if,* - we can handle a concretely typed if as well, which can happen at the end of a block (#1799)
1 parent b650f20 commit 26ef829

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

src/passes/RemoveUnusedBrs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
633633
auto& list = curr->list;
634634
for (Index i = 0; i < list.size(); i++) {
635635
auto* iff = list[i]->dynCast<If>();
636-
if (!iff || !iff->ifFalse || isConcreteType(iff->type)) continue; // if it lacked an if-false, it would already be a br_if, as that's the easy case
636+
if (!iff || !iff->ifFalse) continue; // if it lacked an if-false, it would already be a br_if, as that's the easy case
637637
auto* ifTrueBreak = iff->ifTrue->dynCast<Break>();
638638
if (ifTrueBreak && !ifTrueBreak->condition && canTurnIfIntoBrIf(iff->condition, ifTrueBreak->value, passOptions)) {
639639
// we are an if-else where the ifTrue is a break without a condition, so we can do this

test/lld/hello_world.wast.mem

581 Bytes
Binary file not shown.

test/passes/remove-unused-brs.txt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,16 @@
938938
(loop $typed (result i32)
939939
(block $outer (result i32)
940940
(block (result i32)
941-
(if (result i32)
942-
(i32.const 2)
943-
(block $block (result i32)
944-
(drop
945-
(call $loop-if)
946-
)
947-
(i32.const 0)
941+
(br_if $typed
942+
(i32.eqz
943+
(i32.const 2)
948944
)
949-
(br $typed)
945+
)
946+
(block $block (result i32)
947+
(drop
948+
(call $loop-if)
949+
)
950+
(i32.const 0)
950951
)
951952
)
952953
)
@@ -2382,4 +2383,25 @@
23822383
(br $top)
23832384
)
23842385
)
2386+
(func $loop-end-set (; 111 ;) (type $10) (param $x i32) (result i32)
2387+
(loop $loop
2388+
(nop)
2389+
(br_if $loop
2390+
(get_local $x)
2391+
)
2392+
(set_local $x
2393+
(i32.const 1)
2394+
)
2395+
)
2396+
(get_local $x)
2397+
)
2398+
(func $loop-end-value (; 112 ;) (type $10) (param $x i32) (result i32)
2399+
(loop $loop (result i32)
2400+
(nop)
2401+
(br_if $loop
2402+
(get_local $x)
2403+
)
2404+
(i32.const 1)
2405+
)
2406+
)
23852407
)

test/passes/remove-unused-brs.wast

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,5 +1990,26 @@
19901990
(br $top)
19911991
)
19921992
)
1993+
(func $loop-end-set (param $x i32) (result i32)
1994+
(loop $loop
1995+
(nop)
1996+
(if
1997+
(get_local $x)
1998+
(br $loop)
1999+
(set_local $x (i32.const 1))
2000+
)
2001+
)
2002+
(get_local $x)
2003+
)
2004+
(func $loop-end-value (param $x i32) (result i32)
2005+
(loop $loop (result i32)
2006+
(nop)
2007+
(if (result i32)
2008+
(get_local $x)
2009+
(br $loop)
2010+
(i32.const 1)
2011+
)
2012+
)
2013+
)
19932014
)
19942015

0 commit comments

Comments
 (0)