1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "cg.h"
17 #include "cg_critical_edge.h"
18 #include "cg_ssa.h"
19
20 namespace maplebe {
SplitCriticalEdges()21 void CriticalEdge::SplitCriticalEdges()
22 {
23 for (auto it = criticalEdges.begin(); it != criticalEdges.end(); ++it) {
24 cgFunc->GetTheCFG()->BreakCriticalEdge(*((*it).first), *((*it).second));
25 }
26 }
27
CollectCriticalEdges()28 void CriticalEdge::CollectCriticalEdges()
29 {
30 constexpr int multiPredsNum = 2;
31 FOR_ALL_BB(bb, cgFunc) {
32 const auto &preds = bb->GetPreds();
33 if (preds.size() < multiPredsNum) {
34 continue;
35 }
36 // current BB is a merge
37 for (BB *pred : preds) {
38 if (pred->GetKind() == BB::kBBGoto || pred->GetKind() == BB::kBBIgoto) {
39 continue;
40 }
41 if (pred->GetSuccs().size() > 1) {
42 // pred has more than one succ
43 criticalEdges.push_back(std::make_pair(pred, bb));
44 }
45 }
46 }
47 }
48
PhaseRun(maplebe::CGFunc & f)49 bool CgCriticalEdge::PhaseRun(maplebe::CGFunc &f)
50 {
51 if (Globals::GetInstance()->GetOptimLevel() >= CGOptions::kLevel2 && f.NumBBs() < kBBLimit) {
52 MemPool *memPool = GetPhaseMemPool();
53 CriticalEdge *split = memPool->New<CriticalEdge>(f, *memPool);
54 f.GetTheCFG()->InitInsnVisitor(f);
55 split->CollectCriticalEdges();
56 split->SplitCriticalEdges();
57 }
58 return false;
59 }
60
GetAnalysisDependence(maple::AnalysisDep & aDep) const61 void CgCriticalEdge::GetAnalysisDependence(maple::AnalysisDep &aDep) const
62 {
63 aDep.AddPreserved<CgSSAConstruct>();
64 }
65 MAPLE_TRANSFORM_PHASE_REGISTER_CANSKIP(CgCriticalEdge, cgsplitcriticaledge)
66 } /* namespace maplebe */
67