[llvm-commits] [llvm] r41166 - in /llvm/trunk: include/llvm/Function.h lib/VMCore/Function.cpp

Chris Lattner sabre at nondot.org
Fri Aug 17 23:14:52 PDT 2007


Author: lattner
Date: Sat Aug 18 01:14:52 2007
New Revision: 41166

URL: http://llvm.org/viewvc/llvm-project?rev=41166&view=rev
Log:
Compute the argument list as lazily as possible.  This ensures that clients
that don't use it don't have to pay the memory cost for the arguments.  This
allows us to avoid creating Argument nodes for many prototypes and for clients
who lazily deserialize code from a bytecode file.

Modified:
    llvm/trunk/include/llvm/Function.h
    llvm/trunk/lib/VMCore/Function.cpp

Modified: llvm/trunk/include/llvm/Function.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Function.h?rev=41166&r1=41165&r2=41166&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Function.h (original)
+++ llvm/trunk/include/llvm/Function.h Sat Aug 18 01:14:52 2007
@@ -65,11 +65,10 @@
 
 private:
   // Important things that make up a function!
-  BasicBlockListType  BasicBlocks;   ///< The basic blocks
-  ArgumentListType ArgumentList;     ///< The formal arguments
-  ValueSymbolTable *SymTab;          ///< Symbol table of args/instructions
-  ParamAttrsList *ParamAttrs;        ///< Parameter attributes
-
+  BasicBlockListType  BasicBlocks;        ///< The basic blocks
+  mutable ArgumentListType ArgumentList;  ///< The formal arguments
+  ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
+  ParamAttrsList *ParamAttrs;             ///< Parameter attributes
   
   // The Calling Convention is stored in Value::SubclassData.
   /*unsigned CallingConvention;*/
@@ -90,6 +89,18 @@
   Function *getPrev()             { return Prev; }
   const Function *getPrev() const { return Prev; }
 
+  /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
+  /// built on demand, so that the list isn't allocated until the first client
+  /// needs it.  The hasLazyArguments predicate returns true if the arg list
+  /// hasn't been set up yet.
+  bool hasLazyArguments() const {
+    return SubclassData & 1;
+  }
+  void CheckLazyArguments() const {
+    if (hasLazyArguments())
+      BuildLazyArguments();
+  }
+  void BuildLazyArguments() const;
 public:
   /// Function ctor - If the (optional) Module argument is specified, the
   /// function is automatically inserted into the end of the function list for
@@ -125,9 +136,11 @@
   /// getCallingConv()/setCallingConv(uint) - These method get and set the
   /// calling convention of this function.  The enum values for the known
   /// calling conventions are defined in CallingConv.h.
-  unsigned getCallingConv() const { return SubclassData; }
-  void setCallingConv(unsigned CC) { SubclassData = CC; }
-
+  unsigned getCallingConv() const { return SubclassData >> 1; }
+  void setCallingConv(unsigned CC) {
+    SubclassData = (SubclassData & 1) | CC << 1;
+  }
+  
   /// Obtains a constant pointer to the ParamAttrsList object which holds the
   /// parameter attributes information, if any. 
   /// @returns 0 if no parameter attributes have been set.
@@ -161,8 +174,14 @@
   /// Get the underlying elements of the Function... the basic block list is
   /// empty for external functions.
   ///
-  const ArgumentListType &getArgumentList() const { return ArgumentList; }
-        ArgumentListType &getArgumentList()       { return ArgumentList; }
+  const ArgumentListType &getArgumentList() const {
+    CheckLazyArguments();
+    return ArgumentList;
+  }
+  ArgumentListType &getArgumentList() {
+    CheckLazyArguments();
+    return ArgumentList;
+  }
 
   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
@@ -197,13 +216,25 @@
   //===--------------------------------------------------------------------===//
   // Argument iterator forwarding functions
   //
-  arg_iterator                arg_begin()       { return ArgumentList.begin(); }
-  const_arg_iterator          arg_begin() const { return ArgumentList.begin(); }
-  arg_iterator                arg_end  ()       { return ArgumentList.end();   }
-  const_arg_iterator          arg_end  () const { return ArgumentList.end();   }
+  arg_iterator arg_begin() {
+    CheckLazyArguments();
+    return ArgumentList.begin();
+  }
+  const_arg_iterator arg_begin() const {
+    CheckLazyArguments();
+    return ArgumentList.begin();
+  }
+  arg_iterator arg_end() {
+    CheckLazyArguments();
+    return ArgumentList.end();
+  }
+  const_arg_iterator arg_end() const {
+    CheckLazyArguments();
+    return ArgumentList.end();
+  }
 
-  size_t                      arg_size () const { return ArgumentList.size();  }
-  bool                        arg_empty() const { return ArgumentList.empty(); }
+  size_t arg_size() const;
+  bool arg_empty() const;
 
   virtual void print(std::ostream &OS) const { print(OS, 0); }
   void print(std::ostream *OS) const { if (OS) print(*OS); }

Modified: llvm/trunk/lib/VMCore/Function.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Function.cpp?rev=41166&r1=41165&r2=41166&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Function.cpp (original)
+++ llvm/trunk/lib/VMCore/Function.cpp Sat Aug 18 01:14:52 2007
@@ -152,13 +152,10 @@
   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
          && "LLVM functions cannot return aggregate values!");
 
-  // Create the arguments vector, all arguments start out unnamed.
-  for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
-    assert(Ty->getParamType(i) != Type::VoidTy &&
-           "Cannot have void typed arguments!");
-    ArgumentList.push_back(new Argument(Ty->getParamType(i)));
-  }
-
+  // If the function has arguments, mark them as lazily built.
+  if (Ty->getNumParams())
+    SubclassData = 1;   // Set the "has lazy arguments" bit.
+  
   // Make sure that we get added to a function
   LeakDetector::addGarbageObject(this);
 
@@ -178,6 +175,26 @@
     ParamAttrs->dropRef();
 }
 
+void Function::BuildLazyArguments() const {
+  // Create the arguments vector, all arguments start out unnamed.
+  const FunctionType *FT = getFunctionType();
+  for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
+    assert(FT->getParamType(i) != Type::VoidTy &&
+           "Cannot have void typed arguments!");
+    ArgumentList.push_back(new Argument(FT->getParamType(i)));
+  }
+  
+  // Clear the lazy arguments bit.
+  const_cast<Function*>(this)->SubclassData &= ~1;
+}
+
+size_t Function::arg_size() const {
+  return getFunctionType()->getNumParams();
+}
+bool Function::arg_empty() const {
+  return getFunctionType()->getNumParams() == 0;
+}
+
 void Function::setParent(Module *parent) {
   if (getParent())
     LeakDetector::addGarbageObject(this);





More information about the llvm-commits mailing list