The (negative) offset from a SymbolTableListTraits-using ilist to its container
authorChris Lattner <sabre@nondot.org>
Tue, 17 Apr 2007 04:04:14 +0000 (04:04 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 17 Apr 2007 04:04:14 +0000 (04:04 +0000)
object is always constant.  As such, evaluate it at compile time instead of storing
it as an ivar in SymbolTableListTraits.  This shrinks every SymbolTableListTraits
ilist by a word, shrinking BasicBlock from 44->40 bytes, Function from 96->88 bytes,
and Module from 60->52 bytes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36189 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/BasicBlock.h
include/llvm/Function.h
include/llvm/Module.h
include/llvm/SymbolTableListTraits.h
lib/VMCore/BasicBlock.cpp
lib/VMCore/Function.cpp
lib/VMCore/Module.cpp
lib/VMCore/SymbolTableListTraitsImpl.h

index 8cc450c127d1b8820b858a0090e6b309861fd6bd..0a22f44b985bc3420543b61d758084b90ae7c571 100644 (file)
@@ -31,6 +31,7 @@ template<> struct ilist_traits<Instruction>
   static void destroySentinel(Instruction *I) { delete I; }
   static iplist<Instruction> &getList(BasicBlock *BB);
   static ValueSymbolTable *getSymTab(BasicBlock *ItemParent);
+  static int getListOffset();
 };
 
 /// This represents a single basic block in LLVM. A basic block is simply a
@@ -194,8 +195,19 @@ public:
   /// the basic block).
   ///
   BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = "");
+  
+  
+  static unsigned getInstListOffset() {
+    BasicBlock *Obj = 0;
+    return reinterpret_cast<unsigned>(&Obj->InstList);
+  }
 };
 
+inline int 
+ilist_traits<Instruction>::getListOffset() {
+  return BasicBlock::getInstListOffset();
+}
+
 } // End llvm namespace
 
 #endif
index d42c8d8aa2b5d66889c6779dbe7d13012bf10fcc..3fdbd02500817c74bd652048090b6a4b2cfc3f57 100644 (file)
@@ -37,6 +37,7 @@ template<> struct ilist_traits<BasicBlock>
   static void destroySentinel(BasicBlock *BB) { delete BB; }
   static iplist<BasicBlock> &getList(Function *F);
   static ValueSymbolTable *getSymTab(Function *ItemParent);
+  static int getListOffset();
 };
 
 template<> struct ilist_traits<Argument>
@@ -47,6 +48,7 @@ template<> struct ilist_traits<Argument>
   static void destroySentinel(Argument *A) { delete A; }
   static iplist<Argument> &getList(Function *F);
   static ValueSymbolTable *getSymTab(Function *ItemParent);
+  static int getListOffset();
 };
 
 class Function : public GlobalValue, public Annotable {
@@ -238,6 +240,15 @@ public:
   /// including any contained basic blocks.
   ///
   void dropAllReferences();
+  
+  static unsigned getBasicBlockListOffset() {
+    Function *Obj = 0;
+    return reinterpret_cast<unsigned>(&Obj->BasicBlocks);
+  }
+  static unsigned getArgumentListOffset() {
+    Function *Obj = 0;
+    return reinterpret_cast<unsigned>(&Obj->ArgumentList);
+  }
 };
 
 inline ValueSymbolTable *
@@ -250,6 +261,17 @@ ilist_traits<Argument>::getSymTab(Function *F) {
   return F ? &F->getValueSymbolTable() : 0;
 }
 
+inline int 
+ilist_traits<BasicBlock>::getListOffset() {
+  return Function::getBasicBlockListOffset();
+}
+
+inline int 
+ilist_traits<Argument>::getListOffset() {
+  return Function::getArgumentListOffset();
+}
+
+
 } // End llvm namespace
 
 #endif
