diff options
| -rw-r--r-- | llvm/include/llvm/TableGen/Record.h | 45 | ||||
| -rw-r--r-- | llvm/lib/TableGen/Record.cpp | 22 |
2 files changed, 38 insertions, 29 deletions
diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index 4c1ef4013dd..749f2d9aa5b 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -22,6 +22,7 @@ #include "llvm/Support/DataTypes.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/SMLoc.h" +#include "llvm/Support/TrailingObjects.h" #include "llvm/Support/raw_ostream.h" #include <map> @@ -456,12 +457,12 @@ public: /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value. /// It contains a vector of bits, whose size is determined by the type. /// -class BitsInit : public TypedInit, public FoldingSetNode { - std::vector<Init*> Bits; +class BitsInit final : public TypedInit, public FoldingSetNode, + public TrailingObjects<BitsInit, Init *> { + unsigned NumBits; - BitsInit(ArrayRef<Init *> Range) - : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())), - Bits(Range.begin(), Range.end()) {} + BitsInit(unsigned N) + : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {} BitsInit(const BitsInit &Other) = delete; BitsInit &operator=(const BitsInit &Other) = delete; @@ -474,7 +475,7 @@ public: void Profile(FoldingSetNodeID &ID) const; - unsigned getNumBits() const { return Bits.size(); } + unsigned getNumBits() const { return NumBits; } Init *convertInitializerTo(RecTy *Ty) const override; Init * @@ -503,8 +504,8 @@ public: Init *resolveReferences(Record &R, const RecordVal *RV) const override; Init *getBit(unsigned Bit) const override { - assert(Bit < Bits.size() && "Bit index out of range!"); - return Bits[Bit]; + assert(Bit < NumBits && "Bit index out of range!"); + return getTrailingObjects<Init *>()[Bit]; } }; @@ -585,16 +586,16 @@ public: /// ListInit - [AL, AH, CL] - Represent a list of defs /// -class ListInit : public TypedInit, public FoldingSetNode { - std::vector<Init*> Values; +class ListInit final : public TypedInit, public FoldingSetNode, + public TrailingObjects<BitsInit, Init *> { + unsigned NumValues; public: - typedef std::vector<Init*>::const_iterator const_iterator; + typedef Init *const *const_iterator; private: - explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy) - : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), - Values(Range.begin(), Range.end()) {} + explicit ListInit(unsigned N, RecTy *EltTy) + : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {} ListInit(const ListInit &Other) = delete; ListInit &operator=(const ListInit &Other) = delete; @@ -608,8 +609,8 @@ public: void Profile(FoldingSetNodeID &ID) const; Init *getElement(unsigned i) const { - assert(i < Values.size() && "List element index out of range!"); - return Values[i]; + assert(i < NumValues && "List element index out of range!"); + return getTrailingObjects<Init *>()[i]; } Record *getElementAsRecord(unsigned i) const; @@ -628,13 +629,15 @@ public: std::string getAsString() const override; - ArrayRef<Init*> getValues() const { return Values; } + ArrayRef<Init*> getValues() const { + return makeArrayRef(getTrailingObjects<Init *>(), NumValues); + } - const_iterator begin() const { return Values.begin(); } - const_iterator end () const { return Values.end(); } + const_iterator begin() const { return getTrailingObjects<Init *>(); } + const_iterator end () const { return begin() + NumValues; } - size_t size () const { return Values.size(); } - bool empty() const { return Values.empty(); } + size_t size () const { return NumValues; } + bool empty() const { return NumValues == 0; } /// resolveListElementReference - This method is used to implement /// VarListElementInit::resolveReferences. If the list element is resolvable diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 11e35b75375..943b101d9a3 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -274,14 +274,17 @@ BitsInit *BitsInit::get(ArrayRef<Init *> Range) { if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - BitsInit *I = new BitsInit(Range); + void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size())); + BitsInit *I = new (Mem) BitsInit(Range.size()); + std::uninitialized_copy(Range.begin(), Range.end(), + I->getTrailingObjects<Init *>()); ThePool.InsertNode(I, IP); TheActualPool.push_back(std::unique_ptr<BitsInit>(I)); return I; } void BitsInit::Profile(FoldingSetNodeID &ID) const { - ProfileBitsInit(ID, Bits); + ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits)); } Init *BitsInit::convertInitializerTo(RecTy *Ty) const { @@ -355,7 +358,7 @@ Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { bool CachedBitVarChanged = false; for (unsigned i = 0, e = getNumBits(); i != e; ++i) { - Init *CurBit = Bits[i]; + Init *CurBit = getBit(i); Init *CurBitVar = CurBit->getBitVar(); NewBits[i] = CurBit; @@ -486,7 +489,10 @@ ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) return I; - ListInit *I = new ListInit(Range, EltTy); + void *Mem = ::operator new (totalSizeToAlloc<Init *>(Range.size())); + ListInit *I = new (Mem) ListInit(Range.size(), EltTy); + std::uninitialized_copy(Range.begin(), Range.end(), + I->getTrailingObjects<Init *>()); ThePool.InsertNode(I, IP); TheActualPool.push_back(std::unique_ptr<ListInit>(I)); return I; @@ -495,7 +501,7 @@ ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { void ListInit::Profile(FoldingSetNodeID &ID) const { RecTy *EltTy = cast<ListRecTy>(getType())->getElementType(); - ProfileListInit(ID, Values, EltTy); + ProfileListInit(ID, getValues(), EltTy); } Init *ListInit::convertInitializerTo(RecTy *Ty) const { @@ -530,7 +536,7 @@ ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { Record *ListInit::getElementAsRecord(unsigned i) const { assert(i < Values.size() && "List element index out of range!"); - DefInit *DI = dyn_cast<DefInit>(Values[i]); + DefInit *DI = dyn_cast<DefInit>(getElement(i)); if (!DI) PrintFatalError("Expected record in list!"); return DI->getDef(); @@ -572,9 +578,9 @@ Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, std::string ListInit::getAsString() const { std::string Result = "["; - for (unsigned i = 0, e = Values.size(); i != e; ++i) { + for (unsigned i = 0, e = NumValues; i != e; ++i) { if (i) Result += ", "; - Result += Values[i]->getAsString(); + Result += getElement(i)->getAsString(); } return Result + "]"; } |

