diff --git a/src/mapleall/bin/dex2mpl b/src/mapleall/bin/dex2mpl index 729738c96818833dc28736dc5c62901bb6e8418b..7dd25a7871d5cb5f04eb79f36de5d11eacbfeb5e 100755 Binary files a/src/mapleall/bin/dex2mpl and b/src/mapleall/bin/dex2mpl differ diff --git a/src/mapleall/bin/jbc2mpl b/src/mapleall/bin/jbc2mpl index 7a2bf855da407b622c3ff660e832dbbd5ee3a1a0..5aff0f5a0fc922f25d6118cdce5bc4121cefa954 100755 Binary files a/src/mapleall/bin/jbc2mpl and b/src/mapleall/bin/jbc2mpl differ diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index ef9657d39d9ece44ebf83bfb4d0ed23605cf2f3d..6d23690df65f639ac799e7d10df3bcf9c556182b 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -3717,7 +3717,7 @@ Operand *AArch64CGFunc::SelectBnot(UnaryNode &node, Operand &opnd0) { Operand *AArch64CGFunc::SelectExtractbits(ExtractbitsNode &node, Operand &srcOpnd) { PrimType dtype = node.GetPrimType(); RegOperand &resOpnd = CreateRegisterOperandOfType(dtype); - bool isSigned = IsSignedInteger(dtype); + bool isSigned = (node.GetOpCode() == OP_sext) ? true : (node.GetOpCode() == OP_zext) ? false : IsSignedInteger(dtype); uint8 bitOffset = node.GetBitsOffset(); uint8 bitSize = node.GetBitsSize(); bool is64Bits = (GetPrimTypeBitSize(dtype) == k64BitSize); diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp index 36863c1387732890b5af6ec645e45b5794956043..2e84974ebefd77dfa1f5eca955a219423fdfa0fa 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_global.cpp @@ -226,6 +226,9 @@ bool ForwardPropPattern::CheckCondition(Insn &insn) { } Operand &firstOpnd = insn.GetOperand(kInsnFirstOpnd); Operand &secondOpnd = insn.GetOperand(kInsnSecondOpnd); + if (firstOpnd.GetSize() != secondOpnd.GetSize()) { + return false; + } RegOperand &firstRegOpnd = static_cast(firstOpnd); RegOperand &secondRegOpnd = static_cast(secondOpnd); uint32 firstRegNO = firstRegOpnd.GetRegisterNumber(); @@ -346,7 +349,9 @@ bool BackPropPattern::CheckAndGetOpnd(Insn &insn) { if (RegOperand::IsSameReg(firstOpnd, secondOpnd)) { return false; } - + if (firstOpnd.GetSize() != secondOpnd.GetSize()) { + return false; + } firstRegOpnd = &static_cast(firstOpnd); secondRegOpnd = &static_cast(secondOpnd); if (firstRegOpnd->IsZeroRegister() || !firstRegOpnd->IsVirtualRegister() || !secondRegOpnd->IsVirtualRegister()) { diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp index b8bf42b0bb5481df0090d4a4268f9a2be7bdadad..bd76d7c118e7957a3e4ad44f70d388c4ad5fda2e 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp @@ -233,7 +233,6 @@ void AArch64MemOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { * The ARMv8-A architecture allows many types of load and store accesses to be arbitrarily aligned. * The Cortex- A57 processor handles most unaligned accesses without performance penalties. */ - //ASSERT(!IsOffsetMisaligned(md->GetOperandSize()), "should not be OffsetMisaligned"); #if DEBUG if (IsOffsetMisaligned(md->GetOperandSize())) { INFO(kLncInfo, "The Memory operand's offset is misaligned:", ""); diff --git a/src/mapleall/maple_driver/src/driver_runner.cpp b/src/mapleall/maple_driver/src/driver_runner.cpp index 4373c730ae169437fbac2f1f78dce497f7b6d507..2de1a56f37b079d7011799fb66d3d8cc32e2c36d 100644 --- a/src/mapleall/maple_driver/src/driver_runner.cpp +++ b/src/mapleall/maple_driver/src/driver_runner.cpp @@ -332,7 +332,7 @@ void DriverRunner::ProcessCGPhase(const std::string &outputFile, const std::stri if (!cgOptions->SuppressFileInfo()) { cg->GetEmitter()->EmitFileInfo(actualInput); } - if (withDwarf) { + if (cgOptions->WithDwarf()) { cg->GetEmitter()->EmitDIHeader(); } // Run the cg optimizations phases diff --git a/src/mapleall/maple_ir/src/mir_builder.cpp b/src/mapleall/maple_ir/src/mir_builder.cpp index 6fae92961f4e26a60983af63e1f2b38fe697f792..700795ccebb4b95844d174575e5341ef21788fbb 100644 --- a/src/mapleall/maple_ir/src/mir_builder.cpp +++ b/src/mapleall/maple_ir/src/mir_builder.cpp @@ -618,11 +618,11 @@ AddrofNode *MIRBuilder::CreateExprDread(PregIdx pregID, PrimType pty) { IreadNode *MIRBuilder::CreateExprIread(const MIRType &returnType, const MIRType &ptrType, FieldID fieldID, BaseNode *addr) { TyIdx returnTypeIdx = returnType.GetTypeIndex(); - CHECK(returnTypeIdx < GlobalTables::GetTypeTable().GetTypeTable().size(), + ASSERT(returnTypeIdx < GlobalTables::GetTypeTable().GetTypeTable().size(), "index out of range in MIRBuilder::CreateExprIread"); ASSERT(fieldID != 0 || ptrType.GetPrimType() != PTY_agg, "Error: Fieldid should not be 0 when trying to iread a field from type "); - PrimType type = GetRegPrimType(returnType.GetPrimType()); + PrimType type = GlobalTables::GetTypeTable().GetPrimTypeFromTyIdx(returnTypeIdx); return GetCurrentFuncCodeMp()->New(OP_iread, type, ptrType.GetTypeIndex(), fieldID, addr); } diff --git a/src/mapleall/maple_me/include/alias_class.h b/src/mapleall/maple_me/include/alias_class.h index 5772272a3ae4a611ca2aa2cfa4e9d1f912734d12..2fd7d8a0f0c5db18a520ec9cfead24b314eb3c92 100644 --- a/src/mapleall/maple_me/include/alias_class.h +++ b/src/mapleall/maple_me/include/alias_class.h @@ -168,6 +168,7 @@ class AliasClass : public AnalysisResult { void GenericInsertMayDefUse(StmtNode &stmt, BBId bbID); static bool MayAliasBasicAA(const OriginalSt *ostA, const OriginalSt *ostB); + bool MayAlias(const OriginalSt *ostA, const OriginalSt *ostB) const; protected: virtual bool InConstructorLikeFunc() const { diff --git a/src/mapleall/maple_me/include/dse.h b/src/mapleall/maple_me/include/dse.h index 118345945db3c67cfee3bbb926c63ae74467e089..0a5376f6d71df6e480e6e94d3e25905527792f71 100644 --- a/src/mapleall/maple_me/include/dse.h +++ b/src/mapleall/maple_me/include/dse.h @@ -22,16 +22,19 @@ #include "safe_ptr.h" #include "ssa_tab.h" #include "mir_module.h" +#include "alias_class.h" namespace maple { class DSE { public: - DSE(std::vector &&bbVec, BB &commonEntryBB, BB &commonExitBB, SSATab &ssaTab, - Dominance &postDom, bool enableDebug = false, bool decouple = false, bool islfo = false) + DSE(std::vector &&bbVec, BB &commonEntryBB, BB &commonExitBB, SSATab &ssaTab, Dominance &postDom, + const AliasClass *aliasClass, bool enableDebug = false, bool decouple = false, bool islfo = false) : enableDebug(enableDebug), bbVec(bbVec), commonEntryBB(commonEntryBB), commonExitBB(commonExitBB), ssaTab(ssaTab), - postDom(postDom), bbRequired(bbVec.size(), false), + postDom(postDom), + aliasInfo(aliasClass), + bbRequired(bbVec.size(), false), exprRequired(ssaTab.GetVersionStTableSize(), false), decoupleStatic(decouple), isLfo(islfo) {} @@ -120,6 +123,7 @@ class DSE { BB &commonExitBB; SSATab &ssaTab; Dominance &postDom; + const AliasClass *aliasInfo; std::vector bbRequired; std::vector exprRequired; std::forward_list> workList{}; diff --git a/src/mapleall/maple_me/include/hdse.h b/src/mapleall/maple_me/include/hdse.h index 094aa36ce0033d96c786c2b5a5bfe9671b1d94cf..d89aac89fbc7a114fe39e984bd0c048152dc70d2 100644 --- a/src/mapleall/maple_me/include/hdse.h +++ b/src/mapleall/maple_me/include/hdse.h @@ -17,13 +17,14 @@ #include "bb.h" #include "irmap.h" #include "dominance.h" +#include "alias_class.h" namespace maple { class MeIRMap; class HDSE { public: HDSE(MIRModule &mod, const MapleVector &bbVec, BB &commonEntryBB, BB &commonExitBB, - Dominance &pDom, IRMap &map, bool enabledDebug = false, bool decouple = false) + Dominance &pDom, IRMap &map, const AliasClass *aliasClass, bool enabledDebug = false, bool decouple = false) : hdseDebug(enabledDebug), mirModule(mod), bbVec(bbVec), @@ -31,6 +32,7 @@ class HDSE { commonExitBB(commonExitBB), postDom(pDom), irMap(map), + aliasInfo(aliasClass), bbRequired(bbVec.size(), false), decoupleStatic(decouple) {} @@ -106,6 +108,7 @@ class HDSE { BB &commonExitBB; Dominance &postDom; IRMap &irMap; + const AliasClass *aliasInfo; std::vector bbRequired; std::vector exprLive; std::forward_list workList; diff --git a/src/mapleall/maple_me/include/me_dse.h b/src/mapleall/maple_me/include/me_dse.h index 43c58183d75d9c72697d258d2b0cdbd2e9acb3bd..b4d9fea3654fd57a01d939ec12a3e463c271376f 100644 --- a/src/mapleall/maple_me/include/me_dse.h +++ b/src/mapleall/maple_me/include/me_dse.h @@ -26,10 +26,10 @@ namespace maple { class MeDSE : public DSE { public: - MeDSE(MeFunction &func, Dominance *dom, bool enabledDebug) + MeDSE(MeFunction &func, Dominance *dom, const AliasClass *aliasClass, bool enabledDebug) : DSE(std::vector(func.GetCfg()->GetAllBBs().begin(), func.GetCfg()->GetAllBBs().end()), *func.GetCfg()->GetCommonEntryBB(), *func.GetCfg()->GetCommonExitBB(), *func.GetMeSSATab(), - *dom, enabledDebug, MeOption::decoupleStatic, func.IsLfo()), + *dom, aliasClass, enabledDebug, MeOption::decoupleStatic, func.IsLfo()), func(func), cfg(func.GetCfg()) {} virtual ~MeDSE() = default; diff --git a/src/mapleall/maple_me/include/me_hdse.h b/src/mapleall/maple_me/include/me_hdse.h index d3ce2a72643e7b668786692f23fcced79a791d61..a965e572bda2e87f6185f1e9e7956a602d08eb12 100644 --- a/src/mapleall/maple_me/include/me_hdse.h +++ b/src/mapleall/maple_me/include/me_hdse.h @@ -25,9 +25,9 @@ namespace maple { class MeHDSE : public HDSE { public: - MeHDSE(MeFunction &f, Dominance &pDom, IRMap &map, bool enabledDebug) + MeHDSE(MeFunction &f, Dominance &pDom, IRMap &map, const AliasClass *aliasClass, bool enabledDebug) : HDSE(f.GetMIRModule(), f.GetCfg()->GetAllBBs(), *f.GetCfg()->GetCommonEntryBB(), *f.GetCfg()->GetCommonExitBB(), - pDom, map, enabledDebug, MeOption::decoupleStatic), func(f) {} + pDom, map, aliasClass, enabledDebug, MeOption::decoupleStatic), func(f) {} virtual ~MeHDSE() = default; void BackwardSubstitution(); diff --git a/src/mapleall/maple_me/include/me_rename2preg.h b/src/mapleall/maple_me/include/me_rename2preg.h index bab487a25ae08cb028babd53777ad62333a731fa..427a2003473a9c59960e4bcd0a4c39c8471a608d 100644 --- a/src/mapleall/maple_me/include/me_rename2preg.h +++ b/src/mapleall/maple_me/include/me_rename2preg.h @@ -30,17 +30,20 @@ class SSARename2Preg { sym2reg_map(std::less(), alloc.Adapter()), vstidx2reg_map(alloc.Adapter()), parm_used_vec(alloc.Adapter()), - reg_formal_vec(alloc.Adapter()) {} + reg_formal_vec(alloc.Adapter()), + ostDefedByChi(ssaTab->GetOriginalStTableSize(), false, alloc.Adapter()), + ostUsedByMu(ssaTab->GetOriginalStTableSize(), false, alloc.Adapter()), + ostUsedByDread(ssaTab->GetOriginalStTableSize(), false, alloc.Adapter()) {} void RunSelf(); void PromoteEmptyFunction(); private: - AliasElem *GetAliasElem(const OriginalSt *ost) { + const MapleSet *GetAliasSet(const OriginalSt *ost) { if (ost->GetIndex() >= aliasclass->GetAliasElemCount()) { return nullptr; } - return aliasclass->FindAliasElem(*ost); + return aliasclass->FindAliasElem(*ost)->GetClassSet(); } void Rename2PregStmt(MeStmt *); @@ -54,6 +57,8 @@ class SSARename2Preg { void UpdateMirFunctionFormal(); void SetupParmUsed(const VarMeExpr *); void Init(); + void CollectUsedOst(MeExpr *meExpr); + void CollectDefUseInfoOfOst(); std::string PhaseName() const { return "rename2preg"; } @@ -69,6 +74,9 @@ class SSARename2Preg { MapleVector parm_used_vec; // if parameter is not used, it's false, otherwise true // if the parameter got promoted, the nth of func->mirFunc->_formal is the nth of reg_formal_vec, otherwise nullptr; MapleVector reg_formal_vec; + MapleVector ostDefedByChi; + MapleVector ostUsedByMu; + MapleVector ostUsedByDread; public: uint32 rename2pregCount = 0; }; diff --git a/src/mapleall/maple_me/include/ssa_epre.h b/src/mapleall/maple_me/include/ssa_epre.h index 5db9da0a2a7f79c1b5872f0892cff40bfbcbf50c..ad60e8c102e8fa085227b607a62662982b275e2e 100644 --- a/src/mapleall/maple_me/include/ssa_epre.h +++ b/src/mapleall/maple_me/include/ssa_epre.h @@ -57,6 +57,9 @@ class SSAEPre : public SSAPre { if (!workCand->isSRCand) { return x; } + if (x->GetMeOp() != kMeOpVar && x->GetMeOp() != kMeOpReg) { + return x; + } return static_cast(ResolveAllInjuringDefs(static_cast(x))); } void SubstituteOpnd(MeExpr *x, MeExpr *oldopnd, MeExpr *newopnd) override; diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index b73418fd791fba639919dfb8c93de47346aa0135..909a29a860d71d047b217bc19af755c0a28dfb98 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -1301,6 +1301,11 @@ bool AliasClass::MayAliasBasicAA(const OriginalSt *ostA, const OriginalSt *ostB) } // indirectLevA == 0 && indirectLevB == 0 + // preg has no alias + if (ostA->IsPregOst() || ostB->IsPregOst()) { + return false; + } + // different zero-level-var not alias each other if (ostA->GetMIRSymbol() != ostB->GetMIRSymbol()) { return false; @@ -1335,6 +1340,27 @@ bool AliasClass::MayAliasBasicAA(const OriginalSt *ostA, const OriginalSt *ostB) return (fieldIdA <= fieldIdB + fieldNumOfOstB) && (fieldIdB <= fieldIdA + fieldNumOfOstA); } +bool AliasClass::MayAlias(const OriginalSt *ostA, const OriginalSt *ostB) const { + if (!MayAliasBasicAA(ostA, ostB)) { + return false; + } + + if (ostA->GetIndex() >= osym2Elem.size()) { + return true; + } + if (ostB->GetIndex() >= osym2Elem.size()) { + return true; + } + + auto aliasElemA = osym2Elem[ostA->GetIndex()]; + auto aliasSet = aliasElemA->GetClassSet(); + if (aliasSet == nullptr) { + return false; + } + auto aliasElemB = osym2Elem[ostB->GetIndex()]; + return (aliasSet->find(aliasElemB->GetClassID()) != aliasSet->end()); +} + // here starts pass 2 code void AliasClass::InsertMayUseExpr(BaseNode &expr) { for (size_t i = 0; i < expr.NumOpnds(); ++i) { @@ -1673,8 +1699,7 @@ void AliasClass::CollectMayDefForMustDefs(const StmtNode &stmt, std::setGetClassID() && - aliasElem->GetOriginalSt().GetTyIdx() == lhsAe->GetOriginalSt().GetMIRSymbol()->GetTyIdx()) { + if (elemID != lhsAe->GetClassID()) { (void)mayDefOsts.insert(&aliasElem->GetOriginalSt()); } } diff --git a/src/mapleall/maple_me/src/dse.cpp b/src/mapleall/maple_me/src/dse.cpp index d0c9dc7888e16b30e96f1698583bcc55d2e1902a..f2a30e965f4816b3e5a8d674e88ac116d95fe629 100644 --- a/src/mapleall/maple_me/src/dse.cpp +++ b/src/mapleall/maple_me/src/dse.cpp @@ -260,7 +260,18 @@ void DSE::PropagateUseLive(const VersionSt &vst) { const MayDefNode *mayDef = vst.GetMayDef(); ASSERT(mayDef->GetResult() == &vst, "MarkVst: wrong corresponding version st in maydef"); const VersionSt *verSt = mayDef->GetOpnd(); - MarkStmtRequired(ToRef(mayDef->GetStmt()), ToRef(dfBB)); + + auto defStmt = mayDef->GetStmt(); + if (kOpcodeInfo.IsCallAssigned(defStmt->GetOpCode())) { + MapleVector &mustDefs = ssaTab.GetStmtMustDefNodes(*defStmt); + for (auto &node : mustDefs) { + if (aliasInfo->MayAlias(node.GetResult()->GetOst(), vst.GetOst())) { + AddToWorkList(node.GetResult()); + } + } + } + + MarkStmtRequired(ToRef(defStmt), ToRef(dfBB)); AddToWorkList(verSt); } else { const MustDefNode *mustDef = vst.GetMustDef(); diff --git a/src/mapleall/maple_me/src/hdse.cpp b/src/mapleall/maple_me/src/hdse.cpp index 5747d537c17aa3cc6df6d70e031ee55ef7b2db14..63b6e17dccae57f8c7a73c3acc9e93b04e71b59f 100644 --- a/src/mapleall/maple_me/src/hdse.cpp +++ b/src/mapleall/maple_me/src/hdse.cpp @@ -181,6 +181,17 @@ void HDSE::MarkChiNodeRequired(ChiMeNode &chiNode) { chiNode.SetIsLive(true); workList.push_front(chiNode.GetRHS()); MeStmt *meStmt = chiNode.GetBase(); + + // set MustDefNode live, which defines the chiNode. + auto *mustDefList = meStmt->GetMustDefList(); + if (mustDefList != nullptr) { + for (auto &mustDef : *mustDefList) { + if (aliasInfo->MayAlias(mustDef.GetLHS()->GetOst(), chiNode.GetLHS()->GetOst())) { + mustDef.SetIsLive(true); + } + } + } + MarkStmtRequired(*meStmt); } diff --git a/src/mapleall/maple_me/src/irmap_build.cpp b/src/mapleall/maple_me/src/irmap_build.cpp index 15cb82e7fd055d9ef0068b73b441b9eb25cd8648..1abe38152bd124137f8ac117ec73e03341d687ad 100644 --- a/src/mapleall/maple_me/src/irmap_build.cpp +++ b/src/mapleall/maple_me/src/irmap_build.cpp @@ -334,7 +334,7 @@ MeExpr *IRMapBuild::BuildExpr(BaseNode &mirNode, bool atParm, bool noProp) { if (typesize < GetPrimTypeSize(addrOfNode.GetPrimType()) && typesize != 0) { // need to insert a convert if (typesize < 4) { - OpMeExpr opmeexpr(kInvalidExprID, IsSignedInteger(addrOfNode.GetPrimType()) ? OP_sext : OP_zext, + OpMeExpr opmeexpr(kInvalidExprID, IsSignedInteger(retmeexpr->GetPrimType()) ? OP_sext : OP_zext, addrOfNode.GetPrimType(), 1); opmeexpr.SetBitsSize(static_cast(typesize * 8)); opmeexpr.SetOpnd(0, retmeexpr); @@ -389,7 +389,7 @@ MeExpr *IRMapBuild::BuildExpr(BaseNode &mirNode, bool atParm, bool noProp) { if (typesize < GetPrimTypeSize(iReadSSANode.GetPrimType()) && typesize != 0) { // need to insert a convert if (typesize < 4) { - OpMeExpr opmeexpr(kInvalidExprID, IsSignedInteger(iReadSSANode.GetPrimType()) ? OP_sext : OP_zext, + OpMeExpr opmeexpr(kInvalidExprID, IsSignedInteger(retmeexpr->GetPrimType()) ? OP_sext : OP_zext, iReadSSANode.GetPrimType(), 1); opmeexpr.SetBitsSize(static_cast(typesize * 8)); opmeexpr.SetOpnd(0, retmeexpr); diff --git a/src/mapleall/maple_me/src/me_delegate_rc.cpp b/src/mapleall/maple_me/src/me_delegate_rc.cpp index 6e10df2fa5cfcd32c0eadc08426cbe05b618d05b..2f0a2560d6ed3ff3a5d37afcea610fd0eaebd299 100644 --- a/src/mapleall/maple_me/src/me_delegate_rc.cpp +++ b/src/mapleall/maple_me/src/me_delegate_rc.cpp @@ -830,7 +830,8 @@ AnalysisResult *MeDoDelegateRC::Run(MeFunction *func, MeFuncResultMgr *m, Module ASSERT(dom != nullptr, "dominance phase has problem"); { // invoke hdse to update isLive only - MeHDSE hdse(*func, *dom, *func->GetIRMap(), DEBUGFUNC(func)); + auto aliasClass = static_cast(m->GetAnalysisResult(MeFuncPhase_ALIASCLASS, func)); + MeHDSE hdse(*func, *dom, *func->GetIRMap(), aliasClass, DEBUGFUNC(func)); hdse.InvokeHDSEUpdateLive(); } if (DEBUGFUNC(func)) { diff --git a/src/mapleall/maple_me/src/me_dse.cpp b/src/mapleall/maple_me/src/me_dse.cpp index e6cc71110ce1973bbf5fc4abf12c8ed8677c1637..d9f293fa5baa48c05ed10622dfee6e6dcf2846d6 100644 --- a/src/mapleall/maple_me/src/me_dse.cpp +++ b/src/mapleall/maple_me/src/me_dse.cpp @@ -65,7 +65,8 @@ AnalysisResult *MeDoDSE::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultM CHECK_NULL_FATAL(func); auto *postDom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); CHECK_NULL_FATAL(postDom); - MeDSE dse(*func, postDom, DEBUGFUNC(func)); + auto *aliasClass = static_cast(m->GetAnalysisResult(MeFuncPhase_ALIASCLASS, func)); + MeDSE dse(*func, postDom, aliasClass, DEBUGFUNC(func)); dse.RunDSE(); func->Verify(); // cfg change , invalid results in MeFuncResultMgr diff --git a/src/mapleall/maple_me/src/me_hdse.cpp b/src/mapleall/maple_me/src/me_hdse.cpp index 69de2a716b509eb6585eda6ba2db62aa244d5585..ab1fe9cde7681f38ab0c7b4e7a7d436bc24cfe75 100644 --- a/src/mapleall/maple_me/src/me_hdse.cpp +++ b/src/mapleall/maple_me/src/me_hdse.cpp @@ -169,10 +169,12 @@ void MeDoHDSE::MakeEmptyTrysUnreachable(MeFunction &func) { AnalysisResult *MeDoHDSE::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { auto *postDom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); CHECK_NULL_FATAL(postDom); + auto *aliasClass = static_cast(m->GetAnalysisResult(MeFuncPhase_ALIASCLASS, func)); + CHECK_NULL_FATAL(aliasClass); auto *hMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); CHECK_NULL_FATAL(hMap); - MeHDSE hdse(*func, *postDom, *hMap, DEBUGFUNC(func)); + MeHDSE hdse(*func, *postDom, *hMap, aliasClass, DEBUGFUNC(func)); hdse.hdseKeepRef = MeOption::dseKeepRef; hdse.DoHDSE(); hdse.BackwardSubstitution(); diff --git a/src/mapleall/maple_me/src/me_rename2preg.cpp b/src/mapleall/maple_me/src/me_rename2preg.cpp index 5cc0f56ff7c79f6dd22853d769af7c821aa6757c..282eae8893a37340ac290e0485ad9c3fb9023690 100644 --- a/src/mapleall/maple_me/src/me_rename2preg.cpp +++ b/src/mapleall/maple_me/src/me_rename2preg.cpp @@ -26,9 +26,6 @@ namespace maple { RegMeExpr *SSARename2Preg::RenameVar(const VarMeExpr *varmeexpr) { - if (varmeexpr->GetOst()->GetFieldID() != 0) { - return nullptr; - } const OriginalSt *ost = varmeexpr->GetOst(); if (ost->GetIndirectLev() != 0) { return nullptr; @@ -56,22 +53,39 @@ RegMeExpr *SSARename2Preg::RenameVar(const VarMeExpr *varmeexpr) { } return varreg; } else { - const OriginalSt *origOst = ost; - if (origOst->GetIndex() >= aliasclass->GetAliasElemCount()) { + if (ost->GetIndex() >= aliasclass->GetAliasElemCount()) { return nullptr; } - if (!mirst->IsLocal() || mirst->GetStorageClass() == kScPstatic || mirst->GetStorageClass() == kScFstatic) { + // var can be renamed to preg if ost of var: + // 1. not used by MU or defined by CHI; + // 2. aliased-ost of ost is not used anywhere (by MU or dread). + // If defining of aliased-ost defines ost as well. + // There must be a CHI defines ost, and this condition is included in the prev condition. + // Therefore, condition 2 not includes defined-by-CHI. + if (ostDefedByChi[ost->GetIndex()] || ostUsedByMu[ost->GetIndex()]) { return nullptr; } - if (origOst->IsAddressTaken()) { - return nullptr; + + auto *aliasSet = GetAliasSet(ost); + if (aliasSet != nullptr) { + for (auto aeId : *aliasSet) { + auto aliasedOst = aliasclass->FindID2Elem(aeId)->GetOst(); + if (aliasedOst == ost) { + continue; + } + bool aliasedOstUsed = ostUsedByMu[aliasedOst->GetIndex()] || ostUsedByDread[aliasedOst->GetIndex()]; + if (aliasedOstUsed && AliasClass::MayAliasBasicAA(ost, aliasedOst)) { + return nullptr; + } + } } - AliasElem *aliaselem = GetAliasElem(origOst); - if (aliaselem && aliaselem->GetClassSet()) { + + if (!mirst->IsLocal() || mirst->GetStorageClass() == kScPstatic || mirst->GetStorageClass() == kScFstatic) { return nullptr; } + RegMeExpr *curtemp = nullptr; - MIRType *ty = GlobalTables::GetTypeTable().GetTypeFromTyIdx(mirst->GetTyIdx()); + MIRType *ty = GlobalTables::GetTypeTable().GetTypeFromTyIdx(ost->GetTyIdx()); if (ty->GetKind() != kTypeScalar && ty->GetKind() != kTypePointer) { return nullptr; } @@ -305,6 +319,46 @@ void SSARename2Preg::UpdateMirFunctionFormal() { } } +void SSARename2Preg::CollectUsedOst(MeExpr *meExpr) { + if (meExpr->GetMeOp() == kMeOpIvar) { + auto *mu = static_cast(meExpr)->GetMu(); + ostUsedByMu[mu->GetOstIdx()] = true; + } else if (meExpr->GetMeOp() == kMeOpVar) { + auto ostIdx = static_cast(meExpr)->GetOstIdx(); + ostUsedByDread[ostIdx] = true; + } + for (uint32 id = 0; id < meExpr->GetNumOpnds(); ++id) { + CollectUsedOst(meExpr->GetOpnd(id)); + } +} + +void SSARename2Preg::CollectDefUseInfoOfOst() { + for (BB *meBB : func->GetCfg()->GetAllBBs()) { + if (meBB == nullptr) { + continue; + } + for (MeStmt &stmt: meBB->GetMeStmts()) { + for (uint32 id = 0; id < stmt.NumMeStmtOpnds(); ++id) { + CollectUsedOst(stmt.GetOpnd(id)); + } + + auto *muList = stmt.GetMuList(); + if (muList != nullptr) { + for (const auto &mu : *muList) { + ostUsedByMu[mu.first] = true; + } + } + + auto *chiList = stmt.GetChiList(); + if (chiList != nullptr) { + for (const auto &chi : *chiList) { + ostDefedByChi[chi.first] = true; + } + } + } + } +} + void SSARename2Preg::Init() { uint32 formalsize = func->GetMirFunc()->GetFormalDefVec().size(); parm_used_vec.resize(formalsize); @@ -314,6 +368,8 @@ void SSARename2Preg::Init() { void SSARename2Preg::RunSelf() { auto cfg = func->GetCfg(); Init(); + CollectDefUseInfoOfOst(); + for (BB *mebb : cfg->GetAllBBs()) { if (mebb == nullptr) { continue; diff --git a/src/mapleall/maple_me/src/me_scalar_analysis.cpp b/src/mapleall/maple_me/src/me_scalar_analysis.cpp index ed9896e5b37ec5b1b25baf7f20b654818bffa884..abd553f218ed27c78040b533d7d6a6c34827bd05 100644 --- a/src/mapleall/maple_me/src/me_scalar_analysis.cpp +++ b/src/mapleall/maple_me/src/me_scalar_analysis.cpp @@ -902,7 +902,9 @@ bool IsLegal(MeStmt &meStmt) { return false; } auto *opMeExpr = static_cast(meCmp); - CHECK_FATAL(opMeExpr->GetNumOpnds() == kNumOpnds, "must be"); + if (opMeExpr->GetNumOpnds() != kNumOpnds) { + return false; + } if (opMeExpr->GetOp() != OP_ge && opMeExpr->GetOp() != OP_le && opMeExpr->GetOp() != OP_lt && opMeExpr->GetOp() != OP_gt && opMeExpr->GetOp() != OP_eq) { diff --git a/src/mapleall/maple_me/src/me_ssa_epre.cpp b/src/mapleall/maple_me/src/me_ssa_epre.cpp index 0335d5a007743e156fb7dd1b9c1205d829c1c171..ef4730c43e93be3ea8dedc6a45cd9b73e0e290b8 100644 --- a/src/mapleall/maple_me/src/me_ssa_epre.cpp +++ b/src/mapleall/maple_me/src/me_ssa_epre.cpp @@ -115,7 +115,8 @@ AnalysisResult *MeDoSSAEPre::Run(MeFunction *func, MeFuncResultMgr *m, ModuleRes placeRC.ApplySSUPre(); } if (ssaPre.strengthReduction) { // for deleting redundant injury repairs - MeHDSE hdse(*func, *dom, *func->GetIRMap(), DEBUGFUNC(func)); + auto aliasClass = static_cast(m->GetAnalysisResult(MeFuncPhase_ALIASCLASS, func)); + MeHDSE hdse(*func, *dom, *func->GetIRMap(), aliasClass, DEBUGFUNC(func)); if (!MeOption::quiet) { LogInfo::MapleLogger() << " == " << PhaseName() << " invokes [ " << hdse.PhaseName() << " ] ==\n"; } diff --git a/src/mapleall/maple_me/src/ssa_epre_for_sr.cpp b/src/mapleall/maple_me/src/ssa_epre_for_sr.cpp index e7d0665bbe0bea252ab7c48c3f2aeca448c6cc84..e49de489411b858539393f2c594a42eef74664cd 100644 --- a/src/mapleall/maple_me/src/ssa_epre_for_sr.cpp +++ b/src/mapleall/maple_me/src/ssa_epre_for_sr.cpp @@ -279,7 +279,7 @@ MeExpr* SSAEPre::SRRepairInjuries(MeOccur *useocc, MePhiNode *scalarPhi = (defphiocc->GetRegPhi() ? defphiocc->GetRegPhi() : defphiocc->GetVarPhi()); repairedTemp = scalarPhi->GetLHS(); } - if (useexpr == nullptr) { + if (useexpr == nullptr || repairedTemp == nullptr) { return repairedTemp; } for (int32 i = 0; i < useexpr->GetNumOpnds(); i++) { diff --git a/src/mapleall/mpl2mpl/include/simplify.h b/src/mapleall/mpl2mpl/include/simplify.h index 0b88d584f74f611edaddcebe6eedc2e324303b3f..59284336f4e100d38f8675e42df21680699d28e3 100644 --- a/src/mapleall/mpl2mpl/include/simplify.h +++ b/src/mapleall/mpl2mpl/include/simplify.h @@ -41,6 +41,7 @@ class Simplify : public FuncOptimizeImpl { bool IsMathMax(const std::string funcName); bool SimplifyMathMethod(const StmtNode &stmt, BlockNode &block); void SimplifyCallAssigned(const StmtNode &stmt, BlockNode &block); + void SplitAggCopy(StmtNode *stmt, BlockNode *block, MIRFunction *func); }; class DoSimplify : public ModulePhase { public: diff --git a/src/mapleall/mpl2mpl/src/simplify.cpp b/src/mapleall/mpl2mpl/src/simplify.cpp index 4352ccddddc3040371c71a051dc976694d0025cb..08e382ac49e1a0ad9fabf4edfb18dad3c37e4d4e 100644 --- a/src/mapleall/mpl2mpl/src/simplify.cpp +++ b/src/mapleall/mpl2mpl/src/simplify.cpp @@ -93,6 +93,57 @@ void Simplify::SimplifyCallAssigned(const StmtNode &stmt, BlockNode &block) { } } +void Simplify::SplitAggCopy(StmtNode *stmt, BlockNode *block, MIRFunction *func) { + if (stmt->GetOpCode() == OP_dassign && stmt->Opnd(0)->GetOpCode() == OP_dread && + stmt->Opnd(0)->GetPrimType() == PTY_agg) { + auto *dassign = static_cast(stmt); + const auto &lhsStIdx = dassign->GetStIdx(); + auto *dread = static_cast(stmt->Opnd(0)); + const auto &rhsStIdx = dread->GetStIdx(); + + auto lhsSymbol = func->GetLocalOrGlobalSymbol(lhsStIdx); + auto lhsMIRType = lhsSymbol->GetType(); + auto lhsFieldID = dassign->GetFieldID(); + if (lhsFieldID != 0) { + CHECK_FATAL(lhsMIRType->IsStructType(), "only struct has non-zero fieldID"); + lhsMIRType = static_cast(lhsMIRType)->GetFieldType(lhsFieldID); + } + + auto rhsSymbol = func->GetLocalOrGlobalSymbol(rhsStIdx); + auto rhsMIRType = rhsSymbol->GetType(); + auto rhsFieldID = dread->GetFieldID(); + if (rhsFieldID != 0) { + CHECK_FATAL(rhsMIRType->IsStructType(), "only struct has non-zero fieldID"); + rhsMIRType = static_cast(rhsMIRType)->GetFieldType(rhsFieldID); + } + + if (!lhsSymbol->IsLocal() && !rhsSymbol->IsLocal()) { + return; + } + + if (lhsMIRType != rhsMIRType) { + return; + } + + if (lhsMIRType->IsStructType()) { + auto *structType = static_cast(lhsMIRType); + constexpr uint32 upperLimitOfFieldNum = 10; + if (lhsMIRType->NumberOfFieldIDs() > upperLimitOfFieldNum) { + return; + } + auto mirBuiler = func->GetModule()->GetMIRBuilder(); + for (uint id = 1; id <= lhsMIRType->NumberOfFieldIDs(); ++id) { + auto newRHS = mirBuiler->CreateDread(*rhsSymbol, structType->GetFieldType(id)->GetPrimType()); + newRHS->SetFieldID(id + rhsFieldID); + auto newDassign = mirBuiler->CreateStmtDassign(lhsStIdx, lhsFieldID + id, newRHS); + newDassign->SetSrcPos(stmt->GetSrcPos()); + block->InsertBefore(stmt, newDassign); + } + block->RemoveStmt(stmt); + } + } +} + void Simplify::ProcessFunc(MIRFunction *func) { if (func->IsEmpty()) { return; @@ -135,6 +186,10 @@ void Simplify::ProcessFuncStmt(MIRFunction &func, StmtNode *stmt, BlockNode *blo SimplifyCallAssigned(*stmt, *block); break; } + case OP_dassign: { + SplitAggCopy(stmt, block, &func); + break; + } default: { break; } diff --git a/src/mplfe/ast_input/include/ast_expr.h b/src/mplfe/ast_input/include/ast_expr.h index 8dae6b6d3f0f3281f2f30241ba861667ec020764..91524b138a07abd9ec16c121c7e725f6d455531c 100644 --- a/src/mplfe/ast_input/include/ast_expr.h +++ b/src/mplfe/ast_input/include/ast_expr.h @@ -586,8 +586,8 @@ class ASTInitListExpr : public ASTExpr { ASTInitListExpr *initList, std::list &stmts) const; void ProcessDesignatedInitUpdater(std::variant, UniqueFEIRExpr> &base, ASTExpr *expr, std::list &stmts) const; - void ProcessStringLiteralInitList(UniqueFEIRExpr addrOfCharArray, MIRArrayType *arrayType, - UniqueFEIRExpr addrofStringLiteral, std::list &stmts) const; + void ProcessStringLiteralInitList(UniqueFEIRExpr addrOfCharArray, UniqueFEIRExpr addrOfStringLiteral, + uint32 stringLength, std::list &stmts) const; MIRConst *GenerateMIRConstForArray() const; MIRConst *GenerateMIRConstForStruct() const; std::vector initExprs; @@ -721,6 +721,10 @@ class ASTStringLiteral : public ASTExpr { length = len; } + size_t GetLength() { + return length; + } + void SetCodeUnits(std::vector &units) { codeUnits = std::move(units); } @@ -783,8 +787,8 @@ class ASTArraySubscriptExpr : public ASTExpr { isVLA = flag; } - void SetVLASizeExprs(std::vector &exprs) { - vlaSizeExprs = exprs; + void SetVLASizeExpr(ASTExpr *expr) { + vlaSizeExpr = expr; } private: @@ -796,7 +800,7 @@ class ASTArraySubscriptExpr : public ASTExpr { MIRType *arrayType = nullptr; ASTExpr *idxExpr = nullptr; bool isVLA = false; - std::vector vlaSizeExprs; + ASTExpr *vlaSizeExpr = nullptr; }; class ASTExprUnaryExprOrTypeTraitExpr : public ASTExpr { diff --git a/src/mplfe/ast_input/lib/ast_type.cpp b/src/mplfe/ast_input/lib/ast_type.cpp index 99e54110ee6e398698b0d0ec8aafdedf5cba0223..ff402e7ba76763b55994e9036895cfac6a1dc204 100644 --- a/src/mplfe/ast_input/lib/ast_type.cpp +++ b/src/mplfe/ast_input/lib/ast_type.cpp @@ -38,9 +38,11 @@ PrimType LibAstFile::CvtPrimType(const clang::BuiltinType::Kind kind) const { case clang::BuiltinType::Bool: return PTY_u1; case clang::BuiltinType::Char_U: + return astContext->CharTy->isSignedIntegerType() ? PTY_i8 : PTY_u8; case clang::BuiltinType::UChar: return PTY_u8; case clang::BuiltinType::WChar_U: + return astContext->WCharTy->isSignedIntegerType() ? PTY_i16 : PTY_u16; case clang::BuiltinType::UShort: return PTY_u16; case clang::BuiltinType::UInt: diff --git a/src/mplfe/ast_input/src/ast_expr.cpp b/src/mplfe/ast_input/src/ast_expr.cpp index e24c7278967fb604ef82a22be87d8d37cdbbaf06..38d67d944ee9d7205f37a064907da1bc88175929 100644 --- a/src/mplfe/ast_input/src/ast_expr.cpp +++ b/src/mplfe/ast_input/src/ast_expr.cpp @@ -216,7 +216,7 @@ UniqueFEIRExpr ASTCallExpr::AddRetExpr(std::unique_ptr &callStmt callStmt->SetVar(var->Clone()); } stmts.emplace_back(std::move(callStmt)); - return FEIRBuilder::CreateExprDRead(std::move(dreadVar)); + return FEIRBuilder::CreateExprDRead(dreadVar->Clone()); } std::unique_ptr ASTCallExpr::GenCallStmt() const { @@ -292,18 +292,22 @@ MIRConst *ASTCastExpr::GenerateMIRConstImpl() const { } MIRConst *ASTCastExpr::GenerateMIRDoubleConst() const { - switch (GetConstantValue()->pty) { - case PTY_f32: { + MIRConst *childConst = child->GenerateMIRConst(); + switch (childConst->GetKind()) { + case kConstFloatConst: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.f32), *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); } - case PTY_i64: { + case kConstInt: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.i64), *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); } - case PTY_f64: { + case kConstDoubleConst: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.f64), *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); } default: { CHECK_FATAL(false, "Unsupported pty type: %d", GetConstantValue()->pty); @@ -313,14 +317,17 @@ MIRConst *ASTCastExpr::GenerateMIRDoubleConst() const { } MIRConst *ASTCastExpr::GenerateMIRFloatConst() const { - switch (GetConstantValue()->pty) { - case PTY_f64: { + MIRConst *childConst = child->GenerateMIRConst(); + switch (childConst->GetKind()) { + case kConstDoubleConst: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.f64), *GlobalTables::GetTypeTable().GetPrimType(PTY_f32)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_f32)); } - case PTY_i64: { + case kConstInt: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.i64), *GlobalTables::GetTypeTable().GetPrimType(PTY_f32)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_f32)); } default: { CHECK_FATAL(false, "Unsupported pty type: %d", GetConstantValue()->pty); @@ -330,26 +337,28 @@ MIRConst *ASTCastExpr::GenerateMIRFloatConst() const { } MIRConst *ASTCastExpr::GenerateMIRIntConst() const { - switch (GetConstantValue()->pty) { - case PTY_f64: { + MIRConst *childConst = child->GenerateMIRConst(); + switch (childConst->GetKind()) { + case kConstDoubleConst: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.f64), *GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); } - case PTY_i64: { + case kConstInt: { PrimType srcPrimType = src->GetPrimType(); - int64 val = GetConstantValue()->val.i64; + int64 val = static_cast(childConst)->GetValue(); switch (srcPrimType) { case PTY_u8: - val = static_cast(GetConstantValue()->val.i64); + val = static_cast(val); break; case PTY_u16: - val = static_cast(GetConstantValue()->val.i64); + val = static_cast(val); break; case PTY_u32: - val = static_cast(GetConstantValue()->val.i64); + val = static_cast(val); break; case PTY_u64: - val = static_cast(GetConstantValue()->val.i64); + val = static_cast(val); break; default: break; @@ -357,9 +366,10 @@ MIRConst *ASTCastExpr::GenerateMIRIntConst() const { return FEManager::GetModule().GetMemPool()->New( val, *GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); } - case PTY_a64: { + case kConstStrConst: { return FEManager::GetModule().GetMemPool()->New( - static_cast(GetConstantValue()->val.strIdx), *GlobalTables::GetTypeTable().GetPrimType(PTY_a64)); + static_cast(static_cast(childConst)->GetValue()), + *GlobalTables::GetTypeTable().GetPrimType(PTY_a64)); } default: { CHECK_FATAL(false, "Unsupported pty type: %d", GetConstantValue()->pty); @@ -944,13 +954,12 @@ void ASTInitListExpr::ProcessInitList(std::variant &stmts) const { +void ASTInitListExpr::ProcessStringLiteralInitList(UniqueFEIRExpr addrOfCharArray, UniqueFEIRExpr addrOfStringLiteral, + uint32 stringLength, std::list &stmts) const { std::unique_ptr> argExprList = std::make_unique>(); argExprList->emplace_back(addrOfCharArray->Clone()); - argExprList->emplace_back(addrofStringLiteral->Clone()); - argExprList->emplace_back(FEIRBuilder::CreateExprConstI32(arrayType->GetSize())); + argExprList->emplace_back(addrOfStringLiteral->Clone()); + argExprList->emplace_back(FEIRBuilder::CreateExprConstI32(stringLength)); std::unique_ptr memcpyStmt = std::make_unique( INTRN_C_memcpy, nullptr, nullptr, std::move(argExprList)); stmts.emplace_back(std::move(memcpyStmt)); @@ -1035,9 +1044,8 @@ void ASTInitListExpr::ProcessStructInitList(std::variantGetKind() == kTypeArray && initList->initExprs[i]->GetASTOp() == kASTStringLiteral) { auto addrOfElement = std::make_unique(baseStructFEPtrType->Clone(), fieldID, std::get(base)->Clone()); - ProcessStringLiteralInitList(addrOfElement->Clone(), static_cast(fieldMirType), - elemExpr->Clone(), - stmts); + ProcessStringLiteralInitList(addrOfElement->Clone(), elemExpr->Clone(), + static_cast(initList->initExprs[i])->GetLength(), stmts); } else { auto stmt = std::make_unique(baseStructFEPtrType->Clone(), std::get(base)->Clone(), @@ -1050,9 +1058,8 @@ void ASTInitListExpr::ProcessStructInitList(std::variantGetKind() == kTypeArray && initList->initExprs[i]->GetASTOp() == kASTStringLiteral) { auto addrOfElement = std::make_unique(var->Clone()); addrOfElement->SetFieldID(fieldID); - ProcessStringLiteralInitList(addrOfElement->Clone(), static_cast(fieldMirType), - elemExpr->Clone(), - stmts); + ProcessStringLiteralInitList(addrOfElement->Clone(), elemExpr->Clone(), + static_cast(initList->initExprs[i])->GetLength(), stmts); } else { auto stmt = std::make_unique(var->Clone(), elemExpr->Clone(), fieldID); stmts.emplace_back(std::move(stmt)); @@ -1091,8 +1098,8 @@ void ASTInitListExpr::ProcessArrayInitList(UniqueFEIRExpr addrOfArray, ASTInitLi } else { UniqueFEIRExpr elemExpr = initList->initExprs[i]->Emit2FEExpr(stmts); if (elementType->GetKind() == kTypeArray && initList->initExprs[i]->GetASTOp() == kASTStringLiteral) { - ProcessStringLiteralInitList(addrOfElemExpr->Clone(), static_cast(elementPtrType), - elemExpr->Clone(), stmts); + ProcessStringLiteralInitList(addrOfElemExpr->Clone(), elemExpr->Clone(), + static_cast(initList->initExprs[i])->GetLength(), stmts); } else { auto stmt = FEIRBuilder::CreateStmtIAssign(elementPtrFEType->Clone(), addrOfElemExpr->Clone(), elemExpr->Clone(), @@ -1214,7 +1221,7 @@ UniqueFEIRExpr ASTArraySubscriptExpr::Emit2FEExprImpl(std::list auto feIdxExpr = idxExpr->Emit2FEExpr(stmts); UniqueFEIRExpr feSizeExpr; if (isVLA) { - feSizeExpr = vlaSizeExprs[0]->Emit2FEExpr(stmts); + feSizeExpr = vlaSizeExpr->Emit2FEExpr(stmts); } else { feSizeExpr = FEIRBuilder::CreateExprConstU64(mirType->GetSize()); } diff --git a/src/mplfe/ast_input/src/ast_parser.cpp b/src/mplfe/ast_input/src/ast_parser.cpp index 913075141ae2e616e5c851ec2f011108ef88a7e2..687ef4f69a0eb0babf0bcabdcb33efd53010bfc0 100644 --- a/src/mplfe/ast_input/src/ast_parser.cpp +++ b/src/mplfe/ast_input/src/ast_parser.cpp @@ -1105,46 +1105,26 @@ ASTExpr *ASTParser::ProcessExprArraySubscriptExpr(MapleAllocator &allocator, con } auto arrayMirType = astFile->CvtType(arrayQualType); astArraySubscriptExpr->SetArrayType(arrayMirType); - std::vector vlaTypeSizeExprs; if (llvm::isa(arrayQualType) || (llvm::isa(arrayQualType) && llvm::cast(arrayQualType)->getPointeeType()->isVariableArrayType())) { if (llvm::isa(arrayQualType)) { arrayQualType = llvm::cast(arrayQualType)->getPointeeType(); - auto astSizeExpr = BuildExprToComputeSizeFromVLA(allocator, arrayQualType);; - vlaTypeSizeExprs.emplace_back(astSizeExpr); } astArraySubscriptExpr->SetIsVLA(true); auto elementTypeSizeExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); elementTypeSizeExpr->SetVal(GetSizeFromQualType(llvm::cast(arrayQualType)->getElementType())); elementTypeSizeExpr->SetType(PTY_i32); - while (arrayQualType->isArrayType()) { - if (llvm::isa(arrayQualType)) { - auto sizeExpr = llvm::cast(arrayQualType)->getSizeExpr(); - auto astSizeExpr = ProcessExpr(allocator, sizeExpr); - auto astBOExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); - astBOExpr->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); - astBOExpr->SetOpcode(OP_mul); - astBOExpr->SetLeftExpr(astSizeExpr); - astBOExpr->SetRightExpr(elementTypeSizeExpr); - vlaTypeSizeExprs.emplace_back(astBOExpr); - } else if (llvm::isa(arrayQualType)) { - auto size = llvm::cast(arrayQualType)->getSize().getSExtValue(); - auto astIntegerExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); - astIntegerExpr->SetVal(size); - astIntegerExpr->SetType(PTY_i32); - auto astBOExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); - astBOExpr->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); - astBOExpr->SetOpcode(OP_mul); - astBOExpr->SetLeftExpr(astIntegerExpr); - astBOExpr->SetRightExpr(elementTypeSizeExpr); - vlaTypeSizeExprs.emplace_back(astBOExpr); - } - vlaTypeSizeExprs.pop_back(); - vlaTypeSizeExprs.push_back(elementTypeSizeExpr); - astArraySubscriptExpr->SetVLASizeExprs(vlaTypeSizeExprs); - arrayQualType = llvm::cast(arrayQualType)->getElementType(); - } + + auto sizeExpr = llvm::cast(arrayQualType)->getSizeExpr(); + auto astSizeExpr = ProcessExpr(allocator, sizeExpr); + ASTBinaryOperatorExpr *vlaTypeSizeExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); + vlaTypeSizeExpr->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_i64)); + vlaTypeSizeExpr->SetOpcode(OP_mul); + vlaTypeSizeExpr->SetLeftExpr(astSizeExpr); + vlaTypeSizeExpr->SetRightExpr(elementTypeSizeExpr); + + astArraySubscriptExpr->SetVLASizeExpr(vlaTypeSizeExpr); } ASTExpr *astBaseExpr = ProcessExpr(allocator, base); astArraySubscriptExpr->SetBaseExpr(astBaseExpr); @@ -1725,7 +1705,14 @@ ASTExpr *ASTParser::ProcessExprBinaryOperator(MapleAllocator &allocator, const c rhsType->isIntegerType() && !boType->isVoidPointerType()) { auto ptrSizeExpr = ASTDeclsBuilder::ASTExprBuilder(allocator); ptrSizeExpr->SetType(PTY_i32); - ptrSizeExpr->SetVal(GetSizeFromQualType(boType->getPointeeType())); + auto boMirType = astFile->CvtType(boType); + auto typeSize = GetSizeFromQualType(boType->getPointeeType()); + MIRType *pointedType = GlobalTables::GetTypeTable().GetTypeFromTyIdx( + static_cast(boMirType)->GetPointedTyIdx()); + if (pointedType->GetPrimType() == PTY_f64) { + typeSize = 8; // 8 is f64 byte num, because now f128 also cvt to f64 + } + ptrSizeExpr->SetVal(typeSize); if (lhsType->isPointerType()) { auto rhs = ASTDeclsBuilder::ASTExprBuilder(allocator); rhs->SetLeftExpr(astRExpr); @@ -1741,6 +1728,7 @@ ASTExpr *ASTParser::ProcessExprBinaryOperator(MapleAllocator &allocator, const c lhs->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_i32)); astLExpr = lhs; } + astBinOpExpr->SetCvtNeeded(true); } astBinOpExpr->SetLeftExpr(astLExpr); astBinOpExpr->SetRightExpr(astRExpr); diff --git a/src/mplfe/ast_input/src/ast_stmt.cpp b/src/mplfe/ast_input/src/ast_stmt.cpp index 8131668e459ef0367db9f401ac230589bc860708..6e1efb40b903962ce122cc2117f3ec7a3a0a60b3 100644 --- a/src/mplfe/ast_input/src/ast_stmt.cpp +++ b/src/mplfe/ast_input/src/ast_stmt.cpp @@ -79,6 +79,11 @@ std::list ASTIfStmt::Emit2FEStmtImpl() const { elseStmts = elseStmt->Emit2FEStmt(); } UniqueFEIRExpr condFEExpr = condExpr->Emit2FEExpr(stmts); + if (condFEExpr->GetKind() == kExprConst) { + if (static_cast(condFEExpr.get())->GetValue().u64 != 0) { + condFEExpr = FEIRBuilder::CreateExprConstI32(1); + } + } UniqueFEIRStmt ifStmt; ifStmt = FEIRBuilder::CreateStmtIf(std::move(condFEExpr), thenStmts, elseStmts); ifStmt->SetSrcFileInfo(GetSrcFileIdx(), GetSrcFileLineNum()); diff --git a/src/mplfe/common/include/feir_stmt.h b/src/mplfe/common/include/feir_stmt.h index 09aa6481a9aa85c45fa1222a9a3833de005cf1be..09354b07be44af6606ac0657f1f2b20ff13896fe 100644 --- a/src/mplfe/common/include/feir_stmt.h +++ b/src/mplfe/common/include/feir_stmt.h @@ -337,7 +337,10 @@ class FEIRExpr { if ((type0 == PTY_u64 && type1 == PTY_ptr) || (type0 == PTY_ptr && type1 == PTY_u64)) { return; } - CHECK_FATAL(type0 == type1, "primtype of opnds must be the same"); + CHECK_FATAL(type0 == type1 || + GetRegPrimType(type0) == type1 || + type0 == GetRegPrimType(type1), + "primtype of opnds must be the same"); } protected: diff --git a/src/mplfe/test/feir_builder_test.cpp b/src/mplfe/test/feir_builder_test.cpp index ce7ddb9c27ac7de7f097fd1edc35d36229fcf996..5f6dd9937f002bff58e37c7ea4691a4246466b91 100644 --- a/src/mplfe/test/feir_builder_test.cpp +++ b/src/mplfe/test/feir_builder_test.cpp @@ -98,7 +98,7 @@ TEST_F(FEIRBuilderTest, CreateRetypeShort2Float) { ASSERT_EQ(mirStmts.size(), 1); mirStmts.front()->Dump(); std::string dumpStr = GetBufferString(); - std::string pattern = std::string("dassign %Reg0_F 0 (cvt f32 i16 (dread i16 %Reg0_S))"); + std::string pattern = std::string("dassign %Reg0_F 0 (cvt f32 i16 (dread i32 %Reg0_S))"); EXPECT_EQ(dumpStr.find(pattern), 0); RestoreCout(); } @@ -128,7 +128,7 @@ TEST_F(FEIRBuilderTest, CreateRetypeShort2Ref) { mirStmts.front()->Dump(); std::string dumpStr = GetBufferString(); EXPECT_EQ(dumpStr.find("dassign %Reg0_R"), 0); - EXPECT_EQ(dumpStr.find(" 0 (cvt ref i16 (dread i16 %Reg0_S))", 15) != std::string::npos, true); + EXPECT_EQ(dumpStr.find(" 0 (cvt ref i16 (dread i32 %Reg0_S))", 15) != std::string::npos, true); RestoreCout(); } diff --git a/src/mplfe/test/feir_stmt_test.cpp b/src/mplfe/test/feir_stmt_test.cpp index 3e8116af0b30f857271f4d7aaf9c71864b9f0c68..38dfdd2e2fda9f6d47a3e9eda7284b673ffc3223 100644 --- a/src/mplfe/test/feir_stmt_test.cpp +++ b/src/mplfe/test/feir_stmt_test.cpp @@ -248,7 +248,7 @@ TEST_F(FEIRStmtTest, FEIRExprBinary_lshr) { RedirectCout(); baseNode->Dump(); std::string dumpStr = GetBufferString(); - std::string pattern = std::string("lshr i32 \\(dread i32 %Reg0_I, dread i8 %Reg1_B\\)") + MPLFEUTRegx::Any(); + std::string pattern = std::string("lshr i32 \\(dread i32 %Reg0_I, dread i32 %Reg1_B\\)") + MPLFEUTRegx::Any(); EXPECT_EQ(MPLFEUTRegx::Match(dumpStr, pattern), true); RestoreCout(); } @@ -286,7 +286,7 @@ TEST_F(FEIRStmtTest, FEIRExprTernary_add) { baseNode->Dump(); std::string dumpStr = GetBufferString(); std::string pattern = - std::string("select i32 \\(dread u1 %Reg0_Z, dread i32 %Reg1_I, dread i32 %Reg2_I\\)") + MPLFEUTRegx::Any(); + std::string("select i32 \\(dread u32 %Reg0_Z, dread i32 %Reg1_I, dread i32 %Reg2_I\\)") + MPLFEUTRegx::Any(); EXPECT_EQ(MPLFEUTRegx::Match(dumpStr, pattern), true); RestoreCout(); std::vector varUses = expr2->GetVarUses(); @@ -324,7 +324,7 @@ TEST_F(FEIRStmtTest, FEIRStmtIf) { RedirectCout(); baseNodes.front()->Dump(); std::string pattern = - "if (dread u1 %Reg0_Z) {\n"\ + "if (dread u32 %Reg0_Z) {\n"\ " dassign %Reg0_I 0 (dread i32 %Reg1_I)\n"\ "}\n"\ "else {\n"\