diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index 8c30d3ba73159803822c01c072f58a9d21dd1813..c85ce8d38a5c971e37e92cc1e376df2b1f61eb9e 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -151,6 +151,7 @@ class CGFunc { void MarkCleanupEntryBB(); void SetCleanupLabel(BB &cleanupEntry); bool ExitbbNotInCleanupArea(const BB &bb) const; + void DeleteUnneededCleanup(); uint32 GetMaxRegNum() const { return maxRegCount; }; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_ico.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_ico.cpp index a95ab225044b48786922389db7bb82ab548e7357..8027f3181bc6f0d09cabe10587b648899901301f 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_ico.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_ico.cpp @@ -544,10 +544,18 @@ bool AArch64ICOIfThenElsePattern::DoOpt(BB &cmpBB, BB *ifBB, BB *elseBB, BB &joi /* Remove branches and merge join */ if (ifBB != nullptr) { + BB *prevLast = ifBB->GetPrev(); cgFunc->GetTheCFG()->RemoveBB(*ifBB); + if (ifBB->GetId() == cgFunc->GetLastBB()->GetId()) { + cgFunc->SetLastBB(*prevLast); + } } if (elseBB != nullptr) { + BB *prevLast = elseBB->GetPrev(); cgFunc->GetTheCFG()->RemoveBB(*elseBB); + if (elseBB->GetId() == cgFunc->GetLastBB()->GetId()) { + cgFunc->SetLastBB(*prevLast); + } } if (cmpBB.GetKind() != BB::kBBIf && cmpBB.GetNext() == &joinBB && diff --git a/src/mapleall/maple_be/src/cg/cfgo.cpp b/src/mapleall/maple_be/src/cg/cfgo.cpp index 33e19c8c62a22b3578ace428f512533525c078a0..ff5d476b5da8185edcb744488702c1b458f8837e 100644 --- a/src/mapleall/maple_be/src/cg/cfgo.cpp +++ b/src/mapleall/maple_be/src/cg/cfgo.cpp @@ -143,8 +143,14 @@ bool ChainingPattern::MoveSuccBBAsCurBBNext(BB &curBB, BB &sucBB) { sucBB.GetNext()->SetPrev(sucBB.GetPrev()); } sucBB.SetNext(curBB.GetNext()); - ASSERT(curBB.GetNext() != nullptr, "current goto BB will not be the last bb"); - curBB.GetNext()->SetPrev(&sucBB); + if (curBB.GetNext() != nullptr) { + curBB.GetNext()->SetPrev(&sucBB); + } + if (sucBB.GetId() == cgFunc->GetLastBB()->GetId()) { + cgFunc->SetLastBB(*(sucBB.GetPrev())); + } else if (curBB.GetId() == cgFunc->GetLastBB()->GetId()) { + cgFunc->SetLastBB(sucBB); + } sucBB.SetPrev(&curBB); curBB.SetNext(&sucBB); curBB.RemoveInsn(*curBB.GetLastInsn()); @@ -727,8 +733,11 @@ bool UnreachBBPattern::Optimize(BB &curBB) { ASSERT(curBB.GetPrev() != nullptr, "nullptr check"); curBB.GetPrev()->SetNext(curBB.GetNext()); - ASSERT(curBB.GetNext() != nullptr, "nullptr check"); - curBB.GetNext()->SetPrev(curBB.GetPrev()); + if (curBB.GetNext() == nullptr) { + cgFunc->SetLastBB(*(curBB.GetPrev())); + } else { + curBB.GetNext()->SetPrev(curBB.GetPrev()); + } /* flush after remove; */ for (BB *bb : curBB.GetSuccs()) { diff --git a/src/mapleall/maple_be/src/cg/cg_cfg.cpp b/src/mapleall/maple_be/src/cg/cg_cfg.cpp index f0bce5d888493a23710bab5c1b774525d285dcdd..90ea8e453050124c37e37daf7dc08cd091055a6a 100644 --- a/src/mapleall/maple_be/src/cg/cg_cfg.cpp +++ b/src/mapleall/maple_be/src/cg/cg_cfg.cpp @@ -252,7 +252,11 @@ bool CGCFG::AreCommentAllPreds(const BB &bb) { /* Merge sucBB into curBB. */ void CGCFG::MergeBB(BB &merger, BB &mergee, CGFunc &func) { + BB *prevLast = mergee.GetPrev(); MergeBB(merger, mergee); + if (func.GetLastBB()->GetId() == mergee.GetId()) { + func.SetLastBB(*prevLast); + } if (mergee.GetKind() == BB::kBBReturn) { for (size_t i = 0; i < func.ExitBBsVecSize(); ++i) { if (func.GetExitBB(i) == &mergee) { @@ -468,9 +472,11 @@ void CGCFG::RemoveBB(BB &curBB, bool isGotoIf) { for (BB *ehPred : curBB.GetEhPreds()) { ehPred->RemoveEhSuccs(curBB); } - curBB.GetNext()->RemovePreds(curBB); + if (curBB.GetNext()) { + curBB.GetNext()->RemovePreds(curBB); + curBB.GetNext()->SetPrev(curBB.GetPrev()); + } curBB.GetPrev()->SetNext(curBB.GetNext()); - curBB.GetNext()->SetPrev(curBB.GetPrev()); cgFunc->ClearBBInVec(curBB.GetId()); /* remove callsite */ EHFunc* ehFunc = cgFunc->GetEHFunc(); @@ -809,6 +815,9 @@ void CGCFG::BreakCriticalEdge(BB &pred, BB &succ) { cgFunc->SetLastBB(*newBB); } else { exitBB->AppendBB(*newBB); + if (cgFunc->GetLastBB() == exitBB) { + cgFunc->SetLastBB(*newBB); + } } newBB->AppendInsn(cgFunc->GetCG()->BuildInstruction( MOP_xuncond, cgFunc->GetOrCreateLabelOperand(succ.GetLabIdx()))); diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index dfd50b9e25031fd57650e13c1daf1232eb2396d1..093fec81d8c4edb15a9b01feb46e39e9e3a7f095 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -1653,6 +1653,29 @@ bool CGFunc::ExitbbNotInCleanupArea(const BB &bb) const { return true; } +void CGFunc::DeleteUnneededCleanup() { + if (mirModule.GetSrcLang() != kSrcLangC) { + return; + } + std::list delBB; + FOR_ALL_BB(bb, this) { + if (bb->IsCleanup()) { + delBB.push_back(bb); + } + } + for (auto bb : delBB) { + if (bb == this->GetLastBB()) { + this->SetLastBB(*bb->GetPrev()); + } + bb->GetPrev()->SetNext(bb->GetNext()); + if (bb->GetNext()) { + bb->GetNext()->SetPrev(bb->GetPrev()); + } + bb->SetPrev(nullptr); + bb->SetNext(nullptr); + } +} + /* * Do mem barrier optimization for constructor funcs as follow: * membarstorestore @@ -1745,6 +1768,15 @@ void CGFunc::AddCommonExitBB() { commonExitBB->PushBackPreds(*cgbb); } } + if (mirModule.GetSrcLang() != kSrcLangC) { + BB *oldLastBB = GetLastBB(); + oldLastBB->SetNext(commonExitBB); + commonExitBB->SetPrev(oldLastBB); + commonExitBB->SetNext(nullptr); + /* fake commonExitBB only has preds, no succs, so it is not reachable */ + commonExitBB->SetUnreachable(true); + SetLastBB(*commonExitBB); + } } void CGFunc::UpdateCallBBFrequency() { @@ -1799,6 +1831,7 @@ void CGFunc::HandleFunction() { if (CGOptions::IsLazyBinding() && !GetCG()->IsLibcore()) { ProcessLazyBinding(); } + DeleteUnneededCleanup(); if (GetCG()->DoPatchLongBranch()) { PatchLongBranch(); }