• 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 MAPLE_PHASE_INCLUDE_MAPLE_PHASE_SUPPORT_H
17 #define MAPLE_PHASE_INCLUDE_MAPLE_PHASE_SUPPORT_H
18 #include <map>
19 #include <string>
20 #include <iostream>
21 #include "mempool.h"
22 #include "maple_string.h"
23 #include "mpl_timer.h"
24 #include "mempool_allocator.h"
25 #include "option.h"
26 
27 namespace maple {
28 using MaplePhaseID = const void *;
29 class MaplePhase;
30 typedef MaplePhase *(*MaplePhase_t)(MemPool *);
31 
32 // base class of analysisPhase's result
33 class AnalysisResult {
34 public:
AnalysisResult(MemPool * memPoolParam)35     explicit AnalysisResult(MemPool *memPoolParam)
36     {
37         DEBUG_ASSERT(memPoolParam != nullptr, "memPoolParam is null in AnalysisResult::AnalysisResult");
38         memPool = memPoolParam;
39     }
40 
41     virtual ~AnalysisResult() = default;
42 
GetMempool()43     MemPool *GetMempool() const
44     {
45         return memPool;
46     }
47 
EraseMemPool()48     void EraseMemPool()
49     {
50         delete memPool;
51         memPool = nullptr;
52     }
53 
54 protected:
55     MemPool *memPool;
56 };
57 
58 /* record every phase known by the system */
59 class MaplePhaseInfo {
60 public:
MaplePhaseInfo(const std::string & pName,MaplePhaseID pID,bool isAS,bool isCFGonly,bool canSkip)61     MaplePhaseInfo(const std::string &pName, MaplePhaseID pID, bool isAS, bool isCFGonly, bool canSkip)
62         : phaseName(pName), phaseID(pID), isAnalysis(isAS), isCFGOnlyPass(isCFGonly), canSkip(canSkip)
63     {
64     }
65 
~MaplePhaseInfo()66     ~MaplePhaseInfo()
67     {
68         constructor = nullptr;
69         phaseID = nullptr;
70     };
GetPhaseID()71     MaplePhaseID GetPhaseID() const
72     {
73         return phaseID;
74     }
SetConstructor(MaplePhase_t newConstructor)75     void SetConstructor(MaplePhase_t newConstructor)
76     {
77         constructor = newConstructor;
78     }
GetConstructor()79     MaplePhase_t GetConstructor() const
80     {
81         return constructor;
82     }
IsAnalysis()83     bool IsAnalysis() const
84     {
85         return isAnalysis;
86     };
IsCFGonly()87     bool IsCFGonly() const
88     {
89         return isCFGOnlyPass;
90     };
PhaseName()91     std::string PhaseName() const
92     {
93         return phaseName;
94     }
CanSkip()95     bool CanSkip() const
96     {
97         return canSkip;
98     }
99     MaplePhase_t constructor = nullptr;
100 
101 private:
102     std::string phaseName;
103     MaplePhaseID phaseID;
104     const bool isAnalysis;
105     const bool isCFGOnlyPass;
106     const bool canSkip;
107 };
108 
109 class PhaseTimeHandler {
110 public:
111     PhaseTimeHandler(MemPool &memPool, uint32 threadNum = 1)
112         : allocator(&memPool),
113           phaseTimeRecord(allocator.Adapter()),
114           originOrder(allocator.Adapter()),
115           multiTimers(allocator.Adapter())
116     {
117         if (threadNum > 1) {
118             isMultithread = true;
119         }
120     }
121     virtual ~PhaseTimeHandler() = default;
122     void RunBeforePhase(const MaplePhaseInfo &pi);
123     void RunAfterPhase(const MaplePhaseInfo &pi);
124     void DumpPhasesTime();
125 
126 private:
127     MapleAllocator allocator;
128     MapleMap<std::string, long> phaseTimeRecord;
129     MapleVector<MapleMap<std::string, long>::iterator> originOrder;
130     long phaseTotalTime = 0;
131     MPLTimer timer;
132     bool isMultithread = false;
133     MapleMap<std::thread::id, MPLTimer *> multiTimers;
134 };
135 
136 // usasge :: analysis dependency
137 class AnalysisDep {
138 public:
AnalysisDep(MemPool & mp)139     explicit AnalysisDep(MemPool &mp)
140         : allocator(&mp),
141           required(allocator.Adapter()),
142           preserved(allocator.Adapter()),
143           preservedExcept(allocator.Adapter()) {};
144     virtual ~AnalysisDep() = default;
145     template <class PhaseT>
AddRequired()146     void AddRequired()
147     {
148         required.emplace_back(&PhaseT::id);
149     }
150     template <class PhaseT>
AddPreserved()151     void AddPreserved()
152     {
153         (void)preserved.emplace(&PhaseT::id);
154     }
155     template <class PhaseT>
PreservedAllExcept()156     void PreservedAllExcept()
157     {
158         SetPreservedAll();
159         (void)preservedExcept.emplace(&PhaseT::id);
160     }
SetPreservedAll()161     void SetPreservedAll()
162     {
163         preservedAll = true;
164     }
GetPreservedAll()165     bool GetPreservedAll() const
166     {
167         return preservedAll;
168     }
FindIsPreserved(const MaplePhaseID pid)169     bool FindIsPreserved(const MaplePhaseID pid)
170     {
171         return preserved.find(pid) != preserved.end();
172     }
173     const MapleVector<MaplePhaseID> &GetRequiredPhase() const;
174     const MapleSet<MaplePhaseID> &GetPreservedPhase() const;
175     const MapleSet<MaplePhaseID> &GetPreservedExceptPhase() const;
176 
177 private:
178     MapleAllocator allocator;
179     MapleVector<MaplePhaseID> required;
180     MapleSet<MaplePhaseID> preserved;        // keep analysis result as it is
181     MapleSet<MaplePhaseID> preservedExcept;  // keep analysis result except
182     bool preservedAll = false;
183 };
184 }  // namespace maple
185 #endif  // MAPLE_PHASE_SUPPORT_H
186