From fe12e293080845a3f8f231a6b6cb0955c9e5d3c1 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 20 Aug 2022 00:17:34 -0700 Subject: [PATCH 01/18] In building CFG, recognize the noreturn attribute in calls and regard them as exit nodes --- src/mapleall/maple_be/src/be/lower.cpp | 4 ++++ src/mapleall/maple_ir/src/mir_builder.cpp | 4 +++- src/mapleall/maple_me/src/me_cfg.cpp | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 6ea5212315..58deafaf1a 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4096,6 +4096,10 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); + if (newBody->GetLast()->GetOpCode() == OP_call) { + // the call does not return; insert a dummy return to satisfy mplcg + newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); + } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); diff --git a/src/mapleall/maple_ir/src/mir_builder.cpp b/src/mapleall/maple_ir/src/mir_builder.cpp index 915b0d353e..99196419a0 100644 --- a/src/mapleall/maple_ir/src/mir_builder.cpp +++ b/src/mapleall/maple_ir/src/mir_builder.cpp @@ -984,7 +984,9 @@ IntrinsiccallNode *MIRBuilder::CreateStmtXintrinsicCallAssigned(MIRIntrinsicID i NaryStmtNode *MIRBuilder::CreateStmtReturn(BaseNode *rVal) { auto *stmt = GetCurrentFuncCodeMp()->New(*GetCurrentFuncCodeMpAllocator(), OP_return); ASSERT(stmt != nullptr, "stmt is null"); - stmt->PushOpnd(rVal); + if (rVal != nullptr) { + stmt->PushOpnd(rVal); + } return stmt; } diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index 1f7789f5b2..efd08d6dbe 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1584,6 +1584,24 @@ void MeCFG::CreateBasicBlocks() { curBB = newBB; break; } + case OP_call: { + CallNode *callStmt = static_cast(stmt); + MIRFunction *callee = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callStmt->GetPUIdx()); + if (callee->GetFuncAttrs().GetAttr(FUNCATTR_noreturn)) { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKindReturn(); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + curBB = newBB; + } + break; + } + // fall thru to handle as default + [[clang::fallthrough]]; + } default: { if (curBB->IsEmpty()) { curBB->SetFirst(stmt); -- Gitee From ab8704b16efef3201636a735ff1674b85b415b37 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 20 Aug 2022 00:17:34 -0700 Subject: [PATCH 02/18] In building CFG, recognize the noreturn attribute in calls and regard them as exit nodes --- src/mapleall/maple_be/src/be/lower.cpp | 4 ++++ src/mapleall/maple_me/src/me_cfg.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 2e5b94fdd6..50bd6800f7 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4088,6 +4088,10 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); + if (newBody->GetLast()->GetOpCode() == OP_call) { + // the call does not return; insert a dummy return to satisfy mplcg + newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); + } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index 1f7789f5b2..efd08d6dbe 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1584,6 +1584,24 @@ void MeCFG::CreateBasicBlocks() { curBB = newBB; break; } + case OP_call: { + CallNode *callStmt = static_cast(stmt); + MIRFunction *callee = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callStmt->GetPUIdx()); + if (callee->GetFuncAttrs().GetAttr(FUNCATTR_noreturn)) { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKindReturn(); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + curBB = newBB; + } + break; + } + // fall thru to handle as default + [[clang::fallthrough]]; + } default: { if (curBB->IsEmpty()) { curBB->SetFirst(stmt); -- Gitee From a3270ae3fd6dd094a8b0576bc3aa9a9fd942d9d2 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 25 Aug 2022 21:03:22 -0700 Subject: [PATCH 03/18] Do not update frequencies in ValueRangePropagation::RemoveUnreachableBB() --- src/mapleall/maple_me/src/me_loop_canon.cpp | 4 ++-- src/mapleall/maple_me/src/me_value_range_prop.cpp | 14 +------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index 655cdd656c..03e38347b8 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -295,7 +295,7 @@ void MeLoopCanon::NormalizationHeadAndPreHeaderOfLoop(Dominance &dom) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction before loop normalization----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgbeforLoopNormalization"); + func.GetCfg()->DumpToFile("cfgbeforLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } std::map> heads; isCFGChange = false; @@ -316,7 +316,7 @@ void MeLoopCanon::NormalizationExitOfLoop(IdentifyLoops &meLoop) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction after loop normalization-----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgafterLoopNormalization"); + func.GetCfg()->DumpToFile("cfgafterLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } } diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 227134ab91..a7cccf0685 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3614,12 +3614,6 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - // update frequency before cfg changed - if (func.GetCfg()->UpdateCFGFreq()) { - int64_t removedFreq = static_cast(condGotoBB.GetSuccFreq()[1]); - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ0->SetFrequency(static_cast(succ0->GetFrequency() + removedFreq)); - } condGotoBB.RemoveSucc(*succ1); DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ1, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); @@ -3632,16 +3626,10 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - int64_t removedFreq = 0; - // update frequency before cfg changed if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = static_cast(condGotoBB.GetSuccFreq()[0]); + condGotoBB.SetSuccFreq(0, condGotoBB.GetSuccFreq()[1]); } condGotoBB.RemoveSucc(*succ0); - if (func.GetCfg()->UpdateCFGFreq()) { - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ1->SetFrequency(static_cast(succ1->GetFrequency() + removedFreq)); - } DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ0, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } -- Gitee From 8ba98a31dcd64718a2afd4322ac6fc9cc18a23ed Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Mon, 29 Aug 2022 20:03:54 -0700 Subject: [PATCH 04/18] Remove 1 more frequency update in valueRangePropagation --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index a7cccf0685..a451280b20 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3231,11 +3231,6 @@ void ValueRangePropagation::AnalysisUnreachableBBOrEdge(BB &bb, BB &unreachableB bb.RemoveSucc(unreachableBB); bb.RemoveMeStmt(bb.GetLastMe()); bb.SetKind(kBBFallthru); - if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetSuccFreq(0, bb.GetFrequency()); - succBB.SetFrequency(static_cast(succBB.GetFrequency() + removedFreq)); - unreachableBB.SetFrequency(0); - } auto *loop = loops->GetBBLoopParent(bb.GetBBId()); if (loop == nullptr) { return; -- Gitee From 5e16ab285c9f66f2888a469333ff03d834e6ac16 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 20 Aug 2022 00:17:34 -0700 Subject: [PATCH 05/18] In building CFG, recognize the noreturn attribute in calls and regard them as exit nodes --- src/mapleall/maple_be/src/be/lower.cpp | 4 ++++ src/mapleall/maple_me/src/me_cfg.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index bb39897bab..ffce92b12a 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4094,6 +4094,10 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); + if (newBody->GetLast()->GetOpCode() == OP_call) { + // the call does not return; insert a dummy return to satisfy mplcg + newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); + } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index 533a536123..52957520ee 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1588,6 +1588,24 @@ void MeCFG::CreateBasicBlocks() { curBB = newBB; break; } + case OP_call: { + CallNode *callStmt = static_cast(stmt); + MIRFunction *callee = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callStmt->GetPUIdx()); + if (callee->GetFuncAttrs().GetAttr(FUNCATTR_noreturn)) { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKindReturn(); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + curBB = newBB; + } + break; + } + // fall thru to handle as default + [[clang::fallthrough]]; + } default: { if (curBB->IsEmpty()) { curBB->SetFirst(stmt); -- Gitee From 49bd4d65b91221d6accc0784394c6de0199d0bfe Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 25 Aug 2022 21:03:22 -0700 Subject: [PATCH 06/18] Do not update frequencies in ValueRangePropagation::RemoveUnreachableBB() --- src/mapleall/maple_me/src/me_loop_canon.cpp | 4 ++-- src/mapleall/maple_me/src/me_value_range_prop.cpp | 14 +------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index 3aae6c2514..e2d391a5bb 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -295,7 +295,7 @@ void MeLoopCanon::NormalizationHeadAndPreHeaderOfLoop(Dominance &dom) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction before loop normalization----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgbeforLoopNormalization"); + func.GetCfg()->DumpToFile("cfgbeforLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } std::map> heads; isCFGChange = false; @@ -316,7 +316,7 @@ void MeLoopCanon::NormalizationExitOfLoop(IdentifyLoops &meLoop) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction after loop normalization-----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgafterLoopNormalization"); + func.GetCfg()->DumpToFile("cfgafterLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } } diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 67448df356..962b72b427 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3621,12 +3621,6 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - // update frequency before cfg changed - if (func.GetCfg()->UpdateCFGFreq()) { - int64_t removedFreq = static_cast(condGotoBB.GetSuccFreq()[1]); - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ0->SetFrequency(static_cast(succ0->GetFrequency() + removedFreq)); - } condGotoBB.RemoveSucc(*succ1); DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ1, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); @@ -3639,16 +3633,10 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - int64_t removedFreq = 0; - // update frequency before cfg changed if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = static_cast(condGotoBB.GetSuccFreq()[0]); + condGotoBB.SetSuccFreq(0, condGotoBB.GetSuccFreq()[1]); } condGotoBB.RemoveSucc(*succ0); - if (func.GetCfg()->UpdateCFGFreq()) { - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ1->SetFrequency(static_cast(succ1->GetFrequency() + removedFreq)); - } DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ0, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } -- Gitee From 127e9f0d02f0bb19cab47cd97bbd9555de16f74c Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Mon, 29 Aug 2022 20:03:54 -0700 Subject: [PATCH 07/18] Remove 1 more frequency update in valueRangePropagation --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 962b72b427..f2dad090f1 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3238,11 +3238,6 @@ void ValueRangePropagation::AnalysisUnreachableBBOrEdge(BB &bb, BB &unreachableB bb.RemoveSucc(unreachableBB); bb.RemoveMeStmt(bb.GetLastMe()); bb.SetKind(kBBFallthru); - if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetSuccFreq(0, bb.GetFrequency()); - succBB.SetFrequency(static_cast(succBB.GetFrequency() + removedFreq)); - unreachableBB.SetFrequency(0); - } auto *loop = loops->GetBBLoopParent(bb.GetBBId()); if (loop == nullptr) { return; -- Gitee From be381e038591f3f0fe2460f6fbee9f0de1f583c3 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 1 Sep 2022 22:12:32 -0700 Subject: [PATCH 08/18] More adjustments of frequency updates in vrp --- .../maple_me/src/me_value_range_prop.cpp | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index f2dad090f1..0a46048ebf 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3931,12 +3931,14 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(trueBranch, index); CreateLabelForTargetBB(pred, trueBranch); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); - size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); - int64_t updatedtrueFreq = static_cast(bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); - // transform may not be consistent with frequency value - updatedtrueFreq = updatedtrueFreq > 0 ? updatedtrueFreq : 0; - bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); + int64_t updatedtrueFreq = static_cast(bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); + // transform may not be consistent with frequency value + updatedtrueFreq = updatedtrueFreq > 0 ? updatedtrueFreq : 0; + bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + } pred.AddSuccFreq(static_cast(edgeFreq), index); } } else { @@ -3954,14 +3956,16 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(*exitCopyFallthru, index); CreateLabelForTargetBB(pred, *exitCopyFallthru); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); - exitCopyFallthru->SetFrequency(static_cast(edgeFreq)); - exitCopyFallthru->PushBackSuccFreq(static_cast(edgeFreq)); - size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); - int64_t updatedtrueFreq = static_cast( - bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); - ASSERT(updatedtrueFreq >= 0, "sanity check"); - bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + exitCopyFallthru->SetFrequency(static_cast(edgeFreq)); + exitCopyFallthru->PushBackSuccFreq(static_cast(edgeFreq)); + size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); + int64_t updatedtrueFreq = static_cast( + bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); + ASSERT(updatedtrueFreq >= 0, "sanity check"); + bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + } pred.AddSuccFreq(static_cast(edgeFreq), index); } return true; @@ -3986,10 +3990,12 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(*newBB, index); newBB->AddSucc(trueBranch); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); - bb.UpdateEdgeFreqs(); - newBB->SetFrequency(static_cast(edgeFreq)); - newBB->PushBackSuccFreq(static_cast(edgeFreq)); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + bb.UpdateEdgeFreqs(); + newBB->SetFrequency(static_cast(edgeFreq)); + newBB->PushBackSuccFreq(static_cast(edgeFreq)); + } pred.AddSuccFreq(static_cast(edgeFreq), index); } DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); @@ -5207,7 +5213,6 @@ bool MEValueRangePropagation::PhaseRun(maple::MeFunction &f) { GetAnalysisInfoHook()->ForceRunTransFormPhase(&MELoopCanon::id, f); // update cfg frequency if (f.GetCfg()->UpdateCFGFreq()) { - f.GetCfg()->UpdateEdgeFreqWithBBFreq(); if (f.GetCfg()->DumpIRProfileFile()) { f.GetCfg()->DumpToFile("after-valuerange" + std::to_string(f.vrpRuns), false, true); } -- Gitee From 7fe74890cc69c02ca40d4cd86d17222a234fb64a Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 2 Sep 2022 15:38:07 -0700 Subject: [PATCH 09/18] Undo the change to maple_be/src/be/lower.cpp --- src/mapleall/maple_be/src/be/lower.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index ffce92b12a..bb39897bab 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4094,10 +4094,6 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); - if (newBody->GetLast()->GetOpCode() == OP_call) { - // the call does not return; insert a dummy return to satisfy mplcg - newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); - } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); -- Gitee From 4641d44a5794f761db536c13f3d77fe34e92d2e8 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Wed, 7 Sep 2022 08:55:28 -0700 Subject: [PATCH 10/18] an adjustment to the change in me_value_range_prop.cpp --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 0a46048ebf..354629f5a6 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3993,9 +3993,9 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); bb.UpdateEdgeFreqs(); - newBB->SetFrequency(static_cast(edgeFreq)); - newBB->PushBackSuccFreq(static_cast(edgeFreq)); } + newBB->SetFrequency(static_cast(edgeFreq)); + newBB->PushBackSuccFreq(static_cast(edgeFreq)); pred.AddSuccFreq(static_cast(edgeFreq), index); } DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); -- Gitee From 5bc4b495cac0d4049c8465da46f4afd028aeb870 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 20 Aug 2022 00:17:34 -0700 Subject: [PATCH 11/18] In building CFG, recognize the noreturn attribute in calls and regard them as exit nodes --- src/mapleall/maple_be/src/be/lower.cpp | 4 ++++ src/mapleall/maple_me/src/me_cfg.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index bb39897bab..ffce92b12a 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4094,6 +4094,10 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); + if (newBody->GetLast()->GetOpCode() == OP_call) { + // the call does not return; insert a dummy return to satisfy mplcg + newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); + } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index 533a536123..52957520ee 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1588,6 +1588,24 @@ void MeCFG::CreateBasicBlocks() { curBB = newBB; break; } + case OP_call: { + CallNode *callStmt = static_cast(stmt); + MIRFunction *callee = GlobalTables::GetFunctionTable().GetFunctionFromPuidx(callStmt->GetPUIdx()); + if (callee->GetFuncAttrs().GetAttr(FUNCATTR_noreturn)) { + if (curBB->IsEmpty()) { + curBB->SetFirst(stmt); + } + curBB->SetLast(stmt); + curBB->SetKindReturn(); + if (nextStmt != nullptr) { + BB *newBB = NewBasicBlock(); + curBB = newBB; + } + break; + } + // fall thru to handle as default + [[clang::fallthrough]]; + } default: { if (curBB->IsEmpty()) { curBB->SetFirst(stmt); -- Gitee From b32b7acd909be018fc907162c5bea0791c27dc32 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 25 Aug 2022 21:03:22 -0700 Subject: [PATCH 12/18] Do not update frequencies in ValueRangePropagation::RemoveUnreachableBB() --- src/mapleall/maple_me/src/me_loop_canon.cpp | 4 ++-- src/mapleall/maple_me/src/me_value_range_prop.cpp | 14 +------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/mapleall/maple_me/src/me_loop_canon.cpp b/src/mapleall/maple_me/src/me_loop_canon.cpp index eb645a9f32..a865a8babb 100644 --- a/src/mapleall/maple_me/src/me_loop_canon.cpp +++ b/src/mapleall/maple_me/src/me_loop_canon.cpp @@ -295,7 +295,7 @@ void MeLoopCanon::NormalizationHeadAndPreHeaderOfLoop(Dominance &dom) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction before loop normalization----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgbeforLoopNormalization"); + func.GetCfg()->DumpToFile("cfgbeforLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } std::map> heads; isCFGChange = false; @@ -316,7 +316,7 @@ void MeLoopCanon::NormalizationExitOfLoop(IdentifyLoops &meLoop) { if (isDebugFunc) { LogInfo::MapleLogger() << "-----------------Dump mefunction after loop normalization-----------\n"; func.Dump(true); - func.GetCfg()->DumpToFile("cfgafterLoopNormalization"); + func.GetCfg()->DumpToFile("cfgafterLoopNormalization", false, func.GetCfg()->UpdateCFGFreq()); } } diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 2f5dffcaaf..17bc877818 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3621,12 +3621,6 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - // update frequency before cfg changed - if (func.GetCfg()->UpdateCFGFreq()) { - uint64 removedFreq = condGotoBB.GetSuccFreq()[1]; - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ0->SetFrequency(succ0->GetFrequency() + removedFreq); - } condGotoBB.RemoveSucc(*succ1); DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ1, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); @@ -3639,16 +3633,10 @@ void ValueRangePropagation::RemoveUnreachableBB( UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - uint64 removedFreq = 0; - // update frequency before cfg changed if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = condGotoBB.GetSuccFreq()[0]; + condGotoBB.SetSuccFreq(0, condGotoBB.GetSuccFreq()[1]); } condGotoBB.RemoveSucc(*succ0); - if (func.GetCfg()->UpdateCFGFreq()) { - condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); - succ1->SetFrequency(succ1->GetFrequency() + removedFreq); - } DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ0, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } -- Gitee From 802c1155b0a95d542914dac5a0c26c7209fc5dd4 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Mon, 29 Aug 2022 20:03:54 -0700 Subject: [PATCH 13/18] Remove 1 more frequency update in valueRangePropagation --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 17bc877818..e0cbad7e86 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3238,11 +3238,6 @@ void ValueRangePropagation::AnalysisUnreachableBBOrEdge(BB &bb, BB &unreachableB bb.RemoveSucc(unreachableBB); bb.RemoveMeStmt(bb.GetLastMe()); bb.SetKind(kBBFallthru); - if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetSuccFreq(0, bb.GetFrequency()); - succBB.SetFrequency(succBB.GetFrequency() + removedFreq); - unreachableBB.SetFrequency(0); - } auto *loop = loops->GetBBLoopParent(bb.GetBBId()); if (loop == nullptr) { return; -- Gitee From ec7c62fac1c9cb5b08840cc9a8892b01e07897da Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 1 Sep 2022 22:12:32 -0700 Subject: [PATCH 14/18] More adjustments of frequency updates in vrp --- .../maple_me/src/me_value_range_prop.cpp | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index e0cbad7e86..4cc9282db2 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3931,13 +3931,15 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(trueBranch, index); CreateLabelForTargetBB(pred, trueBranch); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(bb.GetFrequency() - edgeFreq); - size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); - int64_t updatedtrueFreq = static_cast(bb.GetSuccFreq()[trueBranchIdx] - edgeFreq); - // transform may not be consistent with frequency value - updatedtrueFreq = updatedtrueFreq > 0 ? updatedtrueFreq : 0; - bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); - pred.AddSuccFreq(edgeFreq, index); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); + int64_t updatedtrueFreq = static_cast(bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); + // transform may not be consistent with frequency value + updatedtrueFreq = updatedtrueFreq > 0 ? updatedtrueFreq : 0; + bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + } + pred.AddSuccFreq(static_cast(edgeFreq), index); } } else { auto *exitCopyFallthru = GetNewCopyFallthruBB(trueBranch, bb); @@ -3954,14 +3956,17 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(*exitCopyFallthru, index); CreateLabelForTargetBB(pred, *exitCopyFallthru); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(bb.GetFrequency() - edgeFreq); - exitCopyFallthru->SetFrequency(edgeFreq); - exitCopyFallthru->PushBackSuccFreq(edgeFreq); - size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); - int64_t updatedtrueFreq = static_cast(bb.GetSuccFreq()[trueBranchIdx] - edgeFreq); - ASSERT(updatedtrueFreq >= 0, "sanity check"); - bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); - pred.AddSuccFreq(edgeFreq, index); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + exitCopyFallthru->SetFrequency(static_cast(edgeFreq)); + exitCopyFallthru->PushBackSuccFreq(static_cast(edgeFreq)); + size_t trueBranchIdx = static_cast(bb.GetSuccIndex(trueBranch)); + int64_t updatedtrueFreq = static_cast( + bb.GetSuccFreq()[trueBranchIdx] - static_cast(edgeFreq)); + ASSERT(updatedtrueFreq >= 0, "sanity check"); + bb.SetSuccFreq(static_cast(trueBranchIdx), static_cast(updatedtrueFreq)); + } + pred.AddSuccFreq(static_cast(edgeFreq), index); } return true; } @@ -3985,11 +3990,13 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( pred.AddSucc(*newBB, index); newBB->AddSucc(trueBranch); if (func.GetCfg()->UpdateCFGFreq()) { - bb.SetFrequency(bb.GetFrequency() - edgeFreq); - bb.UpdateEdgeFreqs(); - newBB->SetFrequency(edgeFreq); - newBB->PushBackSuccFreq(edgeFreq); - pred.AddSuccFreq(edgeFreq, index); + if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { + bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); + bb.UpdateEdgeFreqs(); + newBB->SetFrequency(static_cast(edgeFreq)); + newBB->PushBackSuccFreq(static_cast(edgeFreq)); + } + pred.AddSuccFreq(static_cast(edgeFreq), index); } DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); (void)func.GetOrCreateBBLabel(trueBranch); @@ -5206,7 +5213,6 @@ bool MEValueRangePropagation::PhaseRun(maple::MeFunction &f) { GetAnalysisInfoHook()->ForceRunTransFormPhase(&MELoopCanon::id, f); // update cfg frequency if (f.GetCfg()->UpdateCFGFreq()) { - f.GetCfg()->UpdateEdgeFreqWithBBFreq(); if (f.GetCfg()->DumpIRProfileFile()) { f.GetCfg()->DumpToFile("after-valuerange" + std::to_string(f.vrpRuns), false, true); } -- Gitee From c7c19f17240129c24a3568a42daee12ca631bcc5 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 2 Sep 2022 15:38:07 -0700 Subject: [PATCH 15/18] Undo the change to maple_be/src/be/lower.cpp --- src/mapleall/maple_be/src/be/lower.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index ffce92b12a..bb39897bab 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -4094,10 +4094,6 @@ void CGLowerer::LowerFunc(MIRFunction &func) { CHECK_FATAL(origBody != nullptr, "origBody should not be nullptr"); BlockNode *newBody = LowerBlock(*origBody); - if (newBody->GetLast()->GetOpCode() == OP_call) { - // the call does not return; insert a dummy return to satisfy mplcg - newBody->AddStatement(mirBuilder->CreateStmtReturn(nullptr)); - } func.SetBody(newBody); if (needBranchCleanup) { CleanupBranches(func); -- Gitee From e1d6693b88c4b012a75e3c88888236e2bcb219b8 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Wed, 7 Sep 2022 08:55:28 -0700 Subject: [PATCH 16/18] an adjustment to the change in me_value_range_prop.cpp --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index 4cc9282db2..e17c0c6a0a 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3993,9 +3993,9 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( if ((static_cast(bb.GetFrequency()) - edgeFreq) > 0) { bb.SetFrequency(static_cast(bb.GetFrequency() - edgeFreq)); bb.UpdateEdgeFreqs(); - newBB->SetFrequency(static_cast(edgeFreq)); - newBB->PushBackSuccFreq(static_cast(edgeFreq)); } + newBB->SetFrequency(static_cast(edgeFreq)); + newBB->PushBackSuccFreq(static_cast(edgeFreq)); pred.AddSuccFreq(static_cast(edgeFreq), index); } DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); -- Gitee From 873f8fd2b1a62b2e9dca61cb2f9c067948f5aaa2 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 9 Sep 2022 17:23:37 -0700 Subject: [PATCH 17/18] Do not call UpdateEdgeFreqWithBBFreq() in hdse phase --- src/mapleall/maple_me/src/me_hdse.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mapleall/maple_me/src/me_hdse.cpp b/src/mapleall/maple_me/src/me_hdse.cpp index a12c440fb0..bfd958c20b 100644 --- a/src/mapleall/maple_me/src/me_hdse.cpp +++ b/src/mapleall/maple_me/src/me_hdse.cpp @@ -202,7 +202,6 @@ bool MEHdse::PhaseRun(maple::MeFunction &f) { f.GetCfg()->WontExitAnalysis(); // update frequency if (hdse.UpdateFreq()) { - f.GetCfg()->UpdateEdgeFreqWithBBFreq(); if (f.GetCfg()->DumpIRProfileFile()) { f.GetCfg()->DumpToFile("after-HDSE" + std::to_string(f.hdseRuns), false, true); } -- Gitee From 362618eb2fa7bd4843df89ad14b5fd2abf5a51f7 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 10 Sep 2022 21:07:35 -0700 Subject: [PATCH 18/18] Fixed inlining issue caused by the addition of enclosingBlk field in CallNode --- src/mapleall/maple_me/src/me_value_range_prop.cpp | 7 ++++--- src/mapleall/maple_me/src/pme_emit.cpp | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index e17c0c6a0a..9d958d0296 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3849,9 +3849,10 @@ bool ValueRangePropagation::ChangeTheSuccOfPred2TrueBranch( mergeAllFallthruBBs->PushBackSuccFreq(edgeFreq); pred.AddSuccFreq(edgeFreq, index); // update bb frequency - ASSERT(bb.GetFrequency() >= edgeFreq, "sanity check"); - bb.SetFrequency(bb.GetFrequency() - edgeFreq); - bb.UpdateEdgeFreqs(); + if (bb.GetFrequency() >= edgeFreq) { + bb.SetFrequency(bb.GetFrequency() - edgeFreq); + bb.UpdateEdgeFreqs(); + } } mergeAllFallthruBBs->AddSucc(trueBranch); DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); diff --git a/src/mapleall/maple_me/src/pme_emit.cpp b/src/mapleall/maple_me/src/pme_emit.cpp index 12606c14ec..efd76f295e 100644 --- a/src/mapleall/maple_me/src/pme_emit.cpp +++ b/src/mapleall/maple_me/src/pme_emit.cpp @@ -499,6 +499,7 @@ StmtNode* PreMeEmitter::EmitPreMeStmt(MeStmt *meStmt, BaseNode *parent) { callnode->SetOriginalID(meStmt->GetOriginalId()); callnode->SetMeStmtID(callMeStmt->GetMeStmtId()); preMeStmtExtensionMap[callnode->GetStmtID()] = pmeExt; + callnode->SetEnclosingBlock(static_cast(parent)); return callnode; } case OP_icall: -- Gitee