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 // a table of all Alpha intrinsic functions
186 std::string name; // The name of the intrinsic
187 unsigned id; // Its ID number
188 } alpha_intrinsics[] = {
189 { "llvm.alpha.ctlz", Intrinsic::alpha_ctlz },
190 { "llvm.alpha.cttz", Intrinsic::alpha_cttz },
191 { "llvm.alpha.ctpop", Intrinsic::alpha_ctpop },
192 { "llvm.alpha.umulh", Intrinsic::alpha_umulh },
193 { "llvm.alpha.vecop", Intrinsic::alpha_vecop },
194 { "llvm.alpha.pup", Intrinsic::alpha_pup },
195 { "llvm.alpha.bytezap", Intrinsic::alpha_bytezap },
196 { "llvm.alpha.bytemanip", Intrinsic::alpha_bytemanip },
197 { "llvm.alpha.dfp_bop", Intrinsic::alpha_dfpbop },
198 { "llvm.alpha.dfp_uop", Intrinsic::alpha_dfpuop },
199 { "llvm.alpha.unordered", Intrinsic::alpha_unordered },
200 { "llvm.alpha.uqtodfp", Intrinsic::alpha_uqtodfp },
201 { "llvm.alpha.uqtosfp", Intrinsic::alpha_uqtosfp },
202 { "llvm.alpha.dfptosq", Intrinsic::alpha_dfptosq },
203 { "llvm.alpha.sfptosq", Intrinsic::alpha_sfptosq },
205 const unsigned num_alpha_intrinsics =
206 sizeof(alpha_intrinsics) / sizeof(*alpha_intrinsics);
208 switch (getName()[5]) {
210 if (getName().size() > 11 &&
211 std::string(getName().begin()+4, getName().begin()+11) == ".alpha.")
212 for (unsigned i = 0; i < num_alpha_intrinsics; ++i)
213 if (getName() == alpha_intrinsics[i].name)
214 return alpha_intrinsics[i].id;
217 if (getName() == "llvm.dbg.stoppoint") return Intrinsic::dbg_stoppoint;
218 if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
219 if (getName() == "llvm.dbg.region.end") return Intrinsic::dbg_region_end;
220 if (getName() == "llvm.dbg.func.start") return Intrinsic::dbg_func_start;
221 if (getName() == "llvm.dbg.declare") return Intrinsic::dbg_declare;
224 if (getName() == "llvm.frameaddress") return Intrinsic::frameaddress;
227 if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
228 if (getName() == "llvm.gcread") return Intrinsic::gcread;
229 if (getName() == "llvm.gcroot") return Intrinsic::gcroot;
232 if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
235 if (getName() == "llvm.longjmp") return Intrinsic::longjmp;
238 if (getName() == "llvm.memcpy") return Intrinsic::memcpy;
239 if (getName() == "llvm.memmove") return Intrinsic::memmove;
240 if (getName() == "llvm.memset") return Intrinsic::memset;
243 if (getName() == "llvm.returnaddress") return Intrinsic::returnaddress;
244 if (getName() == "llvm.readport") return Intrinsic::readport;
245 if (getName() == "llvm.readio") return Intrinsic::readio;
248 if (getName() == "llvm.setjmp") return Intrinsic::setjmp;
249 if (getName() == "llvm.sigsetjmp") return Intrinsic::sigsetjmp;
250 if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
253 if (getName() == "llvm.va_copy") return Intrinsic::vacopy;
254 if (getName() == "llvm.va_end") return Intrinsic::vaend;
255 if (getName() == "llvm.va_start") return Intrinsic::vastart;
257 if (getName() == "llvm.writeport") return Intrinsic::writeport;
258 if (getName() == "llvm.writeio") return Intrinsic::writeio;
261 // The "llvm." namespace is reserved!
262 assert(0 && "Unknown LLVM intrinsic function!");
266 Value *MemIntrinsic::StripPointerCasts(Value *Ptr) {
267 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
268 if (CE->getOpcode() == Instruction::Cast) {
269 if (isa<PointerType>(CE->getOperand(0)->getType()))
270 return StripPointerCasts(CE->getOperand(0));
271 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
272 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
273 if (!CE->getOperand(i)->isNullValue())
275 return StripPointerCasts(CE->getOperand(0));
280 if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
281 if (isa<PointerType>(CI->getOperand(0)->getType()))
282 return StripPointerCasts(CI->getOperand(0));
283 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
284 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
285 if (!isa<Constant>(CE->getOperand(i)) ||
286 !cast<Constant>(CE->getOperand(i))->isNullValue())
288 return StripPointerCasts(CE->getOperand(0));