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 #ifndef MAPLEBE_INCLUDE_CG_LIVE_H
17 #define MAPLEBE_INCLUDE_CG_LIVE_H
18
19 #include "cg_phase.h"
20 #include "insn.h"
21 #include "cgbb.h"
22 #include "sparse_datainfo.h"
23 #include "cgfunc.h"
24
25 namespace maplebe {
26 class LiveAnalysis : public AnalysisResult {
27 public:
LiveAnalysis(CGFunc & func,MemPool & memPool)28 LiveAnalysis(CGFunc &func, MemPool &memPool)
29 : AnalysisResult(&memPool), cgFunc(&func), alloc(&memPool), stackMp(func.GetStackMemPool())
30 {
31 }
32 ~LiveAnalysis() override = default;
33
34 void AnalysisLive();
35 void Dump() const;
36 void DumpInfo(const SparseDataInfo &info) const;
37 void InitBB(BB &bb);
38 void InitAndGetDefUse();
39 bool GenerateLiveOut(BB &bb) const;
40 bool GenerateLiveIn(BB &bb);
41 void BuildInOutforFunc();
42 void DealWithInOutOfCleanupBB();
43 void InsertInOutOfCleanupBB();
44 void ResetLiveSet();
45 void ClearInOutDataInfo();
46 void GetBBDefUse(BB &bb) const;
47 void ProcessAsmListOpnd(BB &bb, Operand &opnd, uint32 idx) const;
48 void ProcessListOpnd(BB &bb, Operand &opnd, bool isDef) const;
49 void ProcessMemOpnd(BB &bb, Operand &opnd) const;
50 void ProcessCondOpnd(BB &bb) const;
51 void CollectLiveInfo(BB &bb, const Operand &opnd, bool isDef, bool isUse) const;
52 void MarkStackMapInsn(Insn &insn, BB &bb) const;
53 void GenerateStackMapLiveIn();
54 SparseDataInfo *GenerateLiveInByDefUse(SparseDataInfo &liveOut, SparseDataInfo &use, SparseDataInfo &def);
55
NewLiveIn(uint32 maxRegCount)56 SparseDataInfo *NewLiveIn(uint32 maxRegCount) const
57 {
58 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
59 }
60
NewLiveOut(uint32 maxRegCount)61 SparseDataInfo *NewLiveOut(uint32 maxRegCount) const
62 {
63 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
64 }
65
NewDef(uint32 maxRegCount)66 SparseDataInfo *NewDef(uint32 maxRegCount) const
67 {
68 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
69 }
70
NewDef(const SparseDataInfo & def)71 SparseDataInfo *NewDef(const SparseDataInfo &def) const
72 {
73 return memPool->New<SparseDataInfo>(def, alloc);
74 }
75
NewUse(uint32 maxRegCount)76 SparseDataInfo *NewUse(uint32 maxRegCount) const
77 {
78 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
79 }
80
NewUse(const SparseDataInfo & use)81 SparseDataInfo *NewUse(const SparseDataInfo &use) const
82 {
83 return memPool->New<SparseDataInfo>(use, alloc);
84 }
85
86 virtual void GenerateReturnBBDefUse(BB &bb) const = 0;
87 virtual void ProcessCallInsnParam(BB &bb, const Insn &insn) const = 0;
88 virtual bool CleanupBBIgnoreReg(regno_t reg) = 0;
89
90 protected:
91 int iteration = 0;
92 CGFunc *cgFunc;
93 MapleAllocator alloc;
94 StackMemPool &stackMp;
95
96 private:
97 bool RemovePhiLiveInFromSuccNotFromThisBB(BB &curBB, BB &succBB) const;
98 };
99
MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgLiveAnalysis,maplebe::CGFunc)100 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgLiveAnalysis, maplebe::CGFunc)
101 LiveAnalysis *GetResult()
102 {
103 return live;
104 }
105 LiveAnalysis *live = nullptr;
106 OVERRIDE_DEPENDENCE
107 MAPLE_FUNC_PHASE_DECLARE_END
108 } /* namespace maplebe */
109 #endif /* MAPLEBE_INCLUDE_CG_LIVE_H */
110