From e07c81a5e716742ba7357ef4ab3ca09cb9d85a0d Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Tue, 2 Apr 2024 13:56:33 -0400 Subject: [PATCH] ConstProp: also negate sub -> add Signed-off-by: Alyssa Rosenzweig --- .../Source/Interface/IR/Passes/ConstProp.cpp | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/FEXCore/Source/Interface/IR/Passes/ConstProp.cpp b/FEXCore/Source/Interface/IR/Passes/ConstProp.cpp index 017497df8d..d647faf0ae 100644 --- a/FEXCore/Source/Interface/IR/Passes/ConstProp.cpp +++ b/FEXCore/Source/Interface/IR/Passes/ConstProp.cpp @@ -682,7 +682,9 @@ bool ConstProp::ConstantPropagation(IREmitter *IREmit, const IRListView& Current } case OP_ADD: - case OP_ADDWITHFLAGS: { + case OP_SUB: + case OP_ADDWITHFLAGS: + case OP_SUBWITHFLAGS: { auto Op = IROp->C(); uint64_t Constant1{}; uint64_t Constant2{}; @@ -693,12 +695,23 @@ bool ConstProp::ConstantPropagation(IREmitter *IREmit, const IRListView& Current uint64_t NewConstant = (Constant1 + Constant2) & getMask(Op) ; IREmit->ReplaceWithConstant(CodeNode, NewConstant); Changed = true; + } else if (IsConstant1 && IsConstant2 && IROp->Op == OP_SUB) { + uint64_t NewConstant = (Constant1 - Constant2) & getMask(Op) ; + IREmit->ReplaceWithConstant(CodeNode, NewConstant); + Changed = true; } else if (IsConstant2 && !IsImmAddSub(Constant2) && IsImmAddSub(-Constant2)) { // If the second argument is constant, the immediate is not ImmAddSub, but when negated is. - // This means we can convert the operation in to a subtract. - // Change the IR operation itself. - IROp->Op = IROp->Op == OP_ADD ? OP_SUB : OP_SUBWITHFLAGS; + // So, negate the operation to negate (and inline) the constant. + if (IROp->Op == OP_ADD) + IROp->Op = OP_SUB; + else if (IROp->Op == OP_SUB) + IROp->Op = OP_ADD; + else if (IROp->Op == OP_ADDWITHFLAGS) + IROp->Op = OP_SUBWITHFLAGS; + else if (IROp->Op == OP_SUBWITHFLAGS) + IROp->Op = OP_ADDWITHFLAGS; + // Set the write cursor to just before this operation. auto CodeIter = CurrentIR.at(CodeNode); --CodeIter; @@ -713,19 +726,6 @@ bool ConstProp::ConstantPropagation(IREmitter *IREmit, const IRListView& Current } break; } - case OP_SUB: { - auto Op = IROp->C(); - uint64_t Constant1{}; - uint64_t Constant2{}; - - if (IREmit->IsValueConstant(Op->Header.Args[0], &Constant1) && - IREmit->IsValueConstant(Op->Header.Args[1], &Constant2)) { - uint64_t NewConstant = (Constant1 - Constant2) & getMask(Op) ; - IREmit->ReplaceWithConstant(CodeNode, NewConstant); - Changed = true; - } - break; - } case OP_SUBSHIFT: { auto Op = IROp->C();