1 //===-- Function.cpp - Implement the Global object classes ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Function class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ParameterAttributes.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Support/LeakDetector.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "SymbolTableListTraitsImpl.h"
21 #include "llvm/ADT/StringExtras.h"
24 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
25 BasicBlock *Ret = new BasicBlock();
26 // This should not be garbage monitored.
27 LeakDetector::removeGarbageObject(Ret);
31 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
32 return F->getBasicBlockList();
35 Argument *ilist_traits<Argument>::createSentinel() {
36 Argument *Ret = new Argument(Type::Int32Ty);
37 // This should not be garbage monitored.
38 LeakDetector::removeGarbageObject(Ret);
42 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
43 return F->getArgumentList();
46 // Explicit instantiations of SymbolTableListTraits since some of the methods
47 // are not in the public header file...
48 template class SymbolTableListTraits<Argument, Function>;
49 template class SymbolTableListTraits<BasicBlock, Function>;
51 //===----------------------------------------------------------------------===//
52 // Argument Implementation
53 //===----------------------------------------------------------------------===//
55 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
56 : Value(Ty, Value::ArgumentVal) {
59 // Make sure that we get added to a function
60 LeakDetector::addGarbageObject(this);
63 Par->getArgumentList().push_back(this);
67 void Argument::setParent(Function *parent) {
69 LeakDetector::addGarbageObject(this);
72 LeakDetector::removeGarbageObject(this);
75 //===----------------------------------------------------------------------===//
76 // ParamAttrsList Implementation
77 //===----------------------------------------------------------------------===//
80 ParamAttrsList::getParamAttrs(uint16_t Index) const {
81 unsigned limit = attrs.size();
82 for (unsigned i = 0; i < limit; ++i)
83 if (attrs[i].index == Index)
84 return attrs[i].attrs;
85 return ParamAttr::None;
90 ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
92 if (Attrs & ParamAttr::ZExt)
94 if (Attrs & ParamAttr::SExt)
96 if (Attrs & ParamAttr::NoReturn)
97 Result += "noreturn ";
98 if (Attrs & ParamAttr::NoUnwind)
99 Result += "nounwind ";
100 if (Attrs & ParamAttr::InReg)
102 if (Attrs & ParamAttr::NoAlias)
103 Result += "noalias ";
104 if (Attrs & ParamAttr::StructRet)
106 if (Attrs & ParamAttr::ByVal)
108 if (Attrs & ParamAttr::Nest)
114 ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
115 for (unsigned i = 0; i < attrs.size(); ++i) {
116 unsigned val = attrs[i].attrs << 16 | attrs[i].index;
121 static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
124 ParamAttrsList::get(const ParamAttrsVector &attrVec) {
125 assert(!attrVec.empty() && "Illegal to create empty ParamAttrsList");
126 ParamAttrsList key(attrVec);
130 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
132 PAL = new ParamAttrsList(attrVec);
133 ParamAttrsLists->InsertNode(PAL, InsertPos);
138 ParamAttrsList::~ParamAttrsList() {
139 ParamAttrsLists->RemoveNode(this);
142 //===----------------------------------------------------------------------===//
143 // Function Implementation
144 //===----------------------------------------------------------------------===//
146 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
147 const std::string &name, Module *ParentModule)
148 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
150 SymTab = new ValueSymbolTable();
152 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
153 && "LLVM functions cannot return aggregate values!");
155 // Create the arguments vector, all arguments start out unnamed.
156 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
157 assert(Ty->getParamType(i) != Type::VoidTy &&
158 "Cannot have void typed arguments!");
159 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
162 // Make sure that we get added to a function
163 LeakDetector::addGarbageObject(this);
166 ParentModule->getFunctionList().push_back(this);
169 Function::~Function() {
170 dropAllReferences(); // After this it is safe to delete instructions.
172 // Delete all of the method arguments and unlink from symbol table...
173 ArgumentList.clear();
176 // Drop our reference to the parameter attributes, if any.
178 ParamAttrs->dropRef();
181 void Function::setParent(Module *parent) {
183 LeakDetector::addGarbageObject(this);
186 LeakDetector::removeGarbageObject(this);
189 void Function::setParamAttrs(ParamAttrsList *attrs) {
191 ParamAttrs->dropRef();
199 const FunctionType *Function::getFunctionType() const {
200 return cast<FunctionType>(getType()->getElementType());
203 bool Function::isVarArg() const {
204 return getFunctionType()->isVarArg();
207 const Type *Function::getReturnType() const {
208 return getFunctionType()->getReturnType();
211 void Function::removeFromParent() {
212 getParent()->getFunctionList().remove(this);
215 void Function::eraseFromParent() {
216 getParent()->getFunctionList().erase(this);
219 // dropAllReferences() - This function causes all the subinstructions to "let
220 // go" of all references that they are maintaining. This allows one to
221 // 'delete' a whole class at a time, even though there may be circular
222 // references... first all references are dropped, and all use counts go to
223 // zero. Then everything is deleted for real. Note that no operations are
224 // valid on an object that has "dropped all references", except operator
227 void Function::dropAllReferences() {
228 for (iterator I = begin(), E = end(); I != E; ++I)
229 I->dropAllReferences();
230 BasicBlocks.clear(); // Delete all basic blocks...
233 /// getIntrinsicID - This method returns the ID number of the specified
234 /// function, or Intrinsic::not_intrinsic if the function is not an
235 /// intrinsic, or if the pointer is null. This value is always defined to be
236 /// zero to allow easy checking for whether a function is intrinsic or not. The
237 /// particular intrinsic functions which correspond to this value are defined in
238 /// llvm/Intrinsics.h.
240 unsigned Function::getIntrinsicID(bool noAssert) const {
241 const ValueName *ValName = this->getValueName();
244 unsigned Len = ValName->getKeyLength();
245 const char *Name = ValName->getKeyData();
247 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
248 || Name[2] != 'v' || Name[3] != 'm')
249 return 0; // All intrinsics start with 'llvm.'
251 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
253 #define GET_FUNCTION_RECOGNIZER
254 #include "llvm/Intrinsics.gen"
255 #undef GET_FUNCTION_RECOGNIZER
256 assert(noAssert && "Invalid LLVM intrinsic name");
260 std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
261 assert(id < num_intrinsics && "Invalid intrinsic ID!");
262 const char * const Table[] = {
264 #define GET_INTRINSIC_NAME_TABLE
265 #include "llvm/Intrinsics.gen"
266 #undef GET_INTRINSIC_NAME_TABLE
270 std::string Result(Table[id]);
271 for (unsigned i = 0; i < numTys; ++i)
273 Result += "." + Tys[i]->getDescription();
277 const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
279 const Type *ResultTy = NULL;
280 std::vector<const Type*> ArgTys;
281 bool IsVarArg = false;
283 #define GET_INTRINSIC_GENERATOR
284 #include "llvm/Intrinsics.gen"
285 #undef GET_INTRINSIC_GENERATOR
287 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
290 Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
292 // There can never be multiple globals with the same name of different types,
293 // because intrinsics must be a specific type.
294 return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
295 getType(id, Tys, numTys)));
298 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
299 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
300 if (CE->getOpcode() == Instruction::BitCast) {
301 if (isa<PointerType>(CE->getOperand(0)->getType()))
302 return StripPointerCasts(CE->getOperand(0));
303 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
304 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
305 if (!CE->getOperand(i)->isNullValue())
307 return StripPointerCasts(CE->getOperand(0));
312 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
313 if (isa<PointerType>(CI->getOperand(0)->getType()))
314 return StripPointerCasts(CI->getOperand(0));
315 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
316 if (GEP->hasAllZeroIndices())
317 return StripPointerCasts(GEP->getOperand(0));