diff --git a/src/mapleall/maple_driver/defs/phases.def b/src/mapleall/maple_driver/defs/phases.def index f30d177731124c766068d94f87703896abeb3f08..80aa29bd5acdc406b4ffebb935f5ccc513c32649 100644 --- a/src/mapleall/maple_driver/defs/phases.def +++ b/src/mapleall/maple_driver/defs/phases.def @@ -24,6 +24,7 @@ ADD_PHASE("gencheckcast", JAVALANG) ADD_PHASE("javaintrnlowering", JAVALANG) ADD_PHASE("inline", Options::O2 && Options::useInline) // mephase begin +ADD_PHASE("mecfgbuild", MeOption::optLevel == 2 || JAVALANG) ADD_PHASE("bypatheh", JAVALANG && MeOption::optLevel == 2) ADD_PHASE("loopcanon", MeOption::optLevel == 2) ADD_PHASE("splitcriticaledge", MeOption::optLevel == 2) diff --git a/src/mapleall/maple_me/include/me_cfg.h b/src/mapleall/maple_me/include/me_cfg.h index e5407adb61f341205b7838fb9093f4d7876df2f3..bd544ec22aed13670db1a9918d322c1dc0312f7d 100644 --- a/src/mapleall/maple_me/include/me_cfg.h +++ b/src/mapleall/maple_me/include/me_cfg.h @@ -18,9 +18,13 @@ #include "me_phase.h" namespace maple { -class MeCFG { +class MeCFG : public AnalysisResult { public: - explicit MeCFG(MeFunction &f) : patternSet(f.GetAlloc().Adapter()), func(f) {} + explicit MeCFG(MemPool *memPool, MeFunction &f) + : AnalysisResult(memPool), + mecfgAlloc(memPool), + func(f), + patternSet(mecfgAlloc.Adapter()) {} ~MeCFG() = default; @@ -52,6 +56,8 @@ class MeCFG { hasDoWhile = hdw; } + MapleAllocator &GetAlloc() { return mecfgAlloc; } + private: void ReplaceSwitchContainsOneCaseBranchWithBrtrue(BB &bb, MapleVector &exitBlocks); void AddCatchHandlerForTryBB(BB &bb, MapleVector &exitBlocks); @@ -61,9 +67,20 @@ class MeCFG { void ConvertMePhiList2IdentityAssigns(BB &meBB) const; bool IsStartTryBB(BB &meBB) const; void FixTryBB(BB &startBB, BB &nextBB); - MapleSet patternSet; + MapleAllocator mecfgAlloc; MeFunction &func; + MapleSet patternSet; bool hasDoWhile = false; }; + +class MeDoMeCfg : public MeFuncPhase { + public: + explicit MeDoMeCfg(MePhaseID id) : MeFuncPhase(id) {} + virtual ~MeDoMeCfg() = default; + AnalysisResult *Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) override; + std::string PhaseName() const override { + return "mecfgbuild"; + } +}; } // namespace maple #endif // MAPLE_ME_INCLUDE_ME_CFG_H diff --git a/src/mapleall/maple_me/include/me_function.h b/src/mapleall/maple_me/include/me_function.h index d102ac9f7547a157bcefdeab1727346fc62ae93a..3148a6f88cbe2696def2147625964574ab7c652a 100644 --- a/src/mapleall/maple_me/include/me_function.h +++ b/src/mapleall/maple_me/include/me_function.h @@ -524,12 +524,12 @@ class MeFunction : public FuncEmit { } void BBTopologicalSort(SCCOfBBs &scc); void BuildSCC(); + void CreateBasicBlocks(); private: void VerifySCC(); void SCCTopologicalSort(std::vector &sccNodes); void BuildSCCDFS(BB &bb, uint32 &visitIndex, std::vector &sccNodes, std::vector &visitedOrder, std::vector &lowestOrder, std::vector &inStack, std::stack &visitStack); - void CreateBasicBlocks(); void SetTryBlockInfo(const StmtNode *nextStmt, StmtNode *tryStmt, BB *lastTryBB, BB *curBB, BB *newBB); MIRFunction *CurFunction() const { return mirModule.CurFunction(); diff --git a/src/mapleall/maple_me/include/me_phases.def b/src/mapleall/maple_me/include/me_phases.def index 53f926c404d617ce3847c50157dc0c4b80fbf9b0..fff5472566e096b8bb61bb4f814f126786ead76d 100644 --- a/src/mapleall/maple_me/include/me_phases.def +++ b/src/mapleall/maple_me/include/me_phases.def @@ -49,6 +49,7 @@ FUNCTPHASE(MeFuncPhase_PREGRENAMER, MeDoPregRename) FUNCAPHASE(MeFuncPhase_MEVERIFY, MeDoVerify) FUNCTPHASE(MeFuncPhase_INTRACONSTPROP, MeDoIntraConstProp) FUNCTPHASE(MeFuncPhase_INTERCONSTPROP, MeDoInterConstProp) +FUNCTPHASE(MeFuncPhase_MECFG, MeDoMeCfg) #if MIR_JAVA FUNCTPHASE(MeFuncPhase_SYNCSELECT, MeDoSyncSelect) #endif diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index 27f36d263b470fc36f711803f0989f2d421d892f..709ce5afd5a095a948c1bad3a9deb5b6bcd70f7c 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -893,6 +893,7 @@ void BBLayout::RunLayout() { } AnalysisResult *MeDoBBLayout::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr*) { + (void)(funcResMgr->GetAnalysisResult(MeFuncPhase_MECFG, func)); // mempool used in analysisresult MemPool *layoutMp = NewMemPool(); auto *bbLayout = layoutMp->New(*layoutMp, *func, DEBUGFUNC(func)); diff --git a/src/mapleall/maple_me/src/me_bypath_eh.cpp b/src/mapleall/maple_me/src/me_bypath_eh.cpp index 32f2ea9cb0f3c88fea67e48218ac9205aa463701..93cbe53f228bc2476394e1eb0592f77cd73d8ca3 100644 --- a/src/mapleall/maple_me/src/me_bypath_eh.cpp +++ b/src/mapleall/maple_me/src/me_bypath_eh.cpp @@ -338,7 +338,8 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c } } -AnalysisResult *MeDoBypathEH::Run(MeFunction *func, MeFuncResultMgr*, ModuleResultMgr *mrm) { +AnalysisResult *MeDoBypathEH::Run(MeFunction *func, MeFuncResultMgr* m, ModuleResultMgr *mrm) { + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); auto *kh = static_cast(mrm->GetAnalysisResult(MoPhase_CHA, &func->GetMIRModule())); CHECK_NULL_FATAL(kh); BypathException(*func, *kh); diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index a2a25998b4641ebd916e4ad2c55bf04327dfe436..a870ed78cb73f269fafabe40b15bd0c1233c26da 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -177,8 +177,8 @@ void MeCFG::AddCatchHandlerForTryBB(BB &bb, MapleVector &exitBlocks) { } void MeCFG::BuildMirCFG() { - MapleVector entryBlocks(func.GetAlloc().Adapter()); - MapleVector exitBlocks(func.GetAlloc().Adapter()); + MapleVector entryBlocks(GetAlloc().Adapter()); + MapleVector exitBlocks(GetAlloc().Adapter()); std::vector switchBBsWithOneCaseBranch; auto eIt = func.valid_end(); for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { @@ -1059,4 +1059,27 @@ void MeCFG::DumpToFile(const std::string &prefix, bool dumpInStrs, bool dumpEdge cfgFile.flush(); LogInfo::MapleLogger().rdbuf(coutBuf); } + +AnalysisResult *MeDoMeCfg::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + MemPool *meCfgMp = NewMemPool(); + MeCFG *theCFG = meCfgMp->New(meCfgMp, *func); + func->SetTheCfg(theCFG); + func->CreateBasicBlocks(); + if (func->NumBBs() == 0) { + /* there's no basicblock generated */ + return theCFG; + } + theCFG->BuildMirCFG(); + if (MeOption::optLevel > mapleOption::kLevelZero) { + theCFG->FixMirCFG(); + } + theCFG->ReplaceWithAssertnonnull(); + theCFG->VerifyLabels(); + theCFG->UnreachCodeAnalysis(); + theCFG->WontExitAnalysis(); + theCFG->Verify(); + + return theCFG; +} + } // namespace maple diff --git a/src/mapleall/maple_me/src/me_cfg_opt.cpp b/src/mapleall/maple_me/src/me_cfg_opt.cpp index 23ce0680f6e230c96a4b7cab12481b3b9b4eeed9..bed601f8b281fe0987e37b7864949ff73c209071 100644 --- a/src/mapleall/maple_me/src/me_cfg_opt.cpp +++ b/src/mapleall/maple_me/src/me_cfg_opt.cpp @@ -378,8 +378,11 @@ AnalysisResult *MeDoCfgOpt::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResu ASSERT(irMap != nullptr, "irMap should not be nullptr"); MeCfgOpt cfgOpt(irMap); if (cfgOpt.Run(*func)) { - SetChangeCFG(); EmitMapleIr(*func, *m); + m->InvalidAllResults(); + func->SetTheCfg(nullptr); + func->SetMeSSATab(nullptr); + func->SetIRMap(nullptr); return nullptr; } diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index f7010caa31a1e1fabcf8832d42f2a1479a8cc96a..233187d3c9bc4f0fe915b4c0df1f9232447e23b8 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -170,6 +170,7 @@ void MeDoSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) con } AnalysisResult *MeDoSplitCEdge::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); std::vector> criticalEdge; auto eIt = func->valid_end(); for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { diff --git a/src/mapleall/maple_me/src/me_dominance.cpp b/src/mapleall/maple_me/src/me_dominance.cpp index 20f2832d131922c18b73bc24279749ef54f29baf..ef981b4a9b9c9120010381656f3778f98514b11e 100644 --- a/src/mapleall/maple_me/src/me_dominance.cpp +++ b/src/mapleall/maple_me/src/me_dominance.cpp @@ -15,14 +15,15 @@ #include "me_dominance.h" #include #include "me_option.h" - +#include "me_cfg.h" // This phase analyses the CFG of the given MeFunction, generates the dominator tree, // and the dominance frontiers of each basic block using Keith Cooper's algorithm. // For some backward data-flow problems, such as LiveOut, // the reverse CFG(The CFG with its edges reversed) is always useful, // so we also generates the above two structures on the reverse CFG. namespace maple { -AnalysisResult *MeDoDominance::Run(MeFunction *func, MeFuncResultMgr*, ModuleResultMgr*) { +AnalysisResult *MeDoDominance::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); MemPool *memPool = NewMemPool(); auto *dom = memPool->New(*memPool, *NewMemPool(), func->GetAllBBs(), *func->GetCommonEntryBB(), *func->GetCommonExitBB()); diff --git a/src/mapleall/maple_me/src/me_function.cpp b/src/mapleall/maple_me/src/me_function.cpp index 8e461ef08723a23b7f3f606601c4f77daa82618f..3ddae2d18110c35ad263cb2d63fef4c16bf0450a 100644 --- a/src/mapleall/maple_me/src/me_function.cpp +++ b/src/mapleall/maple_me/src/me_function.cpp @@ -476,21 +476,6 @@ void MeFunction::Prepare(unsigned long rangeNum) { mirLowerer.SetLowerExpandArray(); ASSERT(CurFunction() != nullptr, "nullptr check"); mirLowerer.LowerFunc(*CurFunction()); - CreateBasicBlocks(); - if (NumBBs() == 0) { - /* there's no basicblock generated */ - return; - } - theCFG = memPool->New(*this); - theCFG->BuildMirCFG(); - if (MeOption::optLevel > mapleOption::kLevelZero) { - theCFG->FixMirCFG(); - } - theCFG->ReplaceWithAssertnonnull(); - theCFG->VerifyLabels(); - theCFG->UnreachCodeAnalysis(); - theCFG->WontExitAnalysis(); - theCFG->Verify(); } void MeFunction::Verify() const { diff --git a/src/mapleall/maple_me/src/me_loop_unrolling.cpp b/src/mapleall/maple_me/src/me_loop_unrolling.cpp index 260c2479b2fc49185fe6069645d822733c813d2d..169aa162f8280584118d19d55da8b16330d035a8 100644 --- a/src/mapleall/maple_me/src/me_loop_unrolling.cpp +++ b/src/mapleall/maple_me/src/me_loop_unrolling.cpp @@ -1285,6 +1285,7 @@ AnalysisResult *MeDoLoopUnrolling::Run(MeFunction *func, MeFuncResultMgr *m, Mod auto *irMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); CHECK_NULL_FATAL(irMap); ExecuteLoopUnrolling(*func, *m, *irMap); + m->InvalidAnalysisResult(MeFuncPhase_MECFG, func); m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, func); m->InvalidAnalysisResult(MeFuncPhase_MELOOP, func); return nullptr; diff --git a/src/mapleall/maple_me/src/me_phase_manager.cpp b/src/mapleall/maple_me/src/me_phase_manager.cpp index 6e1562fe133ae5a2f56f1cc8af4bf201f1af7551..e228a87af2f505a5c05d6543dab1b6e3b10f8db8 100644 --- a/src/mapleall/maple_me/src/me_phase_manager.cpp +++ b/src/mapleall/maple_me/src/me_phase_manager.cpp @@ -86,7 +86,8 @@ void MeFuncPhaseManager::RunFuncPhase(MeFunction *func, MeFuncPhase *phase) { // 4. run: skip mplme phase except "emit" if no cfg in MeFunction AnalysisResult *analysisRes = nullptr; MePhaseID phaseID = phase->GetPhaseId(); - if ((func->NumBBs() > 0 || (mirModule.IsInIPA() && phaseID == MeFuncPhase_IPASIDEEFFECT)) || + if ((phaseID == MeFuncPhase_MECFG) || + (func->NumBBs() > 0 || (mirModule.IsInIPA() && phaseID == MeFuncPhase_IPASIDEEFFECT)) || (phaseID == MeFuncPhase_EMIT) || (phaseID == MeFuncPhase_SSARENAME2PREG)) { analysisRes = phase->Run(func, &arFuncManager, modResMgr); phase->ClearMemPoolsExcept(analysisRes == nullptr ? nullptr : analysisRes->GetMempool()); @@ -144,6 +145,7 @@ void MeFuncPhaseManager::AddPhases(const std::unordered_set &skipPh bool o2 = (MeOption::optLevel == 2); if (mePhaseType == kMePhaseMainopt) { // default phase sequence + addPhase("mecfgbuild"); if (o2) { addPhase("loopcanon"); addPhase("splitcriticaledge"); diff --git a/src/mapleall/maple_me/src/me_placement_rc.cpp b/src/mapleall/maple_me/src/me_placement_rc.cpp index 7af2d23b779aa5eea77fd02949759ab27d6949a6..261adcdced4d966b85d58106ae5d3bbc16b36ad5 100644 --- a/src/mapleall/maple_me/src/me_placement_rc.cpp +++ b/src/mapleall/maple_me/src/me_placement_rc.cpp @@ -881,7 +881,7 @@ AnalysisResult *MeDoPlacementRC::Run(MeFunction *func, MeFuncResultMgr *m, Modul if (whiteListFunc.find(funcName) != whiteListFunc.end() || func->GetMirFunc()->GetAttr(FUNCATTR_rclocalunowned)) { return nullptr; } - + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); // Workaround for RCWeakRef-annotated field access: leave it to analyzerc for (BB *bb : func->GetAllBBs()) { if (bb == nullptr) { diff --git a/src/mapleall/maple_me/src/me_profile_use.cpp b/src/mapleall/maple_me/src/me_profile_use.cpp index 3c4c523a1201e5be83fd9213ace87242932fdcbc..d0a986159413c4ae0095d995b533643ba41acdd3 100644 --- a/src/mapleall/maple_me/src/me_profile_use.cpp +++ b/src/mapleall/maple_me/src/me_profile_use.cpp @@ -291,7 +291,8 @@ bool MeProfUse::Run() { return true; } -AnalysisResult *MeDoProfUse::Run(MeFunction *func, MeFuncResultMgr *, ModuleResultMgr*) { +AnalysisResult *MeDoProfUse::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); MemPool *tempMp = NewMemPool(); MeProfUse profUse(*func, *tempMp, DEBUGFUNC(func)); profUse.Run(); diff --git a/src/mapleall/maple_me/src/me_ssa_tab.cpp b/src/mapleall/maple_me/src/me_ssa_tab.cpp index 334654f8cf04bbba0ebf0210adb96c9e4d328ec1..4a89c20f94b409f754526a0810f75a97c4134fb3 100644 --- a/src/mapleall/maple_me/src/me_ssa_tab.cpp +++ b/src/mapleall/maple_me/src/me_ssa_tab.cpp @@ -18,7 +18,8 @@ // allocate the data structure to store SSA information namespace maple { -AnalysisResult *MeDoSSATab::Run(MeFunction *func, MeFuncResultMgr*, ModuleResultMgr*) { +AnalysisResult *MeDoSSATab::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + (void)(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); MPLTimer timer; timer.Start(); if (DEBUGFUNC(func)) {