• 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_PROP_H
17 #define MAPLEBE_INCLUDE_CG_PROP_H
18 
19 #include "cgfunc.h"
20 #include "cg_ssa.h"
21 #include "cg_dce.h"
22 #include "cg.h"
23 #include "reg_coalesce.h"
24 
25 namespace maplebe {
26 class CGProp {
27 public:
CGProp(MemPool & mp,CGFunc & f,CGSSAInfo & sInfo,LiveIntervalAnalysis & ll)28     CGProp(MemPool &mp, CGFunc &f, CGSSAInfo &sInfo, LiveIntervalAnalysis &ll)
29         : memPool(&mp), cgFunc(&f), propAlloc(&mp), ssaInfo(&sInfo), regll(&ll)
30     {
31         cgDce = f.GetCG()->CreateCGDce(mp, f, sInfo);
32     }
33     virtual ~CGProp() = default;
34 
35     void DoCopyProp();
36     void DoTargetProp();
37 
38 protected:
39     MemPool *memPool;
40     CGFunc *cgFunc;
41     MapleAllocator propAlloc;
GetSSAInfo()42     CGSSAInfo *GetSSAInfo()
43     {
44         return ssaInfo;
45     }
GetDce()46     CGDce *GetDce()
47     {
48         return cgDce;
49     }
GetRegll()50     LiveIntervalAnalysis *GetRegll()
51     {
52         return regll;
53     }
54 
55 private:
56     virtual void CopyProp() = 0;
57     virtual void TargetProp(Insn &insn) = 0;
58     virtual void PropPatternOpt() = 0;
59     CGSSAInfo *ssaInfo;
60     CGDce *cgDce = nullptr;
61     LiveIntervalAnalysis *regll;
62 };
63 
64 class PropOptimizeManager {
65 public:
66     ~PropOptimizeManager() = default;
67     template <typename PropOptimizePattern>
Optimize(CGFunc & cgFunc,CGSSAInfo * cgssaInfo,LiveIntervalAnalysis * ll)68     void Optimize(CGFunc &cgFunc, CGSSAInfo *cgssaInfo, LiveIntervalAnalysis *ll) const
69     {
70         PropOptimizePattern optPattern(cgFunc, cgssaInfo, ll);
71         optPattern.Run();
72     }
73     template <typename PropOptimizePattern>
Optimize(CGFunc & cgFunc,CGSSAInfo * cgssaInfo)74     void Optimize(CGFunc &cgFunc, CGSSAInfo *cgssaInfo) const
75     {
76         PropOptimizePattern optPattern(cgFunc, cgssaInfo);
77         optPattern.Run();
78     }
79 };
80 
81 class PropOptimizePattern {
82 public:
PropOptimizePattern(CGFunc & cgFunc,CGSSAInfo * cgssaInfo,LiveIntervalAnalysis * ll)83     PropOptimizePattern(CGFunc &cgFunc, CGSSAInfo *cgssaInfo, LiveIntervalAnalysis *ll)
84         : cgFunc(cgFunc), optSsaInfo(cgssaInfo), regll(ll)
85     {
86     }
87 
PropOptimizePattern(CGFunc & cgFunc,CGSSAInfo * cgssaInfo)88     PropOptimizePattern(CGFunc &cgFunc, CGSSAInfo *cgssaInfo) : cgFunc(cgFunc), optSsaInfo(cgssaInfo) {}
89     virtual ~PropOptimizePattern() = default;
90     virtual bool CheckCondition(Insn &insn) = 0;
91     virtual void Optimize(Insn &insn) = 0;
92     virtual void Run() = 0;
93 
94 protected:
PhaseName()95     std::string PhaseName() const
96     {
97         return "propopt";
98     }
99     virtual void Init() = 0;
100     Insn *FindDefInsn(const VRegVersion *useVersion);
101 
102     CGFunc &cgFunc;
103     CGSSAInfo *optSsaInfo = nullptr;
104     LiveIntervalAnalysis *regll = nullptr;
105 };
106 
107 class ReplaceRegOpndVisitor : public OperandVisitorBase,
108                               public OperandVisitors<RegOperand, ListOperand, MemOperand>,
109                               public OperandVisitor<PhiOperand> {
110 public:
ReplaceRegOpndVisitor(CGFunc & f,Insn & cInsn,uint32 cIdx,RegOperand & oldR,RegOperand & newR)111     ReplaceRegOpndVisitor(CGFunc &f, Insn &cInsn, uint32 cIdx, RegOperand &oldR, RegOperand &newR)
112         : cgFunc(&f), insn(&cInsn), idx(cIdx), oldReg(&oldR), newReg(&newR)
113     {
114     }
115     virtual ~ReplaceRegOpndVisitor() = default;
116 
117 protected:
118     CGFunc *cgFunc;
119     Insn *insn;
120     uint32 idx;
121     RegOperand *oldReg;
122     RegOperand *newReg;
123 };
124 
125 MAPLE_FUNC_PHASE_DECLARE(CgCopyProp, maplebe::CGFunc)
126 MAPLE_FUNC_PHASE_DECLARE(CgTargetProp, maplebe::CGFunc)
127 }  // namespace maplebe
128 #endif /* MAPLEBE_INCLUDE_CG_PROP_H */
129