Function.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//===-- Function.cpp - Implement the Global object classes ----------------===// 20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// 30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// The LLVM Compiler Infrastructure 40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// 50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This file is distributed under the University of Illinois Open Source 60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// License. See LICENSE.TXT for details. 70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// 80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//===----------------------------------------------------------------------===// 90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// 100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// This file implements the Function class for the IR library. 110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// 120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//===----------------------------------------------------------------------===// 130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/Function.h" 150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "LLVMContextImpl.h" 160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "SymbolTableListTraitsImpl.h" 170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/ADT/DenseMap.h" 180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/ADT/STLExtras.h" 190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/ADT/StringExtras.h" 200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/CodeGen/ValueTypes.h" 210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/CallSite.h" 220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/DerivedTypes.h" 230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/InstIterator.h" 240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/IntrinsicInst.h" 250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/LLVMContext.h" 260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/LeakDetector.h" 270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/IR/Module.h" 280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/Support/ManagedStatic.h" 290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/Support/RWMutex.h" 300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "llvm/Support/StringPool.h" 31cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "llvm/Support/Threading.h" 32cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.orgusing namespace llvm; 332a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org 342a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org// Explicit instantiations of SymbolTableListTraits since some of the methods 352a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org// are not in the public header file... 360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgtemplate class llvm::SymbolTableListTraits<Argument, Function>; 372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgtemplate class llvm::SymbolTableListTraits<BasicBlock, Function>; 380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//===----------------------------------------------------------------------===// 400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Argument Implementation 410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//===----------------------------------------------------------------------===// 420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid Argument::anchor() { } 440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgArgument::Argument(Type *Ty, const Twine &Name, Function *Par) 460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : Value(Ty, Value::ArgumentVal) { 470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org Parent = nullptr; 480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Make sure that we get added to a function 500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LeakDetector::addGarbageObject(this); 510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (Par) 530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org Par->getArgumentList().push_back(this); 540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org setName(Name); 550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid Argument::setParent(Function *parent) { 580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (getParent()) 590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LeakDetector::addGarbageObject(this); 600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org Parent = parent; 610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (getParent()) 620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org LeakDetector::removeGarbageObject(this); 630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// getArgNo - Return the index of this formal argument in its containing 660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// function. For example in "void foo(int a, float b)" a is 0 and b is 1. 672a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgunsigned Argument::getArgNo() const { 680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const Function *F = getParent(); 690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org assert(F && "Argument is not in a function"); 700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 712a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org Function::const_arg_iterator AI = F->arg_begin(); 720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org unsigned ArgIdx = 0; 730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org for (; &*AI != this; ++AI) 740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ++ArgIdx; 750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return ArgIdx; 772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org} 780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasNonNullAttr - Return true if this argument has the nonnull attribute on 800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// it in its containing function. 810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool Argument::hasNonNullAttr() const { 820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!getType()->isPointerTy()) return false; 830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getAttributes(). 840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::NonNull); 850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasByValAttr - Return true if this argument has the byval attribute on it 880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// in its containing function. 890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool Argument::hasByValAttr() const { 900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!getType()->isPointerTy()) return false; 910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getAttributes(). 920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::ByVal); 930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// \brief Return true if this argument has the inalloca attribute on it in 962a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org/// its containing function. 972a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgbool Argument::hasInAllocaAttr() const { 980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!getType()->isPointerTy()) return false; 992a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org return getParent()->getAttributes(). 1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::InAlloca); 1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool Argument::hasByValOrInAllocaAttr() const { 1042a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org if (!getType()->isPointerTy()) return false; 1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org AttributeSet Attrs = getParent()->getAttributes(); 1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) || 1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca); 1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgunsigned Argument::getParamAlignment() const { 1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org assert(getType()->isPointerTy() && "Only pointers have alignments"); 1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getParamAlignment(getArgNo()+1); 1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1142a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org} 1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasNestAttr - Return true if this argument has the nest attribute on 1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// it in its containing function. 1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool Argument::hasNestAttr() const { 1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!getType()->isPointerTy()) return false; 1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getAttributes(). 1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::Nest); 1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasNoAliasAttr - Return true if this argument has the noalias attribute on 1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// it in its containing function. 1262a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgbool Argument::hasNoAliasAttr() const { 1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!getType()->isPointerTy()) return false; 1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getAttributes(). 1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::NoAlias); 1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute 1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// on it in its containing function. 1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool Argument::hasNoCaptureAttr() const { 1352a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org if (!getType()->isPointerTy()) return false; 1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return getParent()->getAttributes(). 1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org hasAttribute(getArgNo()+1, Attribute::NoCapture); 1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/// hasSRetAttr - Return true if this argument has the sret attribute on 141/// it in its containing function. 142bool Argument::hasStructRetAttr() const { 143 if (!getType()->isPointerTy()) return false; 144 if (this != getParent()->arg_begin()) 145 return false; // StructRet param must be first param 146 return getParent()->getAttributes(). 147 hasAttribute(1, Attribute::StructRet); 148} 149 150/// hasReturnedAttr - Return true if this argument has the returned attribute on 151/// it in its containing function. 152bool Argument::hasReturnedAttr() const { 153 return getParent()->getAttributes(). 154 hasAttribute(getArgNo()+1, Attribute::Returned); 155} 156 157/// Return true if this argument has the readonly or readnone attribute on it 158/// in its containing function. 159bool Argument::onlyReadsMemory() const { 160 return getParent()->getAttributes(). 161 hasAttribute(getArgNo()+1, Attribute::ReadOnly) || 162 getParent()->getAttributes(). 163 hasAttribute(getArgNo()+1, Attribute::ReadNone); 164} 165 166/// addAttr - Add attributes to an argument. 167void Argument::addAttr(AttributeSet AS) { 168 assert(AS.getNumSlots() <= 1 && 169 "Trying to add more than one attribute set to an argument!"); 170 AttrBuilder B(AS, AS.getSlotIndex(0)); 171 getParent()->addAttributes(getArgNo() + 1, 172 AttributeSet::get(Parent->getContext(), 173 getArgNo() + 1, B)); 174} 175 176/// removeAttr - Remove attributes from an argument. 177void Argument::removeAttr(AttributeSet AS) { 178 assert(AS.getNumSlots() <= 1 && 179 "Trying to remove more than one attribute set from an argument!"); 180 AttrBuilder B(AS, AS.getSlotIndex(0)); 181 getParent()->removeAttributes(getArgNo() + 1, 182 AttributeSet::get(Parent->getContext(), 183 getArgNo() + 1, B)); 184} 185 186//===----------------------------------------------------------------------===// 187// Helper Methods in Function 188//===----------------------------------------------------------------------===// 189 190LLVMContext &Function::getContext() const { 191 return getType()->getContext(); 192} 193 194FunctionType *Function::getFunctionType() const { 195 return cast<FunctionType>(getType()->getElementType()); 196} 197 198bool Function::isVarArg() const { 199 return getFunctionType()->isVarArg(); 200} 201 202Type *Function::getReturnType() const { 203 return getFunctionType()->getReturnType(); 204} 205 206void Function::removeFromParent() { 207 getParent()->getFunctionList().remove(this); 208} 209 210void Function::eraseFromParent() { 211 getParent()->getFunctionList().erase(this); 212} 213 214//===----------------------------------------------------------------------===// 215// Function Implementation 216//===----------------------------------------------------------------------===// 217 218Function::Function(FunctionType *Ty, LinkageTypes Linkage, 219 const Twine &name, Module *ParentModule) 220 : GlobalObject(PointerType::getUnqual(Ty), 221 Value::FunctionVal, nullptr, 0, Linkage, name) { 222 assert(FunctionType::isValidReturnType(getReturnType()) && 223 "invalid return type"); 224 SymTab = new ValueSymbolTable(); 225 226 // If the function has arguments, mark them as lazily built. 227 if (Ty->getNumParams()) 228 setValueSubclassData(1); // Set the "has lazy arguments" bit. 229 230 // Make sure that we get added to a function 231 LeakDetector::addGarbageObject(this); 232 233 if (ParentModule) 234 ParentModule->getFunctionList().push_back(this); 235 236 // Ensure intrinsics have the right parameter attributes. 237 if (unsigned IID = getIntrinsicID()) 238 setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID))); 239 240} 241 242Function::~Function() { 243 dropAllReferences(); // After this it is safe to delete instructions. 244 245 // Delete all of the method arguments and unlink from symbol table... 246 ArgumentList.clear(); 247 delete SymTab; 248 249 // Remove the function from the on-the-side GC table. 250 clearGC(); 251 252 // Remove the intrinsicID from the Cache. 253 if (getValueName() && isIntrinsic()) 254 getContext().pImpl->IntrinsicIDCache.erase(this); 255} 256 257void Function::BuildLazyArguments() const { 258 // Create the arguments vector, all arguments start out unnamed. 259 FunctionType *FT = getFunctionType(); 260 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { 261 assert(!FT->getParamType(i)->isVoidTy() && 262 "Cannot have void typed arguments!"); 263 ArgumentList.push_back(new Argument(FT->getParamType(i))); 264 } 265 266 // Clear the lazy arguments bit. 267 unsigned SDC = getSubclassDataFromValue(); 268 const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1); 269} 270 271size_t Function::arg_size() const { 272 return getFunctionType()->getNumParams(); 273} 274bool Function::arg_empty() const { 275 return getFunctionType()->getNumParams() == 0; 276} 277 278void Function::setParent(Module *parent) { 279 if (getParent()) 280 LeakDetector::addGarbageObject(this); 281 Parent = parent; 282 if (getParent()) 283 LeakDetector::removeGarbageObject(this); 284} 285 286// dropAllReferences() - This function causes all the subinstructions to "let 287// go" of all references that they are maintaining. This allows one to 288// 'delete' a whole class at a time, even though there may be circular 289// references... first all references are dropped, and all use counts go to 290// zero. Then everything is deleted for real. Note that no operations are 291// valid on an object that has "dropped all references", except operator 292// delete. 293// 294void Function::dropAllReferences() { 295 for (iterator I = begin(), E = end(); I != E; ++I) 296 I->dropAllReferences(); 297 298 // Delete all basic blocks. They are now unused, except possibly by 299 // blockaddresses, but BasicBlock's destructor takes care of those. 300 while (!BasicBlocks.empty()) 301 BasicBlocks.begin()->eraseFromParent(); 302 303 // Prefix data is stored in a side table. 304 setPrefixData(nullptr); 305} 306 307void Function::addAttribute(unsigned i, Attribute::AttrKind attr) { 308 AttributeSet PAL = getAttributes(); 309 PAL = PAL.addAttribute(getContext(), i, attr); 310 setAttributes(PAL); 311} 312 313void Function::addAttributes(unsigned i, AttributeSet attrs) { 314 AttributeSet PAL = getAttributes(); 315 PAL = PAL.addAttributes(getContext(), i, attrs); 316 setAttributes(PAL); 317} 318 319void Function::removeAttributes(unsigned i, AttributeSet attrs) { 320 AttributeSet PAL = getAttributes(); 321 PAL = PAL.removeAttributes(getContext(), i, attrs); 322 setAttributes(PAL); 323} 324 325// Maintain the GC name for each function in an on-the-side table. This saves 326// allocating an additional word in Function for programs which do not use GC 327// (i.e., most programs) at the cost of increased overhead for clients which do 328// use GC. 329static DenseMap<const Function*,PooledStringPtr> *GCNames; 330static StringPool *GCNamePool; 331static ManagedStatic<sys::SmartRWMutex<true> > GCLock; 332 333bool Function::hasGC() const { 334 sys::SmartScopedReader<true> Reader(*GCLock); 335 return GCNames && GCNames->count(this); 336} 337 338const char *Function::getGC() const { 339 assert(hasGC() && "Function has no collector"); 340 sys::SmartScopedReader<true> Reader(*GCLock); 341 return *(*GCNames)[this]; 342} 343 344void Function::setGC(const char *Str) { 345 sys::SmartScopedWriter<true> Writer(*GCLock); 346 if (!GCNamePool) 347 GCNamePool = new StringPool(); 348 if (!GCNames) 349 GCNames = new DenseMap<const Function*,PooledStringPtr>(); 350 (*GCNames)[this] = GCNamePool->intern(Str); 351} 352 353void Function::clearGC() { 354 sys::SmartScopedWriter<true> Writer(*GCLock); 355 if (GCNames) { 356 GCNames->erase(this); 357 if (GCNames->empty()) { 358 delete GCNames; 359 GCNames = nullptr; 360 if (GCNamePool->empty()) { 361 delete GCNamePool; 362 GCNamePool = nullptr; 363 } 364 } 365 } 366} 367 368/// copyAttributesFrom - copy all additional attributes (those not needed to 369/// create a Function) from the Function Src to this one. 370void Function::copyAttributesFrom(const GlobalValue *Src) { 371 assert(isa<Function>(Src) && "Expected a Function!"); 372 GlobalObject::copyAttributesFrom(Src); 373 const Function *SrcF = cast<Function>(Src); 374 setCallingConv(SrcF->getCallingConv()); 375 setAttributes(SrcF->getAttributes()); 376 if (SrcF->hasGC()) 377 setGC(SrcF->getGC()); 378 else 379 clearGC(); 380 if (SrcF->hasPrefixData()) 381 setPrefixData(SrcF->getPrefixData()); 382 else 383 setPrefixData(nullptr); 384} 385 386/// getIntrinsicID - This method returns the ID number of the specified 387/// function, or Intrinsic::not_intrinsic if the function is not an 388/// intrinsic, or if the pointer is null. This value is always defined to be 389/// zero to allow easy checking for whether a function is intrinsic or not. The 390/// particular intrinsic functions which correspond to this value are defined in 391/// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent 392/// requests for the same ID return results much faster from the cache. 393/// 394unsigned Function::getIntrinsicID() const { 395 const ValueName *ValName = this->getValueName(); 396 if (!ValName || !isIntrinsic()) 397 return 0; 398 399 LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache = 400 getContext().pImpl->IntrinsicIDCache; 401 if (!IntrinsicIDCache.count(this)) { 402 unsigned Id = lookupIntrinsicID(); 403 IntrinsicIDCache[this]=Id; 404 return Id; 405 } 406 return IntrinsicIDCache[this]; 407} 408 409/// This private method does the actual lookup of an intrinsic ID when the query 410/// could not be answered from the cache. 411unsigned Function::lookupIntrinsicID() const { 412 const ValueName *ValName = this->getValueName(); 413 unsigned Len = ValName->getKeyLength(); 414 const char *Name = ValName->getKeyData(); 415 416#define GET_FUNCTION_RECOGNIZER 417#include "llvm/IR/Intrinsics.gen" 418#undef GET_FUNCTION_RECOGNIZER 419 420 return 0; 421} 422 423std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { 424 assert(id < num_intrinsics && "Invalid intrinsic ID!"); 425 static const char * const Table[] = { 426 "not_intrinsic", 427#define GET_INTRINSIC_NAME_TABLE 428#include "llvm/IR/Intrinsics.gen" 429#undef GET_INTRINSIC_NAME_TABLE 430 }; 431 if (Tys.empty()) 432 return Table[id]; 433 std::string Result(Table[id]); 434 for (unsigned i = 0; i < Tys.size(); ++i) { 435 if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { 436 Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + 437 EVT::getEVT(PTyp->getElementType()).getEVTString(); 438 } 439 else if (Tys[i]) 440 Result += "." + EVT::getEVT(Tys[i]).getEVTString(); 441 } 442 return Result; 443} 444 445 446/// IIT_Info - These are enumerators that describe the entries returned by the 447/// getIntrinsicInfoTableEntries function. 448/// 449/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! 450enum IIT_Info { 451 // Common values should be encoded with 0-15. 452 IIT_Done = 0, 453 IIT_I1 = 1, 454 IIT_I8 = 2, 455 IIT_I16 = 3, 456 IIT_I32 = 4, 457 IIT_I64 = 5, 458 IIT_F16 = 6, 459 IIT_F32 = 7, 460 IIT_F64 = 8, 461 IIT_V2 = 9, 462 IIT_V4 = 10, 463 IIT_V8 = 11, 464 IIT_V16 = 12, 465 IIT_V32 = 13, 466 IIT_PTR = 14, 467 IIT_ARG = 15, 468 469 // Values from 16+ are only encodable with the inefficient encoding. 470 IIT_MMX = 16, 471 IIT_METADATA = 17, 472 IIT_EMPTYSTRUCT = 18, 473 IIT_STRUCT2 = 19, 474 IIT_STRUCT3 = 20, 475 IIT_STRUCT4 = 21, 476 IIT_STRUCT5 = 22, 477 IIT_EXTEND_ARG = 23, 478 IIT_TRUNC_ARG = 24, 479 IIT_ANYPTR = 25, 480 IIT_V1 = 26, 481 IIT_VARARG = 27, 482 IIT_HALF_VEC_ARG = 28 483}; 484 485 486static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, 487 SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { 488 IIT_Info Info = IIT_Info(Infos[NextElt++]); 489 unsigned StructElts = 2; 490 using namespace Intrinsic; 491 492 switch (Info) { 493 case IIT_Done: 494 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); 495 return; 496 case IIT_VARARG: 497 OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); 498 return; 499 case IIT_MMX: 500 OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); 501 return; 502 case IIT_METADATA: 503 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); 504 return; 505 case IIT_F16: 506 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); 507 return; 508 case IIT_F32: 509 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); 510 return; 511 case IIT_F64: 512 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); 513 return; 514 case IIT_I1: 515 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); 516 return; 517 case IIT_I8: 518 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); 519 return; 520 case IIT_I16: 521 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); 522 return; 523 case IIT_I32: 524 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); 525 return; 526 case IIT_I64: 527 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); 528 return; 529 case IIT_V1: 530 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1)); 531 DecodeIITType(NextElt, Infos, OutputTable); 532 return; 533 case IIT_V2: 534 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); 535 DecodeIITType(NextElt, Infos, OutputTable); 536 return; 537 case IIT_V4: 538 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); 539 DecodeIITType(NextElt, Infos, OutputTable); 540 return; 541 case IIT_V8: 542 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); 543 DecodeIITType(NextElt, Infos, OutputTable); 544 return; 545 case IIT_V16: 546 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); 547 DecodeIITType(NextElt, Infos, OutputTable); 548 return; 549 case IIT_V32: 550 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); 551 DecodeIITType(NextElt, Infos, OutputTable); 552 return; 553 case IIT_PTR: 554 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); 555 DecodeIITType(NextElt, Infos, OutputTable); 556 return; 557 case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] 558 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 559 Infos[NextElt++])); 560 DecodeIITType(NextElt, Infos, OutputTable); 561 return; 562 } 563 case IIT_ARG: { 564 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 565 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); 566 return; 567 } 568 case IIT_EXTEND_ARG: { 569 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 570 OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, 571 ArgInfo)); 572 return; 573 } 574 case IIT_TRUNC_ARG: { 575 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 576 OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, 577 ArgInfo)); 578 return; 579 } 580 case IIT_HALF_VEC_ARG: { 581 unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); 582 OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, 583 ArgInfo)); 584 return; 585 } 586 case IIT_EMPTYSTRUCT: 587 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); 588 return; 589 case IIT_STRUCT5: ++StructElts; // FALL THROUGH. 590 case IIT_STRUCT4: ++StructElts; // FALL THROUGH. 591 case IIT_STRUCT3: ++StructElts; // FALL THROUGH. 592 case IIT_STRUCT2: { 593 OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); 594 595 for (unsigned i = 0; i != StructElts; ++i) 596 DecodeIITType(NextElt, Infos, OutputTable); 597 return; 598 } 599 } 600 llvm_unreachable("unhandled"); 601} 602 603 604#define GET_INTRINSIC_GENERATOR_GLOBAL 605#include "llvm/IR/Intrinsics.gen" 606#undef GET_INTRINSIC_GENERATOR_GLOBAL 607 608void Intrinsic::getIntrinsicInfoTableEntries(ID id, 609 SmallVectorImpl<IITDescriptor> &T){ 610 // Check to see if the intrinsic's type was expressible by the table. 611 unsigned TableVal = IIT_Table[id-1]; 612 613 // Decode the TableVal into an array of IITValues. 614 SmallVector<unsigned char, 8> IITValues; 615 ArrayRef<unsigned char> IITEntries; 616 unsigned NextElt = 0; 617 if ((TableVal >> 31) != 0) { 618 // This is an offset into the IIT_LongEncodingTable. 619 IITEntries = IIT_LongEncodingTable; 620 621 // Strip sentinel bit. 622 NextElt = (TableVal << 1) >> 1; 623 } else { 624 // Decode the TableVal into an array of IITValues. If the entry was encoded 625 // into a single word in the table itself, decode it now. 626 do { 627 IITValues.push_back(TableVal & 0xF); 628 TableVal >>= 4; 629 } while (TableVal); 630 631 IITEntries = IITValues; 632 NextElt = 0; 633 } 634 635 // Okay, decode the table into the output vector of IITDescriptors. 636 DecodeIITType(NextElt, IITEntries, T); 637 while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) 638 DecodeIITType(NextElt, IITEntries, T); 639} 640 641 642static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, 643 ArrayRef<Type*> Tys, LLVMContext &Context) { 644 using namespace Intrinsic; 645 IITDescriptor D = Infos.front(); 646 Infos = Infos.slice(1); 647 648 switch (D.Kind) { 649 case IITDescriptor::Void: return Type::getVoidTy(Context); 650 case IITDescriptor::VarArg: return Type::getVoidTy(Context); 651 case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); 652 case IITDescriptor::Metadata: return Type::getMetadataTy(Context); 653 case IITDescriptor::Half: return Type::getHalfTy(Context); 654 case IITDescriptor::Float: return Type::getFloatTy(Context); 655 case IITDescriptor::Double: return Type::getDoubleTy(Context); 656 657 case IITDescriptor::Integer: 658 return IntegerType::get(Context, D.Integer_Width); 659 case IITDescriptor::Vector: 660 return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); 661 case IITDescriptor::Pointer: 662 return PointerType::get(DecodeFixedType(Infos, Tys, Context), 663 D.Pointer_AddressSpace); 664 case IITDescriptor::Struct: { 665 Type *Elts[5]; 666 assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); 667 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) 668 Elts[i] = DecodeFixedType(Infos, Tys, Context); 669 return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements)); 670 } 671 672 case IITDescriptor::Argument: 673 return Tys[D.getArgumentNumber()]; 674 case IITDescriptor::ExtendArgument: { 675 Type *Ty = Tys[D.getArgumentNumber()]; 676 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 677 return VectorType::getExtendedElementVectorType(VTy); 678 679 return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); 680 } 681 case IITDescriptor::TruncArgument: { 682 Type *Ty = Tys[D.getArgumentNumber()]; 683 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) 684 return VectorType::getTruncatedElementVectorType(VTy); 685 686 IntegerType *ITy = cast<IntegerType>(Ty); 687 assert(ITy->getBitWidth() % 2 == 0); 688 return IntegerType::get(Context, ITy->getBitWidth() / 2); 689 } 690 case IITDescriptor::HalfVecArgument: 691 return VectorType::getHalfElementsVectorType(cast<VectorType>( 692 Tys[D.getArgumentNumber()])); 693 } 694 llvm_unreachable("unhandled"); 695} 696 697 698 699FunctionType *Intrinsic::getType(LLVMContext &Context, 700 ID id, ArrayRef<Type*> Tys) { 701 SmallVector<IITDescriptor, 8> Table; 702 getIntrinsicInfoTableEntries(id, Table); 703 704 ArrayRef<IITDescriptor> TableRef = Table; 705 Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); 706 707 SmallVector<Type*, 8> ArgTys; 708 while (!TableRef.empty()) 709 ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); 710 711 return FunctionType::get(ResultTy, ArgTys, false); 712} 713 714bool Intrinsic::isOverloaded(ID id) { 715#define GET_INTRINSIC_OVERLOAD_TABLE 716#include "llvm/IR/Intrinsics.gen" 717#undef GET_INTRINSIC_OVERLOAD_TABLE 718} 719 720/// This defines the "Intrinsic::getAttributes(ID id)" method. 721#define GET_INTRINSIC_ATTRIBUTES 722#include "llvm/IR/Intrinsics.gen" 723#undef GET_INTRINSIC_ATTRIBUTES 724 725Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { 726 // There can never be multiple globals with the same name of different types, 727 // because intrinsics must be a specific type. 728 return 729 cast<Function>(M->getOrInsertFunction(getName(id, Tys), 730 getType(M->getContext(), id, Tys))); 731} 732 733// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. 734#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 735#include "llvm/IR/Intrinsics.gen" 736#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN 737 738/// hasAddressTaken - returns true if there are any uses of this function 739/// other than direct calls or invokes to it. 740bool Function::hasAddressTaken(const User* *PutOffender) const { 741 for (const Use &U : uses()) { 742 const User *FU = U.getUser(); 743 if (isa<BlockAddress>(FU)) 744 continue; 745 if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) 746 return PutOffender ? (*PutOffender = FU, true) : true; 747 ImmutableCallSite CS(cast<Instruction>(FU)); 748 if (!CS.isCallee(&U)) 749 return PutOffender ? (*PutOffender = FU, true) : true; 750 } 751 return false; 752} 753 754bool Function::isDefTriviallyDead() const { 755 // Check the linkage 756 if (!hasLinkOnceLinkage() && !hasLocalLinkage() && 757 !hasAvailableExternallyLinkage()) 758 return false; 759 760 // Check if the function is used by anything other than a blockaddress. 761 for (const User *U : users()) 762 if (!isa<BlockAddress>(U)) 763 return false; 764 765 return true; 766} 767 768/// callsFunctionThatReturnsTwice - Return true if the function has a call to 769/// setjmp or other function that gcc recognizes as "returning twice". 770bool Function::callsFunctionThatReturnsTwice() const { 771 for (const_inst_iterator 772 I = inst_begin(this), E = inst_end(this); I != E; ++I) { 773 ImmutableCallSite CS(&*I); 774 if (CS && CS.hasFnAttr(Attribute::ReturnsTwice)) 775 return true; 776 } 777 778 return false; 779} 780 781Constant *Function::getPrefixData() const { 782 assert(hasPrefixData()); 783 const LLVMContextImpl::PrefixDataMapTy &PDMap = 784 getContext().pImpl->PrefixDataMap; 785 assert(PDMap.find(this) != PDMap.end()); 786 return cast<Constant>(PDMap.find(this)->second->getReturnValue()); 787} 788 789void Function::setPrefixData(Constant *PrefixData) { 790 if (!PrefixData && !hasPrefixData()) 791 return; 792 793 unsigned SCData = getSubclassDataFromValue(); 794 LLVMContextImpl::PrefixDataMapTy &PDMap = getContext().pImpl->PrefixDataMap; 795 ReturnInst *&PDHolder = PDMap[this]; 796 if (PrefixData) { 797 if (PDHolder) 798 PDHolder->setOperand(0, PrefixData); 799 else 800 PDHolder = ReturnInst::Create(getContext(), PrefixData); 801 SCData |= 2; 802 } else { 803 delete PDHolder; 804 PDMap.erase(this); 805 SCData &= ~2; 806 } 807 setValueSubclassData(SCData); 808} 809