• 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 #ifndef MAPLEBE_INCLUDE_CG_DATA_DEP_BASE_H
16 #define MAPLEBE_INCLUDE_CG_DATA_DEP_BASE_H
17 
18 #include "deps.h"
19 #include "cgbb.h"
20 #include "cg_cdg.h"
21 
22 namespace maplebe {
23 using namespace maple;
24 constexpr maple::uint32 kMaxDependenceNum = 200;
25 constexpr maple::uint32 kMaxInsnNum = 220;
26 
27 class DataDepBase {
28 public:
DataDepBase(MemPool & memPool,CGFunc & func,MAD & mad,bool isIntraAna)29     DataDepBase(MemPool &memPool, CGFunc &func, MAD &mad, bool isIntraAna)
30         : memPool(memPool),
31           alloc(&memPool),
32           cgFunc(func),
33           mad(mad),
34           beforeRA(!cgFunc.IsAfterRegAlloc()),
35           isIntra(isIntraAna)
36     {
37     }
~DataDepBase()38     virtual ~DataDepBase()
39     {
40         curRegion = nullptr;
41         curCDGNode = nullptr;
42     }
43 
44     enum DataFlowInfoType : uint8 {
45         kDataFlowUndef,
46         kMembar,
47         kLastCall,
48         kLastFrameDef,
49         kStackUses,
50         kStackDefs,
51         kHeapUses,
52         kHeapDefs,
53         kMayThrows,
54         kAmbiguous,
55     };
56 
SetCDGNode(CDGNode * cdgNode)57     void SetCDGNode(CDGNode *cdgNode)
58     {
59         curCDGNode = cdgNode;
60     }
GetCDGNode()61     CDGNode *GetCDGNode()
62     {
63         return curCDGNode;
64     }
SetCDGRegion(CDGRegion * region)65     void SetCDGRegion(CDGRegion *region)
66     {
67         curRegion = region;
68     }
69 
70     void ProcessNonMachineInsn(Insn &insn, MapleVector<Insn *> &comments, MapleVector<DepNode *> &dataNodes,
71                                const Insn *&locInsn);
72 
73     void AddDependence4InsnInVectorByType(MapleVector<Insn *> &insns, Insn &insn, const DepType &type);
74     void AddDependence4InsnInVectorByTypeAndCmp(MapleVector<Insn *> &insns, Insn &insn, const DepType &type);
75 
76     const std::string &GetDepTypeName(DepType depType) const;
77 
78     bool IfInAmbiRegs(regno_t regNO) const;
79     void AddDependence(DepNode &fromNode, DepNode &toNode, DepType depType);
80     DepNode *GenerateDepNode(Insn &insn, MapleVector<DepNode *> &nodes, uint32 &nodeSum, MapleVector<Insn *> &comments);
81     void RemoveSelfDeps(Insn &insn);
82     void BuildDepsUseReg(Insn &insn, regno_t regNO);
83     void BuildDepsDefReg(Insn &insn, regno_t regNO);
84     void BuildAmbiInsnDependency(Insn &insn);
85     void BuildDepsControlAll(Insn &insn, const MapleVector<DepNode *> &nodes);
86     void BuildDepsBetweenControlRegAndCall(Insn &insn, bool isDest);
87     void BuildDepsLastCallInsn(Insn &insn);
88     void BuildInterBlockDefUseDependency(DepNode &curDepNode, regno_t regNO, DepType depType, bool isDef);
89     void BuildPredPathDefDependencyDFS(BB &curBB, std::vector<bool> &visited, DepNode &depNode, regno_t regNO,
90                                        DepType depType);
91     void BuildPredPathUseDependencyDFS(BB &curBB, std::vector<bool> &visited, DepNode &depNode, regno_t regNO,
92                                        DepType depType);
93     void BuildInterBlockSpecialDataInfoDependency(DepNode &curDepNode, bool needCmp, DepType depType,
94                                                   DataDepBase::DataFlowInfoType infoType);
95     void BuildPredPathSpecialDataInfoDependencyDFS(BB &curBB, std::vector<bool> &visited, bool needCmp,
96                                                    DepNode &depNode, DepType depType,
97                                                    DataDepBase::DataFlowInfoType infoType);
98 
99     virtual void InitCDGNodeDataInfo(MemPool &mp, MapleAllocator &alloc, CDGNode &cdgNode) = 0;
100     virtual bool IsFrameReg(const RegOperand &) const = 0;
101     virtual void BuildDepsMemBar(Insn &insn) = 0;
102     virtual void BuildDepsUseMem(Insn &insn, MemOperand &memOpnd) = 0;
103     virtual void BuildDepsDefMem(Insn &insn, MemOperand &memOpnd) = 0;
104     virtual void BuildDepsAccessStImmMem(Insn &insn) = 0;
105     virtual void BuildCallerSavedDeps(Insn &insn) = 0;
106     virtual void BuildDepsDirtyStack(Insn &insn) = 0;
107     virtual void BuildDepsUseStack(Insn &insn) = 0;
108     virtual void BuildDepsDirtyHeap(Insn &insn) = 0;
109     virtual void BuildOpndDependency(Insn &insn) = 0;
110     virtual void BuildSpecialInsnDependency(Insn &insn, const MapleVector<DepNode *> &nodes) = 0;
111     virtual void BuildSpecialCallDeps(Insn &insn) = 0;
112     virtual void BuildAsmInsnDependency(Insn &insn) = 0;
113     virtual void BuildInterBlockMemDefUseDependency(DepNode &depNode, bool isMemDef) = 0;
114     virtual void BuildPredPathMemDefDependencyDFS(BB &curBB, std::vector<bool> &visited, DepNode &depNode) = 0;
115     virtual void BuildPredPathMemUseDependencyDFS(BB &curBB, std::vector<bool> &visited, DepNode &depNode) = 0;
116     virtual void DumpNodeStyleInDot(std::ofstream &file, DepNode &depNode) = 0;
117 
118 private:
119     // only called by BuildPredPathSpecialDataInfoDependencyDFS
120     void BuildForStackHeapDefUseInfoData(bool needCmp, MapleVector<Insn *> &insns, DepNode &depNode, DepType depType);
121 
122 protected:
123     MemPool &memPool;
124     MapleAllocator alloc;
125     CGFunc &cgFunc;
126     MAD &mad;
127     bool beforeRA = false;
128     bool isIntra = false;
129     CDGNode *curCDGNode = nullptr;
130     CDGRegion *curRegion = nullptr;
131 };
132 }  // namespace maplebe
133 
134 #endif  // MAPLEBE_INCLUDE_CG_DATA_DEP_BASE_H
135