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_ipa/src/ipa_escape_analysis.cpp b/src/mapleall/maple_ipa/src/ipa_escape_analysis.cpp index 01ac1393661fb1002daffa4d8beaeb066911e573..4496628398dc4e550b9a541d88af64670d4dcea0 100644 --- a/src/mapleall/maple_ipa/src/ipa_escape_analysis.cpp +++ b/src/mapleall/maple_ipa/src/ipa_escape_analysis.cpp @@ -13,6 +13,7 @@ * See the Mulan PSL v2 for more details. */ #include "ipa_escape_analysis.h" +#include "me_cfg.h" #include namespace maple { @@ -1158,7 +1159,7 @@ void IPAEscapeAnalysis::ConstructConnGraph() { analyzeAgain = false; cgChangedInSCC = false; for (BB *bb : scc->GetBBs()) { - if (bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB()) { + if (bb == func->GetCfg()->GetCommonEntryBB() || bb == func->GetCfg()->GetCommonExitBB()) { continue; } UpdateEscConnGraphWithPhi(*bb); @@ -1236,8 +1237,9 @@ VarMeExpr *IPAEscapeAnalysis::GetOrCreateEARetTempVar() { } void IPAEscapeAnalysis::ProcessNoAndRetEscObj() { - for (BB *bb : func->GetAllBBs()) { - if (bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB() || bb == nullptr || + MeCFG *cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { + if (bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB() || bb == nullptr || bb->GetAttributes(kBBAttrIsInLoopForEA)) { continue; } @@ -1268,7 +1270,7 @@ void IPAEscapeAnalysis::ProcessNoAndRetEscObj() { if (noAndRetEscOst.size() == 0) { return; } - BB *firstBB = func->GetFirstBB(); + BB *firstBB = cfg->GetFirstBB(); CHECK_FATAL(firstBB != nullptr, "Impossible"); for (size_t i = 0; i < noAndRetEscOst.size(); ++i) { OriginalSt *ost = noAndRetEscOst[i]; @@ -1295,16 +1297,16 @@ void IPAEscapeAnalysis::ProcessRetStmt() { if (noAndRetEscObj.size() == 0) { return; } - - BB *firstBB = func->GetFirstBB(); + MeCFG *cfg = func->GetCfg(); + BB *firstBB = cfg->GetFirstBB(); OriginalSt *ost = CreateEARetTempOst(); VarMeExpr *initVar = CreateEATempVarMeExpr(*ost); MeExpr *zeroExpr = irMap->CreateIntConstMeExpr(0, PTY_ref); DassignMeStmt *newStmt = static_cast(irMap->CreateAssignMeStmt(*initVar, *zeroExpr, *firstBB)); firstBB->AddMeStmtFirst(newStmt); - for (BB *bb : func->GetAllBBs()) { - if (bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB() || bb == nullptr) { + for (BB *bb : cfg->GetAllBBs()) { + if (bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB() || bb == nullptr) { continue; } for (MeStmt *stmt = to_ptr(bb->GetMeStmts().begin()); stmt != nullptr; stmt = stmt->GetNextMeStmt()) { @@ -1331,8 +1333,9 @@ void IPAEscapeAnalysis::ProcessRetStmt() { } void IPAEscapeAnalysis::CountObjRCOperations() { - for (BB *bb : func->GetAllBBs()) { - if (bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB() || bb == nullptr) { + MeCFG *cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { + if (bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB() || bb == nullptr) { continue; } for (MeStmt *stmt = to_ptr(bb->GetMeStmts().begin()); stmt != nullptr; stmt = stmt->GetNextMeStmt()) { @@ -1502,8 +1505,9 @@ void IPAEscapeAnalysis::CountObjRCOperations() { } void IPAEscapeAnalysis::DeleteRedundantRC() { - for (BB *bb : func->GetAllBBs()) { - if (bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB() || bb == nullptr) { + MeCFG *cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { + if (bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB() || bb == nullptr) { continue; } for (MeStmt *stmt = to_ptr(bb->GetMeStmts().begin()); stmt != nullptr; stmt = stmt->GetNextMeStmt()) { diff --git a/src/mapleall/maple_me/include/me_alias_class.h b/src/mapleall/maple_me/include/me_alias_class.h index 7ec10aef94175b50d0af55926f61397c04ca420c..2900fe6ffbd3952a3e1e7d976820f04a9ab50245 100644 --- a/src/mapleall/maple_me/include/me_alias_class.h +++ b/src/mapleall/maple_me/include/me_alias_class.h @@ -17,6 +17,7 @@ #include "alias_class.h" #include "me_phase.h" #include "me_function.h" +#include "me_cfg.h" namespace maple { class MeAliasClass : public AliasClass { @@ -24,7 +25,7 @@ class MeAliasClass : public AliasClass { MeAliasClass(MemPool &memPool, MIRModule &mod, SSATab &ssaTab, MeFunction &func, bool lessAliasAtThrow, bool ignoreIPA, bool debug, bool setCalleeHasSideEffect, KlassHierarchy *kh) : AliasClass(memPool, mod, ssaTab, lessAliasAtThrow, ignoreIPA, setCalleeHasSideEffect, kh), - func(func), enabledDebug(debug) {} + func(func), cfg(func.GetCfg()), enabledDebug(debug) {} virtual ~MeAliasClass() = default; @@ -32,10 +33,10 @@ class MeAliasClass : public AliasClass { private: BB *GetBB(BBId id) override { - if (func.GetAllBBs().size() < id) { + if (cfg->GetAllBBs().size() < id) { return nullptr; } - return func.GetBBFromID(id); + return cfg->GetBBFromID(id); } bool InConstructorLikeFunc() const override { @@ -45,16 +46,18 @@ class MeAliasClass : public AliasClass { bool HasWriteToStaticFinal() const; MeFunction &func; + MeCFG *cfg; bool enabledDebug; }; class MeDoAliasClass : public MeFuncPhase { + ModuleResultMgr *moduleResultMgr; // keep the moduleResultmgr for later use public: explicit MeDoAliasClass(MePhaseID id) : MeFuncPhase(id) {} virtual ~MeDoAliasClass() = default; - AnalysisResult *Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr *moduleResMgr) override; + AnalysisResult *Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr *mrm) override; std::string PhaseName() const override { return "aliasclass"; diff --git a/src/mapleall/maple_me/include/me_analyze_rc.h b/src/mapleall/maple_me/include/me_analyze_rc.h index b289a75ea4eebe9607a1a05247dc1c7c3d3176b6..67c9815e9c4305926f9dbda7eae229856d1d7f9d 100644 --- a/src/mapleall/maple_me/include/me_analyze_rc.h +++ b/src/mapleall/maple_me/include/me_analyze_rc.h @@ -49,6 +49,7 @@ class AnalyzeRC { public: AnalyzeRC(MeFunction &f, Dominance &dom, AliasClass &ac, MemPool *memPool) : func(f), + cfg(f.GetCfg()), irMap(*f.GetIRMap()), ssaTab(*f.GetMeSSATab()), dominance(dom), @@ -81,6 +82,7 @@ class AnalyzeRC { friend class MeDoAnalyzeRC; MeFunction &func; + MeCFG *cfg; IRMap &irMap; SSATab &ssaTab; Dominance &dominance; diff --git a/src/mapleall/maple_me/include/me_bb_analyze.h b/src/mapleall/maple_me/include/me_bb_analyze.h index 2dc903342aa734be447dd93f6bd25fc4af6ed4f6..dedb2bd56c8c1a66622f42652517853967229ecf 100644 --- a/src/mapleall/maple_me/include/me_bb_analyze.h +++ b/src/mapleall/maple_me/include/me_bb_analyze.h @@ -19,12 +19,13 @@ #include "me_function.h" #include "me_phase.h" #include "me_option.h" +#include "me_cfg.h" namespace maple { const uint32 kPrecision = 100; class BBAnalyze : public AnalysisResult { public: - BBAnalyze(MemPool &memPool, MeFunction &f) : AnalysisResult(&memPool), meBBAlloc(&memPool), func(f) {} + BBAnalyze(MemPool &memPool, MeFunction &f) : AnalysisResult(&memPool), meBBAlloc(&memPool), cfg(f.GetCfg()) {} virtual ~BBAnalyze() = default; @@ -36,7 +37,7 @@ class BBAnalyze : public AnalysisResult { private: MapleAllocator meBBAlloc; - MeFunction &func; + MeCFG *cfg; uint32 hotBBCountThreshold = 0; uint32 coldBBCountThreshold = 0; }; @@ -52,4 +53,4 @@ class MeDoBBAnalyze : public MeFuncPhase { } }; } // namespace maple -#endif // MAPLE_ME_INCLUDE_ME_ANALYZE_H \ No newline at end of file +#endif // MAPLE_ME_INCLUDE_ME_ANALYZE_H diff --git a/src/mapleall/maple_me/include/me_bb_layout.h b/src/mapleall/maple_me/include/me_bb_layout.h index 46a56d277796eebea5525b545e5d7d4b8207d3ee..dcdc0c49f9dfa9fab0f6f9912e51c110ec5215c7 100644 --- a/src/mapleall/maple_me/include/me_bb_layout.h +++ b/src/mapleall/maple_me/include/me_bb_layout.h @@ -25,21 +25,22 @@ class BBLayout{ : func(f), layoutAlloc(&memPool), layoutBBs(layoutAlloc.Adapter()), - startTryBBVec(func.GetAllBBs().size(), false, layoutAlloc.Adapter()), + startTryBBVec(func.GetCfg()->GetAllBBs().size(), false, layoutAlloc.Adapter()), allEdges(layoutAlloc.Adapter()), - laidOut(func.GetAllBBs().size(), false, layoutAlloc.Adapter()), + laidOut(func.GetCfg()->GetAllBBs().size(), false, layoutAlloc.Adapter()), enabledDebug(enabledDebug), - profValid(func.IsIRProfValid()) { - laidOut[func.GetCommonEntryBB()->GetBBId()] = true; - laidOut[func.GetCommonExitBB()->GetBBId()] = true; + profValid(func.IsIRProfValid()), + cfg(f.GetCfg()) { + laidOut[func.GetCfg()->GetCommonEntryBB()->GetBBId()] = true; + laidOut[func.GetCfg()->GetCommonExitBB()->GetBBId()] = true; } ~BBLayout() = default; BB *NextBB() { // return the next BB following strictly program input order ++curBBId; - while (curBBId < func.GetAllBBs().size()) { - BB *nextBB = func.GetBBFromID(curBBId); + while (curBBId < func.GetCfg()->GetAllBBs().size()) { + BB *nextBB = func.GetCfg()->GetBBFromID(curBBId); if (nextBB != nullptr && !laidOut[nextBB->GetBBId()]) { return nextBB; } @@ -121,6 +122,7 @@ class BBLayout{ bool enabledDebug; bool profValid = false; size_t edgeIdx = 0; + MeCFG *cfg; }; class MeDoBBLayout : public MeFuncPhase { diff --git a/src/mapleall/maple_me/include/me_cfg.h b/src/mapleall/maple_me/include/me_cfg.h index e5407adb61f341205b7838fb9093f4d7876df2f3..b48f723257f9d95635f2e6e944ffcf27b2a48053 100644 --- a/src/mapleall/maple_me/include/me_cfg.h +++ b/src/mapleall/maple_me/include/me_cfg.h @@ -18,9 +18,31 @@ #include "me_phase.h" namespace maple { -class MeCFG { +class MeCFG : public AnalysisResult { + using BBPtrHolder = MapleVector; public: - explicit MeCFG(MeFunction &f) : patternSet(f.GetAlloc().Adapter()), func(f) {} + using value_type = BBPtrHolder::value_type; + using size_type = BBPtrHolder::size_type; + using difference_type = BBPtrHolder::difference_type; + using pointer = BBPtrHolder::pointer; + using const_pointer = BBPtrHolder::const_pointer; + using reference = BBPtrHolder::reference; + using const_reference = BBPtrHolder::const_reference; + using iterator = BBPtrHolder::iterator; + using const_iterator = BBPtrHolder::const_iterator; + using reverse_iterator = BBPtrHolder::reverse_iterator; + using const_reverse_iterator = BBPtrHolder::const_reverse_iterator; + + explicit MeCFG(MemPool *memPool, MeFunction &f) + : AnalysisResult(memPool), + mp(memPool), + mecfgAlloc(memPool), + func(f), + patternSet(mecfgAlloc.Adapter()), + bbVec(mecfgAlloc.Adapter()), + labelBBIdMap(mecfgAlloc.Adapter()), + bbTryNodeMap(mecfgAlloc.Adapter()), + endTryBB2TryBB(mecfgAlloc.Adapter()) {} ~MeCFG() = default; @@ -39,6 +61,14 @@ class MeCFG { bool FindUse(const StmtNode &stmt, StIdx stIdx) const; bool FindDef(const StmtNode &stmt, StIdx stIdx) const; bool HasNoOccBetween(StmtNode &from, const StmtNode &to, StIdx stIdx) const; + BB *NewBasicBlock(); + BB &InsertNewBasicBlock(const BB &position, bool isInsertBefore = true); + void DeleteBasicBlock(const BB &bb); + BB *NextBB(const BB *bb); + BB *PrevBB(const BB *bb); + void CloneBasicBlock(BB &newBB, const BB &orig); + BB &SplitBB(BB &bb, StmtNode &splitPoint, BB *newBB = nullptr); + void SplitBBPhysically(BB &bb, StmtNode &splitPoint, BB &newBB); const MeFunction &GetFunc() const { return func; @@ -52,6 +82,190 @@ class MeCFG { hasDoWhile = hdw; } + MapleAllocator &GetAlloc() { return mecfgAlloc; } + + void SetNextBBId(uint32 currNextBBId) { + nextBBId = currNextBBId; + } + uint32 GetNextBBId() const { + return nextBBId; + } + void DecNextBBId() { + --nextBBId; + } + + MapleVector &GetAllBBs() { return bbVec; } + + iterator begin() { + return bbVec.begin(); + } + const_iterator begin() const { + return bbVec.begin(); + } + const_iterator cbegin() const { + return bbVec.cbegin(); + } + + iterator end() { + return bbVec.end(); + } + const_iterator end() const { + return bbVec.end(); + } + const_iterator cend() const { + return bbVec.cend(); + } + + reverse_iterator rbegin() { + return bbVec.rbegin(); + } + const_reverse_iterator rbegin() const { + return bbVec.rbegin(); + } + const_reverse_iterator crbegin() const { + return bbVec.crbegin(); + } + + reverse_iterator rend() { + return bbVec.rend(); + } + const_reverse_iterator rend() const { + return bbVec.rend(); + } + const_reverse_iterator crend() const { + return bbVec.crend(); + } + + reference front() { + return bbVec.front(); + } + + reference back() { + return bbVec.back(); + } + + const_reference front() const { + return bbVec.front(); + } + + const_reference back() const { + return bbVec.back(); + } + + bool empty() const { + return bbVec.empty(); + } + + size_t size() const { + return bbVec.size(); + } + FilterIterator valid_begin() const { + return build_filter_iterator(begin(), std::bind(FilterNullPtr, std::placeholders::_1, end())); + } + + FilterIterator valid_end() const { + return build_filter_iterator(end()); + } + + FilterIterator valid_rbegin() const { + return build_filter_iterator(rbegin(), + std::bind(FilterNullPtr, std::placeholders::_1, rend())); + } + + FilterIterator valid_rend() const { + return build_filter_iterator(rend()); + } + + const_iterator common_entry() const { + return begin(); + } + + const_iterator context_begin() const { + return ++(++begin()); + } + + const_iterator context_end() const { + return end(); + } + + const_iterator common_exit() const { + return ++begin(); + } + + uint32 NumBBs() const { + return nextBBId; + } + + const MapleUnorderedMap &GetLabelBBIdMap() const { + return labelBBIdMap; + } + BB *GetLabelBBAt(LabelIdx idx) const { + auto it = labelBBIdMap.find(idx); + if (it != labelBBIdMap.end()) { + return it->second; + } + return nullptr; + } + + void SetLabelBBAt(LabelIdx idx, BB *bb) { + labelBBIdMap[idx] = bb; + } + void EraseLabelBBAt(LabelIdx idx) { + labelBBIdMap.erase(idx); + } + + BB *GetBBFromID(BBId bbID) const { + ASSERT(bbID < bbVec.size(), "array index out of range"); + return bbVec.at(bbID); + } + + void NullifyBBByID(BBId bbID) { + ASSERT(bbID < bbVec.size(), "array index out of range"); + bbVec.at(bbID) = nullptr; + } + + BB *GetCommonEntryBB() const { + return *common_entry(); + } + + BB *GetCommonExitBB() const { + return *common_exit(); + } + + BB *GetFirstBB() const { + return *(++(++valid_begin())); + } + + BB *GetLastBB() const { + return *valid_rbegin(); + } + + void SetBBTryNodeMap(BB &bb, StmtNode &tryStmt) { + bbTryNodeMap[&bb] = &tryStmt; + } + + const MapleUnorderedMap &GetBBTryNodeMap() const { + return bbTryNodeMap; + } + + const MapleUnorderedMap &GetEndTryBB2TryBB() const { + return endTryBB2TryBB; + } + + const BB *GetTryBBFromEndTryBB(BB *endTryBB) const { + auto it = endTryBB2TryBB.find(endTryBB); + return it == endTryBB2TryBB.end() ? nullptr : it->second; + } + void SetBBTryBBMap(BB *currBB, BB *tryBB) { + endTryBB2TryBB[currBB] = tryBB; + } + void SetTryBBByOtherEndTryBB(BB *endTryBB, BB *otherTryBB) { + endTryBB2TryBB[endTryBB] = endTryBB2TryBB[otherTryBB]; + } + + MemPool * GetMempool() const { return mp; } + void CreateBasicBlocks(); + private: void ReplaceSwitchContainsOneCaseBranchWithBrtrue(BB &bb, MapleVector &exitBlocks); void AddCatchHandlerForTryBB(BB &bb, MapleVector &exitBlocks); @@ -61,9 +275,28 @@ class MeCFG { void ConvertMePhiList2IdentityAssigns(BB &meBB) const; bool IsStartTryBB(BB &meBB) const; void FixTryBB(BB &startBB, BB &nextBB); - MapleSet patternSet; + void SetTryBlockInfo(const StmtNode *nextStmt, StmtNode *tryStmt, BB *lastTryBB, BB *curBB, BB *newBB); + + MemPool *mp; + MapleAllocator mecfgAlloc; MeFunction &func; + MapleSet patternSet; + BBPtrHolder bbVec; + MapleUnorderedMap labelBBIdMap; + MapleUnorderedMap bbTryNodeMap; // maps isTry bb to its try stmt + MapleUnorderedMap endTryBB2TryBB; // maps endtry bb to its try bb bool hasDoWhile = false; + uint32 nextBBId = 0; +}; + +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_cfg_mst.h b/src/mapleall/maple_me/include/me_cfg_mst.h index 4fd0c5ec858c081348bcc77c6a13d445f58af5f4..ca9e468bef4d55d5e3ecb2c8358dad1b574e4e83 100644 --- a/src/mapleall/maple_me/include/me_cfg_mst.h +++ b/src/mapleall/maple_me/include/me_cfg_mst.h @@ -14,9 +14,9 @@ */ #ifndef MAPLE_ME_INCLUDE_CFGMST_H #define MAPLE_ME_INCLUDE_CFGMST_H - #include "me_function.h" #include "bb.h" +#include "me_cfg.h" namespace maple { template @@ -155,12 +155,12 @@ void CFGMST::SortEdges() { template void CFGMST::BuildEdges() { - BB *entry = func->GetCommonEntryBB(); + BB *entry = func->GetCfg()->GetCommonEntryBB(); BB *exit = nullptr; - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + auto eIt = func->GetCfg()->valid_end(); + for (auto bIt = func->GetCfg()->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; - if (bIt == func->common_exit()) { + if (bIt == func->GetCfg()->common_exit()) { exit = *bIt; continue; } @@ -179,7 +179,7 @@ void CFGMST::BuildEdges() { } } - for (BB *bb : func->GetCommonExitBB()->GetPred()) { + for (BB *bb : func->GetCfg()->GetCommonExitBB()->GetPred()) { AddEdge(bb, exit, fakeExitEdgeWeight, false, true); } /* insert fake edge to keep consistent */ diff --git a/src/mapleall/maple_me/include/me_cfg_opt.h b/src/mapleall/maple_me/include/me_cfg_opt.h index 39050ae11c6a35c6a95ed207565754df5891429f..43f91862b7c94950b7544d0f328f9a96bea3b209 100644 --- a/src/mapleall/maple_me/include/me_cfg_opt.h +++ b/src/mapleall/maple_me/include/me_cfg_opt.h @@ -20,20 +20,13 @@ namespace maple { class MeCfgOpt { public: - explicit MeCfgOpt(MeIRMap *irMap) : meIrMap(irMap), isCfgChanged(false) {} + explicit MeCfgOpt(MeIRMap *irMap) : meIrMap(irMap) {} ~MeCfgOpt() = default; - bool Run(MeFunction &func); - bool IsCfgChanged() const { - return isCfgChanged; - } - - void SetCfgChanged() { - isCfgChanged = true; - } + bool Run(MeCFG &cfg); private: - bool PreCheck(const MeFunction &func) const; + bool PreCheck(const MeCFG &func) const; bool IsOk2Select(const MeExpr &expr0, const MeExpr &expr1) const; // collect expensive ops and if there is reference, return true static bool IsExpensiveOp(Opcode op); @@ -59,7 +52,6 @@ class MeCfgOpt { MeStmt *GetTheOnlyMeStmtFromBB(BB &bb) const; MeStmt *GetTheOnlyMeStmtWithGotoFromBB(BB &bb) const; MeIRMap *meIrMap; - bool isCfgChanged; }; class MeDoCfgOpt : public MeFuncPhase { diff --git a/src/mapleall/maple_me/include/me_dse.h b/src/mapleall/maple_me/include/me_dse.h index d12543049c8dbab12f1e75e4e17f5123c451dfc9..f6fb4a7e23ceb1cd317b7b0a06174b627c5aa179 100644 --- a/src/mapleall/maple_me/include/me_dse.h +++ b/src/mapleall/maple_me/include/me_dse.h @@ -20,23 +20,25 @@ #include "me_option.h" #include "dominance.h" #include "me_function.h" -#include "dse.h" #include "me_cfg.h" +#include "dse.h" namespace maple { class MeDSE : public DSE { public: MeDSE(MeFunction &func, Dominance *dom, bool enabledDebug) - : DSE(std::vector(func.GetAllBBs().begin(), func.GetAllBBs().end()), - *func.GetCommonEntryBB(), *func.GetCommonExitBB(), *func.GetMeSSATab(), + : DSE(std::vector(func.GetCfg()->GetAllBBs().begin(), func.GetCfg()->GetAllBBs().end()), + *func.GetCfg()->GetCommonEntryBB(), *func.GetCfg()->GetCommonExitBB(), *func.GetMeSSATab(), *dom, enabledDebug, MeOption::decoupleStatic), - func(func) {} + func(func), + cfg(func.GetCfg()) {} virtual ~MeDSE() = default; void RunDSE(); private: MeFunction &func; + MeCFG *cfg; void VerifyPhi() const; }; diff --git a/src/mapleall/maple_me/include/me_function.h b/src/mapleall/maple_me/include/me_function.h index d102ac9f7547a157bcefdeab1727346fc62ae93a..f0eb0d08fa28ed8ec34cffcfe589478d52fec7c4 100644 --- a/src/mapleall/maple_me/include/me_function.h +++ b/src/mapleall/maple_me/include/me_function.h @@ -145,21 +145,7 @@ enum MeFuncHint { }; class MeFunction : public FuncEmit { - using BBPtrHolder = MapleVector; - public: - using value_type = BBPtrHolder::value_type; - using size_type = BBPtrHolder::size_type; - using difference_type = BBPtrHolder::difference_type; - using pointer = BBPtrHolder::pointer; - using const_pointer = BBPtrHolder::const_pointer; - using reference = BBPtrHolder::reference; - using const_reference = BBPtrHolder::const_reference; - using iterator = BBPtrHolder::iterator; - using const_iterator = BBPtrHolder::const_iterator; - using reverse_iterator = BBPtrHolder::reverse_iterator; - using const_reverse_iterator = BBPtrHolder::const_reverse_iterator; - MeFunction(MIRModule *mod, MIRFunction *func, MemPool *memPool, StackMemPool &funcStackMP, MemPool *versMemPool, const std::string &fileName) : memPool(memPool), @@ -169,119 +155,14 @@ class MeFunction : public FuncEmit { versAlloc(versMemPool), mirModule(*mod), mirFunc(func), - labelBBIdMap(alloc.Adapter()), - bbVec(alloc.Adapter()), laidOutBBVec(alloc.Adapter()), - bbTryNodeMap(alloc.Adapter()), - endTryBB2TryBB(alloc.Adapter()), sccTopologicalVec(alloc.Adapter()), - sccOfBB(GetAllBBs().size(), nullptr, alloc.Adapter()), + sccOfBB(alloc.Adapter()), backEdges(alloc.Adapter()), fileName(fileName, memPool) {} ~MeFunction() override = default; - iterator begin() { - return bbVec.begin(); - } - const_iterator begin() const { - return bbVec.begin(); - } - const_iterator cbegin() const { - return bbVec.cbegin(); - } - - iterator end() { - return bbVec.end(); - } - const_iterator end() const { - return bbVec.end(); - } - const_iterator cend() const { - return bbVec.cend(); - } - - reverse_iterator rbegin() { - return bbVec.rbegin(); - } - const_reverse_iterator rbegin() const { - return bbVec.rbegin(); - } - const_reverse_iterator crbegin() const { - return bbVec.crbegin(); - } - - reverse_iterator rend() { - return bbVec.rend(); - } - const_reverse_iterator rend() const { - return bbVec.rend(); - } - const_reverse_iterator crend() const { - return bbVec.crend(); - } - - reference front() { - return bbVec.front(); - } - - reference back() { - return bbVec.back(); - } - - const_reference front() const { - return bbVec.front(); - } - - const_reference back() const { - return bbVec.back(); - } - - bool empty() const { - return bbVec.empty(); - } - - size_t size() const { - return bbVec.size(); - } - - FilterIterator valid_begin() const { - return build_filter_iterator(begin(), std::bind(FilterNullPtr, std::placeholders::_1, end())); - } - - FilterIterator valid_end() const { - return build_filter_iterator(end()); - } - - FilterIterator valid_rbegin() const { - return build_filter_iterator(rbegin(), - std::bind(FilterNullPtr, std::placeholders::_1, rend())); - } - - FilterIterator valid_rend() const { - return build_filter_iterator(rend()); - } - - const_iterator common_entry() const { - return begin(); - } - - const_iterator context_begin() const { - return ++(++begin()); - } - - const_iterator context_end() const { - return end(); - } - - const_iterator common_exit() const { - return ++begin(); - } - - uint32 NumBBs() const { - return nextBBId; - } - void DumpFunction() const; void DumpFunctionNoSSA() const; void DumpMayDUFunction() const; @@ -296,51 +177,35 @@ class MeFunction : public FuncEmit { return meSSATab->GetVerSt(veridx); } - BB *NewBasicBlock(); - BB &InsertNewBasicBlock(const BB &position, bool isInsertBefore = true); - void DeleteBasicBlock(const BB &bb); - BB *NextBB(const BB *bb); - BB *PrevBB(const BB *bb); /* get or create label for bb */ LabelIdx GetOrCreateBBLabel(BB &bb); - /* clone stmtnodes from orig to newBB */ - void CloneBasicBlock(BB &newBB, const BB &orig); - BB &SplitBB(BB &bb, StmtNode &splitPoint, BB *newBB = nullptr); + + bool IsEmpty() const { return mirFunc->IsEmpty(); } bool HasException() const { return hasEH; } - void SetSecondPass() { - secondPass = true; - } - - bool IsSecondPass() const { - return secondPass; - } - MapleAllocator &GetAlloc() { return alloc; } - - const MapleUnorderedMap &GetLabelBBIdMap() const { - return labelBBIdMap; + MapleAllocator &GetVersAlloc() { + return versAlloc; } - BB *GetLabelBBAt(LabelIdx idx) { - auto it = labelBBIdMap.find(idx); - if (it != labelBBIdMap.end()) { - return it->second; + MemPool *GetVersMp() { + // version mempool may be release if invalid + if (versMemPool == nullptr) { + versMemPool = new ThreadLocalMemPool(memPoolCtrler, "verst mempool"); + versAlloc.SetMemPool(versMemPool); } - return nullptr; - } - void SetLabelBBAt(LabelIdx idx, BB *bb) { - labelBBIdMap[idx] = bb; - } - void EraseLabelBBAt(LabelIdx idx) { - labelBBIdMap.erase(idx); + return versMemPool; } - MapleVector &GetAllBBs() { - return bbVec; + void ReleaseVersMemory() { + if (versMemPool != nullptr) { + delete versMemPool; + versMemPool = nullptr; + versAlloc.SetMemPool(nullptr); + } } const MapleVector &GetLaidOutBBs() const { @@ -362,16 +227,6 @@ class MeFunction : public FuncEmit { laidOutBBVec.clear(); } - BB *GetBBFromID(BBId bbID) { - ASSERT(bbID < bbVec.size(), "array index out of range"); - return bbVec.at(bbID); - } - - void NullifyBBByID(BBId bbID) { - ASSERT(bbID < bbVec.size(), "array index out of range"); - bbVec.at(bbID) = nullptr; - } - SSATab *GetMeSSATab() { return meSSATab; } @@ -383,22 +238,6 @@ class MeFunction : public FuncEmit { return mirFunc; } - BB *GetCommonEntryBB() { - return *common_entry(); - } - - BB *GetCommonExitBB() { - return *common_exit(); - } - - BB *GetFirstBB() { - return *(++(++valid_begin())); - } - - BB *GetLastBB() { - return *valid_rbegin(); - } - MIRModule &GetMIRModule() const { return mirModule; } @@ -411,28 +250,7 @@ class MeFunction : public FuncEmit { irmap = currIRMap; } - void SetBBTryNodeMap(BB &bb, StmtNode &tryStmt) { - bbTryNodeMap[&bb] = &tryStmt; - } - - const MapleUnorderedMap &GetBBTryNodeMap() const { - return bbTryNodeMap; - } - - const MapleUnorderedMap &GetEndTryBB2TryBB() const { - return endTryBB2TryBB; - } - - const BB *GetTryBBFromEndTryBB(BB *endTryBB) const { - auto it = endTryBB2TryBB.find(endTryBB); - return it == endTryBB2TryBB.end() ? nullptr : it->second; - } - - void SetTryBBByOtherEndTryBB(BB *endTryBB, BB *otherTryBB) { - endTryBB2TryBB[endTryBB] = endTryBB2TryBB[otherTryBB]; - } - - MeCFG *GetTheCfg() { + MeCFG *GetCfg() const { return theCFG; } @@ -440,32 +258,6 @@ class MeFunction : public FuncEmit { theCFG = currTheCfg; } - bool GetSecondPass() const { - return secondPass; - } - - MemPool *GetVersMp() { - return versMemPool; - } - - void ReleaseVersMemory() { - if (versMemPool != nullptr) { - delete versMemPool; - versMemPool = nullptr; - versAlloc.SetMemPool(nullptr); - } - } - - void SetNextBBId(uint32 currNextBBId) { - nextBBId = currNextBBId; - } - uint32 GetNextBBId() const { - return nextBBId; - } - void DecNextBBId() { - --nextBBId; - } - uint32 GetRegNum() const { return regNum; } @@ -517,24 +309,21 @@ class MeFunction : public FuncEmit { frequency = f; } - void PartialInit(bool isSecondPass); + void PartialInit(); const MapleVector &GetSccTopologicalVec() const { return sccTopologicalVec; } void BBTopologicalSort(SCCOfBBs &scc); void BuildSCC(); + MIRFunction *CurFunction() const { + return mirModule.CurFunction(); + } 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(); - } - void SplitBBPhysically(BB &bb, StmtNode &splitPoint, BB &newBB); MemPool *memPool; StackMemPool &stackMP; @@ -543,16 +332,11 @@ class MeFunction : public FuncEmit { MapleAllocator versAlloc; MIRModule &mirModule; MIRFunction *mirFunc; - uint32 nextBBId = 0; /* mempool */ - MapleUnorderedMap labelBBIdMap; - BBPtrHolder bbVec; MapleVector laidOutBBVec; MeCFG *theCFG = nullptr; SSATab *meSSATab = nullptr; MeIRMap *irmap = nullptr; - MapleUnorderedMap bbTryNodeMap; // maps isTry bb to its try stmt - MapleUnorderedMap endTryBB2TryBB; // maps endtry bb to its try bb // BB SCC MapleVector sccTopologicalVec; uint32 numOfSCCs = 0; @@ -563,7 +347,6 @@ class MeFunction : public FuncEmit { uint32 regNum = 0; // count virtual registers uint32 hints = 0; bool hasEH = false; /* current has try statement */ - bool secondPass = false; // second pass for the same function bool profValid = false; IRProfileDesc *profileDesc = nullptr; uint32 frequency = 0; diff --git a/src/mapleall/maple_me/include/me_gc_lowering.h b/src/mapleall/maple_me/include/me_gc_lowering.h index e8667c134a767a44547f7dc5d0b0b2a44a335656..cfd1ecdbc8b2decd44c36d3196a90bac1594ac2e 100644 --- a/src/mapleall/maple_me/include/me_gc_lowering.h +++ b/src/mapleall/maple_me/include/me_gc_lowering.h @@ -18,6 +18,7 @@ #include "me_irmap.h" #include "me_phase.h" #include "mir_builder.h" +#include "me_cfg.h" namespace maple { class GCLowering { @@ -25,6 +26,7 @@ class GCLowering { GCLowering(MeFunction &f, bool enabledDebug) : func(f), mirModule(f.GetMIRModule()), + cfg(f.GetCfg()), irMap(*f.GetIRMap()), ssaTab(*f.GetMeSSATab()), enabledDebug(enabledDebug) {} @@ -52,6 +54,7 @@ class GCLowering { MeFunction &func; MIRModule &mirModule; + MeCFG *cfg; IRMap &irMap; SSATab &ssaTab; bool isReferent = false; diff --git a/src/mapleall/maple_me/include/me_gc_write_barrier_opt.h b/src/mapleall/maple_me/include/me_gc_write_barrier_opt.h index 3d8ec9fe0419191b939640a12bfa2581ea8d68b0..cabee25996b62d73b0c27d130c9b10cf553cc4bb 100644 --- a/src/mapleall/maple_me/include/me_gc_write_barrier_opt.h +++ b/src/mapleall/maple_me/include/me_gc_write_barrier_opt.h @@ -26,8 +26,8 @@ class GCWriteBarrierOpt { irMap(*f.GetIRMap()), ssaTab(*f.GetMeSSATab()), dominance(dom), - callBBs(f.GetAllBBs().size(), false), - visited(f.GetAllBBs().size(), false), + callBBs(0, false), + visited(0, false), enabledDebug(enabledDebug) {} ~GCWriteBarrierOpt() = default; diff --git a/src/mapleall/maple_me/include/me_hdse.h b/src/mapleall/maple_me/include/me_hdse.h index 90e521ad2dd709fcd8372593402b6e6b52eca572..a8434b1b636dba988b5d4d5156144721301f4206 100644 --- a/src/mapleall/maple_me/include/me_hdse.h +++ b/src/mapleall/maple_me/include/me_hdse.h @@ -26,7 +26,7 @@ namespace maple { class MeHDSE : public HDSE { public: MeHDSE(MeFunction &f, Dominance &pDom, IRMap &map, bool enabledDebug) - : HDSE(f.GetMIRModule(), f.GetAllBBs(), *f.GetCommonEntryBB(), *f.GetCommonExitBB(), + : HDSE(f.GetMIRModule(), f.GetCfg()->GetAllBBs(), *f.GetCfg()->GetCommonEntryBB(), *f.GetCfg()->GetCommonExitBB(), pDom, map, enabledDebug, MeOption::decoupleStatic) {} virtual ~MeHDSE() = default; diff --git a/src/mapleall/maple_me/include/me_irmap.h b/src/mapleall/maple_me/include/me_irmap.h index f15232655bfacc3446df055fd63a6b9473c0ab99..aa3f0406bebee49492b6c2736143c4a4ef2dea5c 100644 --- a/src/mapleall/maple_me/include/me_irmap.h +++ b/src/mapleall/maple_me/include/me_irmap.h @@ -19,24 +19,25 @@ #include "ssa_tab.h" #include "me_function.h" #include "irmap.h" +#include "me_cfg.h" namespace maple { class MeIRMap : public IRMap { public: static const uint32 kHmapHashLength = 5107; MeIRMap(MeFunction &f, MemPool &memPool) - : IRMap(*f.GetMeSSATab(), memPool, kHmapHashLength), func(f) { + : IRMap(*f.GetMeSSATab(), memPool, kHmapHashLength), func(f), cfg(f.GetCfg()) { SetDumpStmtNum(MeOption::stmtNum); } ~MeIRMap() = default; BB *GetBB(BBId id) override { - return func.GetBBFromID(id); + return cfg->GetBBFromID(id); } BB *GetBBForLabIdx(LabelIdx lidx, PUIdx) override { - return func.GetLabelBBAt(lidx); + return cfg->GetLabelBBAt(lidx); } void Dump() override; @@ -47,6 +48,7 @@ class MeIRMap : public IRMap { private: MeFunction &func; + MeCFG *cfg; }; } // namespace maple #endif // MAPLE_ME_INCLUDE_ME_IRMAP_H diff --git a/src/mapleall/maple_me/include/me_loop_analysis.h b/src/mapleall/maple_me/include/me_loop_analysis.h index 8a8c0778b4c12754c91a7b9d47074c6286abb7e1..1cdb243eadbe825f0fe4e8dd36eeb36ac0450d2e 100644 --- a/src/mapleall/maple_me/include/me_loop_analysis.h +++ b/src/mapleall/maple_me/include/me_loop_analysis.h @@ -19,6 +19,7 @@ #include "me_phase.h" #include "dominance.h" #include +#include "me_cfg.h" namespace maple { class IdentifyLoops; @@ -111,9 +112,10 @@ class IdentifyLoops : public AnalysisResult { meLoopMemPool(memPool), meLoopAlloc(memPool), func(mf), + cfg(mf.GetCfg()), dominance(dm), meLoops(meLoopAlloc.Adapter()), - bbLoopParent(func.GetAllBBs().size(), nullptr, meLoopAlloc.Adapter()) {} + bbLoopParent(func.GetCfg()->GetAllBBs().size(), nullptr, meLoopAlloc.Adapter()) {} virtual ~IdentifyLoops() = default; @@ -139,6 +141,7 @@ class IdentifyLoops : public AnalysisResult { MemPool *meLoopMemPool; MapleAllocator meLoopAlloc; MeFunction &func; + MeCFG *cfg; Dominance *dominance; MapleVector meLoops; MapleVector bbLoopParent; // gives closest nesting loop for each bb diff --git a/src/mapleall/maple_me/include/me_loop_unrolling.h b/src/mapleall/maple_me/include/me_loop_unrolling.h index ad9bf43ea12839efcc1e94476730664b57cfaba1..06749b5752f512c8a01ad81441027d546d76a2b6 100644 --- a/src/mapleall/maple_me/include/me_loop_unrolling.h +++ b/src/mapleall/maple_me/include/me_loop_unrolling.h @@ -23,6 +23,7 @@ #include "me_dominance.h" #include "me_loop_analysis.h" #include "profile.h" +#include "me_cfg.h" namespace maple { constexpr uint32 kMaxCost = 100; @@ -30,7 +31,7 @@ constexpr uint8 unrollTimes[3] = { 8, 4, 2 }; // unrollTimes class LoopUnrolling { public: LoopUnrolling(MeFunction &f, MeFuncResultMgr &m, LoopDesc &l, MeIRMap &map, MemPool &pool, Dominance *d) - : func(&f), mgr(&m), loop(&l), irMap(&map), memPool(&pool), mpAllocator(&pool), dom(d), + : func(&f), cfg(f.GetCfg()), mgr(&m), loop(&l), irMap(&map), memPool(&pool), mpAllocator(&pool), dom(d), cands((std::less(), mpAllocator.Adapter())), lastNew2OldBB(mpAllocator.Adapter()), profValid(func->IsIRProfValid()) {} ~LoopUnrolling() = default; @@ -85,6 +86,7 @@ class LoopUnrolling { bool canUnroll = true; MeFunction *func; + MeCFG *cfg; MeFuncResultMgr *mgr; LoopDesc *loop; MeIRMap *irMap; @@ -128,4 +130,4 @@ class MeDoLoopUnrolling : public MeFuncPhase { LoopUnrolling &loopUnrolling); }; } // namespace maple -#endif // MAPLE_ME_INCLUDE_LOOP_UNROLLING_H \ No newline at end of file +#endif // MAPLE_ME_INCLUDE_LOOP_UNROLLING_H diff --git a/src/mapleall/maple_me/include/me_pgo_instrument.h b/src/mapleall/maple_me/include/me_pgo_instrument.h index cc1e460568ddc2f7d8083ff7b2240b958d744b49..16ca3d7c438183c151a697834a4fa9a88c67b2ff 100644 --- a/src/mapleall/maple_me/include/me_pgo_instrument.h +++ b/src/mapleall/maple_me/include/me_pgo_instrument.h @@ -98,7 +98,7 @@ class PGOInstrument { BB *dest = edge->GetDestBB(); /* instrument the srcbb if it has a single succ */ if (src->GetSucc().size() <= 1) { - if (src == func->GetCommonEntryBB()) { + if (src == func->GetCfg()->GetCommonEntryBB()) { bbs.push_back(dest); } else { bbs.push_back(src); @@ -106,7 +106,7 @@ class PGOInstrument { } else if (!edge->IsCritical()) { bbs.push_back(dest); } else { - func->GetTheCfg()->DumpToFile("profGenError", false); + func->GetCfg()->DumpToFile("profGenError", false); CHECK_FATAL(false, "impossible critial edge %d -> %d", src->UintID(), dest->UintID()); } } @@ -119,11 +119,11 @@ class PGOInstrument { uint64 ComputeFuncHash() { uint64 allEdgeSize = mst.GetAllEdgesSize(); std::ostringstream ss; - auto eIt = func->valid_end(); + auto eIt = func->GetCfg()->valid_end(); // compute func CFG hash,used to verify function IR change - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + for (auto bIt = func->GetCfg()->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; - if (bIt == func->common_exit()) { + if (bIt == func->GetCfg()->common_exit()) { continue; } for (auto *succBB : bb->GetSucc()) { @@ -135,8 +135,8 @@ class PGOInstrument { } void ClearBBGroupInfo() { - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + auto eIt = func->GetCfg()->valid_end(); + for (auto bIt = func->GetCfg()->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->ClearGroup(); } diff --git a/src/mapleall/maple_me/include/me_phase.h b/src/mapleall/maple_me/include/me_phase.h index d902b02d273ebd05f7e15a37c1ee81fc9b485409..4e9431911c7e5a87b1faae88a7d72c57af11bbae 100644 --- a/src/mapleall/maple_me/include/me_phase.h +++ b/src/mapleall/maple_me/include/me_phase.h @@ -58,18 +58,6 @@ class MeFuncPhase : public Phase { virtual std::string PhaseName() const = 0; - void SetChangeCFG() { - isCFGChanged = true; - } - - bool IsChangedCFG() const { - return isCFGChanged; - } - - void ClearChangeCFG() { - isCFGChanged = false; - } - void ClearString() { prevPhaseName.clear(); prevPhaseName.shrink_to_fit(); @@ -78,7 +66,6 @@ class MeFuncPhase : public Phase { private: MePhaseID phaseID; std::string prevPhaseName = ""; // used in filename for emit, init prev_phasename as nullptr - bool isCFGChanged = false; // is this phase changed CFG }; } // namespace maple #endif // MAPLE_ME_INCLUDE_ME_PHASE_H diff --git a/src/mapleall/maple_me/include/me_phases.def b/src/mapleall/maple_me/include/me_phases.def index 25e63036456f6e447e13159a8a9a064ad1bbed34..c625af60c7c8621f1060c3b410105365dfa25343 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) +FUNCAPHASE(MeFuncPhase_MECFG, MeDoMeCfg) #if MIR_JAVA FUNCTPHASE(MeFuncPhase_SYNCSELECT, MeDoSyncSelect) #endif diff --git a/src/mapleall/maple_me/include/me_placement_rc.h b/src/mapleall/maple_me/include/me_placement_rc.h index 51ea6b5cdb8e4354edec323eba85e3f695af60c8..ad329843d7b423b94690b8700c81990164be5668 100644 --- a/src/mapleall/maple_me/include/me_placement_rc.h +++ b/src/mapleall/maple_me/include/me_placement_rc.h @@ -21,7 +21,7 @@ class PlacementRC : public MeSSUPre { public: PlacementRC(MeFunction &f, Dominance &dom, MemPool &memPool, bool enabledDebug) : MeSSUPre(f, dom, memPool, kDecrefPre, enabledDebug), placementRCTemp(nullptr), - bbHasReal(f.GetAllBBs().size(), false, spreAllocator.Adapter()) {} + bbHasReal(f.GetCfg()->GetAllBBs().size(), false, spreAllocator.Adapter()) {} virtual ~PlacementRC() = default; diff --git a/src/mapleall/maple_me/include/me_predict.h b/src/mapleall/maple_me/include/me_predict.h index 07b99b9df526c018390a64cda06a70514b7cc52e..5d7c45dbea700cbabf67511a04c29e417715bb8d 100644 --- a/src/mapleall/maple_me/include/me_predict.h +++ b/src/mapleall/maple_me/include/me_predict.h @@ -64,6 +64,7 @@ class MePrediction : public AnalysisResult { mePredAlloc(&memPool), tmpAlloc(&tmpPool), func(&mf), + cfg(mf.GetCfg()), dom(&dom), meLoop(&loops), hMap(&map), @@ -99,6 +100,7 @@ class MePrediction : public AnalysisResult { MapleAllocator mePredAlloc; MapleAllocator tmpAlloc; MeFunction *func; + MeCFG *cfg; Dominance *dom; IdentifyLoops *meLoop; MeIRMap *hMap; diff --git a/src/mapleall/maple_me/include/me_prop.h b/src/mapleall/maple_me/include/me_prop.h index bc575ec23ecbded17212568008bd6948afda5298..f67f3971295b288fa73c3362d0bfa3860151f77c 100644 --- a/src/mapleall/maple_me/include/me_prop.h +++ b/src/mapleall/maple_me/include/me_prop.h @@ -23,7 +23,7 @@ namespace maple { class MeProp : public Prop { public: MeProp(MeIRMap &irMap, Dominance &dom, MemPool &memPool, const PropConfig &config) - : Prop(irMap, dom, memPool, irMap.GetFunc().GetAllBBs().size(), config), + : Prop(irMap, dom, memPool, irMap.GetFunc().GetCfg()->GetAllBBs().size(), config), func(&irMap.GetFunc()) {} virtual ~MeProp() = default; @@ -31,7 +31,7 @@ class MeProp : public Prop { MeFunction *func; BB *GetBB(BBId id) { - return func->GetAllBBs()[id]; + return func->GetCfg()->GetAllBBs()[id]; } }; diff --git a/src/mapleall/maple_me/include/me_rc_lowering.h b/src/mapleall/maple_me/include/me_rc_lowering.h index dfcdaff43f2271c31187129cb030b06da53cc3ef..82f8f359195092aa3b0bc86b5fd650b588067a6f 100644 --- a/src/mapleall/maple_me/include/me_rc_lowering.h +++ b/src/mapleall/maple_me/include/me_rc_lowering.h @@ -19,6 +19,7 @@ #include "me_irmap.h" #include "me_phase.h" #include "mir_builder.h" +#include "me_cfg.h" namespace maple { class RCLowering { @@ -26,6 +27,7 @@ class RCLowering { RCLowering(MeFunction &f, KlassHierarchy &kh, bool enabledDebug) : func(f), mirModule(f.GetMIRModule()), + cfg(f.GetCfg()), irMap(*f.GetIRMap()), ssaTab(*f.GetMeSSATab()), klassHierarchy(kh), @@ -119,6 +121,7 @@ class RCLowering { void CheckRefReturn(BB &bb); MeFunction &func; MIRModule &mirModule; + MeCFG *cfg; IRMap &irMap; SSATab &ssaTab; KlassHierarchy &klassHierarchy; diff --git a/src/mapleall/maple_me/include/me_ssa.h b/src/mapleall/maple_me/include/me_ssa.h index 0cad346c686cede5f007a44d7671b37a43959de9..cccfefd4ab2f80612c8de3953d1966128c07249e 100644 --- a/src/mapleall/maple_me/include/me_ssa.h +++ b/src/mapleall/maple_me/include/me_ssa.h @@ -17,6 +17,7 @@ #include "mir_module.h" #include "mir_nodes.h" #include "me_function.h" +#include "me_cfg.h" #include "me_phase.h" #include "ssa.h" #include "dominance.h" @@ -25,7 +26,7 @@ namespace maple { class MeSSA : public SSA, public AnalysisResult { public: MeSSA(MeFunction &func, SSATab *stab, Dominance &dom, MemPool &memPool) - : SSA(memPool, *stab, func.GetAllBBs(), &dom), + : SSA(memPool, *stab, func.GetCfg()->GetAllBBs(), &dom), AnalysisResult(&memPool), func(&func) {} diff --git a/src/mapleall/maple_me/include/me_ssa_devirtual.h b/src/mapleall/maple_me/include/me_ssa_devirtual.h index abf25aaa2796b850168629da555eb5c98bc1533e..f2d90f71b3c3e91f16aa1ba494426f6bbf916f70 100644 --- a/src/mapleall/maple_me/include/me_ssa_devirtual.h +++ b/src/mapleall/maple_me/include/me_ssa_devirtual.h @@ -23,16 +23,16 @@ class MeSSADevirtual : public SSADevirtual { public: MeSSADevirtual(MemPool &memPool, MIRModule &mod, MeFunction &func, IRMap &irMap, KlassHierarchy &kh, Dominance &dom, bool skipReturnTypeOpt) - : SSADevirtual(memPool, mod, irMap, kh, dom, func.GetAllBBs().size(), skipReturnTypeOpt), func(&func) {} + : SSADevirtual(memPool, mod, irMap, kh, dom, func.GetCfg()->GetAllBBs().size(), skipReturnTypeOpt), func(&func) {} MeSSADevirtual(MemPool &memPool, MIRModule &mod, MeFunction &func, IRMap &irMap, KlassHierarchy &kh, Dominance &dom, Clone &clone, bool skipReturnTypeOpt) - : SSADevirtual(memPool, mod, irMap, kh, dom, func.GetAllBBs().size(), clone, skipReturnTypeOpt), func(&func) {} + : SSADevirtual(memPool, mod, irMap, kh, dom, func.GetCfg()->GetAllBBs().size(), clone, skipReturnTypeOpt), func(&func) {} ~MeSSADevirtual() = default; protected: BB *GetBB(BBId id) const override { - return func->GetAllBBs().at(id); + return func->GetCfg()->GetAllBBs().at(id); } MIRFunction *GetMIRFunction() const override { diff --git a/src/mapleall/maple_me/include/me_ssa_epre.h b/src/mapleall/maple_me/include/me_ssa_epre.h index c00e0ee978cbbc6652612330c5b29068102b3a4b..e862acc0c73ff236abb8012f0213550a1fa8ba98 100644 --- a/src/mapleall/maple_me/include/me_ssa_epre.h +++ b/src/mapleall/maple_me/include/me_ssa_epre.h @@ -53,7 +53,7 @@ class MeSSAEPre : public SSAEPre { void BuildWorkList() override; bool IsThreadObjField(const IvarMeExpr &expr) const override; BB *GetBB(BBId id) const override { - return func->GetBBFromID(id); + return func->GetCfg()->GetBBFromID(id); } PUIdx GetPUIdx() const override { @@ -61,7 +61,7 @@ class MeSSAEPre : public SSAEPre { } bool CfgHasDoWhile() const override { - return func->GetTheCfg()->GetHasDoWhile(); + return func->GetCfg()->GetHasDoWhile(); } bool EpreLocalRefVar() const override { diff --git a/src/mapleall/maple_me/include/me_ssa_lpre.h b/src/mapleall/maple_me/include/me_ssa_lpre.h index cc5ddd3d7946881511a4d6abb87adad4c0be99c3..7dfd8f0e6f857e3bffd6a510a9539744e1088abe 100644 --- a/src/mapleall/maple_me/include/me_ssa_lpre.h +++ b/src/mapleall/maple_me/include/me_ssa_lpre.h @@ -62,7 +62,7 @@ class MeSSALPre : public SSAPre { void BuildWorkListExpr(MeStmt&, int32, MeExpr&, bool, MeExpr*, bool isRootExpr) override; void BuildWorkList() override; BB *GetBB(BBId id) const override { - return func->GetBBFromID(id); + return func->GetCfg()->GetBBFromID(id); } PUIdx GetPUIdx() const override { diff --git a/src/mapleall/maple_me/include/me_ssi.h b/src/mapleall/maple_me/include/me_ssi.h index 221e4ea229348177df4839a2c9ea5edf405d89ef..b6f68dacb63a2755d762ec3b1bc75c2447225b6b 100644 --- a/src/mapleall/maple_me/include/me_ssi.h +++ b/src/mapleall/maple_me/include/me_ssi.h @@ -179,7 +179,7 @@ class MeSSI { MeSSI(MeFunction &meFunction, Dominance &dom, MeIRMap &map, MemPool &pool, std::map* acs = nullptr, std::map>* cBB = nullptr) - : meFunc(&meFunction), + : cfg(meFunction.GetCfg()), dom(&dom), irMap(&map), memPool(&pool), @@ -235,7 +235,7 @@ class MeSSI { return irMap; } - MeFunction *meFunc; + MeCFG *cfg; Dominance *dom; MeIRMap *irMap; MemPool *memPool; diff --git a/src/mapleall/maple_me/include/me_ssu_pre.h b/src/mapleall/maple_me/include/me_ssu_pre.h index d6686f00e77a75bd53a23315894c9768677e5809..c7f40c8e67d8b30d6bbde5e5e53cf49c473b2414 100644 --- a/src/mapleall/maple_me/include/me_ssu_pre.h +++ b/src/mapleall/maple_me/include/me_ssu_pre.h @@ -349,6 +349,7 @@ class MeSSUPre { MeSSUPre(MeFunction &f, Dominance &dom, MemPool &memPool, PreKind kind, bool enabledDebug) : preKind(kind), func(&f), + cfg(f.GetCfg()), ssaTab(f.GetMeSSATab()), irMap(f.GetIRMap()), mirModule(&f.GetMeSSATab()->GetModule()), @@ -407,6 +408,7 @@ class MeSSUPre { virtual void CreateEmptyCleanupIntrinsics() {} MeFunction *func; + MeCFG *cfg; SSATab *ssaTab; MeIRMap *irMap; MIRModule *mirModule; diff --git a/src/mapleall/maple_me/include/me_stmt_pre.h b/src/mapleall/maple_me/include/me_stmt_pre.h index 05a091f86d58e5b56d3596c607a7c94222aa1fd0..452dc730e3b769c038819445de730a661f4b5694 100644 --- a/src/mapleall/maple_me/include/me_stmt_pre.h +++ b/src/mapleall/maple_me/include/me_stmt_pre.h @@ -76,7 +76,7 @@ class MeStmtPre : public SSAEPre { void RemoveUnnecessaryDassign(DassignMeStmt &dssMeStmt); void DoSSAFRE() override; BB *GetBB(BBId id) const override { - return func->GetBBFromID(id); + return func->GetCfg()->GetBBFromID(id); } PUIdx GetPUIdx() const override { diff --git a/src/mapleall/maple_me/include/me_subsum_rc.h b/src/mapleall/maple_me/include/me_subsum_rc.h index 2e3d9e969b4b41bc6e26034ebe52f187f3e5b5f6..0ac400db8e6e99a5702b5db576361237fd51c2bb 100644 --- a/src/mapleall/maple_me/include/me_subsum_rc.h +++ b/src/mapleall/maple_me/include/me_subsum_rc.h @@ -34,7 +34,7 @@ class SubsumRC : public MeSSUPre { SubsumRC(MeFunction &f, Dominance &dom, MemPool &mp, bool enabledDebug) : MeSSUPre(f, dom, mp, kSubsumePre, enabledDebug), candMap(spreAllocator.Adapter()), - bbVisited(f.GetAllBBs().size(), false, spreAllocator.Adapter()), + bbVisited(f.GetCfg()->GetAllBBs().size(), false, spreAllocator.Adapter()), verstCantSubsum(f.GetIRMap()->GetVerst2MeExprTable().size(), false, spreAllocator.Adapter()) {} virtual ~SubsumRC() = default; diff --git a/src/mapleall/maple_me/src/ipa_side_effect.cpp b/src/mapleall/maple_me/src/ipa_side_effect.cpp index 68dc662be8bd74f8fb155fb22c47a81577eb910e..bf976c238c12d9abb71a501aa16731999daec221 100644 --- a/src/mapleall/maple_me/src/ipa_side_effect.cpp +++ b/src/mapleall/maple_me/src/ipa_side_effect.cpp @@ -1031,6 +1031,7 @@ void IpaSideEffect::DoAnalysis() { std::set argExprs; std::set nextLevelGlobalExprs; std::set nextLevelArgExprs; + auto cfg = meFunc.GetCfg(); for (size_t i = 0; i < meFunc.GetSccTopologicalVec().size(); ++i) { SCCOfBBs *scc = meFunc.GetSccTopologicalVec()[i]; CHECK_FATAL(scc != nullptr, "scc must not be null"); @@ -1041,7 +1042,7 @@ void IpaSideEffect::DoAnalysis() { unsigned loopCount = scc->GetBBs().size() > 1 ? maxLoopCount : 1; // Loop count for (unsigned j = 0; j < loopCount; ++j) { for (BB *bb : scc->GetBBs()) { - if (bb == meFunc.GetCommonEntryBB() || bb == meFunc.GetCommonExitBB()) { + if (bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB()) { continue; } for (auto &meStmt : bb->GetMeStmts()) { diff --git a/src/mapleall/maple_me/src/me_abco.cpp b/src/mapleall/maple_me/src/me_abco.cpp index 45286eb0a7875a8880c389db85390ebc7f7f5d61..fc81f1802013126c196be9bc45dd4d460bced098 100644 --- a/src/mapleall/maple_me/src/me_abco.cpp +++ b/src/mapleall/maple_me/src/me_abco.cpp @@ -55,8 +55,9 @@ void MeABC::ABCCollectArrayExpr(MeStmt &meStmt, MeExpr &meExpr, bool isUpdate) { } bool MeABC::CollectABC() { - auto eIt = meFunc->valid_end(); - for (auto bIt = meFunc->valid_begin(); bIt != eIt; ++bIt) { + auto cfg = meFunc->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { for (auto &meStmt : (*bIt)->GetMeStmts()) { if (meStmt.GetOp() == OP_callassigned) { auto *callNode = static_cast(&meStmt); @@ -1067,7 +1068,7 @@ void MeABC::ExecuteABCO() { *((static_cast(pair.second))->GetOpnd(1))); BuildInequalityGraph(); if (MeABC::isDebug) { - meFunc->GetTheCfg()->DumpToFile(meFunc->GetName()); + meFunc->GetCfg()->DumpToFile(meFunc->GetName()); inequalityGraph->DumpDotFile(DumpType::kDumpUpperAndNone); inequalityGraph->DumpDotFile(DumpType::kDumpLowerAndNone); } @@ -1080,9 +1081,6 @@ void MeABC::ExecuteABCO() { } AnalysisResult *MeDoABCOpt::Run(MeFunction *func, MeFuncResultMgr *frm, ModuleResultMgr*) { - if (func->GetSecondPass()) { - return nullptr; - } CHECK_FATAL(frm != nullptr, "frm is nullptr"); auto *dom = static_cast(frm->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); CHECK_FATAL(dom != nullptr, "dominance phase has problem"); diff --git a/src/mapleall/maple_me/src/me_alias_class.cpp b/src/mapleall/maple_me/src/me_alias_class.cpp index 29d0ddeee45fdc763efb97c8d2684434c6360760..53fe58caef15396ca47e2a01e7ae38d8e7952bc4 100644 --- a/src/mapleall/maple_me/src/me_alias_class.cpp +++ b/src/mapleall/maple_me/src/me_alias_class.cpp @@ -24,7 +24,7 @@ namespace maple { // This phase performs alias analysis based on Steensgaard's algorithm and // represent the resulting alias relationships in the Maple IR representation bool MeAliasClass::HasWriteToStaticFinal() const { - for (auto bIt = func.valid_begin(); bIt != func.valid_end(); ++bIt) { + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { for (const auto &stmt : (*bIt)->GetStmtNodes()) { if (stmt.GetOpCode() == OP_dassign) { const auto &dassignNode = static_cast(stmt); @@ -42,7 +42,7 @@ bool MeAliasClass::HasWriteToStaticFinal() const { void MeAliasClass::DoAliasAnalysis() { // pass 1 through the program statements - for (auto bIt = func.valid_begin(); bIt != func.valid_end(); ++bIt) { + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { for (auto &stmt : (*bIt)->GetStmtNodes()) { ApplyUnionForCopies(stmt); } @@ -83,7 +83,7 @@ void MeAliasClass::DoAliasAnalysis() { LogInfo::MapleLogger() << "\n============ Alias Classification Pass 2 ============" << '\n'; } - for (auto bIt = func.valid_begin(); bIt != func.valid_end(); ++bIt) { + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetStmtNodes()) { GenericInsertMayDefUse(stmt, bb->GetBBId()); @@ -91,14 +91,17 @@ void MeAliasClass::DoAliasAnalysis() { } } -AnalysisResult *MeDoAliasClass::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr *moduleResMgr) { +AnalysisResult *MeDoAliasClass::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr *mrm) { + // moduleResultMgr store moduleresultMgr and is used for FuncResultMgr::Run + moduleResultMgr = (mrm != nullptr) ? mrm : moduleResultMgr; MPLTimer timer; timer.Start(); (void)funcResMgr->GetAnalysisResult(MeFuncPhase_SSATAB, func); MemPool *aliasClassMp = NewMemPool(); KlassHierarchy *kh = nullptr; if (func->GetMIRModule().IsJavaModule()) { - kh = static_cast(moduleResMgr->GetAnalysisResult(MoPhase_CHA, &func->GetMIRModule())); + CHECK_FATAL(moduleResultMgr != nullptr, "alias class needs module result manager"); + kh = static_cast(moduleResultMgr->GetAnalysisResult(MoPhase_CHA, &func->GetMIRModule())); } MeAliasClass *aliasClass = aliasClassMp->New( *aliasClassMp, func->GetMIRModule(), *func->GetMeSSATab(), *func, MeOption::lessThrowAlias, diff --git a/src/mapleall/maple_me/src/me_analyze_rc.cpp b/src/mapleall/maple_me/src/me_analyze_rc.cpp index e9d5285b4c22116bfe7474d439a72e41c365aa29..d2d302f1e2f3abe242bc0b661b0652d70691ccff 100644 --- a/src/mapleall/maple_me/src/me_analyze_rc.cpp +++ b/src/mapleall/maple_me/src/me_analyze_rc.cpp @@ -143,8 +143,8 @@ bool AnalyzeRC::NeedIncref(const MeStmt &stmt) const { // identify assignments to ref pointers and insert decref before it and incref // after it void AnalyzeRC::IdentifyRCStmts() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto &bb = **bIt; for (auto &stmt : bb.GetMeStmts()) { MeExpr *lhsRef = stmt.GetLHSRef(skipLocalRefVars); @@ -195,7 +195,7 @@ void AnalyzeRC::IdentifyRCStmts() { } void AnalyzeRC::CreateCleanupIntrinsics() { - for (BB *bb : func.GetCommonExitBB()->GetPred()) { + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { auto &meStmts = bb->GetMeStmts(); if (meStmts.empty() || meStmts.back().GetOp() != OP_return) { continue; @@ -260,7 +260,7 @@ void AnalyzeRC::RenameRefPtrs(BB *bb) { ASSERT(bb->GetBBId() < dominance.GetDomChildrenSize(), "index out of range in AnalyzeRC::RenameRefPtrs"); const MapleSet &domChildren = dominance.GetDomChildren(bb->GetBBId()); for (const auto &childBBId : domChildren) { - RenameRefPtrs(func.GetAllBBs().at(childBBId)); + RenameRefPtrs(cfg->GetAllBBs().at(childBBId)); } // restore the stacks to their size at entry to this function invocation for (const auto &mapItem : rcItemsMap) { @@ -299,8 +299,8 @@ UnaryMeStmt *AnalyzeRC::CreateIncrefZeroVersion(OriginalSt &ost) { } void AnalyzeRC::OptimizeRC() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto itStmt = bb->GetMeStmts().begin(); itStmt != bb->GetMeStmts().end(); ++itStmt) { MeStmt *stmt = to_ptr(itStmt); @@ -385,7 +385,7 @@ bool AnalyzeRC::NeedDecRef(const VarMeExpr &var) const { // that are zero version are not live, and can be deleted; if the number of // arguments left are > `kCleanupLocalRefVarsLimit`, delete the intrinsiccall. void AnalyzeRC::RemoveUnneededCleanups() { - for (BB *bb : func.GetCommonExitBB()->GetPred()) { + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { auto &meStmts = bb->GetMeStmts(); if (meStmts.empty() || meStmts.back().GetOp() != OP_return) { continue; @@ -428,7 +428,7 @@ void AnalyzeRC::Run() { if (!skipLocalRefVars) { CreateCleanupIntrinsics(); } - RenameRefPtrs(func.GetCommonEntryBB()); + RenameRefPtrs(cfg->GetCommonEntryBB()); if (MeOption::optLevel > 0 && !skipLocalRefVars) { RemoveUnneededCleanups(); } diff --git a/src/mapleall/maple_me/src/me_analyzector.cpp b/src/mapleall/maple_me/src/me_analyzector.cpp index ecb27376ed07b7bb9223512025e1d669a4a1a38b..3ff5998af5225f27bbac79e6392bfdda2dea0ce7 100644 --- a/src/mapleall/maple_me/src/me_analyzector.cpp +++ b/src/mapleall/maple_me/src/me_analyzector.cpp @@ -25,8 +25,9 @@ void AnalyzeCtor::ProcessFunc() { return; } hasSideEffect = false; + auto cfg = func->GetCfg(); if (curFunc->GetBody() != nullptr) { - for (BB *bb : func->GetAllBBs()) { + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) continue; for (auto &meStmt : bb->GetMeStmts()) { diff --git a/src/mapleall/maple_me/src/me_bb_analyze.cpp b/src/mapleall/maple_me/src/me_bb_analyze.cpp index 0b8ac346605f66d4882ca39b240e16b1fd063e9d..1e18dfef2786b306148d4adf996b6c0139fccdc4 100644 --- a/src/mapleall/maple_me/src/me_bb_analyze.cpp +++ b/src/mapleall/maple_me/src/me_bb_analyze.cpp @@ -17,8 +17,8 @@ namespace maple { void BBAnalyze::SetHotAndColdBBCountThreshold() { std::vector times; - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; times.push_back((bb->GetFrequency())); } @@ -32,12 +32,12 @@ namespace maple { } bool BBAnalyze::CheckBBHot(const BBId bbId) { - BB *bb = func.GetBBFromID(bbId); + BB *bb = cfg->GetBBFromID(bbId); return bb->GetFrequency() >= hotBBCountThreshold; } bool BBAnalyze::CheckBBCold(const BBId bbId) { - BB *bb = func.GetBBFromID(bbId); + BB *bb = cfg->GetBBFromID(bbId); return bb->GetFrequency() <= coldBBCountThreshold; } @@ -58,4 +58,4 @@ namespace maple { } return nullptr; } -} // namespace maple \ No newline at end of file +} // namespace maple diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index 393bae292bcfe2ca9646df11a17c391ac9b884a4..dc9ed153c4067eb6422985cc1d3b259cb9a869ae 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -436,11 +436,11 @@ void BBLayout::ResolveUnconditionalFallThru(BB &bb, BB &nextBB) { void BBLayout::FixEndTryBB(BB &bb) { BBId prevID = bb.GetBBId() - 1UL; for (BBId id = prevID; id != 0; --id) { - auto prevBB = func.GetBBFromID(id); + auto prevBB = func.GetCfg()->GetBBFromID(id); if (prevBB != nullptr) { if (prevBB->GetAttributes(kBBAttrIsTry) && !prevBB->GetAttributes(kBBAttrIsTryEnd)) { prevBB->SetAttributes(kBBAttrIsTryEnd); - func.SetTryBBByOtherEndTryBB(prevBB, &bb); + cfg->SetTryBBByOtherEndTryBB(prevBB, &bb); } break; } @@ -463,15 +463,15 @@ void BBLayout::DealWithStartTryBB() { if (!startTryBBVec[i]) { continue; } - auto curBB = func.GetBBFromID(BBId(i)); + auto curBB = cfg->GetBBFromID(BBId(i)); for (size_t j = i + 1; j < size && !startTryBBVec[j]; ++j) { - auto nextBB = func.GetBBFromID(BBId(j)); + auto nextBB = cfg->GetBBFromID(BBId(j)); if (nextBB != nullptr) { if (nextBB->GetAttributes(kBBAttrIsTry)) { FixTryBB(*curBB, *nextBB); } else { curBB->RemoveAllSucc(); - func.NullifyBBByID(curBB->GetBBId()); + cfg->NullifyBBByID(curBB->GetBBId()); for (auto it = layoutBBs.begin(); it != layoutBBs.end(); ++it) { if (*it == curBB) { layoutBBs.erase(it); @@ -482,7 +482,7 @@ void BBLayout::DealWithStartTryBB() { break; } else if (j == size - 1) { curBB->RemoveAllSucc(); - func.NullifyBBByID(curBB->GetBBId()); + cfg->NullifyBBByID(curBB->GetBBId()); for (auto it = layoutBBs.begin(); it != layoutBBs.end(); ++it) { if (*it == curBB) { layoutBBs.erase(it); @@ -523,7 +523,7 @@ void BBLayout::RemoveUnreachable(BB &bb) { FixEndTryBB(bb); } bb.RemoveAllSucc(); - func.NullifyBBByID(bb.GetBBId()); + cfg->NullifyBBByID(bb.GetBBId()); for (auto it = layoutBBs.begin(); it != layoutBBs.end(); ++it) { if (*it == &bb) { layoutBBs.erase(it); @@ -538,10 +538,10 @@ void BBLayout::UpdateNewBBWithAttrTry(const BB &bb, BB &fallthru) const { fallthru.SetAttributes(kBBAttrIsTry); if (bb.IsReturnBB()) { int i = 1; - tryBB = func.GetBBFromID(bb.GetBBId() - i); + tryBB = cfg->GetBBFromID(bb.GetBBId() - i); while (tryBB == nullptr || tryBB->IsReturnBB()) { ++i; - tryBB = func.GetBBFromID(bb.GetBBId() - i); + tryBB = cfg->GetBBFromID(bb.GetBBId() - i); } ASSERT_NOT_NULL(tryBB); ASSERT(tryBB->GetAttributes(kBBAttrIsTry), "must be try"); @@ -564,7 +564,7 @@ void BBLayout::UpdateNewBBWithAttrTry(const BB &bb, BB &fallthru) const { // fallthru fallthru BB *BBLayout::CreateGotoBBAfterCondBB(BB &bb, BB &fallthru) { ASSERT(bb.GetKind() == kBBCondGoto, "CreateGotoBBAfterCondBB: unexpected BB kind"); - BB *newFallthru = func.NewBasicBlock(); + BB *newFallthru = cfg->NewBasicBlock(); newFallthru->SetAttributes(kBBAttrArtificial); AddLaidOut(false); newFallthru->SetKind(kBBGoto); @@ -616,9 +616,9 @@ void BBLayout::DumpBBPhyOrder() const { } void BBLayout::OptimiseCFG() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -632,7 +632,7 @@ void BBLayout::SetAttrTryForTheCanBeMovedBB(BB &bb, BB &canBeMovedBB) const { if (bb.GetAttributes(kBBAttrIsTryEnd)) { bb.ClearAttributes(kBBAttrIsTryEnd); canBeMovedBB.SetAttributes(kBBAttrIsTryEnd); - func.SetTryBBByOtherEndTryBB(&canBeMovedBB, &bb); + cfg->SetTryBBByOtherEndTryBB(&canBeMovedBB, &bb); } if (bb.GetAttributes(kBBAttrIsTry) && !canBeMovedBB.GetAttributes(kBBAttrIsTry)) { UpdateNewBBWithAttrTry(bb, canBeMovedBB); @@ -640,7 +640,7 @@ void BBLayout::SetAttrTryForTheCanBeMovedBB(BB &bb, BB &canBeMovedBB) const { } void BBLayout::LayoutWithoutProf() { - BB *bb = func.GetFirstBB(); + BB *bb = cfg->GetFirstBB(); while (bb != nullptr) { AddBB(*bb); if (bb->GetKind() == kBBCondGoto || bb->GetKind() == kBBGoto) { @@ -658,8 +658,8 @@ void BBLayout::LayoutWithoutProf() { } ASSERT(!(isTry && GetTryOutstanding()), "cannot emit another try if last try has not been ended"); if (nextBB->GetAttributes(kBBAttrIsTryEnd)) { - ASSERT(func.GetTryBBFromEndTryBB(nextBB) == nextBB || - IsBBLaidOut(func.GetTryBBFromEndTryBB(nextBB)->GetBBId()), + ASSERT(cfg->GetTryBBFromEndTryBB(nextBB) == nextBB || + IsBBLaidOut(cfg->GetTryBBFromEndTryBB(nextBB)->GetBBId()), "cannot emit endtry bb before its corresponding try bb"); } } @@ -779,9 +779,9 @@ void BBLayout::AddBBProf(BB &bb) { } void BBLayout::BuildEdges() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -868,7 +868,7 @@ BB *BBLayout::NextBBProf(BB &bb) { void BBLayout::LayoutWithProf() { OptimiseCFG(); BuildEdges(); - BB *bb = func.GetFirstBB(); + BB *bb = cfg->GetFirstBB(); while (bb != nullptr) { AddBBProf(*bb); bb = NextBBProf(*bb); @@ -895,20 +895,21 @@ void BBLayout::RunLayout() { } AnalysisResult *MeDoBBLayout::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr*) { + MeCFG *cfg = static_cast(funcResMgr->GetAnalysisResult(MeFuncPhase_MECFG, func)); // mempool used in analysisresult MemPool *layoutMp = NewMemPool(); auto *bbLayout = layoutMp->New(*layoutMp, *func, DEBUGFUNC(func)); // assume common_entry_bb is always bb 0 - ASSERT(func->front() == func->GetCommonEntryBB(), "assume bb[0] is the commont entry bb"); + ASSERT(cfg->front() == cfg->GetCommonEntryBB(), "assume bb[0] is the commont entry bb"); if (DEBUGFUNC(func)) { - func->GetTheCfg()->DumpToFile("beforeBBLayout", false); + cfg->DumpToFile("beforeBBLayout", false); } bbLayout->RunLayout(); func->SetLaidOutBBs(bbLayout->GetBBs()); funcResMgr->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, func); if (DEBUGFUNC(func)) { bbLayout->DumpBBPhyOrder(); - func->GetTheCfg()->DumpToFile("afterBBLayout", false); + cfg->DumpToFile("afterBBLayout", false); } return nullptr; } diff --git a/src/mapleall/maple_me/src/me_bypath_eh.cpp b/src/mapleall/maple_me/src/me_bypath_eh.cpp index 32f2ea9cb0f3c88fea67e48218ac9205aa463701..68cccf00c10dc863aa090dc688a72746610003ed 100644 --- a/src/mapleall/maple_me/src/me_bypath_eh.cpp +++ b/src/mapleall/maple_me/src/me_bypath_eh.cpp @@ -89,7 +89,7 @@ bool MeDoBypathEH::DoBypathException(BB *tryBB, BB *catchBB, const Klass *catchC // Add fall through bb if (bb->GetKind() == kBBFallthru && !bb->GetAttributes(kBBAttrIsTryEnd)) { bool findBB = false; - for (BB *bbTmp : func.GetAllBBs()) { + for (BB *bbTmp : func.GetCfg()->GetAllBBs()) { if (findBB && bbTmp != nullptr) { if (bbTmp == catchBB || bbTmp->IsEmpty() || bbTmp->GetFirst().GetOpCode() == OP_try || bbTmp->GetAttributes(kBBAttrIsCatch)) { @@ -161,8 +161,8 @@ StmtNode *MeDoBypathEH::IsSyncExit(BB &syncBB, MeFunction &func, LabelIdx second BB *prevBB = &syncBB; while (true) { BB *bbTmp = nullptr; - for (size_t i = prevBB->GetBBId() + 1; i < func.GetAllBBs().size(); ++i) { - bbTmp = func.GetAllBBs()[i]; + for (size_t i = prevBB->GetBBId() + 1; i < func.GetCfg()->GetAllBBs().size(); ++i) { + bbTmp = func.GetCfg()->GetAllBBs()[i]; if (bbTmp != nullptr) { break; } @@ -212,7 +212,7 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c auto labelIdx = static_cast(-1); // Some new bb will be created, so use visited std::set visited; - for (BB *bb : func.GetAllBBs()) { + for (BB *bb : func.GetCfg()->GetAllBBs()) { if (bb == nullptr) { continue; } @@ -232,7 +232,7 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c labelIdx = tryNode->GetOffset(0); } else if (tryNode->GetOffsetsCount() == 2) { // Deal with sync BB *catchBB = nullptr; - for (BB *bbInner : func.GetAllBBs()) { + for (BB *bbInner : func.GetCfg()->GetAllBBs()) { if (bbInner == nullptr) { continue; } @@ -263,7 +263,7 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c continue; } // Find catch label, and create a new bb - for (BB *bbInner : func.GetAllBBs()) { + for (BB *bbInner : func.GetCfg()->GetAllBBs()) { if (bbInner == nullptr || bbInner->GetBBLabel() != labelIdx) { continue; } @@ -300,12 +300,12 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c auto it = func.GetMirFunc()->GetLabelTab()->GetStrIdxToLabelIdxMap().find(labelStrIdx); if (it == func.GetMirFunc()->GetLabelTab()->GetStrIdxToLabelIdxMap().end()) { LabelIdx labIdx = func.GetMirFunc()->GetLabelTab()->AddLabel(labelStrIdx); - newBB = func.NewBasicBlock(); - func.SetLabelBBAt(labIdx, newBB); + newBB = func.GetCfg()->NewBasicBlock(); + func.GetCfg()->SetLabelBBAt(labIdx, newBB); newBB->SetBBLabel(labIdx); } else { hasCreated = true; - for (BB *newBBIter : func.GetAllBBs()) { + for (BB *newBBIter : func.GetCfg()->GetAllBBs()) { if (newBBIter == nullptr) { continue; } @@ -317,16 +317,16 @@ void MeDoBypathEH::BypathException(MeFunction &func, const KlassHierarchy &kh) c } if (DoBypathException(bb, newBB, catchClass, dassignNode->GetStIdx(), kh, func, syncExitStmt)) { if (!hasCreated) { - ASSERT(newBB == func.GetLastBB(), "newBB should be the last one"); - func.GetAllBBs().pop_back(); - newBB = &func.SplitBB(*bbInner, *stmtInner.GetNext(), newBB); + ASSERT(newBB == func.GetCfg()->GetLastBB(), "newBB should be the last one"); + func.GetCfg()->GetAllBBs().pop_back(); + newBB = &func.GetCfg()->SplitBB(*bbInner, *stmtInner.GetNext(), newBB); } } else { if (!hasCreated) { - func.GetAllBBs().pop_back(); - func.DecNextBBId(); + func.GetCfg()->GetAllBBs().pop_back(); + func.GetCfg()->DecNextBBId(); func.GetMirFunc()->GetLabelTab()->GetLabelTable().pop_back(); - func.EraseLabelBBAt( + func.GetCfg()->EraseLabelBBAt( func.GetMirFunc()->GetLabelTab()->GetStrIdxToLabelIdxMap().at(labelStrIdx)); func.GetMirFunc()->GetLabelTab()->EraseStrIdxToLabelIdxElem(labelStrIdx); } @@ -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..4ec7d5c8ddb50d29eb7166633d86361ea344a4c9 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -19,6 +19,8 @@ #include "ssa_mir_nodes.h" #include "me_irmap.h" #include "mir_builder.h" +#include "me_critical_edge.h" +#include "me_loop_canon.h" namespace { constexpr int kFuncNameLenLimit = 80; @@ -106,7 +108,7 @@ void MeCFG::ReplaceSwitchContainsOneCaseBranchWithBrtrue(maple::BB &bb, MapleVec bb.ReplaceStmt(&switchStmt, condGoto); bb.SetKind(kBBCondGoto); - auto *newBB = func.NewBasicBlock(); + auto *newBB = NewBasicBlock(); auto *maxCaseNode = mirBuilder->CreateIntConst(maxCaseVal, PTY_i32); auto *gtNode = mirBuilder->CreateExprCompare(OP_gt, GetTypeFromTyIdx(TyIdx(PTY_u1)), GetTypeFromTyIdx(TyIdx(PTY_i32)), baseNode, maxCaseNode); @@ -114,7 +116,7 @@ void MeCFG::ReplaceSwitchContainsOneCaseBranchWithBrtrue(maple::BB &bb, MapleVec newBB->GetStmtNodes().push_back(condGoto); newBB->SetKind(kBBCondGoto); - BB *defaultBB = func.GetLabelBBAt(defaultLabelIdx); + BB *defaultBB = GetLabelBBAt(defaultLabelIdx); ASSERT(defaultBB != nullptr, "null ptr check"); while (!bb.GetSucc().empty()) { bb.RemoveSucc(*bb.GetSucc(0)); @@ -122,14 +124,14 @@ void MeCFG::ReplaceSwitchContainsOneCaseBranchWithBrtrue(maple::BB &bb, MapleVec bb.AddSucc(*newBB); bb.AddSucc(*defaultBB); - BB *caseBB = func.GetLabelBBAt(switchStmt.GetSwitchTable().front().second); + BB *caseBB = GetLabelBBAt(switchStmt.GetSwitchTable().front().second); ASSERT(caseBB != nullptr, "null ptr check"); newBB->AddSucc(*caseBB); newBB->AddSucc(*defaultBB); if (bb.GetAttributes(kBBAttrIsTry)) { newBB->SetAttributes(kBBAttrIsTry); - func.SetBBTryNodeMap(*newBB, *func.GetBBTryNodeMap().at(&bb)); + SetBBTryNodeMap(*newBB, *GetBBTryNodeMap().at(&bb)); AddCatchHandlerForTryBB(bb, exitBlocks); AddCatchHandlerForTryBB(*newBB, exitBlocks); } @@ -139,16 +141,16 @@ void MeCFG::AddCatchHandlerForTryBB(BB &bb, MapleVector &exitBlocks) { if (!bb.GetAttributes(kBBAttrIsTry)) { return; } - auto it = func.GetBBTryNodeMap().find(&bb); - CHECK_FATAL(it != func.GetBBTryNodeMap().end(), "try bb without try"); + auto it = GetBBTryNodeMap().find(&bb); + CHECK_FATAL(it != GetBBTryNodeMap().end(), "try bb without try"); StmtNode *currTry = it->second; const auto *tryNode = static_cast(currTry); bool hasFinallyHandler = false; // add exception handler bb for (size_t j = 0; j < tryNode->GetOffsetsCount(); ++j) { LabelIdx labelIdx = tryNode->GetOffset(j); - ASSERT(func.GetLabelBBIdMap().find(labelIdx) != func.GetLabelBBIdMap().end(), "runtime check error"); - BB *meBB = func.GetLabelBBAt(labelIdx); + ASSERT(GetLabelBBIdMap().find(labelIdx) != GetLabelBBIdMap().end(), "runtime check error"); + BB *meBB = GetLabelBBAt(labelIdx); CHECK_FATAL(meBB != nullptr, "null ptr check"); ASSERT(meBB->GetAttributes(kBBAttrIsCatch), "runtime check error"); if (meBB->GetAttributes(kBBAttrIsJSFinally) || meBB->GetAttributes(kBBAttrIsCatch)) { @@ -177,12 +179,12 @@ 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) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_entry() || bIt == common_exit()) { continue; } BB *bb = *bIt; @@ -203,7 +205,7 @@ void MeCFG::BuildMirCFG() { ASSERT(lastStmt.GetOpCode() == OP_goto, "runtime check error"); auto &gotoStmt = static_cast(lastStmt); LabelIdx lblIdx = gotoStmt.GetOffset(); - BB *meBB = func.GetLabelBBAt(lblIdx); + BB *meBB = GetLabelBBAt(lblIdx); bb->AddSucc(*meBB); break; } @@ -218,7 +220,7 @@ void MeCFG::BuildMirCFG() { // link goto auto &gotoStmt = static_cast(lastStmt); LabelIdx lblIdx = gotoStmt.GetOffset(); - BB *meBB = func.GetLabelBBAt(lblIdx); + BB *meBB = GetLabelBBAt(lblIdx); if (*rightNextBB == meBB) { constexpr char tmpBool[] = "tmpBool"; auto *mirBuilder = func.GetMIRModule().GetMIRBuilder(); @@ -240,12 +242,12 @@ void MeCFG::BuildMirCFG() { ASSERT(lastStmt.GetOpCode() == OP_switch, "runtime check error"); auto &switchStmt = static_cast(lastStmt); LabelIdx lblIdx = switchStmt.GetDefaultLabel(); - BB *mirBB = func.GetLabelBBAt(lblIdx); + BB *mirBB = GetLabelBBAt(lblIdx); bb->AddSucc(*mirBB); std::set caseLabels; for (size_t j = 0; j < switchStmt.GetSwitchTable().size(); ++j) { lblIdx = switchStmt.GetCasePair(j).second; - BB *meBB = func.GetLabelBBAt(lblIdx); + BB *meBB = GetLabelBBAt(lblIdx); (void)caseLabels.insert(lblIdx); // Avoid duplicate succs. auto it = std::find(bb->GetSucc().begin(), bb->GetSucc().end(), meBB); @@ -263,7 +265,7 @@ void MeCFG::BuildMirCFG() { } case kBBIgoto: { for (LabelIdx lidx : func.GetMirFunc()->GetLabelTab()->GetAddrTakenLabels()) { - BB *mebb = func.GetLabelBBAt(lidx); + BB *mebb = GetLabelBBAt(lidx); bb->AddSucc(*mebb); } break; @@ -291,11 +293,11 @@ void MeCFG::BuildMirCFG() { } // merge all blocks in entryBlocks for (BB *bb : entryBlocks) { - func.GetCommonEntryBB()->AddEntry(*bb); + GetCommonEntryBB()->AddEntry(*bb); } // merge all blocks in exitBlocks for (BB *bb : exitBlocks) { - func.GetCommonExitBB()->AddExit(*bb); + GetCommonExitBB()->AddExit(*bb); } } @@ -417,9 +419,9 @@ bool MeCFG::HasNoOccBetween(StmtNode &from, const StmtNode &to, StIdx stIdx) con // Fix the initially created CFG void MeCFG::FixMirCFG() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_entry() || bIt == common_exit()) { continue; } auto *bb = *bIt; @@ -475,8 +477,8 @@ void MeCFG::FixMirCFG() { } // 2. Split bb to two bbs if there are use and def the same ref var in bb. - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_entry() || bIt == common_exit()) { continue; } auto *bb = *bIt; @@ -511,12 +513,12 @@ void MeCFG::FixMirCFG() { HasNoOccBetween(*bb->GetStmtNodes().begin().d(), *nextStmt, sym->GetStIdx())) { continue; } - BB &newBB = func.SplitBB(*bb, splitPoint); + BB &newBB = SplitBB(*bb, splitPoint); // because SplitBB will insert a bb, we need update bIt & eIt - auto newBBIt = std::find(func.cbegin(), func.cend(), bb); + auto newBBIt = std::find(cbegin(), cend(), bb); bIt = build_filter_iterator( - newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, func.end())); - eIt = func.valid_end(); + newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, end())); + eIt = valid_end(); for (size_t si = 0; si < newBB.GetSucc().size(); ++si) { BB *sucBB = newBB.GetSucc(si); if (sucBB->GetAttributes(kBBAttrIsCatch)) { @@ -554,12 +556,12 @@ void MeCFG::FixMirCFG() { splitPoint = splitPoint->GetPrev(); } CHECK_FATAL(splitPoint != nullptr, "null ptr check"); - BB &newBB = func.SplitBB(*bb, *splitPoint); + BB &newBB = SplitBB(*bb, *splitPoint); // because SplitBB will insert a bb, we need update bIt & eIt - auto newBBIt = std::find(func.cbegin(), func.cend(), bb); + auto newBBIt = std::find(cbegin(), cend(), bb); bIt = build_filter_iterator( - newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, func.end())); - eIt = func.valid_end(); + newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, end())); + eIt = valid_end(); // redirect all succs of new bb to bb for (size_t si = 0; si < newBB.GetSucc().size(); ++si) { BB *sucBB = newBB.GetSucc(si); @@ -583,7 +585,7 @@ void MeCFG::ReplaceWithAssertnonnull() { return; } for (LabelIdx lblIdx : patternSet) { - BB *bb = func.GetLabelBBAt(lblIdx); + BB *bb = GetLabelBBAt(lblIdx); // if BB->pred_.size()==0, it won't enter this function for (size_t i = 0; i < bb->GetPred().size(); ++i) { BB *innerBB = bb->GetPred(i); @@ -645,13 +647,13 @@ void MeCFG::FixTryBB(maple::BB &startBB, maple::BB &nextBB) { // analyse the CFG to find the BBs that are not reachable from function entries // and delete them bool MeCFG::UnreachCodeAnalysis(bool updatePhi) { - std::vector visitedBBs(func.NumBBs(), false); - func.GetCommonEntryBB()->FindReachableBBs(visitedBBs); + std::vector visitedBBs(NumBBs(), false); + GetCommonEntryBB()->FindReachableBBs(visitedBBs); // delete the unreached bb bool cfgChanged = false; - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_exit()) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_exit()) { continue; } auto *bb = *bIt; @@ -666,9 +668,9 @@ bool MeCFG::UnreachCodeAnalysis(bool updatePhi) { // if bb is StartTryBB, relationship between endtry and try should be maintained if (IsStartTryBB(*bb)) { bool needFixTryBB = false; - size_t size = func.GetAllBBs().size(); + size_t size = GetAllBBs().size(); for (size_t nextIdx = idx + 1; nextIdx < size; ++nextIdx) { - auto nextBB = func.GetBBFromID(BBId(nextIdx)); + auto nextBB = GetBBFromID(BBId(nextIdx)); if (nextBB == nullptr) { continue; } @@ -692,20 +694,20 @@ bool MeCFG::UnreachCodeAnalysis(bool updatePhi) { } if (bb->GetAttributes(kBBAttrIsTryEnd)) { // unreachable bb has try end info - auto bbIt = std::find(func.rbegin(), func.rend(), bb); + auto bbIt = std::find(rbegin(), rend(), bb); auto prevIt = ++bbIt; - for (auto it = prevIt; it != func.rend(); ++it) { + for (auto it = prevIt; it != rend(); ++it) { if (*it != nullptr) { // move entrytry tag to previous bb with try if ((*it)->GetAttributes(kBBAttrIsTry) && !(*it)->GetAttributes(kBBAttrIsTryEnd)) { (*it)->SetAttributes(kBBAttrIsTryEnd); - func.SetTryBBByOtherEndTryBB(*it, bb); + SetTryBBByOtherEndTryBB(*it, bb); } break; } } } - func.DeleteBasicBlock(*bb); + DeleteBasicBlock(*bb); cfgChanged = true; // remove the bb from its succ's pred_ list while (bb->GetSucc().size() > 0) { @@ -720,10 +722,10 @@ bool MeCFG::UnreachCodeAnalysis(bool updatePhi) { } } // remove the bb from common_exit_bb's pred list if it is there - auto &predsOfCommonExit = func.GetCommonExitBB()->GetPred(); + auto &predsOfCommonExit = GetCommonExitBB()->GetPred(); auto it = std::find(predsOfCommonExit.begin(), predsOfCommonExit.end(), bb); if (it != predsOfCommonExit.end()) { - func.GetCommonExitBB()->RemoveExit(*bb); + GetCommonExitBB()->RemoveExit(*bb); } } return cfgChanged; @@ -792,14 +794,14 @@ void MeCFG::ConvertPhis2IdentityAssigns(BB &meBB) const { // are BBs inside infinite loops; mark their wontExit flag and create // artificial edges from them to common_exit_bb void MeCFG::WontExitAnalysis() { - if (func.NumBBs() == 0) { + if (NumBBs() == 0) { return; } - std::vector visitedBBs(func.NumBBs(), false); - func.GetCommonExitBB()->FindWillExitBBs(visitedBBs); - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry()) { + std::vector visitedBBs(NumBBs(), false); + GetCommonExitBB()->FindWillExitBBs(visitedBBs); + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_entry()) { continue; } auto *bb = *bIt; @@ -813,16 +815,16 @@ void MeCFG::WontExitAnalysis() { } if (bb->GetKind() == kBBGoto) { // create artificial BB to transition to common_exit_bb - BB *newBB = func.NewBasicBlock(); + BB *newBB = NewBasicBlock(); // update bIt & eIt - auto newBBIt = std::find(func.cbegin(), func.cend(), bb); + auto newBBIt = std::find(cbegin(), cend(), bb); bIt = build_filter_iterator( - newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, func.end())); - eIt = func.valid_end(); + newBBIt, std::bind(FilterNullPtr::const_iterator>, std::placeholders::_1, end())); + eIt = valid_end(); newBB->SetKindReturn(); newBB->SetAttributes(kBBAttrArtificial); bb->AddSucc(*newBB); - func.GetCommonExitBB()->AddExit(*newBB); + GetCommonExitBB()->AddExit(*newBB); } } } @@ -831,14 +833,14 @@ void MeCFG::WontExitAnalysis() { // Check bb-vec and bb-list are strictly consistent. void MeCFG::Verify() const { // Check every bb in bb-list. - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + if (bIt == common_entry() || bIt == common_exit()) { continue; } auto *bb = *bIt; - ASSERT(bb->GetBBId() < func.GetAllBBs().size(), "CFG Error!"); - ASSERT(func.GetBBFromID(bb->GetBBId()) == bb, "CFG Error!"); + ASSERT(bb->GetBBId() < bbVec.size(), "CFG Error!"); + ASSERT(bbVec.at(bb->GetBBId()) == bb, "CFG Error!"); if (bb->IsEmpty()) { continue; } @@ -867,8 +869,8 @@ void MeCFG::Verify() const { // check that all the target labels in jump statements are defined void MeCFG::VerifyLabels() const { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { BB *mirBB = *bIt; auto &stmtNodes = mirBB->GetStmtNodes(); if (stmtNodes.rbegin().base().d() == nullptr) { @@ -879,22 +881,22 @@ void MeCFG::VerifyLabels() const { continue; } ASSERT( - func.GetLabelBBAt(static_cast(stmtNodes.back()).GetOffset())->GetBBLabel() == + GetLabelBBAt(static_cast(stmtNodes.back()).GetOffset())->GetBBLabel() == static_cast(stmtNodes.back()).GetOffset(), "undefined label in goto"); } else if (mirBB->GetKind() == kBBCondGoto) { ASSERT( - func.GetLabelBBAt(static_cast(stmtNodes.back()).GetOffset())->GetBBLabel() == + GetLabelBBAt(static_cast(stmtNodes.back()).GetOffset())->GetBBLabel() == static_cast(stmtNodes.back()).GetOffset(), "undefined label in conditional branch"); } else if (mirBB->GetKind() == kBBSwitch) { auto &switchStmt = static_cast(stmtNodes.back()); LabelIdx targetLabIdx = switchStmt.GetDefaultLabel(); - BB *bb = func.GetLabelBBAt(targetLabIdx); + BB *bb = GetLabelBBAt(targetLabIdx); ASSERT(bb->GetBBLabel() == targetLabIdx, "undefined label in switch"); for (size_t j = 0; j < switchStmt.GetSwitchTable().size(); ++j) { targetLabIdx = switchStmt.GetCasePair(j).second; - bb = func.GetLabelBBAt(targetLabIdx); + bb = GetLabelBBAt(targetLabIdx); ASSERT(bb->GetBBLabel() == targetLabIdx, "undefined switch target label"); } } @@ -904,11 +906,11 @@ void MeCFG::VerifyLabels() const { void MeCFG::Dump() const { // BSF Dump the cfg LogInfo::MapleLogger() << "####### CFG Dump: "; - ASSERT(func.NumBBs() != 0, "size to be allocated is 0"); - auto *visitedMap = static_cast(calloc(func.NumBBs(), sizeof(bool))); + ASSERT(NumBBs() != 0, "size to be allocated is 0"); + auto *visitedMap = static_cast(calloc(NumBBs(), sizeof(bool))); if (visitedMap != nullptr) { std::queue qu; - qu.push(func.GetFirstBB()); + qu.push(GetFirstBB()); while (!qu.empty()) { BB *bb = qu.front(); qu.pop(); @@ -973,8 +975,8 @@ std::string MeCFG::ConstructFileNameToDump(const std::string &prefix) const { } void MeCFG::DumpToFileInStrs(std::ofstream &cfgFile) const { - const auto &eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + const auto &eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; if (bb->GetKind() == kBBCondGoto) { cfgFile << "BB" << bb->GetBBId() << "[shape=diamond,label= \" BB" << bb->GetBBId() << ":\n{ "; @@ -1015,10 +1017,10 @@ void MeCFG::DumpToFile(const std::string &prefix, bool dumpInStrs, bool dumpEdge cfgFile << "digraph {\n"; cfgFile << " # /*" << func.GetName() << " (red line is exception handler)*/\n"; // dump edge - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = valid_end(); + for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; - if (bIt == func.common_exit()) { + if (bIt == common_exit()) { // specical case for common_exit_bb for (auto it = bb->GetPred().begin(); it != bb->GetPred().end(); ++it) { dumpEdgeFreq ? cfgFile << "BB" << (*it)->GetBBId() << "_freq_" << (*it)->GetFrequency() << " -> " << @@ -1035,7 +1037,7 @@ void MeCFG::DumpToFile(const std::string &prefix, bool dumpInStrs, bool dumpEdge dumpEdgeFreq ? cfgFile << "BB" << bb->GetBBId() << "_freq_" << bb->GetFrequency() << " -> " << "BB" << (*it)->GetBBId() << "_freq_" << (*it)->GetFrequency() : cfgFile << "BB" << bb->GetBBId() << " -> " << "BB" << (*it)->GetBBId(); - if (bb == func.GetCommonEntryBB()) { + if (bb == GetCommonEntryBB()) { cfgFile << "[style=dotted]"; continue; } @@ -1059,4 +1061,517 @@ void MeCFG::DumpToFile(const std::string &prefix, bool dumpInStrs, bool dumpEdge cfgFile.flush(); LogInfo::MapleLogger().rdbuf(coutBuf); } + +BB *MeCFG::NewBasicBlock() { + BB *newBB = mp->New(&mecfgAlloc, &func.GetVersAlloc(), BBId(nextBBId++)); + bbVec.push_back(newBB); + return newBB; +} + +// new a basic block and insert before position or after position +BB &MeCFG::InsertNewBasicBlock(const BB &position, bool isInsertBefore) { + BB *newBB = mp->New(&mecfgAlloc, &func.GetVersAlloc(), BBId(nextBBId++)); + + auto bIt = std::find(begin(), end(), &position); + auto idx = position.GetBBId(); + if (!isInsertBefore) { + ++bIt; + ++idx; + } + auto newIt = bbVec.insert(bIt, newBB); + auto eIt = end(); + // update bb's idx + for (auto it = newIt; it != eIt; ++it) { + if ((*it) != nullptr) { + (*it)->SetBBId(BBId(idx)); + } + ++idx; + } + return *newBB; +} + +void MeCFG::DeleteBasicBlock(const BB &bb) { + ASSERT(bbVec[bb.GetBBId()] == &bb, "runtime check error"); + /* update first_bb_ and last_bb if needed */ + bbVec.at(bb.GetBBId()) = nullptr; +} + +/* get next bb in bbVec */ +BB *MeCFG::NextBB(const BB *bb) { + auto bbIt = std::find(begin(), end(), bb); + CHECK_FATAL(bbIt != end(), "bb must be inside bb_vec"); + for (auto it = ++bbIt; it != end(); ++it) { + if (*it != nullptr) { + return *it; + } + } + return nullptr; +} + +/* get prev bb in bbVec */ +BB *MeCFG::PrevBB(const BB *bb) { + auto bbIt = std::find(rbegin(), rend(), bb); + CHECK_FATAL(bbIt != rend(), "bb must be inside bb_vec"); + for (auto it = ++bbIt; it != rend(); ++it) { + if (*it != nullptr) { + return *it; + } + } + return nullptr; +} + +/* clone stmtnode in orig bb to newBB */ +void MeCFG::CloneBasicBlock(BB &newBB, const BB &orig) { + if (orig.IsEmpty()) { + return; + } + for (const auto &stmt : orig.GetStmtNodes()) { + StmtNode *newStmt = stmt.CloneTree(func.GetMIRModule().GetCurFuncCodeMPAllocator()); + newStmt->SetNext(nullptr); + newStmt->SetPrev(nullptr); + newBB.AddStmtNode(newStmt); + if (func.GetMeSSATab() != nullptr) { + func.GetMeSSATab()->CreateSSAStmt(*newStmt, &newBB); + } + } +} + +void MeCFG::SplitBBPhysically(BB &bb, StmtNode &splitPoint, BB &newBB) { + StmtNode *newBBStart = splitPoint.GetNext(); + // Fix Stmt in BB. + if (newBBStart != nullptr) { + newBBStart->SetPrev(nullptr); + for (StmtNode *stmt = newBBStart; stmt != nullptr;) { + StmtNode *nextStmt = stmt->GetNext(); + newBB.AddStmtNode(stmt); + if (func.GetMeSSATab() != nullptr) { + func.GetMeSSATab()->CreateSSAStmt(*stmt, &newBB); + } + stmt = nextStmt; + } + } + bb.SetLast(&splitPoint); + splitPoint.SetNext(nullptr); +} + +/* Split BB at split_point */ +BB &MeCFG::SplitBB(BB &bb, StmtNode &splitPoint, BB *newBB) { + if (newBB == nullptr) { + newBB = mp->New(&mecfgAlloc, &func.GetVersAlloc(), BBId(nextBBId++)); + } + SplitBBPhysically(bb, splitPoint, *newBB); + // Fix BB in CFG + newBB->SetKind(bb.GetKind()); + bb.SetKind(kBBFallthru); + auto bIt = std::find(begin(), end(), &bb); + auto idx = bb.GetBBId(); + auto newIt = bbVec.insert(++bIt, newBB); + auto eIt = end(); + // update bb's idx + for (auto it = newIt; it != eIt; ++it) { + ++idx; + if ((*it) != nullptr) { + (*it)->SetBBId(BBId(idx)); + } + } + // Special Case: commonExitBB is orig bb's succ + auto *commonExitBB = *common_exit(); + if (bb.IsPredBB(*commonExitBB)) { + commonExitBB->RemoveExit(bb); + commonExitBB->AddExit(*newBB); + } + while (bb.GetSucc().size() > 0) { + BB *succ = bb.GetSucc(0); + succ->ReplacePred(&bb, newBB); + } + bb.AddSucc(*newBB); + // Setup flags + newBB->CopyFlagsAfterSplit(bb); + if (newBB->GetAttributes(kBBAttrIsTryEnd)) { + endTryBB2TryBB[newBB] = endTryBB2TryBB[&bb]; + endTryBB2TryBB[&bb] = nullptr; + bb.ClearAttributes(kBBAttrIsTryEnd); + } + bb.ClearAttributes(kBBAttrIsExit); + return *newBB; +} + +void MeCFG::SetTryBlockInfo(const StmtNode *nextStmt, StmtNode *tryStmt, BB *lastTryBB, BB *curBB, BB *newBB) { + if (nextStmt->GetOpCode() == OP_endtry) { + curBB->SetAttributes(kBBAttrIsTryEnd); + ASSERT(lastTryBB != nullptr, "null ptr check"); + endTryBB2TryBB[curBB] = lastTryBB; + } else { + newBB->SetAttributes(kBBAttrIsTry); + bbTryNodeMap[newBB] = tryStmt; + } +} + +void MeCFG::CreateBasicBlocks() { + if (func.CurFunction()->IsEmpty()) { + if (!MeOption::quiet) { + LogInfo::MapleLogger() << "function is empty, cfg is nullptr\n"; + } + return; + } + // create common_entry/exit bb first as bbVec[0] and bb_vec[1] + bool isJavaModule = func.GetMIRModule().IsJavaModule(); + auto *commonEntryBB = NewBasicBlock(); + commonEntryBB->SetAttributes(kBBAttrIsEntry); + auto *commonExitBB = NewBasicBlock(); + commonExitBB->SetAttributes(kBBAttrIsExit); + auto *firstBB = NewBasicBlock(); + firstBB->SetAttributes(kBBAttrIsEntry); + StmtNode *nextStmt = func.CurFunction()->GetBody()->GetFirst(); + ASSERT(nextStmt != nullptr, "function has no statement"); + BB *curBB = firstBB; + StmtNode *tryStmt = nullptr; // record current try stmt for map + BB *lastTryBB = nullptr; // bb containing try_stmt + do { + StmtNode *stmt = nextStmt; + nextStmt = stmt->GetNext(); + switch (stmt->GetOpCode()) { + case OP_goto: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKind(kBBGoto); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + } + curBB = newBB; + } + break; + } + case OP_igoto: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKind(kBBIgoto); + if (nextStmt != nullptr) { + curBB = NewBasicBlock(); + } + break; + } + case OP_dassign: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + if (static_cast(stmt)->GetRHS()->MayThrowException()) { + stmt->SetOpCode(OP_maydassign); + if (tryStmt != nullptr) { + // breaks new BB only inside try blocks + curBB->SetLast(stmt); + curBB->SetKind(kBBFallthru); + BB *newBB = NewBasicBlock(); + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + curBB = newBB; + break; + } + } + if ((nextStmt == nullptr) && to_ptr(curBB->GetStmtNodes().rbegin()) == nullptr) { + curBB->SetLast(stmt); + } + break; + } + case OP_brfalse: + case OP_brtrue: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKind(kBBCondGoto); + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + } + curBB = newBB; + break; + } + case OP_if: + case OP_doloop: + case OP_dowhile: + case OP_while: { + ASSERT(false, "not yet implemented"); + break; + } + case OP_throw: + if (tryStmt != nullptr) { + // handle as goto + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKind(kBBGoto); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + curBB = newBB; + } + break; + } + // fall thru to handle as return + [[clang::fallthrough]]; + case OP_gosub: + case OP_retsub: + case OP_return: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKindReturn(); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + } + curBB = newBB; + if (stmt->GetOpCode() == OP_gosub) { + curBB->SetAttributes(kBBAttrIsEntry); + } + } + break; + } + case OP_endtry: + if (isJavaModule) { + if (tryStmt == nullptr) { + break; + } + /* skip OP_entry and generate it in emit phase */ + ASSERT(lastTryBB != nullptr, "null ptr check"); + tryStmt = nullptr; // reset intryblocks + if (!curBB->IsEmpty()) { + StmtNode *lastStmt = stmt->GetPrev(); + ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || + curBB->GetStmtNodes().rbegin().base().d() == lastStmt, + "something wrong building BB"); + curBB->SetLast(lastStmt); + if (curBB->GetKind() == kBBUnknown) { + curBB->SetKind(kBBFallthru); + } + curBB->SetAttributes(kBBAttrIsTryEnd); + SetBBTryBBMap(curBB, lastTryBB); + curBB = NewBasicBlock(); + } else if (curBB->GetBBLabel() != 0) { + // create the empty BB + curBB->SetKind(kBBFallthru); + curBB->SetAttributes(kBBAttrIsTryEnd); + SetBBTryBBMap(curBB, lastTryBB); + curBB = NewBasicBlock(); + } else { + } // endtry has already been processed in SetTryBlockInfo() + lastTryBB = nullptr; + } else { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + if ((nextStmt == nullptr) && (curBB->GetStmtNodes().rbegin().base().d() == nullptr)) { + curBB->SetLast(stmt); + } + } + break; + case OP_try: { + // start a new bb or with a label + if (!curBB->IsEmpty()) { + // prepare a new bb + StmtNode *lastStmt = stmt->GetPrev(); + ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || + curBB->GetStmtNodes().rbegin().base().d() == lastStmt, + "something wrong building BB"); + curBB->SetLast(lastStmt); + if (curBB->GetKind() == kBBUnknown) { + curBB->SetKind(kBBFallthru); + } + BB *newBB = NewBasicBlock(); + // assume no nested try, so no need to call SetTryBlockInfo() + curBB = newBB; + } + curBB->SetFirst(stmt); + tryStmt = stmt; + lastTryBB = curBB; + curBB->SetAttributes(kBBAttrIsTry); + bbTryNodeMap[curBB] = tryStmt; + // prepare a new bb that contains only a OP_try. It is needed for + // dse to work correctly: assignments in a try block should not affect + // assignments before the try block as exceptions might occur. + curBB->SetLast(stmt); + curBB->SetKind(kBBFallthru); + BB *newBB = NewBasicBlock(); + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + curBB = newBB; + break; + } + case OP_catch: { + // start a new bb or with a label + if (!curBB->IsEmpty()) { + // prepare a new bb + StmtNode *lastStmt = stmt->GetPrev(); + ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || + curBB->GetStmtNodes().rbegin().base().d() == lastStmt, + "something wrong building BB"); + curBB->SetLast(lastStmt); + if (curBB->GetKind() == kBBUnknown) { + curBB->SetKind(kBBFallthru); + } + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + } + curBB = newBB; + } + curBB->SetFirst(stmt); + curBB->SetAttributes(kBBAttrIsCatch); + auto *catchNode = static_cast(stmt); + const MapleVector &exceptionTyIdxVec = catchNode->GetExceptionTyIdxVec(); + + for (TyIdx exceptIdx : exceptionTyIdxVec) { + MIRType *eType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(exceptIdx); + ASSERT(eType != nullptr && (eType->GetPrimType() == PTY_ptr || eType->GetPrimType() == PTY_ref), + "wrong exception type"); + auto *exceptType = static_cast(eType); + MIRType *pointType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(exceptType->GetPointedTyIdx()); + const std::string &eName = GlobalTables::GetStrTable().GetStringFromStrIdx(pointType->GetNameStrIdx()); + if ((pointType->GetPrimType() == PTY_void) || (eName == "Ljava/lang/Throwable;") || + (eName == "Ljava/lang/Exception;")) { + // "Ljava/lang/Exception;" is risk to set isJavaFinally because it + // only deal with "throw exception". if throw error, it's wrong + curBB->SetAttributes(kBBAttrIsJavaFinally); // this is a start of finally handler + } + } + break; + } + case OP_label: { + auto *labelNode = static_cast(stmt); + LabelIdx labelIdx = labelNode->GetLabelIdx(); + if (!curBB->IsEmpty() || curBB->GetBBLabel() != 0) { + // prepare a new bb + StmtNode *lastStmt = stmt->GetPrev(); + ASSERT((curBB->GetStmtNodes().rbegin().base().d() == nullptr || + curBB->GetStmtNodes().rbegin().base().d() == lastStmt), + "something wrong building BB"); + if (curBB->GetStmtNodes().rbegin().base().d() == nullptr && (lastStmt->GetOpCode() != OP_label)) { + if (isJavaModule && lastStmt->GetOpCode() == OP_endtry) { + if (curBB->GetStmtNodes().empty()) { + curBB->SetLast(nullptr); + } else { + // find a valid stmt which is not label or endtry + StmtNode *p = lastStmt->GetPrev(); + ASSERT(p != nullptr, "null ptr check"); + ASSERT(p->GetOpCode() != OP_label, "runtime check error"); + ASSERT(p->GetOpCode() != OP_endtry, "runtime check error"); + curBB->SetLast(p); + } + } else { + curBB->SetLast(lastStmt); + } + } + if (curBB->GetKind() == kBBUnknown) { + curBB->SetKind(kBBFallthru); + } + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + newBB->SetAttributes(kBBAttrIsTry); + //bbTryNodeMap[newBB] = tryStmt; + SetBBTryNodeMap(*newBB, *tryStmt); + } + curBB = newBB; + } + labelBBIdMap[labelIdx] = curBB; + curBB->SetBBLabel(labelIdx); + break; + } + case OP_jscatch: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetAttributes(kBBAttrIsEntry); + curBB->SetAttributes(kBBAttrIsJSCatch); + break; + } + case OP_finally: { + ASSERT(curBB->GetStmtNodes().empty(), "runtime check error"); + curBB->SetFirst(stmt); + curBB->SetAttributes(kBBAttrIsEntry); + curBB->SetAttributes(kBBAttrIsJSFinally); + break; + } + case OP_switch: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKind(kBBSwitch); + BB *newBB = NewBasicBlock(); + if (tryStmt != nullptr) { + SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); + } + curBB = newBB; + break; + } + default: { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + if ((nextStmt == nullptr) && (curBB->GetStmtNodes().rbegin().base().d() == nullptr)) { + curBB->SetLast(stmt); + } + break; + } + } + } while (nextStmt != nullptr); + ASSERT(tryStmt == nullptr, "unclosed try"); // tryandendtry should be one-one mapping + ASSERT(lastTryBB == nullptr, "unclosed tryBB"); // tryandendtry should be one-one mapping + auto *lastBB = curBB; + if (lastBB->IsEmpty()) { + // insert a return statement + lastBB->SetFirst(func.GetMIRModule().GetMIRBuilder()->CreateStmtReturn(nullptr)); + lastBB->SetLast(lastBB->GetStmtNodes().begin().d()); + lastBB->SetKindReturn(); + } else if (lastBB->GetKind() == kBBUnknown) { + lastBB->SetKindReturn(); + lastBB->SetAttributes(kBBAttrIsExit); + } +} + +AnalysisResult *MeDoMeCfg::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr *mrm) { + MemPool *meCfgMp = NewMemPool(); + MeCFG *theCFG = meCfgMp->New(meCfgMp, *func); + func->SetTheCfg(theCFG); + theCFG->CreateBasicBlocks(); + if (theCFG->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(); + + // TODOFORTEST + // if (func->GetMIRModule().IsCModule()) { + if (mrm == nullptr) { + MeDoLoopCanon doLoopCanon(MeFuncPhase_LOOPCANON); + if (!MeOption::quiet) { + LogInfo::MapleLogger() << " == " << PhaseName() << " invokes [ " << doLoopCanon.PhaseName() << " ] ==\n"; + } + doLoopCanon.Run(func, m, NULL); + + MeDoSplitCEdge doSplitCEdge(MeFuncPhase_SPLITCEDGE); + if (!MeOption::quiet) { + LogInfo::MapleLogger() << " == " << PhaseName() << " invokes [ " << doSplitCEdge.PhaseName() << " ] ==\n"; + } + doSplitCEdge.Run(func, m, NULL); + } + 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..bcac0f2fbd1902b55485ee1eeaafff8313b35b42 100644 --- a/src/mapleall/maple_me/src/me_cfg_opt.cpp +++ b/src/mapleall/maple_me/src/me_cfg_opt.cpp @@ -210,9 +210,9 @@ bool MeCfgOpt::HasFloatCmp(const MeExpr &meExpr) const { return false; } -bool MeCfgOpt::PreCheck(const MeFunction &func) const { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { +bool MeCfgOpt::PreCheck(const MeCFG &cfg) const { + auto eIt = cfg.valid_end(); + for (auto bIt = cfg.valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; if (bb->GetAttributes(kBBAttrIsTry) && bb->GetBBLabel() != 0) { for (size_t i = 0; i < bb->GetPred().size(); ++i) { @@ -235,12 +235,13 @@ bool MeCfgOpt::PreCheck(const MeFunction &func) const { return true; } -bool MeCfgOpt::Run(MeFunction &func) { - if (!PreCheck(func)) { +bool MeCfgOpt::Run(MeCFG &cfg) { + if (!PreCheck(cfg)) { return false; } - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + bool isCfgChanged = false; + auto eIt = cfg.valid_end(); + for (auto bIt = cfg.valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; constexpr uint32 numOfSuccs = 2; if (bb->GetKind() == kBBCondGoto && bb->GetSucc().size() == numOfSuccs) { @@ -322,16 +323,16 @@ bool MeCfgOpt::Run(MeFunction &func) { bb->AddSucc(*succBB); bb->SetKind(kBBFallthru); bb->GetSuccFreq().push_back(bb->GetFrequency()); - func.DeleteBasicBlock(*bbLeft); - func.DeleteBasicBlock(*bbRight); - SetCfgChanged(); + cfg.DeleteBasicBlock(*bbLeft); + cfg.DeleteBasicBlock(*bbRight); + isCfgChanged = true; } } - return IsCfgChanged(); + return isCfgChanged; } void MeDoCfgOpt::EmitMapleIr(MeFunction &func, MeFuncResultMgr &m) { - if (func.NumBBs() > 0) { + if (func.GetCfg()->NumBBs() > 0) { (void)m.GetAnalysisResult(MeFuncPhase_BBLAYOUT, &func); CHECK_FATAL(func.HasLaidOut(), "Check bb layout phase."); auto layoutBBs = func.GetLaidOutBBs(); @@ -377,14 +378,16 @@ AnalysisResult *MeDoCfgOpt::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResu auto *irMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); ASSERT(irMap != nullptr, "irMap should not be nullptr"); MeCfgOpt cfgOpt(irMap); - if (cfgOpt.Run(*func)) { - SetChangeCFG(); + if (cfgOpt.Run(*func->GetCfg())) { EmitMapleIr(*func, *m); + m->InvalidAllResults(); + func->SetMeSSATab(nullptr); + func->SetIRMap(nullptr); return nullptr; } // check all basic blocks searching for split case - for (BB *bb : func->GetAllBBs()) { + for (BB *bb : func->GetCfg()->GetAllBBs()) { if (bb == nullptr || !(bb->GetAttributes(kBBAttrIsTry))) { continue; } @@ -411,8 +414,10 @@ AnalysisResult *MeDoCfgOpt::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResu for (size_t i = 0; i < stmtNew->NumMeStmtOpnds(); ++i) { ASSERT(stmtNew->GetOpnd(i) != nullptr, "null ptr check"); if (FindLocalRefVarUses(*irMap, *stmtNew->GetOpnd(i), *stmtNew, *theLHS)) { - SetChangeCFG(); EmitMapleIr(*func, *m); + m->InvalidAllResults(); + func->SetMeSSATab(nullptr); + func->SetIRMap(nullptr); return nullptr; } } diff --git a/src/mapleall/maple_me/src/me_check_cast.cpp b/src/mapleall/maple_me/src/me_check_cast.cpp index c305657cf4bf04cadbc7c237fa204af52f97a88a..e53b75be4d8d8692fdc6ebfc20f4419258f5ecde 100644 --- a/src/mapleall/maple_me/src/me_check_cast.cpp +++ b/src/mapleall/maple_me/src/me_check_cast.cpp @@ -628,7 +628,8 @@ bool CheckCast::ProvedBySSI(const IntrinsiccallMeStmt &callNode) { } void CheckCast::DoCheckCastOpt() { - for (BB *bb : func->GetAllBBs()) { + auto cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } @@ -672,7 +673,8 @@ void CheckCast::DoCheckCastOpt() { } void CheckCast::FindRedundantChecks() { - for (BB *bb : func->GetAllBBs()) { + auto cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } diff --git a/src/mapleall/maple_me/src/me_cond_based_opt.cpp b/src/mapleall/maple_me/src/me_cond_based_opt.cpp index 7035bd5098514c208fb1c345fecb29cb4f430443..54a86fab5a3a3e837a1957ce912210a392dcc694 100644 --- a/src/mapleall/maple_me/src/me_cond_based_opt.cpp +++ b/src/mapleall/maple_me/src/me_cond_based_opt.cpp @@ -87,8 +87,9 @@ bool MeCondBased::NullValueFromTestCond(const VarMeExpr &varMeExpr, const BB &bb size_t bbSize = dominance->GetBBVecSize(); std::vector visitedMap(bbSize, false); bool provenNull = false; + MeCFG *cfg = func->GetCfg(); while (pdomFrt->size() == 1) { - BB &cdBB = *func->GetAllBBs().at(*(pdomFrt->begin())); + BB &cdBB = *cfg->GetAllBBs().at(*(pdomFrt->begin())); if (visitedMap[cdBB.GetBBId()]) { break; } @@ -222,8 +223,9 @@ bool MeCondBased::IsNotNullValue(const VarMeExpr &varMeExpr, const UnaryMeStmt & } void CondBasedNPC::DoCondBasedNPC() const { - auto eIt = GetFunc()->valid_end(); - for (auto bIt = GetFunc()->valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = GetFunc()->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetMeStmts()) { if (stmt.GetOp() != OP_assertnonnull) { @@ -245,8 +247,9 @@ AnalysisResult *MeDoCondBasedRC::Run(MeFunction *func, MeFuncResultMgr *m, Modul auto *dom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); ASSERT(dom != nullptr, "dominance phase has problem"); CondBasedRC condBasedRC(*func, *dom); - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetMeStmts()) { if (stmt.GetOp() != OP_decref) { diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index f7010caa31a1e1fabcf8832d42f2a1479a8cc96a..2dcaf9894f10b67808643d5d1d5cc6f2a5ab2890 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -40,10 +40,10 @@ void MeDoSplitCEdge::UpdateNewBBInTry(MeFunction &func, BB &newBB, const BB &pre // so need find a try bb which have eh edge continue. if (pred.IsReturnBB()) { int i = 1; - tryBB = func.GetBBFromID(newBB.GetBBId() - i); + tryBB = func.GetCfg()->GetBBFromID(newBB.GetBBId() - i); while (tryBB == nullptr || tryBB->IsReturnBB()) { ++i; - tryBB = func.GetBBFromID(newBB.GetBBId() - i); + tryBB = func.GetCfg()->GetBBFromID(newBB.GetBBId() - i); } ASSERT_NOT_NULL(tryBB); ASSERT(tryBB->GetAttributes(kBBAttrIsTry), "must be try"); @@ -98,15 +98,15 @@ void MeDoSplitCEdge::UpdateCaseLabel(BB &newBB, MeFunction &func, BB &pred, BB & void MeDoSplitCEdge::DealWithTryBB(MeFunction &func, BB &pred, BB &succ, BB *&newBB, bool &isInsertAfterPred) const { if (!succ.GetStmtNodes().empty() && succ.GetStmtNodes().front().GetOpCode() == OP_try) { - newBB = &func.InsertNewBasicBlock(pred, false); + newBB = &func.GetCfg()->InsertNewBasicBlock(pred, false); isInsertAfterPred = true; if (pred.GetAttributes(kBBAttrIsTryEnd)) { newBB->SetAttributes(kBBAttrIsTryEnd); pred.ClearAttributes(kBBAttrIsTryEnd); - func.SetTryBBByOtherEndTryBB(newBB, &pred); + func.GetCfg()->SetTryBBByOtherEndTryBB(newBB, &pred); } } else { - newBB = &func.InsertNewBasicBlock(succ); + newBB = &func.GetCfg()->InsertNewBasicBlock(succ); isInsertAfterPred = false; } } @@ -121,12 +121,13 @@ void MeDoSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) con ASSERT(!succ.GetAttributes(kBBAttrIsCatch), "BreakCriticalEdge: cannot break an EH edge"); // create newBB and set pred/succ BB *newBB = nullptr; + MeCFG *cfg = func.GetCfg(); // use replace instead of remove/add to keep position in pred/succ bool isInsertAfterPred = false; bool needUpdateTryAttr = true; size_t index = succ.GetPred().size(); - if (&pred == func.GetCommonEntryBB()) { - newBB = &func.InsertNewBasicBlock(*func.GetFirstBB()); + if (&pred == cfg->GetCommonEntryBB()) { + newBB = &cfg->InsertNewBasicBlock(*cfg->GetFirstBB()); newBB->SetAttributes(kBBAttrIsEntry); succ.ClearAttributes(kBBAttrIsEntry); pred.RemoveEntry(succ); @@ -136,7 +137,7 @@ void MeDoSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) con if (pred.GetAttributes(kBBAttrIsTry)) { DealWithTryBB(func, pred, succ, newBB, isInsertAfterPred); } else { - newBB = func.NewBasicBlock(); + newBB = cfg->NewBasicBlock(); needUpdateTryAttr = false; } while (index > 0) { @@ -170,10 +171,14 @@ void MeDoSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) con } AnalysisResult *MeDoSplitCEdge::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + MeCFG *cfg = func->GetCfg(); + if (cfg == NULL) { + cfg = static_cast(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); + } std::vector> criticalEdge; - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func->common_exit()) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -194,16 +199,16 @@ AnalysisResult *MeDoSplitCEdge::Run(MeFunction *func, MeFuncResultMgr *m, Module } } // separate treatment for commonEntryBB's succ BBs - for (BB *entryBB : func->GetCommonEntryBB()->GetSucc()) { + for (BB *entryBB : cfg->GetCommonEntryBB()->GetSucc()) { if (!entryBB->GetPred().empty()) { - criticalEdge.push_back(std::make_pair(func->GetCommonEntryBB(), entryBB)); + criticalEdge.push_back(std::make_pair(cfg->GetCommonEntryBB(), entryBB)); } } if (!criticalEdge.empty()) { if (DEBUGFUNC(func)) { LogInfo::MapleLogger() << "*******************before break dump function*****************\n"; func->DumpFunctionNoSSA(); - func->GetTheCfg()->DumpToFile("cfgbeforebreak"); + func->GetCfg()->DumpToFile("cfgbeforebreak"); } for (auto it = criticalEdge.begin(); it != criticalEdge.end(); ++it) { BreakCriticalEdge(*func, *((*it).first), *((*it).second)); @@ -211,7 +216,7 @@ AnalysisResult *MeDoSplitCEdge::Run(MeFunction *func, MeFuncResultMgr *m, Module if (DEBUGFUNC(func)) { LogInfo::MapleLogger() << "******************after break dump function******************\n"; func->Dump(true); - func->GetTheCfg()->DumpToFile("cfgafterbreak"); + func->GetCfg()->DumpToFile("cfgafterbreak"); } m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, func); } diff --git a/src/mapleall/maple_me/src/me_delegate_rc.cpp b/src/mapleall/maple_me/src/me_delegate_rc.cpp index aba7e492851af6622b2b76f3cd55fd00a89e8142..50c0b4d8fddaba18f212cd727975b588729d00f2 100644 --- a/src/mapleall/maple_me/src/me_delegate_rc.cpp +++ b/src/mapleall/maple_me/src/me_delegate_rc.cpp @@ -685,8 +685,9 @@ void DelegateRC::RenameDelegatedRefVarUses(MeStmt &meStmt, MeExpr &meExpr) { } void DelegateRC::SetCantDelegateAndCountUses() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto &bb = **bIt; SetCantDelegate(bb.GetMePhiList()); for (auto &stmt : bb.GetMeStmts()) { @@ -708,8 +709,9 @@ void DelegateRC::SetCantDelegateAndCountUses() { } void DelegateRC::DelegateStmtRC() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto &bb = **bIt; for (auto &stmt : bb.GetMeStmts()) { bool withDecref = false; @@ -724,8 +726,9 @@ void DelegateRC::DelegateStmtRC() { std::set DelegateRC::RenameAndGetLiveLocalRefVar() { std::set liveLocalrefvars; - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto &bb = **bIt; for (auto &stmt : bb.GetMeStmts()) { if (IsVarDecRefStmt(stmt)) { @@ -768,7 +771,8 @@ std::set DelegateRC::RenameAndGetLiveLocalRefVar() { } void DelegateRC::CleanUpDeadLocalRefVar(const std::set &liveLocalrefvars) { - for (BB *bb : func.GetCommonExitBB()->GetPred()) { + MeCFG *cfg = func.GetCfg(); + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { auto &meStmts = bb->GetMeStmts(); if (meStmts.empty() || meStmts.back().GetOp() != OP_return) { continue; @@ -799,8 +803,8 @@ void DelegateRC::CleanUpDeadLocalRefVar(const std::set &liveLocalrefvars intrin->EraseOpnds(intrin->GetOpnds().begin() + nextPos, intrin->GetOpnds().end()); } if (func.GetHints() & kPlacementRCed) { // delete decref if opnd not in livelocalrefvars - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetMeStmts()) { if (stmt.GetOp() != OP_decrefreset) { diff --git a/src/mapleall/maple_me/src/me_dominance.cpp b/src/mapleall/maple_me/src/me_dominance.cpp index 20f2832d131922c18b73bc24279749ef54f29baf..f8dd7db4f41d596e4f702b70b07be9cf659acd69 100644 --- a/src/mapleall/maple_me/src/me_dominance.cpp +++ b/src/mapleall/maple_me/src/me_dominance.cpp @@ -15,24 +15,28 @@ #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*) { + MeCFG *cfg = func->GetCfg(); + if (!cfg) { + cfg = static_cast(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); + } MemPool *memPool = NewMemPool(); - auto *dom = memPool->New(*memPool, *NewMemPool(), func->GetAllBBs(), - *func->GetCommonEntryBB(), *func->GetCommonExitBB()); + auto *dom = memPool->New(*memPool, *NewMemPool(), cfg->GetAllBBs(), + *cfg->GetCommonEntryBB(), *cfg->GetCommonExitBB()); dom->GenPostOrderID(); dom->ComputeDominance(); dom->ComputeDomFrontiers(); dom->ComputeDomChildren(); dom->ComputeIterDomFrontiers(); size_t num = 0; - dom->ComputeDtPreorder(*func->GetCommonEntryBB(), num); + dom->ComputeDtPreorder(*cfg->GetCommonEntryBB(), num); dom->GetDtPreOrder().resize(num); dom->ComputeDtDfn(); @@ -42,7 +46,7 @@ AnalysisResult *MeDoDominance::Run(MeFunction *func, MeFuncResultMgr*, ModuleRes dom->ComputePdomChildren(); dom->ComputeIterPdomFrontiers(); num = 0; - dom->ComputePdtPreorder(*func->GetCommonExitBB(), num); + dom->ComputePdtPreorder(*cfg->GetCommonExitBB(), num); dom->ResizePdtPreOrder(num); dom->ComputePdtDfn(); if (DEBUGFUNC(func)) { diff --git a/src/mapleall/maple_me/src/me_dse.cpp b/src/mapleall/maple_me/src/me_dse.cpp index 632c5f855e7125f895859a51b20b68d312f97a84..e6cc71110ce1973bbf5fc4abf12c8ed8677c1637 100644 --- a/src/mapleall/maple_me/src/me_dse.cpp +++ b/src/mapleall/maple_me/src/me_dse.cpp @@ -22,9 +22,9 @@ namespace maple { void MeDSE::VerifyPhi() const { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_exit()) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -54,7 +54,7 @@ void MeDSE::RunDSE() { DoDSE(); // remove unreached BB - func.GetTheCfg()->UnreachCodeAnalysis(true); + cfg->UnreachCodeAnalysis(true); VerifyPhi(); if (enableDebug) { func.Dump(true); diff --git a/src/mapleall/maple_me/src/me_emit.cpp b/src/mapleall/maple_me/src/me_emit.cpp index 6ed15b0ddfc662a37990ef671dd7b252baee45de..f1f4a514836c199239222710582dc7c374a9ddbb 100644 --- a/src/mapleall/maple_me/src/me_emit.cpp +++ b/src/mapleall/maple_me/src/me_emit.cpp @@ -25,7 +25,7 @@ namespace maple { AnalysisResult *MeDoEmit::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { static std::mutex mtx; ParallelGuard guard(mtx, ThreadEnv::IsMeParallel()); - if (func->NumBBs() > 0) { + if (func->GetCfg()->NumBBs() > 0) { if (!func->HasLaidOut()) { (void)m->GetAnalysisResult(MeFuncPhase_BBLAYOUT, func); CHECK_FATAL(func->HasLaidOut(), "Check bb layout phase."); @@ -63,7 +63,7 @@ AnalysisResult *MeDoEmit::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResult func->GetMirFunc()->Dump(); } if (DEBUGFUNC(func)) { - func->GetTheCfg()->DumpToFile("emit", true); + func->GetCfg()->DumpToFile("emit", true); } } return nullptr; diff --git a/src/mapleall/maple_me/src/me_fsaa.cpp b/src/mapleall/maple_me/src/me_fsaa.cpp index c54bf1d47ff86f64a4b6f867683f62a98b6293a5..317bf09d4ee670b6def7e9183436e9a1bde10074 100644 --- a/src/mapleall/maple_me/src/me_fsaa.cpp +++ b/src/mapleall/maple_me/src/me_fsaa.cpp @@ -146,8 +146,8 @@ AnalysisResult *MeDoFSAA::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResult ASSERT(dom != nullptr, "dominance phase has problem"); FSAA fsaa(func, dom); - - for (BB *bb : func->GetAllBBs()) { + auto cfg = func->GetCfg(); + for (BB *bb : cfg->GetAllBBs()) { if (bb != nullptr) { fsaa.ProcessBB(bb); } @@ -156,11 +156,11 @@ AnalysisResult *MeDoFSAA::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResult if (fsaa.needUpdateSSA) { ssa->runRenameOnly = true; - ssa->InitRenameStack(ssaTab->GetOriginalStTable(), func->GetAllBBs().size(), ssaTab->GetVersionStTable()); + ssa->InitRenameStack(ssaTab->GetOriginalStTable(), cfg->GetAllBBs().size(), ssaTab->GetVersionStTable()); // recurse down dominator tree in pre-order traversal - MapleSet *children = &dom->domChildren[func->GetCommonEntryBB()->GetBBId()]; + MapleSet *children = &dom->domChildren[cfg->GetCommonEntryBB()->GetBBId()]; for (BBId child : *children) { - ssa->RenameBB(*func->GetBBFromID(child)); + ssa->RenameBB(*cfg->GetBBFromID(child)); } ssa->VerifySSA(); diff --git a/src/mapleall/maple_me/src/me_function.cpp b/src/mapleall/maple_me/src/me_function.cpp index 8e461ef08723a23b7f3f606601c4f77daa82618f..da437a9a468187f74b00a194aec7c42f0107db29 100644 --- a/src/mapleall/maple_me/src/me_function.cpp +++ b/src/mapleall/maple_me/src/me_function.cpp @@ -30,12 +30,11 @@ MeFunction *globalFunc = nullptr; MeIRMap *globalIRMap = nullptr; SSATab *globalSSATab = nullptr; #endif -void MeFunction::PartialInit(bool isSecondPass) { +void MeFunction::PartialInit() { theCFG = nullptr; irmap = nullptr; regNum = 0; hasEH = false; - secondPass = isSecondPass; ConstantFold cf(mirModule); cf.Simplify(mirModule.CurFunction()->GetBody()); if (mirModule.IsJavaModule() && (!mirModule.CurFunction()->GetInfoVector().empty())) { @@ -55,8 +54,8 @@ void MeFunction::DumpFunction() const { DumpFunctionNoSSA(); return; } - auto eIt = valid_end(); - for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + auto eIt = theCFG->valid_end(); + for (auto bIt = theCFG->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->DumpHeader(&mirModule); for (auto &phiPair : bb->GetPhiList()) { @@ -69,8 +68,8 @@ void MeFunction::DumpFunction() const { } void MeFunction::DumpFunctionNoSSA() const { - auto eIt = valid_end(); - for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + auto eIt = theCFG->valid_end(); + for (auto bIt = theCFG->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->DumpHeader(&mirModule); for (auto &phiPair : bb->GetPhiList()) { @@ -83,8 +82,8 @@ void MeFunction::DumpFunctionNoSSA() const { } void MeFunction::DumpMayDUFunction() const { - auto eIt = valid_end(); - for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + auto eIt = theCFG->valid_end(); + for (auto bIt = theCFG->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->DumpHeader(&mirModule); bool skipStmt = false; @@ -114,8 +113,8 @@ void MeFunction::Dump(bool DumpSimpIr) const { DumpFunction(); return; } - auto eIt = valid_end(); - for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { + auto eIt = theCFG->valid_end(); + for (auto bIt = theCFG->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->DumpHeader(&mirModule); bb->DumpMePhiList(irmap); @@ -125,345 +124,6 @@ void MeFunction::Dump(bool DumpSimpIr) const { } } -void MeFunction::SetTryBlockInfo(const StmtNode *nextStmt, StmtNode *tryStmt, BB *lastTryBB, BB *curBB, BB *newBB) { - if (nextStmt->GetOpCode() == OP_endtry) { - curBB->SetAttributes(kBBAttrIsTryEnd); - ASSERT(lastTryBB != nullptr, "null ptr check"); - endTryBB2TryBB[curBB] = lastTryBB; - } else { - newBB->SetAttributes(kBBAttrIsTry); - bbTryNodeMap[newBB] = tryStmt; - } -} - -void MeFunction::CreateBasicBlocks() { - if (CurFunction()->IsEmpty()) { - if (!MeOption::quiet) { - LogInfo::MapleLogger() << "function is empty, cfg is nullptr\n"; - } - return; - } - // create common_entry/exit bb first as bbVec[0] and bb_vec[1] - auto *commonEntryBB = NewBasicBlock(); - commonEntryBB->SetAttributes(kBBAttrIsEntry); - auto *commonExitBB = NewBasicBlock(); - commonExitBB->SetAttributes(kBBAttrIsExit); - auto *firstBB = NewBasicBlock(); - firstBB->SetAttributes(kBBAttrIsEntry); - StmtNode *nextStmt = CurFunction()->GetBody()->GetFirst(); - ASSERT(nextStmt != nullptr, "function has no statement"); - BB *curBB = firstBB; - StmtNode *tryStmt = nullptr; // record current try stmt for map - BB *lastTryBB = nullptr; // bb containing try_stmt - do { - StmtNode *stmt = nextStmt; - nextStmt = stmt->GetNext(); - switch (stmt->GetOpCode()) { - case OP_goto: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKind(kBBGoto); - if (nextStmt != nullptr) { - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - } - curBB = newBB; - } - break; - } - case OP_igoto: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKind(kBBIgoto); - if (nextStmt != nullptr) { - curBB = NewBasicBlock(); - } - break; - } - case OP_dassign: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - if (static_cast(stmt)->GetRHS()->MayThrowException()) { - stmt->SetOpCode(OP_maydassign); - if (tryStmt != nullptr) { - // breaks new BB only inside try blocks - curBB->SetLast(stmt); - curBB->SetKind(kBBFallthru); - BB *newBB = NewBasicBlock(); - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - curBB = newBB; - break; - } - } - if ((nextStmt == nullptr) && to_ptr(curBB->GetStmtNodes().rbegin()) == nullptr) { - curBB->SetLast(stmt); - } - break; - } - case OP_brfalse: - case OP_brtrue: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKind(kBBCondGoto); - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - } - curBB = newBB; - break; - } - case OP_if: - case OP_doloop: - case OP_dowhile: - case OP_while: { - ASSERT(false, "not yet implemented"); - break; - } - case OP_throw: - if (tryStmt != nullptr) { - // handle as goto - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKind(kBBGoto); - if (nextStmt != nullptr) { - BB *newBB = NewBasicBlock(); - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - curBB = newBB; - } - break; - } - // fall thru to handle as return - [[clang::fallthrough]]; - case OP_gosub: - case OP_retsub: - case OP_return: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKindReturn(); - if (nextStmt != nullptr) { - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - } - curBB = newBB; - if (stmt->GetOpCode() == OP_gosub) { - curBB->SetAttributes(kBBAttrIsEntry); - } - } - break; - } - case OP_endtry: - if (mirModule.IsJavaModule()) { - if (tryStmt == nullptr) { - break; - } - /* skip OP_entry and generate it in emit phase */ - ASSERT(lastTryBB != nullptr, "null ptr check"); - tryStmt = nullptr; // reset intryblocks - if (!curBB->IsEmpty()) { - StmtNode *lastStmt = stmt->GetPrev(); - ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || - curBB->GetStmtNodes().rbegin().base().d() == lastStmt, - "something wrong building BB"); - curBB->SetLast(lastStmt); - if (curBB->GetKind() == kBBUnknown) { - curBB->SetKind(kBBFallthru); - } - curBB->SetAttributes(kBBAttrIsTryEnd); - endTryBB2TryBB[curBB] = lastTryBB; - curBB = NewBasicBlock(); - } else if (curBB->GetBBLabel() != 0) { - // create the empty BB - curBB->SetKind(kBBFallthru); - curBB->SetAttributes(kBBAttrIsTryEnd); - endTryBB2TryBB[curBB] = lastTryBB; - curBB = NewBasicBlock(); - } else { - } // endtry has already been processed in SetTryBlockInfo() - lastTryBB = nullptr; - } else { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - if ((nextStmt == nullptr) && (curBB->GetStmtNodes().rbegin().base().d() == nullptr)) { - curBB->SetLast(stmt); - } - } - break; - case OP_try: { - // start a new bb or with a label - if (!curBB->IsEmpty()) { - // prepare a new bb - StmtNode *lastStmt = stmt->GetPrev(); - ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || - curBB->GetStmtNodes().rbegin().base().d() == lastStmt, - "something wrong building BB"); - curBB->SetLast(lastStmt); - if (curBB->GetKind() == kBBUnknown) { - curBB->SetKind(kBBFallthru); - } - BB *newBB = NewBasicBlock(); - // assume no nested try, so no need to call SetTryBlockInfo() - curBB = newBB; - } - curBB->SetFirst(stmt); - tryStmt = stmt; - lastTryBB = curBB; - curBB->SetAttributes(kBBAttrIsTry); - bbTryNodeMap[curBB] = tryStmt; - // prepare a new bb that contains only a OP_try. It is needed for - // dse to work correctly: assignments in a try block should not affect - // assignments before the try block as exceptions might occur. - curBB->SetLast(stmt); - curBB->SetKind(kBBFallthru); - BB *newBB = NewBasicBlock(); - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - curBB = newBB; - break; - } - case OP_catch: { - // start a new bb or with a label - if (!curBB->IsEmpty()) { - // prepare a new bb - StmtNode *lastStmt = stmt->GetPrev(); - ASSERT(curBB->GetStmtNodes().rbegin().base().d() == nullptr || - curBB->GetStmtNodes().rbegin().base().d() == lastStmt, - "something wrong building BB"); - curBB->SetLast(lastStmt); - if (curBB->GetKind() == kBBUnknown) { - curBB->SetKind(kBBFallthru); - } - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - } - curBB = newBB; - } - curBB->SetFirst(stmt); - curBB->SetAttributes(kBBAttrIsCatch); - auto *catchNode = static_cast(stmt); - const MapleVector &exceptionTyIdxVec = catchNode->GetExceptionTyIdxVec(); - - for (TyIdx exceptIdx : exceptionTyIdxVec) { - MIRType *eType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(exceptIdx); - ASSERT(eType != nullptr && (eType->GetPrimType() == PTY_ptr || eType->GetPrimType() == PTY_ref), - "wrong exception type"); - auto *exceptType = static_cast(eType); - MIRType *pointType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(exceptType->GetPointedTyIdx()); - const std::string &eName = GlobalTables::GetStrTable().GetStringFromStrIdx(pointType->GetNameStrIdx()); - if ((pointType->GetPrimType() == PTY_void) || (eName == "Ljava/lang/Throwable;") || - (eName == "Ljava/lang/Exception;")) { - // "Ljava/lang/Exception;" is risk to set isJavaFinally because it - // only deal with "throw exception". if throw error, it's wrong - curBB->SetAttributes(kBBAttrIsJavaFinally); // this is a start of finally handler - } - } - break; - } - case OP_label: { - auto *labelNode = static_cast(stmt); - LabelIdx labelIdx = labelNode->GetLabelIdx(); - if (!curBB->IsEmpty() || curBB->GetBBLabel() != 0) { - // prepare a new bb - StmtNode *lastStmt = stmt->GetPrev(); - ASSERT((curBB->GetStmtNodes().rbegin().base().d() == nullptr || - curBB->GetStmtNodes().rbegin().base().d() == lastStmt), - "something wrong building BB"); - if (curBB->GetStmtNodes().rbegin().base().d() == nullptr && (lastStmt->GetOpCode() != OP_label)) { - if (mirModule.IsJavaModule() && lastStmt->GetOpCode() == OP_endtry) { - if (curBB->GetStmtNodes().empty()) { - curBB->SetLast(nullptr); - } else { - // find a valid stmt which is not label or endtry - StmtNode *p = lastStmt->GetPrev(); - ASSERT(p != nullptr, "null ptr check"); - ASSERT(p->GetOpCode() != OP_label, "runtime check error"); - ASSERT(p->GetOpCode() != OP_endtry, "runtime check error"); - curBB->SetLast(p); - } - } else { - curBB->SetLast(lastStmt); - } - } - if (curBB->GetKind() == kBBUnknown) { - curBB->SetKind(kBBFallthru); - } - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - newBB->SetAttributes(kBBAttrIsTry); - bbTryNodeMap[newBB] = tryStmt; - } - curBB = newBB; - } - labelBBIdMap[labelIdx] = curBB; - curBB->SetBBLabel(labelIdx); - break; - } - case OP_jscatch: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetAttributes(kBBAttrIsEntry); - curBB->SetAttributes(kBBAttrIsJSCatch); - break; - } - case OP_finally: { - ASSERT(curBB->GetStmtNodes().empty(), "runtime check error"); - curBB->SetFirst(stmt); - curBB->SetAttributes(kBBAttrIsEntry); - curBB->SetAttributes(kBBAttrIsJSFinally); - break; - } - case OP_switch: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - curBB->SetLast(stmt); - curBB->SetKind(kBBSwitch); - BB *newBB = NewBasicBlock(); - if (tryStmt != nullptr) { - SetTryBlockInfo(nextStmt, tryStmt, lastTryBB, curBB, newBB); - } - curBB = newBB; - break; - } - default: { - if (curBB->IsEmpty()) { - curBB->SetFirst(stmt); - } - if ((nextStmt == nullptr) && (curBB->GetStmtNodes().rbegin().base().d() == nullptr)) { - curBB->SetLast(stmt); - } - break; - } - } - } while (nextStmt != nullptr); - ASSERT(tryStmt == nullptr, "unclosed try"); // tryandendtry should be one-one mapping - ASSERT(lastTryBB == nullptr, "unclosed tryBB"); // tryandendtry should be one-one mapping - auto *lastBB = curBB; - if (lastBB->IsEmpty()) { - // insert a return statement - lastBB->SetFirst(mirModule.GetMIRBuilder()->CreateStmtReturn(nullptr)); - lastBB->SetLast(lastBB->GetStmtNodes().begin().d()); - lastBB->SetKindReturn(); - } else if (lastBB->GetKind() == kBBUnknown) { - lastBB->SetKindReturn(); - lastBB->SetAttributes(kBBAttrIsExit); - } -} - void MeFunction::Prepare(unsigned long rangeNum) { if (!MeOption::quiet) { LogInfo::MapleLogger() << "---Preparing Function < " << CurFunction()->GetName() << " > [" << rangeNum @@ -476,21 +136,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 { @@ -499,140 +144,6 @@ void MeFunction::Verify() const { theCFG->VerifyLabels(); } -BB *MeFunction::NewBasicBlock() { - BB *newBB = memPool->New(&alloc, &versAlloc, BBId(nextBBId++)); - bbVec.push_back(newBB); - return newBB; -} - -// new a basic block and insert before position or after position -BB &MeFunction::InsertNewBasicBlock(const BB &position, bool isInsertBefore) { - BB *newBB = memPool->New(&alloc, &versAlloc, BBId(nextBBId++)); - - auto bIt = std::find(begin(), end(), &position); - auto idx = position.GetBBId(); - if (!isInsertBefore) { - ++bIt; - ++idx; - } - auto newIt = bbVec.insert(bIt, newBB); - auto eIt = end(); - // update bb's idx - for (auto it = newIt; it != eIt; ++it) { - if ((*it) != nullptr) { - (*it)->SetBBId(BBId(idx)); - } - ++idx; - } - return *newBB; -} - -void MeFunction::DeleteBasicBlock(const BB &bb) { - ASSERT(bbVec[bb.GetBBId()] == &bb, "runtime check error"); - /* update first_bb_ and last_bb if needed */ - bbVec.at(bb.GetBBId()) = nullptr; -} - -/* get next bb in bbVec */ -BB *MeFunction::NextBB(const BB *bb) { - auto bbIt = std::find(begin(), end(), bb); - CHECK_FATAL(bbIt != end(), "bb must be inside bb_vec"); - for (auto it = ++bbIt; it != end(); ++it) { - if (*it != nullptr) { - return *it; - } - } - return nullptr; -} - -/* get prev bb in bbVec */ -BB *MeFunction::PrevBB(const BB *bb) { - auto bbIt = std::find(rbegin(), rend(), bb); - CHECK_FATAL(bbIt != rend(), "bb must be inside bb_vec"); - for (auto it = ++bbIt; it != rend(); ++it) { - if (*it != nullptr) { - return *it; - } - } - return nullptr; -} - -/* clone stmtnode in orig bb to newBB */ -void MeFunction::CloneBasicBlock(BB &newBB, const BB &orig) { - if (orig.IsEmpty()) { - return; - } - for (const auto &stmt : orig.GetStmtNodes()) { - StmtNode *newStmt = stmt.CloneTree(mirModule.GetCurFuncCodeMPAllocator()); - newStmt->SetNext(nullptr); - newStmt->SetPrev(nullptr); - newBB.AddStmtNode(newStmt); - if (meSSATab != nullptr) { - meSSATab->CreateSSAStmt(*newStmt, &newBB); - } - } -} - -void MeFunction::SplitBBPhysically(BB &bb, StmtNode &splitPoint, BB &newBB) { - StmtNode *newBBStart = splitPoint.GetNext(); - // Fix Stmt in BB. - if (newBBStart != nullptr) { - newBBStart->SetPrev(nullptr); - for (StmtNode *stmt = newBBStart; stmt != nullptr;) { - StmtNode *nextStmt = stmt->GetNext(); - newBB.AddStmtNode(stmt); - if (meSSATab != nullptr) { - meSSATab->CreateSSAStmt(*stmt, &newBB); - } - stmt = nextStmt; - } - } - bb.SetLast(&splitPoint); - splitPoint.SetNext(nullptr); -} - -/* Split BB at split_point */ -BB &MeFunction::SplitBB(BB &bb, StmtNode &splitPoint, BB *newBB) { - if (newBB == nullptr) { - newBB = memPool->New(&alloc, &versAlloc, BBId(nextBBId++)); - } - SplitBBPhysically(bb, splitPoint, *newBB); - // Fix BB in CFG - newBB->SetKind(bb.GetKind()); - bb.SetKind(kBBFallthru); - auto bIt = std::find(begin(), end(), &bb); - auto idx = bb.GetBBId(); - auto newIt = bbVec.insert(++bIt, newBB); - auto eIt = end(); - // update bb's idx - for (auto it = newIt; it != eIt; ++it) { - ++idx; - if ((*it) != nullptr) { - (*it)->SetBBId(BBId(idx)); - } - } - // Special Case: commonExitBB is orig bb's succ - auto *commonExitBB = *common_exit(); - if (bb.IsPredBB(*commonExitBB)) { - commonExitBB->RemoveExit(bb); - commonExitBB->AddExit(*newBB); - } - while (bb.GetSucc().size() > 0) { - BB *succ = bb.GetSucc(0); - succ->ReplacePred(&bb, newBB); - } - bb.AddSucc(*newBB); - // Setup flags - newBB->CopyFlagsAfterSplit(bb); - if (newBB->GetAttributes(kBBAttrIsTryEnd)) { - endTryBB2TryBB[newBB] = endTryBB2TryBB[&bb]; - endTryBB2TryBB[&bb] = nullptr; - bb.ClearAttributes(kBBAttrIsTryEnd); - } - bb.ClearAttributes(kBBAttrIsExit); - return *newBB; -} - /* create label for bb */ LabelIdx MeFunction::GetOrCreateBBLabel(BB &bb) { LabelIdx label = bb.GetBBLabel(); @@ -642,7 +153,7 @@ LabelIdx MeFunction::GetOrCreateBBLabel(BB &bb) { label = mirModule.CurFunction()->GetLabelTab()->CreateLabelWithPrefix('m'); mirModule.CurFunction()->GetLabelTab()->AddToStringLabelMap(label); bb.SetBBLabel(label); - labelBBIdMap.insert(std::make_pair(label, &bb)); + theCFG->SetLabelBBAt(label, &bb); return label; } @@ -681,7 +192,7 @@ void MeFunction::BuildSCCDFS(BB &bb, uint32 &visitIndex, std::vector stackTopId = visitStack.top(); visitStack.pop(); inStack[stackTopId] = false; - auto *topBB = static_cast(GetAllBBs()[stackTopId]); + auto *topBB = static_cast(theCFG->GetAllBBs()[stackTopId]); sccNode->AddBBNode(topBB); sccOfBB[stackTopId] = sccNode; } while (stackTopId != id); @@ -691,8 +202,8 @@ void MeFunction::BuildSCCDFS(BB &bb, uint32 &visitIndex, std::vector } void MeFunction::VerifySCC() { - for (BB *bb : GetAllBBs()) { - if (bb == nullptr || bb == GetCommonExitBB()) { + for (BB *bb : theCFG->GetAllBBs()) { + if (bb == nullptr || bb == theCFG->GetCommonExitBB()) { continue; } SCCOfBBs *scc = sccOfBB.at(bb->UintID()); @@ -701,18 +212,19 @@ void MeFunction::VerifySCC() { } void MeFunction::BuildSCC() { + uint32_t n = theCFG->GetAllBBs().size(); sccTopologicalVec.clear(); sccOfBB.clear(); - sccOfBB.assign(GetAllBBs().size(), nullptr); - std::vector visitedOrder(GetAllBBs().size(), 0); - std::vector lowestOrder(GetAllBBs().size(), 0); - std::vector inStack(GetAllBBs().size(), false); + sccOfBB.assign(n, nullptr); + std::vector visitedOrder(n, 0); + std::vector lowestOrder(n, 0); + std::vector inStack(n, false); std::vector sccNodes; uint32 visitIndex = 1; std::stack visitStack; // Starting from common entry bb for DFS - BuildSCCDFS(*GetCommonEntryBB(), visitIndex, sccNodes, visitedOrder, lowestOrder, inStack, visitStack); + BuildSCCDFS(*theCFG->GetCommonEntryBB(), visitIndex, sccNodes, visitedOrder, lowestOrder, inStack, visitStack); for (SCCOfBBs *scc : sccNodes) { scc->Verify(sccOfBB); diff --git a/src/mapleall/maple_me/src/me_gc_lowering.cpp b/src/mapleall/maple_me/src/me_gc_lowering.cpp index 12d47d9cc4baf3236fd7a2f73158370119c6ed87..cb1e8e0a8e3cbd675f28a7d6de0f44a787685d0d 100644 --- a/src/mapleall/maple_me/src/me_gc_lowering.cpp +++ b/src/mapleall/maple_me/src/me_gc_lowering.cpp @@ -51,9 +51,9 @@ void GCLowering::Prepare() { } void GCLowering::GCLower() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } GCLower(**bIt); @@ -200,11 +200,12 @@ void GCLowering::HandleWriteReferent(IassignMeStmt &stmt) { // LHS of type ref in assign // return value of type ref void GCLowering::CheckRefs() { + auto cfg = func.GetCfg(); ParseCheckFlag(); if (checkRefFormal) { CheckFormals(); } - for (BB *bb : func.GetAllBBs()) { + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } @@ -243,7 +244,7 @@ void GCLowering::CheckFormals() { return; } - BB *firstBB = func.GetFirstBB(); + BB *firstBB = func.GetCfg()->GetFirstBB(); for (size_t i = 0; i < mirFunc->GetFormalCount(); ++i) { MIRSymbol *formalSt = mirFunc->GetFormal(i); if (formalSt == nullptr || formalSt->GetType()->GetPrimType() != PTY_ref) { diff --git a/src/mapleall/maple_me/src/me_gc_write_barrier_opt.cpp b/src/mapleall/maple_me/src/me_gc_write_barrier_opt.cpp index 1b88c4b7f3ceaba56119ce6deda31af8d5471fb6..20147757215786f06445edeae4cacfc2de9b5dbc 100644 --- a/src/mapleall/maple_me/src/me_gc_write_barrier_opt.cpp +++ b/src/mapleall/maple_me/src/me_gc_write_barrier_opt.cpp @@ -26,13 +26,15 @@ AnalysisResult *MeDoGCWriteBarrierOpt::Run(MeFunction *func, MeFuncResultMgr *fu GCWriteBarrierOpt gcWriteBarrierOpt(*func, *dom, DEBUGFUNC(func)); gcWriteBarrierOpt.Prepare(); std::map> writeBarrierMap; - BB *entryBB = func->GetCommonEntryBB(); + BB *entryBB = func->GetCfg()->GetCommonEntryBB(); gcWriteBarrierOpt.GCLower(*entryBB, writeBarrierMap); gcWriteBarrierOpt.Finish(); return nullptr; } void GCWriteBarrierOpt::Prepare() { + callBBs.resize(func.GetCfg()->NumBBs()); + visited.resize(func.GetCfg()->NumBBs()); if (enabledDebug) { LogInfo::MapleLogger() << "\n============== Before GC WRITE BARRIER OPT =============" << '\n'; func.Dump(false); @@ -73,7 +75,7 @@ void GCWriteBarrierOpt::GCLower(BB &bb, std::map> & visited[bb.GetBBId()] = true; const MapleSet domChildren = dominance.GetDomChildren(bb.GetBBId()); for (const auto &childBBId : domChildren) { - BB *child = func.GetAllBBs().at(childBBId); + BB *child = func.GetCfg()->GetBBFromID(childBBId); if (child == nullptr) { continue; } @@ -156,7 +158,7 @@ bool GCWriteBarrierOpt::HasYieldPoint(const MeStmt &start, const MeStmt &end) { } const MapleSet domChildren = dominance.GetDomChildren(startBB->GetBBId()); const MapleSet pdomChildren = dominance.GetPdomChildrenItem(endBB->GetBBId()); - const MapleVector bbVec = func.GetAllBBs(); + const MapleVector bbVec = func.GetCfg()->GetAllBBs(); for (const auto &childBBId : domChildren) { if (pdomChildren.find(childBBId) == pdomChildren.end()) { continue; diff --git a/src/mapleall/maple_me/src/me_hdse.cpp b/src/mapleall/maple_me/src/me_hdse.cpp index ab52e78748386048baa988766f8ce35b4bf3f94b..0b7832ff98adc0b86cbfabea8445da751b70d2e9 100644 --- a/src/mapleall/maple_me/src/me_hdse.cpp +++ b/src/mapleall/maple_me/src/me_hdse.cpp @@ -86,8 +86,9 @@ void MeHDSE::BackwardSubstitution() { } void MeDoHDSE::MakeEmptyTrysUnreachable(MeFunction &func) { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { BB *tryBB = *bIt; // get next valid bb auto endTryIt = bIt; @@ -162,8 +163,8 @@ AnalysisResult *MeDoHDSE::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResult hdse.DoHDSE(); hdse.BackwardSubstitution(); MakeEmptyTrysUnreachable(*func); - (void)func->GetTheCfg()->UnreachCodeAnalysis(/* update_phi = */ true); - func->GetTheCfg()->WontExitAnalysis(); + (void)func->GetCfg()->UnreachCodeAnalysis(/* update_phi = */ true); + func->GetCfg()->WontExitAnalysis(); m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, func); m->InvalidAnalysisResult(MeFuncPhase_MELOOP, func); if (DEBUGFUNC(func)) { diff --git a/src/mapleall/maple_me/src/me_irmap.cpp b/src/mapleall/maple_me/src/me_irmap.cpp index e10f5759cbf4f2fa0013e1f248c0c54dbc726f89..46922d8cb097057189425d585479a526f3e0dd41 100644 --- a/src/mapleall/maple_me/src/me_irmap.cpp +++ b/src/mapleall/maple_me/src/me_irmap.cpp @@ -21,9 +21,10 @@ void MeIRMap::Dump() { MIRFunction *mirFunction = func.GetMirFunc(); MemPool *backup = mirFunction->GetCodeMempool(); mirFunction->SetMemPool(new ThreadLocalMemPool(memPoolCtrler, "IR Dump")); + auto cfg = func.GetCfg(); LogInfo::MapleLogger() << "===================Me IR dump==================\n"; - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->DumpHeader(&GetMIRModule()); LogInfo::MapleLogger() << "frequency : " << bb->GetFrequency() << "\n"; diff --git a/src/mapleall/maple_me/src/me_irmap_build.cpp b/src/mapleall/maple_me/src/me_irmap_build.cpp index 0b66fb87e0884b9dc630bb81c3115d863918d423..ecd9dd39f79f9fa1236c44c37ea15c65ec4dad8b 100644 --- a/src/mapleall/maple_me/src/me_irmap_build.cpp +++ b/src/mapleall/maple_me/src/me_irmap_build.cpp @@ -24,9 +24,13 @@ namespace maple { AnalysisResult *MeDoIRMapBuild::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, ModuleResultMgr *moduleResMgr) { (void)moduleResMgr; + // get all required analysis result IRMap need, cfg, ssa may be invalid in previous phase + (void)(funcResMgr->GetAnalysisResult(MeFuncPhase_MECFG, func)); + (void)(funcResMgr->GetAnalysisResult(MeFuncPhase_SSATAB, func)); + (void)(funcResMgr->GetAnalysisResult(MeFuncPhase_ALIASCLASS, func)); + (void)(funcResMgr->GetAnalysisResult(MeFuncPhase_SSA, func)); Dominance *dom = static_cast(funcResMgr->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); CHECK_FATAL(dom != nullptr, "dominance phase has problem"); - MemPool *irmapmp = NewMemPool(); MeIRMap *irMap = irmapmp->New(*func, *irmapmp); @@ -35,23 +39,25 @@ AnalysisResult *MeDoIRMapBuild::Run(MeFunction *func, MeFuncResultMgr *funcResMg globalIRMap = irMap; #endif MemPool *propMp = nullptr; + MeCFG *cfg = func->GetCfg(); if (!func->GetMIRModule().IsJavaModule() && MeOption::propDuringBuild) { // create propgation propMp = memPoolCtrler.NewMemPool("meirbuild prop", true /* isLocalPool */); MeProp meprop(*irMap, *dom, *propMp, Prop::PropConfig{false, false, false, false, false, false}); IRMapBuild irmapbuild(irMap, dom, &meprop); - std::vector bbIrmapProcessed(func->NumBBs(), false); - irmapbuild.BuildBB(*func->GetCommonEntryBB(), bbIrmapProcessed); + std::vector bbIrmapProcessed(cfg->NumBBs(), false); + irmapbuild.BuildBB(*cfg->GetCommonEntryBB(), bbIrmapProcessed); } else { IRMapBuild irmapbuild(irMap, dom, nullptr); - std::vector bbIrmapProcessed(func->NumBBs(), false); - irmapbuild.BuildBB(*func->GetCommonEntryBB(), bbIrmapProcessed); + std::vector bbIrmapProcessed(cfg->NumBBs(), false); + irmapbuild.BuildBB(*cfg->GetCommonEntryBB(), bbIrmapProcessed); } if (DEBUGFUNC(func)) { irMap->Dump(); } // delete mempool for meirmap temporaries + // now versmp use one mempool as analysis result, just clear the content here // delete input IR code for current function MIRFunction *mirFunc = func->GetMirFunc(); mirFunc->ReleaseCodeMemory(); @@ -62,8 +68,8 @@ AnalysisResult *MeDoIRMapBuild::Run(MeFunction *func, MeFuncResultMgr *funcResMg func->GetMeSSATab()->GetVersionStTable().SetVersionStVectorItem(i, nullptr); } // clear BB's phiList which uses versionst; nullify first_stmt_, last_stmt_ - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; bb->ClearPhiList(); bb->SetFirst(nullptr); diff --git a/src/mapleall/maple_me/src/me_loop_analysis.cpp b/src/mapleall/maple_me/src/me_loop_analysis.cpp index 7b280c40f34a036dadd8ca5eefb66c2971e86233..48da3541faa2a3ed9c66ae4aca0ec6f2300f4179 100644 --- a/src/mapleall/maple_me/src/me_loop_analysis.cpp +++ b/src/mapleall/maple_me/src/me_loop_analysis.cpp @@ -81,7 +81,7 @@ void IdentifyLoops::InsertExitBB(LoopDesc &loop) { // process each BB in preorder traversal of dominator tree void IdentifyLoops::ProcessBB(BB *bb) { - if (bb == nullptr || bb == func.GetCommonExitBB()) { + if (bb == nullptr || bb == cfg->GetCommonExitBB()) { return; } for (BB *pred : bb->GetPred()) { @@ -110,7 +110,7 @@ void IdentifyLoops::ProcessBB(BB *bb) { // recursive call const MapleSet &domChildren = dominance->GetDomChildren(bb->GetBBId()); for (auto bbIt = domChildren.begin(); bbIt != domChildren.end(); ++bbIt) { - ProcessBB(func.GetAllBBs().at(*bbIt)); + ProcessBB(cfg->GetAllBBs().at(*bbIt)); } } @@ -131,10 +131,10 @@ void IdentifyLoops::Dump() const { void IdentifyLoops::MarkBB() { for (LoopDesc *meLoop : meLoops) { for (BBId bbId : meLoop->loopBBs) { - if (func.GetAllBBs().at(bbId) == nullptr) { + if (cfg->GetAllBBs().at(bbId) == nullptr) { continue; } - func.GetAllBBs().at(bbId)->SetAttributes(kBBAttrIsInLoopForEA); + cfg->GetAllBBs().at(bbId)->SetAttributes(kBBAttrIsInLoopForEA); } } } @@ -151,7 +151,7 @@ void IdentifyLoops::SetTryBB() { continue; } for (auto bbId : loop->loopBBs) { - BB *bb = func.GetBBFromID(bbId); + BB *bb = cfg->GetBBFromID(bbId); if (bb->GetAttributes(kBBAttrIsTry) || bb->GetAttributes(kBBAttrWontExit)) { loop->SetHasTryBB(true); break; @@ -175,8 +175,8 @@ void IdentifyLoops::SetIGotoBB() { bool IdentifyLoops::ProcessPreheaderAndLatch(LoopDesc &loop) { // If predsize of head is one, it means that one is entry bb. if (loop.head->GetPred().size() == 1) { - CHECK_FATAL(func.GetCommonEntryBB()->GetSucc(0) == loop.head, "succ of entry bb must be head"); - loop.preheader = func.GetCommonEntryBB(); + CHECK_FATAL(cfg->GetCommonEntryBB()->GetSucc(0) == loop.head, "succ of entry bb must be head"); + loop.preheader = cfg->GetCommonEntryBB(); CHECK_FATAL(!loop.head->GetPred(0)->GetAttributes(kBBAttrIsTry), "must not be kBBAttrIsTry"); loop.latch = loop.head->GetPred(0); return true; @@ -225,7 +225,7 @@ AnalysisResult *MeDoMeLoop::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResu ASSERT(dom != nullptr, "dominance phase has problem"); MemPool *meLoopMp = NewMemPool(); IdentifyLoops *identLoops = meLoopMp->New(meLoopMp, *func, dom); - identLoops->ProcessBB(func->GetCommonEntryBB()); + identLoops->ProcessBB(func->GetCfg()->GetCommonEntryBB()); identLoops->SetTryBB(); identLoops->SetIGotoBB(); for (auto loop : identLoops->GetMeLoops()) { diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index 291a7fcdf4797b303d06a760969d59ff70514791..723883a598c2e19abad777f11fcad2968bf33f0a 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -117,7 +117,7 @@ void MeDoLoopCanon::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapNewBasicBlock(); latchBB->SetAttributes(kBBAttrArtificial); // update pred bb pred.ReplaceSucc(&bb, latchBB); // replace pred.succ with latchBB and set pred of latchBB @@ -161,7 +161,7 @@ void MeDoLoopCanon::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapCloneBasicBlock(*latchBB, bb); // clone bb's succ to latchBB for (BB *succ : bb.GetSucc()) { ASSERT(!latchBB->GetAttributes(kBBAttrIsTry) || latchBB->GetSucc().empty(), @@ -188,10 +188,10 @@ void MeDoLoopCanon::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapvalid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -216,18 +216,18 @@ void MeDoLoopCanon::ExecuteLoopCanon(MeFunction &func, MeFuncResultMgr &m, Domin MapleVector> backEdges(localAlloc.Adapter()); MapleMap swapSuccs(std::less(), localAlloc.Adapter()); // collect backedge first: if bb dominator its pred, then the edge pred->bb is a backedge - eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; const MapleVector &preds = bb->GetPred(); for (BB *pred : preds) { - ASSERT(func.GetCommonEntryBB() != nullptr, "impossible"); + ASSERT(cfg->GetCommonEntryBB() != nullptr, "impossible"); ASSERT_NOT_NULL(pred); // bb is reachable from entry && bb dominator pred - if (dom.Dominate(*func.GetCommonEntryBB(), *bb) && dom.Dominate(*bb, *pred) && + if (dom.Dominate(*cfg->GetCommonEntryBB(), *bb) && dom.Dominate(*bb, *pred) && !pred->GetAttributes(kBBAttrWontExit) && (NeedConvert(&func, *bb, *pred, localAlloc, swapSuccs))) { if (DEBUGFUNC(&func)) { LogInfo::MapleLogger() << "find backedge " << bb->GetBBId() << " <-- " << pred->GetBBId() << '\n'; @@ -270,7 +270,7 @@ void MeDoLoopCanon::ExecuteLoopCanon(MeFunction &func, MeFuncResultMgr &m, Domin // Only when backege or preheader is not try bb, update head map. void MeDoLoopCanon::FindHeadBBs(MeFunction &func, Dominance &dom, const BB *bb) { - if (bb == nullptr || bb == func.GetCommonExitBB()) { + if (bb == nullptr || bb == func.GetCfg()->GetCommonExitBB()) { return; } bool hasTry = false; @@ -305,7 +305,7 @@ void MeDoLoopCanon::FindHeadBBs(MeFunction &func, Dominance &dom, const BB *bb) } const MapleSet &domChildren = dom.GetDomChildren(bb->GetBBId()); for (auto bbit = domChildren.begin(); bbit != domChildren.end(); ++bbit) { - FindHeadBBs(func, dom, func.GetAllBBs().at(*bbit)); + FindHeadBBs(func, dom, func.GetCfg()->GetAllBBs().at(*bbit)); } } @@ -336,9 +336,10 @@ void MeDoLoopCanon::UpdateTheOffsetOfStmtWhenTargetBBIsChange(MeFunction &func, // merge backedges with the same headBB void MeDoLoopCanon::Merge(MeFunction &func) { + MeCFG *cfg = func.GetCfg(); for (auto iter = heads.begin(); iter != heads.end(); ++iter) { - BB *head = func.GetBBFromID(iter->first); - auto *latchBB = func.NewBasicBlock(); + BB *head = cfg->GetBBFromID(iter->first); + auto *latchBB = cfg->NewBasicBlock(); latchBB->SetAttributes(kBBAttrArtificial); latchBB->SetKind(kBBFallthru); for (BB *tail : iter->second) { @@ -353,8 +354,9 @@ void MeDoLoopCanon::Merge(MeFunction &func) { } void MeDoLoopCanon::AddPreheader(MeFunction &func) { + MeCFG *cfg = func.GetCfg(); for (auto iter = heads.begin(); iter != heads.end(); ++iter) { - BB *head = func.GetBBFromID(iter->first); + BB *head = cfg->GetBBFromID(iter->first); std::vector preds; for (BB *pred : head->GetPred()) { // FindHeadBBs has filtered out this possibility. @@ -371,7 +373,7 @@ void MeDoLoopCanon::AddPreheader(MeFunction &func) { continue; } // add preheader - auto *preheader = func.NewBasicBlock(); + auto *preheader = cfg->NewBasicBlock(); preheader->SetAttributes(kBBAttrArtificial); preheader->SetKind(kBBFallthru); for (BB *pred : preds) { @@ -386,8 +388,9 @@ void MeDoLoopCanon::AddPreheader(MeFunction &func) { } void MeDoLoopCanon::InsertNewExitBB(MeFunction &func, LoopDesc &loop) { + MeCFG *cfg = func.GetCfg(); for (auto pair : loop.inloopBB2exitBBs) { - BB *curBB = func.GetBBFromID(pair.first); + BB *curBB = cfg->GetBBFromID(pair.first); if (curBB->GetKind() == kBBIgoto) { continue; } @@ -401,7 +404,7 @@ void MeDoLoopCanon::InsertNewExitBB(MeFunction &func, LoopDesc &loop) { if (!needNewExitBB) { continue; } else { - BB *newExitBB = func.NewBasicBlock(); + BB *newExitBB = cfg->NewBasicBlock(); newExitBB->SetKind(kBBFallthru); size_t index = succBB->GetPred().size(); while (index > 0) { @@ -425,6 +428,7 @@ void MeDoLoopCanon::InsertNewExitBB(MeFunction &func, LoopDesc &loop) { } void MeDoLoopCanon::InsertExitBB(MeFunction &func, LoopDesc &loop) { + MeCFG *cfg = func.GetCfg(); std::set traveledBBs; std::queue inLoopBBs; inLoopBBs.push(loop.head); @@ -445,11 +449,11 @@ void MeDoLoopCanon::InsertExitBB(MeFunction &func, LoopDesc &loop) { if (curBB->GetKind() == kBBCondGoto) { if (curBB->GetSucc().size() == 1) { CHECK_FATAL(false, "return bb"); - loop.InsertInloopBB2exitBBs(*curBB, *func.GetCommonExitBB()); + loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); } } else if (!curBB->GetStmtNodes().empty() && curBB->GetLast().GetOpCode() == OP_return) { CHECK_FATAL(false, "return bb"); - loop.InsertInloopBB2exitBBs(*curBB, *func.GetCommonExitBB()); + loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); } } } @@ -457,7 +461,8 @@ void MeDoLoopCanon::InsertExitBB(MeFunction &func, LoopDesc &loop) { } void MeDoLoopCanon::SplitCondGotBB(MeFunction &func, LoopDesc &loop) { - auto exitBB = func.GetBBFromID(loop.inloopBB2exitBBs.begin()->first); + MeCFG *cfg = func.GetCfg(); + auto exitBB = cfg->GetBBFromID(loop.inloopBB2exitBBs.begin()->first); StmtNode *lastStmt = &(exitBB->GetStmtNodes().back()); if (lastStmt->GetOpCode() != OP_brfalse && lastStmt->GetOpCode() != OP_brtrue && lastStmt->GetOpCode() != OP_switch) { CHECK_FATAL(false, "must be OP_brfalse, OP_brtrue or OP_switch"); @@ -472,10 +477,10 @@ void MeDoLoopCanon::SplitCondGotBB(MeFunction &func, LoopDesc &loop) { if (notOnlyHasBrStmt) { return; } - BB *newFallthru = func.NewBasicBlock(); + BB *newFallthru = cfg->NewBasicBlock(); newFallthru->SetKind(kBBFallthru); newFallthru->SetAttributes(kBBAttrIsInLoop); - func.CloneBasicBlock(*newFallthru, *exitBB); + func.GetCfg()->CloneBasicBlock(*newFallthru, *exitBB); newFallthru->RemoveLastStmt(); for (auto &stmt : exitBB->GetStmtNodes()) { if (&stmt != &exitBB->GetLast()) { @@ -485,7 +490,7 @@ void MeDoLoopCanon::SplitCondGotBB(MeFunction &func, LoopDesc &loop) { auto label = exitBB->GetBBLabel(); if (label != 0) { newFallthru->SetBBLabel(label); - func.SetLabelBBAt(label, newFallthru); + cfg->SetLabelBBAt(label, newFallthru); exitBB->SetBBLabel(0); } while (exitBB->GetPred().size() > 0) { @@ -502,7 +507,7 @@ bool MeDoLoopCanon::IsDoWhileLoop(MeFunction &func, const LoopDesc &loop) const return false; } } - auto exitBB = func.GetBBFromID(loop.inloopBB2exitBBs.begin()->first); + auto exitBB = func.GetCfg()->GetBBFromID(loop.inloopBB2exitBBs.begin()->first); for (auto pred : exitBB->GetPred()) { if (!loop.Has(*pred)) { return false; @@ -515,10 +520,10 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr if (DEBUGFUNC(&func)) { LogInfo::MapleLogger() << "-----------------Dump mefunction before loop normalization----------\n"; func.Dump(true); - func.GetTheCfg()->DumpToFile("cfgbeforLoopNormalization"); + func.GetCfg()->DumpToFile("cfgbeforLoopNormalization"); } heads.clear(); - FindHeadBBs(func, dom, func.GetCommonEntryBB()); + FindHeadBBs(func, dom, func.GetCfg()->GetCommonEntryBB()); AddPreheader(func); Merge(func); // cfg changes only if heads is not empty @@ -551,7 +556,7 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr if (DEBUGFUNC(&func)) { LogInfo::MapleLogger() << "-----------------Dump mefunction after loop normalization-----------\n"; func.Dump(true); - func.GetTheCfg()->DumpToFile("cfgafterLoopNormalization"); + func.GetCfg()->DumpToFile("cfgafterLoopNormalization"); } if (cfgChange) { m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, &func); diff --git a/src/mapleall/maple_me/src/me_loop_unrolling.cpp b/src/mapleall/maple_me/src/me_loop_unrolling.cpp index 260c2479b2fc49185fe6069645d822733c813d2d..0a093322a68e4f8f9206718577e1aebd98a0d660 100644 --- a/src/mapleall/maple_me/src/me_loop_unrolling.cpp +++ b/src/mapleall/maple_me/src/me_loop_unrolling.cpp @@ -240,7 +240,7 @@ void LoopUnrolling::ComputeCodeSize(const MeStmt &meStmt, uint32 &cost) { } BB *LoopUnrolling::CopyBB(BB &bb, bool isInLoop) { - BB *newBB = func->NewBasicBlock(); + BB *newBB = cfg->NewBasicBlock(); if (isInLoop) { newBB->SetAttributes(kBBAttrIsInLoop); } @@ -460,7 +460,7 @@ void LoopUnrolling::ResetFrequency(BB &bb) { // Update frequency of old exiting BB and latch BB. void LoopUnrolling::ResetFrequency() { - auto exitBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + auto exitBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); auto latchBB = loop->latch; if (isUnrollWithVar) { auto latchFreq = loop->head->GetFrequency() % replicatedLoopNum - loop->preheader->GetFrequency(); @@ -526,7 +526,7 @@ void LoopUnrolling::AddEdgeForExitBB(BB &exitBB, std::unordered_map &o // Copy loop except exiting BB and latch BB. void LoopUnrolling::CopyAndInsertBB(bool isPartial) { - auto exitBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + auto exitBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); CHECK_FATAL(exitBB->GetKind() == kBBCondGoto, "exiting bb must be kBBCondGoto"); std::unordered_map old2NewBB; BB *newHeadBB = CopyBB(*loop->head, true); @@ -570,7 +570,7 @@ void LoopUnrolling::CopyAndInsertBB(bool isPartial) { } void LoopUnrolling::RemoveCondGoto() { - BB *exitingBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *exitingBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); CHECK_FATAL(exitingBB->GetSucc().size() == 2, "must has two succ bb"); if (exitingBB->GetSucc(0) != loop->latch && exitingBB->GetSucc(1) != loop->latch) { CHECK_FATAL(false, "latch must be pred of exiting bb"); @@ -582,13 +582,13 @@ void LoopUnrolling::RemoveCondGoto() { exitingBB->SetEdgeFreq(exitingBB->GetSucc(0), exitingBB->GetFrequency()); } loop->head->RemovePred(*loop->latch); - func->DeleteBasicBlock(*loop->latch); + cfg->DeleteBasicBlock(*loop->latch); } bool LoopUnrolling::LoopFullyUnroll(int64 tripCount) { uint32 costResult = 0; for (auto bbId : loop->loopBBs) { - BB *bb = func->GetBBFromID(bbId); + BB *bb = cfg->GetBBFromID(bbId); for (auto &meStmt : bb->GetMeStmts()) { uint32 cost = 0; ComputeCodeSize(meStmt, cost); @@ -625,7 +625,7 @@ void LoopUnrolling::ResetFrequency(BB &newCondGotoBB, BB &exitingBB, const BB &e void LoopUnrolling::InsertCondGotoBB() { CHECK_NULL_FATAL(partialSuccHead); - BB *exitingBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *exitingBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); BB *exitedBB = *(loop->inloopBB2exitBBs.begin()->second->begin()); BB *newCondGotoBB = CopyBB(*exitingBB, true); auto headFreq = loop->head->GetFrequency(); @@ -677,8 +677,8 @@ bool LoopUnrolling::DetermineUnrollTimes(uint32 &index, bool isConst) { uint32 costResult = 0; uint32 unrollTime = unrollTimes[index]; for (auto bbId : loop->loopBBs) { - BB *bb = func->GetBBFromID(bbId); - auto exitBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *bb = cfg->GetBBFromID(bbId); + auto exitBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); if (bb == exitBB) { continue; } @@ -707,7 +707,7 @@ bool LoopUnrolling::DetermineUnrollTimes(uint32 &index, bool isConst) { void LoopUnrolling::AddPreHeader(BB *oldPreHeader, BB *head) { CHECK_FATAL(oldPreHeader->GetKind() == kBBCondGoto, "must be kBBCondGoto"); - auto *preheader = func->NewBasicBlock(); + auto *preheader = cfg->NewBasicBlock(); preheader->SetAttributes(kBBAttrArtificial); preheader->SetKind(kBBFallthru); auto preheaderFreq = 0; @@ -789,7 +789,7 @@ bool LoopUnrolling::LoopPartialUnrollWithConst(uint32 tripCount) { void LoopUnrolling::CopyLoopForPartialAndPre(BB *&newHead, BB *&newExiting) { needUpdateInitLoopFreq = false; - auto exitBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + auto exitBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); CHECK_FATAL(exitBB->GetKind() == kBBCondGoto, "exiting bb must be kBBCondGoto"); std::unordered_map old2NewBB; BB *newHeadBB = CopyBB(*loop->head, true); @@ -960,7 +960,7 @@ MeExpr *LoopUnrolling::CreateExprWithCRNode(CRNode &crNode) { void LoopUnrolling::CreateIndVarAndCondGotoStmt(CR &cr, CRNode &varNode, BB &preCondGoto, uint32 unrollTime, uint32 i) { // create stmt : int i = 0. - BB *indVarAndTripCountDefBB = func->NewBasicBlock(); + BB *indVarAndTripCountDefBB = cfg->NewBasicBlock(); std::string indVarName = std::string("__LoopUnrolllIndvar__") + std::to_string(i); VarMeExpr *indVar = CreateIndVarOrTripCountWithName(indVarName); indVarAndTripCountDefBB->SetKind(kBBFallthru); @@ -970,7 +970,7 @@ void LoopUnrolling::CreateIndVarAndCondGotoStmt(CR &cr, CRNode &varNode, BB &pre InsertCandsForSSAUpdate(indVar->GetOstIdx(), *indVarAndTripCountDefBB); // create stmt : tripCount = (n - start) / stride. - BB *exitingBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *exitingBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); auto opnd0 = CreateExprWithCRNode(*cr.GetOpnd(0)); auto opnd1 = CreateExprWithCRNode(*cr.GetOpnd(1)); MeExpr *conditionExpr = CreateExprWithCRNode(varNode); @@ -1020,13 +1020,13 @@ void LoopUnrolling::CreateIndVarAndCondGotoStmt(CR &cr, CRNode &varNode, BB &pre } void LoopUnrolling::CopyLoopForPartial(CR &cr, CRNode &varNode, uint32 j, uint32 unrollTime) { - BB *exitingBB = func->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *exitingBB = cfg->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); BB *exitedBB = *loop->inloopBB2exitBBs.begin()->second->begin(); BB *partialCondGoto = CopyBB(*exitingBB, false); replicatedLoopNum = unrollTime; CopyLoopForPartial(*partialCondGoto, *exitedBB, *exitingBB); // create preCondGoto bb - BB *preCondGoto = func->NewBasicBlock(); + BB *preCondGoto = cfg->NewBasicBlock(); if (profValid) { preCondGoto->SetFrequency(loop->preheader->GetFrequency()); } @@ -1073,8 +1073,8 @@ void LoopUnrolling::LoopPartialUnrollWithVar(CR &cr, CRNode &varNode, uint32 j) } if (MeDoLoopUnrolling::enableDump) { irMap->Dump(); - profValid ? func->GetTheCfg()->DumpToFile("cfgIncludeFreqInfobeforeLoopPartialWithVarUnrolling", false, true) : - func->GetTheCfg()->DumpToFile("cfgbeforeLoopPartialWithVarUnrolling"); + profValid ? cfg->DumpToFile("cfgIncludeFreqInfobeforeLoopPartialWithVarUnrolling", false, true) : + cfg->DumpToFile("cfgbeforeLoopPartialWithVarUnrolling"); } uint32 unrollTime = unrollTimes[index]; if (MeDoLoopUnrolling::enableDebug) { @@ -1096,8 +1096,8 @@ void LoopUnrolling::LoopPartialUnrollWithVar(CR &cr, CRNode &varNode, uint32 j) ssaUpdate.Run(); if (MeDoLoopUnrolling::enableDump) { irMap->Dump(); - profValid ? func->GetTheCfg()->DumpToFile("cfgIncludeFreqInfoafterLoopPartialWithVarUnrolling", false, true) : - func->GetTheCfg()->DumpToFile("cfgafterLoopPartialWithVarUnrolling"); + profValid ? cfg->DumpToFile("cfgIncludeFreqInfoafterLoopPartialWithVarUnrolling", false, true) : + cfg->DumpToFile("cfgafterLoopPartialWithVarUnrolling"); } } @@ -1122,7 +1122,7 @@ bool MeDoLoopUnrolling::IsDoWhileLoop(MeFunction &func, LoopDesc &loop) const { return false; } } - auto exitBB = func.GetBBFromID(loop.inloopBB2exitBBs.begin()->first); + auto exitBB = func.GetCfg()->GetBBFromID(loop.inloopBB2exitBBs.begin()->first); for (auto pred : exitBB->GetPred()) { if (!loop.Has(*pred)) { return false; @@ -1132,8 +1132,9 @@ bool MeDoLoopUnrolling::IsDoWhileLoop(MeFunction &func, LoopDesc &loop) const { } bool MeDoLoopUnrolling::PredIsOutOfLoopBB(MeFunction &func, LoopDesc &loop) const { + MeCFG *cfg = func.GetCfg(); for (auto bbID : loop.loopBBs) { - auto bb = func.GetBBFromID(bbID); + auto bb = cfg->GetBBFromID(bbID); if (bb == loop.head) { continue; } @@ -1160,7 +1161,7 @@ bool MeDoLoopUnrolling::IsCanonicalAndOnlyOneExitLoop(MeFunction &func, LoopDesc CHECK_NULL_FATAL(loop.preheader); CHECK_NULL_FATAL(loop.latch); auto headBB = loop.head; - auto exitBB = func.GetBBFromID(loop.inloopBB2exitBBs.begin()->first); + auto exitBB = func.GetCfg()->GetBBFromID(loop.inloopBB2exitBBs.begin()->first); CHECK_FATAL(headBB->GetPred().size() == 2, "head must has two preds"); if (!IsDoWhileLoop(func, loop)) { if (enableDebug) { @@ -1193,8 +1194,8 @@ void MeDoLoopUnrolling::ExcuteLoopUnrollingWithConst(uint32 tripCount, MeFunctio } if (enableDump) { irMap.Dump(); - func.IsIRProfValid() ? func.GetTheCfg()->DumpToFile("cfgIncludeFreqInfobeforLoopUnrolling", false, true) : - func.GetTheCfg()->DumpToFile("cfgbeforLoopUnrolling"); + func.IsIRProfValid() ? func.GetCfg()->DumpToFile("cfgIncludeFreqInfobeforLoopUnrolling", false, true) : + func.GetCfg()->DumpToFile("cfgbeforLoopUnrolling"); } // fully unroll if (loopUnrolling.LoopFullyUnroll(tripCount)) { @@ -1203,8 +1204,8 @@ void MeDoLoopUnrolling::ExcuteLoopUnrollingWithConst(uint32 tripCount, MeFunctio } if (enableDump) { irMap.Dump(); - func.IsIRProfValid() ? func.GetTheCfg()->DumpToFile("cfgIncludeFreqInfoafterLoopFullyUnrolling", false, true) : - func.GetTheCfg()->DumpToFile("cfgafterLoopFullyUnrolling"); + func.IsIRProfValid() ? func.GetCfg()->DumpToFile("cfgIncludeFreqInfoafterLoopFullyUnrolling", false, true) : + func.GetCfg()->DumpToFile("cfgafterLoopFullyUnrolling"); } return; } @@ -1215,8 +1216,8 @@ void MeDoLoopUnrolling::ExcuteLoopUnrollingWithConst(uint32 tripCount, MeFunctio } if (enableDump) { irMap.Dump(); - func.IsIRProfValid() ? func.GetTheCfg()->DumpToFile("cfgIncludeFreqInfoafterLoopPartialWithConst", false, true) : - func.GetTheCfg()->DumpToFile("cfgafterLoopPartialWithConstUnrolling"); + func.IsIRProfValid() ? func.GetCfg()->DumpToFile("cfgIncludeFreqInfoafterLoopPartialWithConst", false, true) : + func.GetCfg()->DumpToFile("cfgafterLoopPartialWithConstUnrolling"); } return; } @@ -1278,9 +1279,6 @@ AnalysisResult *MeDoLoopUnrolling::Run(MeFunction *func, MeFuncResultMgr *m, Mod if (enableDebug) { LogInfo::MapleLogger() << "func is hot" << "\n"; } - if (func->GetSecondPass()) { - return nullptr; - } CHECK_NULL_FATAL(m); auto *irMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); CHECK_NULL_FATAL(irMap); diff --git a/src/mapleall/maple_me/src/me_lower_globals.cpp b/src/mapleall/maple_me/src/me_lower_globals.cpp index d808aff4397b324c58cf65608c082f1aa6cd1fb4..2272b07368806883971d83492643792d160ede86 100644 --- a/src/mapleall/maple_me/src/me_lower_globals.cpp +++ b/src/mapleall/maple_me/src/me_lower_globals.cpp @@ -103,8 +103,9 @@ void MeLowerGlobals::LowerGlobalDreads(MeStmt &stmt, MeExpr &expr) { } void MeLowerGlobals::Run() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetMeStmts()) { for (size_t i = 0; i < stmt.NumMeStmtOpnds(); ++i) { diff --git a/src/mapleall/maple_me/src/me_may2dassign.cpp b/src/mapleall/maple_me/src/me_may2dassign.cpp index ada5dc340f22b6d8ec7b5dd893c1bd6e191600cd..fda610349439038c16075b7b93a8f8491bdb610b 100644 --- a/src/mapleall/maple_me/src/me_may2dassign.cpp +++ b/src/mapleall/maple_me/src/me_may2dassign.cpp @@ -17,8 +17,9 @@ // this phase converts all maydassign back to dassign namespace maple { void May2Dassign::DoIt() { - auto eIt = func.valid_end(); - for (auto bIt = func.valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func.GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetMeStmts()) { if (stmt.GetOp() != OP_maydassign) { @@ -41,7 +42,8 @@ void May2Dassign::DoIt() { } } -AnalysisResult *MeDoMay2Dassign::Run(MeFunction *func, MeFuncResultMgr*, ModuleResultMgr*) { +AnalysisResult *MeDoMay2Dassign::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { + (void)(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); May2Dassign may2Dassign(*func); may2Dassign.DoIt(); 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 d941c6b43160354fb850db6d4f7c0a42cac56668..5a93265d6e29e49f70572f5a31a82827906efc79 100644 --- a/src/mapleall/maple_me/src/me_phase_manager.cpp +++ b/src/mapleall/maple_me/src/me_phase_manager.cpp @@ -87,7 +87,9 @@ 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->GetCfg() && func->GetCfg()->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()); @@ -145,6 +147,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"); @@ -215,7 +218,7 @@ void MeFuncPhaseManager::Run(MIRFunction *mirFunc, uint64 rangeNum, const std::s auto funcStackMP = std::make_unique(localMpCtrler, ""); MemPool *versMP = new ThreadLocalMemPool(localMpCtrler, "first verst mempool"); MeFunction &func = *(funcMP->New(&mirModule, mirFunc, funcMP.get(), *funcStackMP, versMP, meInput)); - func.PartialInit(false); + func.PartialInit(); #if DEBUG globalMIRModule = &mirModule; globalFunc = &func; @@ -233,7 +236,7 @@ void MeFuncPhaseManager::Run(MIRFunction *mirFunc, uint64 rangeNum, const std::s mirFunc->SetMeFunc(&func); } std::string phaseName = ""; - MeFuncPhase *changeCFGPhase = nullptr; +// MeFuncPhase *changeCFGPhase = nullptr; // each function level phase bool dumpFunc = FuncFilter(MeOption::dumpFunc, func.GetName()); size_t phaseIndex = 0; @@ -282,11 +285,6 @@ void MeFuncPhaseManager::Run(MIRFunction *mirFunc, uint64 rangeNum, const std::s --it; --it; // restore iterator to emit } - if (p->IsChangedCFG()) { - changeCFGPhase = p; - p->ClearChangeCFG(); - break; - } if (timePhases) { runPhasetimer.Stop(); phaseTimers[phaseIndex] += runPhasetimer.ElapsedMicroseconds(); @@ -305,87 +303,6 @@ void MeFuncPhaseManager::Run(MIRFunction *mirFunc, uint64 rangeNum, const std::s invalidTimer.Stop(); extraMeTimers["invalidResult"] += invalidTimer.ElapsedMicroseconds(); } - if (changeCFGPhase != nullptr) { - if (ipa) { - CHECK_FATAL(false, "phases in ipa will not chang cfg."); - } - if (timePhases) { - funcPrepareTimer.Start(); - } - // do all the phases start over - auto *versMemPool = new ThreadLocalMemPool(localMpCtrler, "second verst mempool"); - auto function = funcMP->New(&mirModule, mirFunc, funcMP.get(), *funcStackMP, versMemPool, meInput); - function->PartialInit(true); - function->Prepare(rangeNum); - if (timePhases) { - funcPrepareTimer.Stop(); - extraMeTimers["prepareFunc"] += funcPrepareTimer.ElapsedMicroseconds(); - iteratorTimer.Start(); - } - for (auto it = PhaseSequenceBegin(); it != PhaseSequenceEnd(); ++it) { - if (timePhases) { - runPhasetimer.Start(); - } - PhaseID id = GetPhaseId(it); - auto *p = static_cast(GetPhase(id)); - if (p == changeCFGPhase) { - continue; - } - if (MeOption::skipFrom.compare(p->PhaseName()) == 0) { - // fast-forward to emit pass, which is last pass - while (++it != PhaseSequenceEnd()) { } - --it; // restore iterator - id = GetPhaseId(it); - p = static_cast(GetPhase(id)); - } - p->SetPreviousPhaseName(phaseName); // prev phase name is for filename used in emission after phase - phaseName = p->PhaseName(); // new phase name - bool dumpPhase = MeOption::DumpPhase(phaseName); - if (MeOption::dumpBefore && dumpFunc && dumpPhase) { - LogInfo::MapleLogger() << ">>>>>Second time Dump before " << phaseName << " <<<<<\n"; - if (phaseName != "emit") { - function->Dump(false); - } else { - function->DumpFunctionNoSSA(); - } - LogInfo::MapleLogger() << ">>>>> Second time Dump before End <<<<<\n"; - } - RunFuncPhase(function, p); - if ((MeOption::dumpAfter || dumpPhase) && dumpFunc) { - LogInfo::MapleLogger() << ">>>>>Second time Dump after " << phaseName << " <<<<<\n"; - if (phaseName != "emit") { - function->Dump(false); - } else { - function->DumpFunctionNoSSA(); - } - LogInfo::MapleLogger() << ">>>>> Second time Dump after End <<<<<\n\n"; - } - if (MeOption::skipAfter.compare(phaseName) == 0) { - // fast-forward to emit pass, which is last pass - while (++it != PhaseSequenceEnd()) { } - --it; - --it; // restore iterator to emit - } - if (timePhases) { - runPhasetimer.Stop(); - phaseTimers[phaseIndex] += runPhasetimer.ElapsedMicroseconds(); - runPhasesTime += runPhasetimer.ElapsedMicroseconds(); - } - } - if (timePhases) { - iteratorTimer.Stop(); - extraMeTimers["iterator"] += iteratorTimer.ElapsedMicroseconds() - runPhasesTime; - runPhasesTime = 0; - invalidTimer.Start(); - } - - GetAnalysisResultManager()->InvalidAllResults(); - - if (timePhases) { - invalidTimer.Stop(); - extraMeTimers["invalidResult"] += invalidTimer.ElapsedMicroseconds(); - } - } if (!ipa) { if (timePhases) { invalidTimer.Start(); diff --git a/src/mapleall/maple_me/src/me_placement_rc.cpp b/src/mapleall/maple_me/src/me_placement_rc.cpp index 7af2d23b779aa5eea77fd02949759ab27d6949a6..66cfe64ff8a2ccfdfe06a05b0e28c9db90acd276 100644 --- a/src/mapleall/maple_me/src/me_placement_rc.cpp +++ b/src/mapleall/maple_me/src/me_placement_rc.cpp @@ -42,7 +42,7 @@ static void InsertAtBBEntry(BB &bb, MeStmt &newStmt) { UnaryMeStmt *PlacementRC::GenIncrefAtEntry(OriginalSt &ost) const { UnaryMeStmt *incRefStmt = irMap->New(OP_incref); incRefStmt->SetMeStmtOpndValue(irMap->GetOrCreateZeroVersionVarMeExpr(ost)); - BB *insertBB = func->GetFirstBB(); + BB *insertBB = func->GetCfg()->GetFirstBB(); incRefStmt->SetBB(insertBB); InsertAtBBEntry(*insertBB, *incRefStmt); return incRefStmt; @@ -121,7 +121,8 @@ void PlacementRC::HandleThrowOperand(SRealOcc &realOcc, ThrowMeStmt &thwStmt) { } void PlacementRC::AddCleanupArg() { - for (BB *bb : func->GetCommonExitBB()->GetPred()) { + auto cfg = func->GetCfg(); + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { auto &meStmts = bb->GetMeStmts(); if (meStmts.empty() || meStmts.back().GetOp() != OP_return) { continue; @@ -350,7 +351,8 @@ bool PlacementRC::GoStraightToThrow(const BB &bb) const { } SOcc *PlacementRC::FindLambdaReal(const SLambdaOcc &occ) const { - std::vector visitedBBs(func->NumBBs(), false); + auto cfg = func->GetCfg(); + std::vector visitedBBs(cfg->NumBBs(), false); std::set lambdaRealBBs; FindRealPredBB(occ.GetBB(), visitedBBs, lambdaRealBBs); SOcc *retOcc = nullptr; @@ -363,7 +365,7 @@ SOcc *PlacementRC::FindLambdaReal(const SLambdaOcc &occ) const { if (retOcc == nullptr) { retOcc = realOcc; } else if (&retOcc->GetBB() != mirBB) { - std::vector bbVisited(func->NumBBs(), false); + std::vector bbVisited(cfg->NumBBs(), false); retOcc->GetBB().FindReachableBBs(bbVisited); if (bbVisited[mirBB->GetBBId()]) { retOcc = realOcc; @@ -411,8 +413,9 @@ void PlacementRC::CodeMotion() { } // Do insertions at catch blocks according to catchBlocks2Insert + auto cfg = func->GetCfg(); for (BBId bbId : catchBlocks2Insert) { - BB *bb = func->GetAllBBs().at(bbId); + BB *bb = cfg->GetBBFromID(bbId); CheckAndInsert(*bb, nullptr); } } @@ -454,8 +457,8 @@ void PlacementRC::DeleteEntryIncref(SRealOcc &realOcc, const UnaryMeStmt *entryI auto *resetDass = irMap->CreateAssignMeStmt(*candVar, *irMap->CreateIntConstMeExpr(0, PTY_ptr), realOcc.GetBB()); resetDass->SetSrcPos(entryIncref->GetSrcPosition()); candVar->SetDefByStmt(*resetDass); - func->GetFirstBB()->InsertMeStmtBefore(entryIncref, resetDass); - func->GetFirstBB()->RemoveMeStmt(entryIncref); + func->GetCfg()->GetFirstBB()->InsertMeStmtBefore(entryIncref, resetDass); + func->GetCfg()->GetFirstBB()->RemoveMeStmt(entryIncref); } void PlacementRC::UpdateCatchBlocks2Insert(const BB &lastUseBB) { @@ -568,7 +571,7 @@ void PlacementRC::CodeMotionForReal(SOcc &occ, const UnaryMeStmt *entryIncref) { // Special case: delete the formal's entry incref if (realOcc.GetStmt() == nullptr) { if (!MeOption::gcOnly) { - CHECK_FATAL(lastUseBB == func->GetFirstBB(), "PlacementRC::CodeMotion: realOcc from entry incref has wrong bb"); + CHECK_FATAL(lastUseBB == func->GetCfg()->GetFirstBB(), "PlacementRC::CodeMotion: realOcc from entry incref has wrong bb"); DeleteEntryIncref(realOcc, entryIncref); } return; @@ -608,7 +611,8 @@ void PlacementRC::CodeMotionForReal(SOcc &occ, const UnaryMeStmt *entryIncref) { // ================ Step 0: Collect occurrences ================ void PlacementRC::CreateEmptyCleanupIntrinsics() { - for (BB *bb : func->GetCommonExitBB()->GetPred()) { + auto cfg = func->GetCfg(); + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { auto &meStmts = bb->GetMeStmts(); if (!meStmts.empty() && meStmts.back().GetOp() == OP_return) { IntrinsiccallMeStmt *intrn = @@ -666,7 +670,7 @@ void PlacementRC::CreateRealOcc(const OStIdx &ostIdx, MeStmt *meStmt, VarMeExpr wkCand->SetTheVar(var); } SRealOcc *newOcc = meStmt != nullptr ? spreMp->New(*meStmt, var) - : spreMp->New(*func->GetFirstBB(), var); + : spreMp->New(*func->GetCfg()->GetFirstBB(), var); if (causedByDef) { newOcc->SetRealFromDef(true); } @@ -869,7 +873,7 @@ void PlacementRC::BuildWorkListBB(BB *bb) { // Recurse on child BBs in post-dominator tree for (BBId bbId : dom->GetPdomChildrenItem(bb->GetBBId())) { - BuildWorkListBB(func->GetAllBBs().at(bbId)); + BuildWorkListBB(func->GetCfg()->GetAllBBs().at(bbId)); } } @@ -881,9 +885,9 @@ AnalysisResult *MeDoPlacementRC::Run(MeFunction *func, MeFuncResultMgr *m, Modul if (whiteListFunc.find(funcName) != whiteListFunc.end() || func->GetMirFunc()->GetAttr(FUNCATTR_rclocalunowned)) { return nullptr; } - + auto cfg = static_cast(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); // Workaround for RCWeakRef-annotated field access: leave it to analyzerc - for (BB *bb : func->GetAllBBs()) { + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } diff --git a/src/mapleall/maple_me/src/me_predict.cpp b/src/mapleall/maple_me/src/me_predict.cpp index 05dfdca10cee2c923a357f3887812c2f34dae4ba..18a6f61f32167a6faf4fdb407d34ef156b7b8218 100644 --- a/src/mapleall/maple_me/src/me_predict.cpp +++ b/src/mapleall/maple_me/src/me_predict.cpp @@ -111,7 +111,7 @@ Predictor MePrediction::ReturnPrediction(const MeExpr *val, Prediction &predicti // Predict edge E with the given PROBABILITY. void MePrediction::PredictEdge(Edge &edge, Predictor predictor, int probability) { - if (&edge.src != func->GetCommonEntryBB() && edge.src.GetSucc().size() > 1) { + if (&edge.src != cfg->GetCommonEntryBB() && edge.src.GetSucc().size() > 1) { const BB &src = edge.src; auto *newEdgePred = tmpAlloc.GetMemPool()->New(edge); EdgePrediction *bbPred = bbPredictions[src.GetBBId()]; @@ -136,7 +136,7 @@ void MePrediction::PredEdgeDef(Edge &edge, Predictor predictor, Prediction taken // of this basic blocks as unlikely. void MePrediction::BBLevelPredictions() { RetMeStmt *retStmt = nullptr; - for (BB *bb : func->GetCommonExitBB()->GetPred()) { + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { MeStmt *lastMeStmt = to_ptr(bb->GetMeStmts().rbegin()); if (lastMeStmt != nullptr && lastMeStmt->GetOp() == OP_return) { retStmt = static_cast(lastMeStmt); @@ -179,10 +179,10 @@ void MePrediction::BBLevelPredictions() { // Make edges for all bbs in the cfg. void MePrediction::Init() { - bbPredictions.resize(func->GetAllBBs().size()); - edges.resize(func->GetAllBBs().size()); - bbVisited.resize(func->GetAllBBs().size()); - for (auto *bb : func->GetAllBBs()) { + bbPredictions.resize(cfg->GetAllBBs().size()); + edges.resize(cfg->GetAllBBs().size()); + bbVisited.resize(cfg->GetAllBBs().size()); + for (auto *bb : cfg->GetAllBBs()) { BBId idx = bb->GetBBId(); bbVisited[idx] = true; bbPredictions[idx] = nullptr; @@ -193,11 +193,11 @@ void MePrediction::Init() { edges[idx] = edge; } } - if (func->GetCommonEntryBB() != func->GetFirstBB()) { - bbVisited[func->GetCommonEntryBB()->GetBBId()] = true; + if (cfg->GetCommonEntryBB() != cfg->GetFirstBB()) { + bbVisited[cfg->GetCommonEntryBB()->GetBBId()] = true; } - if (func->GetCommonExitBB() != func->GetLastBB()) { - bbVisited[func->GetCommonExitBB()->GetBBId()] = true; + if (cfg->GetCommonExitBB() != cfg->GetLastBB()) { + bbVisited[cfg->GetCommonExitBB()->GetBBId()] = true; } } @@ -236,7 +236,7 @@ void MePrediction::PredictLoops() { // Find loop exit bbs. MapleVector exits(tmpAlloc.Adapter()); for (auto &bbID : loopBBs) { - BB *bb = func->GetAllBBs().at(bbID); + BB *bb = cfg->GetAllBBs().at(bbID); if (bb->GetSucc().size() < minBBNumRequired) { continue; } @@ -342,7 +342,7 @@ void MePrediction::EstimateBBProb(const BB &bb) { PredEdgeDef(*FindEdge(bb, *dest), kPredTry, kTaken); } else if (!dest->GetMeStmts().empty() && dest->GetMeStmts().back().GetOp() == OP_return) { PredEdgeDef(*FindEdge(bb, *dest), kPredEarlyReturn, kNotTaken); - } else if (dest != func->GetCommonExitBB() && dest != &bb && dom->Dominate(bb, *dest) && + } else if (dest != cfg->GetCommonExitBB() && dest != &bb && dom->Dominate(bb, *dest) && !dom->PostDominate(*dest, bb)) { for (const MeStmt &stmt : dest->GetMeStmts()) { if (stmt.GetOp() == OP_call || stmt.GetOp() == OP_callassigned) { @@ -600,23 +600,23 @@ void MePrediction::EstimateLoops() { } // Now propagate the frequencies through all the blocks. std::fill(bbVisited.begin(), bbVisited.end(), false); - if (func->GetCommonEntryBB() != func->GetFirstBB()) { - bbVisited[func->GetCommonEntryBB()->GetBBId()] = false; + if (cfg->GetCommonEntryBB() != cfg->GetFirstBB()) { + bbVisited[cfg->GetCommonEntryBB()->GetBBId()] = false; } - if (func->GetCommonExitBB() != func->GetLastBB()) { - bbVisited[func->GetCommonExitBB()->GetBBId()] = false; + if (cfg->GetCommonExitBB() != cfg->GetLastBB()) { + bbVisited[cfg->GetCommonExitBB()->GetBBId()] = false; } - func->GetCommonEntryBB()->SetFrequency(kFreqBase); - for (BB *bb : func->GetCommonEntryBB()->GetSucc()) { + cfg->GetCommonEntryBB()->SetFrequency(kFreqBase); + for (BB *bb : cfg->GetCommonEntryBB()->GetSucc()) { PropagateFreq(*bb, *bb); } } void MePrediction::EstimateBBFrequencies() { - BB *entry = func->GetCommonEntryBB(); + BB *entry = cfg->GetCommonEntryBB(); edges[entry->GetBBId()]->probability = kProbAlways; double backProb = 0.0; - for (size_t i = 0; i < func->GetAllBBs().size(); ++i) { + for (size_t i = 0; i < cfg->GetAllBBs().size(); ++i) { Edge *edge = edges[i]; while (edge != nullptr) { if (edge->probability > 0) { @@ -644,14 +644,14 @@ void MePrediction::EstimateProbability() { PredictLoops(); } - MapleVector &bbVec = func->GetAllBBs(); + MapleVector &bbVec = cfg->GetAllBBs(); for (auto *bb : bbVec) { EstimateBBProb(*bb); } for (auto *bb : bbVec) { CombinePredForBB(*bb); } - for (size_t i = 0; i < func->GetAllBBs().size(); ++i) { + for (size_t i = 0; i < cfg->GetAllBBs().size(); ++i) { int32 all = 0; for (Edge *edge = edges[i]; edge != nullptr; edge = edge->next) { if (predictDebug) { diff --git a/src/mapleall/maple_me/src/me_profile_gen.cpp b/src/mapleall/maple_me/src/me_profile_gen.cpp index 84677f80f82c0cfed9c52ca025e253f47aef287c..dfd2fa9d63dea803ccd24335b24675636ec053d8 100644 --- a/src/mapleall/maple_me/src/me_profile_gen.cpp +++ b/src/mapleall/maple_me/src/me_profile_gen.cpp @@ -87,7 +87,7 @@ void MeProfGen::SaveProfile() { if (func->GetName().find("main") != std::string::npos) { std::vector opnds; IntrinsiccallMeStmt *saveProfCall = hMap->CreateIntrinsicCallMeStmt(INTRN_MCCSaveProf, opnds); - for (BB *exitBB : func->GetCommonExitBB()->GetPred()) { + for (BB *exitBB : func->GetCfg()->GetCommonExitBB()->GetPred()) { exitBB->AddMeStmtFirst(saveProfCall); } } @@ -129,9 +129,10 @@ void MeProfGen::InstrumentFunc() { // if function have infinite loop, can't instrument,because it may cause counter // overflow bool MeProfGen::CanInstrument() const { - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func->common_entry() || bIt == func->common_exit()) { + MeCFG *cfg = func->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto *bb = *bIt; @@ -144,7 +145,7 @@ bool MeProfGen::CanInstrument() const { AnalysisResult *MeDoProfGen::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { MemPool *tempMp = NewMemPool(); - if (!func->empty()) { + if (!func->GetCfg()->empty()) { MeProfGen::IncTotalFunc(); } // function with try can't determine the instrument BB,because @@ -163,7 +164,7 @@ AnalysisResult *MeDoProfGen::Run(MeFunction *func, MeFuncResultMgr *m, ModuleRes if (DEBUGFUNC(func)) { LogInfo::MapleLogger() << "dump edge info in profile gen phase " << func->GetMirFunc()->GetName() << std::endl; - func->GetTheCfg()->DumpToFile("afterProfileGen", false); + func->GetCfg()->DumpToFile("afterProfileGen", false); profGen.DumpEdgeInfo(); } diff --git a/src/mapleall/maple_me/src/me_profile_use.cpp b/src/mapleall/maple_me/src/me_profile_use.cpp index 3c4c523a1201e5be83fd9213ace87242932fdcbc..c3b8330953c1520489c026472f5242b87e136f8f 100644 --- a/src/mapleall/maple_me/src/me_profile_use.cpp +++ b/src/mapleall/maple_me/src/me_profile_use.cpp @@ -96,7 +96,8 @@ void MeProfUse::ComputeBBFreq(BBUseInfo &bbInfo, bool &change) { void MeProfUse::ComputeEdgeFreq() { bool change = true; size_t pass = 0; - auto eIt = func->valid_end(); + MeCFG *cfg = func->GetCfg(); + auto eIt = cfg->valid_end(); while (change) { change = false; pass++; @@ -105,7 +106,7 @@ void MeProfUse::ComputeEdgeFreq() { * use the bb edge to infer the bb's count,when all bb's count is valid * then all edges count is valid */ - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; BBUseInfo *useInfo = GetBBUseInfo(*bb); if (useInfo == nullptr) { @@ -236,12 +237,13 @@ bool MeProfUse::BuildEdgeCount() { } void MeProfUse::SetFuncEdgeInfo() { - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + MeCFG *cfg = func->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; auto *bbInfo = GetBBUseInfo(*bb); bb->SetFrequency(bbInfo->GetCount()); - if (bIt == func->common_entry() || bIt == func->common_exit()) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } bb->InitEdgeFreq(); @@ -250,14 +252,14 @@ void MeProfUse::SetFuncEdgeInfo() { auto *destBB = e->GetDestBB(); // common_exit's pred's BB doesn't have succ BB of common exit // so skip this edge - if (destBB == func->GetCommonExitBB()) { + if (destBB == cfg->GetCommonExitBB()) { continue; } bb->SetEdgeFreq(destBB, e->GetCount()); } } func->SetProfValid(); - func->SetFrequency(func->GetCommonEntryBB()->GetFrequency()); + func->SetFrequency(cfg->GetCommonEntryBB()->GetFrequency()); if (Options::genPGOReport) { func->GetMIRModule().GetProfile().SetFuncStatus(func->GetName(), true); } @@ -269,9 +271,10 @@ void MeProfUse::DumpFuncCFGEdgeFreq() const { return; } LogInfo::MapleLogger() << "func freq " << func->GetFrequency() << "\n"; - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { - if (bIt == func->common_entry() || bIt == func->common_exit()) { + MeCFG *cfg = func->GetCfg(); + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } auto bb = *bIt; @@ -291,14 +294,15 @@ 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(); if (DEBUGFUNC(func) && profUse.IsSuccUseProf()) { LogInfo::MapleLogger() << "******************after profile use dump function******************\n"; profUse.DumpFuncCFGEdgeFreq(); - func->GetTheCfg()->DumpToFile("afterProfileUse", false, true); + func->GetCfg()->DumpToFile("afterProfileUse", false, true); } return nullptr; } diff --git a/src/mapleall/maple_me/src/me_prop.cpp b/src/mapleall/maple_me/src/me_prop.cpp index 9b80e870bba052e5e61d4c39a43aae140553643f..a187fb52d94c9370ae2b23c3e5231213baa7e866 100644 --- a/src/mapleall/maple_me/src/me_prop.cpp +++ b/src/mapleall/maple_me/src/me_prop.cpp @@ -51,7 +51,7 @@ AnalysisResult *MeDoMeProp::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResu } MeProp meProp(*hMap, *dom, *NewMemPool(), Prop::PropConfig { MeOption::propBase, propIloadRef, MeOption::propGlobalRef, MeOption::propFinaliLoadRef, MeOption::propIloadRefNonParm, MeOption::propAtPhi }); - meProp.TraversalBB(*func->GetCommonEntryBB()); + meProp.TraversalBB(*func->GetCfg()->GetCommonEntryBB()); if (DEBUGFUNC(func)) { LogInfo::MapleLogger() << "\n============== After Copy Propagation =============" << '\n'; func->Dump(false); diff --git a/src/mapleall/maple_me/src/me_rc_lowering.cpp b/src/mapleall/maple_me/src/me_rc_lowering.cpp index 5806332f6a89fda8e1bcfe7bf9e4fe57f6481fc0..d9c914f4d4cca01c55ee0420bfe7598c1959e836 100644 --- a/src/mapleall/maple_me/src/me_rc_lowering.cpp +++ b/src/mapleall/maple_me/src/me_rc_lowering.cpp @@ -61,7 +61,7 @@ void RCLowering::PreRCLower() { MarkAllRefOpnds(); CreateCleanupIntrinsics(); - auto *bb = func.GetCommonEntryBB(); + auto *bb = func.GetCfg()->GetCommonEntryBB(); CHECK_FATAL(bb != nullptr, "null ptr check"); UpdateRefVarVersions(*bb); UnmarkNotNeedDecRefOpnds(); @@ -80,7 +80,7 @@ void RCLowering::MarkLocalRefVar() { void RCLowering::MarkAllRefOpnds() { // to prevent valid_end from being called repeatedly, don't modify the definition of eIt - for (auto bIt = func.valid_begin(), eIt = func.valid_end(); bIt != eIt; ++bIt) { + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { for (auto &stmt : (*bIt)->GetMeStmts()) { MeExpr *lhsRef = stmt.GetLHSRef(false); if (lhsRef == nullptr) { @@ -118,7 +118,7 @@ void RCLowering::UpdateRefVarVersions(BB &bb) { ASSERT(bb.GetBBId() < dominance->GetDomChildrenSize(), "index out of range"); const MapleSet &domChildren = dominance->GetDomChildren(bb.GetBBId()); for (const auto &id : domChildren) { - UpdateRefVarVersions(*func.GetAllBBs().at(id)); + UpdateRefVarVersions(*cfg->GetAllBBs().at(id)); } // restore the stacks to their size at entry to this function invocation @@ -212,7 +212,7 @@ void RCLowering::UnmarkNotNeedDecRefOpnds() { } void RCLowering::CreateCleanupIntrinsics() { - for (BB *bb : func.GetCommonExitBB()->GetPred()) { + for (BB *bb : cfg->GetCommonExitBB()->GetPred()) { MeStmt *lastMeStmt = to_ptr(bb->GetMeStmts().rbegin()); if (!CheckOp(lastMeStmt, OP_return)) { continue; @@ -773,8 +773,8 @@ MIRIntrinsicID RCLowering::SelectWriteBarrier(const MeStmt &stmt) { void RCLowering::RCLower() { // to prevent valid_end from being called repeatedly, don't modify the definition of eIt - for (auto bIt = func.valid_begin(), eIt = func.valid_end(); bIt != eIt; ++bIt) { - if (bIt == func.common_entry() || bIt == func.common_exit()) { + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { continue; } EpreFixup(**bIt); @@ -1124,7 +1124,7 @@ void RCLowering::HandleArguments() { // handle arguments, if the formal gets modified // insert incref at entry and decref before all returns MIRFunction *mirFunc = func.GetMirFunc(); - BB *firstBB = func.GetFirstBB(); + BB *firstBB = func.GetCfg()->GetFirstBB(); MeStmt *firstMeStmt = to_ptr(firstBB->GetMeStmts().begin()); for (size_t i = (mirFunc->IsStatic() ? 0 : 1); i < mirFunc->GetFormalCount(); ++i) { MIRSymbol *sym = mirFunc->GetFormal(i); @@ -1155,7 +1155,7 @@ void RCLowering::PostRCLower() { HandleReturnStmt(); // compact RC // to prevent valid_end from being called repeatedly, don't modify the definition of eIt - for (auto bIt = func.valid_begin(), eIt = func.valid_end(); bIt != eIt; ++bIt) { + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { CompactRC(**bIt); } } @@ -1173,7 +1173,7 @@ void RCLowering::CheckRefs() { if (checkRefFormal) { CheckFormals(); } - for (BB *bb : func.GetAllBBs()) { + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } @@ -1208,7 +1208,7 @@ void RCLowering::ParseCheckFlag() { void RCLowering::CheckFormals() { MIRFunction *mirFunc = func.GetMirFunc(); - BB *firstBB = func.GetFirstBB(); + BB *firstBB = func.GetCfg()->GetFirstBB(); if (firstBB->IsMeStmtEmpty()) { return; } @@ -1653,10 +1653,11 @@ AnalysisResult *MeDoRCLowering::Run(MeFunction *func, MeFuncResultMgr *funcResMg rcLowering.SetDominance(*dom); } MIRFunction *mirFunction = func->GetMirFunc(); + MeCFG *cfg = func->GetCfg(); if (whiteListFunc.find(mirFunction->GetName()) != whiteListFunc.end() || mirFunction->GetAttr(FUNCATTR_rclocalunowned)) { - auto eIt = func->valid_end(); - for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + auto eIt = cfg->valid_end(); + for (auto bIt = cfg->valid_begin(); bIt != eIt; ++bIt) { auto *bb = *bIt; rcLowering.FastBBLower(*bb); } diff --git a/src/mapleall/maple_me/src/me_rename2preg.cpp b/src/mapleall/maple_me/src/me_rename2preg.cpp index 466b1f740faa9385b5fdd592c432d5e32ca0a796..f693750d51181cabdbbd96f78c1f09b14fb1e81a 100644 --- a/src/mapleall/maple_me/src/me_rename2preg.cpp +++ b/src/mapleall/maple_me/src/me_rename2preg.cpp @@ -312,8 +312,9 @@ void SSARename2Preg::Init() { } void SSARename2Preg::RunSelf() { + auto cfg = func->GetCfg(); Init(); - for (BB *mebb : func->GetAllBBs()) { + for (BB *mebb : cfg->GetAllBBs()) { if (mebb == nullptr) { continue; } @@ -347,7 +348,7 @@ void SSARename2Preg::PromoteEmptyFunction() { } AnalysisResult *MeDoSSARename2Preg::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr *mrMgr) { - if (func->GetAllBBs().empty()) { + if (func->IsEmpty()) { return nullptr; } (void)mrMgr; @@ -355,7 +356,7 @@ AnalysisResult *MeDoSSARename2Preg::Run(MeFunction *func, MeFuncResultMgr *m, Mo ASSERT(irMap != nullptr, ""); MemPool *renamemp = memPoolCtrler.NewMemPool(PhaseName().c_str(), true /* isLocalPool */); - if (func->GetAllBBs().size() == 0) { + if (func->GetCfg()->GetAllBBs().size() == 0) { // empty function, we only promote the parameter auto *emptyrenamer = renamemp->New(renamemp, func, nullptr, nullptr); emptyrenamer->PromoteEmptyFunction(); diff --git a/src/mapleall/maple_me/src/me_scalar_analysis.cpp b/src/mapleall/maple_me/src/me_scalar_analysis.cpp index 10986995afa8e9d93b8c88de067dd717e25b121c..ed9896e5b37ec5b1b25baf7f20b654818bffa884 100644 --- a/src/mapleall/maple_me/src/me_scalar_analysis.cpp +++ b/src/mapleall/maple_me/src/me_scalar_analysis.cpp @@ -1037,7 +1037,7 @@ void LoopScalarAnalysisResult::DumpTripCount(const CR &cr, int32 value, const Me TripCountType LoopScalarAnalysisResult::ComputeTripCount(MeFunction &func, uint32 &tripCountResult, CRNode *&conditionCRNode, CR *&itCR) { enableDebug = false; - BB *exitBB = func.GetBBFromID(loop->inloopBB2exitBBs.begin()->first); + BB *exitBB = func.GetCfg()->GetBBFromID(loop->inloopBB2exitBBs.begin()->first); if (exitBB->GetKind() == kBBCondGoto && IsLegal(*(exitBB->GetLastMe()))) { auto *brMeStmt = static_cast(exitBB->GetLastMe()); BB *brTarget = exitBB->GetSucc(1); diff --git a/src/mapleall/maple_me/src/me_ssa.cpp b/src/mapleall/maple_me/src/me_ssa.cpp index f9f265516ca69f4ed384c6e9d3bc75092bd1da2e..541fb9d79081786a8707d101c4d182d79ec16e11 100644 --- a/src/mapleall/maple_me/src/me_ssa.cpp +++ b/src/mapleall/maple_me/src/me_ssa.cpp @@ -93,8 +93,9 @@ void MeSSA::VerifySSAOpnd(const BaseNode &node) const { void MeSSA::VerifySSA() const { size_t vtableSize = func->GetMeSSATab()->GetVersionStTable().GetVersionStVectorSize(); + auto cfg = func->GetCfg(); // to prevent valid_end from being called repeatedly, don't modify the definition of eIt - for (auto bIt = func->valid_begin(), eIt = func->valid_end(); bIt != eIt; ++bIt) { + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { auto *bb = *bIt; Opcode opcode; for (auto &stmt : bb->GetStmtNodes()) { @@ -117,16 +118,16 @@ AnalysisResult *MeDoSSA::Run(MeFunction *func, MeFuncResultMgr *funcResMgr, Modu CHECK_FATAL(ssaTab != nullptr, "ssaTab phase has problem"); MemPool *ssaMp = NewMemPool(); auto *ssa = ssaMp->New(*func, func->GetMeSSATab(), *dom, *ssaMp); - + auto cfg = func->GetCfg(); ssa->InsertPhiNode(); - ssa->InitRenameStack(func->GetMeSSATab()->GetOriginalStTable(), func->GetAllBBs().size(), + ssa->InitRenameStack(func->GetMeSSATab()->GetOriginalStTable(), cfg->GetAllBBs().size(), func->GetMeSSATab()->GetVersionStTable()); // recurse down dominator tree in pre-order traversal - MapleSet *children = &dom->domChildren[func->GetCommonEntryBB()->GetBBId()]; + MapleSet *children = &dom->domChildren[cfg->GetCommonEntryBB()->GetBBId()]; for (BBId child : *children) { - ssa->RenameBB(*func->GetBBFromID(child)); + ssa->RenameBB(*cfg->GetBBFromID(child)); } ssa->VerifySSA(); diff --git a/src/mapleall/maple_me/src/me_ssa_devirtual.cpp b/src/mapleall/maple_me/src/me_ssa_devirtual.cpp index d44a7bdecc1be90c395e6fd2805a08895cb3c70c..a6b23fe625b92596d011c1f467c6e2e6ff1925cd 100644 --- a/src/mapleall/maple_me/src/me_ssa_devirtual.cpp +++ b/src/mapleall/maple_me/src/me_ssa_devirtual.cpp @@ -41,7 +41,7 @@ AnalysisResult *MeDoSSADevirtual::Run(MeFunction *func, MeFuncResultMgr *frm, Mo if (DEBUGFUNC(func)) { SSADevirtual::debug = true; } - meSSADevirtual.Perform(*func->GetCommonEntryBB()); + meSSADevirtual.Perform(*func->GetCfg()->GetCommonEntryBB()); if (DEBUGFUNC(func)) { SSADevirtual::debug = false; LogInfo::MapleLogger() << "\n============== After SSA Devirtualization =============" << "\n"; diff --git a/src/mapleall/maple_me/src/me_ssa_epre.cpp b/src/mapleall/maple_me/src/me_ssa_epre.cpp index 8d86ef897a2a6217ed3b7a6fb8aeef2bf7f1935c..89942763d40129c4571d14673fd189e6919c4190 100644 --- a/src/mapleall/maple_me/src/me_ssa_epre.cpp +++ b/src/mapleall/maple_me/src/me_ssa_epre.cpp @@ -29,9 +29,10 @@ const std::set propWhiteList { // the set dfSet, visiting each BB only once namespace maple { void MeSSAEPre::BuildWorkList() { + auto cfg = func->GetCfg(); const MapleVector &preOrderDt = dom->GetDtPreOrder(); for (auto &bbID : preOrderDt) { - BB *bb = func->GetAllBBs().at(bbID); + BB *bb = cfg->GetAllBBs().at(bbID); BuildWorkListBB(bb); } } @@ -63,10 +64,11 @@ AnalysisResult *MeDoSSAEPre::Run(MeFunction *func, MeFuncResultMgr *m, ModuleRes ++puCount; return nullptr; } - auto *dom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); - ASSERT(dom != nullptr, "dominance phase has problem"); + // make irmapbuild first because previous phase may invalid all analysis results auto *irMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAPBUILD, func)); ASSERT(irMap != nullptr, "irMap phase has problem"); + auto *dom = static_cast(m->GetAnalysisResult(MeFuncPhase_DOMINANCE, func)); + ASSERT(dom != nullptr, "dominance phase has problem"); KlassHierarchy *kh = nullptr; if (func->GetMIRModule().IsJavaModule()) { kh = static_cast(mrm->GetAnalysisResult(MoPhase_CHA, &func->GetMIRModule())); diff --git a/src/mapleall/maple_me/src/me_ssa_lpre.cpp b/src/mapleall/maple_me/src/me_ssa_lpre.cpp index 8734c39408109d94a431a33e98037ef6c2db6522..7073b2dfbcaefc5c8ad1f2f79209ac78769e0d7b 100644 --- a/src/mapleall/maple_me/src/me_ssa_lpre.cpp +++ b/src/mapleall/maple_me/src/me_ssa_lpre.cpp @@ -204,7 +204,7 @@ void MeSSALPre::BuildEntryLHSOcc4Formals() const { (void)workCand->GetRealOccs().insert(occIt, occ); // insert at beginning occ->SetIsLHS(true); occ->SetIsFormalAtEntry(true); - occ->SetBB(*func->GetFirstBB()); + occ->SetBB(*func->GetCfg()->GetFirstBB()); } void MeSSALPre::BuildWorkListLHSOcc(MeStmt &meStmt, int32 seqStmt) { @@ -359,7 +359,7 @@ void MeSSALPre::BuildWorkList() { } const MapleVector &preOrderDt = dom->GetDtPreOrder(); for (size_t i = 0; i < numBBs; ++i) { - BB *bb = tmpFunc.GetBBFromID(preOrderDt[i]); + BB *bb = tmpFunc.GetCfg()->GetBBFromID(preOrderDt[i]); BuildWorkListBB(bb); } } diff --git a/src/mapleall/maple_me/src/me_ssa_tab.cpp b/src/mapleall/maple_me/src/me_ssa_tab.cpp index 334654f8cf04bbba0ebf0210adb96c9e4d328ec1..b059b7f2e7b45ce0fd9ce85bab30117f15e19780 100644 --- a/src/mapleall/maple_me/src/me_ssa_tab.cpp +++ b/src/mapleall/maple_me/src/me_ssa_tab.cpp @@ -13,12 +13,14 @@ * See the Mulan PSL v2 for more details. */ #include "me_ssa_tab.h" +#include "me_cfg.h" #include #include "mpl_timer.h" // 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*) { + auto cfg = static_cast(m->GetAnalysisResult(MeFuncPhase_MECFG, func)); MPLTimer timer; timer.Start(); if (DEBUGFUNC(func)) { @@ -32,7 +34,7 @@ AnalysisResult *MeDoSSATab::Run(MeFunction *func, MeFuncResultMgr*, ModuleResult globalSSATab = ssaTab; #endif // pass through the program statements - for (auto bIt = func->valid_begin(); bIt != func->valid_end(); ++bIt) { + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { auto *bb = *bIt; for (auto &stmt : bb->GetStmtNodes()) { ssaTab->CreateSSAStmt(stmt, bb); // this adds the SSANodes for exprs diff --git a/src/mapleall/maple_me/src/me_ssa_update.cpp b/src/mapleall/maple_me/src/me_ssa_update.cpp index 3e39a2a25277b061933ce14edc10930d93e4faa6..10505e478b7b89daf245f569e4b57114b640bb2b 100644 --- a/src/mapleall/maple_me/src/me_ssa_update.cpp +++ b/src/mapleall/maple_me/src/me_ssa_update.cpp @@ -25,6 +25,7 @@ namespace maple { void MeSSAUpdate::InsertPhis() { MapleMap *>::iterator it = updateCands.begin(); MapleSet dfSet(ssaUpdateAlloc.Adapter()); + auto cfg = func.GetCfg(); for (; it != updateCands.end(); ++it) { dfSet.clear(); for (const auto &bbId : *it->second) { @@ -32,7 +33,7 @@ void MeSSAUpdate::InsertPhis() { } for (const auto &bbId : dfSet) { // insert a phi node - BB *bb = func.GetBBFromID(bbId); + BB *bb = cfg->GetBBFromID(bbId); ASSERT_NOT_NULL(bb); auto phiListIt = bb->GetMePhiList().find(it->first); if (phiListIt != bb->GetMePhiList().end()) { @@ -224,6 +225,7 @@ void MeSSAUpdate::RenameBB(BB &bb) { // for recording stack height on entering this BB, to pop back to same height // when backing up the dominator tree std::map origStackSize; + auto cfg = func.GetCfg(); for (auto it = renameStacks.begin(); it != renameStacks.end(); ++it) { origStackSize[it->first] = it->second->size(); } @@ -233,7 +235,7 @@ void MeSSAUpdate::RenameBB(BB &bb) { // recurse down dominator tree in pre-order traversal const MapleSet &children = dom.GetDomChildren(bb.GetBBId()); for (const auto &child : children) { - RenameBB(*func.GetBBFromID(child)); + RenameBB(*cfg->GetBBFromID(child)); } // pop stacks back to where they were at entry to this BB for (auto it = renameStacks.begin(); it != renameStacks.end(); ++it) { @@ -253,9 +255,10 @@ void MeSSAUpdate::Run() { renameStack->push(zeroVersVar); } // recurse down dominator tree in pre-order traversal - const MapleSet &children = dom.GetDomChildren(func.GetCommonEntryBB()->GetBBId()); + auto cfg = func.GetCfg(); + const MapleSet &children = dom.GetDomChildren(cfg->GetCommonEntryBB()->GetBBId()); for (const auto &child : children) { - RenameBB(*func.GetBBFromID(child)); + RenameBB(*cfg->GetBBFromID(child)); } } } // namespace maple diff --git a/src/mapleall/maple_me/src/me_ssi.cpp b/src/mapleall/maple_me/src/me_ssi.cpp index 336eb0d5045476aa52dd9ce32abbc0c2d7e6494e..e706e764f62565aa13ba665b3fe60b5f2209e865 100644 --- a/src/mapleall/maple_me/src/me_ssi.cpp +++ b/src/mapleall/maple_me/src/me_ssi.cpp @@ -159,7 +159,7 @@ void MeSSI::AddNullPointerInfoForVar() { } void MeSSI::InsertPiNodes() { - for (auto bIt = meFunc->valid_begin(), eIt = meFunc->valid_end(); bIt != eIt; ++bIt) { + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { BB *bb = *bIt; if (bb->GetKind() == kBBCondGoto) { uint8_t careOpt = AnalysisBranch(*(bb->GetLastMe())); @@ -268,13 +268,13 @@ void MeSSI::InsertPhiNodes() { } BB *oldDefBB = rhs->DefByBB(); if (oldDefBB == nullptr) { - oldDefBB = meFunc->GetCommonEntryBB(); + oldDefBB = cfg->GetCommonEntryBB(); CHECK_FATAL(rhs->IsZeroVersion(), "must be"); } CHECK_NULL_FATAL(oldDefBB); MapleSet &dfs = dom->GetDomFrontier(newDefBB->GetBBId()); for (auto bbID : dfs) { - BB *dfBB = meFunc->GetBBFromID(bbID); + BB *dfBB = cfg->GetBBFromID(bbID); if (!dom->Dominate(*oldDefBB, *dfBB)) { MapleSet &dfsTmp = dom->GetDomFrontier(oldDefBB->GetBBId()); CHECK_FATAL(dfsTmp.find(bbID) != dfsTmp.end(), "must be"); @@ -330,7 +330,7 @@ void MeSSI::RenameStartPiArray(DefPoint &newDefPoint) { ReplacePiPhiInSuccs(*newDefBB, *(newDefPoint.GetLHS())); const MapleSet &children = dom->GetDomChildren(newDefBB->GetBBId()); for (const BBId &child : children) { - ReplaceBB(*(meFunc->GetBBFromID(child)), *newDefBB, newDefPoint); + ReplaceBB(*(cfg->GetBBFromID(child)), *newDefBB, newDefPoint); } } @@ -344,7 +344,7 @@ void MeSSI::RenameStartPhi(DefPoint &newDefPoint) { ReplacePiPhiInSuccs(*newDefBB, *(newDefPoint.GetLHS())); const MapleSet &children = dom->GetDomChildren(newDefBB->GetBBId()); for (const BBId &child : children) { - ReplaceBB(*(meFunc->GetBBFromID(child)), *newDefBB, newDefPoint); + ReplaceBB(*(cfg->GetBBFromID(child)), *newDefBB, newDefPoint); } } @@ -609,7 +609,7 @@ void MeSSI::ReplaceBB(BB &bb, BB &parentBB, DefPoint &newDefPoint) { ReplacePiPhiInSuccs(bb, *(newDefPoint.GetLHS())); const MapleSet &children = dom->GetDomChildren(bb.GetBBId()); for (const BBId &child : children) { - ReplaceBB(*(meFunc->GetBBFromID(child)), bb, newDefPoint); + ReplaceBB(*(cfg->GetBBFromID(child)), bb, newDefPoint); } } diff --git a/src/mapleall/maple_me/src/me_ssu_pre.cpp b/src/mapleall/maple_me/src/me_ssu_pre.cpp index 308a187f4c1b1d976deadc0d92653c531f146820..8b640dfe4a2da99cd81760cf300293cbbbfd5798 100644 --- a/src/mapleall/maple_me/src/me_ssu_pre.cpp +++ b/src/mapleall/maple_me/src/me_ssu_pre.cpp @@ -363,7 +363,7 @@ void MeSSUPre::CreateSortedOccs() { std::multiset lambdaResDfns; for (uint32 dfn : lambdaDfns) { const BBId &bbId = dom->GetPdtPreOrderItem(dfn); - BB *bb = func->GetAllBBs().at(bbId); + BB *bb = cfg->GetAllBBs().at(bbId); for (BB *succ : bb->GetSucc()) { (void)lambdaResDfns.insert(dom->GetPdtDfnItem(succ->GetBBId())); } @@ -386,11 +386,11 @@ void MeSSUPre::CreateSortedOccs() { SLambdaOcc *nextLambdaOcc = nullptr; if (lambdaDfnIt != lambdaDfns.end()) { nextLambdaOcc = - spreMp->New(*func->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaDfnIt)), spreAllocator); + spreMp->New(*cfg->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaDfnIt)), spreAllocator); } SLambdaResOcc *nextLambdaResOcc = nullptr; if (lambdaResDfnIt != lambdaResDfns.end()) { - nextLambdaResOcc = spreMp->New(*func->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaResDfnIt))); + nextLambdaResOcc = spreMp->New(*cfg->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaResDfnIt))); auto it = bb2LambdaResMap.find(dom->GetPdtPreOrderItem(*lambdaResDfnIt)); if (it == bb2LambdaResMap.end()) { std::forward_list newlist = { nextLambdaResOcc }; @@ -447,7 +447,7 @@ void MeSSUPre::CreateSortedOccs() { ++lambdaDfnIt; if (lambdaDfnIt != lambdaDfns.end()) { nextLambdaOcc = - spreMp->New(*func->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaDfnIt)), spreAllocator); + spreMp->New(*cfg->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaDfnIt)), spreAllocator); } else { nextLambdaOcc = nullptr; } @@ -457,7 +457,7 @@ void MeSSUPre::CreateSortedOccs() { ++lambdaResDfnIt; if (lambdaResDfnIt != lambdaResDfns.end()) { nextLambdaResOcc = - spreMp->New(*func->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaResDfnIt))); + spreMp->New(*cfg->GetAllBBs().at(dom->GetPdtPreOrderItem(*lambdaResDfnIt))); CHECK_NULL_FATAL(dom); auto it = bb2LambdaResMap.find(dom->GetPdtPreOrderItem(*lambdaResDfnIt)); if (it == bb2LambdaResMap.end()) { @@ -495,7 +495,7 @@ void MeSSUPre::CreateSortedOccs() { } void MeSSUPre::ApplySSUPre() { - BuildWorkListBB(func->GetCommonExitBB()); + BuildWorkListBB(cfg->GetCommonExitBB()); if (!MeOption::gcOnly && preKind != kSecondDecrefPre) { // #0 build worklist CreateEmptyCleanupIntrinsics(); } diff --git a/src/mapleall/maple_me/src/me_stmt_pre.cpp b/src/mapleall/maple_me/src/me_stmt_pre.cpp index 2fd9989c52c8b0b8164432e7b85b81799d2104da..08c71788c05f85f4029bead04a5ea7fe92b25bce 100644 --- a/src/mapleall/maple_me/src/me_stmt_pre.cpp +++ b/src/mapleall/maple_me/src/me_stmt_pre.cpp @@ -747,8 +747,9 @@ void MeStmtPre::ConstructUseOccurMap() { } // do a pass over the program const MapleVector &preOrderDt = dom->GetDtPreOrder(); + auto cfg = func->GetCfg(); for (size_t i = 0; i < preOrderDt.size(); ++i) { - BB *bb = func->GetAllBBs().at(preOrderDt[i]); + BB *bb = cfg->GetAllBBs().at(preOrderDt[i]); for (auto &stmt : bb->GetMeStmts()) { for (size_t j = 0; j < stmt.NumMeStmtOpnds(); ++j) { ConstructUseOccurMapExpr(static_cast(i), *stmt.GetOpnd(j)); @@ -1084,7 +1085,7 @@ void MeStmtPre::BuildWorkList() { varStack->push(static_cast(irMap->GetOrCreateZeroVersionVarMeExpr(*ost))); versionStackVec[ost->GetIndex()] = varStack; } - BuildWorkListBB(func->GetCommonEntryBB()); + BuildWorkListBB(func->GetCfg()->GetCommonEntryBB()); } void MeStmtPre::RemoveUnnecessaryDassign(DassignMeStmt &dssMeStmt) { diff --git a/src/mapleall/maple_me/src/me_store_pre.cpp b/src/mapleall/maple_me/src/me_store_pre.cpp index 672e6cb31aa61623c41f70963671be19a98492fa..e61a66b404c1070ca1d6b52594103d849ab3489f 100644 --- a/src/mapleall/maple_me/src/me_store_pre.cpp +++ b/src/mapleall/maple_me/src/me_store_pre.cpp @@ -38,7 +38,7 @@ void MeStorePre::CheckCreateCurTemp() { // each bb. The return value is the curTemp version that contains the RHS value // at the entry to bb; RegMeExpr *MeStorePre::EnsureRHSInCurTemp(BB &bb) { - CHECK_FATAL(&bb != func->GetCommonEntryBB(), "EnsureRHSInCurTemp: cannot find earlier definition"); + CHECK_FATAL(&bb != func->GetCfg()->GetCommonEntryBB(), "EnsureRHSInCurTemp: cannot find earlier definition"); // see if processed before auto mapIt = bbCurTempMap.find(&bb); if (mapIt != bbCurTempMap.end()) { @@ -212,7 +212,7 @@ void MeStorePre::CreateRealOcc(const OStIdx &ostIdx, MeStmt &meStmt) { workCandMap[ostIdx] = wkCand; // if it is local symbol, insert artificial use occ at common_exit_bb if (ost->IsLocal()) { - SRealOcc *artOcc = spreMp->New(*func->GetCommonExitBB()); + SRealOcc *artOcc = spreMp->New(*func->GetCfg()->GetCommonExitBB()); wkCand->GetRealOccs().push_back(artOcc); } } @@ -242,7 +242,7 @@ void MeStorePre::CreateUseOcc(const OStIdx &ostIdx, BB &bb) { workCandMap[ostIdx] = wkCand; // if it is local symbol, insert artificial real occ at common_exit_bb if (ost->IsLocal()) { - SRealOcc *artOcc = spreMp->New(*func->GetCommonExitBB()); + SRealOcc *artOcc = spreMp->New(*func->GetCfg()->GetCommonExitBB()); wkCand->GetRealOccs().push_back(artOcc); } } else { @@ -359,7 +359,7 @@ void MeStorePre::BuildWorkListBB(BB *bb) { } // recurse on child BBs in post-dominator tree for (BBId bbId : dom->GetPdomChildrenItem(bb->GetBBId())) { - BuildWorkListBB(func->GetAllBBs().at(bbId)); + BuildWorkListBB(func->GetCfg()->GetAllBBs().at(bbId)); } } diff --git a/src/mapleall/maple_me/src/me_subsum_rc.cpp b/src/mapleall/maple_me/src/me_subsum_rc.cpp index bc50ea09a962d4c4aba6a8deedf2f3cef51c8227..17601f7c800ee9199dd8d4b91ec2fac7ae81e69e 100644 --- a/src/mapleall/maple_me/src/me_subsum_rc.cpp +++ b/src/mapleall/maple_me/src/me_subsum_rc.cpp @@ -16,7 +16,7 @@ namespace maple { void SubsumRC::SetCantSubsum() { - for (BB *bb : func->GetAllBBs()) { + for (BB *bb : cfg->GetAllBBs()) { if (bb == nullptr) { continue; } @@ -201,7 +201,7 @@ void SubsumRC::BuildRealOccs(MeStmt &stmt, BB &bb) { } // recurse on child BBs in post-dominator tree for (BBId bbId : dom->GetPdomChildrenItem(bb.GetBBId())) { - BuildRealOccs(stmt, *func->GetBBFromID(bbId)); + BuildRealOccs(stmt, *cfg->GetBBFromID(bbId)); } } @@ -264,7 +264,7 @@ void SubsumRC::BuildWorkListBB(BB *bb) { LogInfo::MapleLogger() << "Create realocc for stmt:"; dass->Dump(irMap); } - BuildRealOccs(*dass, *(func->GetCommonExitBB())); + BuildRealOccs(*dass, *(cfg->GetCommonExitBB())); } } if (bb->GetAttributes(kBBAttrIsEntry)) { @@ -272,13 +272,13 @@ void SubsumRC::BuildWorkListBB(BB *bb) { } // recurse on child BBs in post-dominator tree for (BBId bbId : dom->GetPdomChildrenItem(bb->GetBBId())) { - BuildWorkListBB(func->GetBBFromID(bbId)); + BuildWorkListBB(cfg->GetBBFromID(bbId)); } } void SubsumRC::RunSSUPre() { SetCantSubsum(); - BuildWorkListBB(func->GetCommonExitBB()); + BuildWorkListBB(cfg->GetCommonExitBB()); if (enabledDebug) { LogInfo::MapleLogger() << "------ worklist initial size " << candMap.size() << '\n'; } diff --git a/src/mapleall/maple_me/src/me_verify.cpp b/src/mapleall/maple_me/src/me_verify.cpp index c48071e8086668f7d14de561ac344839005d7355..210cd4224cc7951ef0985073cec47994c8f70de3 100644 --- a/src/mapleall/maple_me/src/me_verify.cpp +++ b/src/mapleall/maple_me/src/me_verify.cpp @@ -244,7 +244,7 @@ void VerifyGlobalTypeTable() { void MeVerify::VerifyPhiNode(const BB &bb, Dominance &dom) const { if (enableDebug) { - meFunc.GetTheCfg()->DumpToFile("meverify"); + meFunc.GetCfg()->DumpToFile("meverify"); } for (auto &it : bb.GetMePhiList()) { auto *phiNode = it.second; @@ -330,7 +330,7 @@ void MeVerify::VerifyBBKind(const BB &bb) const { // Filter the special case which created in the function named RemoveEhEdgesInSyncRegion of mefunction. void MeVerify::DealWithSpecialCase(const BB &currBB, const BB &tryBB) const { - if (meFunc.GetEndTryBB2TryBB().size() != 1) { + if (meFunc.GetCfg()->GetEndTryBB2TryBB().size() != 1) { CHECK_FATAL(false, "must be try"); } if (currBB.GetAttributes(kBBAttrIsTryEnd)) { @@ -366,7 +366,7 @@ void MeVerify::VerifyAttrTryBB(BB &tryBB, int index) { int i = 0; for (auto offsetIt = tryStmt.GetOffsets().rbegin(), offsetEIt = tryStmt.GetOffsets().rend(); offsetIt != offsetEIt; ++offsetIt) { - auto offsetBBId = meFunc.GetLabelBBAt(*offsetIt)->GetBBId(); + auto offsetBBId = meFunc.GetCfg()->GetLabelBBAt(*offsetIt)->GetBBId(); bool needExit = false; for (size_t j = index; j < meFunc.GetLaidOutBBs().size() && !needExit; ++j) { auto currBB = meFunc.GetLaidOutBBs().at(j); @@ -481,7 +481,7 @@ bool MeVerify::IsOnlyHaveReturnOrThrowStmt(const BB &bb, Opcode op) const { } void MeVerify::VerifyCommonExitBB() const { - for (auto &pred : meFunc.GetCommonExitBB()->GetPred()) { + for (auto &pred : meFunc.GetCfg()->GetCommonExitBB()->GetPred()) { if (pred->GetKind() == kBBReturn) { continue; } @@ -493,7 +493,7 @@ void MeVerify::VerifyCommonExitBB() const { } void MeVerify::VerifyReturnBB(const BB &bb) const { - for (auto &pred : meFunc.GetCommonExitBB()->GetPred()) { + for (auto &pred : meFunc.GetCfg()->GetCommonExitBB()->GetPred()) { if (pred == &bb) { return; } @@ -525,4 +525,4 @@ AnalysisResult *MeDoVerify::Run(MeFunction *func, MeFuncResultMgr*, ModuleResult meVerify.VerifyFunction(); return nullptr; } -} // namespace maple \ No newline at end of file +} // namespace maple diff --git a/src/mapleall/maple_me/src/preg_renamer.cpp b/src/mapleall/maple_me/src/preg_renamer.cpp index 9dbb367614faccbe7c97376170d880545a272cf8..9d3595ddb9a19699cd0fd8f062e85d46459f53a0 100644 --- a/src/mapleall/maple_me/src/preg_renamer.cpp +++ b/src/mapleall/maple_me/src/preg_renamer.cpp @@ -27,9 +27,10 @@ void PregRenamer::RunSelf() { std::vector firstappeartable(pregtab->GetPregTable().size()); uint32 renameCount = 0; UnionFind unionFind(*mp, regmeexprtable.size()); + auto cfg = func->GetCfg(); // iterate all the bbs' phi to setup the union - for (BB *bb : func->GetAllBBs()) { - if (bb == nullptr || bb == func->GetCommonEntryBB() || bb == func->GetCommonExitBB()) { + for (BB *bb : cfg->GetAllBBs()) { + if (bb == nullptr || bb == cfg->GetCommonEntryBB() || bb == cfg->GetCommonExitBB()) { continue; } MapleMap &mePhiList = bb->GetMePhiList(); @@ -127,7 +128,7 @@ AnalysisResult *MeDoPregRename::Run(MeFunction *func, MeFuncResultMgr *frm, Modu auto *kh = static_cast(mrm->GetAnalysisResult(MoPhase_CHA, &func->GetMIRModule())); CHECK_FATAL(kh != nullptr, "kh phase has problem"); MeVerify verify(*func); - for (auto &bb : func->GetAllBBs()) { + for (auto &bb : func->GetCfg()->GetAllBBs()) { if (bb == nullptr) { continue; } diff --git a/src/mapleall/maple_me/src/sync_select.cpp b/src/mapleall/maple_me/src/sync_select.cpp index 14ef035529d0e01ce2272e4f3c4035056d2d55d2..eb273c358af42c7b2746df4b2c10e9deec126c0b 100644 --- a/src/mapleall/maple_me/src/sync_select.cpp +++ b/src/mapleall/maple_me/src/sync_select.cpp @@ -15,6 +15,7 @@ #include "sync_select.h" #include "safe_cast.h" #include "mpl_logging.h" +#include "me_cfg.h" namespace { using namespace maple; @@ -47,7 +48,8 @@ class SyncSelect { } void SetAllSyncKind(SyncKind kind) { - for (auto bIt = func.valid_begin(), eIt = func.valid_end(); bIt != eIt; ++bIt) { + auto cfg = func.GetCfg(); + for (auto bIt = cfg->valid_begin(), eIt = cfg->valid_end(); bIt != eIt; ++bIt) { for (auto &stmt : (*bIt)->GetStmtNodes()) { if (stmt.GetOpCode() == OP_syncenter) { SetSyncKind(stmt, kind);