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 LoopAnalysis *loop = nullptr;
48 if (Globals::GetInstance()->GetOptimLevel() >= CGOptions::kLevel1 &&
49 f.GetCG()->GetCGOptions().DoColoringBasedRegisterAllocation()) {
50 MaplePhase *phase =
51 GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgDomAnalysis::id, f);
52 dom = static_cast<CgDomAnalysis *>(phase)->GetResult();
53 CHECK_FATAL(dom != nullptr, "null ptr check");
54 phase =
55 GetAnalysisInfoHook()->ForceRunAnalysisPhase<MapleFunctionPhase<CGFunc>, CGFunc>(&CgPostDomAnalysis::id, f);
56 pdom = static_cast<CgPostDomAnalysis *>(phase)->GetResult();
57 CHECK_FATAL(pdom != nullptr, "null ptr check");
58 loop = static_cast<CgLoopAnalysis*>(phase)->GetResult();
59 CHECK_FATAL(loop != nullptr, "null ptr check");
60 }
61
62 MemPool *memPool = GetPhaseMemPool();
63 RegSavesOpt *regSavesOpt = nullptr;
64 #if TARGAARCH64
65 CHECK_FATAL(dom != nullptr, "nullptr check");
66 regSavesOpt = memPool->New<AArch64RegSavesOpt>(f, *memPool, *dom, *pdom, *loop);
67 #elif || TARGRISCV64
68 regSavesOpt = memPool->New<Riscv64RegSavesOpt>(f, *memPool);
69 #endif
70
71 if (regSavesOpt) {
72 regSavesOpt->SetEnabledDebug(false); /* To turn on debug trace */
73 if (regSavesOpt->GetEnabledDebug()) {
74 dom->Dump();
75 }
76 regSavesOpt->Run();
77 }
78 return true;
79 }
80 MAPLE_TRANSFORM_PHASE_REGISTER(CgRegSavesOpt, regsaves)
81 } /* namespace maplebe */
82