LLVM API Documentation
00001 //===-- Function.cpp - Implement the Global object classes ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Function class for the VMCore library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Module.h" 00015 #include "llvm/DerivedTypes.h" 00016 #include "llvm/IntrinsicInst.h" 00017 #include "llvm/LLVMContext.h" 00018 #include "llvm/CodeGen/ValueTypes.h" 00019 #include "llvm/Support/LeakDetector.h" 00020 #include "llvm/Support/ManagedStatic.h" 00021 #include "llvm/Support/StringPool.h" 00022 #include "llvm/System/RWMutex.h" 00023 #include "llvm/System/Threading.h" 00024 #include "SymbolTableListTraitsImpl.h" 00025 #include "llvm/ADT/DenseMap.h" 00026 #include "llvm/ADT/StringExtras.h" 00027 using namespace llvm; 00028 00029 00030 // Explicit instantiations of SymbolTableListTraits since some of the methods 00031 // are not in the public header file... 00032 template class SymbolTableListTraits<Argument, Function>; 00033 template class SymbolTableListTraits<BasicBlock, Function>; 00034 00035 //===----------------------------------------------------------------------===// 00036 // Argument Implementation 00037 //===----------------------------------------------------------------------===// 00038 00039 Argument::Argument(const Type *Ty, const Twine &Name, Function *Par) 00040 : Value(Ty, Value::ArgumentVal) { 00041 Parent = 0; 00042 00043 // Make sure that we get added to a function 00044 LeakDetector::addGarbageObject(this); 00045 00046 if (Par) 00047 Par->getArgumentList().push_back(this); 00048 setName(Name); 00049 } 00050 00051 void Argument::setParent(Function *parent) { 00052 if (getParent()) 00053 LeakDetector::addGarbageObject(this); 00054 Parent = parent; 00055 if (getParent()) 00056 LeakDetector::removeGarbageObject(this); 00057 } 00058 00059 /// getArgNo - Return the index of this formal argument in its containing 00060 /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. 00061 unsigned Argument::getArgNo() const { 00062 const Function *F = getParent(); 00063 assert(F && "Argument is not in a function"); 00064 00065 Function::const_arg_iterator AI = F->arg_begin(); 00066 unsigned ArgIdx = 0; 00067 for (; &*AI != this; ++AI) 00068 ++ArgIdx; 00069 00070 return ArgIdx; 00071 } 00072 00073 /// hasByValAttr - Return true if this argument has the byval attribute on it 00074 /// in its containing function. 00075 bool Argument::hasByValAttr() const { 00076 if (!isa<PointerType>(getType())) return false; 00077 return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal); 00078 } 00079 00080 /// hasNoAliasAttr - Return true if this argument has the noalias attribute on 00081 /// it in its containing function. 00082 bool Argument::hasNoAliasAttr() const { 00083 if (!isa<PointerType>(getType())) return false; 00084 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias); 00085 } 00086 00087 /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute 00088 /// on it in its containing function. 00089 bool Argument::hasNoCaptureAttr() const { 00090 if (!isa<PointerType>(getType())) return false; 00091 return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture); 00092 } 00093 00094 /// hasSRetAttr - Return true if this argument has the sret attribute on 00095 /// it in its containing function. 00096 bool Argument::hasStructRetAttr() const { 00097 if (!isa<PointerType>(getType())) return false; 00098 if (this != getParent()->arg_begin()) 00099 return false; // StructRet param must be first param 00100 return getParent()->paramHasAttr(1, Attribute::StructRet); 00101 } 00102 00103 /// addAttr - Add a Attribute to an argument 00104 void Argument::addAttr(Attributes attr) { 00105 getParent()->addAttribute(getArgNo() + 1, attr); 00106 } 00107 00108 /// removeAttr - Remove a Attribute from an argument 00109 void Argument::removeAttr(Attributes attr) { 00110 getParent()->removeAttribute(getArgNo() + 1, attr); 00111 } 00112 00113 00114 //===----------------------------------------------------------------------===// 00115 // Helper Methods in Function 00116 //===----------------------------------------------------------------------===// 00117 00118 LLVMContext &Function::getContext() const { 00119 return getType()->getContext(); 00120 } 00121 00122 const FunctionType *Function::getFunctionType() const { 00123 return cast<FunctionType>(getType()->getElementType()); 00124 } 00125 00126 bool Function::isVarArg() const { 00127 return getFunctionType()->isVarArg(); 00128 } 00129 00130 const Type *Function::getReturnType() const { 00131 return getFunctionType()->getReturnType(); 00132 } 00133 00134 void Function::removeFromParent() { 00135 getParent()->getFunctionList().remove(this); 00136 } 00137 00138 void Function::eraseFromParent() { 00139 getParent()->getFunctionList().erase(this); 00140 } 00141 00142 //===----------------------------------------------------------------------===// 00143 // Function Implementation 00144 //===----------------------------------------------------------------------===// 00145 00146 Function::Function(const FunctionType *Ty, LinkageTypes Linkage, 00147 const Twine &name, Module *ParentModule) 00148 : GlobalValue(PointerType::getUnqual(Ty), 00149 Value::FunctionVal, 0, 0, Linkage, name) { 00150 assert(FunctionType::isValidReturnType(getReturnType()) && 00151 !isa<OpaqueType>(getReturnType()) && "invalid return type"); 00152 SymTab = new ValueSymbolTable(); 00153 00154 // If the function has arguments, mark them as lazily built. 00155 if (Ty->getNumParams()) 00156 SubclassData = 1; // Set the "has lazy arguments" bit. 00157 00158 // Make sure that we get added to a function 00159 LeakDetector::addGarbageObject(this); 00160 00161 if (ParentModule) 00162 ParentModule->getFunctionList().push_back(this); 00163 00164 // Ensure intrinsics have the right parameter attributes. 00165 if (unsigned IID = getIntrinsicID()) 00166 setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID))); 00167 00168 } 00169 00170 Function::~Function() { 00171 dropAllReferences(); // After this it is safe to delete instructions. 00172 00173 // Delete all of the method arguments and unlink from symbol table... 00174 ArgumentList.clear(); 00175 delete SymTab; 00176 00177 // Remove the function from the on-the-side GC table. 00178 clearGC(); 00179 } 00180 00181 void Function::BuildLazyArguments() const { 00182 // Create the arguments vector, all arguments start out unnamed. 00183 const FunctionType *FT = getFunctionType(); 00184 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { 00185 assert(FT->getParamType(i) != Type::getVoidTy(FT->getContext()) && 00186 "Cannot have void typed arguments!"); 00187 ArgumentList.push_back(new Argument(FT->getParamType(i))); 00188 } 00189 00190 // Clear the lazy arguments bit. 00191 const_cast<Function*>(this)->SubclassData &= ~1; 00192 } 00193 00194 size_t Function::arg_size() const { 00195 return getFunctionType()->getNumParams(); 00196 } 00197 bool Function::arg_empty() const { 00198 return getFunctionType()->getNumParams() == 0; 00199 } 00200 00201 void Function::setParent(Module *parent) { 00202 if (getParent()) 00203 LeakDetector::addGarbageObject(this); 00204 Parent = parent; 00205 if (getParent()) 00206 LeakDetector::removeGarbageObject(this); 00207 } 00208 00209 // dropAllReferences() - This function causes all the subinstructions to "let 00210 // go" of all references that they are maintaining. This allows one to 00211 // 'delete' a whole class at a time, even though there may be circular 00212 // references... first all references are dropped, and all use counts go to 00213 // zero. Then everything is deleted for real. Note that no operations are 00214 // valid on an object that has "dropped all references", except operator 00215 // delete. 00216 // 00217 void Function::dropAllReferences() { 00218 for (iterator I = begin(), E = end(); I != E; ++I) 00219 I->dropAllReferences(); 00220 BasicBlocks.clear(); // Delete all basic blocks... 00221 } 00222 00223 void Function::addAttribute(unsigned i, Attributes attr) { 00224 AttrListPtr PAL = getAttributes(); 00225 PAL = PAL.addAttr(i, attr); 00226 setAttributes(PAL); 00227 } 00228 00229 void Function::removeAttribute(unsigned i, Attributes attr) { 00230 AttrListPtr PAL = getAttributes(); 00231 PAL = PAL.removeAttr(i, attr); 00232 setAttributes(PAL); 00233 } 00234 00235 // Maintain the GC name for each function in an on-the-side table. This saves 00236 // allocating an additional word in Function for programs which do not use GC 00237 // (i.e., most programs) at the cost of increased overhead for clients which do 00238 // use GC. 00239 static DenseMap<const Function*,PooledStringPtr> *GCNames; 00240 static StringPool *GCNamePool; 00241 static ManagedStatic<sys::SmartRWMutex<true> > GCLock; 00242 00243 bool Function::hasGC() const { 00244 sys::SmartScopedReader<true> Reader(*GCLock); 00245 return GCNames && GCNames->count(this); 00246 } 00247 00248 const char *Function::getGC() const { 00249 assert(hasGC() && "Function has no collector"); 00250 sys::SmartScopedReader<true> Reader(*GCLock); 00251 return *(*GCNames)[this]; 00252 } 00253 00254 void Function::setGC(const char *Str) { 00255 sys::SmartScopedWriter<true> Writer(*GCLock); 00256 if (!GCNamePool) 00257 GCNamePool = new StringPool(); 00258 if (!GCNames) 00259 GCNames = new DenseMap<const Function*,PooledStringPtr>(); 00260 (*GCNames)[this] = GCNamePool->intern(Str); 00261 } 00262 00263 void Function::clearGC() { 00264 sys::SmartScopedWriter<true> Writer(*GCLock); 00265 if (GCNames) { 00266 GCNames->erase(this); 00267 if (GCNames->empty()) { 00268 delete GCNames; 00269 GCNames = 0; 00270 if (GCNamePool->empty()) { 00271 delete GCNamePool; 00272 GCNamePool = 0; 00273 } 00274 } 00275 } 00276 } 00277 00278 /// copyAttributesFrom - copy all additional attributes (those not needed to 00279 /// create a Function) from the Function Src to this one. 00280 void Function::copyAttributesFrom(const GlobalValue *Src) { 00281 assert(isa<Function>(Src) && "Expected a Function!"); 00282 GlobalValue::copyAttributesFrom(Src); 00283 const Function *SrcF = cast<Function>(Src); 00284 setCallingConv(SrcF->getCallingConv()); 00285 setAttributes(SrcF->getAttributes()); 00286 if (SrcF->hasGC()) 00287 setGC(SrcF->getGC()); 00288 else 00289 clearGC(); 00290 } 00291 00292 /// getIntrinsicID - This method returns the ID number of the specified 00293 /// function, or Intrinsic::not_intrinsic if the function is not an 00294 /// intrinsic, or if the pointer is null. This value is always defined to be 00295 /// zero to allow easy checking for whether a function is intrinsic or not. The 00296 /// particular intrinsic functions which correspond to this value are defined in 00297 /// llvm/Intrinsics.h. 00298 /// 00299 unsigned Function::getIntrinsicID() const { 00300 const ValueName *ValName = this->getValueName(); 00301 if (!ValName) 00302 return 0; 00303 unsigned Len = ValName->getKeyLength(); 00304 const char *Name = ValName->getKeyData(); 00305 00306 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' 00307 || Name[2] != 'v' || Name[3] != 'm') 00308 return 0; // All intrinsics start with 'llvm.' 00309 00310 #define GET_FUNCTION_RECOGNIZER 00311 #include "llvm/Intrinsics.gen" 00312 #undef GET_FUNCTION_RECOGNIZER 00313 return 0; 00314 } 00315 00316 std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { 00317 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 00318 const char * const Table[] = { 00319 "not_intrinsic", 00320 #define GET_INTRINSIC_NAME_TABLE 00321 #include "llvm/Intrinsics.gen" 00322 #undef GET_INTRINSIC_NAME_TABLE 00323 }; 00324 if (numTys == 0) 00325 return Table[id]; 00326 std::string Result(Table[id]); 00327 for (unsigned i = 0; i < numTys; ++i) { 00328 if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { 00329 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + 00330 EVT::getEVT(PTyp->getElementType()).getEVTString(); 00331 } 00332 else if (Tys[i]) 00333 Result += "." + EVT::getEVT(Tys[i]).getEVTString(); 00334 } 00335 return Result; 00336 } 00337 00338 const FunctionType *Intrinsic::getType(LLVMContext &Context, 00339 ID id, const Type **Tys, 00340 unsigned numTys) { 00341 const Type *ResultTy = NULL; 00342 std::vector<const Type*> ArgTys; 00343 bool IsVarArg = false; 00344 00345 #define GET_INTRINSIC_GENERATOR 00346 #include "llvm/Intrinsics.gen" 00347 #undef GET_INTRINSIC_GENERATOR 00348 00349 return FunctionType::get(ResultTy, ArgTys, IsVarArg); 00350 } 00351 00352 bool Intrinsic::isOverloaded(ID id) { 00353 const bool OTable[] = { 00354 false, 00355 #define GET_INTRINSIC_OVERLOAD_TABLE 00356 #include "llvm/Intrinsics.gen" 00357 #undef GET_INTRINSIC_OVERLOAD_TABLE 00358 }; 00359 return OTable[id]; 00360 } 00361 00362 /// This defines the "Intrinsic::getAttributes(ID id)" method. 00363 #define GET_INTRINSIC_ATTRIBUTES 00364 #include "llvm/Intrinsics.gen" 00365 #undef GET_INTRINSIC_ATTRIBUTES 00366 00367 Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, 00368 unsigned numTys) { 00369 // There can never be multiple globals with the same name of different types, 00370 // because intrinsics must be a specific type. 00371 return 00372 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), 00373 getType(M->getContext(), 00374 id, Tys, numTys))); 00375 } 00376 00377 // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. 00378 #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 00379 #include "llvm/Intrinsics.gen" 00380 #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 00381 00382 /// hasAddressTaken - returns true if there are any uses of this function 00383 /// other than direct calls or invokes to it. 00384 bool Function::hasAddressTaken() const { 00385 for (Value::use_const_iterator I = use_begin(), E = use_end(); I != E; ++I) { 00386 if (I.getOperandNo() != 0 || 00387 (!isa<CallInst>(*I) && !isa<InvokeInst>(*I))) 00388 return true; 00389 } 00390 return false; 00391 } 00392 00393 // vim: sw=2 ai