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/CodeGen/ValueTypes.h"
19 #include "llvm/Support/LeakDetector.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "SymbolTableListTraitsImpl.h"
22 #include "llvm/ADT/StringExtras.h"
25 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
26 BasicBlock *Ret = new BasicBlock();
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
32 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
33 return F->getBasicBlockList();
36 Argument *ilist_traits<Argument>::createSentinel() {
37 Argument *Ret = new Argument(Type::Int32Ty);
38 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
43 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
44 return F->getArgumentList();
47 // Explicit instantiations of SymbolTableListTraits since some of the methods
48 // are not in the public header file...
49 template class SymbolTableListTraits<Argument, Function>;
50 template class SymbolTableListTraits<BasicBlock, Function>;
52 //===----------------------------------------------------------------------===//
53 // Argument Implementation
54 //===----------------------------------------------------------------------===//
56 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
57 : Value(Ty, Value::ArgumentVal) {
60 // Make sure that we get added to a function
61 LeakDetector::addGarbageObject(this);
64 Par->getArgumentList().push_back(this);
68 void Argument::setParent(Function *parent) {
70 LeakDetector::addGarbageObject(this);
73 LeakDetector::removeGarbageObject(this);
76 //===----------------------------------------------------------------------===//
77 // ParamAttrsList Implementation
78 //===----------------------------------------------------------------------===//
81 ParamAttrsList::getParamAttrs(uint16_t Index) const {
82 unsigned limit = attrs.size();
83 for (unsigned i = 0; i < limit; ++i)
84 if (attrs[i].index == Index)
85 return attrs[i].attrs;
86 return ParamAttr::None;
91 ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
93 if (Attrs & ParamAttr::ZExt)
95 if (Attrs & ParamAttr::SExt)
97 if (Attrs & ParamAttr::NoReturn)
98 Result += "noreturn ";
99 if (Attrs & ParamAttr::NoUnwind)
100 Result += "nounwind ";
101 if (Attrs & ParamAttr::InReg)
103 if (Attrs & ParamAttr::NoAlias)
104 Result += "noalias ";
105 if (Attrs & ParamAttr::StructRet)
107 if (Attrs & ParamAttr::ByVal)
109 if (Attrs & ParamAttr::Nest)
115 ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
116 for (unsigned i = 0; i < attrs.size(); ++i) {
117 unsigned val = attrs[i].attrs << 16 | attrs[i].index;
122 static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
125 ParamAttrsList::get(const ParamAttrsVector &attrVec) {
126 assert(!attrVec.empty() && "Illegal to create empty ParamAttrsList");
127 ParamAttrsList key(attrVec);
131 ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
133 PAL = new ParamAttrsList(attrVec);
134 ParamAttrsLists->InsertNode(PAL, InsertPos);
139 ParamAttrsList::~ParamAttrsList() {
140 ParamAttrsLists->RemoveNode(this);
143 //===----------------------------------------------------------------------===//
144 // Function Implementation
145 //===----------------------------------------------------------------------===//
147 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
148 const std::string &name, Module *ParentModule)
149 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
151 SymTab = new ValueSymbolTable();
153 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
154 && "LLVM functions cannot return aggregate values!");
156 // If the function has arguments, mark them as lazily built.
157 if (Ty->getNumParams())
158 SubclassData = 1; // Set the "has lazy arguments" bit.
160 // Make sure that we get added to a function
161 LeakDetector::addGarbageObject(this);
164 ParentModule->getFunctionList().push_back(this);
167 Function::~Function() {
168 dropAllReferences(); // After this it is safe to delete instructions.
170 // Delete all of the method arguments and unlink from symbol table...
171 ArgumentList.clear();
174 // Drop our reference to the parameter attributes, if any.
176 ParamAttrs->dropRef();
179 void Function::BuildLazyArguments() const {
180 // Create the arguments vector, all arguments start out unnamed.
181 const FunctionType *FT = getFunctionType();
182 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
183 assert(FT->getParamType(i) != Type::VoidTy &&
184 "Cannot have void typed arguments!");
185 ArgumentList.push_back(new Argument(FT->getParamType(i)));
188 // Clear the lazy arguments bit.
189 const_cast<Function*>(this)->SubclassData &= ~1;
192 size_t Function::arg_size() const {
193 return getFunctionType()->getNumParams();
195 bool Function::arg_empty() const {
196 return getFunctionType()->getNumParams() == 0;
199 void Function::setParent(Module *parent) {
201 LeakDetector::addGarbageObject(this);
204 LeakDetector::removeGarbageObject(this);
207 void Function::setParamAttrs(ParamAttrsList *attrs) {
209 ParamAttrs->dropRef();
217 const FunctionType *Function::getFunctionType() const {
218 return cast<FunctionType>(getType()->getElementType());
221 bool Function::isVarArg() const {
222 return getFunctionType()->isVarArg();
225 const Type *Function::getReturnType() const {
226 return getFunctionType()->getReturnType();
229 void Function::removeFromParent() {
230 getParent()->getFunctionList().remove(this);
233 void Function::eraseFromParent() {
234 getParent()->getFunctionList().erase(this);
237 // dropAllReferences() - This function causes all the subinstructions to "let
238 // go" of all references that they are maintaining. This allows one to
239 // 'delete' a whole class at a time, even though there may be circular
240 // references... first all references are dropped, and all use counts go to
241 // zero. Then everything is deleted for real. Note that no operations are
242 // valid on an object that has "dropped all references", except operator
245 void Function::dropAllReferences() {
246 for (iterator I = begin(), E = end(); I != E; ++I)
247 I->dropAllReferences();
248 BasicBlocks.clear(); // Delete all basic blocks...
251 /// getIntrinsicID - This method returns the ID number of the specified
252 /// function, or Intrinsic::not_intrinsic if the function is not an
253 /// intrinsic, or if the pointer is null. This value is always defined to be
254 /// zero to allow easy checking for whether a function is intrinsic or not. The
255 /// particular intrinsic functions which correspond to this value are defined in
256 /// llvm/Intrinsics.h.
258 unsigned Function::getIntrinsicID(bool noAssert) const {
259 const ValueName *ValName = this->getValueName();
262 unsigned Len = ValName->getKeyLength();
263 const char *Name = ValName->getKeyData();
265 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
266 || Name[2] != 'v' || Name[3] != 'm')
267 return 0; // All intrinsics start with 'llvm.'
269 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
271 #define GET_FUNCTION_RECOGNIZER
272 #include "llvm/Intrinsics.gen"
273 #undef GET_FUNCTION_RECOGNIZER
274 assert(noAssert && "Invalid LLVM intrinsic name");
278 std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
279 assert(id < num_intrinsics && "Invalid intrinsic ID!");
280 const char * const Table[] = {
282 #define GET_INTRINSIC_NAME_TABLE
283 #include "llvm/Intrinsics.gen"
284 #undef GET_INTRINSIC_NAME_TABLE
288 std::string Result(Table[id]);
289 for (unsigned i = 0; i < numTys; ++i)
291 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
295 const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
297 const Type *ResultTy = NULL;
298 std::vector<const Type*> ArgTys;
299 bool IsVarArg = false;
301 #define GET_INTRINSIC_GENERATOR
302 #include "llvm/Intrinsics.gen"
303 #undef GET_INTRINSIC_GENERATOR
305 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
308 Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
310 // There can never be multiple globals with the same name of different types,
311 // because intrinsics must be a specific type.
312 return cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
313 getType(id, Tys, numTys)));
316 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
317 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
318 if (CE->getOpcode() == Instruction::BitCast) {
319 if (isa<PointerType>(CE->getOperand(0)->getType()))
320 return StripPointerCasts(CE->getOperand(0));
321 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
322 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
323 if (!CE->getOperand(i)->isNullValue())
325 return StripPointerCasts(CE->getOperand(0));
330 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
331 if (isa<PointerType>(CI->getOperand(0)->getType()))
332 return StripPointerCasts(CI->getOperand(0));
333 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
334 if (GEP->hasAllZeroIndices())
335 return StripPointerCasts(GEP->getOperand(0));