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_SSA_TAB_H 17 #define MAPLE_ME_INCLUDE_SSA_TAB_H 18 #include "mempool.h" 19 #include "mempool_allocator.h" 20 #include "ver_symbol.h" 21 #include "ssa_mir_nodes.h" 22 23 namespace maple { 24 25 class MeFunction; 26 27 class SSATab : public AnalysisResult { 28 // represent the SSA table 29 public: SSATab(MemPool * memPool,MemPool * versMp,MIRModule * mod,MeFunction * f)30 SSATab(MemPool *memPool, MemPool *versMp, MIRModule *mod, MeFunction *f) 31 : AnalysisResult(memPool), 32 mirModule(*mod), 33 func(f), 34 versAlloc(versMp), 35 versionStTable(*versMp), 36 originalStTable(*memPool, *mod), 37 stmtsSSAPart(versMp), 38 defBBs4Ost(16, nullptr, versAlloc.Adapter()) // preallocation 16 element memory 39 { 40 } 41 42 ~SSATab() = default; 43 44 BaseNode *CreateSSAExpr(BaseNode *expr); HasDefBB(OStIdx oidx)45 bool HasDefBB(OStIdx oidx) 46 { 47 return oidx < defBBs4Ost.size() && defBBs4Ost[oidx] && !defBBs4Ost[oidx]->empty(); 48 } AddDefBB4Ost(OStIdx oidx,BBId bbid)49 void AddDefBB4Ost(OStIdx oidx, BBId bbid) 50 { 51 if (oidx >= defBBs4Ost.size()) { 52 defBBs4Ost.resize(oidx + k16BitSize, nullptr); // expand 16 element memory 53 } 54 if (defBBs4Ost[oidx] == nullptr) { 55 defBBs4Ost[oidx] = versAlloc.GetMemPool()->New<MapleSet<BBId>>(versAlloc.Adapter()); 56 } 57 (void)defBBs4Ost[oidx]->insert(bbid); 58 } GetDefBBs4Ost(OStIdx oidx)59 MapleSet<BBId> *GetDefBBs4Ost(OStIdx oidx) 60 { 61 return defBBs4Ost[oidx]; 62 } GetVerSt(size_t verIdx)63 VersionSt *GetVerSt(size_t verIdx) 64 { 65 return versionStTable.GetVersionStVectorItem(verIdx); 66 } 67 GetVersAlloc()68 MapleAllocator &GetVersAlloc() 69 { 70 return versAlloc; 71 } 72 73 // following are handles to methods in originalStTable CreateSymbolOriginalSt(MIRSymbol & mirSt,PUIdx puIdx,FieldID fld)74 OriginalSt *CreateSymbolOriginalSt(MIRSymbol &mirSt, PUIdx puIdx, FieldID fld) 75 { 76 return originalStTable.CreateSymbolOriginalSt(mirSt, puIdx, fld); 77 } 78 FindOrCreateSymbolOriginalSt(MIRSymbol & mirSt,PUIdx puIdx,FieldID fld)79 OriginalSt *FindOrCreateSymbolOriginalSt(MIRSymbol &mirSt, PUIdx puIdx, FieldID fld) 80 { 81 return originalStTable.FindOrCreateSymbolOriginalSt(mirSt, puIdx, fld); 82 } 83 FindOrCreateAddrofSymbolOriginalSt(OriginalSt * ost)84 OriginalSt *FindOrCreateAddrofSymbolOriginalSt(OriginalSt *ost) 85 { 86 CHECK_FATAL(ost, "ost is nullptr!"); 87 auto *addrofOst = ost->GetPrevLevelOst(); 88 if (ost->GetPrevLevelOst() != nullptr) { 89 return addrofOst; 90 } 91 addrofOst = originalStTable.FindOrCreateAddrofSymbolOriginalSt(ost); 92 auto *zeroVersionSt = versionStTable.GetOrCreateZeroVersionSt(*addrofOst); 93 originalStTable.AddNextLevelOstOfVst(zeroVersionSt, ost); 94 ost->SetPointerVst(zeroVersionSt); 95 return addrofOst; 96 } 97 GetOriginalStFromID(OStIdx id)98 const OriginalSt *GetOriginalStFromID(OStIdx id) const 99 { 100 return originalStTable.GetOriginalStFromID(id); 101 } GetOriginalStFromID(OStIdx id)102 OriginalSt *GetOriginalStFromID(OStIdx id) 103 { 104 return originalStTable.GetOriginalStFromID(id); 105 } 106 GetSymbolOriginalStFromID(OStIdx id)107 const OriginalSt *GetSymbolOriginalStFromID(OStIdx id) const 108 { 109 const OriginalSt *ost = originalStTable.GetOriginalStFromID(id); 110 DEBUG_ASSERT(ost->IsSymbolOst(), "GetSymbolOriginalStFromid: id has wrong ost type"); 111 return ost; 112 } GetSymbolOriginalStFromID(OStIdx id)113 OriginalSt *GetSymbolOriginalStFromID(OStIdx id) 114 { 115 OriginalSt *ost = originalStTable.GetOriginalStFromID(id); 116 DEBUG_ASSERT(ost->IsSymbolOst() || ost->GetIndirectLev() > 0, 117 "GetSymbolOriginalStFromid: id has wrong ost type"); 118 return ost; 119 } 120 GetNextLevelOsts(const VersionSt & vst)121 MapleVector<OriginalSt *> *GetNextLevelOsts(const VersionSt &vst) const 122 { 123 return originalStTable.GetNextLevelOstsOfVst(vst.GetIndex()); 124 } 125 GetNextLevelOsts(size_t vstIdx)126 MapleVector<OriginalSt *> *GetNextLevelOsts(size_t vstIdx) const 127 { 128 return originalStTable.GetNextLevelOstsOfVst(vstIdx); 129 } 130 GetPrimType(OStIdx idx)131 PrimType GetPrimType(OStIdx idx) const 132 { 133 const MIRSymbol *symbol = GetMIRSymbolFromID(idx); 134 return symbol->GetType()->GetPrimType(); 135 } 136 GetMIRSymbolFromID(OStIdx id)137 const MIRSymbol *GetMIRSymbolFromID(OStIdx id) const 138 { 139 return originalStTable.GetMIRSymbolFromID(id); 140 } GetMIRSymbolFromID(OStIdx id)141 MIRSymbol *GetMIRSymbolFromID(OStIdx id) 142 { 143 return originalStTable.GetMIRSymbolFromID(id); 144 } 145 GetVersionStTable()146 VersionStTable &GetVersionStTable() 147 { 148 return versionStTable; 149 } 150 GetVersionStTableSize()151 size_t GetVersionStTableSize() const 152 { 153 return versionStTable.GetVersionStVectorSize(); 154 } 155 GetOriginalStTable()156 OriginalStTable &GetOriginalStTable() 157 { 158 return originalStTable; 159 } 160 GetOriginalStTable()161 const OriginalStTable &GetOriginalStTable() const 162 { 163 return originalStTable; 164 } 165 GetOriginalStTableSize()166 size_t GetOriginalStTableSize() const 167 { 168 return originalStTable.Size(); 169 } 170 GetStmtsSSAPart()171 StmtsSSAPart &GetStmtsSSAPart() 172 { 173 return stmtsSSAPart; 174 } GetStmtsSSAPart()175 const StmtsSSAPart &GetStmtsSSAPart() const 176 { 177 return stmtsSSAPart; 178 } 179 180 // should check HasSSAUse first GetStmtMayUseNodes(const StmtNode & stmt)181 const TypeOfMayUseList &GetStmtMayUseNodes(const StmtNode &stmt) const 182 { 183 return stmtsSSAPart.SSAPartOf(stmt)->GetMayUseNodes(); 184 } 185 // should check IsCallAssigned first GetStmtMustDefNodes(const StmtNode & stmt)186 MapleVector<MustDefNode> &GetStmtMustDefNodes(const StmtNode &stmt) 187 { 188 return stmtsSSAPart.GetMustDefNodesOf(stmt); 189 } 190 IsWholeProgramScope()191 bool IsWholeProgramScope() const 192 { 193 return wholeProgramScope; 194 } 195 SetWholeProgramScope(bool val)196 void SetWholeProgramScope(bool val) 197 { 198 wholeProgramScope = val; 199 } 200 GetModule()201 MIRModule &GetModule() const 202 { 203 return mirModule; 204 } 205 206 void SetEPreLocalRefVar(const OStIdx &ostIdx, bool epreLocalrefvarPara = true) 207 { 208 originalStTable.SetEPreLocalRefVar(ostIdx, epreLocalrefvarPara); 209 } 210 SetZeroVersionIndex(const OStIdx & ostIdx,size_t zeroVersionIndexParam)211 void SetZeroVersionIndex(const OStIdx &ostIdx, size_t zeroVersionIndexParam) 212 { 213 originalStTable.SetZeroVersionIndex(ostIdx, zeroVersionIndexParam); 214 } 215 GetVersionsIndicesSize(const OStIdx & ostIdx)216 size_t GetVersionsIndicesSize(const OStIdx &ostIdx) const 217 { 218 return originalStTable.GetVersionsIndicesSize(ostIdx); 219 } 220 UpdateVarOstMap(const OStIdx & ostIdx,std::map<OStIdx,OriginalSt * > & varOstMap)221 void UpdateVarOstMap(const OStIdx &ostIdx, std::map<OStIdx, OriginalSt *> &varOstMap) 222 { 223 originalStTable.UpdateVarOstMap(ostIdx, varOstMap); 224 } 225 226 // MIRSymbol query GetStmtMIRSymbol(const StmtNode & stmt)227 const MIRSymbol &GetStmtMIRSymbol(const StmtNode &stmt) const 228 { 229 return *(GetStmtsSSAPart().GetAssignedVarOf(stmt)->GetOst()->GetMIRSymbol()); 230 } 231 IsInitVersion(size_t vstIdx,const OStIdx & ostIdx)232 bool IsInitVersion(size_t vstIdx, const OStIdx &ostIdx) const 233 { 234 auto *ost = GetOriginalStFromID(ostIdx); 235 DEBUG_ASSERT(ost != nullptr, "null pointer check"); 236 return ost->GetZeroVersionIndex() == vstIdx; 237 } 238 239 void CollectIterNextLevel(size_t vstIdx, std::set<OriginalSt *> &resultOsts); 240 241 private: 242 MIRModule &mirModule; 243 MeFunction *func; 244 MapleAllocator versAlloc; 245 VersionStTable versionStTable; // this uses special versMp because it will be freed earlier 246 OriginalStTable originalStTable; 247 StmtsSSAPart stmtsSSAPart; // this uses special versMp because it will be freed earlier 248 MapleVector<MapleSet<BBId> *> defBBs4Ost; // gives the set of BBs that has def for each original symbol 249 bool wholeProgramScope = false; 250 }; 251 } // namespace maple 252 #endif // MAPLE_ME_INCLUDE_SSA_TAB_H 253