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_REACHING_H
17 #define MAPLEBE_INCLUDE_CG_REACHING_H
18
19 #include "cg_phase.h"
20 #include "cgbb.h"
21 #include "datainfo.h"
22 #include "maple_phase.h"
23
24 namespace maplebe {
25 enum VisitStatus : uint8 { kNotVisited, kNormalVisited, kEHVisited };
26
27 enum AnalysisType : uint8 { kRDRegAnalysis = 1, kRDMemAnalysis = 2, kRDAllAnalysis = 3 };
28
29 enum DumpType : uint32 {
30 kDumpAll = 0xFFF,
31 kDumpRegGen = 0x001,
32 kDumpRegUse = 0x002,
33 kDumpRegIn = 0x004,
34 kDumpRegOut = 0x008,
35 kDumpMemGen = 0x010,
36 kDumpMemIn = 0x020,
37 kDumpMemOut = 0x040,
38 kDumpMemUse = 0x080,
39 kDumpBBCGIR = 0x100
40 };
41
42 class ReachingDefinition : public AnalysisResult {
43 public:
44 ReachingDefinition(CGFunc &func, MemPool &memPool);
45 ~ReachingDefinition() override = default;
46 void AnalysisStart();
47 void Dump(uint32 flag) const;
48 void DumpInfo(const BB &bb, DumpType flag) const;
49 void DumpBBCGIR(const BB &bb) const;
50 void ClearDefUseInfo();
51 void UpdateInOut(BB &changedBB);
52 void UpdateInOut(BB &changedBB, bool isReg);
SetAnalysisMode(AnalysisType analysisMode)53 void SetAnalysisMode(AnalysisType analysisMode)
54 {
55 mode = analysisMode;
56 }
57
OnlyAnalysisReg()58 bool OnlyAnalysisReg() const
59 {
60 return mode == kRDRegAnalysis;
61 }
62
GetMaxInsnNO()63 uint32 GetMaxInsnNO() const
64 {
65 return maxInsnNO;
66 }
67
GetRegSize(const BB & bb)68 size_t GetRegSize(const BB &bb) const
69 {
70 return regUse[bb.GetId()]->Size();
71 }
72
CheckRegGen(const BB & bb,uint32 regNO)73 bool CheckRegGen(const BB &bb, uint32 regNO) const
74 {
75 return regGen[bb.GetId()]->TestBit(regNO);
76 }
77
78 void EnlargeRegCapacity(uint32 size);
79 bool IsFrameReg(const Operand &opnd) const;
80 InsnSet FindUseForRegOpnd(Insn &insn, uint32 indexOrRegNO, bool isRegNO) const;
81 bool RegIsUsedIncaller(uint32 regNO, Insn &startInsn, Insn &endInsn) const;
82 bool CheckRegLiveinReturnBB(uint32 regNO, const BB &bb) const;
83 bool RegIsLiveBetweenInsn(uint32 regNO, Insn &startInsn, Insn &endInsn, bool isBack = false,
84 bool isFirstNo = false) const;
85 bool RegIsUsedOrDefBetweenInsn(uint32 regNO, Insn &startInsn, Insn &endInsn) const;
86 bool IsLiveInAllPathBB(uint32 regNO, const BB &startBB, const BB &endBB, std::vector<bool> &visitedBB,
87 bool isFirstNo = false) const;
88 bool IsUseOrDefInAllPathBB(uint32 regNO, const BB &startBB, const BB &endBB, std::vector<bool> &visitedBB) const;
89 bool IsUseOrDefBetweenInsn(uint32 regNO, const BB &curBB, const Insn &startInsn, Insn &endInsn) const;
90 bool HasCallBetweenDefUse(const Insn &defInsn, const Insn &useInsn) const;
91 std::vector<Insn *> FindRegDefBetweenInsn(uint32 regNO, Insn *startInsn, Insn *endInsn, bool findAll = false,
92 bool analysisDone = true) const;
93 virtual void InitGenUse(BB &bb, bool firstTime = true) = 0;
94 virtual InsnSet FindDefForMemOpnd(Insn &insn, uint32 indexOrOffset, bool isOffset = false) const = 0;
95 virtual InsnSet FindUseForMemOpnd(Insn &insn, uint8 index, bool secondMem = false) const = 0;
96 virtual std::vector<Insn *> FindMemDefBetweenInsn(uint32 offset, const Insn *startInsn, Insn *endInsn) const = 0;
97 virtual std::vector<Insn *> FindRegDefBetweenInsnGlobal(uint32 regNO, Insn *startInsn, Insn *endInsn) const = 0;
98 virtual bool FindRegUseBetweenInsn(uint32 regNO, Insn *startInsn, Insn *endInsn, InsnSet &useInsnSet) const = 0;
99 virtual bool FindRegUseBetweenInsnGlobal(uint32 regNO, Insn *startInsn, Insn *endInsn, BB *movBB) const = 0;
100 virtual bool FindMemUseBetweenInsn(uint32 offset, Insn *startInsn, const Insn *endInsn,
101 InsnSet &useInsnSet) const = 0;
102 virtual InsnSet FindDefForRegOpnd(Insn &insn, uint32 indexOrRegNO, bool isRegNO = false) const = 0;
103
104 static constexpr int32 kWordByteNum = 4;
105 static constexpr int32 kDoubleWordByteNum = 8;
106 /* to save storage space, the offset of stack memory is devided by 4 and then saved in DataInfo */
107 static constexpr int32 kMemZoomSize = 4;
108 /* number the insn interval 3. make sure no repeated insn number when new insn inserted */
109 static constexpr uint32 kInsnNoInterval = 3;
110 bool HasCallBetweenInsnInSameBB(const Insn &startInsn, const Insn &endInsn) const;
111 virtual bool KilledByCallBetweenInsnInSameBB(const Insn &startInsn, const Insn &endInsn, regno_t regNO) const = 0;
112
113 protected:
114 virtual void InitStartGen() = 0;
115 virtual void GenAllAsmDefRegs(BB &bb, Insn &insn, uint32 index) = 0;
116 virtual void GenAllAsmUseRegs(BB &bb, Insn &insn, uint32 index) = 0;
117 virtual void GenAllCallerSavedRegs(BB &bb, Insn &insn) = 0;
118 virtual void AddRetPseudoInsn(BB &bb) = 0;
119 virtual void AddRetPseudoInsns() = 0;
120 virtual int32 GetStackSize() const = 0;
121 virtual bool IsCallerSavedReg(uint32 regNO) const = 0;
122 virtual void FindRegDefInBB(uint32 regNO, BB &bb, InsnSet &defInsnSet) const = 0;
123 virtual void FindMemDefInBB(uint32 offset, BB &bb, InsnSet &defInsnSet) const = 0;
124 virtual bool IsRegKilledByCallInsn(const Insn &insn, regno_t regNO) const = 0;
125 virtual void DFSFindDefForRegOpnd(const BB &startBB, uint32 regNO, std::vector<VisitStatus> &visitedBB,
126 InsnSet &defInsnSet) const = 0;
127 virtual void DFSFindDefForMemOpnd(const BB &startBB, uint32 offset, std::vector<VisitStatus> &visitedBB,
128 InsnSet &defInsnSet) const = 0;
129 void DFSFindUseForMemOpnd(const BB &startBB, uint32 offset, std::vector<bool> &visitedBB, InsnSet &useInsnSet,
130 bool onlyFindForEhSucc) const;
131
132 CGFunc *cgFunc;
133 MapleAllocator rdAlloc;
134 StackMemPool &stackMp;
135 MapleVector<Insn *> pseudoInsns;
136 AnalysisType mode = kRDRegAnalysis;
137 BB *firstCleanUpBB = nullptr;
138 std::vector<DataInfo *> regGen;
139 std::vector<DataInfo *> regUse;
140 std::vector<DataInfo *> regIn;
141 std::vector<DataInfo *> regOut;
142 std::vector<DataInfo *> memGen;
143 std::vector<DataInfo *> memUse;
144 std::vector<DataInfo *> memIn;
145 std::vector<DataInfo *> memOut;
146 const uint32 kMaxBBNum;
147
148 private:
149 void Initialize();
150 void InitDataSize();
151 void BuildInOutForFuncBody();
152 void BuildInOutForCleanUpBB();
153 void BuildInOutForCleanUpBB(bool isReg, const std::set<uint32> &index);
154 void InitRegAndMemInfo(const BB &bb);
155 void InitOut(const BB &bb);
156 bool GenerateIn(const BB &bb);
157 bool GenerateIn(const BB &bb, const std::set<uint32> &infoIndex, const bool isReg);
158 bool GenerateOut(const BB &bb);
159 bool GenerateOut(const BB &bb, const std::set<uint32> &infoIndex, const bool isReg);
160 bool GenerateInForFirstCleanUpBB();
161 bool GenerateInForFirstCleanUpBB(bool isReg, const std::set<uint32> &infoIndex);
162 void DFSFindUseForRegOpnd(const BB &startBB, uint32 regNO, std::vector<bool> &visitedBB, InsnSet &useInsnSet,
163 bool onlyFindForEhSucc) const;
164 bool RegIsUsedInOtherBB(const BB &startBB, uint32 regNO, std::vector<bool> &visitedBB) const;
165 bool RegHasUsePoint(uint32 regNO, Insn ®DefInsn) const;
166 bool CanReachEndBBFromCurrentBB(const BB ¤tBB, const BB &endBB, std::vector<bool> &traversedBBSet) const;
167
168 bool HasCallInPath(const BB &startBB, const BB &endBB, std::vector<bool> &visitedBB) const;
169 bool RegIsUsedInCleanUpBB(uint32 regNO) const;
170
171 MapleSet<BB *, BBIdCmp> normalBBSet;
172 MapleSet<BB *, BBIdCmp> cleanUpBBSet;
173 uint32 maxInsnNO = 0;
174 };
175
MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgReachingDefinition,maplebe::CGFunc)176 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgReachingDefinition, maplebe::CGFunc)
177 ReachingDefinition *GetResult()
178 {
179 return reachingDef;
180 }
181 ReachingDefinition *reachingDef = nullptr;
182 MAPLE_FUNC_PHASE_DECLARE_END
183 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgClearRDInfo, maplebe::CGFunc)
184 MAPLE_FUNC_PHASE_DECLARE_END
185 } /* namespace maplebe */
186
187 #endif /* MAPLEBE_INCLUDE_CG_REACHING_H */
188