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 #ifndef MAPLE_PGO_INCLUDE_INSTRUMENT_H 16 #define MAPLE_PGO_INCLUDE_INSTRUMENT_H 17 18 #include "types_def.h" 19 #include "cfg_mst.h" 20 #include "mir_function.h" 21 22 namespace maple { 23 MIRSymbol *GetOrCreateFuncCounter(MIRFunction &func, uint32 elemCnt, uint32 cfgHash); 24 25 template <typename BB> 26 class BBEdge { 27 public: 28 BBEdge(BB *src, BB *dest, uint64 w = 1, bool isCritical = false, bool isFake = false) srcBB(src)29 : srcBB(src), destBB(dest), weight(w), inMST(false), isCritical(isCritical), isFake(isFake) 30 { 31 } 32 33 ~BBEdge() = default; 34 GetSrcBB()35 BB *GetSrcBB() 36 { 37 return srcBB; 38 } 39 GetDestBB()40 BB *GetDestBB() 41 { 42 return destBB; 43 } 44 GetWeight()45 uint64 GetWeight() const 46 { 47 return weight; 48 } 49 SetWeight(uint64 w)50 void SetWeight(uint64 w) 51 { 52 weight = w; 53 } 54 IsCritical()55 bool IsCritical() const 56 { 57 return isCritical; 58 } 59 IsFake()60 bool IsFake() const 61 { 62 return isFake; 63 } 64 IsInMST()65 bool IsInMST() const 66 { 67 return inMST; 68 } 69 SetInMST()70 void SetInMST() 71 { 72 inMST = true; 73 } 74 GetCondition()75 int32 GetCondition() const 76 { 77 return condition; 78 } 79 SetCondition(int32 cond)80 void SetCondition(int32 cond) 81 { 82 condition = cond; 83 } 84 IsBackEdge()85 bool IsBackEdge() const 86 { 87 return isBackEdge; 88 } 89 SetIsBackEdge()90 void SetIsBackEdge() 91 { 92 isBackEdge = true; 93 } 94 95 private: 96 BB *srcBB; 97 BB *destBB; 98 uint64 weight; 99 bool inMST; 100 bool isCritical; 101 bool isFake; 102 int32 condition = -1; 103 bool isBackEdge = false; 104 }; 105 106 template <typename BB> 107 class BBUseEdge : public BBEdge<BB> { 108 public: 109 BBUseEdge(BB *src, BB *dest, uint64 w = 1, bool isCritical = false, bool isFake = false) 110 : BBEdge<BB>(src, dest, w, isCritical, isFake) 111 { 112 } 113 virtual ~BBUseEdge() = default; SetCount(uint64 value)114 void SetCount(uint64 value) 115 { 116 countValue = value; 117 valid = true; 118 } 119 GetCount()120 uint64 GetCount() const 121 { 122 return countValue; 123 } 124 GetStatus()125 bool GetStatus() const 126 { 127 return valid; 128 } 129 130 private: 131 bool valid = false; 132 uint64 countValue = 0; 133 }; 134 135 template <class IRBB, class Edge> 136 class PGOInstrumentTemplate { 137 public: PGOInstrumentTemplate(MemPool & mp)138 explicit PGOInstrumentTemplate(MemPool &mp) : mst(mp) {} 139 140 void GetInstrumentBBs(std::vector<IRBB *> &bbs, IRBB *commonEntry) const; PrepareInstrumentInfo(IRBB * commonEntry,IRBB * commmonExit)141 void PrepareInstrumentInfo(IRBB *commonEntry, IRBB *commmonExit) 142 { 143 mst.ComputeMST(commonEntry, commmonExit); 144 } GetAllEdges()145 const MapleVector<Edge *> &GetAllEdges() 146 { 147 return mst.GetAllEdges(); 148 } 149 150 private: 151 CFGMST<Edge, IRBB> mst; 152 }; 153 154 template <typename BB> 155 class BBUseInfo { 156 public: BBUseInfo(MemPool & tmpPool)157 explicit BBUseInfo(MemPool &tmpPool) 158 : innerAlloc(&tmpPool), inEdges(innerAlloc.Adapter()), outEdges(innerAlloc.Adapter()) 159 { 160 } 161 virtual ~BBUseInfo() = default; SetCount(uint64 value)162 void SetCount(uint64 value) 163 { 164 countValue = value; 165 valid = true; 166 } GetCount()167 uint64 GetCount() const 168 { 169 return countValue; 170 } 171 GetStatus()172 bool GetStatus() const 173 { 174 return valid; 175 } 176 AddOutEdge(BBUseEdge<BB> * e)177 void AddOutEdge(BBUseEdge<BB> *e) 178 { 179 outEdges.push_back(e); 180 if (!e->GetStatus()) { 181 unknownOutEdges++; 182 } 183 } 184 AddInEdge(BBUseEdge<BB> * e)185 void AddInEdge(BBUseEdge<BB> *e) 186 { 187 inEdges.push_back(e); 188 if (!e->GetStatus()) { 189 unknownInEdges++; 190 } 191 } 192 GetInEdges()193 const MapleVector<BBUseEdge<BB> *> &GetInEdges() const 194 { 195 return inEdges; 196 } 197 GetInEdges()198 MapleVector<BBUseEdge<BB> *> &GetInEdges() 199 { 200 return inEdges; 201 } 202 GetInEdgeSize()203 size_t GetInEdgeSize() const 204 { 205 return inEdges.size(); 206 } 207 GetOutEdges()208 const MapleVector<BBUseEdge<BB> *> &GetOutEdges() const 209 { 210 return outEdges; 211 } 212 GetOutEdges()213 MapleVector<BBUseEdge<BB> *> &GetOutEdges() 214 { 215 return outEdges; 216 } 217 GetOutEdgeSize()218 size_t GetOutEdgeSize() const 219 { 220 return outEdges.size(); 221 } 222 DecreaseUnKnownOutEdges()223 void DecreaseUnKnownOutEdges() 224 { 225 unknownOutEdges--; 226 } 227 DecreaseUnKnownInEdges()228 void DecreaseUnKnownInEdges() 229 { 230 unknownInEdges--; 231 } 232 GetUnknownOutEdges()233 uint32 GetUnknownOutEdges() const 234 { 235 return unknownOutEdges; 236 } 237 238 BBUseEdge<BB> *GetOnlyUnknownOutEdges(); 239 GetUnknownInEdges()240 uint32 GetUnknownInEdges() const 241 { 242 return unknownInEdges; 243 } 244 245 BBUseEdge<BB> *GetOnlyUnknownInEdges(); 246 247 void Dump(); 248 249 private: 250 bool valid = false; 251 uint64 countValue = 0; 252 uint32 unknownInEdges = 0; 253 uint32 unknownOutEdges = 0; 254 MapleAllocator innerAlloc; 255 MapleVector<BBUseEdge<BB> *> inEdges; 256 MapleVector<BBUseEdge<BB> *> outEdges; 257 }; 258 } /* namespace maple */ 259 #endif // MAPLE_PGO_INCLUDE_INSTRUMENT_H 260