diff --git a/src/julec/opt/expr.jule b/src/julec/opt/expr.jule index 4a3fc34e1..d597dd0e8 100644 --- a/src/julec/opt/expr.jule +++ b/src/julec/opt/expr.jule @@ -371,6 +371,16 @@ impl exprOptimizer { m.Op.Kind = token::Kind.Shl m.Right.Model = constant::Const.NewU64(1) ret + | token::Id.Minus: + // If type is integer and expressions is like "x-x", then we can simplfy to "0" which is cheaper. + // Floating-point types is exceptional because we cannot predict the result because of NaN or similar values. + if !types::IsInt(lp.Kind) || !equalModels(m.Left.Model, m.Right.Model) { + break + } + mut c := constant::Const.NewU64(0) + c.Kind = lp.Kind + *self.model = c + ret } // Check whether the right operand is constant for safety. diff --git a/src/julec/opt/scope.jule b/src/julec/opt/scope.jule index b19561557..51aa9d198 100644 --- a/src/julec/opt/scope.jule +++ b/src/julec/opt/scope.jule @@ -453,6 +453,9 @@ impl scopeOptimizer { | token::Id.PlusEq: assign.Op.Id = token::Id.Plus assign.Op.Kind = token::Kind.Plus + | token::Id.MinusEq: + assign.Op.Id = token::Id.Minus + assign.Op.Kind = token::Kind.Minus | token::Id.SolidusEq: assign.Op.Id = token::Id.Solidus assign.Op.Kind = token::Kind.Solidus