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 "cgfunc.h" 20 #include "maple_phase_manager.h" 21 22 namespace maplebe { 23 class RATimerManager { 24 public: 25 RATimerManager(const RATimerManager&) = delete; 26 RATimerManager& operator=(const RATimerManager&) = delete; 27 GetInstance()28 static MPLTimerManager &GetInstance() 29 { 30 static RATimerManager raTimerM{}; 31 return raTimerM.timerM; 32 } 33 PrintAllTimerAndClear(const std::string & funcName)34 void PrintAllTimerAndClear(const std::string &funcName) 35 { 36 LogInfo::MapleLogger() << "Func[" << funcName << "] Reg Alloc Time:\n"; 37 LogInfo::MapleLogger() << timerM.ConvertAllTimer2Str() << std::endl; 38 timerM.Clear(); 39 } 40 private: 41 RATimerManager() = default; 42 ~RATimerManager() = default; 43 44 MPLTimerManager timerM; 45 }; 46 47 // RA time statistics marco. If defined, RA time consumed will print. 48 #ifdef REG_ALLOC_TIME_STATISTICS 49 #define RA_TIMER_REGISTER(timerName, str) MPLTimerRegister timerName##Timer(RATimerManager::GetInstance(), str) 50 #define RA_TIMER_STOP(timerName) timerName##Timer.Stop() 51 #define RA_TIMER_PRINT(funcName) RATimerManager::GetInstance().PrintAllTimerAndClear(funcName) 52 #else 53 #define RA_TIMER_REGISTER(name, str) 54 #define RA_TIMER_STOP(name) 55 #define RA_TIMER_PRINT(funcName) 56 #endif 57 58 class RegAllocator { 59 public: RegAllocator(CGFunc & tempCGFunc,MemPool & memPool)60 RegAllocator(CGFunc &tempCGFunc, MemPool &memPool) 61 : cgFunc(&tempCGFunc), 62 memPool(&memPool), 63 alloc(&memPool), 64 regInfo(tempCGFunc.GetTargetRegInfo()) 65 { 66 regInfo->Init(); 67 } 68 69 virtual ~RegAllocator() = default; 70 71 virtual bool AllocateRegisters() = 0; 72 PhaseName()73 virtual std::string PhaseName() const 74 { 75 return "regalloc"; 76 } 77 SetNeedDump(bool dump)78 void SetNeedDump(bool dump) 79 { 80 needDump = dump; 81 } 82 protected: 83 CGFunc *cgFunc = nullptr; 84 MemPool *memPool = nullptr; 85 MapleAllocator alloc; 86 RegisterInfo *regInfo = nullptr; 87 bool needDump = false; 88 }; 89 90 MAPLE_FUNC_PHASE_DECLARE_BEGIN(CgRegAlloc, CGFunc) 91 OVERRIDE_DEPENDENCE 92 MAPLE_FUNC_PHASE_DECLARE_END 93 } /* namespace maplebe */ 94 95 #endif /* MAPLEBE_INCLUDE_CG_REG_ALLOC_H */ 96