diff --git a/src/mapleall/bin/dex2mpl b/src/mapleall/bin/dex2mpl index f81c2d35ec04100620078e8bf1d0ce32a378930c..98a0ef5633e1a2310c14d1eaea31645c4303ff3d 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 1b37e3b04a12f02f27e530dfece9bfa1b0c6d216..5a1ce25b2da67647467b801d198f369c010336a4 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_proepilog.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_proepilog.cpp index a8d92666ee373c53c750f4f08e3b9226c862c737..3cd16e26c986ee1b5ed4aa4286b03db0bceb3816 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_proepilog.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_proepilog.cpp @@ -160,7 +160,7 @@ bool AArch64GenProEpilog::TailCallOpt() { exitBB = cgFunc.GetExitBBsVec().front(); } - CHECK_FATAL(exitBB->GetFirstInsn() == nullptr, "exit bb should be empty."); + CHECK_FATAL(exitBB->GetFirstMachineInsn() == nullptr, "exit bb should be empty."); /* Count how many call insns in the whole function. */ uint32 nCount = 0; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_reaching.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_reaching.cpp index e49ded12a0cc72885675888bad1ed934fb862ec5..45985bcb9a1d92d9a445d2b234dc9f00754c9ab8 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_reaching.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_reaching.cpp @@ -872,6 +872,10 @@ void AArch64ReachingDefinition::InitInfoForMemOperand(Insn &insn, Operand &opnd, return; } if ((mode & kRDMemAnalysis) && IsFrameReg(*base)) { + if (index != nullptr) { + SetAnalysisMode(kRDRegAnalysis); + return; + } CHECK_FATAL(index == nullptr, "Existing [x29 + index] Memory Address"); ASSERT(memOpnd.GetOffsetImmediate(), "offset must be a immediate value"); int32 offsetVal = memOpnd.GetOffsetImmediate()->GetOffsetValue(); diff --git a/src/mapleall/maple_ipa/include/inline.h b/src/mapleall/maple_ipa/include/inline.h index de4104c382a1a341e6fac321d3f842ddeb5b68db..0f68df61918810660500d41302305abb06f1bb1a 100644 --- a/src/mapleall/maple_ipa/include/inline.h +++ b/src/mapleall/maple_ipa/include/inline.h @@ -126,6 +126,7 @@ class MInline { return false; } + void ConvertPStaticToFStatic(MIRFunction &func) const; bool CheckCalleeAndInline(MIRFunction*, BlockNode *enclosingBlk, CallNode*, MIRFunction*); void InlineCalls(const CGNode&); void InlineCallsBlock(MIRFunction&, BlockNode&, BaseNode&, bool&); diff --git a/src/mapleall/maple_ipa/src/inline.cpp b/src/mapleall/maple_ipa/src/inline.cpp index fd52719f406d443e12d4808e592e1b550ac63576..3903d3814dc90ad64b0dbd1bd7252be8ecff285e 100644 --- a/src/mapleall/maple_ipa/src/inline.cpp +++ b/src/mapleall/maple_ipa/src/inline.cpp @@ -199,6 +199,7 @@ uint32 MInline::RenameSymbols(MIRFunction &caller, const MIRFunction &callee, ui if (sym == nullptr) { continue; } + CHECK_FATAL(sym->GetStorageClass() != kScPstatic, "pstatic symbols should have been converted to fstatic ones"); std::string syName(kUnderlineStr); // Use puIdx here instead of func name because our mangled func name can be // really long. @@ -233,12 +234,15 @@ uint32 MInline::RenameSymbols(MIRFunction &caller, const MIRFunction &callee, ui } static StIdx UpdateIdx(const StIdx &stIdx, uint32 stIdxOff, const std::unordered_map &staticOld2New) { + // If the callee has pstatic symbols, we will save all symbol mapping info in this map. + // So if this map is empty, we only use stIdxOff to update stIdx, otherwise we only use this map. StIdx newStIdx = stIdx; - auto it = staticOld2New.find(newStIdx.FullIdx()); - if (it != staticOld2New.end()) { - newStIdx.SetFullIdx(it->second); - } else { + if (staticOld2New.empty()) { newStIdx.SetIdx(newStIdx.Idx() + stIdxOff); + } else { + auto it = staticOld2New.find(newStIdx.FullIdx()); + CHECK_FATAL(it != staticOld2New.end(), "All symbol mapping info should be in this map"); + newStIdx.SetFullIdx(it->second); } return newStIdx; } @@ -600,6 +604,62 @@ void MInline::RecordRealCaller(MIRFunction &caller, const MIRFunction &callee) { } } +void MInline::ConvertPStaticToFStatic(MIRFunction &func) const { + bool hasPStatic = false; + for (int i = 0; i < func.GetSymbolTabSize(); ++i) { + MIRSymbol *sym = func.GetSymbolTabItem(i); + if (sym != nullptr && sym->GetStorageClass() == kScPstatic) { + hasPStatic = true; + break; + } + } + if (!hasPStatic) { + return; // No pu-static symbols, just return + } + std::unordered_map oldStFullIdx2New; + std::vector localSymbols; + int pstaticNum = 0; + for (int i = 0; i < func.GetSymbolTabSize(); ++i) { + MIRSymbol *sym = func.GetSymbolTabItem(i); + if (sym == nullptr) { + continue; + } + StIdx oldStIdx = sym->GetStIdx(); + if (sym->GetStorageClass() == kScPstatic) { + ++pstaticNum; + // convert pu-static to file-static + // pstatic symbol name mangling example: "foo_bar" --> "__pstatic__125__foo_bar" + const auto &symNameOrig = sym->GetName(); + std::string symNameMangling = "__pstatic__" + std::to_string(func.GetPuidx()) + kVerticalLineStr + symNameOrig; + GStrIdx strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(symNameMangling); + MIRSymbol *newSym = GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal); + newSym->SetNameStrIdx(strIdx); + newSym->SetStorageClass(kScFstatic); + newSym->SetTyIdx(sym->GetTyIdx()); + newSym->SetSKind(sym->GetSKind()); + newSym->SetAttrs(sym->GetAttrs()); + newSym->SetValue(sym->GetValue()); + bool success = GlobalTables::GetGsymTable().AddToStringSymbolMap(*newSym); + CHECK_FATAL(success, "Found repeated global symbols!"); + (void)oldStFullIdx2New.emplace(oldStIdx.FullIdx(), newSym->GetStIdx().FullIdx()); + } else { + StIdx newStIdx(oldStIdx); + newStIdx.SetIdx(oldStIdx.Idx() - pstaticNum); + (void)oldStFullIdx2New.emplace(oldStIdx.FullIdx(), newStIdx.FullIdx()); + sym->SetStIdx(newStIdx); + localSymbols.push_back(sym); + } + } + func.GetSymTab()->Clear(); + func.GetSymTab()->PushNullSymbol(); + for (MIRSymbol *sym : localSymbols) { + func.GetSymTab()->AddStOutside(sym); + } + // The map is never empty, so the stIdxOff will be ignored, 0 is just a placeholder + ASSERT(!oldStFullIdx2New.empty(), "Impossible"); + ReplaceSymbols(func.GetBody(), 0, oldStFullIdx2New); +} + // Inline CALLEE into CALLER. bool MInline::PerformInline(MIRFunction &caller, BlockNode &enclosingBlk, CallNode &callStmt, MIRFunction &callee) { if (callee.IsEmpty()) { @@ -616,6 +676,9 @@ bool MInline::PerformInline(MIRFunction &caller, BlockNode &enclosingBlk, CallNo } else { inlinedTimes = 0; } + // If the callee has local static variables, We convert local pu-static symbols to global file-static symbol to avoid + // multiple definition for these static symbols + ConvertPStaticToFStatic(callee); // Step 1: Clone CALLEE's body. auto getBody = [callee, this] (BlockNode* funcBody) { if (callee.IsFromMpltInline()) { @@ -663,7 +726,8 @@ bool MInline::PerformInline(MIRFunction &caller, BlockNode &enclosingBlk, CallNo // Step 4: Null check 'this' and assign actuals to formals. if (static_cast(callStmt.NumOpnds()) != callee.GetFormalCount()) { LogInfo::MapleLogger() << "warning: # formal arguments != # actual arguments in the function " << - callee.GetName() << "\n"; + callee.GetName() << ". [formal count] " << callee.GetFormalCount() << ", " << + "[argument count] " << callStmt.NumOpnds() << std::endl; } if (callee.GetFormalCount() > 0 && callee.GetFormal(0)->GetName() == kThisStr) { UnaryStmtNode *nullCheck = module.CurFuncCodeMemPool()->New(OP_assertnonnull); @@ -1024,7 +1088,7 @@ bool MInline::FuncInlinable(const MIRFunction &func) const { return false; } if (func.GetAttr(FUNCATTR_abstract) || func.GetAttr(FUNCATTR_const) || func.GetAttr(FUNCATTR_declared_synchronized) || - func.GetAttr(FUNCATTR_synchronized) || func.GetAttr(FUNCATTR_weak) || + func.GetAttr(FUNCATTR_synchronized) || func.GetAttr(FUNCATTR_weak) || func.GetAttr(FUNCATTR_varargs) || ((func.GetAttr(FUNCATTR_critical_native) || func.GetAttr(FUNCATTR_fast_native) || func.GetAttr(FUNCATTR_native)) && (func.GetBody() == nullptr || func.GetBody()->GetFirst() == nullptr))) { @@ -1064,11 +1128,14 @@ void MInline::InlineCalls(const CGNode &node) { return; } bool changed = false; + constexpr int maxInlineLevel = 8; + int currInlineLevel = 0; do { changed = false; currFuncBody = nullptr; InlineCallsBlock(*func, *(func->GetBody()), *(func->GetBody()), changed); - } while (changed); + ++currInlineLevel; + } while (changed && currInlineLevel < maxInlineLevel); } void MInline::InlineCallsBlock(MIRFunction &func, BlockNode &enclosingBlk, BaseNode &baseNode, bool &changed) { @@ -1109,7 +1176,7 @@ InlineResult MInline::AnalyzeCallsite(const MIRFunction &caller, MIRFunction &ca if (callerList->empty()) { return InlineResult(false, "LIST_NOINLINE_FUNC"); } - if (callerList->find(calleeStrIdx) != callerList->end()) { + if (callerList->find(callerStrIdx) != callerList->end()) { return InlineResult(false, "LIST_NOINLINE_CALLSITE"); } } @@ -1137,6 +1204,15 @@ InlineResult MInline::AnalyzeCallsite(const MIRFunction &caller, MIRFunction &ca if (!FuncInlinable(callee)) { return InlineResult(false, "ATTR"); } + // Incompatible type conversion from arguments to formals + size_t realArgNum = std::min(callStmt.NumOpnds(), callee.GetFormalCount()); + for (size_t i = 0; i < realArgNum; ++i) { + PrimType formalPrimType = callee.GetFormal(i)->GetType()->GetPrimType(); + PrimType realArgPrimType = callStmt.Opnd(i)->GetPrimType(); + if (formalPrimType == PTY_agg ^ realArgPrimType == PTY_agg) { + return InlineResult(false, "INCOMPATIBLE_TYPE_CVT_FORM_ARG_TO_FORMAL"); + } + } if (!callee.GetLabelTab()->GetAddrTakenLabels().empty()) { return InlineResult(false, "ADDR_TAKEN_LABELS"); } @@ -1219,7 +1295,6 @@ InlineResult MInline::AnalyzeCallee(const MIRFunction &caller, MIRFunction &call // This is self recursive inline calleeBody = currFuncBody; } - if (funcToCostMap.find(&callee) != funcToCostMap.end()) { cost = funcToCostMap[&callee]; } else { @@ -1420,7 +1495,12 @@ AnalysisResult *DoInline::Run(MIRModule *module, ModuleResultMgr *mgr) { MemPool *memPool = memPoolCtrler.NewMemPool("inline mempool", false /* isLocalPool */); CallGraph *cg = static_cast(mgr->GetAnalysisResult(MoPhase_CALLGRAPH_ANALYSIS, module)); CHECK_FATAL(cg != nullptr, "Expecting a valid CallGraph, found nullptr"); - + // Reset inlining threshold for other srcLang, especially for srcLangJava. Because those methods related to + // reflection in Java cannot be inlined safely. + if (module->GetSrcLang() != kSrcLangC) { + Options::inlineSmallFunctionThreshold = 15; + Options::inlineHotFunctionThreshold = 30; + } MInline mInline(*module, memPool, cg); mInline.Inline(); mInline.CleanupInline(); diff --git a/src/mapleall/maple_ir/include/mir_symbol.h b/src/mapleall/maple_ir/include/mir_symbol.h index 8c2652799071f65022458c696169dcedef6b97c2..37a8fa4bc98c0b90411f56274e7712110c9e595f 100644 --- a/src/mapleall/maple_ir/include/mir_symbol.h +++ b/src/mapleall/maple_ir/include/mir_symbol.h @@ -530,6 +530,7 @@ class MIRSymbolTable { void Clear() { symbolTable.clear(); + strIdxToStIdxMap.clear(); } MIRSymbol *CloneLocalSymbol(const MIRSymbol &oldSym) const { diff --git a/src/mapleall/maple_ir/src/option.cpp b/src/mapleall/maple_ir/src/option.cpp index 2bdce1eb8cb2e02233d49195c96459018a64f99a..b25509a1f4d4afc80939e2af8878ab7cb9810640 100644 --- a/src/mapleall/maple_ir/src/option.cpp +++ b/src/mapleall/maple_ir/src/option.cpp @@ -36,8 +36,8 @@ bool Options::inlineWithProfile = false; bool Options::useInline = true; // Enabled by default bool Options::useCrossModuleInline = true; // Enabled by default std::string Options::noInlineFuncList = ""; -uint32 Options::inlineSmallFunctionThreshold = 15; -uint32 Options::inlineHotFunctionThreshold = 30; +uint32 Options::inlineSmallFunctionThreshold = 60; // Only for srcLangC, value will be reset later for other srcLang +uint32 Options::inlineHotFunctionThreshold = 100; // Only for srcLangC, value will be reset later for other srcLang uint32 Options::inlineRecursiveFunctionThreshold = 15; uint32 Options::inlineModuleGrowth = 10; uint32 Options::inlineColdFunctionThreshold = 3; diff --git a/src/mapleall/maple_me/BUILD.gn b/src/mapleall/maple_me/BUILD.gn index 9e6567c90f34981ae41a62a811fa187e1f31f7a2..fe6ee3432eadac45bb5610f32ba4c27f9aa9b5dd 100755 --- a/src/mapleall/maple_me/BUILD.gn +++ b/src/mapleall/maple_me/BUILD.gn @@ -91,7 +91,7 @@ src_libmplme = [ "src/me_verify.cpp", "src/me_fsaa.cpp", "src/lfo_mir_lower.cpp", - "src/lfo_inject_iv.cpp", + "src/lfo_inject_iv.cpp" ] src_libmplmewpo = [ diff --git a/src/mapleall/maple_me/include/alias_class.h b/src/mapleall/maple_me/include/alias_class.h index b8d18bbefc2586b32a79ffa7afdd8983f7799c22..a2697e5cdea57d4ebd4faf277be2b02589e3e4eb 100644 --- a/src/mapleall/maple_me/include/alias_class.h +++ b/src/mapleall/maple_me/include/alias_class.h @@ -204,27 +204,27 @@ class AliasClass : public AnalysisResult { void CollectMayUseForNextLevel(const OriginalSt *ost, std::set &mayUseOsts, const StmtNode &stmt, bool isFirstOpnd); void CollectMayUseForCallOpnd(const StmtNode &stmt, std::set &mayUseOsts); - void InsertMayDefNodeForCall(std::set &mayDefOsts, TypeOfMayDefList &mayDefNodes, + void InsertMayDefNodeForCall(std::set &mayDefOsts, AccessSSANodes *ssaPart, StmtNode &stmt, BBId bbid, bool hasNoPrivateDefEffect); void InsertMayUseExpr(BaseNode &expr); void CollectMayUseFromGlobalsAffectedByCalls(std::set &mayUseOsts); void CollectMayUseFromNADS(std::set &mayUseOsts); void CollectMayUseFromDefinedFinalField(std::set &mayUseOsts); - void InsertMayUseNode(std::set &mayUseOsts, TypeOfMayUseList &mayUseNodes); + void InsertMayUseNode(std::set &mayUseOsts, AccessSSANodes *ssaPart); void InsertMayUseReturn(const StmtNode &stmt); void CollectPtsToOfReturnOpnd(const OriginalSt &ost, std::set &mayUseOsts); void InsertReturnOpndMayUse(const StmtNode &stmt); void InsertMayUseAll(const StmtNode &stmt); void CollectMayDefForDassign(const StmtNode &stmt, std::set &mayDefOsts); - void InsertMayDefNode(std::set &mayDefOsts, TypeOfMayDefList &mayDefNodes, StmtNode &stmt, BBId bbid); + void InsertMayDefNode(std::set &mayDefOsts, AccessSSANodes *ssaPart, StmtNode &stmt, BBId bbid); void InsertMayDefDassign(StmtNode &stmt, BBId bbid); bool IsEquivalentField(TyIdx tyIdxA, FieldID fldA, TyIdx tyIdxB, FieldID fldB) const; void CollectMayDefForIassign(StmtNode &stmt, std::set &mayDefOsts); - void InsertMayDefNodeExcludeFinalOst(std::set &mayDefOsts, TypeOfMayDefList &mayDefNodes, + void InsertMayDefNodeExcludeFinalOst(std::set &mayDefOsts, AccessSSANodes *ssaPart, StmtNode &stmt, BBId bbid); void InsertMayDefIassign(StmtNode &stmt, BBId bbid); void InsertMayDefUseSyncOps(StmtNode &stmt, BBId bbid); - void InsertMayUseNodeExcludeFinalOst(const std::set &mayUseOsts, TypeOfMayUseList &mayUseNodes); + void InsertMayUseNodeExcludeFinalOst(const std::set &mayUseOsts, AccessSSANodes *ssaPart); void InsertMayDefUseIntrncall(StmtNode &stmt, BBId bbid); void InsertMayDefUseClinitCheck(IntrinsiccallNode &stmt, BBId bbid); virtual BB *GetBB(BBId id) = 0; diff --git a/src/mapleall/maple_me/include/lfo_function.h b/src/mapleall/maple_me/include/lfo_function.h index 5a74f9ff37b19ee025a3f46e859ef7e176a5d185..013414012954748760f97fb37121c007dc390ef0 100644 --- a/src/mapleall/maple_me/include/lfo_function.h +++ b/src/mapleall/maple_me/include/lfo_function.h @@ -19,7 +19,6 @@ #include "me_ir.h" namespace maple { - class MeFunction; class LfoWhileInfo { @@ -38,33 +37,34 @@ class LfoIfInfo { LabelIdx elseLabel = 0; // the label that is the begin of else branch }; - class LfoFunction { public: MemPool *lfomp; MapleAllocator lfoAlloc; MeFunction *meFunc; - MapleMap label2WhileInfo; // key is label at beginning of lowered while code sequence - MapleMap label2IfInfo; // key is target label of first conditional branch of lowered if code sequence - MapleSet lfoCreatedLabelSet; // for the labels that were created by lfo, we won't emit it + // key is label at beginning of lowered while code sequence + MapleMap label2WhileInfo; + // key is target label of first conditional branch of lowered if code sequence + MapleMap label2IfInfo; + // for the labels that were created by lfo, we won't emit it + MapleSet lfoCreatedLabelSet; public: - LfoFunction(MemPool *mp, MeFunction *func) : - lfomp(mp), - lfoAlloc(mp), - meFunc(func), - label2WhileInfo(lfoAlloc.Adapter()), - label2IfInfo(lfoAlloc.Adapter()), - lfoCreatedLabelSet(lfoAlloc.Adapter()) {} + LfoFunction(MemPool *mp, MeFunction *func) + : lfomp(mp), + lfoAlloc(mp), + meFunc(func), + label2WhileInfo(lfoAlloc.Adapter()), + label2IfInfo(lfoAlloc.Adapter()), + lfoCreatedLabelSet(lfoAlloc.Adapter()) {} void SetLabelCreatedByLfo(LabelIdx lbidx) { lfoCreatedLabelSet.insert(lbidx); } bool LabelCreatedByLfo(LabelIdx lbidx) { - return lfoCreatedLabelSet.count(lbidx) != 0; + return lfoCreatedLabelSet.count(lbidx) != 0; } }; - } // namespace maple #endif // MAPLE_LFO_INCLUDE_LFO_FUNCTION_H diff --git a/src/mapleall/maple_me/include/lfo_inject_iv.h b/src/mapleall/maple_me/include/lfo_inject_iv.h index 1181f2b6f20fd052ca18c310b5939bc6e8f6deb4..d2a5ab7b825b281925cb284d688fd1cf60198e00 100644 --- a/src/mapleall/maple_me/include/lfo_inject_iv.h +++ b/src/mapleall/maple_me/include/lfo_inject_iv.h @@ -18,8 +18,7 @@ #include "me_phase.h" namespace maple { - -/*emit ir to specified file*/ +/* emit ir to specified file */ class DoLfoInjectIV : public MeFuncPhase { public: DoLfoInjectIV(MePhaseID id) : MeFuncPhase(id) {} @@ -27,6 +26,5 @@ class DoLfoInjectIV : public MeFuncPhase { AnalysisResult *Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) override; std::string PhaseName() const override { return "injectiv"; } }; - } // namespace maple #endif // MAPLE_ME_INCLUDE_LFO_INJECT_IV_H diff --git a/src/mapleall/maple_me/include/lfo_mir_lower.h b/src/mapleall/maple_me/include/lfo_mir_lower.h index cb3aa490db63c9d41c860372fbc54d5d32f90908..b346edb063092ba78d9f792387469295c99b819c 100644 --- a/src/mapleall/maple_me/include/lfo_mir_lower.h +++ b/src/mapleall/maple_me/include/lfo_mir_lower.h @@ -24,7 +24,8 @@ class LFOMIRLower : public MIRLower { LfoFunction *lfoFunc; public: - LFOMIRLower(MIRModule &mod, MeFunction *f) : MIRLower(mod, f->GetMirFunc()), + LFOMIRLower(MIRModule &mod, MeFunction *f) + : MIRLower(mod, f->GetMirFunc()), func(f), lfoFunc(f->GetLfoFunc()) {} diff --git a/src/mapleall/maple_me/include/lfo_mir_nodes.h b/src/mapleall/maple_me/include/lfo_mir_nodes.h index fe408391352d1a824148b2548fb65f35f8fa4e16..2d497bc3c068e244c393b44228de2994cb2078c4 100644 --- a/src/mapleall/maple_me/include/lfo_mir_nodes.h +++ b/src/mapleall/maple_me/include/lfo_mir_nodes.h @@ -19,7 +19,6 @@ #include "mir_nodes.h" namespace maple { - class LfoParentPart { public: LfoParentPart *parent; @@ -29,8 +28,9 @@ class LfoParentPart { virtual BaseNode *Cvt2BaseNode() = 0; bool IsParentOf(LfoParentPart *canNode) { LfoParentPart *dParent = canNode->parent; - while(dParent && dParent != this) - dParent = dParent->parent; + while (dParent && dParent != this) { + dParent = dParent->parent; + } return dParent != NULL; } }; @@ -43,21 +43,25 @@ class LfoUnaryNode : public UnaryNode, public LfoParentPart{ class LfoTypeCvtNode : public TypeCvtNode, public LfoParentPart { public: - LfoTypeCvtNode(Opcode o, PrimType ptyp, LfoParentPart *parent) : - TypeCvtNode(o, ptyp), LfoParentPart(parent) {} + LfoTypeCvtNode(Opcode o, PrimType ptyp, LfoParentPart *parent) + : TypeCvtNode(o, ptyp), + LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoRetypeNode : public RetypeNode, public LfoParentPart { public: - LfoRetypeNode(Opcode o, PrimType ptyp, LfoParentPart *parent) : - RetypeNode(ptyp), LfoParentPart(parent) {} + LfoRetypeNode(Opcode o, PrimType ptyp, LfoParentPart *parent) + : RetypeNode(ptyp), LfoParentPart(parent) { + (void)o; + } BaseNode *Cvt2BaseNode() { return this; } }; class LfoExtractbitsNode : public ExtractbitsNode, public LfoParentPart { public: - LfoExtractbitsNode(Opcode o, PrimType ptyp, LfoParentPart *parent) : ExtractbitsNode(o, ptyp), LfoParentPart(parent) {} + LfoExtractbitsNode(Opcode o, PrimType ptyp, LfoParentPart *parent) + : ExtractbitsNode(o, ptyp), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -66,55 +70,58 @@ class LfoIreadNode : public IreadNode, public LfoParentPart { IvarMeExpr *ivarx; public: - LfoIreadNode(Opcode o, PrimType ptyp, LfoParentPart *parent, IvarMeExpr *v) : - IreadNode(o, ptyp), LfoParentPart(parent), ivarx(v) {} + LfoIreadNode(Opcode o, PrimType ptyp, LfoParentPart *parent, IvarMeExpr *v) + : IreadNode(o, ptyp), LfoParentPart(parent), ivarx(v) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoIaddrofNode : public IreadNode, public LfoParentPart { public: - LfoIaddrofNode(Opcode o, PrimType pty, LfoParentPart *parent) : IreadNode(o, pty), LfoParentPart(parent) {} + LfoIaddrofNode(Opcode o, PrimType pty, LfoParentPart *parent) : IreadNode(o, pty), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoBinaryNode : public BinaryNode, public LfoParentPart { public: - LfoBinaryNode (Opcode o, PrimType typ, LfoParentPart *parent) : BinaryNode (o,typ), - LfoParentPart (parent) {} + LfoBinaryNode (Opcode o, PrimType typ, LfoParentPart *parent) : BinaryNode(o, typ), LfoParentPart (parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoCompareNode : public CompareNode, public LfoParentPart { public: - LfoCompareNode (Opcode o, PrimType typ, PrimType otype, BaseNode *l, BaseNode *r, LfoParentPart *parent) : - CompareNode (o, typ, otype, l, r), LfoParentPart (parent) {} + LfoCompareNode (Opcode o, PrimType typ, PrimType otype, BaseNode *l, BaseNode *r, LfoParentPart *parent) + : CompareNode (o, typ, otype, l, r), LfoParentPart (parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoTernaryNode : public TernaryNode, public LfoParentPart { public: - LfoTernaryNode (Opcode o, PrimType ptyp, LfoParentPart *parent) : TernaryNode(o, ptyp), - LfoParentPart(parent) {} + LfoTernaryNode (Opcode o, PrimType ptyp, LfoParentPart *parent) + : TernaryNode(o, ptyp), + LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoNaryNode : public NaryNode, public LfoParentPart { public: - LfoNaryNode (MapleAllocator *allc, Opcode o, PrimType pty, LfoParentPart *parent) : NaryNode (*allc, o, pty), - LfoParentPart(parent) {} + LfoNaryNode (MapleAllocator *allc, Opcode o, PrimType pty, LfoParentPart *parent) + : NaryNode (*allc, o, pty), + LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoIntrinsicopNode : public IntrinsicopNode, public LfoParentPart { public: - LfoIntrinsicopNode (MapleAllocator *allc, Opcode o, PrimType ptyp, TyIdx tidx, LfoParentPart *parent) : - IntrinsicopNode(*allc, o, ptyp, tidx), LfoParentPart(parent) {} + LfoIntrinsicopNode (MapleAllocator *allc, Opcode o, PrimType ptyp, TyIdx tidx, LfoParentPart *parent) + : IntrinsicopNode(*allc, o, ptyp, tidx), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoConstvalNode : public ConstvalNode, public LfoParentPart { public: - LfoConstvalNode(MIRConst *constv, LfoParentPart *parent) : ConstvalNode(constv->GetType().GetPrimType(), constv), LfoParentPart(parent) {} + LfoConstvalNode(MIRConst *constv, LfoParentPart *parent) + : ConstvalNode(constv->GetType().GetPrimType(), constv), + LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -126,19 +133,22 @@ class LfoConststrNode : public ConststrNode, public LfoParentPart { class LfoConststr16Node : public Conststr16Node, public LfoParentPart { public: - LfoConststr16Node(PrimType ptyp, U16StrIdx i, LfoParentPart *parent) : Conststr16Node(ptyp, i), LfoParentPart(parent) {} + LfoConststr16Node(PrimType ptyp, U16StrIdx i, LfoParentPart *parent) + : Conststr16Node(ptyp, i), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoSizeoftypeNode : public SizeoftypeNode, public LfoParentPart { public: - LfoSizeoftypeNode(PrimType ptyp, TyIdx tidx, LfoParentPart *parent) : SizeoftypeNode(ptyp, tidx), LfoParentPart(parent) {} + LfoSizeoftypeNode(PrimType ptyp, TyIdx tidx, LfoParentPart *parent) + : SizeoftypeNode(ptyp, tidx), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoArrayNode : public ArrayNode, public LfoParentPart { public: - LfoArrayNode(MapleAllocator *allc, PrimType typ, TyIdx idx, LfoParentPart *parent) : ArrayNode (*allc, typ, idx), LfoParentPart (parent) {} + LfoArrayNode(MapleAllocator *allc, PrimType typ, TyIdx idx, LfoParentPart *parent) + : ArrayNode (*allc, typ, idx), LfoParentPart (parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -147,14 +157,15 @@ class LfoDreadNode : public AddrofNode, public LfoParentPart { VarMeExpr *varx; public: - LfoDreadNode(PrimType ptyp, StIdx sidx, FieldID fid, LfoParentPart *parent, VarMeExpr *v) : - AddrofNode(OP_dread, ptyp, sidx, fid), LfoParentPart(parent), varx(v) {} + LfoDreadNode(PrimType ptyp, StIdx sidx, FieldID fid, LfoParentPart *parent, VarMeExpr *v) + : AddrofNode(OP_dread, ptyp, sidx, fid), LfoParentPart(parent), varx(v) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoAddrofNode : public AddrofNode, public LfoParentPart { public: - LfoAddrofNode(PrimType ptyp, StIdx sidx, FieldID fid, LfoParentPart *parent) : AddrofNode(OP_addrof, ptyp, sidx, fid), LfoParentPart(parent) {} + LfoAddrofNode(PrimType ptyp, StIdx sidx, FieldID fid, LfoParentPart *parent) + : AddrofNode(OP_addrof, ptyp, sidx, fid), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -169,7 +180,8 @@ class LfoRegreadNode : public RegreadNode, public LfoParentPart { class LfoAddroffuncNode : public AddroffuncNode, public LfoParentPart { public: - LfoAddroffuncNode(PrimType ptyp, PUIdx pidx, LfoParentPart *parent) : AddroffuncNode(ptyp, pidx), LfoParentPart(parent) {} + LfoAddroffuncNode(PrimType ptyp, PUIdx pidx, LfoParentPart *parent) + : AddroffuncNode(ptyp, pidx), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -181,14 +193,15 @@ class LfoAddroflabelNode : public AddroflabelNode, public LfoParentPart { class LfoGCMallocNode : public GCMallocNode, public LfoParentPart { public: - LfoGCMallocNode(Opcode o, PrimType pty, TyIdx tidx, LfoParentPart *parent) : GCMallocNode(o, pty, tidx), LfoParentPart(parent) {} + LfoGCMallocNode(Opcode o, PrimType pty, TyIdx tidx, LfoParentPart *parent) + : GCMallocNode(o, pty, tidx), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoFieldsDistNode : public FieldsDistNode, public LfoParentPart { public: - LfoFieldsDistNode(PrimType ptyp, TyIdx tidx, FieldID f1, FieldID f2, LfoParentPart *parent) : - FieldsDistNode(ptyp, tidx, f1, f2), LfoParentPart(parent) {} + LfoFieldsDistNode(PrimType ptyp, TyIdx tidx, FieldID f1, FieldID f2, LfoParentPart *parent) + : FieldsDistNode(ptyp, tidx, f1, f2), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -241,7 +254,8 @@ class LfoDoloopNode : public DoloopNode, public LfoParentPart { public: LfoDoloopNode (LfoParentPart *parent) : DoloopNode (), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } - void InitLfoDoloopNode (StIdx stIdx, bool ispg, BaseNode *startExp, BaseNode *contExp, BaseNode *incrExp, BlockNode *blk) { + void InitLfoDoloopNode (StIdx stIdx, bool ispg, BaseNode *startExp, BaseNode *contExp, + BaseNode *incrExp, BlockNode *blk) { SetDoVarStIdx(stIdx); SetIsPreg(ispg); SetStartExpr(startExp); @@ -253,8 +267,8 @@ class LfoDoloopNode : public DoloopNode, public LfoParentPart { class LfoNaryStmtNode : public NaryStmtNode, public LfoParentPart { public: - LfoNaryStmtNode (MapleAllocator *allc, Opcode o, LfoParentPart *parent) : - NaryStmtNode(*allc, o), LfoParentPart(parent) {} + LfoNaryStmtNode (MapleAllocator *allc, Opcode o, LfoParentPart *parent) + : NaryStmtNode(*allc, o), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -263,8 +277,8 @@ class LfoReturnStmtNode : public NaryStmtNode, public LfoParentPart { RetMeStmt *retmestmt; public: - LfoReturnStmtNode(MapleAllocator *allc, LfoParentPart *parent, RetMeStmt *ret) : - NaryStmtNode(*allc, OP_return), LfoParentPart(parent), retmestmt(ret) {} + LfoReturnStmtNode(MapleAllocator *allc, LfoParentPart *parent, RetMeStmt *ret) + : NaryStmtNode(*allc, OP_return), LfoParentPart(parent), retmestmt(ret) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -273,7 +287,8 @@ class LfoCallNode : public CallNode, public LfoParentPart { CallMeStmt *callmestmt; public: - LfoCallNode(MapleAllocator *allc, Opcode o, LfoParentPart *parent, CallMeStmt *cl) : CallNode(*allc, o), LfoParentPart(parent), callmestmt(cl) {} + LfoCallNode(MapleAllocator *allc, Opcode o, LfoParentPart *parent, CallMeStmt *cl) + : CallNode(*allc, o), LfoParentPart(parent), callmestmt(cl) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -282,8 +297,8 @@ class LfoIcallNode : public IcallNode, public LfoParentPart { IcallMeStmt *icallmestmt; public: - LfoIcallNode (MapleAllocator *allc, Opcode o, TyIdx idx, LfoParentPart *parent, IcallMeStmt *ic) : - IcallNode (*allc, o, idx), LfoParentPart(parent), icallmestmt(ic) {} + LfoIcallNode (MapleAllocator *allc, Opcode o, TyIdx idx, LfoParentPart *parent, IcallMeStmt *ic) + : IcallNode (*allc, o, idx), LfoParentPart(parent), icallmestmt(ic) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -292,27 +307,27 @@ class LfoIntrinsiccallNode : public IntrinsiccallNode, public LfoParentPart { IntrinsiccallMeStmt *intrinmestmt; public: - LfoIntrinsiccallNode (MapleAllocator *allc, Opcode o, MIRIntrinsicID id, LfoParentPart *parent, IntrinsiccallMeStmt *intncall) : - IntrinsiccallNode(*allc, o, id), LfoParentPart(parent), intrinmestmt(intncall) {} - BaseNode *Cvt2BaseNode () { return this; } + LfoIntrinsiccallNode (MapleAllocator *allc, Opcode o, MIRIntrinsicID id, + LfoParentPart *parent, IntrinsiccallMeStmt *intncall) + : IntrinsiccallNode(*allc, o, id), LfoParentPart(parent), intrinmestmt(intncall) {} + BaseNode *Cvt2BaseNode () { return this; } }; class LfoIfStmtNode : public IfStmtNode, public LfoParentPart { public: - LfoIfStmtNode(LfoParentPart *parent) : IfStmtNode(),LfoParentPart(parent) {} + LfoIfStmtNode(LfoParentPart *parent) : IfStmtNode(), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoBlockNode : public BlockNode, public LfoParentPart { public: - LfoBlockNode (LfoParentPart *parent) : BlockNode(),LfoParentPart(parent) {} + LfoBlockNode (LfoParentPart *parent) : BlockNode(), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; class LfoStmtNode : public StmtNode, public LfoParentPart { public: - LfoStmtNode (LfoParentPart *parent, Opcode o) : StmtNode (o), - LfoParentPart(parent) {} + LfoStmtNode (LfoParentPart *parent, Opcode o) : StmtNode (o), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; @@ -326,10 +341,9 @@ class LfoUnaryStmtNode : public UnaryStmtNode, public LfoParentPart { class LfoSwitchNode : public SwitchNode, public LfoParentPart { public: - LfoSwitchNode(MapleAllocator *allc, LfoParentPart *parent) : - SwitchNode(*allc), LfoParentPart(parent) {} + LfoSwitchNode(MapleAllocator *allc, LfoParentPart *parent) + : SwitchNode(*allc), LfoParentPart(parent) {} BaseNode *Cvt2BaseNode() { return this; } }; - } // namespace maple #endif // MAPLE_LFO_INCLUDE_LFO_MIR_NODES_H_ diff --git a/src/mapleall/maple_me/include/me_function.h b/src/mapleall/maple_me/include/me_function.h index b64e32ce0217b9825396be64d8926dd4ff2f7e8d..4064ebf5a12e2d87dc08f11a9afdb432e9f519e7 100644 --- a/src/mapleall/maple_me/include/me_function.h +++ b/src/mapleall/maple_me/include/me_function.h @@ -158,12 +158,12 @@ class MeFunction : public FuncEmit { mirFunc(func), laidOutBBVec(alloc.Adapter()), fileName(fileName, memPool), - lfoFunc(nullptr), lfoMp(nullptr) { + lfoFunc(nullptr), + lfoMp(nullptr) { isLfo = (MeOption::optLevel == 3); } ~MeFunction() override = default; - void DumpFunction() const; void DumpFunctionNoSSA() const; void DumpMayDUFunction() const; diff --git a/src/mapleall/maple_me/include/ssa_mir_nodes.h b/src/mapleall/maple_me/include/ssa_mir_nodes.h index 4d0926b0f6cd46b66fe85d102574c7b853f9cd62..e9cfdf858c1a8457724df57de738e0ce36015c35 100644 --- a/src/mapleall/maple_me/include/ssa_mir_nodes.h +++ b/src/mapleall/maple_me/include/ssa_mir_nodes.h @@ -62,6 +62,10 @@ class MayDefNode { LogInfo::MapleLogger() << ")\n"; } + bool operator==(const MayDefNode &other) const { + return opnd == other.opnd && result == other.result && stmt == other.stmt; + } + VersionSt *base = nullptr; // only provided if indirectLev is 1 and attached to iassign private: VersionSt *opnd; @@ -89,6 +93,10 @@ class MayUseNode { LogInfo::MapleLogger() << ")"; } + bool operator==(const MayUseNode &other) const { + return opnd == other.opnd; + } + private: VersionSt *opnd; }; @@ -191,9 +199,24 @@ class AccessSSANodes { } } - virtual void InsertMayDefNode(VersionSt *vst, StmtNode *stmtNode) { - CHECK_FATAL(vst != nullptr, "null ptr check"); - GetMayDefNodes().emplace_back(MayDefNode(vst, stmtNode)); + inline void InsertMayDefNode(VersionSt *vst, StmtNode *stmtNode) { + ASSERT(vst != nullptr, "null ptr check"); + auto &mayDefNodes = GetMayDefNodes(); + MayDefNode mayDefNode(vst, stmtNode); + if (std::find(mayDefNodes.begin(), mayDefNodes.end(), mayDefNode) != mayDefNodes.end()) { + return; + } + mayDefNodes.emplace_back(mayDefNode); + } + + inline void InsertMayUseNode(VersionSt *vst) { + ASSERT(vst != nullptr, "null ptr check"); + auto &mayUseNodes = GetMayUseNodes(); + MayUseNode mayUseNode(vst); + if (std::find(mayUseNodes.begin(), mayUseNodes.end(), mayUseNode) != mayUseNodes.end()) { + return; + } + mayUseNodes.emplace_back(mayUseNode); } virtual void InsertMustDefNode(VersionSt *sym, StmtNode *s) { diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index b169001e7acc84291a89c6009fa36f0f877e937c..7561a53dfddef64a7f7e00dcdcb0dd45e75d3510 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -1309,10 +1309,10 @@ void AliasClass::CollectMayUseFromDefinedFinalField(std::set &mayUs } // insert the ost of mayUseOsts into mayUseNodes -void AliasClass::InsertMayUseNode(std::set &mayUseOsts, TypeOfMayUseList &mayUseNodes) { +void AliasClass::InsertMayUseNode(std::set &mayUseOsts, AccessSSANodes *ssaPart) { for (OriginalSt *ost : mayUseOsts) { - mayUseNodes.emplace_back( - MayUseNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(ost->GetZeroVersionIndex()))); + ssaPart->InsertMayUseNode( + ssaTab.GetVersionStTable().GetVersionStVectorItem(ost->GetZeroVersionIndex())); } } @@ -1331,8 +1331,8 @@ void AliasClass::InsertMayUseReturn(const StmtNode &stmt) { if (mirModule.CurFunction()->IsConstructor()) { CollectMayUseFromDefinedFinalField(mayUseOsts); } - TypeOfMayUseList &mayUseNodes = ssaTab.GetStmtsSSAPart().GetMayUseNodesOf(stmt); - InsertMayUseNode(mayUseOsts, mayUseNodes); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); + InsertMayUseNode(mayUseOsts, ssaPart); } // collect next_level_nodes of the ost of ReturnOpnd into mayUseOsts @@ -1371,8 +1371,8 @@ void AliasClass::InsertReturnOpndMayUse(const StmtNode &stmt) { } } // insert mayUses - TypeOfMayUseList &mayUseNodes = ssaTab.GetStmtsSSAPart().GetMayUseNodesOf(stmt); - InsertMayUseNode(mayUseOsts, mayUseNodes); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); + InsertMayUseNode(mayUseOsts, ssaPart); } } } @@ -1441,11 +1441,11 @@ void AliasClass::CollectMayDefForDassign(const StmtNode &stmt, std::set &mayDefOsts, TypeOfMayDefList &mayDefNodes, +void AliasClass::InsertMayDefNode(std::set &mayDefOsts, AccessSSANodes *ssaPart, StmtNode &stmt, BBId bbid) { for (OriginalSt *mayDefOst : mayDefOsts) { - mayDefNodes.emplace_back( - MayDefNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt)); + ssaPart->InsertMayDefNode( + ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt); ssaTab.AddDefBB4Ost(mayDefOst->GetIndex(), bbid); } } @@ -1453,8 +1453,7 @@ void AliasClass::InsertMayDefNode(std::set &mayDefOsts, TypeOfMayDe void AliasClass::InsertMayDefDassign(StmtNode &stmt, BBId bbid) { std::set mayDefOsts; CollectMayDefForDassign(stmt, mayDefOsts); - TypeOfMayDefList &mayDefNodes = ssaTab.GetStmtsSSAPart().GetMayDefNodesOf(stmt); - InsertMayDefNode(mayDefOsts, mayDefNodes, stmt, bbid); + InsertMayDefNode(mayDefOsts, ssaTab.GetStmtsSSAPart().SSAPartOf(stmt), stmt, bbid); } bool AliasClass::IsEquivalentField(TyIdx tyIdxA, FieldID fldA, TyIdx tyIdxB, FieldID fldB) const { @@ -1542,12 +1541,12 @@ void AliasClass::CollectMayDefForIassign(StmtNode &stmt, std::set & } void AliasClass::InsertMayDefNodeExcludeFinalOst(std::set &mayDefOsts, - TypeOfMayDefList &mayDefNodes, StmtNode &stmt, + AccessSSANodes *ssapPart, StmtNode &stmt, BBId bbid) { for (OriginalSt *mayDefOst : mayDefOsts) { if (!mayDefOst->IsFinal()) { - mayDefNodes.emplace_back( - MayDefNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt)); + ssapPart->InsertMayDefNode( + ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt); ssaTab.AddDefBB4Ost(mayDefOst->GetIndex(), bbid); } } @@ -1556,12 +1555,14 @@ void AliasClass::InsertMayDefNodeExcludeFinalOst(std::set &mayDefOs void AliasClass::InsertMayDefIassign(StmtNode &stmt, BBId bbid) { std::set mayDefOsts; CollectMayDefForIassign(stmt, mayDefOsts); - TypeOfMayDefList &mayDefNodes = ssaTab.GetStmtsSSAPart().GetMayDefNodesOf(stmt); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); if (mayDefOsts.size() == 1) { - InsertMayDefNode(mayDefOsts, mayDefNodes, stmt, bbid); + InsertMayDefNode(mayDefOsts, ssaPart, stmt, bbid); } else { - InsertMayDefNodeExcludeFinalOst(mayDefOsts, mayDefNodes, stmt, bbid); + InsertMayDefNodeExcludeFinalOst(mayDefOsts, ssaPart, stmt, bbid); } + + TypeOfMayDefList &mayDefNodes = ssaTab.GetStmtsSSAPart().GetMayDefNodesOf(stmt); ASSERT(!mayDefNodes.empty(), "AliasClass::InsertMayUseIassign(): iassign cannot have empty maydef"); // go thru inserted MayDefNode to add the base info TypeOfMayDefList::iterator it = mayDefNodes.begin(); @@ -1717,13 +1718,13 @@ void AliasClass::CollectMayUseForCallOpnd(const StmtNode &stmt, std::set &mayDefOsts, TypeOfMayDefList &mayDefNodes, +void AliasClass::InsertMayDefNodeForCall(std::set &mayDefOsts, AccessSSANodes *ssaPart, StmtNode &stmt, BBId bbid, bool hasNoPrivateDefEffect) { for (OriginalSt *mayDefOst : mayDefOsts) { if (!hasNoPrivateDefEffect || !mayDefOst->IsPrivate()) { - mayDefNodes.emplace_back( - MayDefNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt)); + ssaPart->InsertMayDefNode( + ssaTab.GetVersionStTable().GetVersionStVectorItem(mayDefOst->GetZeroVersionIndex()), &stmt); ssaTab.AddDefBB4Ost(mayDefOst->GetIndex(), bbid); } } @@ -1733,39 +1734,39 @@ void AliasClass::InsertMayDefNodeForCall(std::set &mayDefOsts, Type // Four kinds of mayDefs and mayUses are inserted, which are caused by callee // opnds, not_all_def_seen_ae, globalsAffectedByCalls, and mustDefs. void AliasClass::InsertMayDefUseCall(StmtNode &stmt, BBId bbid, bool hasSideEffect, bool hasNoPrivateDefEffect) { - auto *theSSAPart = static_cast(ssaTab.GetStmtsSSAPart().SSAPartOf(stmt)); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); std::set mayDefUseOstsA; // 1. collect mayDefs and mayUses caused by callee-opnds CollectMayUseForCallOpnd(stmt, mayDefUseOstsA); // 2. collect mayDefs and mayUses caused by not_all_def_seen_ae CollectMayUseFromNADS(mayDefUseOstsA); - InsertMayUseNode(mayDefUseOstsA, theSSAPart->GetMayUseNodes()); + InsertMayUseNode(mayDefUseOstsA, ssaPart); // insert may def node, if the callee has side-effect. if (hasSideEffect) { - InsertMayDefNodeForCall(mayDefUseOstsA, theSSAPart->GetMayDefNodes(), stmt, bbid, hasNoPrivateDefEffect); + InsertMayDefNodeForCall(mayDefUseOstsA, ssaPart, stmt, bbid, hasNoPrivateDefEffect); } // 3. insert mayDefs and mayUses caused by globalsAffectedByCalls std::set mayDefUseOstsB; CollectMayUseFromGlobalsAffectedByCalls(mayDefUseOstsB); - InsertMayUseNode(mayDefUseOstsB, theSSAPart->GetMayUseNodes()); + InsertMayUseNode(mayDefUseOstsB, ssaPart); // insert may def node, if the callee has side-effect. if (hasSideEffect) { - InsertMayDefNodeExcludeFinalOst(mayDefUseOstsB, theSSAPart->GetMayDefNodes(), stmt, bbid); + InsertMayDefNodeExcludeFinalOst(mayDefUseOstsB, ssaPart, stmt, bbid); if (kOpcodeInfo.IsCallAssigned(stmt.GetOpCode())) { // 4. insert mayDefs caused by the mustDefs std::set mayDefOstsC; CollectMayDefForMustDefs(stmt, mayDefOstsC); - InsertMayDefNodeExcludeFinalOst(mayDefOstsC, theSSAPart->GetMayDefNodes(), stmt, bbid); + InsertMayDefNodeExcludeFinalOst(mayDefOstsC, ssaPart, stmt, bbid); } } } void AliasClass::InsertMayUseNodeExcludeFinalOst(const std::set &mayUseOsts, - TypeOfMayUseList &mayUseNodes) { + AccessSSANodes *ssaPart) { for (OriginalSt *mayUseOst : mayUseOsts) { if (!mayUseOst->IsFinal()) { - mayUseNodes.emplace_back( - MayUseNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(mayUseOst->GetZeroVersionIndex()))); + ssaPart->InsertMayUseNode( + ssaTab.GetVersionStTable().GetVersionStVectorItem(mayUseOst->GetZeroVersionIndex())); } } } @@ -1774,7 +1775,7 @@ void AliasClass::InsertMayUseNodeExcludeFinalOst(const std::set &ma // Four kinds of mayDefs and mayUses are inserted, which are caused by callee // opnds, not_all_def_seen_ae, globalsAffectedByCalls, and mustDefs. void AliasClass::InsertMayDefUseIntrncall(StmtNode &stmt, BBId bbid) { - auto *theSSAPart = static_cast(ssaTab.GetStmtsSSAPart().SSAPartOf(stmt)); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); auto &intrinNode = static_cast(stmt); IntrinDesc *intrinDesc = &IntrinDesc::intrinTable[intrinNode.GetIntrinsic()]; std::set mayDefUseOsts; @@ -1790,28 +1791,28 @@ void AliasClass::InsertMayDefUseIntrncall(StmtNode &stmt, BBId bbid) { CollectMayUseFromNADS(mayDefUseOsts); // 3. collect mayDefs and mayUses caused by globalsAffectedByCalls CollectMayUseFromGlobalsAffectedByCalls(mayDefUseOsts); - InsertMayUseNodeExcludeFinalOst(mayDefUseOsts, theSSAPart->GetMayUseNodes()); + InsertMayUseNodeExcludeFinalOst(mayDefUseOsts, ssaPart); if (!intrinDesc->HasNoSideEffect() || calleeHasSideEffect) { - InsertMayDefNodeExcludeFinalOst(mayDefUseOsts, theSSAPart->GetMayDefNodes(), stmt, bbid); + InsertMayDefNodeExcludeFinalOst(mayDefUseOsts, ssaPart, stmt, bbid); } if (kOpcodeInfo.IsCallAssigned(stmt.GetOpCode())) { // 4. insert maydefs caused by the mustdefs std::set mayDefOsts; CollectMayDefForMustDefs(stmt, mayDefOsts); - InsertMayDefNodeExcludeFinalOst(mayDefOsts, theSSAPart->GetMayDefNodes(), stmt, bbid); + InsertMayDefNodeExcludeFinalOst(mayDefOsts, ssaPart, stmt, bbid); } } void AliasClass::InsertMayDefUseClinitCheck(IntrinsiccallNode &stmt, BBId bbid) { - TypeOfMayDefList &mayDefNodes = ssaTab.GetStmtsSSAPart().GetMayDefNodesOf(stmt); + auto *ssaPart = ssaTab.GetStmtsSSAPart().SSAPartOf(stmt); for (OStIdx ostIdx : globalsMayAffectedByClinitCheck) { AliasElem *aliasElem = osym2Elem[ostIdx]; OriginalSt &ostOfAE = aliasElem->GetOriginalSt(); std::string typeNameOfOst = ostOfAE.GetMIRSymbol()->GetName(); std::string typeNameOfStmt = GlobalTables::GetTypeTable().GetTypeFromTyIdx(stmt.GetTyIdx())->GetName(); if (typeNameOfOst.find(typeNameOfStmt) != std::string::npos) { - mayDefNodes.emplace_back( - MayDefNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(ostOfAE.GetZeroVersionIndex()), &stmt)); + ssaPart->InsertMayDefNode(ssaTab.GetVersionStTable().GetVersionStVectorItem(ostOfAE.GetZeroVersionIndex()), + &stmt); ssaTab.AddDefBB4Ost(ostOfAE.GetIndex(), bbid); } } diff --git a/src/mapleall/maple_me/src/lfo_inject_iv.cpp b/src/mapleall/maple_me/src/lfo_inject_iv.cpp index ffbdc5825ea6eaf6a48d27f60a5e70c4e2bf8524..3e0fc544b5371c51f9ebe4391425b8de666eb11d 100644 --- a/src/mapleall/maple_me/src/lfo_inject_iv.cpp +++ b/src/mapleall/maple_me/src/lfo_inject_iv.cpp @@ -21,7 +21,6 @@ #include namespace maple { - AnalysisResult *DoLfoInjectIV::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { Dominance *dom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); CHECK_FATAL(dom, "dominance phase has problem"); @@ -48,7 +47,7 @@ AnalysisResult *DoLfoInjectIV::Run(MeFunction *func, MeFuncResultMgr *m, ModuleR LfoWhileInfo *whileInfo = it->second; // find the entry BB as the predecessor of headbb that dominates headbb MapleVector::iterator predit = headbb->GetPred().begin(); - for ( ; predit != headbb->GetPred().end(); predit++) { + for (; predit != headbb->GetPred().end(); predit++) { if (dom->Dominate(**predit, *headbb)) break; } @@ -70,9 +69,10 @@ AnalysisResult *DoLfoInjectIV::Run(MeFunction *func, MeFuncResultMgr *m, ModuleR // initialize IV to 0 at loop entry DassignNode *dass = mirbuilder->CreateStmtDassign(st->GetStIdx(), 0, mirbuilder->CreateIntConst(0, PTY_i64)); - StmtNode *laststmt = entrybb->IsEmpty() ? NULL : &entrybb->GetLast(); + StmtNode *laststmt = &entrybb->GetLast(); if (laststmt && - (laststmt->op == OP_brfalse || laststmt->op == OP_brtrue || laststmt->op == OP_goto || laststmt->op == OP_igoto || laststmt->op == OP_switch)) { + (laststmt->op == OP_brfalse || laststmt->op == OP_brtrue || laststmt->op == OP_goto || + laststmt->op == OP_igoto || laststmt->op == OP_switch)) { entrybb->InsertStmtBefore(laststmt, dass); } else { entrybb->AddStmtNode(dass); @@ -81,12 +81,12 @@ AnalysisResult *DoLfoInjectIV::Run(MeFunction *func, MeFuncResultMgr *m, ModuleR // insert IV increment at loop tail BB BB *tailbb = aloop->tail; AddrofNode *dread = mirbuilder->CreateExprDread(*GlobalTables::GetTypeTable().GetInt64(), *st); - BinaryNode *addnode = mirbuilder->CreateExprBinary(OP_add, *GlobalTables::GetTypeTable().GetInt64(), dread, mirbuilder->CreateIntConst(1, PTY_i64)); + BinaryNode *addnode = mirbuilder->CreateExprBinary(OP_add, *GlobalTables::GetTypeTable().GetInt64(), + dread, mirbuilder->CreateIntConst(1, PTY_i64)); dass = mirbuilder->CreateStmtDassign(*st, 0, addnode); laststmt = &tailbb->GetLast(); tailbb->InsertStmtBefore(laststmt, dass); } return nullptr; } - } // namespace maple diff --git a/src/mapleall/maple_me/src/lfo_mir_lower.cpp b/src/mapleall/maple_me/src/lfo_mir_lower.cpp index 9a23ef211eed584b3f2c94b23abbc31d7da9624d..a164c2114437b21d05a3210f3943427cf1aa0148 100644 --- a/src/mapleall/maple_me/src/lfo_mir_lower.cpp +++ b/src/mapleall/maple_me/src/lfo_mir_lower.cpp @@ -24,10 +24,8 @@ using namespace maple; // // goto // label - BlockNode *LFOMIRLower::LowerWhileStmt(WhileStmtNode &whilestmt) { MIRBuilder *mirbuilder = mirModule.GetMIRBuilder(); -//DoCondVarProp(whilestmt); whilestmt.SetBody(LowerBlock(*whilestmt.GetBody())); BlockNode *blk = mirModule.CurFuncCodeMemPool()->New(); LabelIdx whilelblidx = func->GetMirFunc()->GetLabelTab()->CreateLabel(); @@ -75,7 +73,7 @@ BlockNode *LFOMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { BlockNode *blk = mirModule.CurFuncCodeMemPool()->New(); MIRFunction *mirFunc = func->GetMirFunc(); - MIRBuilder *mirbuilder = mirModule.GetMIRBuilder(); + MIRBuilder *mirbuilder = mirModule.GetMIRBuilder(); if (thenempty && elseempty) { // generate EVAL statement diff --git a/src/mapleall/maple_me/src/me_function.cpp b/src/mapleall/maple_me/src/me_function.cpp index e9a62fc9b40d7cadf6fb52071f45654f4af31f6a..7b88f4b65a02797327b2640a094c17fbe503ac8f 100644 --- a/src/mapleall/maple_me/src/me_function.cpp +++ b/src/mapleall/maple_me/src/me_function.cpp @@ -130,14 +130,15 @@ void MeFunction::Prepare(unsigned long rangeNum) { LogInfo::MapleLogger() << "---Preparing Function < " << CurFunction()->GetName() << " > [" << rangeNum << "] ---\n"; } - /* lower first */ + if (MeOption::optLevel >= 3) { MemPool* lfomp = memPoolCtrler.NewMemPool("lfo", true); SetLfoFunc(lfomp->New(lfomp, this)); SetLfoMempool(lfomp); LFOMIRLower lfomirlowerer(mirModule, this); lfomirlowerer.LowerFunc(*CurFunction()); - } else { + } else { + /* lower first */ MIRLower mirLowerer(mirModule, CurFunction()); mirLowerer.Init(); mirLowerer.SetLowerME(); diff --git a/src/mplfe/ast_input/include/ast_builtin_func.def b/src/mplfe/ast_input/include/ast_builtin_func.def index c629fb8cc1863fc8c4a9999142779cdd6185bbb3..1bf2d3c81d21a556a21c57409f3e746c385c83bc 100644 --- a/src/mplfe/ast_input/include/ast_builtin_func.def +++ b/src/mplfe/ast_input/include/ast_builtin_func.def @@ -5,6 +5,7 @@ BUILTIN_FUNC(strncpy) BUILTIN_FUNC(strcpy) BUILTIN_FUNC(strcmp) BUILTIN_FUNC(strlen) +BUILTIN_FUNC(strchr) BUILTIN_FUNC(memcmp) BUILTIN_FUNC(memcpy) BUILTIN_FUNC(memset) diff --git a/src/mplfe/ast_input/include/ast_expr.h b/src/mplfe/ast_input/include/ast_expr.h index beb8d31211faf689a0e5d20e92e02ac0c2cd3c68..714a4a9a01155c59019f8a5c8803a3afa574bfa1 100644 --- a/src/mplfe/ast_input/include/ast_expr.h +++ b/src/mplfe/ast_input/include/ast_expr.h @@ -733,11 +733,12 @@ class ASTArraySubscriptExpr : public ASTExpr { int32 TranslateArraySubscript2Offset() const; private: + bool CheckFirstDimIfZero() const; UniqueFEIRExpr Emit2FEExprImpl(std::list &stmts) const override; ASTExpr *baseExpr = nullptr; - MIRType *arrayType = nullptr; + mutable MIRType *arrayType = nullptr; std::vector baseExprTypes; - std::vector idxExprs; + mutable std::vector idxExprs; }; class ASTExprUnaryExprOrTypeTraitExpr : public ASTExpr { diff --git a/src/mplfe/ast_input/src/ast_decl.cpp b/src/mplfe/ast_input/src/ast_decl.cpp index d1d9b48fbff3a2e5c2378a201cc5683d4dfcb6fe..0c7a5ee1253abecfea6d5f627ee00b9bed15b1e2 100644 --- a/src/mplfe/ast_input/src/ast_decl.cpp +++ b/src/mplfe/ast_input/src/ast_decl.cpp @@ -170,16 +170,16 @@ std::list ASTFunc::EmitASTStmtToFEIR() const { } // fix int main() no return 0 and void func() no return. there are multiple branches, insert return at the end. if (stmts.size() == 0 || stmts.back()->GetKind() != kStmtReturn) { - UniqueFEIRStmt retStmt = nullptr; - if (name == "main" && typeDesc[1]->GetPrimType() == PTY_i32) { - UniqueFEIRExpr retExpr = std::make_unique(static_cast(0), PTY_i32); - retStmt = std::make_unique(std::move(retExpr)); - } else if (typeDesc[1]->GetPrimType() == PTY_void) { - retStmt = std::make_unique(nullptr); - } - if (retStmt != nullptr) { - stmts.emplace_back(std::move(retStmt)); + UniqueFEIRExpr retExpr = nullptr; + PrimType retType = typeDesc[1]->GetPrimType(); + if (retType != PTY_void) { + if (!typeDesc[1]->IsScalarType()) { + retType = PTY_i32; + } + retExpr = FEIRBuilder::CreateExprConstAnyScalar(retType, static_cast(0)); } + UniqueFEIRStmt retStmt = std::make_unique(std::move(retExpr)); + stmts.emplace_back(std::move(retStmt)); } return stmts; } diff --git a/src/mplfe/ast_input/src/ast_expr.cpp b/src/mplfe/ast_input/src/ast_expr.cpp index 44569e78642e4c1b33e13713a3096e5161a5d4d9..7f187132f4da64611b30504dccebef61799dde33 100644 --- a/src/mplfe/ast_input/src/ast_expr.cpp +++ b/src/mplfe/ast_input/src/ast_expr.cpp @@ -446,12 +446,9 @@ UniqueFEIRExpr ASTUOLNotExpr::Emit2FEExprImpl(std::list &stmts) ASTExpr *childExpr = expr; CHECK_FATAL(childExpr != nullptr, "childExpr is nullptr"); UniqueFEIRExpr childFEIRExpr = childExpr->Emit2FEExpr(stmts); - PrimType dstType = uoType->GetPrimType(); - if (childFEIRExpr->GetPrimType() != dstType) { - UniqueFEIRExpr lnotExpr = std::make_unique(OP_lnot, subType, std::move(childFEIRExpr)); - return FEIRBuilder::CreateExprCvtPrim(std::move(lnotExpr), dstType); - } - return std::make_unique(OP_lnot, subType, std::move(childFEIRExpr)); + UniqueFEIRExpr zeroConstExpr = childFEIRExpr->GetPrimType() == PTY_ptr ? FEIRBuilder::CreateExprConstPtrNull() : + FEIRBuilder::CreateExprConstAnyScalar(childFEIRExpr->GetPrimType(), 0); + return FEIRBuilder::CreateExprMathBinary(OP_eq, std::move(childFEIRExpr), std::move(zeroConstExpr)); } UniqueFEIRExpr ASTUnaryOperatorExpr::ASTUOSideEffectExpr(Opcode op, std::list &stmts, @@ -1098,6 +1095,20 @@ int32 ASTArraySubscriptExpr::TranslateArraySubscript2Offset() const { return offset; } +bool ASTArraySubscriptExpr::CheckFirstDimIfZero() const { + bool needChange = false; + auto tmpArrayType = static_cast(arrayType); + uint32 size = tmpArrayType->GetSizeArrayItem(0); + uint32 oriDim = idxExprs.size(); + if (size == 0 && oriDim >= 2) { // 2 is the array dim + arrayType = GlobalTables::GetTypeTable().GetOrCreateArrayType(*tmpArrayType->GetElemType(), + tmpArrayType->GetSizeArrayItem(1)); + idxExprs.pop_back(); + needChange = true; + } + return needChange; +} + UniqueFEIRExpr ASTArraySubscriptExpr::Emit2FEExprImpl(std::list &stmts) const { auto baseAddrFEExpr = baseExpr->Emit2FEExpr(stmts); auto retFEType = std::make_unique(*mirType); @@ -1106,6 +1117,9 @@ UniqueFEIRExpr ASTArraySubscriptExpr::Emit2FEExprImpl(std::list auto fePtrType = std::make_unique(*mirPtrType); UniqueFEIRExpr addrOfArray; if (arrayType->GetKind() == MIRTypeKind::kTypeArray) { + if(CheckFirstDimIfZero()) { + arrayFEType = std::make_unique(*arrayType); + } std::list feIdxExprs; for (auto idxExpr : idxExprs) { auto feIdxExpr = idxExpr->Emit2FEExpr(stmts); diff --git a/src/mplfe/ast_input/src/ast_parser.cpp b/src/mplfe/ast_input/src/ast_parser.cpp index 5cf01aa1d689360ae3b30a5ad928ccf572fd6747..0df2274c99575663dc43df5e0eed064e7260d9eb 100644 --- a/src/mplfe/ast_input/src/ast_parser.cpp +++ b/src/mplfe/ast_input/src/ast_parser.cpp @@ -1587,10 +1587,8 @@ ASTExpr *ASTParser::ProcessExprBinaryOperator(MapleAllocator &allocator, const c ASTExpr *astRExpr = ProcessExpr(allocator, bo.getRHS()); ASTExpr *astLExpr = ProcessExpr(allocator, bo.getLHS()); if (clangOpCode == clang::BO_Div) { - if (astBinOpExpr->GetRetType()->GetPrimType() == PTY_u16 || astBinOpExpr->GetRetType()->GetPrimType() == PTY_u8) { - astBinOpExpr->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_u32)); - } - if (astBinOpExpr->GetRetType()->GetPrimType() == PTY_i16 || astBinOpExpr->GetRetType()->GetPrimType() == PTY_i8) { + auto primaryType = astBinOpExpr->GetRetType()->GetPrimType(); + if (primaryType == PTY_u16 || primaryType == PTY_u8 || primaryType == PTY_i16 || primaryType == PTY_i8) { astBinOpExpr->SetRetType(GlobalTables::GetTypeTable().GetPrimType(PTY_i32)); } } @@ -2160,8 +2158,6 @@ bool ASTParser::RetrieveFuncs(MapleAllocator &allocator) { clang::FunctionDecl *funcDecl = llvm::cast(func); if (funcDecl->isDefined()) { funcDecl = funcDecl->getDefinition(); - } else { - continue; } ASTFunc *af = static_cast(ProcessDecl(allocator, *funcDecl)); if (af == nullptr) { diff --git a/src/mplfe/common/src/feir_builder.cpp b/src/mplfe/common/src/feir_builder.cpp index 24dd046a5d560541760479e3397cd4eca04f8f5d..27b92a084aa04d9d44970c827b40ff2849d41834 100644 --- a/src/mplfe/common/src/feir_builder.cpp +++ b/src/mplfe/common/src/feir_builder.cpp @@ -200,6 +200,7 @@ UniqueFEIRExpr FEIRBuilder::CreateExprConstF64(double val) { // Note that loss of precision, byte value is only supported. UniqueFEIRExpr FEIRBuilder::CreateExprConstAnyScalar(PrimType primType, int64 val) { switch (primType) { + case PTY_u1: case PTY_u8: case PTY_u16: case PTY_u32: diff --git a/src/mplfe/common/src/feir_stmt.cpp b/src/mplfe/common/src/feir_stmt.cpp index 27a742f929dcf5739c78b1b153ea4130497c6145..e004cbbdf704ebb0c93a54bad6bdae000adf2c60 100644 --- a/src/mplfe/common/src/feir_stmt.cpp +++ b/src/mplfe/common/src/feir_stmt.cpp @@ -3630,7 +3630,7 @@ BaseNode *FEIRExprCStyleCast::GenMIRNodeImpl(MIRBuilder &mirBuilder) const { if (sub != nullptr && srcType != nullptr && destType != nullptr) { PrimType fromType = srcType->GetPrimType(); PrimType toType = destType->GetPrimType(); - if (fromType == toType || toType == PTY_void) { + if (fromType == toType || toType == PTY_void || destType->GetKind() == kTypeUnion) { return sub; } if (IsPrimitiveFloat(fromType) && IsPrimitiveInteger(toType)) {