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, Name) {
57 // Make sure that we get added to a function
58 LeakDetector::addGarbageObject(this);
61 Par->getArgumentList().push_back(this);
64 void Argument::setParent(Function *parent) {
66 LeakDetector::addGarbageObject(this);
69 LeakDetector::removeGarbageObject(this);
72 //===----------------------------------------------------------------------===//
73 // Function Implementation
74 //===----------------------------------------------------------------------===//
76 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
77 const std::string &name, Module *ParentModule)
78 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
79 CallingConvention = 0;
80 BasicBlocks.setItemParent(this);
81 BasicBlocks.setParent(this);
82 ArgumentList.setItemParent(this);
83 ArgumentList.setParent(this);
84 SymTab = new ValueSymbolTable();
86 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
87 && "LLVM functions cannot return aggregate values!");
89 // Create the arguments vector, all arguments start out unnamed.
90 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
91 assert(Ty->getParamType(i) != Type::VoidTy &&
92 "Cannot have void typed arguments!");
93 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
96 // Make sure that we get added to a function
97 LeakDetector::addGarbageObject(this);
100 ParentModule->getFunctionList().push_back(this);
103 Function::~Function() {
104 dropAllReferences(); // After this it is safe to delete instructions.
106 // Delete all of the method arguments and unlink from symbol table...
107 ArgumentList.clear();
108 ArgumentList.setParent(0);
112 void Function::setParent(Module *parent) {
114 LeakDetector::addGarbageObject(this);
117 LeakDetector::removeGarbageObject(this);
120 const FunctionType *Function::getFunctionType() const {
121 return cast<FunctionType>(getType()->getElementType());
124 bool Function::isVarArg() const {
125 return getFunctionType()->isVarArg();
128 const Type *Function::getReturnType() const {
129 return getFunctionType()->getReturnType();
132 void Function::removeFromParent() {
133 getParent()->getFunctionList().remove(this);
136 void Function::eraseFromParent() {
137 getParent()->getFunctionList().erase(this);
140 // dropAllReferences() - This function causes all the subinstructions to "let
141 // go" of all references that they are maintaining. This allows one to
142 // 'delete' a whole class at a time, even though there may be circular
143 // references... first all references are dropped, and all use counts go to
144 // zero. Then everything is deleted for real. Note that no operations are
145 // valid on an object that has "dropped all references", except operator
148 void Function::dropAllReferences() {
149 for (iterator I = begin(), E = end(); I != E; ++I)
150 I->dropAllReferences();
151 BasicBlocks.clear(); // Delete all basic blocks...
154 /// getIntrinsicID - This method returns the ID number of the specified
155 /// function, or Intrinsic::not_intrinsic if the function is not an
156 /// intrinsic, or if the pointer is null. This value is always defined to be
157 /// zero to allow easy checking for whether a function is intrinsic or not. The
158 /// particular intrinsic functions which correspond to this value are defined in
159 /// llvm/Intrinsics.h.
161 unsigned Function::getIntrinsicID() const {
162 const std::string& Name = this->getName();
163 if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
164 || Name[2] != 'v' || Name[3] != 'm')
165 return 0; // All intrinsics start with 'llvm.'
167 assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
169 #define GET_FUNCTION_RECOGNIZER
170 #include "llvm/Intrinsics.gen"
171 #undef GET_FUNCTION_RECOGNIZER
175 const char *Intrinsic::getName(ID id) {
176 assert(id < num_intrinsics && "Invalid intrinsic ID!");
177 const char * const Table[] = {
179 #define GET_INTRINSIC_NAME_TABLE
180 #include "llvm/Intrinsics.gen"
181 #undef GET_INTRINSIC_NAME_TABLE
186 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
187 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
188 if (CE->getOpcode() == Instruction::BitCast) {
189 if (isa<PointerType>(CE->getOperand(0)->getType()))
190 return StripPointerCasts(CE->getOperand(0));
191 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
192 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
193 if (!CE->getOperand(i)->isNullValue())
195 return StripPointerCasts(CE->getOperand(0));
200 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
201 if (isa<PointerType>(CI->getOperand(0)->getType()))
202 return StripPointerCasts(CI->getOperand(0));
203 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
204 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
205 if (!isa<Constant>(GEP->getOperand(i)) ||
206 !cast<Constant>(GEP->getOperand(i))->isNullValue())
208 return StripPointerCasts(GEP->getOperand(0));