Skip to content

Commit 52668c3

Browse files
committed
Opt: Rewrite 1 + ~x to -x.
1 parent 303de0d commit 52668c3

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

linker/shared/src/main/scala/org/scalajs/linker/frontend/optimizer/OptimizerCore.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4141,6 +4141,10 @@ private[optimizer] abstract class OptimizerCore(
41414141
PreTransLit(IntLiteral(y)), z)) =>
41424142
foldBinaryOp(innerOp, PreTransLit(IntLiteral(x + y)), z)
41434143

4144+
// 1 + (-1 ^ x) == 1 + ~x == -x == 0 - x (this appears when optimizing a Range with step == -1)
4145+
case (PreTransLit(IntLiteral(1)), PreTransBinaryOp(Int_^, PreTransLit(IntLiteral(-1)), x)) =>
4146+
foldBinaryOp(Int_-, PreTransLit(IntLiteral(0)), x)
4147+
41444148
case _ => default
41454149
}
41464150

0 commit comments

Comments
 (0)