Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Function.cpp - Implement the Global object classes ----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Function class for the IR library. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Function.h" |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 16 | #include "SymbolTableListTraitsImpl.h" |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Rafael Espindola | 0e00c6c | 2011-05-16 03:05:33 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | caa4ae7 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/ValueTypes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
| 22 | #include "llvm/IR/IntrinsicInst.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CallSite.h" |
| 26 | #include "llvm/Support/InstIterator.h" |
| 27 | #include "llvm/Support/LeakDetector.h" |
| 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/RWMutex.h" |
| 30 | #include "llvm/Support/StringPool.h" |
| 31 | #include "llvm/Support/Threading.h" |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 34 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 35 | // are not in the public header file... |
John McCall | c63ca0a | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 36 | template class llvm::SymbolTableListTraits<Argument, Function>; |
| 37 | template class llvm::SymbolTableListTraits<BasicBlock, Function>; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 80dd50b | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 40 | // Argument Implementation |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 43 | void Argument::anchor() { } |
| 44 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 45 | Argument::Argument(Type *Ty, const Twine &Name, Function *Par) |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 46 | : Value(Ty, Value::ArgumentVal) { |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 47 | Parent = 0; |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 48 | |
| 49 | // Make sure that we get added to a function |
| 50 | LeakDetector::addGarbageObject(this); |
| 51 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 52 | if (Par) |
| 53 | Par->getArgumentList().push_back(this); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 54 | setName(Name); |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 57 | void Argument::setParent(Function *parent) { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 58 | if (getParent()) |
| 59 | LeakDetector::addGarbageObject(this); |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 60 | Parent = parent; |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 61 | if (getParent()) |
| 62 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 65 | /// getArgNo - Return the index of this formal argument in its containing |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 66 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 67 | unsigned Argument::getArgNo() const { |
| 68 | const Function *F = getParent(); |
| 69 | assert(F && "Argument is not in a function"); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 70 | |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 71 | Function::const_arg_iterator AI = F->arg_begin(); |
| 72 | unsigned ArgIdx = 0; |
| 73 | for (; &*AI != this; ++AI) |
| 74 | ++ArgIdx; |
| 75 | |
| 76 | return ArgIdx; |
| 77 | } |
| 78 | |
| 79 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 80 | /// in its containing function. |
| 81 | bool Argument::hasByValAttr() const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 82 | if (!getType()->isPointerTy()) return false; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 83 | return getParent()->getAttributes(). |
| 84 | hasAttribute(getArgNo()+1, Attribute::ByVal); |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | ae441cc | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 87 | unsigned Argument::getParamAlignment() const { |
| 88 | assert(getType()->isPointerTy() && "Only pointers have alignments"); |
| 89 | return getParent()->getParamAlignment(getArgNo()+1); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 90 | |
Chris Lattner | ae441cc | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Duncan Sands | be1ce0a | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 93 | /// hasNestAttr - Return true if this argument has the nest attribute on |
| 94 | /// it in its containing function. |
| 95 | bool Argument::hasNestAttr() const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 96 | if (!getType()->isPointerTy()) return false; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 97 | return getParent()->getAttributes(). |
| 98 | hasAttribute(getArgNo()+1, Attribute::Nest); |
Duncan Sands | be1ce0a | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 101 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 102 | /// it in its containing function. |
| 103 | bool Argument::hasNoAliasAttr() const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 104 | if (!getType()->isPointerTy()) return false; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 105 | return getParent()->getAttributes(). |
| 106 | hasAttribute(getArgNo()+1, Attribute::NoAlias); |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Duncan Sands | 17da06f | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 109 | /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute |
| 110 | /// on it in its containing function. |
| 111 | bool Argument::hasNoCaptureAttr() const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 112 | if (!getType()->isPointerTy()) return false; |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 113 | return getParent()->getAttributes(). |
| 114 | hasAttribute(getArgNo()+1, Attribute::NoCapture); |
Duncan Sands | 17da06f | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Owen Anderson | 7d54254 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 117 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 118 | /// it in its containing function. |
| 119 | bool Argument::hasStructRetAttr() const { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 120 | if (!getType()->isPointerTy()) return false; |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 121 | if (this != getParent()->arg_begin()) |
| 122 | return false; // StructRet param must be first param |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 123 | return getParent()->getAttributes(). |
| 124 | hasAttribute(1, Attribute::StructRet); |
Owen Anderson | 7d54254 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame^] | 127 | /// hasReturnedAttr - Return true if this argument has the returned attribute on |
| 128 | /// it in its containing function. |
| 129 | bool Argument::hasReturnedAttr() const { |
| 130 | return getParent()->getAttributes(). |
| 131 | hasAttribute(getArgNo()+1, Attribute::Returned); |
| 132 | } |
| 133 | |
Bill Wendling | 28d6572 | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 134 | /// addAttr - Add attributes to an argument. |
| 135 | void Argument::addAttr(AttributeSet AS) { |
Bill Wendling | fece442 | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 136 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | b7a1dda | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 137 | "Trying to add more than one attribute set to an argument!"); |
| 138 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 139 | getParent()->addAttributes(getArgNo() + 1, |
| 140 | AttributeSet::get(Parent->getContext(), |
| 141 | getArgNo() + 1, B)); |
Gordon Henriksen | e2435da | 2008-04-28 17:37:06 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 143 | |
Bill Wendling | 28d6572 | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 144 | /// removeAttr - Remove attributes from an argument. |
| 145 | void Argument::removeAttr(AttributeSet AS) { |
Bill Wendling | fece442 | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 146 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | b7a1dda | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 147 | "Trying to remove more than one attribute set from an argument!"); |
| 148 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 149 | getParent()->removeAttributes(getArgNo() + 1, |
| 150 | AttributeSet::get(Parent->getContext(), |
| 151 | getArgNo() + 1, B)); |
Duncan Sands | cfa3c23 | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | de6fa5f | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 80dd50b | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 154 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 155 | // Helper Methods in Function |
Reid Spencer | 4746ecf | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 156 | //===----------------------------------------------------------------------===// |
| 157 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 158 | LLVMContext &Function::getContext() const { |
| 159 | return getType()->getContext(); |
Owen Anderson | 62fabf5 | 2009-07-02 18:03:58 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 162 | FunctionType *Function::getFunctionType() const { |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 163 | return cast<FunctionType>(getType()->getElementType()); |
Reid Spencer | 4746ecf | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 166 | bool Function::isVarArg() const { |
| 167 | return getFunctionType()->isVarArg(); |
Reid Spencer | 4746ecf | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 170 | Type *Function::getReturnType() const { |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 171 | return getFunctionType()->getReturnType(); |
Duncan Sands | 827cde1 | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 174 | void Function::removeFromParent() { |
| 175 | getParent()->getFunctionList().remove(this); |
Duncan Sands | 827cde1 | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 178 | void Function::eraseFromParent() { |
| 179 | getParent()->getFunctionList().erase(this); |
Reid Spencer | 4746ecf | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Reid Spencer | 4746ecf | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 183 | // Function Implementation |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 186 | Function::Function(FunctionType *Ty, LinkageTypes Linkage, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 187 | const Twine &name, Module *ParentModule) |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 188 | : GlobalValue(PointerType::getUnqual(Ty), |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 189 | Value::FunctionVal, 0, 0, Linkage, name) { |
Chris Lattner | c8e222b | 2009-01-05 07:58:59 +0000 | [diff] [blame] | 190 | assert(FunctionType::isValidReturnType(getReturnType()) && |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 191 | "invalid return type"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 192 | SymTab = new ValueSymbolTable(); |
Chris Lattner | 4827015 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 194 | // If the function has arguments, mark them as lazily built. |
| 195 | if (Ty->getNumParams()) |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 196 | setValueSubclassData(1); // Set the "has lazy arguments" bit. |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 197 | |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 198 | // Make sure that we get added to a function |
| 199 | LeakDetector::addGarbageObject(this); |
| 200 | |
Chris Lattner | 4827015 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 201 | if (ParentModule) |
| 202 | ParentModule->getFunctionList().push_back(this); |
Duncan Sands | 79ab3e8 | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 203 | |
| 204 | // Ensure intrinsics have the right parameter attributes. |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 205 | if (unsigned IID = getIntrinsicID()) |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 206 | setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID))); |
Devang Patel | 81b2ab8 | 2008-09-02 20:51:15 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 210 | Function::~Function() { |
| 211 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 213 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 214 | ArgumentList.clear(); |
| 215 | delete SymTab; |
Reid Spencer | b90909e | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 216 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 217 | // Remove the function from the on-the-side GC table. |
| 218 | clearGC(); |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 219 | |
| 220 | // Remove the intrinsicID from the Cache. |
Chris Lattner | 1ac186e | 2013-03-20 21:04:53 +0000 | [diff] [blame] | 221 | if (getValueName() && isIntrinsic()) |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 222 | getContext().pImpl->IntrinsicIDCache.erase(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 225 | void Function::BuildLazyArguments() const { |
| 226 | // Create the arguments vector, all arguments start out unnamed. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 227 | FunctionType *FT = getFunctionType(); |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 228 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 229 | assert(!FT->getParamType(i)->isVoidTy() && |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 230 | "Cannot have void typed arguments!"); |
| 231 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 232 | } |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 234 | // Clear the lazy arguments bit. |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 235 | unsigned SDC = getSubclassDataFromValue(); |
| 236 | const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1); |
Chris Lattner | 0162c18 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | size_t Function::arg_size() const { |
| 240 | return getFunctionType()->getNumParams(); |
| 241 | } |
| 242 | bool Function::arg_empty() const { |
| 243 | return getFunctionType()->getNumParams() == 0; |
| 244 | } |
| 245 | |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 246 | void Function::setParent(Module *parent) { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 247 | if (getParent()) |
| 248 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 249 | Parent = parent; |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 250 | if (getParent()) |
| 251 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 255 | // go" of all references that they are maintaining. This allows one to |
| 256 | // 'delete' a whole class at a time, even though there may be circular |
| 257 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 258 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 259 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 260 | // delete. |
| 261 | // |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 262 | void Function::dropAllReferences() { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 263 | for (iterator I = begin(), E = end(); I != E; ++I) |
| 264 | I->dropAllReferences(); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 265 | |
Dan Gohman | a6399ae | 2010-12-07 19:56:51 +0000 | [diff] [blame] | 266 | // Delete all basic blocks. They are now unused, except possibly by |
| 267 | // blockaddresses, but BasicBlock's destructor takes care of those. |
| 268 | while (!BasicBlocks.empty()) |
| 269 | BasicBlocks.begin()->eraseFromParent(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 271 | |
Bill Wendling | 70d2ca0 | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 272 | void Function::addAttribute(unsigned i, Attribute::AttrKind attr) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 273 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 70d2ca0 | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 274 | PAL = PAL.addAttribute(getContext(), i, attr); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 275 | setAttributes(PAL); |
Eric Christopher | 0bf7b41 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Bill Wendling | 70d2ca0 | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 278 | void Function::addAttributes(unsigned i, AttributeSet attrs) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 279 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 70d2ca0 | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 280 | PAL = PAL.addAttributes(getContext(), i, attrs); |
| 281 | setAttributes(PAL); |
| 282 | } |
| 283 | |
Bill Wendling | 8246df6 | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 284 | void Function::removeAttributes(unsigned i, AttributeSet attrs) { |
Bill Wendling | 70d2ca0 | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 285 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 8246df6 | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 286 | PAL = PAL.removeAttributes(getContext(), i, attrs); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 287 | setAttributes(PAL); |
Duncan Sands | cfa3c23 | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 290 | // Maintain the GC name for each function in an on-the-side table. This saves |
| 291 | // allocating an additional word in Function for programs which do not use GC |
| 292 | // (i.e., most programs) at the cost of increased overhead for clients which do |
| 293 | // use GC. |
Owen Anderson | bf5ec1b | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 294 | static DenseMap<const Function*,PooledStringPtr> *GCNames; |
| 295 | static StringPool *GCNamePool; |
Owen Anderson | b2c0fe4 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 296 | static ManagedStatic<sys::SmartRWMutex<true> > GCLock; |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 297 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 298 | bool Function::hasGC() const { |
Owen Anderson | a9d1f2c | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 299 | sys::SmartScopedReader<true> Reader(*GCLock); |
Owen Anderson | b2c0fe4 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 300 | return GCNames && GCNames->count(this); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 303 | const char *Function::getGC() const { |
| 304 | assert(hasGC() && "Function has no collector"); |
Owen Anderson | a9d1f2c | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 305 | sys::SmartScopedReader<true> Reader(*GCLock); |
Owen Anderson | b2c0fe4 | 2009-06-18 20:56:48 +0000 | [diff] [blame] | 306 | return *(*GCNames)[this]; |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 309 | void Function::setGC(const char *Str) { |
Owen Anderson | a9d1f2c | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 310 | sys::SmartScopedWriter<true> Writer(*GCLock); |
Owen Anderson | bf5ec1b | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 311 | if (!GCNamePool) |
| 312 | GCNamePool = new StringPool(); |
| 313 | if (!GCNames) |
| 314 | GCNames = new DenseMap<const Function*,PooledStringPtr>(); |
| 315 | (*GCNames)[this] = GCNamePool->intern(Str); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 318 | void Function::clearGC() { |
Owen Anderson | a9d1f2c | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 319 | sys::SmartScopedWriter<true> Writer(*GCLock); |
Owen Anderson | bf5ec1b | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 320 | if (GCNames) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 321 | GCNames->erase(this); |
Owen Anderson | bf5ec1b | 2009-06-17 23:49:06 +0000 | [diff] [blame] | 322 | if (GCNames->empty()) { |
| 323 | delete GCNames; |
| 324 | GCNames = 0; |
| 325 | if (GCNamePool->empty()) { |
| 326 | delete GCNamePool; |
| 327 | GCNamePool = 0; |
| 328 | } |
| 329 | } |
| 330 | } |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 333 | /// copyAttributesFrom - copy all additional attributes (those not needed to |
| 334 | /// create a Function) from the Function Src to this one. |
| 335 | void Function::copyAttributesFrom(const GlobalValue *Src) { |
| 336 | assert(isa<Function>(Src) && "Expected a Function!"); |
| 337 | GlobalValue::copyAttributesFrom(Src); |
| 338 | const Function *SrcF = cast<Function>(Src); |
| 339 | setCallingConv(SrcF->getCallingConv()); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 340 | setAttributes(SrcF->getAttributes()); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 341 | if (SrcF->hasGC()) |
| 342 | setGC(SrcF->getGC()); |
| 343 | else |
| 344 | clearGC(); |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 347 | /// getIntrinsicID - This method returns the ID number of the specified |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 348 | /// function, or Intrinsic::not_intrinsic if the function is not an |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 349 | /// intrinsic, or if the pointer is null. This value is always defined to be |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 350 | /// zero to allow easy checking for whether a function is intrinsic or not. The |
| 351 | /// particular intrinsic functions which correspond to this value are defined in |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 352 | /// llvm/Intrinsics.h. Results are cached in the LLVM context, subsequent |
| 353 | /// requests for the same ID return results much faster from the cache. |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 354 | /// |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 355 | unsigned Function::getIntrinsicID() const { |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 356 | const ValueName *ValName = this->getValueName(); |
Michael Ilseman | f846f16 | 2012-12-19 23:17:20 +0000 | [diff] [blame] | 357 | if (!ValName || !isIntrinsic()) |
Reid Spencer | 085659f | 2007-04-16 07:08:44 +0000 | [diff] [blame] | 358 | return 0; |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 359 | |
| 360 | LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache = |
| 361 | getContext().pImpl->IntrinsicIDCache; |
Chris Lattner | 1ac186e | 2013-03-20 21:04:53 +0000 | [diff] [blame] | 362 | if (!IntrinsicIDCache.count(this)) { |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 363 | unsigned Id = lookupIntrinsicID(); |
| 364 | IntrinsicIDCache[this]=Id; |
| 365 | return Id; |
| 366 | } |
| 367 | return IntrinsicIDCache[this]; |
| 368 | } |
| 369 | |
| 370 | /// This private method does the actual lookup of an intrinsic ID when the query |
| 371 | /// could not be answered from the cache. |
| 372 | unsigned Function::lookupIntrinsicID() const { |
| 373 | const ValueName *ValName = this->getValueName(); |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 374 | unsigned Len = ValName->getKeyLength(); |
| 375 | const char *Name = ValName->getKeyData(); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 9a016ff | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 377 | #define GET_FUNCTION_RECOGNIZER |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 378 | #include "llvm/IR/Intrinsics.gen" |
Chris Lattner | 9a016ff | 2006-03-09 20:35:01 +0000 | [diff] [blame] | 379 | #undef GET_FUNCTION_RECOGNIZER |
Michael Ilseman | 4c8e74f | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 380 | |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 384 | std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { |
Chris Lattner | b847423 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 385 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
Chris Lattner | 607b8eb | 2011-04-25 22:14:33 +0000 | [diff] [blame] | 386 | static const char * const Table[] = { |
Chris Lattner | b847423 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 387 | "not_intrinsic", |
| 388 | #define GET_INTRINSIC_NAME_TABLE |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 389 | #include "llvm/IR/Intrinsics.gen" |
Chris Lattner | b847423 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 390 | #undef GET_INTRINSIC_NAME_TABLE |
| 391 | }; |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 392 | if (Tys.empty()) |
Reid Spencer | 1437a09 | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 393 | return Table[id]; |
| 394 | std::string Result(Table[id]); |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 395 | for (unsigned i = 0; i < Tys.size(); ++i) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 396 | if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) { |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 397 | Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) + |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 398 | EVT::getEVT(PTyp->getElementType()).getEVTString(); |
Mon P Wang | e3b3a72 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 399 | } |
| 400 | else if (Tys[i]) |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 401 | Result += "." + EVT::getEVT(Tys[i]).getEVTString(); |
Mon P Wang | e3b3a72 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 402 | } |
Reid Spencer | 1437a09 | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 403 | return Result; |
Chris Lattner | b847423 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Chris Lattner | a98aa6a | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 407 | /// IIT_Info - These are enumerators that describe the entries returned by the |
| 408 | /// getIntrinsicInfoTableEntries function. |
| 409 | /// |
| 410 | /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! |
| 411 | enum IIT_Info { |
| 412 | // Common values should be encoded with 0-15. |
| 413 | IIT_Done = 0, |
| 414 | IIT_I1 = 1, |
| 415 | IIT_I8 = 2, |
| 416 | IIT_I16 = 3, |
| 417 | IIT_I32 = 4, |
| 418 | IIT_I64 = 5, |
Michael Ilseman | 4d0b4a4 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 419 | IIT_F16 = 6, |
| 420 | IIT_F32 = 7, |
| 421 | IIT_F64 = 8, |
| 422 | IIT_V2 = 9, |
| 423 | IIT_V4 = 10, |
| 424 | IIT_V8 = 11, |
| 425 | IIT_V16 = 12, |
| 426 | IIT_V32 = 13, |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 427 | IIT_PTR = 14, |
| 428 | IIT_ARG = 15, |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 430 | // Values from 16+ are only encodable with the inefficient encoding. |
Michael Ilseman | 4d0b4a4 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 431 | IIT_MMX = 16, |
| 432 | IIT_METADATA = 17, |
| 433 | IIT_EMPTYSTRUCT = 18, |
| 434 | IIT_STRUCT2 = 19, |
| 435 | IIT_STRUCT3 = 20, |
| 436 | IIT_STRUCT4 = 21, |
| 437 | IIT_STRUCT5 = 22, |
| 438 | IIT_EXTEND_VEC_ARG = 23, |
| 439 | IIT_TRUNC_VEC_ARG = 24, |
| 440 | IIT_ANYPTR = 25 |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | |
| 444 | static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, |
| 445 | SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { |
Chris Lattner | 626b108 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 446 | IIT_Info Info = IIT_Info(Infos[NextElt++]); |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 447 | unsigned StructElts = 2; |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 448 | using namespace Intrinsic; |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 626b108 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 450 | switch (Info) { |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 451 | case IIT_Done: |
| 452 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); |
| 453 | return; |
| 454 | case IIT_MMX: |
| 455 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); |
| 456 | return; |
| 457 | case IIT_METADATA: |
| 458 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); |
| 459 | return; |
Michael Ilseman | 4d0b4a4 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 460 | case IIT_F16: |
| 461 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); |
| 462 | return; |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 463 | case IIT_F32: |
| 464 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); |
| 465 | return; |
| 466 | case IIT_F64: |
| 467 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); |
| 468 | return; |
| 469 | case IIT_I1: |
| 470 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); |
| 471 | return; |
| 472 | case IIT_I8: |
| 473 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); |
| 474 | return; |
| 475 | case IIT_I16: |
| 476 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); |
| 477 | return; |
| 478 | case IIT_I32: |
| 479 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); |
| 480 | return; |
| 481 | case IIT_I64: |
| 482 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); |
| 483 | return; |
Chris Lattner | 46aaf69 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 484 | case IIT_V2: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 485 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); |
| 486 | DecodeIITType(NextElt, Infos, OutputTable); |
| 487 | return; |
Chris Lattner | 46aaf69 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 488 | case IIT_V4: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 489 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); |
| 490 | DecodeIITType(NextElt, Infos, OutputTable); |
| 491 | return; |
Chris Lattner | 46aaf69 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 492 | case IIT_V8: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 493 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); |
| 494 | DecodeIITType(NextElt, Infos, OutputTable); |
| 495 | return; |
Chris Lattner | 46aaf69 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 496 | case IIT_V16: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 497 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); |
| 498 | DecodeIITType(NextElt, Infos, OutputTable); |
| 499 | return; |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 500 | case IIT_V32: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 501 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); |
| 502 | DecodeIITType(NextElt, Infos, OutputTable); |
| 503 | return; |
Chris Lattner | a48289a | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 504 | case IIT_PTR: |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 505 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); |
| 506 | DecodeIITType(NextElt, Infos, OutputTable); |
| 507 | return; |
Chris Lattner | a48289a | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 508 | case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 509 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 510 | Infos[NextElt++])); |
| 511 | DecodeIITType(NextElt, Infos, OutputTable); |
| 512 | return; |
Pete Cooper | b428511 | 2012-05-21 23:21:28 +0000 | [diff] [blame] | 513 | } |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 514 | case IIT_ARG: { |
| 515 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 516 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); |
| 517 | return; |
| 518 | } |
| 519 | case IIT_EXTEND_VEC_ARG: { |
| 520 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 521 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument, |
| 522 | ArgInfo)); |
| 523 | return; |
| 524 | } |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 525 | case IIT_TRUNC_VEC_ARG: { |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 526 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 527 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument, |
| 528 | ArgInfo)); |
| 529 | return; |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 531 | case IIT_EMPTYSTRUCT: |
| 532 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); |
| 533 | return; |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 534 | case IIT_STRUCT5: ++StructElts; // FALL THROUGH. |
| 535 | case IIT_STRUCT4: ++StructElts; // FALL THROUGH. |
| 536 | case IIT_STRUCT3: ++StructElts; // FALL THROUGH. |
| 537 | case IIT_STRUCT2: { |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 538 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); |
| 539 | |
Chris Lattner | d7cf5eb | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 540 | for (unsigned i = 0; i != StructElts; ++i) |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 541 | DecodeIITType(NextElt, Infos, OutputTable); |
| 542 | return; |
Chris Lattner | 46aaf69 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | a98aa6a | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 544 | } |
| 545 | llvm_unreachable("unhandled"); |
| 546 | } |
Chris Lattner | a98aa6a | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 548 | |
| 549 | #define GET_INTRINSIC_GENERATOR_GLOBAL |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 550 | #include "llvm/IR/Intrinsics.gen" |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 551 | #undef GET_INTRINSIC_GENERATOR_GLOBAL |
| 552 | |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 553 | void Intrinsic::getIntrinsicInfoTableEntries(ID id, |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 554 | SmallVectorImpl<IITDescriptor> &T){ |
Chris Lattner | a98aa6a | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 555 | // Check to see if the intrinsic's type was expressible by the table. |
| 556 | unsigned TableVal = IIT_Table[id-1]; |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 387c9dc | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 558 | // Decode the TableVal into an array of IITValues. |
| 559 | SmallVector<unsigned char, 8> IITValues; |
| 560 | ArrayRef<unsigned char> IITEntries; |
| 561 | unsigned NextElt = 0; |
| 562 | if ((TableVal >> 31) != 0) { |
| 563 | // This is an offset into the IIT_LongEncodingTable. |
| 564 | IITEntries = IIT_LongEncodingTable; |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 387c9dc | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 566 | // Strip sentinel bit. |
| 567 | NextElt = (TableVal << 1) >> 1; |
| 568 | } else { |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 569 | // Decode the TableVal into an array of IITValues. If the entry was encoded |
| 570 | // into a single word in the table itself, decode it now. |
Chris Lattner | 626b108 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 571 | do { |
| 572 | IITValues.push_back(TableVal & 0xF); |
| 573 | TableVal >>= 4; |
| 574 | } while (TableVal); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 387c9dc | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 576 | IITEntries = IITValues; |
| 577 | NextElt = 0; |
Chris Lattner | a98aa6a | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 578 | } |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 579 | |
| 580 | // Okay, decode the table into the output vector of IITDescriptors. |
| 581 | DecodeIITType(NextElt, IITEntries, T); |
| 582 | while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) |
| 583 | DecodeIITType(NextElt, IITEntries, T); |
| 584 | } |
| 585 | |
| 586 | |
| 587 | static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 588 | ArrayRef<Type*> Tys, LLVMContext &Context) { |
| 589 | using namespace Intrinsic; |
| 590 | IITDescriptor D = Infos.front(); |
| 591 | Infos = Infos.slice(1); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 593 | switch (D.Kind) { |
| 594 | case IITDescriptor::Void: return Type::getVoidTy(Context); |
| 595 | case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); |
| 596 | case IITDescriptor::Metadata: return Type::getMetadataTy(Context); |
Michael Ilseman | 4d0b4a4 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 597 | case IITDescriptor::Half: return Type::getHalfTy(Context); |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 598 | case IITDescriptor::Float: return Type::getFloatTy(Context); |
| 599 | case IITDescriptor::Double: return Type::getDoubleTy(Context); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 601 | case IITDescriptor::Integer: |
| 602 | return IntegerType::get(Context, D.Integer_Width); |
| 603 | case IITDescriptor::Vector: |
| 604 | return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); |
| 605 | case IITDescriptor::Pointer: |
| 606 | return PointerType::get(DecodeFixedType(Infos, Tys, Context), |
| 607 | D.Pointer_AddressSpace); |
| 608 | case IITDescriptor::Struct: { |
| 609 | Type *Elts[5]; |
| 610 | assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); |
| 611 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 612 | Elts[i] = DecodeFixedType(Infos, Tys, Context); |
| 613 | return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements)); |
| 614 | } |
| 615 | |
| 616 | case IITDescriptor::Argument: |
| 617 | return Tys[D.getArgumentNumber()]; |
| 618 | case IITDescriptor::ExtendVecArgument: |
| 619 | return VectorType::getExtendedElementVectorType(cast<VectorType>( |
| 620 | Tys[D.getArgumentNumber()])); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 622 | case IITDescriptor::TruncVecArgument: |
| 623 | return VectorType::getTruncatedElementVectorType(cast<VectorType>( |
| 624 | Tys[D.getArgumentNumber()])); |
| 625 | } |
| 626 | llvm_unreachable("unhandled"); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | |
| 631 | FunctionType *Intrinsic::getType(LLVMContext &Context, |
| 632 | ID id, ArrayRef<Type*> Tys) { |
| 633 | SmallVector<IITDescriptor, 8> Table; |
| 634 | getIntrinsicInfoTableEntries(id, Table); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 636 | ArrayRef<IITDescriptor> TableRef = Table; |
| 637 | Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 387c9dc | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 639 | SmallVector<Type*, 8> ArgTys; |
Chris Lattner | 908a831 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 640 | while (!TableRef.empty()) |
| 641 | ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 642 | |
Michael Ilseman | 0f47e7e | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 643 | return FunctionType::get(ResultTy, ArgTys, false); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Mon P Wang | 0d52ff1 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 646 | bool Intrinsic::isOverloaded(ID id) { |
Mon P Wang | 0d52ff1 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 647 | #define GET_INTRINSIC_OVERLOAD_TABLE |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 648 | #include "llvm/IR/Intrinsics.gen" |
Mon P Wang | 0d52ff1 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 649 | #undef GET_INTRINSIC_OVERLOAD_TABLE |
Mon P Wang | 0d52ff1 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Chris Lattner | 048ffb2 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 652 | /// This defines the "Intrinsic::getAttributes(ID id)" method. |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 653 | #define GET_INTRINSIC_ATTRIBUTES |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 654 | #include "llvm/IR/Intrinsics.gen" |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 655 | #undef GET_INTRINSIC_ATTRIBUTES |
| 656 | |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 657 | Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 658 | // There can never be multiple globals with the same name of different types, |
| 659 | // because intrinsics must be a specific type. |
Duncan Sands | 79ab3e8 | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 660 | return |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 661 | cast<Function>(M->getOrInsertFunction(getName(id, Tys), |
| 662 | getType(M->getContext(), id, Tys))); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 665 | // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. |
| 666 | #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
Chandler Carruth | 351ba14 | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 667 | #include "llvm/IR/Intrinsics.gen" |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 668 | #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 669 | |
Gabor Greif | 0054c7a | 2010-03-23 14:40:20 +0000 | [diff] [blame] | 670 | /// hasAddressTaken - returns true if there are any uses of this function |
| 671 | /// other than direct calls or invokes to it. |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 672 | bool Function::hasAddressTaken(const User* *PutOffender) const { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 673 | for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 674 | const User *U = *I; |
Jay Foad | b7454fd | 2012-05-12 08:30:16 +0000 | [diff] [blame] | 675 | if (isa<BlockAddress>(U)) |
| 676 | continue; |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 677 | if (!isa<CallInst>(U) && !isa<InvokeInst>(U)) |
| 678 | return PutOffender ? (*PutOffender = U, true) : true; |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 679 | ImmutableCallSite CS(cast<Instruction>(U)); |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 680 | if (!CS.isCallee(I)) |
| 681 | return PutOffender ? (*PutOffender = U, true) : true; |
Jay Foad | 757068f | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 682 | } |
| 683 | return false; |
| 684 | } |
| 685 | |
Eli Friedman | c663305 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 686 | bool Function::isDefTriviallyDead() const { |
| 687 | // Check the linkage |
| 688 | if (!hasLinkOnceLinkage() && !hasLocalLinkage() && |
| 689 | !hasAvailableExternallyLinkage()) |
| 690 | return false; |
| 691 | |
| 692 | // Check if the function is used by anything other than a blockaddress. |
| 693 | for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) |
| 694 | if (!isa<BlockAddress>(*I)) |
| 695 | return false; |
| 696 | |
| 697 | return true; |
| 698 | } |
| 699 | |
Bill Wendling | 3c5e609 | 2011-10-17 18:43:40 +0000 | [diff] [blame] | 700 | /// callsFunctionThatReturnsTwice - Return true if the function has a call to |
| 701 | /// setjmp or other function that gcc recognizes as "returning twice". |
| 702 | bool Function::callsFunctionThatReturnsTwice() const { |
| 703 | for (const_inst_iterator |
| 704 | I = inst_begin(this), E = inst_end(this); I != E; ++I) { |
| 705 | const CallInst* callInst = dyn_cast<CallInst>(&*I); |
| 706 | if (!callInst) |
| 707 | continue; |
| 708 | if (callInst->canReturnTwice()) |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | return false; |
| 713 | } |
| 714 | |