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_ME_INCLUDE_VER_SYMBOL_H 17 #define MAPLE_ME_INCLUDE_VER_SYMBOL_H 18 #include <iostream> 19 #include "mir_module.h" 20 #include "mir_symbol.h" 21 #include "orig_symbol.h" 22 23 namespace maple { 24 class BB; // circular dependency exists, no other choice 25 class PhiNode; // circular dependency exists, no other choice 26 class MayDefNode; // circular dependency exists, no other choice 27 class MustDefNode; // circular dependency exists, no other choice 28 class VersionStTable; // circular dependency exists, no other choice 29 class OriginalSt; // circular dependency exists, no other choice 30 31 constexpr size_t kInvalidVstIdx = 0; 32 class VersionSt { 33 public: 34 enum DefType { kUnknown, kAssign, kPhi, kMayDef, kMustDef }; 35 VersionSt(size_t index,uint32 version,OriginalSt * ost)36 VersionSt(size_t index, uint32 version, OriginalSt *ost) : index(index), version(version), ost(ost), defStmt() {} 37 38 ~VersionSt() = default; 39 GetIndex()40 size_t GetIndex() const 41 { 42 return index; 43 } 44 GetVersion()45 int GetVersion() const 46 { 47 return version; 48 } 49 SetDefBB(BB * defbb)50 void SetDefBB(BB *defbb) 51 { 52 defBB = defbb; 53 } 54 GetDefBB()55 BB *GetDefBB() const 56 { 57 return defBB; 58 } 59 60 void DumpDefStmt(const MIRModule *mod) const; 61 IsInitVersion()62 bool IsInitVersion() const 63 { 64 return version == kInitVersion; 65 } 66 GetDefType()67 DefType GetDefType() const 68 { 69 return defType; 70 } 71 SetDefType(DefType defType)72 void SetDefType(DefType defType) 73 { 74 this->defType = defType; 75 } 76 GetOrigIdx()77 OStIdx GetOrigIdx() const 78 { 79 return ost->GetIndex(); 80 } 81 GetOst()82 OriginalSt *GetOst() const 83 { 84 return ost; 85 } 86 SetOst(OriginalSt * originalSt)87 void SetOst(OriginalSt *originalSt) 88 { 89 this->ost = originalSt; 90 } 91 GetAssignNode()92 const StmtNode *GetAssignNode() const 93 { 94 return defStmt.assign; 95 } 96 GetAssignNode()97 StmtNode *GetAssignNode() 98 { 99 return defStmt.assign; 100 } 101 SetAssignNode(StmtNode * assignNode)102 void SetAssignNode(StmtNode *assignNode) 103 { 104 defStmt.assign = assignNode; 105 } 106 GetPhi()107 const PhiNode *GetPhi() const 108 { 109 return defStmt.phi; 110 } GetPhi()111 PhiNode *GetPhi() 112 { 113 return defStmt.phi; 114 } SetPhi(PhiNode * phiNode)115 void SetPhi(PhiNode *phiNode) 116 { 117 defStmt.phi = phiNode; 118 } 119 GetMayDef()120 const MayDefNode *GetMayDef() const 121 { 122 return defStmt.mayDef; 123 } GetMayDef()124 MayDefNode *GetMayDef() 125 { 126 return defStmt.mayDef; 127 } SetMayDef(MayDefNode * mayDefNode)128 void SetMayDef(MayDefNode *mayDefNode) 129 { 130 defStmt.mayDef = mayDefNode; 131 } 132 GetMustDef()133 const MustDefNode *GetMustDef() const 134 { 135 return defStmt.mustDef; 136 } GetMustDef()137 MustDefNode *GetMustDef() 138 { 139 return defStmt.mustDef; 140 } SetMustDef(MustDefNode * mustDefNode)141 void SetMustDef(MustDefNode *mustDefNode) 142 { 143 defStmt.mustDef = mustDefNode; 144 } 145 IsReturn()146 bool IsReturn() const 147 { 148 return isReturn; 149 } 150 151 void Dump(bool omitName = false) const 152 { 153 if (!omitName) { 154 ost->Dump(); 155 } 156 LogInfo::MapleLogger() << "(vno" << version << "|vstIdx:" << index << ")"; 157 } 158 DefByMayDef()159 bool DefByMayDef() const 160 { 161 return defType == kMayDef; 162 } 163 164 private: 165 size_t index; // index number in versionst_table_ 166 int version; // starts from 0 for each symbol 167 OriginalSt *ost; // the index of related originalst in originalst_table 168 BB *defBB = nullptr; 169 DefType defType = kUnknown; 170 171 union DefStmt { 172 StmtNode *assign; 173 PhiNode *phi; 174 MayDefNode *mayDef; 175 MustDefNode *mustDef; 176 } defStmt; // only valid after SSA 177 178 bool isReturn = false; // the symbol will return in its function 179 }; 180 181 class VersionStTable { 182 public: VersionStTable(MemPool & vstMp)183 explicit VersionStTable(MemPool &vstMp) : vstAlloc(&vstMp), versionStVector(vstAlloc.Adapter()) 184 { 185 versionStVector.push_back(static_cast<VersionSt *>(nullptr)); 186 } 187 188 using VersionStContainer = MapleVector<VersionSt *>; 189 using VersionStIterator = VersionStContainer::iterator; 190 using ConstVersionStIterator = VersionStContainer::const_iterator; 191 192 ~VersionStTable() = default; 193 194 VersionSt *CreateNextVersionSt(OriginalSt *ost); 195 196 void CreateZeroVersionSt(OriginalSt *ost); 197 VersionSt *GetOrCreateZeroVersionSt(OriginalSt &ost); 198 GetZeroVersionSt(const OriginalSt * ost)199 VersionSt *GetZeroVersionSt(const OriginalSt *ost) const 200 { 201 CHECK_FATAL(ost, "ost is nullptr!"); 202 CHECK_FATAL(!ost->GetVersionsIndices().empty(), "GetZeroVersionSt:: zero version has not been created"); 203 return versionStVector[ost->GetZeroVersionIndex()]; 204 } 205 GetVersionStVectorSize()206 size_t GetVersionStVectorSize() const 207 { 208 return versionStVector.size(); 209 } 210 GetVersionStVectorItem(size_t index)211 VersionSt *GetVersionStVectorItem(size_t index) const 212 { 213 CHECK_FATAL(index < versionStVector.size(), "out of range"); 214 return versionStVector[index]; 215 } 216 SetVersionStVectorItem(size_t index,VersionSt * vst)217 void SetVersionStVectorItem(size_t index, VersionSt *vst) 218 { 219 CHECK_FATAL(index < versionStVector.size(), "out of range"); 220 versionStVector[index] = vst; 221 } 222 GetVSTAlloc()223 MapleAllocator &GetVSTAlloc() 224 { 225 return vstAlloc; 226 } 227 228 void Dump(const MIRModule *mod) const; 229 begin()230 ConstVersionStIterator begin() const 231 { 232 auto it = versionStVector.begin(); 233 // first element in versionStVector is null. 234 ++it; 235 return it; 236 } 237 begin()238 VersionStIterator begin() 239 { 240 auto it = versionStVector.begin(); 241 // first element in versionStVector is null. 242 ++it; 243 return it; 244 } 245 end()246 ConstVersionStIterator end() const 247 { 248 return versionStVector.end(); 249 } 250 end()251 VersionStIterator end() 252 { 253 return versionStVector.end(); 254 } 255 256 private: 257 MapleAllocator vstAlloc; // this stores versionStVector 258 MapleVector<VersionSt *> versionStVector; // the vector that map a versionst's index to its pointer 259 }; 260 } // namespace maple 261 #endif // MAPLE_ME_INCLUDE_VER_SYMBOL_H 262