index e645f51c1c3a426fddaabf8c9e029a1560795288..1d82e2aad3d58ca564b23da9d5172e09cc4a95be 100644 (file)
@@ -32,6 +32,7 @@ template<> struct ilist_traits<Function>
   static void destroySentinel(Function *F) { delete F; }
   static iplist<Function> &getList(Module *M);
   static inline ValueSymbolTable *getSymTab(Module *M);
+  static int getListOffset();
 };
 template<> struct ilist_traits<GlobalVariable>
   : public SymbolTableListTraits<GlobalVariable, Module> {
@@ -40,6 +41,7 @@ template<> struct ilist_traits<GlobalVariable>
   static void destroySentinel(GlobalVariable *GV) { delete GV; }
   static iplist<GlobalVariable> &getList(Module *M);
   static inline ValueSymbolTable *getSymTab(Module *M);
+  static int getListOffset();
 };
 
 /// A Module instance is used to store all the information related to an
@@ -313,6 +315,15 @@ public:
   /// that has "dropped all references", except operator delete.
   void dropAllReferences();
 /// @}
+
+  static unsigned getFunctionListOffset() {
+    Module *Obj = 0;
+    return reinterpret_cast<unsigned>(&Obj->FunctionList);
+  }
+  static unsigned getGlobalVariableListOffset() {
+    Module *Obj = 0;
+    return reinterpret_cast<unsigned>(&Obj->GlobalList);
+  }
 };
 
 /// An iostream inserter for modules.
@@ -331,6 +342,15 @@ ilist_traits<GlobalVariable>::getSymTab(Module *M) {
   return M ? &M->getValueSymbolTable() : 0;
 }
 
+inline int 
+ilist_traits<Function>::getListOffset() {
+  return Module::getFunctionListOffset();
+}
+
+inline int 
+ilist_traits<GlobalVariable>::getListOffset() {
+  return Module::getGlobalVariableListOffset();
+}
 
 } // End llvm namespace
 
index 099cfe0ca7fe256dd20502f5e1891f561b25e340..205b409e8eb23b332a40e01daa425e57c9fd4894 100644 (file)
@@ -39,10 +39,15 @@ template<typename Ty> struct ilist_traits;
 template<typename ValueSubClass, typename ItemParentClass>
 class SymbolTableListTraits {
   typedef ilist_traits<ValueSubClass> TraitsClass;
-  ItemParentClass *ItemParent;
 public:
-  SymbolTableListTraits() : ItemParent(0) {}
+  SymbolTableListTraits() {}
 
+  /// getListOwner - Return the object that owns this list.  If this is a list
+  /// of instructions, it returns the BasicBlock that owns them.
+  ItemParentClass *getListOwner() {
+    return reinterpret_cast<ItemParentClass*>((char*)this-
+                                              TraitsClass::getListOffset());
+  }
   static ValueSubClass *getPrev(ValueSubClass *V) { return V->getPrev(); }
   static ValueSubClass *getNext(ValueSubClass *V) { return V->getNext(); }
   static const ValueSubClass *getPrev(const ValueSubClass *V) {
@@ -62,7 +67,6 @@ public:
                              ilist_iterator<ValueSubClass> first,
                              ilist_iterator<ValueSubClass> last);
 //private:
-  void setItemParent(ItemParentClass *IP) { ItemParent = IP; }
   template<typename TPtr>
   void setSymTabObject(TPtr *, TPtr);
 };
