From 4ea9edb4090456467f98fe24876f36520c4571e9 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 9 Jun 2022 23:47:57 -0700 Subject: [PATCH] remove adjacent occurrence of sext/zext of the same width --- src/mapleall/mpl2mpl/src/constantfold.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/mapleall/mpl2mpl/src/constantfold.cpp b/src/mapleall/mpl2mpl/src/constantfold.cpp index 860ed2846a..f84c351bd8 100644 --- a/src/mapleall/mpl2mpl/src/constantfold.cpp +++ b/src/mapleall/mpl2mpl/src/constantfold.cpp @@ -1676,13 +1676,23 @@ std::pair ConstantFold::FoldExtractbits(ExtractbitsNode *node) ConstvalNode *cst = safe_cast(p.first); if (cst != nullptr && (opcode == OP_sext || opcode == OP_zext)) { result = FoldSignExtend(opcode, node->GetPrimType(), size, *cst); + return std::make_pair(result, 0); + } + BaseNode *e = PairToExpr(node->Opnd(0)->GetPrimType(), p); + if (e != node->Opnd(0)) { + result = mirModule->CurFuncCodeMemPool()->New(opcode, PrimType(node->GetPrimType()), + offset, size, e); } else { - BaseNode *e = PairToExpr(node->Opnd(0)->GetPrimType(), p); - if (e != node->Opnd(0)) { - result = mirModule->CurFuncCodeMemPool()->New(opcode, PrimType(node->GetPrimType()), - offset, size, e); - } else { - result = node; + result = node; + } + // check for consecutive and redundant extraction of same bits + BaseNode *opnd = result->Opnd(0); + Opcode opndOp = opnd->GetOpCode(); + if (opndOp == OP_extractbits || opndOp == OP_sext || opndOp == OP_zext) { + uint8 opndOffset = static_cast(opnd)->GetBitsOffset(); + uint8 opndSize = static_cast(opnd)->GetBitsSize(); + if (offset == opndOffset && size == opndSize) { + result->SetOpnd(opnd->Opnd(0), 0); // delete the redundant extraction } } return std::make_pair(result, 0); -- Gitee