• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PEEP_H
17 #define MAPLEBE_INCLUDE_CG_PEEP_H
18 
19 #include "cg.h"
20 #include "optimize_common.h"
21 
22 namespace maplebe {
23 enum ReturnType : uint8 { kResUseFirst, kResDefFirst, kResNotFind };
24 
25 class PeepOptimizeManager {
26 public:
27     /* normal constructor */
PeepOptimizeManager(CGFunc & f,BB & bb,Insn & insn)28     PeepOptimizeManager(CGFunc &f, BB &bb, Insn &insn) : cgFunc(&f), currBB(&bb), currInsn(&insn), ssaInfo(nullptr) {}
29     /* constructor for ssa */
PeepOptimizeManager(CGFunc & f,BB & bb,Insn & insn,CGSSAInfo & info)30     PeepOptimizeManager(CGFunc &f, BB &bb, Insn &insn, CGSSAInfo &info)
31         : cgFunc(&f), currBB(&bb), currInsn(&insn), ssaInfo(&info)
32     {
33     }
34     ~PeepOptimizeManager() = default;
35     template <typename OptimizePattern>
36     void Optimize(bool patternEnable = false)
37     {
38         if (!patternEnable) {
39             return;
40         }
41         OptimizePattern optPattern(*cgFunc, *currBB, *currInsn, *ssaInfo);
42         optPattern.Run(*currBB, *currInsn);
43         optSuccess = optPattern.GetPatternRes();
44         if (optSuccess && optPattern.GetCurrInsn() != nullptr) {
45             currInsn = optPattern.GetCurrInsn();
46         }
47     }
48     template <typename OptimizePattern>
49     void NormalPatternOpt(bool patternEnable = false)
50     {
51         if (!patternEnable) {
52             return;
53         }
54         OptimizePattern optPattern(*cgFunc, *currBB, *currInsn);
55         optPattern.Run(*currBB, *currInsn);
56     }
OptSuccess()57     bool OptSuccess() const
58     {
59         return optSuccess;
60     }
61 
62 private:
63     CGFunc *cgFunc;
64     BB *currBB;
65     Insn *currInsn;
66     CGSSAInfo *ssaInfo;
67     bool optSuccess = false;
68 };
69 
70 class CGPeepHole {
71 public:
72     /* normal constructor */
CGPeepHole(CGFunc & f,MemPool * memPool)73     CGPeepHole(CGFunc &f, MemPool *memPool) : cgFunc(&f), peepMemPool(memPool), ssaInfo(nullptr) {}
74     /* constructor for ssa */
CGPeepHole(CGFunc & f,MemPool * memPool,CGSSAInfo * cgssaInfo)75     CGPeepHole(CGFunc &f, MemPool *memPool, CGSSAInfo *cgssaInfo) : cgFunc(&f), peepMemPool(memPool), ssaInfo(cgssaInfo)
76     {
77     }
78     ~CGPeepHole() = default;
79 
80     virtual void Run() = 0;
81     virtual bool DoSSAOptimize(BB &bb, Insn &insn) = 0;
82     virtual void DoNormalOptimize(BB &bb, Insn &insn) = 0;
83 
84 protected:
85     CGFunc *cgFunc;
86     MemPool *peepMemPool;
87     CGSSAInfo *ssaInfo;
88     PeepOptimizeManager *manager = nullptr;
89 };
90 
91 class PeepPattern {
92 public:
PeepPattern(CGFunc & oneCGFunc)93     explicit PeepPattern(CGFunc &oneCGFunc) : cgFunc(oneCGFunc) {}
94     virtual ~PeepPattern() = default;
95     virtual void Run(BB &bb, Insn &insn) = 0;
96     /* optimization support function */
97     bool IfOperandIsLiveAfterInsn(const RegOperand &regOpnd, Insn &insn);
98     bool FindRegLiveOut(const RegOperand &regOpnd, const BB &bb);
99     bool CheckOpndLiveinSuccs(const RegOperand &regOpnd, const BB &bb) const;
100     bool CheckRegLiveinReturnBB(const RegOperand &regOpnd, const BB &bb) const;
101     ReturnType IsOpndLiveinBB(const RegOperand &regOpnd, const BB &bb) const;
102     int logValueAtBase2(int64 val) const;
103     bool IsMemOperandOptPattern(const Insn &insn, Insn &nextInsn);
104 
105 protected:
106     CGFunc &cgFunc;
107 };
108 
109 class CGPeepPattern {
110 public:
111     /* normal constructor */
CGPeepPattern(CGFunc & f,BB & bb,Insn & insn)112     CGPeepPattern(CGFunc &f, BB &bb, Insn &insn) : cgFunc(&f), currBB(&bb), currInsn(&insn), ssaInfo(nullptr) {}
113     /* constructor for ssa */
CGPeepPattern(CGFunc & f,BB & bb,Insn & insn,CGSSAInfo & info)114     CGPeepPattern(CGFunc &f, BB &bb, Insn &insn, CGSSAInfo &info)
115         : cgFunc(&f), currBB(&bb), currInsn(&insn), ssaInfo(&info)
116     {
117     }
118     virtual ~CGPeepPattern() = default;
119 
PhaseName()120     std::string PhaseName() const
121     {
122         return "cgpeephole";
123     }
124 
125     virtual std::string GetPatternName() = 0;
126     Insn *GetDefInsn(const RegOperand &useReg);
127     void DumpAfterPattern(std::vector<Insn *> &prevInsns, const Insn *replacedInsn, const Insn *newInsn);
128     InsnSet GetAllUseInsn(const RegOperand &defReg);
129     int64 GetLogValueAtBase2(int64 val) const;
130     /* The CC reg is unique and cannot cross-version props. */
131     bool IsCCRegCrossVersion(Insn &startInsn, Insn &endInsn, const RegOperand &ccReg);
132     /* optimization support function */
133     bool IfOperandIsLiveAfterInsn(const RegOperand &regOpnd, Insn &insn);
134     bool FindRegLiveOut(const RegOperand &regOpnd, const BB &bb);
135     bool CheckOpndLiveinSuccs(const RegOperand &regOpnd, const BB &bb) const;
136     bool CheckRegLiveinReturnBB(const RegOperand &regOpnd, const BB &bb) const;
137     ReturnType IsOpndLiveinBB(const RegOperand &regOpnd, const BB &bb) const;
GetPatternRes()138     bool GetPatternRes() const
139     {
140         return optSuccess;
141     }
GetCurrInsn()142     Insn *GetCurrInsn()
143     {
144         return currInsn;
145     }
SetCurrInsn(Insn * updateInsn)146     void SetCurrInsn(Insn *updateInsn)
147     {
148         currInsn = updateInsn;
149     }
150     virtual void Run(BB &bb, Insn &insn) = 0;
151     virtual bool CheckCondition(Insn &insn) = 0;
152 
153 protected:
154     CGFunc *cgFunc;
155     BB *currBB;
156     Insn *currInsn;
157     CGSSAInfo *ssaInfo;
158     bool optSuccess = false;
159 };
160 
161 class PeepHoleOptimizer {
162 public:
PeepHoleOptimizer(CGFunc * cf)163     explicit PeepHoleOptimizer(CGFunc *cf) : cgFunc(cf)
164     {
165         cg = cgFunc->GetCG();
166     }
167     ~PeepHoleOptimizer() = default;
168     void Peephole0();
169     void PeepholeOpt();
170     void PrePeepholeOpt();
171     void PrePeepholeOpt1();
172 
173 private:
174     CGFunc *cgFunc;
175     CG *cg;
176 }; /* class PeepHoleOptimizer */
177 
178 class PeepPatternMatch {
179 public:
PeepPatternMatch(CGFunc & oneCGFunc,MemPool * memPool)180     PeepPatternMatch(CGFunc &oneCGFunc, MemPool *memPool)
181         : optOwnMemPool(memPool), peepAllocator(memPool), optimizations(peepAllocator.Adapter()), cgFunc(oneCGFunc)
182     {
183     }
184     virtual ~PeepPatternMatch() = default;
185     virtual void Run(BB &bb, Insn &insn) = 0;
186     virtual void InitOpts() = 0;
187 
188 protected:
189     MemPool *optOwnMemPool;
190     MapleAllocator peepAllocator;
191     MapleVector<PeepPattern *> optimizations;
192     CGFunc &cgFunc;
193 };
194 
195 class PeepOptimizer {
196 public:
PeepOptimizer(CGFunc & oneCGFunc,MemPool * memPool)197     PeepOptimizer(CGFunc &oneCGFunc, MemPool *memPool) : cgFunc(oneCGFunc), peepOptMemPool(memPool)
198     {
199         index = 0;
200     }
201     ~PeepOptimizer() = default;
202     template <typename T>
203     void Run();
204     static int32 index;
205 
206 private:
207     CGFunc &cgFunc;
208     MemPool *peepOptMemPool;
209 };
210 
211 MAPLE_FUNC_PHASE_DECLARE(CgPeepHole, maplebe::CGFunc)
212 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPrePeepHole, maplebe::CGFunc)
213 MAPLE_FUNC_PHASE_DECLARE_END
214 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPostPeepHole, maplebe::CGFunc)
215 MAPLE_FUNC_PHASE_DECLARE_END
216 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPrePeepHole0, maplebe::CGFunc)
217 MAPLE_FUNC_PHASE_DECLARE_END
218 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPrePeepHole1, maplebe::CGFunc)
219 MAPLE_FUNC_PHASE_DECLARE_END
220 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPeepHole0, maplebe::CGFunc)
221 MAPLE_FUNC_PHASE_DECLARE_END
222 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPeepHole1, maplebe::CGFunc)
223 MAPLE_FUNC_PHASE_DECLARE_END
224 } /* namespace maplebe */
225 #endif /* MAPLEBE_INCLUDE_CG_PEEP_H */
226