From 0162c1815e5a1b33750d313c49f707bc446ea946 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 18 Aug 2007 06:14:52 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41166 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Function.h | 63 ++++++++++++++++++++++++++++++----------- lib/VMCore/Function.cpp | 31 +++++++++++++++----- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/include/llvm/Function.h b/include/llvm/Function.h index b3b9716d4df..7776c29f1d7 100644 --- a/include/llvm/Function.h +++ b/include/llvm/Function.h @@ -65,11 +65,10 @@ public: 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 @@ private: 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 @@ public: /// 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 @@ public: /// 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 @@ public: //===--------------------------------------------------------------------===// // 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); } diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index 1374d55e7d9..04541dfbfdc 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -152,13 +152,10 @@ Function::Function(const FunctionType *Ty, LinkageTypes Linkage, 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 @@ Function::~Function() { 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(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);