• 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) {}
29 
30     ~PeepOptimizeManager() = default;
31     template <typename OptimizePattern>
32     void Optimize(bool patternEnable = false)
33     {
34         if (!patternEnable) {
35             return;
36         }
37         OptimizePattern optPattern(*cgFunc, *currBB, *currInsn);
38         optPattern.Run(*currBB, *currInsn);
39         optSuccess = optPattern.GetPatternRes() || optSuccess;
40         if (optSuccess && optPattern.GetCurrInsn() != nullptr) {
41             currInsn = optPattern.GetCurrInsn();
42         }
43     }
44     template <typename OptimizePattern>
45     void NormalPatternOpt(bool patternEnable = false)
46     {
47         if (!patternEnable) {
48             return;
49         }
50         OptimizePattern optPattern(*cgFunc, *currBB, *currInsn);
51         optPattern.Run(*currBB, *currInsn);
52         optSuccess = optPattern.GetPatternRes() || optSuccess;
53         if (optSuccess && optPattern.GetCurrInsn() != nullptr) {
54             currInsn = optPattern.GetCurrInsn();
55         }
56     }
SetOptSuccess(bool optRes)57     void SetOptSuccess(bool optRes)
58     {
59         optSuccess = optRes;
60     }
OptSuccess()61     bool OptSuccess() const
62     {
63         return optSuccess;
64     }
65 
66 private:
67     CGFunc *cgFunc;
68     BB *currBB;
69     Insn *currInsn;
70     /*
71      * The flag indicates whether the optimization pattern is successful,
72      * this prevents the next optimization pattern that processs the same mop from failing to get the validInsn,
73      * which was changed by previous pattern.
74      *
75      * Set the flag to true when the pattern optimize successfully.
76      */
77     bool optSuccess = false;
78 };
79 
80 class CGPeepHole {
81 public:
82     /* normal constructor */
CGPeepHole(CGFunc & f,MemPool * memPool)83     CGPeepHole(CGFunc &f, MemPool *memPool) : cgFunc(&f), peepMemPool(memPool) {}
~CGPeepHole()84     virtual ~CGPeepHole() { }
85 
86     virtual void Run() = 0;
87     virtual void DoNormalOptimize(BB &bb, Insn &insn) = 0;
88 
89 protected:
90     CGFunc *cgFunc;
91     MemPool *peepMemPool;
92     PeepOptimizeManager *manager = nullptr;
93 };
94 
95 class PeepPattern {
96 public:
PeepPattern(CGFunc & oneCGFunc)97     explicit PeepPattern(CGFunc &oneCGFunc) : cgFunc(oneCGFunc) {}
98     virtual ~PeepPattern() = default;
99     virtual void Run(BB &bb, Insn &insn) = 0;
100     /* optimization support function */
101     bool IfOperandIsLiveAfterInsn(const RegOperand &regOpnd, Insn &insn);
102     bool FindRegLiveOut(const RegOperand &regOpnd, const BB &bb);
103     bool CheckOpndLiveinSuccs(const RegOperand &regOpnd, const BB &bb) const;
104     bool CheckRegLiveinReturnBB(const RegOperand &regOpnd, const BB &bb) const;
105     ReturnType IsOpndLiveinBB(const RegOperand &regOpnd, const BB &bb) const;
106     int LogValueAtBase2(int64 val) const;
107     bool IsMemOperandOptPattern(const Insn &insn, Insn &nextInsn);
108 
109 protected:
110     CGFunc &cgFunc;
111 };
112 
113 class CGPeepPattern {
114 public:
115     /* normal constructor */
CGPeepPattern(CGFunc & f,BB & bb,Insn & insn)116     CGPeepPattern(CGFunc &f, BB &bb, Insn &insn) : cgFunc(&f), currBB(&bb), currInsn(&insn) {}
117     virtual ~CGPeepPattern() = default;
118 
PhaseName()119     std::string PhaseName() const
120     {
121         return "cgpeephole";
122     }
123 
124     virtual std::string GetPatternName() = 0;
125     /* optimization support function */
126     bool IfOperandIsLiveAfterInsn(const RegOperand &regOpnd, Insn &insn);
127     bool FindRegLiveOut(const RegOperand &regOpnd, const BB &bb);
128     bool CheckOpndLiveinSuccs(const RegOperand &regOpnd, const BB &bb) const;
129     bool CheckRegLiveinReturnBB(const RegOperand &regOpnd, const BB &bb) const;
130     ReturnType IsOpndLiveinBB(const RegOperand &regOpnd, const BB &bb) const;
GetPatternRes()131     bool GetPatternRes() const
132     {
133         return optSuccess;
134     }
GetCurrInsn()135     Insn *GetCurrInsn()
136     {
137         return currInsn;
138     }
SetCurrInsn(Insn * updateInsn)139     void SetCurrInsn(Insn *updateInsn)
140     {
141         currInsn = updateInsn;
142     }
143     virtual void Run(BB &bb, Insn &insn) = 0;
144     virtual bool CheckCondition(Insn &insn) = 0;
145 
146 protected:
147     CGFunc *cgFunc;
148     BB *currBB;
149     Insn *currInsn;
150     // !!! If the pattern is optimized, set the $optSuccess to true and check before the subsequent patterns
151     // of the same mop, otherwise, the subsequent patterns of the same mop will get the old wrong instruction.
152     bool optSuccess = false;
153 };
154 
155 class PeepHoleOptimizer {
156 public:
PeepHoleOptimizer(CGFunc * cf)157     explicit PeepHoleOptimizer(CGFunc *cf) : cgFunc(cf)
158     {
159         cg = cgFunc->GetCG();
160     }
161     ~PeepHoleOptimizer() = default;
162     void Peephole0();
163     void PeepholeOpt();
164 
165 private:
166     CGFunc *cgFunc;
167     CG *cg = nullptr;
168 }; /* class PeepHoleOptimizer */
169 
170 class PeepPatternMatch {
171 public:
PeepPatternMatch(CGFunc & oneCGFunc,MemPool * memPool)172     PeepPatternMatch(CGFunc &oneCGFunc, MemPool *memPool)
173         : optOwnMemPool(memPool), peepAllocator(memPool), optimizations(peepAllocator.Adapter()), cgFunc(oneCGFunc)
174     {
175     }
176     virtual ~PeepPatternMatch() = default;
177     virtual void Run(BB &bb, Insn &insn) = 0;
178     virtual void InitOpts() = 0;
179 
180 protected:
181     MemPool *optOwnMemPool;
182     MapleAllocator peepAllocator;
183     MapleVector<PeepPattern *> optimizations;
184     CGFunc &cgFunc;
185 };
186 
187 class PeepOptimizer {
188 public:
PeepOptimizer(CGFunc & oneCGFunc,MemPool * memPool)189     PeepOptimizer(CGFunc &oneCGFunc, MemPool *memPool) : cgFunc(oneCGFunc), peepOptMemPool(memPool)
190     {
191         index = 0;
192     }
193     ~PeepOptimizer() = default;
194     template <typename T>
195     void Run();
196     static int32 index;
197 
198 private:
199     CGFunc &cgFunc;
200     MemPool *peepOptMemPool;
201 };
202 
203 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPostPeepHole, maplebe::CGFunc)
204 MAPLE_FUNC_PHASE_DECLARE_END
205 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPeepHole0, maplebe::CGFunc)
206 MAPLE_FUNC_PHASE_DECLARE_END
207 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgPeepHole1, maplebe::CGFunc)
208 MAPLE_FUNC_PHASE_DECLARE_END
209 } /* namespace maplebe */
210 #endif /* MAPLEBE_INCLUDE_CG_PEEP_H */
211