Skip to content

Commit 39b9e38

Browse files
authored
Optimize select fallthrough values (#2220)
This became noticeable after #2216 which led to some eqz eqz pairs in the test suite.
1 parent 6d47b7b commit 39b9e38

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ struct OptimizeInstructions
939939

940940
// Optimize given that the expression is flowing into a boolean context
941941
Expression* optimizeBoolean(Expression* boolean) {
942+
// TODO use a general getFallthroughs
942943
if (auto* unary = boolean->dynCast<Unary>()) {
943944
if (unary && unary->op == EqZInt32) {
944945
auto* unary2 = unary->value->dynCast<Unary>();
@@ -975,6 +976,9 @@ struct OptimizeInstructions
975976
iff->ifTrue = optimizeBoolean(iff->ifTrue);
976977
iff->ifFalse = optimizeBoolean(iff->ifFalse);
977978
}
979+
} else if (auto* select = boolean->dynCast<Select>()) {
980+
select->ifTrue = optimizeBoolean(select->ifTrue);
981+
select->ifFalse = optimizeBoolean(select->ifFalse);
978982
}
979983
// TODO: recurse into br values?
980984
return boolean;

test/emcc_hello_world.fromasm

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10329,13 +10329,9 @@
1032910329
(block
1033010330
(br_if $label$break$L279
1033110331
(select
10332-
(i32.eqz
10333-
(i32.eqz
10334-
(local.tee $3
10335-
(i32.load
10336-
(i32.const 616)
10337-
)
10338-
)
10332+
(local.tee $3
10333+
(i32.load
10334+
(i32.const 616)
1033910335
)
1034010336
)
1034110337
(i32.const 0)

test/emcc_hello_world.fromasm.clamp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10379,13 +10379,9 @@
1037910379
(block
1038010380
(br_if $label$break$L279
1038110381
(select
10382-
(i32.eqz
10383-
(i32.eqz
10384-
(local.tee $3
10385-
(i32.load
10386-
(i32.const 616)
10387-
)
10388-
)
10382+
(local.tee $3
10383+
(i32.load
10384+
(i32.const 616)
1038910385
)
1039010386
)
1039110387
(i32.const 0)

test/emcc_hello_world.fromasm.imprecise

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10225,13 +10225,9 @@
1022510225
(block
1022610226
(br_if $label$break$L279
1022710227
(select
10228-
(i32.eqz
10229-
(i32.eqz
10230-
(local.tee $3
10231-
(i32.load
10232-
(i32.const 616)
10233-
)
10234-
)
10228+
(local.tee $3
10229+
(i32.load
10230+
(i32.const 616)
1023510231
)
1023610232
)
1023710233
(i32.const 0)

test/passes/optimize-instructions_enable-threads.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,6 +3293,16 @@
32933293
)
32943294
)
32953295
)
3296+
(func $select-into-arms (; 77 ;) (type $FUNCSIG$vii) (param $x i32) (param $y i32)
3297+
(if
3298+
(select
3299+
(local.get $x)
3300+
(local.get $y)
3301+
(local.get $y)
3302+
)
3303+
(unreachable)
3304+
)
3305+
)
32963306
)
32973307
(module
32983308
(type $FUNCSIG$v (func))

test/passes/optimize-instructions_enable-threads.wast

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,6 +3738,16 @@
37383738
))
37393739
;; TODO: more stuff here
37403740
)
3741+
(func $select-into-arms (param $x i32) (param $y i32)
3742+
(if
3743+
(select
3744+
(i32.eqz (i32.eqz (local.get $x)))
3745+
(i32.eqz (i32.eqz (local.get $y)))
3746+
(local.get $y)
3747+
)
3748+
(unreachable)
3749+
)
3750+
)
37413751
)
37423752
(module
37433753
(import "env" "memory" (memory $0 (shared 256 256)))

0 commit comments

Comments
 (0)