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 "reg_alloc.h"
17 #include "live.h"
18 #include "loop.h"
19 #include "cg_dominance.h"
20 #include "mir_lower.h"
21 #include "securec.h"
22 #include "reg_alloc_basic.h"
23 #include "reg_alloc_lsra.h"
24 #include "cg.h"
25 #if TARGAARCH64
26 #include "aarch64_color_ra.h"
27 #endif
28
29 namespace maplebe {
30 #define RA_DUMP (CG_DEBUG_FUNC(f))
31
GetAnalysisDependence(AnalysisDep & aDep) const32 void CgRegAlloc::GetAnalysisDependence(AnalysisDep &aDep) const
33 {
34 if (Globals::GetInstance()->GetOptimLevel() == CGOptions::kLevel0) {
35 return;
36 }
37
38 if (CGOptions::GetInstance().DoLinearScanRegisterAllocation()) {
39 aDep.AddRequired<CgBBSort>();
40 } else if (CGOptions::GetInstance().DoColoringBasedRegisterAllocation()) {
41 aDep.AddRequired<CgDomAnalysis>();
42 }
43
44 aDep.AddRequired<CgLoopAnalysis>();
45 aDep.AddRequired<CgLiveAnalysis>();
46 aDep.PreservedAllExcept<CgLiveAnalysis>();
47 }
48
PhaseRun(maplebe::CGFunc & f)49 bool CgRegAlloc::PhaseRun(maplebe::CGFunc &f)
50 {
51 bool success = false;
52 while (!success) {
53 MemPool *phaseMp = GetPhaseMemPool();
54 LiveAnalysis *live = nullptr;
55
56 /* create register allocator */
57 RegAllocator *regAllocator = nullptr;
58 MemPool *tempMP = memPoolCtrler.NewMemPool("regalloc", true);
59 if (Globals::GetInstance()->GetOptimLevel() == CGOptions::kLevel0) {
60 regAllocator = phaseMp->New<DefaultO0RegAllocator>(f, *phaseMp);
61 } else if (f.GetCG()->GetCGOptions().DoLinearScanRegisterAllocation()) {
62 Bfs *bfs = GET_ANALYSIS(CgBBSort, f);
63 CHECK_FATAL(bfs != nullptr, "null ptr check");
64 live = GET_ANALYSIS(CgLiveAnalysis, f);
65 CHECK_FATAL(live != nullptr, "null ptr check");
66 LoopAnalysis *loop = GET_ANALYSIS(CgLoopAnalysis, f);
67 CHECK_FATAL(loop != nullptr, "null ptr check");
68 regAllocator = phaseMp->New<LSRALinearScanRegAllocator>(f, *phaseMp, bfs, *loop);
69 #if TARGAARCH64
70 } else if (f.GetCG()->GetCGOptions().DoColoringBasedRegisterAllocation()) {
71 if (Triple::GetTriple().GetArch() == Triple::ArchType::aarch64) {
72 MaplePhase *it = GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(
73 &CgLiveAnalysis::id, f);
74 live = static_cast<CgLiveAnalysis*>(it)->GetResult();
75 CHECK_FATAL(live != nullptr, "null ptr check");
76 DomAnalysis *dom = GET_ANALYSIS(CgDomAnalysis, f);
77 CHECK_FATAL(dom != nullptr, "null ptr check");
78 LoopAnalysis *loop = GET_ANALYSIS(CgLoopAnalysis, f);
79 CHECK_FATAL(loop != nullptr, "null ptr check");
80 regAllocator = phaseMp->New<GraphColorRegAllocator>(f, *tempMP, *dom, *loop);
81 }
82 #endif
83 } else {
84 maple::LogInfo::MapleLogger(kLlErr)
85 << "Warning: We only support Linear Scan and GraphColor register allocation\n";
86 }
87 RA_TIMER_REGISTER(ra, "RA Time");
88 /* do register allocation */
89 CHECK_FATAL(regAllocator != nullptr, "regAllocator is null in CgDoRegAlloc::Run");
90 regAllocator->SetNeedDump(RA_DUMP);
91 f.SetIsAfterRegAlloc();
92 success = regAllocator->AllocateRegisters();
93
94 if (Globals::GetInstance()->GetOptimLevel() > CGOptions::kLevel0) {
95 GetAnalysisInfoHook()->ForceEraseAnalysisPhase(f.GetUniqueID(), &CgLiveAnalysis::id);
96 }
97 memPoolCtrler.DeleteMemPool(tempMP);
98 }
99 RA_TIMER_PRINT(f.GetName());
100 return false;
101 }
102 } /* namespace maplebe */
103