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