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/IntrinsicInst.h"
17 #include "llvm/Support/LeakDetector.h"
18 #include "SymbolTableListTraitsImpl.h"
19 #include "llvm/ADT/StringExtras.h"
22 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
23 BasicBlock *Ret = new BasicBlock();
24 // This should not be garbage monitored.
25 LeakDetector::removeGarbageObject(Ret);
29 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
30 return F->getBasicBlockList();
33 Argument *ilist_traits<Argument>::createSentinel() {
34 Argument *Ret = new Argument(Type::Int32Ty);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
40 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
41 return F->getArgumentList();
44 // Explicit instantiations of SymbolTableListTraits since some of the methods
45 // are not in the public header file...
46 template class SymbolTableListTraits<Argument, Function, Function>;
47 template class SymbolTableListTraits<BasicBlock, Function, Function>;
49 //===----------------------------------------------------------------------===//
50 // Argument Implementation
51 //===----------------------------------------------------------------------===//
53 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
54 : Value(Ty, Value::ArgumentVal) {
57 // Make sure that we get added to a function
58 LeakDetector::addGarbageObject(this);
61 Par->getArgumentList().push_back(this);
65 void Argument::setParent(Function *parent) {
67 LeakDetector::addGarbageObject(this);
70 LeakDetector::removeGarbageObject(this);
73 //===----------------------------------------------------------------------===//
74 // Function Implementation
75 //===----------------------------------------------------------------------===//
77 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
78 const std::string &name, Module *ParentModule)
79 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
80 CallingConvention = 0;
81 BasicBlocks.setItemParent(this);
82 BasicBlocks.setParent(this);
83 ArgumentList.setItemParent(this);
84 ArgumentList.setParent(this);
85 SymTab = new ValueSymbolTable();
87 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
88 && "LLVM functions cannot return aggregate values!");
90 // Create the arguments vector, all arguments start out unnamed.
91 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
92 assert(Ty->getParamType(i) != Type::VoidTy &&
93 "Cannot have void typed arguments!");
94 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
97 // Make sure that we get added to a function
98 LeakDetector::addGarbageObject(this);
101 ParentModule->getFunctionList().push_back(this);
104 Function::~Function() {
105 dropAllReferences(); // After this it is safe to delete instructions.
107 // Delete all of the method arguments and unlink from symbol table...
108 ArgumentList.clear();
109 ArgumentList.setParent(0);
113 void Function::setParent(Module *parent) {
115 LeakDetector::addGarbageObject(this);
118 LeakDetector::removeGarbageObject(this);
121 const FunctionType *Function::getFunctionType() const {
122 return cast<FunctionType>(getType()->getElementType());
125 bool Function::isVarArg() const {
126 return getFunctionType()->isVarArg();
129 const Type *Function::getReturnType() const {
130 return getFunctionType()->getReturnType();
133 void Function::removeFromParent() {
134 getParent()->getFunctionList().remove(this);
137 void Function::eraseFromParent() {
138 getParent()->getFunctionList().erase(this);
141 // dropAllReferences() - This function causes all the subinstructions to "let
142 // go" of all references that they are maintaining. This allows one to
143 // 'delete' a whole class at a time, even though there may be circular
144 // references... first all references are dropped, and all use counts go to
145 // zero. Then everything is deleted for real. Note that no operations are
146 // valid on an object that has "dropped all references", except operator
149 void Function::dropAllReferences() {
150 for (iterator I = begin(), E = end(); I != E; ++I)
151 I->dropAllReferences();
152 BasicBlocks.clear(); // Delete all basic blocks...
155 /// getIntrinsicID - This method returns the ID number of the specified
156 /// function, or Intrinsic::not_intrinsic if the function is not an
157 /// intrinsic, or if the pointer is null. This value is always defined to be
158 /// zero to allow easy checking for whether a function is intrinsic or not. The
159 /// particular intrinsic functions which correspond to this value are defined in
160 /// llvm/Intrinsics.h.
162 unsigned Function::getIntrinsicID() const {
163 const ValueName *ValName = this->getValueName();
164 unsigned Len = ValName->getKeyLength();
165 const char *Name = ValName->getKeyData();
167 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
168 || Name[2] != 'v' || Name[3] != 'm')
169 return 0; // All intrinsics start with 'llvm.'
171 assert(Len != 5 && "'llvm.' is an invalid intrinsic name!");
173 #define GET_FUNCTION_RECOGNIZER
174 #include "llvm/Intrinsics.gen"
175 #undef GET_FUNCTION_RECOGNIZER
179 const char *Intrinsic::getName(ID id) {
180 assert(id < num_intrinsics && "Invalid intrinsic ID!");
181 const char * const Table[] = {
183 #define GET_INTRINSIC_NAME_TABLE
184 #include "llvm/Intrinsics.gen"
185 #undef GET_INTRINSIC_NAME_TABLE
190 const FunctionType *Intrinsic::getType(ID id) {
191 const Type *ResultTy = NULL;
192 std::vector<const Type*> ArgTys;
193 std::vector<FunctionType::ParameterAttributes> Attrs;
194 bool IsVarArg = false;
196 #define GET_INTRINSIC_GENERATOR
197 #include "llvm/Intrinsics.gen"
198 #undef GET_INTRINSIC_GENERATOR
200 return FunctionType::get(ResultTy, ArgTys, IsVarArg, Attrs);
203 Function *Intrinsic::getDeclaration(Module *M, ID id) {
204 // There can never be multiple globals with the same name of different types,
205 // because intrinsics must be a specific type.
206 return cast<Function>(M->getOrInsertFunction(getName(id), getType(id)));
209 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
210 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
211 if (CE->getOpcode() == Instruction::BitCast) {
212 if (isa<PointerType>(CE->getOperand(0)->getType()))
213 return StripPointerCasts(CE->getOperand(0));
214 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
215 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
216 if (!CE->getOperand(i)->isNullValue())
218 return StripPointerCasts(CE->getOperand(0));
223 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
224 if (isa<PointerType>(CI->getOperand(0)->getType()))
225 return StripPointerCasts(CI->getOperand(0));
226 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
227 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
228 if (!isa<Constant>(GEP->getOperand(i)) ||
229 !cast<Constant>(GEP->getOperand(i))->isNullValue())
231 return StripPointerCasts(GEP->getOperand(0));