• 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_PRESSURE_H
17 #define MAPLEBE_INCLUDE_CG_PRESSURE_H
18 
19 #include "cgbb.h"
20 #include "cgfunc.h"
21 
22 namespace maplebe {
23 struct RegList {
24     Insn *insn;
25     RegList *next;
26 };
27 
28 #define FOR_ALL_REGCLASS(i) for (uint32 i = 0; i < static_cast<uint32>(RegPressure::GetMaxRegClassNum()); ++i)
29 
30 class RegPressure {
31 public:
RegPressure(MapleAllocator & alloc)32     explicit RegPressure(MapleAllocator &alloc)
33         : regUses(alloc.Adapter()), regDefs(alloc.Adapter()), pressure(alloc.Adapter()), deadDefNum(alloc.Adapter())
34     {
35     }
36 
37     virtual ~RegPressure() = default;
38 
39     void DumpRegPressure() const;
40 
SetRegUses(RegList * regList)41     void SetRegUses(RegList *regList)
42     {
43         regUses.emplace_back(regList);
44     }
45 
SetRegDefs(size_t idx,RegList * regList)46     void SetRegDefs(size_t idx, RegList *regList)
47     {
48         if (idx < regDefs.size()) {
49             regDefs[idx] = regList;
50         } else {
51             regDefs.emplace_back(regList);
52         }
53     }
54 
SetMaxRegClassNum(int32 maxClassNum)55     static void SetMaxRegClassNum(int32 maxClassNum)
56     {
57         maxRegClassNum = maxClassNum;
58     }
59 
GetMaxRegClassNum()60     static int32 GetMaxRegClassNum()
61     {
62         return maxRegClassNum;
63     }
64 
GetPriority()65     int32 GetPriority() const
66     {
67         return priority;
68     }
69 
SetPriority(int32 value)70     void SetPriority(int32 value)
71     {
72         priority = value;
73     }
74 
GetMaxDepth()75     int32 GetMaxDepth() const
76     {
77         return maxDepth;
78     }
79 
SetMaxDepth(int32 value)80     void SetMaxDepth(int32 value)
81     {
82         maxDepth = value;
83     }
84 
GetNear()85     int32 GetNear() const
86     {
87         return near;
88     }
89 
SetNear(int32 value)90     void SetNear(int32 value)
91     {
92         near = value;
93     }
94 
GetIncPressure()95     int32 GetIncPressure() const
96     {
97         return incPressure;
98     }
99 
SetIncPressure(bool value)100     void SetIncPressure(bool value)
101     {
102         incPressure = value;
103     }
GetPressure()104     const MapleVector<int32> &GetPressure() const
105     {
106         return pressure;
107     }
108 
IncPressureByIndex(uint32 index)109     void IncPressureByIndex(uint32 index)
110     {
111         DEBUG_ASSERT(index < pressure.size(), "index out of range");
112         ++pressure[index];
113     }
114 
DecPressureByIndex(uint32 index)115     void DecPressureByIndex(uint32 index)
116     {
117         DEBUG_ASSERT(index < pressure.size(), "index out of range");
118         --pressure[index];
119     }
120 
InitPressure()121     void InitPressure()
122     {
123         pressure.resize(static_cast<uint64>(maxRegClassNum), 0);
124         deadDefNum.resize(static_cast<uint64>(maxRegClassNum), 0);
125         incPressure = false;
126     }
127 
GetDeadDefNum()128     const MapleVector<int32> &GetDeadDefNum() const
129     {
130         return deadDefNum;
131     }
132 
IncDeadDefByIndex(uint32 index)133     void IncDeadDefByIndex(uint32 index)
134     {
135         DEBUG_ASSERT(index < deadDefNum.size(), "index out of range");
136         ++deadDefNum[index];
137     }
138 
GetRegUses(size_t idx)139     RegList *GetRegUses(size_t idx) const
140     {
141         return idx < regUses.size() ? regUses[idx] : nullptr;
142     }
143 
InitRegUsesSize(size_t size)144     void InitRegUsesSize(size_t size)
145     {
146         regUses.reserve(size);
147     }
148 
GetRegDefs(size_t idx)149     RegList *GetRegDefs(size_t idx) const
150     {
151         return idx < regDefs.size() ? regDefs[idx] : nullptr;
152     }
153 
InitRegDefsSize(size_t size)154     void InitRegDefsSize(size_t size)
155     {
156         regDefs.reserve(size);
157     }
158 
SetHasPreg(bool value)159     void SetHasPreg(bool value)
160     {
161         hasPreg = value;
162     }
163 
GetHasPreg()164     bool GetHasPreg() const
165     {
166         return hasPreg;
167     }
168 
SetNumCall(int32 value)169     void SetNumCall(int32 value)
170     {
171         callNum = value;
172     }
173 
GetNumCall()174     int32 GetNumCall() const
175     {
176         return callNum;
177     }
178 
SetHasNativeCallRegister(bool value)179     void SetHasNativeCallRegister(bool value)
180     {
181         hasNativeCallRegister = value;
182     }
183 
GetHasNativeCallRegister()184     bool GetHasNativeCallRegister() const
185     {
186         return hasNativeCallRegister;
187     }
188 
189 private:
190     /* save reglist of every uses'register */
191     MapleVector<RegList *> regUses;
192     /* save reglist of every defs'register */
193     MapleVector<RegList *> regDefs;
194 
195     /* the number of the node needs registers */
196     MapleVector<int32> pressure;
197     /* the count of dead define registers */
198     MapleVector<int32> deadDefNum;
199     /* max number of reg's class */
200     static int32 maxRegClassNum;
201     int32 priority = 0;
202     int32 maxDepth = 0;
203     int32 near = 0;
204     /* the number of successor call */
205     int32 callNum = 0;
206     /* if a type register increase then set incPressure as true. */
207     bool incPressure = false;
208     /* if define physical register, set hasPreg as true */
209     bool hasPreg = false;
210     /* it is call native special register */
211     bool hasNativeCallRegister = false;
212 };
213 } /* namespace maplebe */
214 
215 #endif /* MAPLEBE_INCLUDE_CG_PRESSURE_H */
216