From e76e7f01649e180b77fd9aa1b2ac3950d8b21c66 Mon Sep 17 00:00:00 2001 From: linma Date: Fri, 7 May 2021 20:28:21 -0700 Subject: [PATCH] OAC merge: make building cfg to a mplme phase with phase name [ mecfgbuild ] --- src/mapleall/maple_driver/defs/phases.def | 1 + src/mapleall/maple_me/include/me_cfg.h | 23 +++++++++++++--- src/mapleall/maple_me/include/me_function.h | 2 +- src/mapleall/maple_me/include/me_phases.def | 1 + src/mapleall/maple_me/src/me_bb_layout.cpp | 1 + src/mapleall/maple_me/src/me_bypath_eh.cpp | 3 ++- src/mapleall/maple_me/src/me_cfg.cpp | 27 +++++++++++++++++-- src/mapleall/maple_me/src/me_cfg_opt.cpp | 5 +++- .../maple_me/src/me_critical_edge.cpp | 1 + src/mapleall/maple_me/src/me_dominance.cpp | 5 ++-- src/mapleall/maple_me/src/me_function.cpp | 15 ----------- .../maple_me/src/me_loop_unrolling.cpp | 1 + .../maple_me/src/me_phase_manager.cpp | 4 ++- src/mapleall/maple_me/src/me_placement_rc.cpp | 2 +- src/mapleall/maple_me/src/me_profile_use.cpp | 3 ++- src/mapleall/maple_me/src/me_ssa_tab.cpp | 3 ++- 16 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/mapleall/maple_driver/defs/phases.def b/src/mapleall/maple_driver/defs/phases.def index f30d177731..80aa29bd5a 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 e5407adb61..bd544ec22a 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 d102ac9f75..3148a6f88c 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 53f926c404..fff5472566 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 27f36d263b..709ce5afd5 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 32f2ea9cb0..93cbe53f22 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 a2a25998b4..a870ed78cb 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 23ce0680f6..bed601f8b2 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 f7010caa31..233187d3c9 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 20f2832d13..ef981b4a9b 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 8e461ef087..3ddae2d181 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 260c2479b2..169aa162f8 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 6e1562fe13..e228a87af2 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 7af2d23b77..261adcdced 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 3c4c523a12..d0a9861594 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 334654f8cf..4a89c20f94 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)) { -- Gitee