[llvm] r258063 - [TableGen] Allocate the Init pointer array for BitsInit/ListInit after the BitsInit/ListInit object itself. Saves a bit of memory. NFC

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 18 11:52:25 PST 2016


Author: ctopper
Date: Mon Jan 18 13:52:24 2016
New Revision: 258063

URL: http://llvm.org/viewvc/llvm-project?rev=258063&view=rev
Log:
[TableGen] Allocate the Init pointer array for BitsInit/ListInit after the BitsInit/ListInit object itself. Saves a bit of memory. NFC

Modified:
    llvm/trunk/include/llvm/TableGen/Record.h
    llvm/trunk/lib/TableGen/Record.cpp

Modified: llvm/trunk/include/llvm/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TableGen/Record.h?rev=258063&r1=258062&r2=258063&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h (original)
+++ llvm/trunk/include/llvm/TableGen/Record.h Mon Jan 18 13:52:24 2016
@@ -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

Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=258063&r1=258062&r2=258063&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Mon Jan 18 13:52:24 2016
@@ -274,14 +274,17 @@ BitsInit *BitsInit::get(ArrayRef<Init *>
   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
   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 *>
   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 *>
 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
 
 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::resolveListElementRefere
 
 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 + "]";
 }




More information about the llvm-commits mailing list