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_coalesce.h"
17 #include "cg_option.h"
18 #ifdef TARGAARCH64
19 #include "aarch64_reg_coalesce.h"
20 #include "aarch64_isa.h"
21 #include "aarch64_insn.h"
22 #endif
23 #include "cg.h"
24
25 /*
26 * This phase implements if-conversion optimization,
27 * which tries to convert conditional branches into cset/csel instructions
28 */
29 namespace maplebe {
30
Run()31 void LiveIntervalAnalysis::Run()
32 {
33 Analysis();
34 CoalesceRegisters();
35 ClearBFS();
36 }
37
DoAnalysis()38 void LiveIntervalAnalysis::DoAnalysis()
39 {
40 runAnalysis = true;
41 Analysis();
42 }
43
Analysis()44 void LiveIntervalAnalysis::Analysis()
45 {
46 bfs = memPool->New<Bfs>(*cgFunc, *memPool);
47 bfs->ComputeBlockOrder();
48 }
49
50 /* bfs is not utilized outside the function. */
ClearBFS()51 void LiveIntervalAnalysis::ClearBFS()
52 {
53 bfs = nullptr;
54 }
55
Dump()56 void LiveIntervalAnalysis::Dump()
57 {
58 for (auto it : vregIntervals) {
59 LiveInterval *li = it.second;
60 li->Dump();
61 li->DumpDefs();
62 li->DumpUses();
63 }
64 }
65
CoalesceLiveIntervals(LiveInterval & lrDest,LiveInterval & lrSrc)66 void LiveIntervalAnalysis::CoalesceLiveIntervals(LiveInterval &lrDest, LiveInterval &lrSrc)
67 {
68 if (cgFunc->IsExtendReg(lrDest.GetRegNO())) {
69 cgFunc->InsertExtendSet(lrSrc.GetRegNO());
70 }
71 cgFunc->RemoveFromExtendSet(lrDest.GetRegNO());
72 /* merge destlr to srclr */
73 lrSrc.MergeRanges(lrDest);
74 /* update conflicts */
75 lrSrc.MergeConflict(lrDest);
76 for (auto reg : lrDest.GetConflict()) {
77 LiveInterval *conf = GetLiveInterval(reg);
78 if (conf) {
79 conf->AddConflict(lrSrc.GetRegNO());
80 }
81 }
82 /* merge refpoints */
83 lrSrc.MergeRefPoints(lrDest);
84 vregIntervals.erase(lrDest.GetRegNO());
85 }
86
PhaseRun(maplebe::CGFunc & f)87 bool CGliveIntervalAnalysis::PhaseRun(maplebe::CGFunc &f)
88 {
89 LiveAnalysis *live = GET_ANALYSIS(CgLiveAnalysis, f);
90 live->ResetLiveSet();
91 MemPool *memPool = GetPhaseMemPool();
92 liveInterval = f.GetCG()->CreateLLAnalysis(*memPool, f);
93 liveInterval->DoAnalysis();
94 return false;
95 }
GetAnalysisDependence(AnalysisDep & aDep) const96 void CGliveIntervalAnalysis::GetAnalysisDependence(AnalysisDep &aDep) const
97 {
98 aDep.AddRequired<CgLiveAnalysis>();
99 aDep.AddRequired<CgLoopAnalysis>();
100 }
MAPLE_ANALYSIS_PHASE_REGISTER_CANSKIP(CGliveIntervalAnalysis,cgliveintervalananlysis)101 MAPLE_ANALYSIS_PHASE_REGISTER_CANSKIP(CGliveIntervalAnalysis, cgliveintervalananlysis)
102
103 bool CgRegCoalesce::PhaseRun(maplebe::CGFunc &f)
104 {
105 LiveAnalysis *live = GET_ANALYSIS(CgLiveAnalysis, f);
106 DEBUG_ASSERT(live != nullptr, "nullptr check");
107 live->ResetLiveSet();
108 MemPool *memPool = GetPhaseMemPool();
109 LiveIntervalAnalysis *ll = f.GetCG()->CreateLLAnalysis(*memPool, f);
110 ll->Run();
111 /* the live range info may changed, so invalid the info. */
112 if (live != nullptr) {
113 live->ClearInOutDataInfo();
114 }
115 return false;
116 }
117
GetAnalysisDependence(maple::AnalysisDep & aDep) const118 void CgRegCoalesce::GetAnalysisDependence(maple::AnalysisDep &aDep) const
119 {
120 aDep.AddRequired<CgLiveAnalysis>();
121 aDep.AddRequired<CgLoopAnalysis>();
122 aDep.PreservedAllExcept<CgLiveAnalysis>();
123 }
124 MAPLE_TRANSFORM_PHASE_REGISTER(CgRegCoalesce, cgregcoalesce)
125
126 } /* namespace maplebe */
127