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 "cgfunc.h"
17 #if TARGAARCH64
18 #include "aarch64_regsaves.h"
19 #elif TARGRISCV64
20 #include "riscv64_regsaves.h"
21 #endif
22
23 namespace maplebe {
24 using namespace maple;
25
PhaseRun(maplebe::CGFunc & f)26 bool CgRegSavesOpt::PhaseRun(maplebe::CGFunc &f)
27 {
28 if (Globals::GetInstance()->GetOptimLevel() <= CGOptions::kLevel1) {
29 return false;
30 }
31
32 /* Perform loop analysis, result to be obtained in CGFunc */
33 (void)GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgLoopAnalysis::id, f);
34
35 /* Perform live analysis, result to be obtained in CGFunc */
36 LiveAnalysis *live = nullptr;
37 MaplePhase *it =
38 GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgLiveAnalysis::id, f);
39 live = static_cast<CgLiveAnalysis *>(it)->GetResult();
40 CHECK_FATAL(live != nullptr, "null ptr check");
41 /* revert liveanalysis result container. */
42 live->ResetLiveSet();
43
44 /* Perform dom analysis, result to be inserted into AArch64RegSavesOpt object */
45 DomAnalysis *dom = nullptr;
46 PostDomAnalysis *pdom = nullptr;
47 if (Globals::GetInstance()->GetOptimLevel() >= CGOptions::kLevel1 &&
48 f.GetCG()->GetCGOptions().DoColoringBasedRegisterAllocation()) {
49 MaplePhase *phase =
50 GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgDomAnalysis::id, f);
51 dom = static_cast<CgDomAnalysis *>(phase)->GetResult();
52 CHECK_FATAL(dom != nullptr, "null ptr check");
53 phase =
54 GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgPostDomAnalysis::id, f);
55 pdom = static_cast<CgPostDomAnalysis *>(phase)->GetResult();
56 CHECK_FATAL(pdom != nullptr, "null ptr check");
57 }
58
59 MemPool *memPool = GetPhaseMemPool();
60 RegSavesOpt *regSavesOpt = nullptr;
61 #if TARGAARCH64
62 regSavesOpt = memPool->New<AArch64RegSavesOpt>(f, *memPool, *dom, *pdom);
63 #elif || TARGRISCV64
64 regSavesOpt = memPool->New<Riscv64RegSavesOpt>(f, *memPool);
65 #endif
66
67 if (regSavesOpt) {
68 regSavesOpt->SetEnabledDebug(false); /* To turn on debug trace */
69 if (regSavesOpt->GetEnabledDebug()) {
70 dom->Dump();
71 }
72 regSavesOpt->Run();
73 }
74 return true;
75 }
76 MAPLE_TRANSFORM_PHASE_REGISTER(CgRegSavesOpt, regsaves)
77 } /* namespace maplebe */
78