• 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_REG_ALLOC_BASIC_H
17 #define MAPLEBE_INCLUDE_CG_REG_ALLOC_BASIC_H
18 
19 #include "reg_alloc.h"
20 #include "operand.h"
21 #include "cgfunc.h"
22 
23 namespace maplebe {
24 class DefaultO0RegAllocator : public RegAllocator {
25 public:
DefaultO0RegAllocator(CGFunc & cgFunc,MemPool & memPool)26     DefaultO0RegAllocator(CGFunc &cgFunc, MemPool &memPool)
27         : RegAllocator(cgFunc, memPool),
28           calleeSaveUsed(alloc.Adapter()),
29           availRegSet(alloc.Adapter()),
30           regMap(std::less<uint32>(), alloc.Adapter()),
31           liveReg(std::less<uint8>(), alloc.Adapter()),
32           allocatedSet(std::less<Operand *>(), alloc.Adapter()),
33           regLiveness(alloc.Adapter()),
34           rememberRegs(alloc.Adapter())
35     {
36         availRegSet.resize(regInfo->GetAllRegNum());
37     }
38 
~DefaultO0RegAllocator()39     ~DefaultO0RegAllocator() override
40     {
41         regInfo = nullptr;
42     }
43 
44     bool AllocateRegisters() override;
45 
46     void InitAvailReg();
47 
48     bool AllocatePhysicalRegister(const RegOperand &opnd);
49     void ReleaseReg(regno_t reg);
50     void ReleaseReg(const RegOperand &regOpnd);
51     void GetPhysicalRegisterBank(RegType regType, regno_t &start, regno_t &end) const;
52     void AllocHandleDestList(Insn &insn, Operand &opnd, uint32 idx);
53     void AllocHandleDest(Insn &insn, Operand &opnd, uint32 idx);
54     void AllocHandleSrcList(Insn &insn, Operand &opnd, uint32 idx);
55     void AllocHandleSrc(Insn &insn, Operand &opnd, uint32 idx);
56     void SaveCalleeSavedReg(const RegOperand &opnd);
57 
58 protected:
59     Operand *HandleRegOpnd(Operand &opnd);
60     Operand *HandleMemOpnd(Operand &opnd);
61     Operand *AllocSrcOpnd(Operand &opnd);
62     Operand *AllocDestOpnd(Operand &opnd, const Insn &insn);
63     uint32 GetRegLivenessId(regno_t regNo);
64     bool CheckRangesOverlap(const std::pair<uint32, uint32> &range1,
65                             const MapleVector<std::pair<uint32, uint32>> &ranges2) const;
66     void SetupRegLiveness(BB *bb);
67     void SetupRegLiveness(MemOperand &opnd, uint32 insnId);
68     void SetupRegLiveness(ListOperand &opnd, uint32 insnId, bool isDef);
69     void SetupRegLiveness(RegOperand &opnd, uint32 insnId, bool isDef);
70 
71     MapleSet<regno_t> calleeSaveUsed;
72     MapleVector<bool> availRegSet;
73     MapleMap<uint32, regno_t> regMap; /* virtual-register-to-physical-register map */
74     MapleSet<uint8> liveReg;          /* a set of currently live physical registers */
75     MapleSet<Operand *> allocatedSet; /* already allocated */
76     MapleMap<regno_t, MapleVector<std::pair<uint32, uint32>>> regLiveness;
77     MapleVector<regno_t> rememberRegs;
78 };
79 } /* namespace maplebe */
80 
81 #endif /* MAPLEBE_INCLUDE_CG_REG_ALLOC_BASIC_H */
82