index e10948e2ad3103133240aac3b6cf4993c5fe1635..431bb5090a764870d1f8179ef97667c2b3f75747 100644 (file)
@@ -72,8 +72,6 @@ template class SymbolTableListTraits<Instruction, BasicBlock>;
 BasicBlock::BasicBlock(const std::string &Name, Function *NewParent,
                        BasicBlock *InsertBefore)
   : Value(Type::LabelTy, Value::BasicBlockVal), Parent(0) {
-  // Initialize the instlist.
-  InstList.setItemParent(this);
 
   // Make sure that we get added to a function
   LeakDetector::addGarbageObject(this);
index 7949e39f84d0c0d6260a94a66684a771a96adb3b..e47798e12cd277b0dcb264f046209896ff20babc 100644 (file)
@@ -143,8 +143,6 @@ Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
   ParamAttrs = 0;
   CallingConvention = 0;
-  BasicBlocks.setItemParent(this);
-  ArgumentList.setItemParent(this);
   SymTab = new ValueSymbolTable();
 
   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
index 465cb6944586c9d8555055030a367adaa4886bf9..ddd503dea1c9cf3793b21baa08d1f84def9b7cee 100644 (file)
@@ -64,8 +64,6 @@ template class SymbolTableListTraits<Function, Module>;
 
 Module::Module(const std::string &MID)
   : ModuleID(MID), DataLayout("") {
-  FunctionList.setItemParent(this);
-  GlobalList.setItemParent(this);
   ValSymTab = new ValueSymbolTable();
   TypeSymTab = new TypeSymbolTable();
 }
index ce2c0c0cc79ef0e488b7f223d3363a8732586f7c..b2b6a3fa346db059c6ffe7fc82cda97b87f7a57e 100644 (file)
@@ -29,19 +29,19 @@ template<typename TPtr>
 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
 ::setSymTabObject(TPtr *Dest, TPtr Src) {
   // Get the old symtab and value list before doing the assignment.
-  ValueSymbolTable *OldST = TraitsClass::getSymTab(ItemParent);
+  ValueSymbolTable *OldST = TraitsClass::getSymTab(getListOwner());
 
   // Do it.
   *Dest = Src;
   
   // Get the new SymTab object.
-  ValueSymbolTable *NewST = TraitsClass::getSymTab(ItemParent);
+  ValueSymbolTable *NewST = TraitsClass::getSymTab(getListOwner());
   
   // If there is nothing to do, quick exit.
   if (OldST == NewST) return;
   
   // Move all the elements from the old symtab to the new one.
-  iplist<ValueSubClass> &ItemList = TraitsClass::getList(ItemParent);
+  iplist<ValueSubClass> &ItemList = TraitsClass::getList(getListOwner());
   if (ItemList.empty()) return;
   
   if (OldST) {
@@ -66,9 +66,10 @@ template<typename ValueSubClass, typename ItemParentClass>
 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
 ::addNodeToList(ValueSubClass *V) {
   assert(V->getParent() == 0 && "Value already in a container!!");
-  V->setParent(ItemParent);
+  ItemParentClass *Owner = getListOwner();
+  V->setParent(Owner);
   if (V->hasName())
-    if (ValueSymbolTable *ST = TraitsClass::getSymTab(ItemParent))
+    if (ValueSymbolTable *ST = TraitsClass::getSymTab(Owner))
       ST->reinsertValue(V);
 }
 
@@ -77,7 +78,7 @@ void SymbolTableListTraits<ValueSubClass,ItemParentClass>
 ::removeNodeFromList(ValueSubClass *V) {
   V->setParent(0);
   if (V->hasName())
-    if (ValueSymbolTable *ST = TraitsClass::getSymTab(ItemParent))
+    if (ValueSymbolTable *ST = TraitsClass::getSymTab(getListOwner()))
       ST->removeValueName(V->getValueName());
 }
 
@@ -87,12 +88,12 @@ void SymbolTableListTraits<ValueSubClass,ItemParentClass>
                         ilist_iterator<ValueSubClass> first,
                         ilist_iterator<ValueSubClass> last) {
   // We only have to do work here if transferring instructions between BBs
-  ItemParentClass *NewIP = ItemParent, *OldIP = L2.ItemParent;
+  ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
   if (NewIP == OldIP) return;  // No work to do at all...
 
   // We only have to update symbol table entries if we are transferring the
   // instructions to a different symtab object...
-  ValueSymbolTable *NewST = TraitsClass::getSymTab(ItemParent);
+  ValueSymbolTable *NewST = TraitsClass::getSymTab(NewIP);
   ValueSymbolTable *OldST = TraitsClass::getSymTab(OldIP);
   if (NewST != OldST) {
     for (; first != last; ++first) {