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 MAPLE_PHASE_INCLUDE_MAPLE_PHASE_H 17 #define MAPLE_PHASE_INCLUDE_MAPLE_PHASE_H 18 #include "maple_phase_support.h" 19 namespace maple { 20 class MaplePhase; 21 class AnalysisInfoHook; 22 class MIRModule; 23 enum MaplePhaseKind : uint8 { kModulePM, kModulePhase, kFunctionPM, kFunctionPhase, kSccPhasePM, kSccPhase }; 24 25 class MaplePhase { 26 public: MaplePhase(MaplePhaseKind kind,MaplePhaseID id,MemPool & mp)27 MaplePhase(MaplePhaseKind kind, MaplePhaseID id, MemPool &mp) 28 : phaseAllocator(&mp), phaseKind(kind), phaseID(id), tempMemPools(phaseAllocator.Adapter()) 29 { 30 } ~MaplePhase()31 virtual ~MaplePhase() 32 { 33 if (analysisInfo != nullptr) { 34 analysisInfo = nullptr; 35 } 36 }; 37 void Dump() const; GetPhaseID()38 MaplePhaseID GetPhaseID() const 39 { 40 return phaseID; 41 } SetPhaseID(MaplePhaseID id)42 void SetPhaseID(MaplePhaseID id) 43 { 44 phaseID = id; 45 } 46 virtual std::string PhaseName() const = 0; 47 void SetAnalysisInfoHook(AnalysisInfoHook *hook); 48 AnalysisInfoHook *GetAnalysisInfoHook(); 49 50 void AnalysisDepInit(AnalysisDep &aDep) const; GetPhaseAllocator()51 MapleAllocator *GetPhaseAllocator() 52 { 53 return &phaseAllocator; 54 } GetPhaseMemPool()55 MemPool *GetPhaseMemPool() 56 { 57 return phaseAllocator.GetMemPool(); 58 } 59 60 MemPool *ApplyTempMemPool(); 61 void ClearTempMemPool(); 62 63 private: 64 MapleAllocator phaseAllocator; // mempool for phase 65 AnalysisInfoHook *analysisInfo = nullptr; 66 virtual void GetAnalysisDependence(AnalysisDep &aDep) const; 67 MaplePhaseKind phaseKind; 68 MaplePhaseID phaseID; 69 MapleVector<MemPool *> tempMemPools; // delete after phase Run [Thread Local] 70 }; 71 72 class MapleModulePhase : public MaplePhase { 73 public: MapleModulePhase(MaplePhaseID id,MemPool * mp)74 MapleModulePhase(MaplePhaseID id, MemPool *mp) : MaplePhase(kModulePhase, id, *mp) {} 75 ~MapleModulePhase() override = default; 76 77 virtual bool PhaseRun(MIRModule &m) = 0; 78 }; 79 80 template <class funcT> 81 class MapleFunctionPhase : public MaplePhase { 82 public: MapleFunctionPhase(MaplePhaseID id,MemPool * mp)83 MapleFunctionPhase(MaplePhaseID id, MemPool *mp) : MaplePhase(kFunctionPhase, id, *mp) {} 84 ~MapleFunctionPhase() override = default; 85 virtual bool PhaseRun(funcT &f) = 0; 86 }; 87 88 template <class SccT> 89 class MapleSccPhase : public MaplePhase { 90 public: MapleSccPhase(MaplePhaseID id,MemPool * mp)91 MapleSccPhase(MaplePhaseID id, MemPool *mp) : MaplePhase(kSccPhase, id, *mp) {} 92 ~MapleSccPhase() override = default; 93 virtual bool PhaseRun(SccT &scc) = 0; 94 template <class funcT> 95 void Dump(funcT &f, const std::string phaseName); 96 }; 97 98 class MaplePhaseRegister { 99 public: 100 MaplePhaseRegister() = default; 101 ~MaplePhaseRegister() = default; 102 103 static MaplePhaseRegister *GetMaplePhaseRegister(); 104 void RegisterPhase(const MaplePhaseInfo &PI); 105 const MaplePhaseInfo *GetPhaseByID(MaplePhaseID id); GetAllPassInfo()106 const auto &GetAllPassInfo() const 107 { 108 return passInfoMap; 109 } 110 111 private: 112 std::map<MaplePhaseID, const MaplePhaseInfo *> passInfoMap; 113 }; 114 115 template <typename phaseNameT> 116 class RegisterPhase : public MaplePhaseInfo { 117 public: 118 RegisterPhase(const std::string name, bool isAnalysis = false, bool CFGOnly = false, bool canSkip = false) 119 : MaplePhaseInfo(name, &phaseNameT::id, isAnalysis, CFGOnly, canSkip) 120 { 121 SetConstructor(phaseNameT::CreatePhase); 122 auto globalRegistry = maple::MaplePhaseRegister::GetMaplePhaseRegister(); 123 globalRegistry->RegisterPhase(*this); 124 } 125 ~RegisterPhase() = default; 126 }; 127 128 #define PHASECONSTRUCTOR(PHASENAME) \ 129 static unsigned int id; \ 130 static MaplePhase *CreatePhase(MemPool *createMP) \ 131 { \ 132 return createMP->New<PHASENAME>(createMP); \ 133 } 134 135 #define OVERRIDE_DEPENDENCE \ 136 private: \ 137 void GetAnalysisDependence(maple::AnalysisDep &aDep) const override; 138 139 #define MAPLE_FUNC_PHASE_DECLARE_BEGIN(PHASENAME, IRTYPE) \ 140 class PHASENAME : public MapleFunctionPhase<IRTYPE> { \ 141 public: \ 142 explicit PHASENAME(MemPool *mp) : MapleFunctionPhase<IRTYPE>(&id, mp) {} \ 143 ~PHASENAME() override = default; \ 144 std::string PhaseName() const override; \ 145 static unsigned int id; \ 146 static MaplePhase *CreatePhase(MemPool *createMP) \ 147 { \ 148 return createMP->New<PHASENAME>(createMP); \ 149 } \ 150 bool PhaseRun(IRTYPE &func) override; 151 152 #define MAPLE_FUNC_PHASE_DECLARE_END \ 153 } \ 154 ; 155 156 #define MAPLE_SCC_PHASE_DECLARE_BEGIN(PHASENAME, IRTYPE) \ 157 class PHASENAME : public MapleSccPhase<IRTYPE> { \ 158 public: \ 159 explicit PHASENAME(MemPool *mp) : MapleSccPhase<IRTYPE>(&id, mp) {} \ 160 ~PHASENAME() override = default; \ 161 std::string PhaseName() const override; \ 162 static unsigned int id; \ 163 static MaplePhase *CreatePhase(MemPool *createMP) \ 164 { \ 165 return createMP->New<PHASENAME>(createMP); \ 166 } \ 167 bool PhaseRun(IRTYPE &scc) override; 168 169 #define MAPLE_SCC_PHASE_DECLARE_END \ 170 } \ 171 ; 172 173 #define MAPLE_FUNC_PHASE_DECLARE(PHASENAME, IRTYPE) \ 174 MAPLE_FUNC_PHASE_DECLARE_BEGIN(PHASENAME, IRTYPE) \ 175 OVERRIDE_DEPENDENCE \ 176 MAPLE_FUNC_PHASE_DECLARE_END 177 178 #define MAPLE_MODULE_PHASE_DECLARE_BEGIN(PHASENAME) \ 179 class PHASENAME : public MapleModulePhase { \ 180 public: \ 181 explicit PHASENAME(MemPool *mp) : MapleModulePhase(&id, mp) {} \ 182 ~PHASENAME() override = default; \ 183 std::string PhaseName() const override; \ 184 static unsigned int id; \ 185 static MaplePhase *CreatePhase(MemPool *createMP) \ 186 { \ 187 return createMP->New<PHASENAME>(createMP); \ 188 } \ 189 bool PhaseRun(MIRModule &m) override; 190 191 #define MAPLE_MODULE_PHASE_DECLARE_END \ 192 } \ 193 ; 194 195 #define MAPLE_MODULE_PHASE_DECLARE(PHASENAME) \ 196 MAPLE_MODULE_PHASE_DECLARE_BEGIN(PHASENAME) \ 197 OVERRIDE_DEPENDENCE \ 198 MAPLE_MODULE_PHASE_DECLARE_END 199 200 #define MAPLE_ANALYSIS_PHASE_REGISTER(CLASSNAME, PHASENAME) \ 201 unsigned int CLASSNAME::id = 0; \ 202 std::string CLASSNAME::PhaseName() const \ 203 { \ 204 return #PHASENAME; \ 205 } \ 206 static RegisterPhase<CLASSNAME> MAPLEPHASE_##PHASENAME(#PHASENAME, true); 207 208 #define MAPLE_TRANSFORM_PHASE_REGISTER(CLASSNAME, PHASENAME) \ 209 unsigned int CLASSNAME::id = 0; \ 210 std::string CLASSNAME::PhaseName() const \ 211 { \ 212 return #PHASENAME; \ 213 } \ 214 static RegisterPhase<CLASSNAME> MAPLEPHASE_##PHASENAME(#PHASENAME, false); 215 216 #define MAPLE_ANALYSIS_PHASE_REGISTER_CANSKIP(CLASSNAME, PHASENAME) \ 217 unsigned int CLASSNAME::id = 0; \ 218 std::string CLASSNAME::PhaseName() const \ 219 { \ 220 return #PHASENAME; \ 221 } \ 222 static RegisterPhase<CLASSNAME> MAPLEPHASE_##PHASENAME(#PHASENAME, true, false, true); 223 224 #define MAPLE_TRANSFORM_PHASE_REGISTER_CANSKIP(CLASSNAME, PHASENAME) \ 225 unsigned int CLASSNAME::id = 0; \ 226 std::string CLASSNAME::PhaseName() const \ 227 { \ 228 return #PHASENAME; \ 229 } \ 230 static RegisterPhase<CLASSNAME> MAPLEPHASE_##PHASENAME(#PHASENAME, false, false, true); 231 232 #define GET_ANALYSIS(PHASENAME, PHASEKEY) \ 233 static_cast<PHASENAME *>(GetAnalysisInfoHook()->FindAnalysisData((PHASEKEY).GetUniqueID(), this, &PHASENAME::id)) \ 234 ->GetResult() 235 236 #define FORCE_GET(PHASENAME) \ 237 static_cast<PHASENAME *>(GetAnalysisInfoHook()->ForceRunAnalysisPhase<meFuncOptTy, MeFunction>(&PHASENAME::id, f)) \ 238 ->GetResult() 239 240 #define FORCE_INVALID(PHASENAME, PHASEKEY) \ 241 GetAnalysisInfoHook()->ForceEraseAnalysisPhase(PHASEKEY.GetUniqueID(), &PHASENAME::id) 242 } // namespace maple 243 244 #endif // MAPLE_ME_INCLUDE_ME_PHASE_MANAGER_H 245