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