From ed660b77a31e606289f131f59ab472bb92178946 Mon Sep 17 00:00:00 2001 From: linma Date: Tue, 25 May 2021 17:29:12 -0700 Subject: [PATCH] refine code in loopcanon and identloop, check loop status before converting loop form --- src/mapleall/maple_me/include/me_loop_canon.h | 2 +- .../maple_me/src/me_loop_analysis.cpp | 16 +++---- src/mapleall/maple_me/src/me_loop_canon.cpp | 45 ++++++++++++------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/mapleall/maple_me/include/me_loop_canon.h b/src/mapleall/maple_me/include/me_loop_canon.h index b917fab6f9..239774d062 100644 --- a/src/mapleall/maple_me/include/me_loop_canon.h +++ b/src/mapleall/maple_me/include/me_loop_canon.h @@ -30,7 +30,7 @@ class MeDoLoopCanon : public MeFuncPhase { std::string PhaseName() const override { return "loopcanon"; } - + bool cfgchanged = false; private: using Key = std::pair; std::map> heads; diff --git a/src/mapleall/maple_me/src/me_loop_analysis.cpp b/src/mapleall/maple_me/src/me_loop_analysis.cpp index 48da3541fa..eacd5b47d1 100644 --- a/src/mapleall/maple_me/src/me_loop_analysis.cpp +++ b/src/mapleall/maple_me/src/me_loop_analysis.cpp @@ -46,6 +46,14 @@ void IdentifyLoops::InsertExitBB(LoopDesc &loop) { while (!inLoopBBs.empty()) { BB *curBB = inLoopBBs.front(); inLoopBBs.pop(); + if (curBB->GetKind() == kBBCondGoto) { + if (curBB->GetSucc().size() == 1) { + // When the size of succs is one, one of succs may be commonExitBB. Need insert to loopBB2exitBBs. + CHECK_FATAL(false, "return bb"); + } + } else if (!curBB->GetStmtNodes().empty() && curBB->GetLast().GetOpCode() == OP_return) { + CHECK_FATAL(false, "return bb"); + } for (BB *succ : curBB->GetSucc()) { if (traveledBBs.count(succ) != 0) { continue; @@ -56,14 +64,6 @@ void IdentifyLoops::InsertExitBB(LoopDesc &loop) { } else { loop.InsertInloopBB2exitBBs(*curBB, *succ); } - if (curBB->GetKind() == kBBCondGoto) { - if (curBB->GetSucc().size() == 1) { - // When the size of succs is one, one of succs may be commonExitBB. Need insert to loopBB2exitBBs. - CHECK_FATAL(false, "return bb"); - } - } else if (!curBB->GetStmtNodes().empty() && curBB->GetLast().GetOpCode() == OP_return) { - CHECK_FATAL(false, "return bb"); - } } } for (auto pair : loop.inloopBB2exitBBs) { diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index 723883a598..fcdb011383 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -51,6 +51,10 @@ static bool CompareBackedge(const std::pair &a, const std::pair &swapSuccs) const { + // loop is transformed + if (pred.GetAttributes(kBBAttrArtificial)) { + return false; + } bb.SetAttributes(kBBAttrIsInLoop); pred.SetAttributes(kBBAttrIsInLoop); // do not convert do-while loop @@ -119,6 +123,7 @@ void MeDoLoopCanon::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapNewBasicBlock(); latchBB->SetAttributes(kBBAttrArtificial); + latchBB->SetAttributes(kBBAttrIsInLoop); // latchBB is inloop // update pred bb pred.ReplaceSucc(&bb, latchBB); // replace pred.succ with latchBB and set pred of latchBB // update pred stmt if needed @@ -339,8 +344,15 @@ void MeDoLoopCanon::Merge(MeFunction &func) { MeCFG *cfg = func.GetCfg(); for (auto iter = heads.begin(); iter != heads.end(); ++iter) { BB *head = cfg->GetBBFromID(iter->first); + // skip case : check latch bb is already added + if ((head->GetPred().size() == 2) && + (head->GetPred(1)->GetAttributes(kBBAttrArtificial)) && + (head->GetPred(1)->GetKind() == kBBFallthru)) { + continue; + } auto *latchBB = cfg->NewBasicBlock(); latchBB->SetAttributes(kBBAttrArtificial); + latchBB->SetAttributes(kBBAttrIsInLoop); // latchBB is inloop latchBB->SetKind(kBBFallthru); for (BB *tail : iter->second) { tail->ReplaceSucc(head, latchBB); @@ -350,6 +362,7 @@ void MeDoLoopCanon::Merge(MeFunction &func) { UpdateTheOffsetOfStmtWhenTargetBBIsChange(func, *tail, *head, *latchBB); } head->AddPred(*latchBB); + cfgchanged = true; } } @@ -384,6 +397,7 @@ void MeDoLoopCanon::AddPreheader(MeFunction &func) { UpdateTheOffsetOfStmtWhenTargetBBIsChange(func, *pred, *head, *preheader); } head->AddPred(*preheader); + cfgchanged = true; // set cfgchanged flag } } @@ -436,6 +450,15 @@ void MeDoLoopCanon::InsertExitBB(MeFunction &func, LoopDesc &loop) { while (!inLoopBBs.empty()) { BB *curBB = inLoopBBs.front(); inLoopBBs.pop(); + if (curBB->GetKind() == kBBCondGoto) { + if (curBB->GetSucc().size() == 1) { + CHECK_FATAL(false, "return bb"); + loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); + } + } else if (!curBB->GetStmtNodes().empty() && curBB->GetLast().GetOpCode() == OP_return) { + CHECK_FATAL(false, "return bb"); + loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); + } for (BB *succ : curBB->GetSucc()) { if (traveledBBs.count(succ) != 0) { continue; @@ -446,15 +469,6 @@ void MeDoLoopCanon::InsertExitBB(MeFunction &func, LoopDesc &loop) { } else { loop.InsertInloopBB2exitBBs(*curBB, *succ); } - if (curBB->GetKind() == kBBCondGoto) { - if (curBB->GetSucc().size() == 1) { - CHECK_FATAL(false, "return bb"); - loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); - } - } else if (!curBB->GetStmtNodes().empty() && curBB->GetLast().GetOpCode() == OP_return) { - CHECK_FATAL(false, "return bb"); - loop.InsertInloopBB2exitBBs(*curBB, *cfg->GetCommonExitBB()); - } } } InsertNewExitBB(func, loop); @@ -523,11 +537,12 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr func.GetCfg()->DumpToFile("cfgbeforLoopNormalization"); } heads.clear(); + cfgchanged = false; FindHeadBBs(func, dom, func.GetCfg()->GetCommonEntryBB()); AddPreheader(func); Merge(func); - // cfg changes only if heads is not empty - if (!heads.empty()) { + // cfgchanged is true if preheader bb or latchbb is added + if (cfgchanged) { m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, &func); m->InvalidAnalysisResult(MeFuncPhase_MELOOP, &func); } @@ -535,7 +550,7 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr if (meLoop == nullptr) { return; } - bool cfgChange = false; + cfgchanged = false; for (auto loop : meLoop->GetMeLoops()) { if (loop->HasTryBB()) { continue; @@ -545,12 +560,12 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr CHECK_FATAL(loop->inloopBB2exitBBs.size() == 0, "must be zero"); InsertExitBB(func, *loop); loop->SetIsCanonicalLoop(true); - cfgChange = true; + cfgchanged = true; } if (loop->inloopBB2exitBBs.size() == 1 && loop->inloopBB2exitBBs.begin()->second->size() == 1 && IsDoWhileLoop(func, *loop)) { SplitCondGotBB(func, *loop); - cfgChange = true; + cfgchanged = true; } } if (DEBUGFUNC(&func)) { @@ -558,7 +573,7 @@ void MeDoLoopCanon::ExecuteLoopNormalization(MeFunction &func, MeFuncResultMgr func.Dump(true); func.GetCfg()->DumpToFile("cfgafterLoopNormalization"); } - if (cfgChange) { + if (cfgchanged) { m->InvalidAnalysisResult(MeFuncPhase_DOMINANCE, &func); m->InvalidAnalysisResult(MeFuncPhase_MELOOP, &func); } -- Gitee