diff --git a/src/mapleall/mpl2mpl/src/constantfold.cpp b/src/mapleall/mpl2mpl/src/constantfold.cpp index 860ed2846a2f8c8c181406f265ba399cff95825a..f84c351bd85b33eba986519e469bb88a1a1ffdc2 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);