diff --git a/src/bin/jbc2mpl b/src/bin/jbc2mpl index bf225a67b8f2693d4c96ec724b5adf0f046e977f..72245affccde0759788be6263f2edfa91305f718 100755 Binary files a/src/bin/jbc2mpl and b/src/bin/jbc2mpl differ diff --git a/src/bin/maple b/src/bin/maple index 1092b627b9e4119c14a74c1a4591562ac8fd8448..5aab2e9d7002113e58fd002adbe59d1e30371892 100755 Binary files a/src/bin/maple and b/src/bin/maple differ diff --git a/src/deplibs/libmempool.a b/src/deplibs/libmempool.a index ae96249a2a597d9bbcac2c7ec3355fbc74878f1b..3cfd8646c9069be63e58585b3e3e0ff75054a41b 100644 Binary files a/src/deplibs/libmempool.a and b/src/deplibs/libmempool.a differ diff --git a/src/deplibs/libmplphase.a b/src/deplibs/libmplphase.a index e459563de06c47cc7794c8f7c2bb9a042d8a6627..83f0173bed4525f5d4bc7fdfffcb27fd2d233ed8 100644 Binary files a/src/deplibs/libmplphase.a and b/src/deplibs/libmplphase.a differ diff --git a/src/maple_ipa/src/callgraph.cpp b/src/maple_ipa/src/callgraph.cpp index 3429523c5787c4ab29756d0db3a301173917eecc..0f6a94554fe1e31e6bfc43f65b6972881d9be31b 100644 --- a/src/maple_ipa/src/callgraph.cpp +++ b/src/maple_ipa/src/callgraph.cpp @@ -447,7 +447,7 @@ void CallGraph::HandleBody(MIRFunction *func, BlockNode *body, CGNode *node, uin CallInfo *callInfo = GenCallInfo(kCallTypeVirtualCall, calleefunc, stmt, loopDepth, stmt->GetStmtID()); // Retype makes object type more inaccurate. StmtNode *stmtPrev = static_cast(stmt)->GetPrev(); - if (stmtPrev->GetOpCode() == OP_dassign) { + if (stmtPrev && stmtPrev->GetOpCode() == OP_dassign) { DassignNode *dassignNode = static_cast(stmtPrev); if (dassignNode->GetRHS()->GetOpCode() == OP_retype) { CallNode *callNode = static_cast(stmt); diff --git a/src/maple_ipa/src/clone.cpp b/src/maple_ipa/src/clone.cpp index a76c2708771447ebd3d347c6e3978242b143c3d6..c78c73a93d09e9abd74265ef215618c3db7037da 100644 --- a/src/maple_ipa/src/clone.cpp +++ b/src/maple_ipa/src/clone.cpp @@ -53,7 +53,7 @@ std::string ReplaceRetIgnored::GenerateNewFullName(const MIRFunction *originalFu } MIRSymbol *Clone::CloneLocalSymbol(const MIRSymbol *oldSym, MIRFunction *newFunc) { - MemPool *newMP = newFunc->GetMemPool(); + MemPool *newMP = newFunc->GetDataMemPool(); MIRSymbol *newSym = newMP->New(*oldSym); if (oldSym->GetSKind() == kStConst) { newSym->SetKonst(oldSym->GetKonst()->Clone(*newMP)); diff --git a/src/maple_ir/include/global_tables.h b/src/maple_ir/include/global_tables.h index 250be9c70235ea93199e4ce7f6d8c8768c667265..2b752cd58f07da6753cd7e017b9a2c08ee34a231 100644 --- a/src/maple_ir/include/global_tables.h +++ b/src/maple_ir/include/global_tables.h @@ -55,6 +55,30 @@ class UStrIdxHash { } }; +class IntConstKey { + friend class IntConstHash; + friend class IntConstCmp; + public: + IntConstKey(int64 v, uint64 id) : val(v), tyidxFieldID(id) {} + private: + int64 val; + uint64 tyidxFieldID; +}; + +class IntConstHash { + public: + std::size_t operator() (const IntConstKey &key) const { + return std::hash{}(key.val) ^ (std::hash{}(key.tyidxFieldID) << 1); + } +}; + +class IntConstCmp { + public: + bool operator() (const IntConstKey &lkey, const IntConstKey &rkey) const { + return lkey.val == rkey.val && lkey.tyidxFieldID == rkey.tyidxFieldID; + } +}; + class TypeTable { public: static MIRType *voidPtrType; @@ -421,6 +445,24 @@ class FPConstTable { MIRDoubleConst *minusZeroDoubleConst = nullptr; }; +class IntConstTable { + public: + IntConstTable(const IntConstTable &p) = delete; + IntConstTable &operator=(const IntConstTable &p) = delete; + ~IntConstTable(); + + MIRIntConst *GetOrCreateIntConst(int64 val, MIRType &type, uint32 fieldID = 0); + + static std::unique_ptr Create() { + auto p = std::unique_ptr(new IntConstTable()); + return p; + } + + private: + IntConstTable() = default; + std::unordered_map intConstTable; +}; + // STypeNameTable is only used to store class and interface types. // Each module maintains its own MIRTypeNameTable. class STypeNameTable { @@ -603,13 +645,18 @@ class GlobalTables { return globalTables.constPool; } + static IntConstTable &GetIntConstTable() { + return *(globalTables.intConstTablePtr); + } + GlobalTables(const GlobalTables &globalTables) = delete; GlobalTables(const GlobalTables &&globalTables) = delete; GlobalTables &operator=(const GlobalTables &globalTables) = delete; GlobalTables &operator=(const GlobalTables &&globalTables) = delete; private: - GlobalTables() : fpConstTablePtr(FPConstTable::Create()) { + GlobalTables() : fpConstTablePtr(FPConstTable::Create()), + intConstTablePtr(IntConstTable::Create()) { gStringTable.Init(); uStrTable.Init(); u16StringTable.Init(); @@ -623,6 +670,7 @@ class GlobalTables { GSymbolTable gSymbolTable; ConstPool constPool; std::unique_ptr fpConstTablePtr; + std::unique_ptr intConstTablePtr; StringTable gStringTable; StringTable uStrTable; StringTable u16StringTable; diff --git a/src/maple_ir/include/mir_const.h b/src/maple_ir/include/mir_const.h index 755ccca980c5b8f7b143b00862905c6e9c76ae83..fa4524404ba1f82f7d7eeb5145abcbcf9d29e90a 100644 --- a/src/maple_ir/include/mir_const.h +++ b/src/maple_ir/include/mir_const.h @@ -50,7 +50,8 @@ class MIRConst { return fieldID; } - void SetFieldID(uint32 fieldIdx) { + virtual void SetFieldID(uint32 fieldIdx) { + CHECK_FATAL(kind != kConstInt, "must be"); fieldID = fieldIdx; } @@ -90,6 +91,7 @@ class MIRConst { private: MIRType &type; MIRConstKind kind; + protected: uint32 fieldID; }; @@ -127,7 +129,7 @@ class MIRIntConst : public MIRConst { return value == -1 && IsPrimitiveInteger(GetType().GetPrimType()); }; void Neg() override { - value = -value; + CHECK_FATAL(false, "Can't Use This Interface in This Object"); } int64 GetValue() const { @@ -135,13 +137,18 @@ class MIRIntConst : public MIRConst { } void SetValue(int64 val) { - value = val; + CHECK_FATAL(false, "Can't Use This Interface in This Object"); + } + + void SetFieldID(uint32 fieldIdx) override { + CHECK_FATAL(false, "Can't Use This Interface in This Object"); } bool operator==(const MIRConst &rhs) const override; MIRIntConst *Clone(MemPool &memPool) const override { - return memPool.New(*this); + CHECK_FATAL(false, "Can't Use This Interface in This Object"); + return nullptr; } private: diff --git a/src/maple_ir/include/mir_function.h b/src/maple_ir/include/mir_function.h index a5b7c6d68d25029080ee42ea10bc3e5438218d2c..6209b2cd039efd663aa9a6e61aa085b035629225 100644 --- a/src/maple_ir/include/mir_function.h +++ b/src/maple_ir/include/mir_function.h @@ -379,14 +379,23 @@ class MIRFunction { void ResetGDBEnv(); MemPool *GetCodeMempool() { + if (codeMemPool == nullptr) { + codeMemPool = memPoolCtrler.NewMemPool("func code mempool"); + codeMemPoolAllocator.SetMemPool(codeMemPool); + } return codeMemPool; } MapleAllocator &GetCodeMemPoolAllocator() { + GetCodeMempool(); return codeMemPoolAllocator; } MapleAllocator &GetCodeMempoolAllocator() { + if (codeMemPool == nullptr) { + codeMemPool = memPoolCtrler.NewMemPool("func code mempool"); + codeMemPoolAllocator.SetMemPool(codeMemPool); + } return codeMemPoolAllocator; } @@ -452,7 +461,9 @@ class MIRFunction { void ClearArgumentsAttrs() { argumentsAttrs.clear(); } - + bool HaveTypeNameTab() { + return typeNameTab != nullptr; + } const MapleMap &GetGStrIdxToTyIdxMap() const { return typeNameTab->GetGStrIdxToTyIdxMap(); } @@ -483,10 +494,6 @@ class MIRFunction { return pregTab->PregFromPregIdx(idx); } - MemPool *GetMemPool() { - return dataMemPool; - } - BlockNode *GetBody() { return body; } @@ -549,11 +556,21 @@ class MIRFunction { infoIsString.push_back(isString); } - const MapleMap &GetAliasVarMap() const { - return aliasVarMap; + bool NeedEmitAliasInfo() { + return aliasVarMap != nullptr; + } + + const MapleMap &GetAliasVarMap() { + if (aliasVarMap == nullptr) { + aliasVarMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); + } + return *aliasVarMap; } void SetAliasVarMap(GStrIdx idx, MIRAliasVars &vars) { - aliasVarMap[idx] = vars; + if (aliasVarMap == nullptr) { + aliasVarMap = module->GetMemPool()->New>(module->GetMPAllocator().Adapter()); + } + (*aliasVarMap)[idx] = vars; } @@ -703,9 +720,13 @@ class MIRFunction { } const MIRLabelTable *GetLabelTab() const { + CHECK_FATAL(labelTab != nullptr, "must be"); return labelTab; } MIRLabelTable *GetLabelTab() { + if (labelTab == nullptr) { + labelTab = module->GetMemPool()->New(module->GetMPAllocator()); + } return labelTab; } void SetLabelTab(MIRLabelTable *currLabelTab) { @@ -720,10 +741,14 @@ class MIRFunction { } MemPool *GetDataMemPool() { - return dataMemPool; + return module->GetMemPool(); } MemPool *GetCodeMemPool() { + if (codeMemPool == nullptr) { + codeMemPool = memPoolCtrler.NewMemPool("func code mempool"); + codeMemPoolAllocator.SetMemPool(codeMemPool); + } return codeMemPool; } @@ -732,6 +757,7 @@ class MIRFunction { } MapleAllocator &GetCodeMPAllocator() { + GetCodeMemPool(); return codeMemPoolAllocator; } @@ -755,10 +781,8 @@ class MIRFunction { MIRTypeNameTable *typeNameTab = nullptr; MIRLabelTable *labelTab = nullptr; MIRPregTable *pregTab = nullptr; - MemPool *dataMemPool = memPoolCtrler.NewMemPool("func data mempool"); - MapleAllocator dataMPAllocator{dataMemPool}; - MemPool *codeMemPool = memPoolCtrler.NewMemPool("func code mempool"); - MapleAllocator codeMemPoolAllocator{codeMemPool}; + MemPool *codeMemPool = nullptr; + MapleAllocator codeMemPoolAllocator{nullptr}; uint32 callTimes = 0; BlockNode *body = nullptr; SrcPosition srcPosition{}; @@ -768,7 +792,7 @@ class MIRFunction { uint32 fileIndex = 0; // this function belongs to which file, used by VM for plugin manager MIRInfoVector info{module->GetMPAllocator().Adapter()}; MapleVector infoIsString{module->GetMPAllocator().Adapter()}; // tells if an entry has string value - MapleMap aliasVarMap{module->GetMPAllocator().Adapter()}; // source code alias variables + MapleMap *aliasVarMap = nullptr; // source code alias variables // for debuginfo bool withLocInfo = true; diff --git a/src/maple_ir/include/mir_module.h b/src/maple_ir/include/mir_module.h index beb879f7bc16ff1addf9a17b533e5fa6181a40f9..ac7ecef60d5113a13ab14e2ffdd59230d3b88be9 100644 --- a/src/maple_ir/include/mir_module.h +++ b/src/maple_ir/include/mir_module.h @@ -127,7 +127,12 @@ class MIRModule { MemPool *GetMemPool() { return memPool; } - + MemPool *GetPragmaMemPool() { + return pragmaMemPool; + } + MapleAllocator &GetPragmaMPAllocator() { + return pragmaMemPoolAllocator; + } const MapleAllocator &GetMPAllocator() const { return memPoolAllocator; } @@ -466,7 +471,9 @@ class MIRModule { private: MemPool *memPool; + MemPool *pragmaMemPool; MapleAllocator memPoolAllocator; + MapleAllocator pragmaMemPoolAllocator; MapleVector functionList; // function table in the order of the appearance of function bodies; it // excludes prototype-only functions MapleVector compilationList; // functions in the order of to be compiled. diff --git a/src/maple_ir/include/mir_pragma.h b/src/maple_ir/include/mir_pragma.h index b388167ae7a38d3fd724ef93950fe123612b69c2..75a2bb28b4ff33b31d6401e0f3ce5628a44186e5 100644 --- a/src/maple_ir/include/mir_pragma.h +++ b/src/maple_ir/include/mir_pragma.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2019] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. @@ -71,7 +71,7 @@ enum PragmaValueType { class MIRPragmaElement { public: explicit MIRPragmaElement(MIRModule &m) - : subElemVec(m.GetMPAllocator().Adapter()) { + : subElemVec(m.GetPragmaMPAllocator().Adapter()) { val.u = 0; subElemVec.clear(); } @@ -176,7 +176,7 @@ class MIRPragma { public: explicit MIRPragma(MIRModule &m) : mod(&m), - elementVec(m.GetMPAllocator().Adapter()) {} + elementVec(m.GetPragmaMPAllocator().Adapter()) {} ~MIRPragma() = default; MIRPragmaElement *GetPragmaElemFromSignature(const std::string &signature); diff --git a/src/maple_ir/include/mir_type.h b/src/maple_ir/include/mir_type.h index 6ec9ac2fa43d8a6b8098623c078995c4f75be656..04c0de96166bd8ee4934360dcfd0894a04529867 100644 --- a/src/maple_ir/include/mir_type.h +++ b/src/maple_ir/include/mir_type.h @@ -962,6 +962,10 @@ class MIRStructType : public MIRType { CHECK_FATAL(false, "can not use GetPragmaVec"); } + virtual std::vector &GetPragmaVec() { + CHECK_FATAL(false, "can not use GetPragmaVec"); + } + virtual const MIREncodedArray &GetStaticValue() const { CHECK_FATAL(false, "can not use GetStaticValue"); } @@ -1103,7 +1107,7 @@ class MIRClassType : public MIRStructType { return infoIsString.at(n); } - std::vector &GetPragmaVec() { + std::vector &GetPragmaVec() override { return pragmaVec; } const std::vector &GetPragmaVec() const override { @@ -1245,7 +1249,7 @@ class MIRInterfaceType : public MIRStructType { return infoIsString.at(n); } - std::vector &GetPragmaVec() { + std::vector &GetPragmaVec() override { return pragmaVec; } const std::vector &GetPragmaVec() const override { diff --git a/src/maple_ir/src/bin_mpl_import.cpp b/src/maple_ir/src/bin_mpl_import.cpp index 51c867ec1a4e91c12ea82de0500bfcf0bbf2c446..5794557e12ee919ad3e8999829a111d0d80655ca 100644 --- a/src/maple_ir/src/bin_mpl_import.cpp +++ b/src/maple_ir/src/bin_mpl_import.cpp @@ -112,7 +112,7 @@ MIRConst *BinaryMplImport::ImportConst(MIRFunction *func) { switch (tag) { case kBinKindConstInt: ImportConstBase(kind, type, fieldID); - return mod.GetMemPool()->New(ReadNum(), *type, fieldID); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(ReadNum(), *type, fieldID); case kBinKindConstAddrof: { ImportConstBase(kind, type, fieldID); MIRSymbol *sym = InSymbol(func); @@ -225,7 +225,7 @@ UStrIdx BinaryMplImport::ImportUsrStr() { } MIRPragmaElement *BinaryMplImport::ImportPragmaElement() { - MIRPragmaElement *element = mod.GetMemPool()->New(mod); + MIRPragmaElement *element = mod.GetPragmaMemPool()->New(mod); element->SetNameStrIdx(ImportStr()); element->SetTypeStrIdx(ImportStr()); element->SetType((PragmaValueType)ReadNum()); @@ -243,7 +243,7 @@ MIRPragmaElement *BinaryMplImport::ImportPragmaElement() { } MIRPragma *BinaryMplImport::ImportPragma() { - MIRPragma *p = mod.GetMemPool()->New(mod); + MIRPragma *p = mod.GetPragmaMemPool()->New(mod); p->SetKind(static_cast(ReadNum())); p->SetVisibility(ReadNum()); p->SetStrIdx(ImportStr()); diff --git a/src/maple_ir/src/global_tables.cpp b/src/maple_ir/src/global_tables.cpp index ac9fda89ca4c86bcc2337516a382dc90a6e908e6..8c26e21eb0b5649ef67321d5dcc97eb7713b7b8d 100644 --- a/src/maple_ir/src/global_tables.cpp +++ b/src/maple_ir/src/global_tables.cpp @@ -204,6 +204,22 @@ void FPConstTable::PostInit() { minusZeroDoubleConst = new MIRDoubleConst(-0.0, typeDouble); } +MIRIntConst *IntConstTable::GetOrCreateIntConst(int64 val, MIRType &type, uint32 fieldID) { + uint64 idid = static_cast(type.GetTypeIndex()) + (static_cast(fieldID) << 32); + IntConstKey key(val, idid); + if (intConstTable.find(key) != intConstTable.end()) { + return intConstTable[key]; + } + intConstTable[key] = new MIRIntConst(val, type, fieldID); + return intConstTable[key]; +} + +IntConstTable::~IntConstTable() { + for (auto pair : intConstTable) { + delete pair.second; + } +} + MIRFloatConst *FPConstTable::GetOrCreateFloatConst(float floatVal) { if (std::isnan(floatVal)) { return nanFloatConst; diff --git a/src/maple_ir/src/mir_builder.cpp b/src/maple_ir/src/mir_builder.cpp index f8e11d5a57397f0af38588b605d36996282eeecd..9282d1dd5dabdc6ce599d46f61a0f1b606de2269 100644 --- a/src/maple_ir/src/mir_builder.cpp +++ b/src/maple_ir/src/mir_builder.cpp @@ -19,7 +19,8 @@ namespace maple { // This is for compiler-generated metadata 1-level struct void MIRBuilder::AddIntFieldConst(const MIRStructType &sType, MIRAggConst &newConst, uint32 fieldID, int64 constValue) { - auto *fieldConst = mirModule->GetMemPool()->New(constValue, *sType.GetElemType(fieldID - 1), fieldID); + auto *fieldConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + constValue, *sType.GetElemType(fieldID - 1), fieldID); newConst.PushBack(fieldConst); } @@ -39,7 +40,7 @@ void MIRBuilder::AddAddroffuncFieldConst(const MIRStructType &structType, MIRAgg MIRConst *fieldConst = nullptr; MIRFunction *vMethod = funcSymbol.GetFunction(); if (vMethod->IsAbstract()) { - fieldConst = mirModule->GetMemPool()->New(0, *structType.GetElemType(fieldID - 1), fieldID); + fieldConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, *structType.GetElemType(fieldID - 1), fieldID); } else { AddroffuncNode *addrofFuncExpr = CreateExprAddroffunc(funcSymbol.GetFunction()->GetPuidx(), mirModule->GetMemPool()); @@ -466,9 +467,8 @@ MIRSymbol *MIRBuilder::CreatePregFormalSymbol(TyIdx tyIdx, PregIdx pRegIdx, MIRF } ConstvalNode *MIRBuilder::CreateIntConst(int64 val, PrimType pty) { - MIRFunction *currentFunctionInner = GetCurrentFunctionNotNull(); auto *mirConst = - currentFunctionInner->GetDataMemPool()->New(val, *GlobalTables::GetTypeTable().GetPrimType(pty)); + GlobalTables::GetIntConstTable().GetOrCreateIntConst(val, *GlobalTables::GetTypeTable().GetPrimType(pty)); return GetCurrentFuncCodeMp()->New(pty, mirConst); } @@ -494,7 +494,7 @@ ConstvalNode *MIRBuilder::CreateFloat128Const(const uint64 *val) { } ConstvalNode *MIRBuilder::GetConstInt(MemPool &memPool, int val) { - auto *mirConst = memPool.New(val, *GlobalTables::GetTypeTable().GetInt64()); + auto *mirConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(val, *GlobalTables::GetTypeTable().GetInt64()); return memPool.New(PTY_i32, mirConst); } diff --git a/src/maple_ir/src/mir_function.cpp b/src/maple_ir/src/mir_function.cpp index 24963e717e84b9e48f740140556d102f1f7624e0..96a108940b19d27eb438d3ded3189d74c2591651 100644 --- a/src/maple_ir/src/mir_function.cpp +++ b/src/maple_ir/src/mir_function.cpp @@ -35,13 +35,8 @@ enum FuncProp : uint32_t { namespace maple { void MIRFunction::Init() { - // Initially allocate symTab and pregTab on the module mempool for storing - // parameters. If later mirfunction turns out to be a definition, new - // tables will be allocated on the local data mempool. symTab = module->GetMemPool()->New(module->GetMPAllocator()); - pregTab = module->GetMemPool()->New(module, &module->GetMPAllocator()); - typeNameTab = module->GetMemPool()->New(module->GetMPAllocator()); - labelTab = module->GetMemPool()->New(module->GetMPAllocator()); + return; } const MIRSymbol *MIRFunction::GetFuncSymbol() const { @@ -254,7 +249,6 @@ void MIRFunction::Dump(bool withoutBody) { // codeMemPool is nullptr, means maple_ir has been released for memory's sake if (codeMemPool == nullptr) { LogInfo::MapleLogger() << '\n'; - LogInfo::MapleLogger() << "# [WARNING] skipped dumping because codeMemPool is nullptr " << '\n'; } else if (GetBody() != nullptr && !withoutBody && symbol->GetStorageClass() != kScExtern) { ResetInfoPrinted(); // this ensures funcinfo will be printed GetBody()->Dump(0, module->GetFlavor() < kMmpl ? GetSymTab() : nullptr, @@ -481,6 +475,10 @@ const MIRType *MIRFunction::GetNodeType(const BaseNode &node) const { } void MIRFunction::NewBody() { + if (codeMemPool == nullptr) { + codeMemPool = memPoolCtrler.NewMemPool("func code mempool"); + codeMemPoolAllocator.SetMemPool(codeMemPool); + } SetBody(codeMemPool->New()); // If mir_function.has been seen as a declaration, its symtab has to be moved // from module mempool to function mempool. @@ -488,10 +486,10 @@ void MIRFunction::NewBody() { MIRPregTable *oldPregTable = GetPregTab(); MIRTypeNameTable *oldTypeNameTable = typeNameTab; MIRLabelTable *oldLabelTable = GetLabelTab(); - symTab = dataMemPool->New(dataMPAllocator); - SetPregTab(dataMemPool->New(module, &dataMPAllocator)); - typeNameTab = dataMemPool->New(dataMPAllocator); - SetLabelTab(dataMemPool->New(dataMPAllocator)); + symTab = module->GetMemPool()->New(module->GetMPAllocator()); + pregTab = module->GetMemPool()->New(module, &module->GetMPAllocator()); + typeNameTab = module->GetMemPool()->New(module->GetMPAllocator()); + labelTab = module->GetMemPool()->New(module->GetMPAllocator()); if (oldSymTable != nullptr) { for (size_t i = 1; i < oldSymTable->GetSymbolTableSize(); ++i) { (void)GetSymTab()->AddStOutside(oldSymTable->GetSymbolFromStIdx(i)); diff --git a/src/maple_ir/src/mir_module.cpp b/src/maple_ir/src/mir_module.cpp index 539bc766fcda6946776971459aace31bea879cad..3a18b35a0943eca5e4882775d896ed1353dc65ee 100644 --- a/src/maple_ir/src/mir_module.cpp +++ b/src/maple_ir/src/mir_module.cpp @@ -30,7 +30,9 @@ namespace maple { #if MIR_FEATURE_FULL // to avoid compilation error when MIR_FEATURE_FULL=0 MIRModule::MIRModule(const std::string &fn) : memPool(memPoolCtrler.NewMemPool("maple_ir mempool")), + pragmaMemPool(memPoolCtrler.NewMemPool("pragma mempool")), memPoolAllocator(memPool), + pragmaMemPoolAllocator(pragmaMemPool), functionList(memPoolAllocator.Adapter()), compilationList(memPoolAllocator.Adapter()), importedMplt(memPoolAllocator.Adapter()), diff --git a/src/maple_ir/src/mir_nodes.cpp b/src/maple_ir/src/mir_nodes.cpp index bdba14e39f5b984df042f101c25f68ad65c28274..f25bc6559d3880c90890bf2ea4a352ca1cc9e49c 100644 --- a/src/maple_ir/src/mir_nodes.cpp +++ b/src/maple_ir/src/mir_nodes.cpp @@ -423,7 +423,8 @@ MIRType *ArrayNode::GetArrayType(const TypeTable &tt) { const BaseNode *ArrayNode::GetDim(const MIRModule &mod, TypeTable &tt, int i) const { const auto *arrayType = static_cast(GetArrayType(tt)); - auto *mirConst = mod.CurFuncCodeMemPool()->New(i, *tt.GetTypeFromTyIdx(arrayType->GetElemTyIdx())); + auto *mirConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + i, *tt.GetTypeFromTyIdx(arrayType->GetElemTyIdx())); return mod.CurFuncCodeMemPool()->New(mirConst); } BaseNode *ArrayNode::GetDim(const MIRModule &mod, TypeTable &tt, int i) { @@ -1068,33 +1069,42 @@ void BlockNode::Dump(int32 indent, const MIRSymbolTable *theSymTab, MIRPregTable // output puid for debugging purpose if (isFuncbody) { theMIRModule->CurFunction()->DumpFuncBody(indent); - if (theSymTab != nullptr && thePregTab != nullptr) { + if (theSymTab != nullptr || thePregTab != nullptr) { // print the locally declared type names - for (auto it : theMIRModule->CurFunction()->GetGStrIdxToTyIdxMap()) { - const std::string &name = GlobalTables::GetStrTable().GetStringFromStrIdx(it.first); - MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second); - PrintIndentation(indent + 1); - LogInfo::MapleLogger() << "type %" << name << " "; - if (type->GetKind() != kTypeByName) { - type->Dump(indent + 2, true); - } else { - type->Dump(indent + 2); + if (theMIRModule->CurFunction()->HaveTypeNameTab()) { + for (auto it : theMIRModule->CurFunction()->GetGStrIdxToTyIdxMap()) { + const std::string &name = GlobalTables::GetStrTable().GetStringFromStrIdx(it.first); + MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second); + PrintIndentation(indent + 1); + LogInfo::MapleLogger() << "type %" << name << " "; + if (type->GetKind() != kTypeByName) { + type->Dump(indent + 2, true); + } else { + type->Dump(indent + 2); + } + LogInfo::MapleLogger() << '\n'; } - LogInfo::MapleLogger() << '\n'; } // print the locally declared variables - theSymTab->Dump(true, indent + 1); - thePregTab->DumpRef(indent + 1); + if (theSymTab != nullptr) { + theSymTab->Dump(true, indent + 1); + } + if (thePregTab != nullptr) { + thePregTab->DumpRef(indent + 1); + } } LogInfo::MapleLogger() << '\n'; - for (std::pair it : theMIRModule->CurFunction()->GetAliasVarMap()) { - LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << " %" - << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.memPoolStrIdx) << " "; - GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); - if (it.second.sigStrIdx) { - LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; + if (theMIRModule->CurFunction()->NeedEmitAliasInfo()) { + for (std::pair it : theMIRModule->CurFunction()->GetAliasVarMap()) { + LogInfo::MapleLogger() << "ALIAS %" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) << " %" + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.memPoolStrIdx) << " "; + GlobalTables::GetTypeTable().GetTypeFromTyIdx(it.second.tyIdx)->Dump(0); + if (it.second.sigStrIdx) { + LogInfo::MapleLogger() << " \"" + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; + } + LogInfo::MapleLogger() << '\n'; } - LogInfo::MapleLogger() << '\n'; } } if (srcPosition.FileNum() != 0 && srcPosition.LineNum() != 0 && srcPosition.LineNum() != lastPrintedLineNum && diff --git a/src/maple_ir/src/mir_parser.cpp b/src/maple_ir/src/mir_parser.cpp index 6ca8dd84f0f1b1729fd83115206fe5b98cce0ad5..eae19b0ea0cd5636cc3dc25812c1d535d3fc1171 100644 --- a/src/maple_ir/src/mir_parser.cpp +++ b/src/maple_ir/src/mir_parser.cpp @@ -1312,7 +1312,7 @@ bool MIRParser::ParseNaryStmt(StmtNodePtr &stmt, Opcode op) { } else { MIRType *intType = GlobalTables::GetTypeTable().GetTypeFromTyIdx((TyIdx)PTY_i32); // default 2 for __sync_enter_fast() - MIRIntConst *intConst = mod.GetMemPool()->New(2, *intType); + MIRIntConst *intConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(2, *intType); ConstvalNode *exprConst = mod.GetMemPool()->New(); exprConst->SetPrimType(PTY_i32); exprConst->SetConstVal(intConst); @@ -2603,7 +2603,7 @@ bool MIRParser::ParseScalarValue(MIRConstPtr &stype, MIRType &type) { Error("constant value incompatible with integer type at "); return false; } - stype = mod.GetMemPool()->New(lexer.GetTheIntVal(), type); + stype = GlobalTables::GetIntConstTable().GetOrCreateIntConst(lexer.GetTheIntVal(), type); } else if (ptp == PTY_f32) { if (lexer.GetTokenKind() != TK_floatconst) { Error("constant value incompatible with single-precision float type at "); diff --git a/src/maple_ir/src/parser.cpp b/src/maple_ir/src/parser.cpp index 2b76c2e20e9925c3318a9fa5c9087ac1d859c0f8..72c88e915b64a6c56214278ce5f1f655cb2496e6 100644 --- a/src/maple_ir/src/parser.cpp +++ b/src/maple_ir/src/parser.cpp @@ -2094,7 +2094,12 @@ bool MIRParser::ParseInitValue(MIRConstPtr &theConst, TyIdx tyIdx, bool allowEmp return false; } ASSERT(subConst != nullptr, "subConst is null in MIRParser::ParseInitValue"); - subConst->SetFieldID(theFieldID); + if (subConst->GetKind() == kConstInt) { + subConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(subConst)->GetValue(), subConst->GetType(), theFieldID); + } else { + subConst->SetFieldID(theFieldID); + } newConst->PushBack(subConst); tokenKind = lexer.GetTokenKind(); // parse comma or rbrack diff --git a/src/maple_me/src/irmap.cpp b/src/maple_me/src/irmap.cpp index 9dd80cb862b7d1b1d18945fe44d1688966afca72..3f6b9b59da8ae74cc23a3d3f24e20443a4032901 100644 --- a/src/maple_me/src/irmap.cpp +++ b/src/maple_me/src/irmap.cpp @@ -864,7 +864,7 @@ MeExpr *IRMap::CreateConstMeExpr(PrimType pType, MIRConst &mirConst) { MeExpr *IRMap::CreateIntConstMeExpr(int64 value, PrimType pType) { auto *intConst = - mirModule.GetMemPool()->New(value, *GlobalTables::GetTypeTable().GetPrimType(pType)); + GlobalTables::GetIntConstTable().GetOrCreateIntConst(value, *GlobalTables::GetTypeTable().GetPrimType(pType)); return CreateConstMeExpr(pType, *intConst); } diff --git a/src/maple_me/src/irmap_emit.cpp b/src/maple_me/src/irmap_emit.cpp index 06abfe17be0092b2054ffd8bc63a6f9a7c5b5a8e..36eaaa40f6ddbef4347f253cd503795911087a7a 100644 --- a/src/maple_me/src/irmap_emit.cpp +++ b/src/maple_me/src/irmap_emit.cpp @@ -57,7 +57,9 @@ BaseNode &ConstMeExpr::EmitExpr(SSATab &ssaTab) { // if int const has been promoted from dyn int const, remove the type tag if (IsPrimitiveInteger(exprConst->GetPrimType())) { auto *intConst = safe_cast(exprConst->GetConstVal()); - intConst->SetValue(intConst->GetValueUnderType()); + MIRIntConst *newIntConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(intConst->GetValueUnderType(), + intConst->GetType(), intConst->GetFieldId()); + exprConst->SetConstVal(newIntConst); } return *exprConst; } diff --git a/src/maple_me/src/me_phase_manager.cpp b/src/maple_me/src/me_phase_manager.cpp index b29be385697dc7fb8c5e42fefd839a95dd4607c0..691e6b4de64d0074cd321f658523f3cd33fe3d16 100644 --- a/src/maple_me/src/me_phase_manager.cpp +++ b/src/maple_me/src/me_phase_manager.cpp @@ -133,7 +133,7 @@ void MeFuncPhaseManager::Run(MIRFunction *mirFunc, uint64 rangeNum, const std::s } MemPool *funcMP = memPoolCtrler.NewMemPool("maple_me per-function mempool"); MemPool *versMP = memPoolCtrler.NewMemPool("first verst mempool"); - MeFunction func(&mirModule, mirFunc, funcMP, versMP, meInput); + MeFunction &func = *(funcMP->New(&mirModule, mirFunc, funcMP, versMP, meInput)); func.PartialInit(false); #if DEBUG globalMIRModule = &mirModule; diff --git a/src/maple_me/src/prop.cpp b/src/maple_me/src/prop.cpp index a3141471644ffba113a48315369ced39d2d51a8c..0bdde361e03ec999939462be976ea2aee46ecbff 100644 --- a/src/maple_me/src/prop.cpp +++ b/src/maple_me/src/prop.cpp @@ -137,7 +137,7 @@ MeExpr *Prop::SimplifyCompareMeExpr(OpMeExpr &opMeExpr) { } if (constOpnd->IsZero()) { // addrof will not be zero, so this comparison can be replaced with a constant - auto *resConst = mirModule.GetMemPool()->New((opcode == OP_ne), + auto *resConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst((opcode == OP_ne), utils::ToRef(GlobalTables::GetTypeTable().GetUInt1())); return irMap.CreateConstMeExpr(opMeExpr.GetPrimType(), *resConst); } diff --git a/src/maple_me/src/ssa_pre.cpp b/src/maple_me/src/ssa_pre.cpp index da5559d5e75546a1aadb6b58587352fe43505e58..40e5192bcdf07782626f4075cbbfe31cfc4d4fba 100644 --- a/src/maple_me/src/ssa_pre.cpp +++ b/src/maple_me/src/ssa_pre.cpp @@ -1625,7 +1625,6 @@ void SSAPre::ApplySSAPRE() { } ConstructUseOccurMap(); for (size_t i = 0; i < workList.size() && i <= preLimit; i++) { - perCandMemPool->Push(); workCand = workList[i]; if (workCand->GetRealOccs().empty()) { continue; @@ -1697,7 +1696,7 @@ void SSAPre::ApplySSAPRE() { // apply full redundancy elimination DoSSAFRE(); } - perCandMemPool->Pop(); + perCandMemPool->ReleaseContainingMem(); } } } // namespace maple diff --git a/src/mempool/include/mempool.h b/src/mempool/include/mempool.h index 9445ffb96f4b19ce9e08e3f5a36d8f8f324c9aaa..c6892415e7882012de242a2e7f62063e5b49378b 100644 --- a/src/mempool/include/mempool.h +++ b/src/mempool/include/mempool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2019] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. @@ -39,12 +39,25 @@ class MemPoolCtrler { friend MemPool; public: // Methods + static bool freeMemInTime; MemPoolCtrler() = default; ~MemPoolCtrler(); MemPool *NewMemPool(const std::string&); void DeleteMemPool(MemPool *memPool); + void FreeMem() { + for (MemBlock *block : freeMemBlocks) { + free(block); + } + for (auto it = largeFreeMemBlocks.begin(); it != largeFreeMemBlocks.end(); ++it) { + for (auto itr = (*it).second.begin(); itr != (*it).second.end(); ++itr) { + free(*itr); + } + } + freeMemBlocks.clear(); + largeFreeMemBlocks.clear(); + } bool IsEmpty() const { return memPools.empty(); } @@ -83,8 +96,7 @@ class MemPool { void *Malloc(size_t size); void *Calloc(size_t size); void *Realloc(const void *ptr, size_t oldSize, size_t newSize); - void Push(); - bool Pop(); + void ReleaseContainingMem(); const std::string &GetName() const { return name; } @@ -129,8 +141,6 @@ class MemPool { // Save the memory block stack std::stack memBlockStack; std::stack largeMemBlockStack; - // Save mem_block and large_mem_block pointers when push() - std::stack> markerStack; }; extern MemPoolCtrler memPoolCtrler; diff --git a/src/mpl2mpl/src/coderelayout.cpp b/src/mpl2mpl/src/coderelayout.cpp index 4779c0e4141155d2bd21194796296f46453f0cad..3a0ef825fabaa918bfc2cd51766befe7969b0f98 100644 --- a/src/mpl2mpl/src/coderelayout.cpp +++ b/src/mpl2mpl/src/coderelayout.cpp @@ -209,7 +209,8 @@ MIRSymbol *CodeReLayout::GenStrSym(const std::string &str) { MIRAggConst *strTabAggConst = GetMIRModule().GetMemPool()->New(GetMIRModule(), strTabType); staticSym->SetStorageClass(kScFstatic); for (const char &c : newStr) { - MIRConst *newConst = GetMIRModule().GetMemPool()->New(c, *GlobalTables::GetTypeTable().GetUInt8()); + MIRConst *newConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + c, *GlobalTables::GetTypeTable().GetUInt8()); strTabAggConst->PushBack(newConst); } staticSym->SetKonst(strTabAggConst); diff --git a/src/mpl2mpl/src/constantfold.cpp b/src/mpl2mpl/src/constantfold.cpp index 5d409909a8a1509b2dba501ef59284f9cbe993d4..682e3b9c32f420c41bbbb3f1695dafdfd0ad134d 100644 --- a/src/mpl2mpl/src/constantfold.cpp +++ b/src/mpl2mpl/src/constantfold.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2019] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. @@ -315,10 +315,10 @@ MIRIntConst *ConstantFold::FoldIntConstComparisonMIRConst(Opcode opcode, PrimTyp // form the constant MIRIntConst *constValue = nullptr; if (type.GetPrimType() == PTY_dyni32) { - constValue = mirModule->GetMemPool()->New(0, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); constValue->SetValue(kJsTypeNumberInHigh32Bit | (static_cast(result))); } else { - constValue = mirModule->GetMemPool()->New(result, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result, type); } return constValue; } @@ -522,12 +522,12 @@ ConstvalNode *ConstantFold::FoldIntConstBinary(Opcode opcode, PrimType resultTyp // form the constant MIRIntConst *constValue = nullptr; if (type.GetPrimType() == PTY_dyni32) { - constValue = mirModule->GetMemPool()->New(0, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); constValue->SetValue(kJsTypeNumberInHigh32Bit | (static_cast(result32))); } else if (useResult64) { - constValue = mirModule->GetMemPool()->New(result64, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result64, type); } else { - constValue = mirModule->GetMemPool()->New(result32, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result32, type); } // form the ConstvalNode ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); @@ -741,7 +741,7 @@ MIRIntConst *ConstantFold::FoldFPConstComparisonMIRConst(Opcode opcode, PrimType default: ASSERT(false, "Unknown opcode for FoldFPConstComparison"); } - MIRIntConst *resultConst = mirModule->GetMemPool()->New(constValue, type); + MIRIntConst *resultConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(constValue, type); return resultConst; } @@ -859,12 +859,12 @@ ConstvalNode *ConstantFold::FoldIntConstUnary(Opcode opcode, PrimType resultType // form the constant MIRIntConst *constValue = nullptr; if (type.GetPrimType() == PTY_dyni32) { - constValue = mirModule->GetMemPool()->New(0, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); constValue->SetValue(kJsTypeNumberInHigh32Bit | (static_cast(result32))); } else if (useResult64) { - constValue = mirModule->GetMemPool()->New(result64, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result64, type); } else { - constValue = mirModule->GetMemPool()->New(result32, type); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result32, type); } // form the ConstvalNode ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); @@ -941,7 +941,8 @@ std::pair ConstantFold::FoldSizeoftype(SizeoftypeNode *node) { uint32 size = GetPrimTypeSize(argType->GetPrimType()); ConstvalNode *constValueNode = mirModule->CurFuncCodeMemPool()->New(); constValueNode->SetPrimType(node->GetPrimType()); - constValueNode->SetConstVal(mirModule->GetMemPool()->New(static_cast(size), resultType)); + constValueNode->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(size), resultType)); result = constValueNode; } return std::make_pair(result, 0); @@ -1010,11 +1011,13 @@ ConstvalNode *ConstantFold::FoldCeil(ConstvalNode *cst, PrimType fromType, PrimT if (fromType == PTY_f32) { MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); float floutValue = ceil(constValue->GetValue()); - resultConst->SetConstVal(mirModule->GetMemPool()->New(static_cast(floutValue), resultType)); + resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(floutValue), resultType)); } else { MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); double doubleValue = ceil(constValue->GetValue()); - resultConst->SetConstVal(mirModule->GetMemPool()->New(static_cast(doubleValue), resultType)); + resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(doubleValue), resultType)); } return resultConst; } @@ -1025,11 +1028,11 @@ MIRConst *ConstantFold::FoldFloorMIRConst(MIRConst *cst, PrimType fromType, Prim if (fromType == PTY_f32) { MIRFloatConst *constValue = safe_cast(cst); float floutValue = floor(constValue->GetValue()); - return mirModule->GetMemPool()->New(static_cast(floutValue), resultType); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(floutValue), resultType); } else { MIRDoubleConst *constValue = safe_cast(cst); double doubleValue = floor(constValue->GetValue()); - return mirModule->GetMemPool()->New(static_cast(doubleValue), resultType); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(doubleValue), resultType); } } @@ -1045,11 +1048,11 @@ MIRConst *ConstantFold::FoldRoundMIRConst(MIRConst *cst, PrimType fromType, Prim if (fromType == PTY_f32) { MIRFloatConst *constValue = safe_cast(cst); float floutValue = round(constValue->GetValue()); - return mirModule->GetMemPool()->New(static_cast(floutValue), resultType); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(floutValue), resultType); } else if (fromType == PTY_f64) { MIRDoubleConst *constValue = safe_cast(cst); double doubleValue = round(constValue->GetValue()); - return mirModule->GetMemPool()->New(static_cast(doubleValue), resultType); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(doubleValue), resultType); } else if (toType == PTY_f32 && IsPrimitiveInteger(fromType)) { MIRIntConst *constValue = safe_cast(cst); int64 fromValue = constValue->GetValue(); @@ -1084,11 +1087,13 @@ ConstvalNode *ConstantFold::FoldTrunk(ConstvalNode *cst, PrimType fromType, Prim if (fromType == PTY_f32) { MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); float floutValue = trunc(constValue->GetValue()); - resultConst->SetConstVal(mirModule->GetMemPool()->New(static_cast(floutValue), resultType)); + resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(floutValue), resultType)); } else { MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); double doubleValue = trunc(constValue->GetValue()); - resultConst->SetConstVal(mirModule->GetMemPool()->New(static_cast(doubleValue), resultType)); + resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(doubleValue), resultType)); } return resultConst; } @@ -1111,7 +1116,7 @@ MIRConst *ConstantFold::FoldTypeCvtMIRConst(MIRConst *cst, PrimType fromType, Pr } else { MIRIntConst *c = safe_cast(cst); MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(toType); - toConst = mirModule->GetMemPool()->New(c->GetValue(), type); + toConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(c->GetValue(), type); } return toConst; } @@ -1208,7 +1213,7 @@ MIRConst *ConstantFold::FoldSignExtendMIRConst(Opcode opcode, PrimType resultTyp result64 = ((static_cast(c->GetValue())) << (64u - size)) >> (64u - size); } MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(resultType); - MIRIntConst *constValue = mirModule->GetMemPool()->New(result64, type); + MIRIntConst *constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(result64, type); return constValue; } @@ -1572,7 +1577,7 @@ std::pair ConstantFold::FoldDepositbits(DepositbitsNode *node) ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(node->GetPrimType()); MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(node->GetPrimType()); - MIRIntConst *constValue = mirModule->GetMemPool()->New(0, type); + MIRIntConst *constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); uint64 op0ExtractVal = 0; uint64 op1ExtractVal = 0; uint64 mask0 = (1LLU << (bSize + bOffset)) - 1; @@ -1580,7 +1585,8 @@ std::pair ConstantFold::FoldDepositbits(DepositbitsNode *node) uint64 op0Mask = ~(mask0 ^ mask1); op0ExtractVal = (static_cast(intConst0->GetValue()) & op0Mask); op1ExtractVal = (static_cast(intConst1->GetValue()) << bOffset) & ((1ULL << (bSize + bOffset)) - 1); - constValue->SetValue(op0ExtractVal | op1ExtractVal); + constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + op0ExtractVal | op1ExtractVal, constValue->GetType(), constValue->GetFieldId()); resultConst->SetConstVal(constValue); result = resultConst; } else { diff --git a/src/mpl2mpl/src/muid_replacement.cpp b/src/mpl2mpl/src/muid_replacement.cpp index 57a312d51328bfa2fdbe34b086c8f4af7b8041bc..b9fc099e2e9119d17393bf88838d4a7ba5d6c0cb 100644 --- a/src/mpl2mpl/src/muid_replacement.cpp +++ b/src/mpl2mpl/src/muid_replacement.cpp @@ -267,7 +267,8 @@ void MUIDReplacement::GenerateFuncDefTable() { keyVal.second.second = idx++; // Use the muid index for now. It will be back-filled once we have the whole vector. MIRIntConst *indexConst = - GetMIRModule().GetMemPool()->New(keyVal.second.second, *GlobalTables::GetTypeTable().GetUInt32()); + GlobalTables::GetIntConstTable().GetOrCreateIntConst(keyVal.second.second, + *GlobalTables::GetTypeTable().GetUInt32()); muidIdxTabConst->PushBack(indexConst); } FieldVector parentFields; @@ -318,12 +319,14 @@ void MUIDReplacement::GenerateFuncDefTable() { constexpr uint32 weakFuncFlag = 0x80000000; // 0b10000000 00000000 00000000 00000000 auto *indexConst = safe_cast(muidIdxTabConst->GetConstVecItem(muidIdx)); uint32 tempIdx = (static_cast(indexConst->GetValue()) & weakFuncFlag) | idx; - indexConst = GetMIRModule().GetMemPool()->New(tempIdx, *GlobalTables::GetTypeTable().GetUInt32()); + indexConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(tempIdx, + *GlobalTables::GetTypeTable().GetUInt32()); muidIdxTabConst->SetConstVecItem(muidIdx, *indexConst); if (reflectionList.find(mirFunc->GetName()) != reflectionList.end()) { auto *tempConst = safe_cast(muidIdxTabConst->GetConstVecItem(idx)); tempIdx = weakFuncFlag | static_cast(tempConst->GetValue()); - tempConst = GetMIRModule().GetMemPool()->New(tempIdx, *GlobalTables::GetTypeTable().GetUInt32()); + tempConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(tempIdx, + *GlobalTables::GetTypeTable().GetUInt32()); muidIdxTabConst->SetConstVecItem(idx, *tempConst); } // Store the real idx of funcdefTab, for ReplaceAddroffuncConst->FindIndexFromDefTable @@ -401,7 +404,7 @@ void MUIDReplacement::ReplaceMethodMetaFuncAddr(MIRSymbol &funcSymbol, int64 ind MIRConst *elem = agg->GetConstVecItem(0); if (elem->GetKind() == kConstAddrofFunc) { MIRType &type = elem->GetType(); - MIRConst *constNode = GetMIRModule().GetMemPool()->New(index, type, 1); + MIRConst *constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(index, type, 1); agg->SetConstVecItem(0, *constNode); } } @@ -498,7 +501,7 @@ void MUIDReplacement::ReplaceFieldMetaStaticAddr(MIRSymbol &mirSymbol, int64 ind MIRType &type = elem->GetType(); int64 idx = index * 2 + 1; // add flag to indicate that it's def tab index for emit. - MIRConst *constNode = GetMIRModule().GetMemPool()->New(idx, type, 1); + MIRConst *constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(idx, type, 1); agg->SetConstVecItem(0, *constNode); (void)idx; } @@ -793,20 +796,21 @@ void MUIDReplacement::ReplaceAddroffuncConst(MIRConst *&entry, uint32 fieldID, b // The second least significant bit is set to 1, indicating // this is an index into the funcDefTab constexpr uint64 idxIntoFuncDefTabFlag = 2u; - constNode = GetMIRModule().GetMemPool()->New(((offset + 1) << reservedBits) + idxIntoFuncDefTabFlag, - voidType); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + ((offset + 1) << reservedBits) + idxIntoFuncDefTabFlag, voidType); } else if (isVtab && func->IsAbstract()) { MIRType &type = *GlobalTables::GetTypeTable().GetVoidPtr(); - constNode = GetMIRModule().GetMemPool()->New(0, type); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); } else { ASSERT(func->GetFuncSymbol() != nullptr, "null ptr check!"); offset = FindIndexFromUndefTable(*(func->GetFuncSymbol()), true); // The second least significant bit is set to 0, indicating // this is an index into the funcUndefTab - constNode = GetMIRModule().GetMemPool()->New((offset + 1) << reservedBits, voidType); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst((offset + 1) << reservedBits, voidType); } if (fieldID != 0xffffffff) { - constNode->SetFieldID(fieldID); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(constNode->GetValue(), + constNode->GetType(), fieldID); } entry = constNode; } @@ -827,7 +831,14 @@ void MUIDReplacement::ReplaceDataTable(const std::string &name) { for (size_t i = 0; i < aggrC->GetConstVec().size(); ++i) { CHECK_NULL_FATAL(aggrC->GetConstVecItem(i)); ReplaceAddrofConst(aggrC->GetConstVecItem(i)); - aggrC->GetConstVecItem(i)->SetFieldID(i + 1); + MIRConstPtr mirConst = aggrC->GetConstVecItem(i); + if (mirConst->GetKind() == kConstInt) { + MIRIntConst *newIntConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(mirConst)->GetValue(), mirConst->GetType(), i + 1); + aggrC->SetConstVecItem(i, *newIntConst); + } else { + aggrC->GetConstVecItem(i)->SetFieldID(i + 1); + } } } else if (oldTabEntry->GetKind() == kConstAddrof) { ReplaceAddrofConst(oldTabEntry); @@ -849,7 +860,14 @@ void MUIDReplacement::ReplaceDecoupleKeyTable(MIRAggConst* oldConst) { ReplaceDecoupleKeyTable(safe_cast(aggrC->GetConstVecItem(i))); } else { ReplaceAddrofConst(aggrC->GetConstVecItem(i)); - aggrC->GetConstVecItem(i)->SetFieldID(i + 1); + MIRConstPtr mirConst = aggrC->GetConstVecItem(i); + if (mirConst->GetKind() == kConstInt) { + MIRIntConst *newIntConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + static_cast(mirConst)->GetValue(), mirConst->GetType(), i + 1); + aggrC->SetConstVecItem(i, *newIntConst); + } else { + aggrC->GetConstVecItem(i)->SetFieldID(i + 1); + } } } } else if (oldTabEntry->GetKind() == kConstAddrof) { @@ -873,10 +891,10 @@ void MUIDReplacement::ReplaceAddrofConst(MIRConst *&entry) { MIRIntConst *constNode = nullptr; if (addrSym->GetStorageClass() != kScExtern) { offset = FindIndexFromDefTable(*addrSym, false); - constNode = GetMIRModule().GetMemPool()->New(offset | kFromDefIndexMask, voidType); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(offset | kFromDefIndexMask, voidType); } else { offset = FindIndexFromUndefTable(*addrSym, false); - constNode = GetMIRModule().GetMemPool()->New(offset | kFromUndefIndexMask, voidType); + constNode = GlobalTables::GetIntConstTable().GetOrCreateIntConst(offset | kFromUndefIndexMask, voidType); } entry = constNode; } @@ -1225,8 +1243,8 @@ void MUIDReplacement::GenerateCompilerVersionNum() { MIRArrayType &arrayType = *GlobalTables::GetTypeTable().GetOrCreateArrayType(*ptrType, 0); MIRAggConst *newConst = GetMIRModule().GetMemPool()->New(GetMIRModule(), arrayType); MIRType &type = *GlobalTables::GetTypeTable().GetInt32(); - MIRConst *firstConst = GetMIRModule().GetMemPool()->New(Version::kMajorMplVersion, type); - MIRConst *secondConst = GetMIRModule().GetMemPool()->New(Version::kMinorCompilerVersion, type); + MIRConst *firstConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(Version::kMajorMplVersion, type); + MIRConst *secondConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(Version::kMinorCompilerVersion, type); newConst->PushBack(firstConst); newConst->PushBack(secondConst); std::string symName = NameMangler::kCompilerVersionNum + GetMIRModule().GetFileNameAsPostfix(); diff --git a/src/mpl2mpl/src/native_stub_func.cpp b/src/mpl2mpl/src/native_stub_func.cpp index a6ada15e8e723cf48902356228fa7dab36d1398b..de90791b3bdebdcd1d7656739c24be7fc6d7cc3f 100644 --- a/src/mpl2mpl/src/native_stub_func.cpp +++ b/src/mpl2mpl/src/native_stub_func.cpp @@ -51,7 +51,7 @@ MIRFunction &NativeStubFuncGeneration::GetOrCreateDefaultNativeFunc(MIRFunction if (nativeFunc->GetBody() == nullptr) { builder->SetCurrentFunction(*nativeFunc); nativeFunc->SetAttr(FUNCATTR_weak); - nativeFunc->SetBody(nativeFunc->GetCodeMempool()->New()); + nativeFunc->NewBody(); // We would not throw exception here. // Use regnative-dynamic-only option when run case expr14301a_setFields__IF as qemu solution. MIRType *voidPointerType = GlobalTables::GetTypeTable().GetVoidPtr(); @@ -297,8 +297,8 @@ void NativeStubFuncGeneration::GenerateRegFuncTabEntry() { constexpr uint64 locIdxMask = 0xFF00000000000000; uint64 locIdx = regFuncTabConst->GetConstVec().size(); auto *newConst = - GetMIRModule().GetMemPool()->New(static_cast((locIdx << locIdxShift) | locIdxMask), - *GlobalTables::GetTypeTable().GetVoidPtr()); + GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast((locIdx << locIdxShift) | locIdxMask), + *GlobalTables::GetTypeTable().GetVoidPtr()); regFuncTabConst->PushBack(newConst); } @@ -320,10 +320,10 @@ void NativeStubFuncGeneration::GenerateRegTabEntry(const MIRFunction &func) { uint32 classIdx = ReflectionAnalysis::FindOrInsertRepeatString(base, true); // always used // Using MIRIntConst instead of MIRStruct for RegTable. auto *baseConst = - GetMIRModule().GetMemPool()->New(classIdx, *GlobalTables::GetTypeTable().GetVoidPtr()); + GlobalTables::GetIntConstTable().GetOrCreateIntConst(classIdx, *GlobalTables::GetTypeTable().GetVoidPtr()); regTableConst->PushBack(baseConst); - auto *newConst = GetMIRModule().GetMemPool()->New(nameIdx, - *GlobalTables::GetTypeTable().GetVoidPtr()); + auto *newConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(nameIdx, + *GlobalTables::GetTypeTable().GetVoidPtr()); regTableConst->PushBack(newConst); } diff --git a/src/mpl2mpl/src/reflection_analysis.cpp b/src/mpl2mpl/src/reflection_analysis.cpp index a5d8b33beed8e3ddfb0663654f27ec9396c42fec..a3470f283c7d339df2ba629fe7ad462b4e30c983 100644 --- a/src/mpl2mpl/src/reflection_analysis.cpp +++ b/src/mpl2mpl/src/reflection_analysis.cpp @@ -1731,7 +1731,8 @@ static void ReflectionAnalysisGenStrTab(MIRModule &mirModule, const std::string } strTabSt->SetStorageClass(kScFstatic); for (char c : strTab) { - MIRConst *newConst = mirModule.GetMemPool()->New(c, *GlobalTables::GetTypeTable().GetUInt8()); + MIRConst *newConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst( + c, *GlobalTables::GetTypeTable().GetUInt8()); strTabAggconst->PushBack(newConst); } strTabSt->SetKonst(strTabAggconst); @@ -1798,6 +1799,12 @@ void ReflectionAnalysis::Run() { reflectionMuidStr.clear(); reflectionMuidStr.shrink_to_fit(); GenClassHashMetaData(); + for (Klass *klass : klasses) { + MIRStructType *mirStruct = klass->GetMIRStructType(); + mirStruct->GetPragmaVec().clear(); + mirStruct->GetPragmaVec().shrink_to_fit(); + } + memPoolCtrler.DeleteMemPool(mirModule->GetPragmaMemPool()); } AnalysisResult *DoReflectionAnalysis::Run(MIRModule *module, ModuleResultMgr *moduleResultMgr) { diff --git a/src/mpl2mpl/src/vtable_analysis.cpp b/src/mpl2mpl/src/vtable_analysis.cpp index d9b3b5dee950f0b02e03522ccdebd12712a9fe3a..2fe661600e8777b11e03c3a4924e795f232f5698 100644 --- a/src/mpl2mpl/src/vtable_analysis.cpp +++ b/src/mpl2mpl/src/vtable_analysis.cpp @@ -35,8 +35,8 @@ VtableAnalysis::VtableAnalysis(MIRModule *mod, KlassHierarchy *kh, bool dump) : voidPtrType = GlobalTables::GetTypeTable().GetVoidPtr(); // zeroConst and oneConst are shared amony itab entries. It is safe to share them because // they are never removed by anybody. - zeroConst = GetMIRModule().GetMemPool()->New(0, *voidPtrType); - oneConst = GetMIRModule().GetMemPool()->New(1, *voidPtrType); + zeroConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, *voidPtrType); + oneConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(1, *voidPtrType); for (Klass *klass : klassHierarchy->GetTopoSortedKlasses()) { ASSERT(klass != nullptr, "null ptr check!"); GenVtableList(*klass); @@ -296,13 +296,13 @@ void VtableAnalysis::GenItableDefinition(const Klass &klass) { auto *secondItabEmitArray = GetMIRModule().GetMemPool()->New(GetMIRModule(), *voidPtrType); // remember count in secondItabVec count = ((secondConflictList.size() | (1UL << (kShiftCountBit - 1))) << kShiftCountBit) + count; - secondItabEmitArray->PushBack(GetMIRModule().GetMemPool()->New(count, *voidPtrType)); + secondItabEmitArray->PushBack(GlobalTables::GetIntConstTable().GetOrCreateIntConst(count, *voidPtrType)); secondItabEmitArray->PushBack(oneConst); // padding for (uint32 i = 0; i < kItabSecondHashSize; ++i) { if (!secondItab[i] && !secondConflictFlag[i]) { continue; } else { - secondItabEmitArray->PushBack(GetMIRModule().GetMemPool()->New(i, *voidPtrType)); + secondItabEmitArray->PushBack(GlobalTables::GetIntConstTable().GetOrCreateIntConst(i, *voidPtrType)); if (secondItab[i]) { secondItabEmitArray->PushBack( GetMIRModule().GetMemPool()->New(secondItab[i]->GetPuidx(), *voidPtrType)); @@ -316,7 +316,7 @@ void VtableAnalysis::GenItableDefinition(const Klass &klass) { ASSERT_NOT_NULL(func); const std::string &signatureName = DecodeBaseNameWithType(*func); uint32 nameIdx = ReflectionAnalysis::FindOrInsertRepeatString(signatureName); - secondItabEmitArray->PushBack(GetMIRModule().GetMemPool()->New(nameIdx, *voidPtrType)); + secondItabEmitArray->PushBack(GlobalTables::GetIntConstTable().GetOrCreateIntConst(nameIdx, *voidPtrType)); secondItabEmitArray->PushBack( GetMIRModule().GetMemPool()->New(func->GetPuidx(), *voidPtrType)); }