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 "reg_alloc_lsra.h"
18 #include "cg.h"
19
20 namespace maplebe {
21 #define RA_DUMP (CG_DEBUG_FUNC(f))
22
GetAnalysisDependence(AnalysisDep & aDep) const23 void CgRegAlloc::GetAnalysisDependence(AnalysisDep &aDep) const
24 {
25 if (Globals::GetInstance()->GetOptimLevel() == CGOptions::kLevel0) {
26 return;
27 }
28
29 if (CGOptions::GetInstance().DoLinearScanRegisterAllocation()) {
30 aDep.AddRequired<CgBBSort>();
31 }
32 aDep.AddRequired<CgLoopAnalysis>();
33 aDep.AddRequired<CgLiveAnalysis>();
34 aDep.PreservedAllExcept<CgLiveAnalysis>();
35 }
36
PhaseRun(maplebe::CGFunc & f)37 bool CgRegAlloc::PhaseRun(maplebe::CGFunc &f)
38 {
39 bool success = false;
40 while (!success) {
41 MemPool *phaseMp = GetPhaseMemPool();
42 LiveAnalysis *live = nullptr;
43
44 /* create register allocator */
45 RegAllocator *regAllocator = nullptr;
46 MemPool *tempMP = memPoolCtrler.NewMemPool("regalloc", true);
47 if (f.GetCG()->GetCGOptions().DoLinearScanRegisterAllocation()) {
48 Bfs *bfs = GET_ANALYSIS(CgBBSort, f);
49 CHECK_FATAL(bfs != nullptr, "null ptr check");
50 live = GET_ANALYSIS(CgLiveAnalysis, f);
51 CHECK_FATAL(live != nullptr, "null ptr check");
52 LoopAnalysis *loop = GET_ANALYSIS(CgLoopAnalysis, f);
53 CHECK_FATAL(loop != nullptr, "null ptr check");
54 regAllocator = phaseMp->New<LSRALinearScanRegAllocator>(f, *phaseMp, bfs, *loop);
55 } else {
56 maple::LogInfo::MapleLogger(kLlErr)
57 << "Warning: We only support Linear Scan and GraphColor register allocation\n";
58 }
59 RA_TIMER_REGISTER(ra, "RA Time");
60 /* do register allocation */
61 CHECK_FATAL(regAllocator != nullptr, "regAllocator is null in CgDoRegAlloc::Run");
62 regAllocator->SetNeedDump(RA_DUMP);
63 f.SetIsAfterRegAlloc();
64 success = regAllocator->AllocateRegisters();
65
66 if (Globals::GetInstance()->GetOptimLevel() > CGOptions::kLevel0) {
67 GetAnalysisInfoHook()->ForceEraseAnalysisPhase(f.GetUniqueID(), &CgLiveAnalysis::id);
68 }
69 memPoolCtrler.DeleteMemPool(tempMP);
70 }
71 RA_TIMER_PRINT(f.GetName());
72 return false;
73 }
74 } /* namespace maplebe */
75