diff --git a/include/llvm/TableGen/Record.h b/include/llvm/TableGen/Record.h index 55c3d23e4b6..764a5f6cf86 100644 --- a/include/llvm/TableGen/Record.h +++ b/include/llvm/TableGen/Record.h @@ -63,7 +63,7 @@ public: private: RecTyKind Kind; - std::unique_ptr ListTy; + ListRecTy *ListTy = nullptr; public: RecTy(RecTyKind K) : Kind(K) {} @@ -408,11 +408,6 @@ class TypedInit : public Init { protected: explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0) : Init(K, Opc), Ty(T) {} - ~TypedInit() override { - // If this is a DefInit we need to delete the RecordRecTy. - if (getKind() == IK_DefInit) - delete Ty; - } public: TypedInit(const TypedInit &Other) = delete; @@ -602,7 +597,7 @@ public: /// "foo" - Represent an initialization by a string value. /// class StringInit : public TypedInit { - std::string Value; + StringRef Value; explicit StringInit(StringRef V) : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {} @@ -621,7 +616,7 @@ public: Init *convertInitializerTo(RecTy *Ty) const override; - std::string getAsString() const override { return "\"" + Value + "\""; } + std::string getAsString() const override { return "\"" + Value.str() + "\""; } std::string getAsUnquotedString() const override { return Value; } @@ -639,7 +634,7 @@ public: }; class CodeInit : public TypedInit { - std::string Value; + StringRef Value; explicit CodeInit(StringRef V) : TypedInit(IK_CodeInit, static_cast(CodeRecTy::get())), @@ -660,7 +655,7 @@ public: Init *convertInitializerTo(RecTy *Ty) const override; std::string getAsString() const override { - return "[{" + Value + "}]"; + return "[{" + Value.str() + "}]"; } std::string getAsUnquotedString() const override { return Value; } @@ -1276,7 +1271,7 @@ class Record { // Tracks Record instances. Not owned by Record. RecordKeeper &TrackedRecords; - std::unique_ptr TheInit; + DefInit *TheInit = nullptr; // Unique record ID. unsigned ID; diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 7fa20d115e0..e705fe8977c 100644 --- a/lib/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -28,6 +28,8 @@ using namespace llvm; +static BumpPtrAllocator Allocator; + //===----------------------------------------------------------------------===// // std::string wrapper for DenseMap purposes //===----------------------------------------------------------------------===// @@ -97,8 +99,8 @@ LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); } ListRecTy *RecTy::getListTy() { if (!ListTy) - ListTy.reset(new ListRecTy(this)); - return ListTy.get(); + ListTy = new(Allocator) ListRecTy(this); + return ListTy; } bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { @@ -115,13 +117,13 @@ bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ } BitsRecTy *BitsRecTy::get(unsigned Sz) { - static std::vector> Shared; + static std::vector Shared; if (Sz >= Shared.size()) Shared.resize(Sz + 1); - std::unique_ptr &Ty = Shared[Sz]; + BitsRecTy *&Ty = Shared[Sz]; if (!Ty) - Ty.reset(new BitsRecTy(Sz)); - return Ty.get(); + Ty = new(Allocator) BitsRecTy(Sz); + return Ty; } std::string BitsRecTy::getAsString() const { @@ -268,7 +270,7 @@ ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef Range) { BitsInit *BitsInit::get(ArrayRef Range) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileBitsInit(ID, Range); @@ -277,12 +279,13 @@ BitsInit *BitsInit::get(ArrayRef Range) { if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - void *Mem = ::operator new (totalSizeToAlloc(Range.size())); - BitsInit *I = new (Mem) BitsInit(Range.size()); + void *Mem = Allocator.Allocate(totalSizeToAlloc(Range.size()), + alignof(BitsInit)); + BitsInit *I = new(Mem) BitsInit(Range.size()); std::uninitialized_copy(Range.begin(), Range.end(), I->getTrailingObjects()); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -398,11 +401,11 @@ Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { } IntInit *IntInit::get(int64_t V) { - static DenseMap> ThePool; + static DenseMap ThePool; - std::unique_ptr &I = ThePool[V]; - if (!I) I.reset(new IntInit(V)); - return I.get(); + IntInit *&I = ThePool[V]; + if (!I) I = new(Allocator) IntInit(V); + return I; } std::string IntInit::getAsString() const { @@ -455,19 +458,27 @@ IntInit::convertInitializerBitRange(const std::vector &Bits) const { } CodeInit *CodeInit::get(StringRef V) { - static StringMap> ThePool; + static DenseMap ThePool; - std::unique_ptr &I = ThePool[V]; - if (!I) I.reset(new CodeInit(V)); - return I.get(); + auto I = ThePool.insert(std::make_pair(V, nullptr)); + if (I.second) { + StringRef VCopy = V.copy(Allocator); + I.first->first = VCopy; + I.first->second = new(Allocator) CodeInit(VCopy); + } + return I.first->second; } StringInit *StringInit::get(StringRef V) { - static StringMap> ThePool; + static DenseMap ThePool; - std::unique_ptr &I = ThePool[V]; - if (!I) I.reset(new StringInit(V)); - return I.get(); + auto I = ThePool.insert(std::make_pair(V, nullptr)); + if (I.second) { + StringRef VCopy = V.copy(Allocator); + I.first->first = VCopy; + I.first->second = new(Allocator) StringInit(VCopy); + } + return I.first->second; } Init *StringInit::convertInitializerTo(RecTy *Ty) const { @@ -496,7 +507,7 @@ static void ProfileListInit(FoldingSetNodeID &ID, ListInit *ListInit::get(ArrayRef Range, RecTy *EltTy) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileListInit(ID, Range, EltTy); @@ -505,12 +516,13 @@ ListInit *ListInit::get(ArrayRef Range, RecTy *EltTy) { if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - void *Mem = ::operator new (totalSizeToAlloc(Range.size())); - ListInit *I = new (Mem) ListInit(Range.size(), EltTy); + void *Mem = Allocator.Allocate(totalSizeToAlloc(Range.size()), + alignof(ListInit)); + ListInit *I = new(Mem) ListInit(Range.size(), EltTy); std::uninitialized_copy(Range.begin(), Range.end(), I->getTrailingObjects()); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -634,7 +646,7 @@ ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) { UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileUnOpInit(ID, Opc, LHS, Type); @@ -643,9 +655,9 @@ UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) { if (UnOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - UnOpInit *I = new UnOpInit(Opc, LHS, Type); + UnOpInit *I = new(Allocator) UnOpInit(Opc, LHS, Type); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -782,7 +794,7 @@ ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS, BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, Init *RHS, RecTy *Type) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileBinOpInit(ID, Opc, LHS, RHS, Type); @@ -791,9 +803,9 @@ BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, if (BinOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - BinOpInit *I = new BinOpInit(Opc, LHS, RHS, Type); + BinOpInit *I = new(Allocator) BinOpInit(Opc, LHS, RHS, Type); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -937,7 +949,7 @@ ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS, TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS, RecTy *Type) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type); @@ -946,9 +958,9 @@ TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS, if (TernOpInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - TernOpInit *I = new TernOpInit(Opc, LHS, MHS, RHS, Type); + TernOpInit *I = new(Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -1289,13 +1301,14 @@ VarInit *VarInit::get(StringRef VN, RecTy *T) { VarInit *VarInit::get(Init *VN, RecTy *T) { typedef std::pair Key; - static DenseMap> ThePool; + static DenseMap ThePool; Key TheKey(std::make_pair(T, VN)); - std::unique_ptr &I = ThePool[TheKey]; - if (!I) I.reset(new VarInit(VN, T)); - return I.get(); + VarInit *&I = ThePool[TheKey]; + if (!I) + I = new(Allocator) VarInit(VN, T); + return I; } StringRef VarInit::getName() const { @@ -1363,13 +1376,14 @@ Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { typedef std::pair Key; - static DenseMap> ThePool; + static DenseMap ThePool; Key TheKey(std::make_pair(T, B)); - std::unique_ptr &I = ThePool[TheKey]; - if (!I) I.reset(new VarBitInit(T, B)); - return I.get(); + VarBitInit *&I = ThePool[TheKey]; + if (!I) + I = new(Allocator) VarBitInit(T, B); + return I; } Init *VarBitInit::convertInitializerTo(RecTy *Ty) const { @@ -1394,13 +1408,13 @@ Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { VarListElementInit *VarListElementInit::get(TypedInit *T, unsigned E) { typedef std::pair Key; - static DenseMap> ThePool; + static DenseMap ThePool; Key TheKey(std::make_pair(T, E)); - std::unique_ptr &I = ThePool[TheKey]; - if (!I) I.reset(new VarListElementInit(T, E)); - return I.get(); + VarListElementInit *&I = ThePool[TheKey]; + if (!I) I = new(Allocator) VarListElementInit(T, E); + return I; } std::string VarListElementInit::getAsString() const { @@ -1464,13 +1478,13 @@ std::string DefInit::getAsString() const { FieldInit *FieldInit::get(Init *R, StringRef FN) { typedef std::pair Key; - static DenseMap> ThePool; + static DenseMap ThePool; Key TheKey(std::make_pair(R, FN)); - std::unique_ptr &I = ThePool[TheKey]; - if (!I) I.reset(new FieldInit(R, FN)); - return I.get(); + FieldInit *&I = ThePool[TheKey]; + if (!I) I = new(Allocator) FieldInit(R, FN); + return I; } Init *FieldInit::getBit(unsigned Bit) const { @@ -1528,7 +1542,7 @@ DagInit * DagInit::get(Init *V, StringRef VN, ArrayRef ArgRange, ArrayRef NameRange) { static FoldingSet ThePool; - static std::vector> TheActualPool; + static std::vector TheActualPool; FoldingSetNodeID ID; ProfileDagInit(ID, V, VN, ArgRange, NameRange); @@ -1537,9 +1551,9 @@ DagInit::get(Init *V, StringRef VN, ArrayRef ArgRange, if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - DagInit *I = new DagInit(V, VN, ArgRange, NameRange); + DagInit *I = new(Allocator) DagInit(V, VN, ArgRange, NameRange); ThePool.InsertNode(I, IP); - TheActualPool.push_back(std::unique_ptr(I)); + TheActualPool.push_back(I); return I; } @@ -1648,8 +1662,8 @@ void Record::checkName() { DefInit *Record::getDefInit() { if (!TheInit) - TheInit.reset(new DefInit(this, new RecordRecTy(this))); - return TheInit.get(); + TheInit = new(Allocator) DefInit(this, new(Allocator) RecordRecTy(this)); + return TheInit; } StringRef Record::getName() const {