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 ResetLiveSet();
43 void GetBBDefUse(BB &bb) const;
44 void ProcessAsmListOpnd(BB &bb, Operand &opnd, uint32 idx) const;
45 void ProcessListOpnd(BB &bb, Operand &opnd, bool isDef) const;
46 void ProcessMemOpnd(BB &bb, Operand &opnd) const;
47 void ProcessCondOpnd(BB &bb) const;
48 void CollectLiveInfo(BB &bb, const Operand &opnd, bool isDef, bool isUse) const;
49 void MarkStackMapInsn(Insn &insn, BB &bb) const;
50 void GenerateStackMapLiveIn();
51 SparseDataInfo *GenerateLiveInByDefUse(SparseDataInfo &liveOut, SparseDataInfo &use, SparseDataInfo &def);
52
NewLiveIn(uint32 maxRegCount)53 SparseDataInfo *NewLiveIn(uint32 maxRegCount) const
54 {
55 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
56 }
57
NewLiveOut(uint32 maxRegCount)58 SparseDataInfo *NewLiveOut(uint32 maxRegCount) const
59 {
60 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
61 }
62
NewDef(uint32 maxRegCount)63 SparseDataInfo *NewDef(uint32 maxRegCount) const
64 {
65 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
66 }
67
NewDef(const SparseDataInfo & def)68 SparseDataInfo *NewDef(const SparseDataInfo &def) const
69 {
70 return memPool->New<SparseDataInfo>(def, alloc);
71 }
72
NewUse(uint32 maxRegCount)73 SparseDataInfo *NewUse(uint32 maxRegCount) const
74 {
75 return memPool->New<SparseDataInfo>(maxRegCount, alloc);
76 }
77
NewUse(const SparseDataInfo & use)78 SparseDataInfo *NewUse(const SparseDataInfo &use) const
79 {
80 return memPool->New<SparseDataInfo>(use, alloc);
81 }
82
83 virtual void GenerateReturnBBDefUse(BB &bb) const = 0;
84 virtual void ProcessCallInsnParam(BB &bb, const Insn &insn) const = 0;
85 protected:
86 int iteration = 0;
87 CGFunc *cgFunc;
88 MapleAllocator alloc;
89 StackMemPool &stackMp;
90
91 private:
92 bool RemovePhiLiveInFromSuccNotFromThisBB(BB &curBB, BB &succBB) const;
93 };
94
MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgLiveAnalysis,maplebe::CGFunc)95 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgLiveAnalysis, maplebe::CGFunc)
96 LiveAnalysis *GetResult()
97 {
98 return live;
99 }
100 LiveAnalysis *live = nullptr;
101 OVERRIDE_DEPENDENCE
102 MAPLE_FUNC_PHASE_DECLARE_END
103 } /* namespace maplebe */
104 #endif /* MAPLEBE_INCLUDE_CG_LIVE_H */
105