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 & GlobalVariable classes for the VMCore
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Module.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Support/LeakDetector.h"
19 #include "SymbolTableListTraitsImpl.h"
22 BasicBlock *ilist_traits<BasicBlock>::createNode() {
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>::createNode() {
34 Argument *Ret = new Argument(Type::IntTy);
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);
65 // Specialize setName to take care of symbol table majik
66 void Argument::setName(const std::string &name, SymbolTable *ST) {
68 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
69 "Invalid symtab argument!");
70 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
72 if (P && hasName()) P->getSymbolTable().insert(this);
75 void Argument::setParent(Function *parent) {
77 LeakDetector::addGarbageObject(this);
80 LeakDetector::removeGarbageObject(this);
83 //===----------------------------------------------------------------------===//
84 // Function Implementation
85 //===----------------------------------------------------------------------===//
87 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
88 const std::string &name, Module *ParentModule)
89 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
90 BasicBlocks.setItemParent(this);
91 BasicBlocks.setParent(this);
92 ArgumentList.setItemParent(this);
93 ArgumentList.setParent(this);
94 SymTab = new SymbolTable();
96 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
97 && "LLVM functions cannot return aggregate values!");
99 // Create the arguments vector, all arguments start out unnamed.
100 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
101 assert(Ty->getParamType(i) != Type::VoidTy &&
102 "Cannot have void typed arguments!");
103 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
106 // Make sure that we get added to a function
107 LeakDetector::addGarbageObject(this);
110 ParentModule->getFunctionList().push_back(this);
113 Function::~Function() {
114 dropAllReferences(); // After this it is safe to delete instructions.
116 // Delete all of the method arguments and unlink from symbol table...
117 ArgumentList.clear();
118 ArgumentList.setParent(0);
122 // Specialize setName to take care of symbol table majik
123 void Function::setName(const std::string &name, SymbolTable *ST) {
125 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
126 "Invalid symtab argument!");
127 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
128 Value::setName(name);
129 if (P && hasName()) P->getSymbolTable().insert(this);
132 void Function::setParent(Module *parent) {
134 LeakDetector::addGarbageObject(this);
137 LeakDetector::removeGarbageObject(this);
140 const FunctionType *Function::getFunctionType() const {
141 return cast<FunctionType>(getType()->getElementType());
144 const Type *Function::getReturnType() const {
145 return getFunctionType()->getReturnType();
148 void Function::removeFromParent() {
149 getParent()->getFunctionList().remove(this);
152 void Function::eraseFromParent() {
153 getParent()->getFunctionList().erase(this);
156 // dropAllReferences() - This function causes all the subinstructions to "let
157 // go" of all references that they are maintaining. This allows one to
158 // 'delete' a whole class at a time, even though there may be circular
159 // references... first all references are dropped, and all use counts go to
160 // zero. Then everything is deleted for real. Note that no operations are
161 // valid on an object that has "dropped all references", except operator
164 void Function::dropAllReferences() {
165 for (iterator I = begin(), E = end(); I != E; ++I)
166 I->dropAllReferences();
167 BasicBlocks.clear(); // Delete all basic blocks...
170 /// getIntrinsicID - This method returns the ID number of the specified
171 /// function, or Intrinsic::not_intrinsic if the function is not an
172 /// intrinsic, or if the pointer is null. This value is always defined to be
173 /// zero to allow easy checking for whether a function is intrinsic or not. The
174 /// particular intrinsic functions which correspond to this value are defined in
175 /// llvm/Intrinsics.h.
177 unsigned Function::getIntrinsicID() const {
178 if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
179 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
180 return 0; // All intrinsics start with 'llvm.'
182 assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
184 switch (getName()[5]) {
186 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
187 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
188 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
189 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
190 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
193 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
196 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
197 if (getName() == "llvm.gcread") return Intrinsic::gcread;
198 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
201 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
204 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
207 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
208 if (getName() == "llvm.memmove") return Intrinsic::memmove;
209 if (getName() == "llvm.memset") return Intrinsic::memset;
212 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
213 if (getName() == "llvm.readport") return Intrinsic::readport;
214 if (getName() == "llvm.readio") return Intrinsic::readio;
217 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
218 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
219 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
222 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
223 if (getName() == "llvm.va_end") return Intrinsic::vaend;
224 if (getName() == "llvm.va_start") return Intrinsic::vastart;
226 if (getName() == "llvm.writeport") return Intrinsic::writeport;
227 if (getName() == "llvm.writeio") return Intrinsic::writeio;
230 // The "llvm." namespace is reserved!
231 assert(0 && "Unknown LLVM intrinsic function!");
235 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
236 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
237 if (CE->getOpcode() == Instruction::Cast) {
238 if (isa<PointerType>(CE->getOperand(0)->getType()))
239 return StripPointerCasts(CE->getOperand(0));
240 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
241 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
242 if (!CE->getOperand(i)->isNullValue())
244 return StripPointerCasts(CE->getOperand(0));
249 if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
250 if (isa<PointerType>(CI->getOperand(0)->getType()))
251 return StripPointerCasts(CI->getOperand(0));
252 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
253 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
254 if (!isa<Constant>(GEP->getOperand(i)) ||
255 !cast<Constant>(GEP->getOperand(i))->isNullValue())
257 return StripPointerCasts(GEP->getOperand(0));