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