From 5eb096d7bdc6d3bd4f55035872c25ebb54652d11 Mon Sep 17 00:00:00 2001 From: Yi Jiang Date: Thu, 21 May 2020 11:10:36 -0700 Subject: [PATCH] Seperate Ref and Ptr in type table --- src/maple_ir/include/global_tables.h | 6 +++- src/maple_ir/src/global_tables.cpp | 44 +++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/maple_ir/include/global_tables.h b/src/maple_ir/include/global_tables.h index f4b70e0b77..54b88bab57 100644 --- a/src/maple_ir/include/global_tables.h +++ b/src/maple_ir/include/global_tables.h @@ -114,7 +114,9 @@ class TypeTable { void SetTypeWithTyIdx(const TyIdx &tyIdx, MIRType &type); - TyIdx GetOrCreateMIRType(MIRType *pType); + MIRType *CreateAndUpdateMirTypeNode(MIRType *pType); + MIRType *GetOrCreateMIRTypeNode(MIRType *ptype); + TyIdx GetOrCreateMIRType(MIRType *pType) { return GetOrCreateMIRTypeNode(pType)->GetTypeIndex(); } size_t GetTypeTableSize() const { return typeTable.size(); @@ -357,6 +359,8 @@ class TypeTable { MIRType *GetOrCreateClassOrInterface(const std::string &name, MIRModule &module, bool forClass); std::unordered_set typeHashTable; + std::unordered_map ptrTypeMap; + std::unordered_map refTypeMap; std::vector typeTable; }; diff --git a/src/maple_ir/src/global_tables.cpp b/src/maple_ir/src/global_tables.cpp index 4a0af15f29..ec82e28476 100644 --- a/src/maple_ir/src/global_tables.cpp +++ b/src/maple_ir/src/global_tables.cpp @@ -27,7 +27,7 @@ MIRType *TypeTable::CreateMirType(uint32 primTypeIdx) const { return mirType; } -TypeTable::TypeTable() { +TypeTable::TypeTable() : ptrTypeMap(), refTypeMap() { // enter the primitve types in type_table_ typeTable.push_back(static_cast(nullptr)); ASSERT(typeTable.size() == static_cast(PTY_void), "use PTY_void as the first index to type table"); @@ -63,15 +63,43 @@ void TypeTable::PutToHashTable(MIRType *mirType) { typeHashTable.insert(mirType); } -TyIdx TypeTable::GetOrCreateMIRType(MIRType *pType) { - const auto it = typeHashTable.find(pType); - if (it != typeHashTable.end()) { - return (*it)->GetTypeIndex(); +MIRType *TypeTable::CreateAndUpdateMirTypeNode(MIRType *ptype) { + MIRType *ntype = ptype->CopyMIRTypeNode(); + ntype->SetTypeIndex(TyIdx(typeTable.size())); + typeTable.push_back(ntype); + + if (ptype->GetKind() == kTypePointer) { + MIRPtrType *pty = static_cast(ptype); + if (pty->GetPrimType() == PTY_ptr) { + ptrTypeMap[pty->GetPointedTyIdx()] = ntype->GetTypeIndex(); + } else { + refTypeMap[pty->GetPointedTyIdx()] = ntype->GetTypeIndex(); + } + } else { + typeHashTable.insert(ntype); + } + return ntype; +} + +MIRType* TypeTable::GetOrCreateMIRTypeNode(MIRType *ptype) { + if (ptype->GetKind() == kTypePointer) { + MIRPtrType *type = static_cast(ptype); + auto *pMap = (type->GetPrimType() == PTY_ptr ? &ptrTypeMap : &refTypeMap); + auto *otherPMap = (type->GetPrimType() == PTY_ref ? &ptrTypeMap : &refTypeMap); + auto it = pMap->find(type->GetPointedTyIdx()); + if (it != pMap->end()) { + return GetTypeFromTyIdx(it->second); + } + CHECK_FATAL(!((PrimType)(type->GetPointedTyIdx().GetIdx()) >= kPtyDerived && type->GetPrimType() == PTY_ref && + otherPMap->find(type->GetPointedTyIdx()) != otherPMap->end()), "GetOrCreateMIRType: ref pointed-to type %d has previous ptr occurrence", type->GetPointedTyIdx().GetIdx()); + return CreateAndUpdateMirTypeNode(ptype); } - MIRType *newTy = CreateType(*pType); - PutToHashTable(newTy); - return newTy->GetTypeIndex(); + auto it = typeHashTable.find(ptype); + if (it != typeHashTable.end()) { + return *it; + } + return CreateAndUpdateMirTypeNode(ptype); } MIRType *TypeTable::voidPtrType = nullptr; -- Gitee