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_H 17 #define MAPLEBE_INCLUDE_CG_REG_ALLOC_H 18 19 #include <map> 20 #include <stack> 21 #include "isa.h" 22 #include "cg_phase.h" 23 #include "maple_phase_manager.h" 24 25 namespace maplebe { 26 class VirtualRegNode { 27 public: 28 VirtualRegNode() = default; 29 VirtualRegNode(RegType type,uint32 size)30 VirtualRegNode(RegType type, uint32 size) : regType(type), size(size), regNO(kInvalidRegNO) {} 31 32 virtual ~VirtualRegNode() = default; 33 AssignPhysicalRegister(regno_t phyRegNO)34 void AssignPhysicalRegister(regno_t phyRegNO) 35 { 36 regNO = phyRegNO; 37 } 38 GetType()39 RegType GetType() const 40 { 41 return regType; 42 } 43 GetSize()44 uint32 GetSize() const 45 { 46 return size; 47 } 48 49 private: 50 RegType regType = kRegTyUndef; 51 uint32 size = 0; /* size in bytes */ 52 regno_t regNO = kInvalidRegNO; /* physical register assigned by register allocation */ 53 }; 54 55 class RegAllocator { 56 public: RegAllocator(CGFunc & tempCGFunc,MemPool & memPool)57 RegAllocator(CGFunc &tempCGFunc, MemPool &memPool) : cgFunc(&tempCGFunc), memPool(&memPool), alloc(&memPool) {} 58 59 virtual ~RegAllocator() = default; 60 61 virtual bool AllocateRegisters() = 0; 62 63 bool IsYieldPointReg(regno_t regNO) const; 64 bool IsUntouchableReg(regno_t regNO) const; 65 SetNeedDump(bool dump)66 void SetNeedDump(bool dump) 67 { 68 needDump = dump; 69 } 70 protected: 71 CGFunc *cgFunc; 72 MemPool *memPool; 73 MapleAllocator alloc; 74 bool needDump = false; 75 }; 76 77 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgRegAlloc, CGFunc) 78 OVERRIDE_DEPENDENCE 79 MAPLE_FUNC_PHASE_DECLARE_END 80 } /* namespace maplebe */ 81 82 #endif /* MAPLEBE_INCLUDE_CG_REG_ALLOC_H */ 83