Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Function.cpp - Implement the Global object classes ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Function class for the IR library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Function.h" |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 16 | #include "SymbolTableListTraitsImpl.h" |
Rafael Espindola | 2050af8 | 2011-05-16 03:05:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | b392d30 | 2004-12-05 06:43:27 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ValueTypes.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CallSite.h" |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
| 25 | #include "llvm/IR/LLVMContext.h" |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 26 | #include "llvm/IR/MDBuilder.h" |
| 27 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 31 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 32 | // are not in the public header file... |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 33 | template class llvm::SymbolTableListTraits<Argument>; |
| 34 | template class llvm::SymbolTableListTraits<BasicBlock>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 37 | // Argument Implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 40 | void Argument::anchor() { } |
| 41 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 42 | Argument::Argument(Type *Ty, const Twine &Name, Function *Par) |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 43 | : Value(Ty, Value::ArgumentVal) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 44 | Parent = nullptr; |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 46 | if (Par) |
| 47 | Par->getArgumentList().push_back(this); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 48 | setName(Name); |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | 9ed7aef | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 51 | void Argument::setParent(Function *parent) { |
| 52 | Parent = parent; |
| 53 | } |
| 54 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 55 | /// getArgNo - Return the index of this formal argument in its containing |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 56 | /// function. For example in "void foo(int a, float b)" a is 0 and b is 1. |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 57 | unsigned Argument::getArgNo() const { |
| 58 | const Function *F = getParent(); |
| 59 | assert(F && "Argument is not in a function"); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 60 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 61 | Function::const_arg_iterator AI = F->arg_begin(); |
| 62 | unsigned ArgIdx = 0; |
| 63 | for (; &*AI != this; ++AI) |
| 64 | ++ArgIdx; |
| 65 | |
| 66 | return ArgIdx; |
| 67 | } |
| 68 | |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 69 | /// hasNonNullAttr - Return true if this argument has the nonnull attribute on |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 70 | /// it in its containing function. Also returns true if at least one byte is |
| 71 | /// known to be dereferenceable and the pointer is in addrspace(0). |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 72 | bool Argument::hasNonNullAttr() const { |
| 73 | if (!getType()->isPointerTy()) return false; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 74 | if (getParent()->getAttributes(). |
| 75 | hasAttribute(getArgNo()+1, Attribute::NonNull)) |
| 76 | return true; |
| 77 | else if (getDereferenceableBytes() > 0 && |
| 78 | getType()->getPointerAddressSpace() == 0) |
| 79 | return true; |
| 80 | return false; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 83 | /// hasByValAttr - Return true if this argument has the byval attribute on it |
| 84 | /// in its containing function. |
| 85 | bool Argument::hasByValAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 86 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 87 | return hasAttribute(Attribute::ByVal); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 90 | bool Argument::hasSwiftSelfAttr() const { |
| 91 | return getParent()->getAttributes(). |
| 92 | hasAttribute(getArgNo()+1, Attribute::SwiftSelf); |
| 93 | } |
| 94 | |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 95 | bool Argument::hasSwiftErrorAttr() const { |
| 96 | return getParent()->getAttributes(). |
| 97 | hasAttribute(getArgNo()+1, Attribute::SwiftError); |
| 98 | } |
| 99 | |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 100 | /// \brief Return true if this argument has the inalloca attribute on it in |
| 101 | /// its containing function. |
| 102 | bool Argument::hasInAllocaAttr() const { |
| 103 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 104 | return hasAttribute(Attribute::InAlloca); |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 107 | bool Argument::hasByValOrInAllocaAttr() const { |
| 108 | if (!getType()->isPointerTy()) return false; |
| 109 | AttributeSet Attrs = getParent()->getAttributes(); |
| 110 | return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) || |
| 111 | Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca); |
| 112 | } |
| 113 | |
Chris Lattner | 4d37d99 | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 114 | unsigned Argument::getParamAlignment() const { |
| 115 | assert(getType()->isPointerTy() && "Only pointers have alignments"); |
| 116 | return getParent()->getParamAlignment(getArgNo()+1); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 4d37d99 | 2011-05-22 23:57:23 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 120 | uint64_t Argument::getDereferenceableBytes() const { |
| 121 | assert(getType()->isPointerTy() && |
| 122 | "Only pointers have dereferenceable bytes"); |
| 123 | return getParent()->getDereferenceableBytes(getArgNo()+1); |
| 124 | } |
| 125 | |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 126 | uint64_t Argument::getDereferenceableOrNullBytes() const { |
| 127 | assert(getType()->isPointerTy() && |
| 128 | "Only pointers have dereferenceable bytes"); |
| 129 | return getParent()->getDereferenceableOrNullBytes(getArgNo()+1); |
| 130 | } |
| 131 | |
Duncan Sands | 5d96f3f | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 132 | /// hasNestAttr - Return true if this argument has the nest attribute on |
| 133 | /// it in its containing function. |
| 134 | bool Argument::hasNestAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 135 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 136 | return hasAttribute(Attribute::Nest); |
Duncan Sands | 5d96f3f | 2009-12-11 08:36:17 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 139 | /// hasNoAliasAttr - Return true if this argument has the noalias attribute on |
| 140 | /// it in its containing function. |
| 141 | bool Argument::hasNoAliasAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 142 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 143 | return hasAttribute(Attribute::NoAlias); |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 146 | /// hasNoCaptureAttr - Return true if this argument has the nocapture attribute |
| 147 | /// on it in its containing function. |
| 148 | bool Argument::hasNoCaptureAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 149 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 150 | return hasAttribute(Attribute::NoCapture); |
Duncan Sands | df128eb | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 153 | /// hasSRetAttr - Return true if this argument has the sret attribute on |
| 154 | /// it in its containing function. |
| 155 | bool Argument::hasStructRetAttr() const { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 156 | if (!getType()->isPointerTy()) return false; |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 157 | return hasAttribute(Attribute::StructRet); |
Owen Anderson | c64dfb4 | 2008-02-17 23:22:28 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 160 | /// hasReturnedAttr - Return true if this argument has the returned attribute on |
| 161 | /// it in its containing function. |
| 162 | bool Argument::hasReturnedAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 163 | return hasAttribute(Attribute::Returned); |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 166 | /// hasZExtAttr - Return true if this argument has the zext attribute on it in |
| 167 | /// its containing function. |
| 168 | bool Argument::hasZExtAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 169 | return hasAttribute(Attribute::ZExt); |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /// hasSExtAttr Return true if this argument has the sext attribute on it in its |
| 173 | /// containing function. |
| 174 | bool Argument::hasSExtAttr() const { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 175 | return hasAttribute(Attribute::SExt); |
Juergen Ributzka | 384c3b5 | 2014-08-05 05:43:41 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 178 | /// Return true if this argument has the readonly or readnone attribute on it |
| 179 | /// in its containing function. |
| 180 | bool Argument::onlyReadsMemory() const { |
| 181 | return getParent()->getAttributes(). |
| 182 | hasAttribute(getArgNo()+1, Attribute::ReadOnly) || |
| 183 | getParent()->getAttributes(). |
| 184 | hasAttribute(getArgNo()+1, Attribute::ReadNone); |
| 185 | } |
| 186 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 187 | /// addAttr - Add attributes to an argument. |
| 188 | void Argument::addAttr(AttributeSet AS) { |
Bill Wendling | efbbbfd | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 189 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | 1c20ff0 | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 190 | "Trying to add more than one attribute set to an argument!"); |
| 191 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 192 | getParent()->addAttributes(getArgNo() + 1, |
| 193 | AttributeSet::get(Parent->getContext(), |
| 194 | getArgNo() + 1, B)); |
Gordon Henriksen | 2d9cc21 | 2008-04-28 17:37:06 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 196 | |
Bill Wendling | 49bc76c | 2013-01-23 06:14:59 +0000 | [diff] [blame] | 197 | /// removeAttr - Remove attributes from an argument. |
| 198 | void Argument::removeAttr(AttributeSet AS) { |
Bill Wendling | efbbbfd | 2013-02-21 19:46:51 +0000 | [diff] [blame] | 199 | assert(AS.getNumSlots() <= 1 && |
Bill Wendling | 1c20ff0 | 2013-02-20 23:04:11 +0000 | [diff] [blame] | 200 | "Trying to remove more than one attribute set from an argument!"); |
| 201 | AttrBuilder B(AS, AS.getSlotIndex(0)); |
| 202 | getParent()->removeAttributes(getArgNo() + 1, |
| 203 | AttributeSet::get(Parent->getContext(), |
| 204 | getArgNo() + 1, B)); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | e30f09d | 2008-01-24 17:47:11 +0000 | [diff] [blame] | 206 | |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 207 | /// hasAttribute - Checks if an argument has a given attribute. |
| 208 | bool Argument::hasAttribute(Attribute::AttrKind Kind) const { |
| 209 | return getParent()->hasAttribute(getArgNo() + 1, Kind); |
| 210 | } |
| 211 | |
Chris Lattner | d255ae2 | 2002-04-09 19:39:35 +0000 | [diff] [blame] | 212 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 213 | // Helper Methods in Function |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 216 | bool Function::isMaterializable() const { |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 217 | return getGlobalObjectSubClassData() & (1 << IsMaterializableBit); |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 220 | void Function::setIsMaterializable(bool V) { |
Peter Collingbourne | cceae7f | 2016-05-31 23:01:54 +0000 | [diff] [blame] | 221 | unsigned Mask = 1 << IsMaterializableBit; |
| 222 | setGlobalObjectSubClassData((~Mask & getGlobalObjectSubClassData()) | |
| 223 | (V ? Mask : 0u)); |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 224 | } |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 225 | |
Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 226 | LLVMContext &Function::getContext() const { |
| 227 | return getType()->getContext(); |
Owen Anderson | 0a2c458 | 2009-07-02 18:03:58 +0000 | [diff] [blame] | 228 | } |
| 229 | |
David Blaikie | ab105bb | 2016-01-15 23:07:58 +0000 | [diff] [blame] | 230 | FunctionType *Function::getFunctionType() const { |
| 231 | return cast<FunctionType>(getValueType()); |
| 232 | } |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 234 | bool Function::isVarArg() const { |
| 235 | return getFunctionType()->isVarArg(); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 238 | Type *Function::getReturnType() const { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 239 | return getFunctionType()->getReturnType(); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 242 | void Function::removeFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 243 | getParent()->getFunctionList().remove(getIterator()); |
Duncan Sands | 185eeac | 2007-11-25 14:10:56 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 246 | void Function::eraseFromParent() { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 247 | getParent()->getFunctionList().erase(getIterator()); |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Reid Spencer | 019c886 | 2007-04-09 15:01:12 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 251 | // Function Implementation |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 252 | //===----------------------------------------------------------------------===// |
| 253 | |
Rafael Espindola | 1b47a28 | 2014-10-23 15:20:05 +0000 | [diff] [blame] | 254 | Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, |
| 255 | Module *ParentModule) |
David Blaikie | d583b19 | 2015-08-21 21:35:28 +0000 | [diff] [blame] | 256 | : GlobalObject(Ty, Value::FunctionVal, |
David Blaikie | ab105bb | 2016-01-15 23:07:58 +0000 | [diff] [blame] | 257 | OperandTraits<Function>::op_begin(this), 0, Linkage, name) { |
Chris Lattner | 654695b | 2009-01-05 07:58:59 +0000 | [diff] [blame] | 258 | assert(FunctionType::isValidReturnType(getReturnType()) && |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 259 | "invalid return type"); |
Duncan P. N. Exon Smith | 4472b77 | 2015-04-24 20:47:23 +0000 | [diff] [blame] | 260 | setGlobalObjectSubClassData(0); |
Mehdi Amini | a53d49e | 2016-09-17 06:00:02 +0000 | [diff] [blame] | 261 | |
| 262 | // We only need a symbol table for a function if the context keeps value names |
| 263 | if (!getContext().shouldDiscardValueNames()) |
| 264 | SymTab = make_unique<ValueSymbolTable>(); |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 265 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 266 | // If the function has arguments, mark them as lazily built. |
| 267 | if (Ty->getNumParams()) |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 268 | setValueSubclassData(1); // Set the "has lazy arguments" bit. |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 6213ae0 | 2002-09-06 20:46:32 +0000 | [diff] [blame] | 270 | if (ParentModule) |
| 271 | ParentModule->getFunctionList().push_back(this); |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 272 | |
| 273 | // Ensure intrinsics have the right parameter attributes. |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 274 | // Note, the IntID field will have been set in Value::setName if this function |
| 275 | // name is a valid intrinsic ID. |
| 276 | if (IntID) |
| 277 | setAttributes(Intrinsic::getAttributes(getContext(), IntID)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 280 | Function::~Function() { |
| 281 | dropAllReferences(); // After this it is safe to delete instructions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 283 | // Delete all of the method arguments and unlink from symbol table... |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 284 | ArgumentList.clear(); |
Reid Spencer | c6a8384 | 2007-04-22 17:28:03 +0000 | [diff] [blame] | 285 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 286 | // Remove the function from the on-the-side GC table. |
| 287 | clearGC(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 290 | void Function::BuildLazyArguments() const { |
| 291 | // Create the arguments vector, all arguments start out unnamed. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 292 | FunctionType *FT = getFunctionType(); |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 293 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 294 | assert(!FT->getParamType(i)->isVoidTy() && |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 295 | "Cannot have void typed arguments!"); |
| 296 | ArgumentList.push_back(new Argument(FT->getParamType(i))); |
| 297 | } |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 298 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 299 | // Clear the lazy arguments bit. |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 300 | unsigned SDC = getSubclassDataFromValue(); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 301 | const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0)); |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Duncan P. N. Exon Smith | bdfc984 | 2016-04-06 06:38:15 +0000 | [diff] [blame] | 304 | void Function::stealArgumentListFrom(Function &Src) { |
| 305 | assert(isDeclaration() && "Expected no references to current arguments"); |
| 306 | |
| 307 | // Drop the current arguments, if any, and set the lazy argument bit. |
| 308 | if (!hasLazyArguments()) { |
| 309 | assert(llvm::all_of(ArgumentList, |
| 310 | [](const Argument &A) { return A.use_empty(); }) && |
| 311 | "Expected arguments to be unused in declaration"); |
| 312 | ArgumentList.clear(); |
| 313 | setValueSubclassData(getSubclassDataFromValue() | (1 << 0)); |
| 314 | } |
| 315 | |
| 316 | // Nothing to steal if Src has lazy arguments. |
| 317 | if (Src.hasLazyArguments()) |
| 318 | return; |
| 319 | |
| 320 | // Steal arguments from Src, and fix the lazy argument bits. |
| 321 | ArgumentList.splice(ArgumentList.end(), Src.ArgumentList); |
| 322 | setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0)); |
| 323 | Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0)); |
| 324 | } |
| 325 | |
Chris Lattner | e2de908 | 2007-08-18 06:14:52 +0000 | [diff] [blame] | 326 | size_t Function::arg_size() const { |
| 327 | return getFunctionType()->getNumParams(); |
| 328 | } |
| 329 | bool Function::arg_empty() const { |
| 330 | return getFunctionType()->getNumParams() == 0; |
| 331 | } |
| 332 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 333 | void Function::setParent(Module *parent) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 334 | Parent = parent; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 337 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 338 | // go" of all references that they are maintaining. This allows one to |
| 339 | // 'delete' a whole class at a time, even though there may be circular |
| 340 | // references... first all references are dropped, and all use counts go to |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 341 | // zero. Then everything is deleted for real. Note that no operations are |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 342 | // valid on an object that has "dropped all references", except operator |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 343 | // delete. |
| 344 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 345 | void Function::dropAllReferences() { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 346 | setIsMaterializable(false); |
| 347 | |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 348 | for (BasicBlock &BB : *this) |
| 349 | BB.dropAllReferences(); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 350 | |
Dan Gohman | f844b3b | 2010-12-07 19:56:51 +0000 | [diff] [blame] | 351 | // Delete all basic blocks. They are now unused, except possibly by |
| 352 | // blockaddresses, but BasicBlock's destructor takes care of those. |
| 353 | while (!BasicBlocks.empty()) |
| 354 | BasicBlocks.begin()->eraseFromParent(); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 355 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 356 | // Drop uses of any optional data (real or placeholder). |
| 357 | if (getNumOperands()) { |
| 358 | User::dropAllReferences(); |
| 359 | setNumHungOffUseOperands(0); |
| 360 | setValueSubclassData(getSubclassDataFromValue() & ~0xe); |
| 361 | } |
Duncan P. N. Exon Smith | e2510cd | 2015-04-24 21:51:02 +0000 | [diff] [blame] | 362 | |
| 363 | // Metadata is stored in a side-table. |
| 364 | clearMetadata(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 365 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 366 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 367 | void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 368 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 369 | PAL = PAL.addAttribute(getContext(), i, Kind); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 370 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Amaury Sechet | 5db224e | 2016-06-12 06:17:24 +0000 | [diff] [blame] | 373 | void Function::addAttribute(unsigned i, Attribute Attr) { |
| 374 | AttributeSet PAL = getAttributes(); |
| 375 | PAL = PAL.addAttribute(getContext(), i, Attr); |
| 376 | setAttributes(PAL); |
| 377 | } |
| 378 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 379 | void Function::addAttributes(unsigned i, AttributeSet Attrs) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 380 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 381 | PAL = PAL.addAttributes(getContext(), i, Attrs); |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 382 | setAttributes(PAL); |
| 383 | } |
| 384 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 385 | void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) { |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 386 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 387 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
Amaury Sechet | 7b05a4c | 2016-03-14 01:37:29 +0000 | [diff] [blame] | 388 | setAttributes(PAL); |
| 389 | } |
| 390 | |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 391 | void Function::removeAttribute(unsigned i, StringRef Kind) { |
| 392 | AttributeSet PAL = getAttributes(); |
| 393 | PAL = PAL.removeAttribute(getContext(), i, Kind); |
| 394 | setAttributes(PAL); |
| 395 | } |
| 396 | |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 397 | void Function::removeAttributes(unsigned i, AttributeSet Attrs) { |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 398 | AttributeSet PAL = getAttributes(); |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 399 | PAL = PAL.removeAttributes(getContext(), i, Attrs); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 400 | setAttributes(PAL); |
Duncan Sands | 66336db | 2008-07-08 09:41:30 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 403 | void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
| 404 | AttributeSet PAL = getAttributes(); |
| 405 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 406 | setAttributes(PAL); |
| 407 | } |
| 408 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 409 | void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
| 410 | AttributeSet PAL = getAttributes(); |
| 411 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 412 | setAttributes(PAL); |
| 413 | } |
| 414 | |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 415 | const std::string &Function::getGC() const { |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 416 | assert(hasGC() && "Function has no collector"); |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 417 | return getContext().getGC(*this); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Benjamin Kramer | 728f444 | 2016-05-29 10:46:35 +0000 | [diff] [blame] | 420 | void Function::setGC(std::string Str) { |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 421 | setValueSubclassDataBit(14, !Str.empty()); |
| 422 | getContext().setGC(*this, std::move(Str)); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 425 | void Function::clearGC() { |
Mehdi Amini | 599ebf2 | 2016-01-08 02:28:20 +0000 | [diff] [blame] | 426 | if (!hasGC()) |
| 427 | return; |
| 428 | getContext().deleteGC(*this); |
| 429 | setValueSubclassDataBit(14, false); |
Gordon Henriksen | 71183b6 | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 432 | /// Copy all additional attributes (those not needed to create a Function) from |
| 433 | /// the Function Src to this one. |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 434 | void Function::copyAttributesFrom(const GlobalValue *Src) { |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 435 | GlobalObject::copyAttributesFrom(Src); |
Rafael Espindola | 769efe6 | 2015-12-02 20:03:17 +0000 | [diff] [blame] | 436 | const Function *SrcF = dyn_cast<Function>(Src); |
| 437 | if (!SrcF) |
| 438 | return; |
| 439 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 440 | setCallingConv(SrcF->getCallingConv()); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 441 | setAttributes(SrcF->getAttributes()); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 442 | if (SrcF->hasGC()) |
| 443 | setGC(SrcF->getGC()); |
| 444 | else |
| 445 | clearGC(); |
Vedant Kumar | 44dd987 | 2015-12-19 08:48:43 +0000 | [diff] [blame] | 446 | if (SrcF->hasPersonalityFn()) |
| 447 | setPersonalityFn(SrcF->getPersonalityFn()); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 448 | if (SrcF->hasPrefixData()) |
| 449 | setPrefixData(SrcF->getPrefixData()); |
| 450 | if (SrcF->hasPrologueData()) |
| 451 | setPrologueData(SrcF->getPrologueData()); |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 454 | /// Table of string intrinsic names indexed by enum value. |
| 455 | static const char * const IntrinsicNameTable[] = { |
| 456 | "not_intrinsic", |
| 457 | #define GET_INTRINSIC_NAME_TABLE |
| 458 | #include "llvm/IR/Intrinsics.gen" |
| 459 | #undef GET_INTRINSIC_NAME_TABLE |
| 460 | }; |
| 461 | |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 462 | /// Table of per-target intrinsic name tables. |
| 463 | #define GET_INTRINSIC_TARGET_DATA |
| 464 | #include "llvm/IR/Intrinsics.gen" |
| 465 | #undef GET_INTRINSIC_TARGET_DATA |
| 466 | |
| 467 | /// Find the segment of \c IntrinsicNameTable for intrinsics with the same |
| 468 | /// target as \c Name, or the generic table if \c Name is not target specific. |
| 469 | /// |
| 470 | /// Returns the relevant slice of \c IntrinsicNameTable |
| 471 | static ArrayRef<const char *> findTargetSubtable(StringRef Name) { |
| 472 | assert(Name.startswith("llvm.")); |
| 473 | |
| 474 | ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos); |
| 475 | // Drop "llvm." and take the first dotted component. That will be the target |
| 476 | // if this is target specific. |
| 477 | StringRef Target = Name.drop_front(5).split('.').first; |
| 478 | auto It = std::lower_bound(Targets.begin(), Targets.end(), Target, |
| 479 | [](const IntrinsicTargetInfo &TI, |
| 480 | StringRef Target) { return TI.Name < Target; }); |
| 481 | // We've either found the target or just fall back to the generic set, which |
| 482 | // is always first. |
| 483 | const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; |
| 484 | return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count); |
| 485 | } |
| 486 | |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 487 | /// \brief This does the actual lookup of an intrinsic ID which |
| 488 | /// matches the given function name. |
Tim Northover | 6b3bd61 | 2016-07-29 20:32:59 +0000 | [diff] [blame] | 489 | Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) { |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 490 | ArrayRef<const char *> NameTable = findTargetSubtable(Name); |
Reid Kleckner | c2752da | 2016-01-26 22:33:19 +0000 | [diff] [blame] | 491 | int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name); |
Justin Bogner | 92a8c61 | 2016-07-15 16:31:37 +0000 | [diff] [blame] | 492 | if (Idx == -1) |
| 493 | return Intrinsic::not_intrinsic; |
| 494 | |
| 495 | // Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have |
| 496 | // an index into a sub-table. |
| 497 | int Adjust = NameTable.data() - IntrinsicNameTable; |
| 498 | Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust); |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 499 | |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 500 | // If the intrinsic is not overloaded, require an exact match. If it is |
| 501 | // overloaded, require a prefix match. |
| 502 | bool IsPrefixMatch = Name.size() > strlen(NameTable[Idx]); |
| 503 | return IsPrefixMatch == isOverloaded(ID) ? ID : Intrinsic::not_intrinsic; |
Pete Cooper | 2124113 | 2015-05-19 00:02:25 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 506 | void Function::recalculateIntrinsicID() { |
Justin Lebar | 9cbc301 | 2016-07-28 23:58:15 +0000 | [diff] [blame] | 507 | const ValueName *ValName = this->getValueName(); |
| 508 | if (!ValName || !isIntrinsic()) { |
Pete Cooper | a7c0c18 | 2015-05-19 00:24:26 +0000 | [diff] [blame] | 509 | IntID = Intrinsic::not_intrinsic; |
| 510 | return; |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 511 | } |
Tim Northover | 6b3bd61 | 2016-07-29 20:32:59 +0000 | [diff] [blame] | 512 | IntID = lookupIntrinsicID(ValName->getKey()); |
Michael Ilseman | 516d703 | 2013-03-01 18:48:54 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 515 | /// Returns a stable mangling for the type specified for use in the name |
Philip Reames | 059ecbf | 2014-11-24 23:24:24 +0000 | [diff] [blame] | 516 | /// mangling scheme used by 'any' types in intrinsic signatures. The mangling |
| 517 | /// of named types is simply their name. Manglings for unnamed types consist |
| 518 | /// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) |
| 519 | /// combined with the mangling of their component types. A vararg function |
| 520 | /// type will have a suffix of 'vararg'. Since function types can contain |
| 521 | /// other function types, we close a function type mangling with suffix 'f' |
| 522 | /// which can't be confused with it's prefix. This ensures we don't have |
| 523 | /// collisions between two unrelated function types. Otherwise, you might |
| 524 | /// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) |
Philip Reames | 8d5d68f | 2015-01-14 23:05:17 +0000 | [diff] [blame] | 525 | /// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most |
| 526 | /// cases) fall back to the MVT codepath, where they could be mangled to |
| 527 | /// 'x86mmx', for example; matching on derived types is not sufficient to mangle |
| 528 | /// everything. |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 529 | static std::string getMangledTypeStr(Type* Ty) { |
| 530 | std::string Result; |
| 531 | if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) { |
| 532 | Result += "p" + llvm::utostr(PTyp->getAddressSpace()) + |
| 533 | getMangledTypeStr(PTyp->getElementType()); |
| 534 | } else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) { |
| 535 | Result += "a" + llvm::utostr(ATyp->getNumElements()) + |
| 536 | getMangledTypeStr(ATyp->getElementType()); |
| 537 | } else if (StructType* STyp = dyn_cast<StructType>(Ty)) { |
David Blaikie | b340f0a | 2015-05-08 18:52:28 +0000 | [diff] [blame] | 538 | assert(!STyp->isLiteral() && "TODO: implement literal types"); |
| 539 | Result += STyp->getName(); |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 540 | } else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) { |
| 541 | Result += "f_" + getMangledTypeStr(FT->getReturnType()); |
| 542 | for (size_t i = 0; i < FT->getNumParams(); i++) |
| 543 | Result += getMangledTypeStr(FT->getParamType(i)); |
| 544 | if (FT->isVarArg()) |
| 545 | Result += "vararg"; |
Philip Reames | 059ecbf | 2014-11-24 23:24:24 +0000 | [diff] [blame] | 546 | // Ensure nested function types are distinguishable. |
| 547 | Result += "f"; |
Elena Demikhovsky | 1ca72e1 | 2015-11-19 07:17:16 +0000 | [diff] [blame] | 548 | } else if (isa<VectorType>(Ty)) |
| 549 | Result += "v" + utostr(Ty->getVectorNumElements()) + |
| 550 | getMangledTypeStr(Ty->getVectorElementType()); |
| 551 | else if (Ty) |
Philip Reames | 319c48e | 2014-11-12 00:21:51 +0000 | [diff] [blame] | 552 | Result += EVT::getEVT(Ty).getEVTString(); |
| 553 | return Result; |
| 554 | } |
| 555 | |
Pete Cooper | a8db71e | 2016-08-18 18:30:54 +0000 | [diff] [blame] | 556 | StringRef Intrinsic::getName(ID id) { |
| 557 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
Pete Cooper | a5f8c72 | 2016-08-22 20:18:28 +0000 | [diff] [blame] | 558 | assert(!isOverloaded(id) && |
| 559 | "This version of getName does not support overloading"); |
Pete Cooper | a8db71e | 2016-08-18 18:30:54 +0000 | [diff] [blame] | 560 | return IntrinsicNameTable[id]; |
| 561 | } |
| 562 | |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 563 | std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) { |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 564 | assert(id < num_intrinsics && "Invalid intrinsic ID!"); |
Reid Kleckner | 0b5220d | 2016-01-26 02:06:41 +0000 | [diff] [blame] | 565 | std::string Result(IntrinsicNameTable[id]); |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 566 | for (Type *Ty : Tys) { |
| 567 | Result += "." + getMangledTypeStr(Ty); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 568 | } |
Reid Spencer | 2a2117c | 2007-04-01 07:25:33 +0000 | [diff] [blame] | 569 | return Result; |
Chris Lattner | 71b8c98 | 2006-03-25 06:32:47 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 572 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 573 | /// IIT_Info - These are enumerators that describe the entries returned by the |
| 574 | /// getIntrinsicInfoTableEntries function. |
| 575 | /// |
| 576 | /// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter! |
| 577 | enum IIT_Info { |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 578 | // Common values should be encoded with 0-15. |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 579 | IIT_Done = 0, |
| 580 | IIT_I1 = 1, |
| 581 | IIT_I8 = 2, |
| 582 | IIT_I16 = 3, |
| 583 | IIT_I32 = 4, |
| 584 | IIT_I64 = 5, |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 585 | IIT_F16 = 6, |
| 586 | IIT_F32 = 7, |
| 587 | IIT_F64 = 8, |
| 588 | IIT_V2 = 9, |
| 589 | IIT_V4 = 10, |
| 590 | IIT_V8 = 11, |
| 591 | IIT_V16 = 12, |
| 592 | IIT_V32 = 13, |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 593 | IIT_PTR = 14, |
| 594 | IIT_ARG = 15, |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 595 | |
Robert Khasanov | 65c2756 | 2014-10-20 19:25:05 +0000 | [diff] [blame] | 596 | // Values from 16+ are only encodable with the inefficient encoding. |
| 597 | IIT_V64 = 16, |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 598 | IIT_MMX = 17, |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 599 | IIT_TOKEN = 18, |
| 600 | IIT_METADATA = 19, |
| 601 | IIT_EMPTYSTRUCT = 20, |
| 602 | IIT_STRUCT2 = 21, |
| 603 | IIT_STRUCT3 = 22, |
| 604 | IIT_STRUCT4 = 23, |
| 605 | IIT_STRUCT5 = 24, |
| 606 | IIT_EXTEND_ARG = 25, |
| 607 | IIT_TRUNC_ARG = 26, |
| 608 | IIT_ANYPTR = 27, |
| 609 | IIT_V1 = 28, |
| 610 | IIT_VARARG = 29, |
| 611 | IIT_HALF_VEC_ARG = 30, |
| 612 | IIT_SAME_VEC_WIDTH_ARG = 31, |
| 613 | IIT_PTR_TO_ARG = 32, |
| 614 | IIT_VEC_OF_PTRS_TO_ELT = 33, |
Krzysztof Parzyszek | b8bb90b | 2015-11-24 16:28:14 +0000 | [diff] [blame] | 615 | IIT_I128 = 34, |
| 616 | IIT_V512 = 35, |
| 617 | IIT_V1024 = 36 |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | |
| 621 | static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos, |
| 622 | SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) { |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 623 | IIT_Info Info = IIT_Info(Infos[NextElt++]); |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 624 | unsigned StructElts = 2; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 625 | using namespace Intrinsic; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 626 | |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 627 | switch (Info) { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 628 | case IIT_Done: |
| 629 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0)); |
| 630 | return; |
Andrew Trick | a2efd99 | 2013-10-31 17:18:11 +0000 | [diff] [blame] | 631 | case IIT_VARARG: |
| 632 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0)); |
| 633 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 634 | case IIT_MMX: |
| 635 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0)); |
| 636 | return; |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 637 | case IIT_TOKEN: |
| 638 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0)); |
| 639 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 640 | case IIT_METADATA: |
| 641 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0)); |
| 642 | return; |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 643 | case IIT_F16: |
| 644 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0)); |
| 645 | return; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 646 | case IIT_F32: |
| 647 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0)); |
| 648 | return; |
| 649 | case IIT_F64: |
| 650 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0)); |
| 651 | return; |
| 652 | case IIT_I1: |
| 653 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1)); |
| 654 | return; |
| 655 | case IIT_I8: |
| 656 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8)); |
| 657 | return; |
| 658 | case IIT_I16: |
| 659 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16)); |
| 660 | return; |
| 661 | case IIT_I32: |
| 662 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32)); |
| 663 | return; |
| 664 | case IIT_I64: |
| 665 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64)); |
| 666 | return; |
Kit Barton | 6646033 | 2015-05-25 15:49:26 +0000 | [diff] [blame] | 667 | case IIT_I128: |
| 668 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128)); |
| 669 | return; |
Jiangning Liu | 63dc840 | 2013-09-24 02:47:27 +0000 | [diff] [blame] | 670 | case IIT_V1: |
| 671 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1)); |
| 672 | DecodeIITType(NextElt, Infos, OutputTable); |
| 673 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 674 | case IIT_V2: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 675 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2)); |
| 676 | DecodeIITType(NextElt, Infos, OutputTable); |
| 677 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 678 | case IIT_V4: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 679 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4)); |
| 680 | DecodeIITType(NextElt, Infos, OutputTable); |
| 681 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 682 | case IIT_V8: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 683 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8)); |
| 684 | DecodeIITType(NextElt, Infos, OutputTable); |
| 685 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 686 | case IIT_V16: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 687 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16)); |
| 688 | DecodeIITType(NextElt, Infos, OutputTable); |
| 689 | return; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 690 | case IIT_V32: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 691 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32)); |
| 692 | DecodeIITType(NextElt, Infos, OutputTable); |
| 693 | return; |
Robert Khasanov | b25e562 | 2014-09-30 11:32:22 +0000 | [diff] [blame] | 694 | case IIT_V64: |
| 695 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64)); |
| 696 | DecodeIITType(NextElt, Infos, OutputTable); |
| 697 | return; |
Krzysztof Parzyszek | b8bb90b | 2015-11-24 16:28:14 +0000 | [diff] [blame] | 698 | case IIT_V512: |
| 699 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512)); |
| 700 | DecodeIITType(NextElt, Infos, OutputTable); |
| 701 | return; |
| 702 | case IIT_V1024: |
| 703 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024)); |
| 704 | DecodeIITType(NextElt, Infos, OutputTable); |
| 705 | return; |
Chris Lattner | 4f18aa8 | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 706 | case IIT_PTR: |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 707 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0)); |
| 708 | DecodeIITType(NextElt, Infos, OutputTable); |
| 709 | return; |
Chris Lattner | 4f18aa8 | 2012-05-23 05:19:18 +0000 | [diff] [blame] | 710 | case IIT_ANYPTR: { // [ANYPTR addrspace, subtype] |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 711 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 712 | Infos[NextElt++])); |
| 713 | DecodeIITType(NextElt, Infos, OutputTable); |
| 714 | return; |
Pete Cooper | 243efd7 | 2012-05-21 23:21:28 +0000 | [diff] [blame] | 715 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 716 | case IIT_ARG: { |
| 717 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 718 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo)); |
| 719 | return; |
| 720 | } |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 721 | case IIT_EXTEND_ARG: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 722 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 723 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 724 | ArgInfo)); |
| 725 | return; |
| 726 | } |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 727 | case IIT_TRUNC_ARG: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 728 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 729 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 730 | ArgInfo)); |
| 731 | return; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 732 | } |
Tim Northover | 4516de3 | 2014-03-29 07:04:54 +0000 | [diff] [blame] | 733 | case IIT_HALF_VEC_ARG: { |
| 734 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 735 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument, |
| 736 | ArgInfo)); |
| 737 | return; |
| 738 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 739 | case IIT_SAME_VEC_WIDTH_ARG: { |
| 740 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 741 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, |
| 742 | ArgInfo)); |
| 743 | return; |
| 744 | } |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 745 | case IIT_PTR_TO_ARG: { |
| 746 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 747 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, |
| 748 | ArgInfo)); |
| 749 | return; |
| 750 | } |
Elena Demikhovsky | 23a485a | 2015-02-08 08:27:19 +0000 | [diff] [blame] | 751 | case IIT_VEC_OF_PTRS_TO_ELT: { |
| 752 | unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); |
| 753 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt, |
| 754 | ArgInfo)); |
| 755 | return; |
| 756 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 757 | case IIT_EMPTYSTRUCT: |
| 758 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); |
| 759 | return; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 760 | case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH; |
| 761 | case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH; |
| 762 | case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH; |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 763 | case IIT_STRUCT2: { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 764 | OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts)); |
| 765 | |
Chris Lattner | 3e34a7b | 2012-05-17 05:03:24 +0000 | [diff] [blame] | 766 | for (unsigned i = 0; i != StructElts; ++i) |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 767 | DecodeIITType(NextElt, Infos, OutputTable); |
| 768 | return; |
Chris Lattner | 827b253 | 2012-05-17 04:30:58 +0000 | [diff] [blame] | 769 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 770 | } |
| 771 | llvm_unreachable("unhandled"); |
| 772 | } |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 773 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 774 | |
| 775 | #define GET_INTRINSIC_GENERATOR_GLOBAL |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 776 | #include "llvm/IR/Intrinsics.gen" |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 777 | #undef GET_INTRINSIC_GENERATOR_GLOBAL |
| 778 | |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 779 | void Intrinsic::getIntrinsicInfoTableEntries(ID id, |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 780 | SmallVectorImpl<IITDescriptor> &T){ |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 781 | // Check to see if the intrinsic's type was expressible by the table. |
| 782 | unsigned TableVal = IIT_Table[id-1]; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 783 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 784 | // Decode the TableVal into an array of IITValues. |
| 785 | SmallVector<unsigned char, 8> IITValues; |
| 786 | ArrayRef<unsigned char> IITEntries; |
| 787 | unsigned NextElt = 0; |
| 788 | if ((TableVal >> 31) != 0) { |
| 789 | // This is an offset into the IIT_LongEncodingTable. |
| 790 | IITEntries = IIT_LongEncodingTable; |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 791 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 792 | // Strip sentinel bit. |
| 793 | NextElt = (TableVal << 1) >> 1; |
| 794 | } else { |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 795 | // Decode the TableVal into an array of IITValues. If the entry was encoded |
| 796 | // into a single word in the table itself, decode it now. |
Chris Lattner | a57c797 | 2012-05-17 05:13:57 +0000 | [diff] [blame] | 797 | do { |
| 798 | IITValues.push_back(TableVal & 0xF); |
| 799 | TableVal >>= 4; |
| 800 | } while (TableVal); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 801 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 802 | IITEntries = IITValues; |
| 803 | NextElt = 0; |
Chris Lattner | 7f0e7ba | 2012-05-16 06:34:44 +0000 | [diff] [blame] | 804 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 805 | |
| 806 | // Okay, decode the table into the output vector of IITDescriptors. |
| 807 | DecodeIITType(NextElt, IITEntries, T); |
| 808 | while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0) |
| 809 | DecodeIITType(NextElt, IITEntries, T); |
| 810 | } |
| 811 | |
| 812 | |
| 813 | static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 814 | ArrayRef<Type*> Tys, LLVMContext &Context) { |
| 815 | using namespace Intrinsic; |
| 816 | IITDescriptor D = Infos.front(); |
| 817 | Infos = Infos.slice(1); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 818 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 819 | switch (D.Kind) { |
| 820 | case IITDescriptor::Void: return Type::getVoidTy(Context); |
Andrew Trick | a2efd99 | 2013-10-31 17:18:11 +0000 | [diff] [blame] | 821 | case IITDescriptor::VarArg: return Type::getVoidTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 822 | case IITDescriptor::MMX: return Type::getX86_MMXTy(Context); |
Joseph Tremoulet | 917c738 | 2015-09-02 13:36:25 +0000 | [diff] [blame] | 823 | case IITDescriptor::Token: return Type::getTokenTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 824 | case IITDescriptor::Metadata: return Type::getMetadataTy(Context); |
Michael Ilseman | 6c6d715 | 2013-01-11 01:45:05 +0000 | [diff] [blame] | 825 | case IITDescriptor::Half: return Type::getHalfTy(Context); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 826 | case IITDescriptor::Float: return Type::getFloatTy(Context); |
| 827 | case IITDescriptor::Double: return Type::getDoubleTy(Context); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 828 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 829 | case IITDescriptor::Integer: |
| 830 | return IntegerType::get(Context, D.Integer_Width); |
| 831 | case IITDescriptor::Vector: |
| 832 | return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width); |
| 833 | case IITDescriptor::Pointer: |
| 834 | return PointerType::get(DecodeFixedType(Infos, Tys, Context), |
| 835 | D.Pointer_AddressSpace); |
| 836 | case IITDescriptor::Struct: { |
| 837 | Type *Elts[5]; |
| 838 | assert(D.Struct_NumElements <= 5 && "Can't handle this yet"); |
| 839 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 840 | Elts[i] = DecodeFixedType(Infos, Tys, Context); |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 841 | return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements)); |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | case IITDescriptor::Argument: |
| 845 | return Tys[D.getArgumentNumber()]; |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 846 | case IITDescriptor::ExtendArgument: { |
| 847 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 848 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 849 | return VectorType::getExtendedElementVectorType(VTy); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 850 | |
Tim Northover | aa3cf1e | 2014-03-28 12:31:39 +0000 | [diff] [blame] | 851 | return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth()); |
| 852 | } |
| 853 | case IITDescriptor::TruncArgument: { |
| 854 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 855 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 856 | return VectorType::getTruncatedElementVectorType(VTy); |
| 857 | |
| 858 | IntegerType *ITy = cast<IntegerType>(Ty); |
| 859 | assert(ITy->getBitWidth() % 2 == 0); |
| 860 | return IntegerType::get(Context, ITy->getBitWidth() / 2); |
| 861 | } |
Tim Northover | 4516de3 | 2014-03-29 07:04:54 +0000 | [diff] [blame] | 862 | case IITDescriptor::HalfVecArgument: |
| 863 | return VectorType::getHalfElementsVectorType(cast<VectorType>( |
| 864 | Tys[D.getArgumentNumber()])); |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 865 | case IITDescriptor::SameVecWidthArgument: { |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 866 | Type *EltTy = DecodeFixedType(Infos, Tys, Context); |
| 867 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 868 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 869 | return VectorType::get(EltTy, VTy->getNumElements()); |
| 870 | } |
| 871 | llvm_unreachable("unhandled"); |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 872 | } |
| 873 | case IITDescriptor::PtrToArgument: { |
| 874 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 875 | return PointerType::getUnqual(Ty); |
| 876 | } |
Elena Demikhovsky | 23a485a | 2015-02-08 08:27:19 +0000 | [diff] [blame] | 877 | case IITDescriptor::VecOfPtrsToElt: { |
| 878 | Type *Ty = Tys[D.getArgumentNumber()]; |
| 879 | VectorType *VTy = dyn_cast<VectorType>(Ty); |
| 880 | if (!VTy) |
| 881 | llvm_unreachable("Expected an argument of Vector Type"); |
| 882 | Type *EltTy = VTy->getVectorElementType(); |
| 883 | return VectorType::get(PointerType::getUnqual(EltTy), |
| 884 | VTy->getNumElements()); |
| 885 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 886 | } |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 887 | llvm_unreachable("unhandled"); |
| 888 | } |
| 889 | |
| 890 | |
| 891 | |
| 892 | FunctionType *Intrinsic::getType(LLVMContext &Context, |
| 893 | ID id, ArrayRef<Type*> Tys) { |
| 894 | SmallVector<IITDescriptor, 8> Table; |
| 895 | getIntrinsicInfoTableEntries(id, Table); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 896 | |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 897 | ArrayRef<IITDescriptor> TableRef = Table; |
| 898 | Type *ResultTy = DecodeFixedType(TableRef, Tys, Context); |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 899 | |
Chris Lattner | a3b0f52 | 2012-05-17 15:55:41 +0000 | [diff] [blame] | 900 | SmallVector<Type*, 8> ArgTys; |
Chris Lattner | f39c278 | 2012-05-27 18:28:35 +0000 | [diff] [blame] | 901 | while (!TableRef.empty()) |
| 902 | ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context)); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 903 | |
Steven Wu | d05affc | 2014-10-20 15:47:24 +0000 | [diff] [blame] | 904 | // DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg |
| 905 | // If we see void type as the type of the last argument, it is vararg intrinsic |
| 906 | if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) { |
| 907 | ArgTys.pop_back(); |
| 908 | return FunctionType::get(ResultTy, ArgTys, true); |
| 909 | } |
Michael Ilseman | acdb76d | 2012-12-17 20:37:55 +0000 | [diff] [blame] | 910 | return FunctionType::get(ResultTy, ArgTys, false); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 913 | bool Intrinsic::isOverloaded(ID id) { |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 914 | #define GET_INTRINSIC_OVERLOAD_TABLE |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 915 | #include "llvm/IR/Intrinsics.gen" |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 916 | #undef GET_INTRINSIC_OVERLOAD_TABLE |
Mon P Wang | b402493 | 2009-02-24 23:17:49 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Sanjoy Das | c65d43e | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 919 | bool Intrinsic::isLeaf(ID id) { |
| 920 | switch (id) { |
| 921 | default: |
| 922 | return true; |
| 923 | |
| 924 | case Intrinsic::experimental_gc_statepoint: |
| 925 | case Intrinsic::experimental_patchpoint_void: |
| 926 | case Intrinsic::experimental_patchpoint_i64: |
| 927 | return false; |
| 928 | } |
| 929 | } |
| 930 | |
Chris Lattner | 49b7ee1 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 931 | /// This defines the "Intrinsic::getAttributes(ID id)" method. |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 932 | #define GET_INTRINSIC_ATTRIBUTES |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 933 | #include "llvm/IR/Intrinsics.gen" |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 934 | #undef GET_INTRINSIC_ATTRIBUTES |
| 935 | |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 936 | Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) { |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 937 | // There can never be multiple globals with the same name of different types, |
| 938 | // because intrinsics must be a specific type. |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 939 | return |
Benjamin Kramer | e6e1933 | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 940 | cast<Function>(M->getOrInsertFunction(getName(id, Tys), |
| 941 | getType(M->getContext(), id, Tys))); |
Jim Laskey | 2682ea6 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 944 | // This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method. |
| 945 | #define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 946 | #include "llvm/IR/Intrinsics.gen" |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 947 | #undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN |
| 948 | |
Saleem Abdulrasool | 4e63fc4 | 2014-07-04 18:42:25 +0000 | [diff] [blame] | 949 | // This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method. |
| 950 | #define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN |
| 951 | #include "llvm/IR/Intrinsics.gen" |
| 952 | #undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN |
| 953 | |
Artur Pilipenko | bc55227 | 2016-06-22 14:56:33 +0000 | [diff] [blame] | 954 | bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 955 | SmallVectorImpl<Type*> &ArgTys) { |
| 956 | using namespace Intrinsic; |
| 957 | |
| 958 | // If we ran out of descriptors, there are too many arguments. |
| 959 | if (Infos.empty()) return true; |
| 960 | IITDescriptor D = Infos.front(); |
| 961 | Infos = Infos.slice(1); |
| 962 | |
| 963 | switch (D.Kind) { |
| 964 | case IITDescriptor::Void: return !Ty->isVoidTy(); |
| 965 | case IITDescriptor::VarArg: return true; |
| 966 | case IITDescriptor::MMX: return !Ty->isX86_MMXTy(); |
| 967 | case IITDescriptor::Token: return !Ty->isTokenTy(); |
| 968 | case IITDescriptor::Metadata: return !Ty->isMetadataTy(); |
| 969 | case IITDescriptor::Half: return !Ty->isHalfTy(); |
| 970 | case IITDescriptor::Float: return !Ty->isFloatTy(); |
| 971 | case IITDescriptor::Double: return !Ty->isDoubleTy(); |
| 972 | case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width); |
| 973 | case IITDescriptor::Vector: { |
| 974 | VectorType *VT = dyn_cast<VectorType>(Ty); |
| 975 | return !VT || VT->getNumElements() != D.Vector_Width || |
| 976 | matchIntrinsicType(VT->getElementType(), Infos, ArgTys); |
| 977 | } |
| 978 | case IITDescriptor::Pointer: { |
| 979 | PointerType *PT = dyn_cast<PointerType>(Ty); |
| 980 | return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace || |
| 981 | matchIntrinsicType(PT->getElementType(), Infos, ArgTys); |
| 982 | } |
| 983 | |
| 984 | case IITDescriptor::Struct: { |
| 985 | StructType *ST = dyn_cast<StructType>(Ty); |
| 986 | if (!ST || ST->getNumElements() != D.Struct_NumElements) |
| 987 | return true; |
| 988 | |
| 989 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 990 | if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys)) |
| 991 | return true; |
| 992 | return false; |
| 993 | } |
| 994 | |
| 995 | case IITDescriptor::Argument: |
| 996 | // Two cases here - If this is the second occurrence of an argument, verify |
| 997 | // that the later instance matches the previous instance. |
| 998 | if (D.getArgumentNumber() < ArgTys.size()) |
| 999 | return Ty != ArgTys[D.getArgumentNumber()]; |
| 1000 | |
| 1001 | // Otherwise, if this is the first instance of an argument, record it and |
| 1002 | // verify the "Any" kind. |
| 1003 | assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error"); |
| 1004 | ArgTys.push_back(Ty); |
| 1005 | |
| 1006 | switch (D.getArgumentKind()) { |
| 1007 | case IITDescriptor::AK_Any: return false; // Success |
| 1008 | case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); |
| 1009 | case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); |
| 1010 | case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty); |
| 1011 | case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty); |
| 1012 | } |
| 1013 | llvm_unreachable("all argument kinds not covered"); |
| 1014 | |
| 1015 | case IITDescriptor::ExtendArgument: { |
| 1016 | // This may only be used when referring to a previous vector argument. |
| 1017 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1018 | return true; |
| 1019 | |
| 1020 | Type *NewTy = ArgTys[D.getArgumentNumber()]; |
| 1021 | if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) |
| 1022 | NewTy = VectorType::getExtendedElementVectorType(VTy); |
| 1023 | else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) |
| 1024 | NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth()); |
| 1025 | else |
| 1026 | return true; |
| 1027 | |
| 1028 | return Ty != NewTy; |
| 1029 | } |
| 1030 | case IITDescriptor::TruncArgument: { |
| 1031 | // This may only be used when referring to a previous vector argument. |
| 1032 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1033 | return true; |
| 1034 | |
| 1035 | Type *NewTy = ArgTys[D.getArgumentNumber()]; |
| 1036 | if (VectorType *VTy = dyn_cast<VectorType>(NewTy)) |
| 1037 | NewTy = VectorType::getTruncatedElementVectorType(VTy); |
| 1038 | else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy)) |
| 1039 | NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2); |
| 1040 | else |
| 1041 | return true; |
| 1042 | |
| 1043 | return Ty != NewTy; |
| 1044 | } |
| 1045 | case IITDescriptor::HalfVecArgument: |
| 1046 | // This may only be used when referring to a previous vector argument. |
| 1047 | return D.getArgumentNumber() >= ArgTys.size() || |
| 1048 | !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || |
| 1049 | VectorType::getHalfElementsVectorType( |
| 1050 | cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; |
| 1051 | case IITDescriptor::SameVecWidthArgument: { |
| 1052 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1053 | return true; |
| 1054 | VectorType * ReferenceType = |
| 1055 | dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]); |
| 1056 | VectorType *ThisArgType = dyn_cast<VectorType>(Ty); |
| 1057 | if (!ThisArgType || !ReferenceType || |
| 1058 | (ReferenceType->getVectorNumElements() != |
| 1059 | ThisArgType->getVectorNumElements())) |
| 1060 | return true; |
| 1061 | return matchIntrinsicType(ThisArgType->getVectorElementType(), |
| 1062 | Infos, ArgTys); |
| 1063 | } |
| 1064 | case IITDescriptor::PtrToArgument: { |
| 1065 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1066 | return true; |
| 1067 | Type * ReferenceType = ArgTys[D.getArgumentNumber()]; |
| 1068 | PointerType *ThisArgType = dyn_cast<PointerType>(Ty); |
| 1069 | return (!ThisArgType || ThisArgType->getElementType() != ReferenceType); |
| 1070 | } |
| 1071 | case IITDescriptor::VecOfPtrsToElt: { |
| 1072 | if (D.getArgumentNumber() >= ArgTys.size()) |
| 1073 | return true; |
| 1074 | VectorType * ReferenceType = |
| 1075 | dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]); |
| 1076 | VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty); |
| 1077 | if (!ThisArgVecTy || !ReferenceType || |
| 1078 | (ReferenceType->getVectorNumElements() != |
| 1079 | ThisArgVecTy->getVectorNumElements())) |
| 1080 | return true; |
| 1081 | PointerType *ThisArgEltTy = |
| 1082 | dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType()); |
| 1083 | if (!ThisArgEltTy) |
| 1084 | return true; |
| 1085 | return ThisArgEltTy->getElementType() != |
| 1086 | ReferenceType->getVectorElementType(); |
| 1087 | } |
| 1088 | } |
| 1089 | llvm_unreachable("unhandled"); |
| 1090 | } |
| 1091 | |
Artur Pilipenko | b68b821 | 2016-06-24 14:47:27 +0000 | [diff] [blame] | 1092 | bool |
| 1093 | Intrinsic::matchIntrinsicVarArg(bool isVarArg, |
| 1094 | ArrayRef<Intrinsic::IITDescriptor> &Infos) { |
| 1095 | // If there are no descriptors left, then it can't be a vararg. |
| 1096 | if (Infos.empty()) |
| 1097 | return isVarArg; |
| 1098 | |
| 1099 | // There should be only one descriptor remaining at this point. |
| 1100 | if (Infos.size() != 1) |
| 1101 | return true; |
| 1102 | |
| 1103 | // Check and verify the descriptor. |
| 1104 | IITDescriptor D = Infos.front(); |
| 1105 | Infos = Infos.slice(1); |
| 1106 | if (D.Kind == IITDescriptor::VarArg) |
| 1107 | return !isVarArg; |
| 1108 | |
| 1109 | return true; |
| 1110 | } |
| 1111 | |
Artur Pilipenko | 6c7a8ab | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 1112 | Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) { |
| 1113 | Intrinsic::ID ID = F->getIntrinsicID(); |
| 1114 | if (!ID) |
| 1115 | return None; |
| 1116 | |
| 1117 | FunctionType *FTy = F->getFunctionType(); |
| 1118 | // Accumulate an array of overloaded types for the given intrinsic |
| 1119 | SmallVector<Type *, 4> ArgTys; |
| 1120 | { |
| 1121 | SmallVector<Intrinsic::IITDescriptor, 8> Table; |
| 1122 | getIntrinsicInfoTableEntries(ID, Table); |
| 1123 | ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; |
| 1124 | |
| 1125 | // If we encounter any problems matching the signature with the descriptor |
| 1126 | // just give up remangling. It's up to verifier to report the discrepancy. |
| 1127 | if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys)) |
| 1128 | return None; |
| 1129 | for (auto Ty : FTy->params()) |
| 1130 | if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys)) |
| 1131 | return None; |
| 1132 | if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef)) |
| 1133 | return None; |
| 1134 | } |
| 1135 | |
| 1136 | StringRef Name = F->getName(); |
| 1137 | if (Name == Intrinsic::getName(ID, ArgTys)) |
| 1138 | return None; |
| 1139 | |
| 1140 | auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys); |
| 1141 | NewDecl->setCallingConv(F->getCallingConv()); |
| 1142 | assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature"); |
| 1143 | return NewDecl; |
| 1144 | } |
| 1145 | |
Gabor Greif | 161cb04 | 2010-03-23 14:40:20 +0000 | [diff] [blame] | 1146 | /// hasAddressTaken - returns true if there are any uses of this function |
| 1147 | /// other than direct calls or invokes to it. |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1148 | bool Function::hasAddressTaken(const User* *PutOffender) const { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1149 | for (const Use &U : uses()) { |
| 1150 | const User *FU = U.getUser(); |
| 1151 | if (isa<BlockAddress>(FU)) |
Jay Foad | ca0c499 | 2012-05-12 08:30:16 +0000 | [diff] [blame] | 1152 | continue; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1153 | if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) { |
| 1154 | if (PutOffender) |
| 1155 | *PutOffender = FU; |
| 1156 | return true; |
| 1157 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1158 | ImmutableCallSite CS(cast<Instruction>(FU)); |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 1159 | if (!CS.isCallee(&U)) { |
| 1160 | if (PutOffender) |
| 1161 | *PutOffender = FU; |
| 1162 | return true; |
| 1163 | } |
Jay Foad | 557169d | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 1164 | } |
| 1165 | return false; |
| 1166 | } |
| 1167 | |
Eli Friedman | 1923a33 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 1168 | bool Function::isDefTriviallyDead() const { |
| 1169 | // Check the linkage |
| 1170 | if (!hasLinkOnceLinkage() && !hasLocalLinkage() && |
| 1171 | !hasAvailableExternallyLinkage()) |
| 1172 | return false; |
| 1173 | |
| 1174 | // Check if the function is used by anything other than a blockaddress. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1175 | for (const User *U : users()) |
| 1176 | if (!isa<BlockAddress>(U)) |
Eli Friedman | 1923a33 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 1177 | return false; |
| 1178 | |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Bill Wendling | 63a4ea1 | 2011-10-17 18:43:40 +0000 | [diff] [blame] | 1182 | /// callsFunctionThatReturnsTwice - Return true if the function has a call to |
| 1183 | /// setjmp or other function that gcc recognizes as "returning twice". |
| 1184 | bool Function::callsFunctionThatReturnsTwice() const { |
| 1185 | for (const_inst_iterator |
| 1186 | I = inst_begin(this), E = inst_end(this); I != E; ++I) { |
Mark Seaborn | 8271118 | 2014-01-14 04:20:01 +0000 | [diff] [blame] | 1187 | ImmutableCallSite CS(&*I); |
| 1188 | if (CS && CS.hasFnAttr(Attribute::ReturnsTwice)) |
Bill Wendling | 63a4ea1 | 2011-10-17 18:43:40 +0000 | [diff] [blame] | 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | return false; |
| 1193 | } |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1194 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1195 | Constant *Function::getPersonalityFn() const { |
| 1196 | assert(hasPersonalityFn() && getNumOperands()); |
| 1197 | return cast<Constant>(Op<0>()); |
Vedant Kumar | 1ab5ea5 | 2015-10-06 20:31:57 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1200 | void Function::setPersonalityFn(Constant *Fn) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1201 | setHungoffOperand<0>(Fn); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1202 | setValueSubclassDataBit(3, Fn != nullptr); |
Vedant Kumar | 1ab5ea5 | 2015-10-06 20:31:57 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1205 | Constant *Function::getPrefixData() const { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1206 | assert(hasPrefixData() && getNumOperands()); |
| 1207 | return cast<Constant>(Op<1>()); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | void Function::setPrefixData(Constant *PrefixData) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1211 | setHungoffOperand<1>(PrefixData); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1212 | setValueSubclassDataBit(1, PrefixData != nullptr); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1213 | } |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1214 | |
| 1215 | Constant *Function::getPrologueData() const { |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1216 | assert(hasPrologueData() && getNumOperands()); |
| 1217 | return cast<Constant>(Op<2>()); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | void Function::setPrologueData(Constant *PrologueData) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1221 | setHungoffOperand<2>(PrologueData); |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1222 | setValueSubclassDataBit(2, PrologueData != nullptr); |
| 1223 | } |
| 1224 | |
| 1225 | void Function::allocHungoffUselist() { |
| 1226 | // If we've already allocated a uselist, stop here. |
| 1227 | if (getNumOperands()) |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1230 | allocHungoffUses(3, /*IsPhi=*/ false); |
| 1231 | setNumHungOffUseOperands(3); |
| 1232 | |
| 1233 | // Initialize the uselist with placeholder operands to allow traversal. |
| 1234 | auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)); |
| 1235 | Op<0>().set(CPN); |
| 1236 | Op<1>().set(CPN); |
| 1237 | Op<2>().set(CPN); |
| 1238 | } |
| 1239 | |
| 1240 | template <int Idx> |
| 1241 | void Function::setHungoffOperand(Constant *C) { |
Keno Fischer | 9bc46b1 | 2015-12-23 18:27:23 +0000 | [diff] [blame] | 1242 | if (C) { |
| 1243 | allocHungoffUselist(); |
| 1244 | Op<Idx>().set(C); |
| 1245 | } else if (getNumOperands()) { |
| 1246 | Op<Idx>().set( |
| 1247 | ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0))); |
| 1248 | } |
Vedant Kumar | 3a63fb3 | 2015-12-19 08:52:49 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | void Function::setValueSubclassDataBit(unsigned Bit, bool On) { |
| 1252 | assert(Bit < 16 && "SubclassData contains only 16 bits"); |
| 1253 | if (On) |
| 1254 | setValueSubclassData(getSubclassDataFromValue() | (1 << Bit)); |
| 1255 | else |
| 1256 | setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit)); |
Peter Collingbourne | 51d2de7 | 2014-12-03 02:08:38 +0000 | [diff] [blame] | 1257 | } |
Akira Hatanaka | 3058d0f | 2015-05-06 23:54:14 +0000 | [diff] [blame] | 1258 | |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 1259 | void Function::setEntryCount(uint64_t Count) { |
| 1260 | MDBuilder MDB(getContext()); |
| 1261 | setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count)); |
| 1262 | } |
| 1263 | |
| 1264 | Optional<uint64_t> Function::getEntryCount() const { |
| 1265 | MDNode *MD = getMetadata(LLVMContext::MD_prof); |
| 1266 | if (MD && MD->getOperand(0)) |
| 1267 | if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0))) |
| 1268 | if (MDS->getString().equals("function_entry_count")) { |
| 1269 | ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1)); |
David Callahan | c1c810d | 2016-08-05 18:38:19 +0000 | [diff] [blame] | 1270 | uint64_t Count = CI->getValue().getZExtValue(); |
| 1271 | if (Count == 0) |
| 1272 | return None; |
| 1273 | return Count; |
Diego Novillo | 2567f3d | 2015-05-13 15:13:45 +0000 | [diff] [blame] | 1274 | } |
| 1275 | return None; |
| 1276 | } |