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 #include "maple_phase.h"
17 namespace maple {
GetMaplePhaseRegister()18 MaplePhaseRegister *MaplePhaseRegister::GetMaplePhaseRegister()
19 {
20     static MaplePhaseRegister *globalRegister = new MaplePhaseRegister();
21     return globalRegister;
22 }
23 
RegisterPhase(const MaplePhaseInfo &PI)24 void MaplePhaseRegister::RegisterPhase(const MaplePhaseInfo &PI)
25 {
26     bool checkSucc = passInfoMap.emplace(std::pair<MaplePhaseID, const MaplePhaseInfo *>(PI.GetPhaseID(), &PI)).second;
27     CHECK_FATAL(checkSucc, "Register Phase failed");
28 }
29 
GetPhaseByID(MaplePhaseID id)30 const MaplePhaseInfo *MaplePhaseRegister::GetPhaseByID(MaplePhaseID id)
31 {
32     if (passInfoMap.count(id)) {
33         return passInfoMap.find(id)->second;
34     } else {
35         CHECK_FATAL(false, "This phase has not been registered");
36         return passInfoMap.end()->second;
37     }
38 }
39 
ApplyTempMemPool()40 MemPool *MaplePhase::ApplyTempMemPool()
41 {
42     MemPool *res = memPoolCtrler.NewMemPool("temp Mempool", true);
43     tempMemPools.emplace_back(res);
44     return res;
45 }
46 
ClearTempMemPool()47 void MaplePhase::ClearTempMemPool()
48 {
49 #ifdef DEBUG
50     unsigned int maxMemPoolNum = 5;
51     DEBUG_ASSERT(tempMemPools.size() <= maxMemPoolNum, " maple phase uses too many temp mempool");
52 #endif
53     if (!tempMemPools.empty()) {
54         for (auto mpIt : tempMemPools) {
55             delete mpIt;
56             mpIt = nullptr;
57         }
58         tempMemPools.clear();
59     }
60 }
61 
62 /* default : do not require any phases */
AnalysisDepInit(AnalysisDep &aDep) const63 void MaplePhase::AnalysisDepInit(AnalysisDep &aDep) const
64 {
65     GetAnalysisDependence(aDep);
66 }
67 
GetAnalysisDependence(AnalysisDep &aDep) const68 void MaplePhase::GetAnalysisDependence(AnalysisDep &aDep) const
69 {
70     (void)aDep;
71     // do nothing if derived class not defined analysis dependence
72 }
73 
SetAnalysisInfoHook(AnalysisInfoHook *hook)74 void MaplePhase::SetAnalysisInfoHook(AnalysisInfoHook *hook)
75 {
76     analysisInfo = hook;
77 }
78 
GetAnalysisInfoHook()79 AnalysisInfoHook *MaplePhase::GetAnalysisInfoHook()
80 {
81     return analysisInfo;
82 }
83 }  // namespace maple
84