Skip to content

Commit 14cb0c0

Browse files
authored
optimize out 0-x, a zero only used to negate an int, when possible (#1365)
1 parent 692069c commit 14cb0c0

12 files changed

+134
-48
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,43 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
506506
}
507507
}
508508
// note that both left and right may be consts, but then we let precompute compute the constant result
509-
} else if (binary->op == AddInt32 || binary->op == SubInt32) {
509+
} else if (binary->op == AddInt32) {
510+
// try to get rid of (0 - ..), that is, a zero only used to negate an
511+
// int. an add of a subtract can be flipped in order to remove it:
512+
// (i32.add
513+
// (i32.sub
514+
// (i32.const 0)
515+
// X
516+
// )
517+
// Y
518+
// )
519+
// =>
520+
// (i32.sub
521+
// Y
522+
// X
523+
// )
524+
if (auto* sub = binary->left->dynCast<Binary>()) {
525+
if (sub->op == SubInt32) {
526+
if (auto* subZero = sub->left->dynCast<Const>()) {
527+
if (subZero->value.geti32() == 0) {
528+
sub->left = binary->right;
529+
return sub;
530+
}
531+
}
532+
}
533+
}
534+
if (auto* sub = binary->right->dynCast<Binary>()) {
535+
if (sub->op == SubInt32) {
536+
if (auto* subZero = sub->left->dynCast<Const>()) {
537+
if (subZero->value.geti32() == 0) {
538+
sub->left = binary->left;
539+
return sub;
540+
}
541+
}
542+
}
543+
}
544+
return optimizeAddedConstants(binary);
545+
} else if (binary->op == SubInt32) {
510546
return optimizeAddedConstants(binary);
511547
}
512548
// a bunch of operations on a constant right side can be simplified

test/emcc_O2_hello_world.fromasm

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5966,12 +5966,9 @@
59665966
(if
59675967
(i32.lt_u
59685968
(tee_local $1
5969-
(i32.add
5969+
(i32.sub
59705970
(get_local $1)
5971-
(i32.sub
5972-
(i32.const 0)
5973-
(get_local $11)
5974-
)
5971+
(get_local $11)
59755972
)
59765973
)
59775974
(get_local $14)

test/emcc_O2_hello_world.fromasm.clamp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5966,12 +5966,9 @@
59665966
(if
59675967
(i32.lt_u
59685968
(tee_local $1
5969-
(i32.add
5969+
(i32.sub
59705970
(get_local $1)
5971-
(i32.sub
5972-
(i32.const 0)
5973-
(get_local $11)
5974-
)
5971+
(get_local $11)
59755972
)
59765973
)
59775974
(get_local $14)

test/emcc_O2_hello_world.fromasm.imprecise

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5965,12 +5965,9 @@
59655965
(if
59665966
(i32.lt_u
59675967
(tee_local $1
5968-
(i32.add
5968+
(i32.sub
59695969
(get_local $1)
5970-
(i32.sub
5971-
(i32.const 0)
5972-
(get_local $11)
5973-
)
5970+
(get_local $11)
59745971
)
59755972
)
59765973
(get_local $14)

test/emcc_hello_world.fromasm

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13329,12 +13329,9 @@
1332913329
(if
1333013330
(i32.lt_u
1333113331
(tee_local $1
13332-
(i32.add
13332+
(i32.sub
1333313333
(get_local $1)
13334-
(i32.sub
13335-
(i32.const 0)
13336-
(get_local $7)
13337-
)
13334+
(get_local $7)
1333813335
)
1333913336
)
1334013337
(get_local $11)

test/emcc_hello_world.fromasm.clamp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13379,12 +13379,9 @@
1337913379
(if
1338013380
(i32.lt_u
1338113381
(tee_local $1
13382-
(i32.add
13382+
(i32.sub
1338313383
(get_local $1)
13384-
(i32.sub
13385-
(i32.const 0)
13386-
(get_local $7)
13387-
)
13384+
(get_local $7)
1338813385
)
1338913386
)
1339013387
(get_local $11)

test/emcc_hello_world.fromasm.imprecise

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13263,14 +13263,11 @@
1326313263
(if
1326413264
(i32.lt_u
1326513265
(tee_local $1
13266-
(i32.add
13266+
(i32.sub
1326713267
(get_local $1)
13268-
(i32.sub
13269-
(i32.const 0)
13270-
(tee_local $8
13271-
(i32.load
13272-
(get_local $1)
13273-
)
13268+
(tee_local $8
13269+
(i32.load
13270+
(get_local $1)
1327413271
)
1327513272
)
1327613273
)

test/memorygrowth.fromasm

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,12 +6017,9 @@
60176017
(if
60186018
(i32.lt_u
60196019
(tee_local $1
6020-
(i32.add
6020+
(i32.sub
60216021
(get_local $1)
6022-
(i32.sub
6023-
(i32.const 0)
6024-
(get_local $10)
6025-
)
6022+
(get_local $10)
60266023
)
60276024
)
60286025
(get_local $14)

test/memorygrowth.fromasm.clamp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,12 +6017,9 @@
60176017
(if
60186018
(i32.lt_u
60196019
(tee_local $1
6020-
(i32.add
6020+
(i32.sub
60216021
(get_local $1)
6022-
(i32.sub
6023-
(i32.const 0)
6024-
(get_local $10)
6025-
)
6022+
(get_local $10)
60266023
)
60276024
)
60286025
(get_local $14)

test/memorygrowth.fromasm.imprecise

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6015,12 +6015,9 @@
60156015
(if
60166016
(i32.lt_u
60176017
(tee_local $1
6018-
(i32.add
6018+
(i32.sub
60196019
(get_local $1)
6020-
(i32.sub
6021-
(i32.const 0)
6022-
(get_local $10)
6023-
)
6020+
(get_local $10)
60246021
)
60256022
)
60266023
(get_local $14)

0 commit comments

Comments
 (0)