From d83ff5a8bd5959293e3bc91015bceb52706ca0ed Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 27 Nov 2024 10:01:29 +0900 Subject: [PATCH] Add optimization of a-0 and a+(-0) This can sometimes be helpful after all. --- CodeGen/src/OptimizeConstProp.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/CodeGen/src/OptimizeConstProp.cpp b/CodeGen/src/OptimizeConstProp.cpp index 84f8e4e9..f93354a3 100644 --- a/CodeGen/src/OptimizeConstProp.cpp +++ b/CodeGen/src/OptimizeConstProp.cpp @@ -1194,9 +1194,26 @@ static void constPropInInst(ConstPropState& state, IrBuilder& build, IrFunction& break; case IrCmd::ADD_INT: case IrCmd::SUB_INT: + state.substituteOrRecord(inst, index); + break; case IrCmd::ADD_NUM: case IrCmd::SUB_NUM: - state.substituteOrRecord(inst, index); + if (FFlag::LuauCodeGenArithOpt) + { + if (std::optional k = function.asDoubleOp(inst.b.kind == IrOpKind::Constant ? inst.b : state.tryGetValue(inst.b))) + { + // a + 0.0 and a - (-0.0) can't be folded since the behavior is different for negative zero + // however, a - 0.0 and a + (-0.0) can be folded into a + if (*k == 0.0 && bool(signbit(*k)) == (inst.cmd == IrCmd::ADD_NUM)) + substitute(function, inst, inst.a); + else + state.substituteOrRecord(inst, index); + } + else + state.substituteOrRecord(inst, index); + } + else + state.substituteOrRecord(inst, index); break; case IrCmd::MUL_NUM: if (FFlag::LuauCodeGenArithOpt)