Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Function.cpp - Implement the Global object classes ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 10 | // This file implements the Function class for the VMCore library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 89046ca | 2004-10-12 04:20:25 +0000 | [diff] [blame] | 16 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 3a07148 | 2007-08-20 19:23:34 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ValueTypes.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/LeakDetector.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/StringPool.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 20 | #include "SymbolTableListTraitsImpl.h" |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/BitVector.h" |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 26 | BasicBlock *ilist_traits<BasicBlock>::createSentinel() { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 27 | BasicBlock *Ret = BasicBlock::Create(); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 28 | // This should not be garbage monitored. |
| 29 | LeakDetector::removeGarbageObject(Ret); |
| 30 | return Ret; |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 33 | iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) { |
| 34 | return F->getBasicBlockList(); |
| 35 | } |
| 36 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 37 | Argument *ilist_traits<Argument>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 38 | Argument *Ret = new Argument(Type::Int32Ty); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 39 | // This should not be garbage monitored. |
| 40 | LeakDetector::removeGarbageObject(Ret); |
| 41 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | iplist<Argument> &ilist_traits<Argument>::getList(Function *F) { |
| 45 | return F->getArgumentList(); |
| 46 | } |
| 47 | |
| 48 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 49 | // are not in the public header file... |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 50 | template class SymbolTableListTraits<Argument, Function>; |
| 51 | template class SymbolTableListTraits<BasicBlock, Function>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 54 | // Argument Implementation |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 57 | Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 58 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 59 | Parent = 0; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 60 | |
| 61 | // Make sure that we get added to a function |
| 62 | LeakDetector::addGarbageObject(this); |
| 63 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 64 | if (Par) |
| 65 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 66 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 69 | void Argument::setParent(Function *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 70 | if (getParent()) |
| 71 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 72 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 73 | if (getParent()) |
| 74 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 77 | /// getArgNo - Return the index of this formal argument in its containing |
| 78 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
| 79 | unsigned Argument::getArgNo() const { |
| 80 | const Function *F = getParent(); |
| 81 | assert(F && "Argument is not in a function"); |
| 82 | |
| 83 | Function::const_arg_iterator AI = F->arg_begin(); |
| 84 | unsigned ArgIdx = 0; |
| 85 | for (; &*AI != this; ++AI) |
| 86 | ++ArgIdx; |
| 87 | |
| 88 | return ArgIdx; |
| 89 | } |
| 90 | |
| 91 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 92 | /// in its containing function. |
| 93 | bool Argument::hasByValAttr() const { |
| 94 | if (!isa<PointerType>(getType())) return false; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 95 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::ByVal); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 99 | /// it in its containing function. |
| 100 | bool Argument::hasNoAliasAttr() const { |
| 101 | if (!isa<PointerType>(getType())) return false; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 102 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoAlias); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 105 | /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute |
| 106 | /// on it in its containing function. |
| 107 | bool Argument::hasNoCaptureAttr() const { |
| 108 | if (!isa<PointerType>(getType())) return false; |
| 109 | return getParent()->paramHasAttr(getArgNo()+1, Attribute::NoCapture); |
| 110 | } |
| 111 | |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 112 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 113 | /// it in its containing function. |
| 114 | bool Argument::hasStructRetAttr() const { |
| 115 | if (!isa<PointerType>(getType())) return false; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 116 | if (this != getParent()->arg_begin()) |
| 117 | return false; // StructRet param must be first param |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 118 | return getParent()->paramHasAttr(1, Attribute::StructRet); |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 121 | /// addAttr - Add a Attribute to an argument |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 122 | void Argument::addAttr(Attributes attr) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 123 | getParent()->addAttribute(getArgNo() + 1, attr); |
Gordon Henriksen | 2d9cc21 | 2008-04-28 17:37:06 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 125 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 126 | /// removeAttr - Remove a Attribute from an argument |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 127 | void Argument::removeAttr(Attributes attr) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 128 | getParent()->removeAttribute(getArgNo() + 1, attr); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 130 | |
| 131 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 133 | // Helper Methods in Function |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
| 135 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 136 | const FunctionType *Function::getFunctionType() const { |
| 137 | return cast<FunctionType>(getType()->getElementType()); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 140 | bool Function::isVarArg() const { |
| 141 | return getFunctionType()->isVarArg(); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 144 | const Type *Function::getReturnType() const { |
| 145 | return getFunctionType()->getReturnType(); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 148 | void Function::removeFromParent() { |
| 149 | getParent()->getFunctionList().remove(this); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 152 | void Function::eraseFromParent() { |
| 153 | getParent()->getFunctionList().erase(this); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 156 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 157 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 158 | //===----------------------------------------------------------------------===// |
| 159 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 160 | Function::Function(const FunctionType *Ty, LinkageTypes Linkage, |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 161 | const std::string &name, Module *ParentModule) |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 162 | : GlobalValue(PointerType::getUnqual(Ty), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 163 | Value::FunctionVal, 0, 0, Linkage, name) { |
Chris Lattner | 654695b | 2009-01-05 07:58:59 +0000 | [diff] [blame^] | 164 | assert(FunctionType::isValidReturnType(getReturnType()) && |
| 165 | !isa<OpaqueType>(getReturnType()) && "invalid return type"); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 166 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 167 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 168 | // If the function has arguments, mark them as lazily built. |
| 169 | if (Ty->getNumParams()) |
| 170 | SubclassData = 1; // Set the "has lazy arguments" bit. |
| 171 | |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 172 | // Make sure that we get added to a function |
| 173 | LeakDetector::addGarbageObject(this); |
| 174 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 175 | if (ParentModule) |
| 176 | ParentModule->getFunctionList().push_back(this); |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 177 | |
| 178 | // Ensure intrinsics have the right parameter attributes. |
| 179 | if (unsigned IID = getIntrinsicID(true)) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 180 | setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID))); |
Devang Patel | d334a43 | 2008-09-02 20:51:15 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 184 | Function::~Function() { |
| 185 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 188 | ArgumentList.clear(); |
| 189 | delete SymTab; |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 190 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 191 | // Remove the function from the on-the-side GC table. |
| 192 | clearGC(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 195 | void Function::BuildLazyArguments() const { |
| 196 | // Create the arguments vector, all arguments start out unnamed. |
| 197 | const FunctionType *FT = getFunctionType(); |
| 198 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 199 | assert(FT->getParamType(i) != Type::VoidTy && |
| 200 | "Cannot have void typed arguments!"); |
| 201 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 202 | } |
| 203 | |
| 204 | // Clear the lazy arguments bit. |
| 205 | const_cast<Function*>(this)->SubclassData &= ~1; |
| 206 | } |
| 207 | |
| 208 | size_t Function::arg_size() const { |
| 209 | return getFunctionType()->getNumParams(); |
| 210 | } |
| 211 | bool Function::arg_empty() const { |
| 212 | return getFunctionType()->getNumParams() == 0; |
| 213 | } |
| 214 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 215 | void Function::setParent(Module *parent) { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 216 | if (getParent()) |
| 217 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | Parent = parent; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 219 | if (getParent()) |
| 220 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 224 | // go" of all references that they are maintaining. This allows one to |
| 225 | // 'delete' a whole class at a time, even though there may be circular |
| 226 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 227 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 228 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 229 | // delete. |
| 230 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 231 | void Function::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 232 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 233 | I->dropAllReferences(); |
Chris Lattner | c1b1651 | 2003-09-17 04:58:59 +0000 | [diff] [blame] | 234 | BasicBlocks.clear(); // Delete all basic blocks... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 236 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 237 | void Function::addAttribute(unsigned i, Attributes attr) { |
| 238 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 239 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 240 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 243 | void Function::removeAttribute(unsigned i, Attributes attr) { |
| 244 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 245 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 246 | setAttributes(PAL); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 249 | // Maintain the GC name for each function in an on-the-side table. This saves |
| 250 | // allocating an additional word in Function for programs which do not use GC |
| 251 | // (i.e., most programs) at the cost of increased overhead for clients which do |
| 252 | // use GC. |
| 253 | static DenseMap<const Function*,PooledStringPtr> *GCNames; |
| 254 | static StringPool *GCNamePool; |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 255 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 256 | bool Function::hasGC() const { |
| 257 | return GCNames && GCNames->count(this); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 260 | const char *Function::getGC() const { |
| 261 | assert(hasGC() && "Function has no collector"); |
| 262 | return *(*GCNames)[this]; |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 265 | void Function::setGC(const char *Str) { |
| 266 | if (!GCNamePool) |
| 267 | GCNamePool = new StringPool(); |
| 268 | if (!GCNames) |
| 269 | GCNames = new DenseMap<const Function*,PooledStringPtr>(); |
| 270 | (*GCNames)[this] = GCNamePool->intern(Str); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 273 | void Function::clearGC() { |
| 274 | if (GCNames) { |
| 275 | GCNames->erase(this); |
| 276 | if (GCNames->empty()) { |
| 277 | delete GCNames; |
| 278 | GCNames = 0; |
| 279 | if (GCNamePool->empty()) { |
| 280 | delete GCNamePool; |
| 281 | GCNamePool = 0; |
Gordon Henriksen | 4b904b9 | 2007-12-10 03:35:18 +0000 | [diff] [blame] | 282 | } |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 287 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 288 | /// create a Function) from the Function Src to this one. |
| 289 | void Function::copyAttributesFrom(const GlobalValue *Src) { |
| 290 | assert(isa<Function>(Src) && "Expected a Function!"); |
| 291 | GlobalValue::copyAttributesFrom(Src); |
| 292 | const Function *SrcF = cast<Function>(Src); |
| 293 | setCallingConv(SrcF->getCallingConv()); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 294 | setAttributes(SrcF->getAttributes()); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 295 | if (SrcF->hasGC()) |
| 296 | setGC(SrcF->getGC()); |
| 297 | else |
| 298 | clearGC(); |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 301 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 302 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 303 | /// intrinsic, or if the pointer is null. This value is always defined to be |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 304 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 305 | /// particular intrinsic functions which correspond to this value are defined in |
| 306 | /// llvm/Intrinsics.h. |
| 307 | /// |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 308 | unsigned Function::getIntrinsicID(bool noAssert) const { |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 309 | const ValueName *ValName = this->getValueName(); |
Reid Spencer | c5f397a | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 310 | if (!ValName) |
| 311 | return 0; |
Chris Lattner | 1e92e06 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 312 | unsigned Len = ValName->getKeyLength(); |
| 313 | const char *Name = ValName->getKeyData(); |
| 314 | |
Reid Spencer | 78d71f1 | 2007-04-16 16:56:54 +0000 | [diff] [blame] | 315 | if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l' |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 316 | || Name[2] != 'v' || Name[3] != 'm') |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 317 | return 0; // All intrinsics start with 'llvm.' |
Chris Lattner | 3284ed7 | 2003-09-19 19:31:41 +0000 | [diff] [blame] | 318 | |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 319 | assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 320 | |
Chris Lattner | ff4d4ee | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 321 | #define GET_FUNCTION_RECOGNIZER |
| 322 | #include "llvm/Intrinsics.gen" |
| 323 | #undef GET_FUNCTION_RECOGNIZER |
Reid Spencer | 9c2eec3 | 2007-04-16 06:54:34 +0000 | [diff] [blame] | 324 | assert(noAssert && "Invalid LLVM intrinsic name"); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 328 | std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 329 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
| 330 | const char * const Table[] = { |
| 331 | "not_intrinsic", |
| 332 | #define GET_INTRINSIC_NAME_TABLE |
| 333 | #include "llvm/Intrinsics.gen" |
| 334 | #undef GET_INTRINSIC_NAME_TABLE |
| 335 | }; |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 336 | if (numTys == 0) |
| 337 | return Table[id]; |
| 338 | std::string Result(Table[id]); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 339 | for (unsigned i = 0; i < numTys; ++i) { |
| 340 | if (const PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { |
| 341 | Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + |
| 342 | MVT::getMVT(PTyp->getElementType()).getMVTString(); |
| 343 | } |
| 344 | else if (Tys[i]) |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 345 | Result += "." + MVT::getMVT(Tys[i]).getMVTString(); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 346 | } |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 347 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 350 | const FunctionType *Intrinsic::getType(ID id, const Type **Tys, |
Chris Lattner | 31f82df | 2007-06-05 23:49:06 +0000 | [diff] [blame] | 351 | unsigned numTys) { |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 352 | const Type *ResultTy = NULL; |
| 353 | std::vector<const Type*> ArgTys; |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 354 | bool IsVarArg = false; |
| 355 | |
| 356 | #define GET_INTRINSIC_GENERATOR |
| 357 | #include "llvm/Intrinsics.gen" |
| 358 | #undef GET_INTRINSIC_GENERATOR |
| 359 | |
Reid Spencer | 26d9ff6 | 2007-04-09 06:11:23 +0000 | [diff] [blame] | 360 | return FunctionType::get(ResultTy, ArgTys, IsVarArg); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 363 | AttrListPtr Intrinsic::getAttributes(ID id) { |
| 364 | Attributes Attr = Attribute::None; |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 365 | |
| 366 | #define GET_INTRINSIC_ATTRIBUTES |
| 367 | #include "llvm/Intrinsics.gen" |
| 368 | #undef GET_INTRINSIC_ATTRIBUTES |
| 369 | |
| 370 | // Intrinsics cannot throw exceptions. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 371 | Attr |= Attribute::NoUnwind; |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 372 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 373 | AttributeWithIndex PAWI = AttributeWithIndex::get(~0, Attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 374 | return AttrListPtr::get(&PAWI, 1); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 377 | Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, |
| 378 | unsigned numTys) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 379 | // There can never be multiple globals with the same name of different types, |
| 380 | // because intrinsics must be a specific type. |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 381 | return |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 382 | cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys), |
| 383 | getType(id, Tys, numTys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Reid Spencer | c49dd8d | 2004-07-17 23:50:19 +0000 | [diff] [blame] | 386 | // vim: sw=2 ai |