From f01a8d813c8f6310ca4c2f61794ca0532d69377f Mon Sep 17 00:00:00 2001 From: linma Date: Mon, 24 Jan 2022 22:23:53 -0800 Subject: [PATCH] delete more empty fallthru BBs in optimiseCFG bblayout phase --- src/mapleall/maple_me/include/me_bb_layout.h | 1 + src/mapleall/maple_me/src/me_bb_layout.cpp | 54 +++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/mapleall/maple_me/include/me_bb_layout.h b/src/mapleall/maple_me/include/me_bb_layout.h index 99d100a206..a5d51856ba 100644 --- a/src/mapleall/maple_me/include/me_bb_layout.h +++ b/src/mapleall/maple_me/include/me_bb_layout.h @@ -236,6 +236,7 @@ class BBLayout { void LayoutWithoutProf(); void RunLayout(); void DumpBBPhyOrder() const; + void VerifyBB(); private: void FixEndTryBB(BB &bb); diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index dc8797651b..0226e8c606 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -947,16 +947,27 @@ BB *BBLayout::CreateGotoBBAfterCondBB(BB &bb, BB &fallthru) { void BBLayout::OptimizeEmptyFallThruBB(BB &bb) { if (needDealWithTryBB) { return; } auto *fallthru = bb.GetSucc().front(); - if (fallthru && fallthru->GetBBLabel() == 0 && - (BBEmptyAndFallthru(*fallthru) || BBContainsOnlyGoto(*fallthru))) { - if (fallthru->GetSucc().front() == bb.GetSucc().back()) { - bb.ReplaceSucc(fallthru, bb.GetSucc().back()); + while (fallthru && (fallthru->GetPred().size() == 1) && + (BBEmptyAndFallthru(*fallthru) || BBContainsOnlyGoto(*fallthru))) { + BB *newFallthru = fallthru->GetSucc().front(); + if (newFallthru == bb.GetSucc().back()) { + bb.ReplaceSucc(fallthru, newFallthru); ASSERT(fallthru->GetPred().empty(), "fallthru should not has other pred"); ChangeToFallthruFromCondGoto(bb); bb.GetSucc().resize(1); // resize succ to 1 - laidOut[fallthru->GetBBId()] = true; - RemoveUnreachable(*fallthru); + } else if (newFallthru->GetPred().size() == 1) { + if (newFallthru->GetBBLabel() != 0) { + // reset newFallthru label + newFallthru->SetBBLabel(0); + } + // replace empty fallthru with fallthru's succ + bb.ReplaceSucc(fallthru, newFallthru); + } else { + break; } + laidOut[fallthru->GetBBId()] = true; + RemoveUnreachable(*fallthru); + fallthru = newFallthru; } } @@ -993,6 +1004,19 @@ void BBLayout::OptimiseCFG() { } } (void)cfg->UnreachCodeAnalysis(false); + + // do 2nd fallthru optimize if succs of bb were optimized by first iteration + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { + if (bIt == cfg->common_entry() || bIt == cfg->common_exit()) { + continue; + } + auto *bb = *bIt; + if (bb->GetKind() == kBBCondGoto) { + OptimizeEmptyFallThruBB(*bb); + } + } + (void)cfg->UnreachCodeAnalysis(false); + } void BBLayout::SetAttrTryForTheCanBeMovedBB(BB &bb, BB &canBeMovedBB) const { @@ -1306,6 +1330,21 @@ void BBLayout::RunLayout() { } } +// check condBB if they have same succs +void BBLayout::VerifyBB() { + for (auto bIt = cfg->valid_begin(); bIt != cfg->valid_end(); ++bIt) { + auto *bb = *bIt; + if (bb->GetKind() == kBBCondGoto) { + auto *fallthru = bb->GetSucc(0); + auto *targetBB = bb->GetSucc(1); + if (fallthru == targetBB || + (BBEmptyAndFallthru(*fallthru) && (fallthru->GetSucc(0) == targetBB))) { + LogInfo::MapleLogger() << "WARN: cond BB " << bb->GetBBId() << " has same target"; + } + } + } +} + void MEBBLayout::GetAnalysisDependence(maple::AnalysisDep &aDep) const { aDep.AddRequired(); aDep.AddRequired(); @@ -1322,7 +1361,10 @@ bool MEBBLayout::PhaseRun(maple::MeFunction &f) { } bbLayout->RunLayout(); f.SetLaidOutBBs(bbLayout->GetBBs()); + if (DEBUGFUNC_NEWPM(f)) { + // verify CFG : check condBB's succs should be different + bbLayout->VerifyBB(); bbLayout->DumpBBPhyOrder(); cfg->DumpToFile("afterBBLayout", false); } -- Gitee