Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1 | //===- Attributes.cpp - Implement AttributesList --------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 10 | // \file |
| 11 | // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 12 | // AttributeListImpl, and AttributeList classes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 16 | #include "AttributeImpl.h" |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 17 | #include "LLVMContextImpl.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/ADT/FoldingSet.h" |
| 20 | #include "llvm/ADT/Optional.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
Benjamin Kramer | 502b9e1 | 2014-04-12 16:15:53 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringRef.h" |
| 25 | #include "llvm/ADT/Twine.h" |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 26 | #include "llvm/IR/AttributeSetNode.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Attributes.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Type.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compiler.h" |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1a25d73 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 37 | #include <cassert> |
| 38 | #include <cstdint> |
| 39 | #include <limits> |
| 40 | #include <map> |
| 41 | #include <string> |
| 42 | #include <tuple> |
| 43 | #include <utility> |
| 44 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Bill Wendling | 7707c5a | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 48 | // Attribute Construction Methods |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 50 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 51 | // allocsize has two integer arguments, but because they're both 32 bits, we can |
| 52 | // pack them into one 64-bit value, at the cost of making said value |
| 53 | // nonsensical. |
| 54 | // |
| 55 | // In order to do this, we need to reserve one value of the second (optional) |
| 56 | // allocsize argument to signify "not present." |
George Burgess IV | 381fc0e | 2016-08-25 01:05:08 +0000 | [diff] [blame] | 57 | static const unsigned AllocSizeNumElemsNotPresent = -1; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 58 | |
| 59 | static uint64_t packAllocSizeArgs(unsigned ElemSizeArg, |
| 60 | const Optional<unsigned> &NumElemsArg) { |
| 61 | assert((!NumElemsArg.hasValue() || |
| 62 | *NumElemsArg != AllocSizeNumElemsNotPresent) && |
| 63 | "Attempting to pack a reserved value"); |
| 64 | |
| 65 | return uint64_t(ElemSizeArg) << 32 | |
| 66 | NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent); |
| 67 | } |
| 68 | |
| 69 | static std::pair<unsigned, Optional<unsigned>> |
| 70 | unpackAllocSizeArgs(uint64_t Num) { |
| 71 | unsigned NumElems = Num & std::numeric_limits<unsigned>::max(); |
| 72 | unsigned ElemSizeArg = Num >> 32; |
| 73 | |
| 74 | Optional<unsigned> NumElemsArg; |
| 75 | if (NumElems != AllocSizeNumElemsNotPresent) |
| 76 | NumElemsArg = NumElems; |
| 77 | return std::make_pair(ElemSizeArg, NumElemsArg); |
| 78 | } |
| 79 | |
Matt Arsenault | e1cef6e | 2014-09-03 23:24:31 +0000 | [diff] [blame] | 80 | Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, |
| 81 | uint64_t Val) { |
| 82 | LLVMContextImpl *pImpl = Context.pImpl; |
| 83 | FoldingSetNodeID ID; |
| 84 | ID.AddInteger(Kind); |
Matt Arsenault | fb4308e | 2014-09-03 23:38:05 +0000 | [diff] [blame] | 85 | if (Val) ID.AddInteger(Val); |
Matt Arsenault | e1cef6e | 2014-09-03 23:24:31 +0000 | [diff] [blame] | 86 | |
| 87 | void *InsertPoint; |
| 88 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 89 | |
| 90 | if (!PA) { |
| 91 | // If we didn't find any existing attributes of the same shape then create a |
| 92 | // new one and insert it. |
Matt Arsenault | fb4308e | 2014-09-03 23:38:05 +0000 | [diff] [blame] | 93 | if (!Val) |
| 94 | PA = new EnumAttributeImpl(Kind); |
| 95 | else |
| 96 | PA = new IntAttributeImpl(Kind, Val); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 97 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 98 | } |
| 99 | |
| 100 | // Return the Attribute that we found or created. |
| 101 | return Attribute(PA); |
| 102 | } |
| 103 | |
| 104 | Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { |
| 105 | LLVMContextImpl *pImpl = Context.pImpl; |
| 106 | FoldingSetNodeID ID; |
| 107 | ID.AddString(Kind); |
| 108 | if (!Val.empty()) ID.AddString(Val); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 109 | |
| 110 | void *InsertPoint; |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 111 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 112 | |
| 113 | if (!PA) { |
| 114 | // If we didn't find any existing attributes of the same shape then create a |
| 115 | // new one and insert it. |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 116 | PA = new StringAttributeImpl(Kind, Val); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 117 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 118 | } |
| 119 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 120 | // Return the Attribute that we found or created. |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 121 | return Attribute(PA); |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 124 | Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) { |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 125 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 126 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 127 | return get(Context, Alignment, Align); |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | Attribute Attribute::getWithStackAlignment(LLVMContext &Context, |
| 131 | uint64_t Align) { |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 132 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 133 | assert(Align <= 0x100 && "Alignment too large."); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 134 | return get(Context, StackAlignment, Align); |
Bill Wendling | 4bbe9db | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 137 | Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context, |
| 138 | uint64_t Bytes) { |
| 139 | assert(Bytes && "Bytes must be non-zero."); |
| 140 | return get(Context, Dereferenceable, Bytes); |
| 141 | } |
| 142 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 143 | Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context, |
| 144 | uint64_t Bytes) { |
| 145 | assert(Bytes && "Bytes must be non-zero."); |
| 146 | return get(Context, DereferenceableOrNull, Bytes); |
| 147 | } |
| 148 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 149 | Attribute |
| 150 | Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, |
| 151 | const Optional<unsigned> &NumElemsArg) { |
| 152 | assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) && |
| 153 | "Invalid allocsize arguments -- given allocsize(0, 0)"); |
| 154 | return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg)); |
| 155 | } |
| 156 | |
Bill Wendling | 7707c5a | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 157 | //===----------------------------------------------------------------------===// |
| 158 | // Attribute Accessor Methods |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 161 | bool Attribute::isEnumAttribute() const { |
| 162 | return pImpl && pImpl->isEnumAttribute(); |
| 163 | } |
| 164 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 165 | bool Attribute::isIntAttribute() const { |
| 166 | return pImpl && pImpl->isIntAttribute(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool Attribute::isStringAttribute() const { |
| 170 | return pImpl && pImpl->isStringAttribute(); |
| 171 | } |
| 172 | |
| 173 | Attribute::AttrKind Attribute::getKindAsEnum() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 174 | if (!pImpl) return None; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 175 | assert((isEnumAttribute() || isIntAttribute()) && |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 176 | "Invalid attribute type to get the kind as an enum!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 177 | return pImpl->getKindAsEnum(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | uint64_t Attribute::getValueAsInt() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 181 | if (!pImpl) return 0; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 182 | assert(isIntAttribute() && |
| 183 | "Expected the attribute to be an integer attribute!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 184 | return pImpl->getValueAsInt(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | StringRef Attribute::getKindAsString() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 188 | if (!pImpl) return StringRef(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 189 | assert(isStringAttribute() && |
| 190 | "Invalid attribute type to get the kind as a string!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 191 | return pImpl->getKindAsString(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | StringRef Attribute::getValueAsString() const { |
Bill Wendling | 440e9d8 | 2013-07-25 00:34:29 +0000 | [diff] [blame] | 195 | if (!pImpl) return StringRef(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 196 | assert(isStringAttribute() && |
| 197 | "Invalid attribute type to get the value as a string!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 198 | return pImpl->getValueAsString(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Bill Wendling | ae89a0f | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 201 | bool Attribute::hasAttribute(AttrKind Kind) const { |
| 202 | return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); |
| 203 | } |
| 204 | |
| 205 | bool Attribute::hasAttribute(StringRef Kind) const { |
| 206 | if (!isStringAttribute()) return false; |
| 207 | return pImpl && pImpl->hasAttribute(Kind); |
Bill Wendling | 03eefb3 | 2013-01-29 20:45:34 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 210 | unsigned Attribute::getAlignment() const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 211 | assert(hasAttribute(Attribute::Alignment) && |
| 212 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 213 | return pImpl->getValueAsInt(); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 216 | unsigned Attribute::getStackAlignment() const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 217 | assert(hasAttribute(Attribute::StackAlignment) && |
| 218 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 219 | return pImpl->getValueAsInt(); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 222 | uint64_t Attribute::getDereferenceableBytes() const { |
| 223 | assert(hasAttribute(Attribute::Dereferenceable) && |
| 224 | "Trying to get dereferenceable bytes from " |
| 225 | "non-dereferenceable attribute!"); |
| 226 | return pImpl->getValueAsInt(); |
| 227 | } |
| 228 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 229 | uint64_t Attribute::getDereferenceableOrNullBytes() const { |
| 230 | assert(hasAttribute(Attribute::DereferenceableOrNull) && |
| 231 | "Trying to get dereferenceable bytes from " |
| 232 | "non-dereferenceable attribute!"); |
| 233 | return pImpl->getValueAsInt(); |
| 234 | } |
| 235 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 236 | std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const { |
| 237 | assert(hasAttribute(Attribute::AllocSize) && |
| 238 | "Trying to get allocsize args from non-allocsize attribute"); |
| 239 | return unpackAllocSizeArgs(pImpl->getValueAsInt()); |
| 240 | } |
| 241 | |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 242 | std::string Attribute::getAsString(bool InAttrGrp) const { |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 243 | if (!pImpl) return ""; |
| 244 | |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 245 | if (hasAttribute(Attribute::SanitizeAddress)) |
| 246 | return "sanitize_address"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 247 | if (hasAttribute(Attribute::AlwaysInline)) |
| 248 | return "alwaysinline"; |
Igor Laevsky | 39d662f | 2015-07-11 10:30:36 +0000 | [diff] [blame] | 249 | if (hasAttribute(Attribute::ArgMemOnly)) |
| 250 | return "argmemonly"; |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 251 | if (hasAttribute(Attribute::Builtin)) |
| 252 | return "builtin"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 253 | if (hasAttribute(Attribute::ByVal)) |
| 254 | return "byval"; |
Owen Anderson | 85fa7d5 | 2015-05-26 23:48:40 +0000 | [diff] [blame] | 255 | if (hasAttribute(Attribute::Convergent)) |
| 256 | return "convergent"; |
Manman Ren | 9bfd0d0 | 2016-04-01 21:41:15 +0000 | [diff] [blame] | 257 | if (hasAttribute(Attribute::SwiftError)) |
| 258 | return "swifterror"; |
Manman Ren | f46262e | 2016-03-29 17:37:21 +0000 | [diff] [blame] | 259 | if (hasAttribute(Attribute::SwiftSelf)) |
| 260 | return "swiftself"; |
Vaivaswatha Nagaraj | fb3f490 | 2015-12-16 16:16:19 +0000 | [diff] [blame] | 261 | if (hasAttribute(Attribute::InaccessibleMemOnly)) |
| 262 | return "inaccessiblememonly"; |
| 263 | if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly)) |
| 264 | return "inaccessiblemem_or_argmemonly"; |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 265 | if (hasAttribute(Attribute::InAlloca)) |
| 266 | return "inalloca"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 267 | if (hasAttribute(Attribute::InlineHint)) |
| 268 | return "inlinehint"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 269 | if (hasAttribute(Attribute::InReg)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 270 | return "inreg"; |
Tom Roeder | 44cb65f | 2014-06-05 19:29:43 +0000 | [diff] [blame] | 271 | if (hasAttribute(Attribute::JumpTable)) |
| 272 | return "jumptable"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 273 | if (hasAttribute(Attribute::MinSize)) |
| 274 | return "minsize"; |
| 275 | if (hasAttribute(Attribute::Naked)) |
| 276 | return "naked"; |
| 277 | if (hasAttribute(Attribute::Nest)) |
| 278 | return "nest"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 279 | if (hasAttribute(Attribute::NoAlias)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 280 | return "noalias"; |
Bill Wendling | 09bd1f7 | 2013-02-22 00:12:35 +0000 | [diff] [blame] | 281 | if (hasAttribute(Attribute::NoBuiltin)) |
| 282 | return "nobuiltin"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 283 | if (hasAttribute(Attribute::NoCapture)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 284 | return "nocapture"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 285 | if (hasAttribute(Attribute::NoDuplicate)) |
| 286 | return "noduplicate"; |
| 287 | if (hasAttribute(Attribute::NoImplicitFloat)) |
| 288 | return "noimplicitfloat"; |
| 289 | if (hasAttribute(Attribute::NoInline)) |
| 290 | return "noinline"; |
| 291 | if (hasAttribute(Attribute::NonLazyBind)) |
| 292 | return "nonlazybind"; |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 293 | if (hasAttribute(Attribute::NonNull)) |
| 294 | return "nonnull"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 295 | if (hasAttribute(Attribute::NoRedZone)) |
| 296 | return "noredzone"; |
| 297 | if (hasAttribute(Attribute::NoReturn)) |
| 298 | return "noreturn"; |
James Molloy | e6f87ca | 2015-11-06 10:32:53 +0000 | [diff] [blame] | 299 | if (hasAttribute(Attribute::NoRecurse)) |
| 300 | return "norecurse"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 301 | if (hasAttribute(Attribute::NoUnwind)) |
| 302 | return "nounwind"; |
Andrea Di Biagio | 377496b | 2013-08-23 11:53:55 +0000 | [diff] [blame] | 303 | if (hasAttribute(Attribute::OptimizeNone)) |
| 304 | return "optnone"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 305 | if (hasAttribute(Attribute::OptimizeForSize)) |
| 306 | return "optsize"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 307 | if (hasAttribute(Attribute::ReadNone)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 308 | return "readnone"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 309 | if (hasAttribute(Attribute::ReadOnly)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 310 | return "readonly"; |
Nicolai Haehnle | 84c9f99 | 2016-07-04 08:01:29 +0000 | [diff] [blame] | 311 | if (hasAttribute(Attribute::WriteOnly)) |
| 312 | return "writeonly"; |
Stephen Lin | b8bd232 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 313 | if (hasAttribute(Attribute::Returned)) |
| 314 | return "returned"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 315 | if (hasAttribute(Attribute::ReturnsTwice)) |
| 316 | return "returns_twice"; |
| 317 | if (hasAttribute(Attribute::SExt)) |
| 318 | return "signext"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 319 | if (hasAttribute(Attribute::StackProtect)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 320 | return "ssp"; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 321 | if (hasAttribute(Attribute::StackProtectReq)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 322 | return "sspreq"; |
Bill Wendling | d154e283 | 2013-01-23 06:41:41 +0000 | [diff] [blame] | 323 | if (hasAttribute(Attribute::StackProtectStrong)) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 324 | return "sspstrong"; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 325 | if (hasAttribute(Attribute::SafeStack)) |
| 326 | return "safestack"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 327 | if (hasAttribute(Attribute::StructRet)) |
| 328 | return "sret"; |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 329 | if (hasAttribute(Attribute::SanitizeThread)) |
| 330 | return "sanitize_thread"; |
| 331 | if (hasAttribute(Attribute::SanitizeMemory)) |
| 332 | return "sanitize_memory"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 333 | if (hasAttribute(Attribute::UWTable)) |
| 334 | return "uwtable"; |
| 335 | if (hasAttribute(Attribute::ZExt)) |
| 336 | return "zeroext"; |
Diego Novillo | c639953 | 2013-05-24 12:26:52 +0000 | [diff] [blame] | 337 | if (hasAttribute(Attribute::Cold)) |
| 338 | return "cold"; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 339 | |
| 340 | // FIXME: These should be output like this: |
| 341 | // |
| 342 | // align=4 |
| 343 | // alignstack=8 |
| 344 | // |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 345 | if (hasAttribute(Attribute::Alignment)) { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 346 | std::string Result; |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 347 | Result += "align"; |
| 348 | Result += (InAttrGrp) ? "=" : " "; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 349 | Result += utostr(getValueAsInt()); |
| 350 | return Result; |
| 351 | } |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 352 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 353 | auto AttrWithBytesToString = [&](const char *Name) { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 354 | std::string Result; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 355 | Result += Name; |
Bill Wendling | 829b478 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 356 | if (InAttrGrp) { |
| 357 | Result += "="; |
| 358 | Result += utostr(getValueAsInt()); |
| 359 | } else { |
| 360 | Result += "("; |
| 361 | Result += utostr(getValueAsInt()); |
| 362 | Result += ")"; |
| 363 | } |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 364 | return Result; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 365 | }; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 366 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 367 | if (hasAttribute(Attribute::StackAlignment)) |
| 368 | return AttrWithBytesToString("alignstack"); |
| 369 | |
| 370 | if (hasAttribute(Attribute::Dereferenceable)) |
| 371 | return AttrWithBytesToString("dereferenceable"); |
| 372 | |
| 373 | if (hasAttribute(Attribute::DereferenceableOrNull)) |
| 374 | return AttrWithBytesToString("dereferenceable_or_null"); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 375 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 376 | if (hasAttribute(Attribute::AllocSize)) { |
| 377 | unsigned ElemSize; |
| 378 | Optional<unsigned> NumElems; |
| 379 | std::tie(ElemSize, NumElems) = getAllocSizeArgs(); |
| 380 | |
| 381 | std::string Result = "allocsize("; |
| 382 | Result += utostr(ElemSize); |
| 383 | if (NumElems.hasValue()) { |
| 384 | Result += ','; |
| 385 | Result += utostr(*NumElems); |
| 386 | } |
| 387 | Result += ')'; |
| 388 | return Result; |
| 389 | } |
| 390 | |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 391 | // Convert target-dependent attributes to strings of the form: |
| 392 | // |
| 393 | // "kind" |
| 394 | // "kind" = "value" |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 395 | // |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 396 | if (isStringAttribute()) { |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 397 | std::string Result; |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 398 | Result += (Twine('"') + getKindAsString() + Twine('"')).str(); |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 399 | |
Honggyu Kim | 9eb6a10 | 2016-09-01 11:44:06 +0000 | [diff] [blame] | 400 | std::string AttrVal = pImpl->getValueAsString(); |
| 401 | if (AttrVal.empty()) return Result; |
Bill Wendling | 7a33f77 | 2013-02-01 22:32:30 +0000 | [diff] [blame] | 402 | |
Honggyu Kim | 9eb6a10 | 2016-09-01 11:44:06 +0000 | [diff] [blame] | 403 | // Since some attribute strings contain special characters that cannot be |
| 404 | // printable, those have to be escaped to make the attribute value printable |
| 405 | // as is. e.g. "\01__gnu_mcount_nc" |
| 406 | { |
| 407 | raw_string_ostream OS(Result); |
| 408 | OS << "=\""; |
| 409 | PrintEscapedString(AttrVal, OS); |
| 410 | OS << "\""; |
| 411 | } |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 412 | return Result; |
Bill Wendling | 9c2eba9 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 413 | } |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 414 | |
| 415 | llvm_unreachable("Unknown attribute"); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 418 | bool Attribute::operator<(Attribute A) const { |
| 419 | if (!pImpl && !A.pImpl) return false; |
| 420 | if (!pImpl) return true; |
| 421 | if (!A.pImpl) return false; |
| 422 | return *pImpl < *A.pImpl; |
| 423 | } |
| 424 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 425 | //===----------------------------------------------------------------------===// |
| 426 | // AttributeImpl Definition |
| 427 | //===----------------------------------------------------------------------===// |
| 428 | |
Eric Christopher | 0eaa541 | 2014-07-02 22:05:40 +0000 | [diff] [blame] | 429 | // Pin the vtables to this file. |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 430 | AttributeImpl::~AttributeImpl() = default; |
| 431 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 432 | void EnumAttributeImpl::anchor() {} |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 433 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 434 | void IntAttributeImpl::anchor() {} |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 435 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 436 | void StringAttributeImpl::anchor() {} |
Alexey Samsonov | 49109a2 | 2013-11-18 09:31:53 +0000 | [diff] [blame] | 437 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 438 | bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 439 | if (isStringAttribute()) return false; |
| 440 | return getKindAsEnum() == A; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 443 | bool AttributeImpl::hasAttribute(StringRef Kind) const { |
| 444 | if (!isStringAttribute()) return false; |
| 445 | return getKindAsString() == Kind; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 448 | Attribute::AttrKind AttributeImpl::getKindAsEnum() const { |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 449 | assert(isEnumAttribute() || isIntAttribute()); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 450 | return static_cast<const EnumAttributeImpl *>(this)->getEnumKind(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 453 | uint64_t AttributeImpl::getValueAsInt() const { |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 454 | assert(isIntAttribute()); |
| 455 | return static_cast<const IntAttributeImpl *>(this)->getValue(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 458 | StringRef AttributeImpl::getKindAsString() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 459 | assert(isStringAttribute()); |
| 460 | return static_cast<const StringAttributeImpl *>(this)->getStringKind(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 463 | StringRef AttributeImpl::getValueAsString() const { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 464 | assert(isStringAttribute()); |
| 465 | return static_cast<const StringAttributeImpl *>(this)->getStringValue(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | bool AttributeImpl::operator<(const AttributeImpl &AI) const { |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 469 | // This sorts the attributes with Attribute::AttrKinds coming first (sorted |
| 470 | // relative to their enum value) and then strings. |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 471 | if (isEnumAttribute()) { |
| 472 | if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 473 | if (AI.isIntAttribute()) return true; |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 474 | if (AI.isStringAttribute()) return true; |
| 475 | } |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 476 | |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 477 | if (isIntAttribute()) { |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 478 | if (AI.isEnumAttribute()) return false; |
Reid Kleckner | 7de6761 | 2016-04-04 23:06:05 +0000 | [diff] [blame] | 479 | if (AI.isIntAttribute()) { |
| 480 | if (getKindAsEnum() == AI.getKindAsEnum()) |
| 481 | return getValueAsInt() < AI.getValueAsInt(); |
| 482 | return getKindAsEnum() < AI.getKindAsEnum(); |
| 483 | } |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 484 | if (AI.isStringAttribute()) return true; |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 485 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 486 | |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 487 | if (AI.isEnumAttribute()) return false; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 488 | if (AI.isIntAttribute()) return false; |
Bill Wendling | 26b9575 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 489 | if (getKindAsString() == AI.getKindAsString()) |
| 490 | return getValueAsString() < AI.getValueAsString(); |
| 491 | return getKindAsString() < AI.getKindAsString(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 494 | //===----------------------------------------------------------------------===// |
| 495 | // AttributeSetNode Definition |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
| 498 | AttributeSetNode *AttributeSetNode::get(LLVMContext &C, |
| 499 | ArrayRef<Attribute> Attrs) { |
| 500 | if (Attrs.empty()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 501 | return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 502 | |
| 503 | // Otherwise, build a key to look up the existing attributes. |
| 504 | LLVMContextImpl *pImpl = C.pImpl; |
| 505 | FoldingSetNodeID ID; |
| 506 | |
| 507 | SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); |
Reid Kleckner | 7de6761 | 2016-04-04 23:06:05 +0000 | [diff] [blame] | 508 | std::sort(SortedAttrs.begin(), SortedAttrs.end()); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 509 | |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 510 | for (Attribute Attr : SortedAttrs) |
| 511 | Attr.Profile(ID); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 512 | |
| 513 | void *InsertPoint; |
| 514 | AttributeSetNode *PA = |
| 515 | pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); |
| 516 | |
| 517 | // If we didn't find any existing attributes of the same shape then create a |
| 518 | // new one and insert it. |
| 519 | if (!PA) { |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 520 | // Coallocate entries after the AttributeSetNode itself. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 521 | void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size())); |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 522 | PA = new (Mem) AttributeSetNode(SortedAttrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 523 | pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); |
| 524 | } |
| 525 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 526 | // Return the AttributeSetNode that we found or created. |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 527 | return PA; |
| 528 | } |
| 529 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 530 | AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) { |
| 531 | // Add target-independent attributes. |
| 532 | SmallVector<Attribute, 8> Attrs; |
| 533 | for (Attribute::AttrKind Kind = Attribute::None; |
| 534 | Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) { |
| 535 | if (!B.contains(Kind)) |
| 536 | continue; |
| 537 | |
| 538 | Attribute Attr; |
| 539 | switch (Kind) { |
| 540 | case Attribute::Alignment: |
| 541 | Attr = Attribute::getWithAlignment(C, B.getAlignment()); |
| 542 | break; |
| 543 | case Attribute::StackAlignment: |
| 544 | Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment()); |
| 545 | break; |
| 546 | case Attribute::Dereferenceable: |
| 547 | Attr = Attribute::getWithDereferenceableBytes( |
| 548 | C, B.getDereferenceableBytes()); |
| 549 | break; |
| 550 | case Attribute::DereferenceableOrNull: |
| 551 | Attr = Attribute::getWithDereferenceableOrNullBytes( |
| 552 | C, B.getDereferenceableOrNullBytes()); |
| 553 | break; |
| 554 | case Attribute::AllocSize: { |
| 555 | auto A = B.getAllocSizeArgs(); |
| 556 | Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second); |
| 557 | break; |
| 558 | } |
| 559 | default: |
| 560 | Attr = Attribute::get(C, Kind); |
| 561 | } |
| 562 | Attrs.push_back(Attr); |
| 563 | } |
| 564 | |
| 565 | // Add target-dependent (string) attributes. |
| 566 | for (const auto &TDA : B.td_attrs()) |
| 567 | Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second)); |
| 568 | |
| 569 | return get(C, Attrs); |
| 570 | } |
| 571 | |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 572 | bool AttributeSetNode::hasAttribute(StringRef Kind) const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 573 | for (Attribute I : *this) |
| 574 | if (I.hasAttribute(Kind)) |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 575 | return true; |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 580 | if (hasAttribute(Kind)) { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 581 | for (Attribute I : *this) |
| 582 | if (I.hasAttribute(Kind)) |
| 583 | return I; |
Matthias Braun | 31eeb76f | 2016-01-29 22:25:13 +0000 | [diff] [blame] | 584 | } |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 585 | return Attribute(); |
| 586 | } |
| 587 | |
| 588 | Attribute AttributeSetNode::getAttribute(StringRef Kind) const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 589 | for (Attribute I : *this) |
| 590 | if (I.hasAttribute(Kind)) |
| 591 | return I; |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 592 | return Attribute(); |
| 593 | } |
| 594 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 595 | unsigned AttributeSetNode::getAlignment() const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 596 | for (Attribute I : *this) |
| 597 | if (I.hasAttribute(Attribute::Alignment)) |
| 598 | return I.getAlignment(); |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | unsigned AttributeSetNode::getStackAlignment() const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 603 | for (Attribute I : *this) |
| 604 | if (I.hasAttribute(Attribute::StackAlignment)) |
| 605 | return I.getStackAlignment(); |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 609 | uint64_t AttributeSetNode::getDereferenceableBytes() const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 610 | for (Attribute I : *this) |
| 611 | if (I.hasAttribute(Attribute::Dereferenceable)) |
| 612 | return I.getDereferenceableBytes(); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 616 | uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 617 | for (Attribute I : *this) |
| 618 | if (I.hasAttribute(Attribute::DereferenceableOrNull)) |
| 619 | return I.getDereferenceableOrNullBytes(); |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 623 | std::pair<unsigned, Optional<unsigned>> |
| 624 | AttributeSetNode::getAllocSizeArgs() const { |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 625 | for (Attribute I : *this) |
| 626 | if (I.hasAttribute(Attribute::AllocSize)) |
| 627 | return I.getAllocSizeArgs(); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 628 | return std::make_pair(0, 0); |
| 629 | } |
| 630 | |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 631 | std::string AttributeSetNode::getAsString(bool InAttrGrp) const { |
Benjamin Kramer | 0baf8f4 | 2013-04-19 11:43:21 +0000 | [diff] [blame] | 632 | std::string Str; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 633 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 634 | if (I != begin()) |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 635 | Str += ' '; |
| 636 | Str += I->getAsString(InAttrGrp); |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 637 | } |
| 638 | return Str; |
| 639 | } |
| 640 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 641 | //===----------------------------------------------------------------------===// |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 642 | // AttributeListImpl Definition |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 643 | //===----------------------------------------------------------------------===// |
| 644 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 645 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 646 | LLVM_DUMP_METHOD void AttributeListImpl::dump() const { |
| 647 | AttributeList(const_cast<AttributeListImpl *>(this)).dump(); |
Peter Collingbourne | abca2ec | 2013-08-02 22:34:30 +0000 | [diff] [blame] | 648 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 649 | #endif |
Peter Collingbourne | abca2ec | 2013-08-02 22:34:30 +0000 | [diff] [blame] | 650 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 651 | //===----------------------------------------------------------------------===// |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 652 | // AttributeList Construction and Mutation Methods |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 653 | //===----------------------------------------------------------------------===// |
| 654 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 655 | AttributeList AttributeList::getImpl( |
| 656 | LLVMContext &C, ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) { |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 657 | assert(!Attrs.empty() && "creating pointless AttributeList"); |
| 658 | #ifndef NDEBUG |
| 659 | unsigned LastIndex = 0; |
| 660 | bool IsFirst = true; |
| 661 | for (const auto &AttrPair : Attrs) { |
| 662 | assert((IsFirst || LastIndex < AttrPair.first) && |
| 663 | "unsorted or duplicate AttributeList indices"); |
| 664 | assert(AttrPair.second && "pointless AttributeList slot"); |
| 665 | LastIndex = AttrPair.first; |
| 666 | IsFirst = false; |
| 667 | } |
| 668 | #endif |
| 669 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 670 | LLVMContextImpl *pImpl = C.pImpl; |
| 671 | FoldingSetNodeID ID; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 672 | AttributeListImpl::Profile(ID, Attrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 673 | |
| 674 | void *InsertPoint; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 675 | AttributeListImpl *PA = |
| 676 | pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 677 | |
| 678 | // If we didn't find any existing attributes of the same shape then |
| 679 | // create a new one and insert it. |
| 680 | if (!PA) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 681 | // Coallocate entries after the AttributeListImpl itself. |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 682 | void *Mem = ::operator new( |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 683 | AttributeListImpl::totalSizeToAlloc<IndexAttrPair>(Attrs.size())); |
| 684 | PA = new (Mem) AttributeListImpl(C, Attrs); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 685 | pImpl->AttrsLists.InsertNode(PA, InsertPoint); |
| 686 | } |
| 687 | |
| 688 | // Return the AttributesList that we found or created. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 689 | return AttributeList(PA); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 692 | AttributeList |
| 693 | AttributeList::get(LLVMContext &C, |
| 694 | ArrayRef<std::pair<unsigned, Attribute>> Attrs) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 695 | // If there are no attributes then return a null AttributesList pointer. |
| 696 | if (Attrs.empty()) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 697 | return AttributeList(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 698 | |
Craig Topper | e30b8ca | 2016-01-03 19:43:40 +0000 | [diff] [blame] | 699 | assert(std::is_sorted(Attrs.begin(), Attrs.end(), |
| 700 | [](const std::pair<unsigned, Attribute> &LHS, |
| 701 | const std::pair<unsigned, Attribute> &RHS) { |
| 702 | return LHS.first < RHS.first; |
| 703 | }) && "Misordered Attributes list!"); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 704 | assert(none_of(Attrs, |
| 705 | [](const std::pair<unsigned, Attribute> &Pair) { |
| 706 | return Pair.second.hasAttribute(Attribute::None); |
| 707 | }) && |
| 708 | "Pointless attribute!"); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 709 | |
| 710 | // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes |
| 711 | // list. |
| 712 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 713 | for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(), |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 714 | E = Attrs.end(); I != E; ) { |
| 715 | unsigned Index = I->first; |
| 716 | SmallVector<Attribute, 4> AttrVec; |
NAKAMURA Takumi | f05d2f2 | 2013-01-29 15:18:16 +0000 | [diff] [blame] | 717 | while (I != E && I->first == Index) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 718 | AttrVec.push_back(I->second); |
| 719 | ++I; |
| 720 | } |
| 721 | |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 722 | AttrPairVec.emplace_back(Index, AttributeSetNode::get(C, AttrVec)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return getImpl(C, AttrPairVec); |
| 726 | } |
| 727 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 728 | AttributeList |
| 729 | AttributeList::get(LLVMContext &C, |
| 730 | ArrayRef<std::pair<unsigned, AttributeSetNode *>> Attrs) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 731 | // If there are no attributes then return a null AttributesList pointer. |
| 732 | if (Attrs.empty()) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 733 | return AttributeList(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 734 | |
| 735 | return getImpl(C, Attrs); |
| 736 | } |
| 737 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 738 | AttributeList AttributeList::get(LLVMContext &C, ArrayRef<AttributeSetNode*> Attrs) { |
| 739 | assert(Attrs.size() >= 2 && |
| 740 | "should always have function and return attr slots"); |
| 741 | SmallVector<std::pair<unsigned, AttributeSetNode *>, 8> AttrPairs; |
| 742 | size_t Index = 0; |
| 743 | for (AttributeSetNode *AS : Attrs) { |
| 744 | if (AS) { |
| 745 | // If this is the last AttributeSetNode, it's for the function. |
| 746 | if (Index == Attrs.size() - 1) |
| 747 | Index = AttributeList::FunctionIndex; |
| 748 | AttrPairs.emplace_back(Index, AS); |
| 749 | } |
| 750 | ++Index; |
| 751 | } |
| 752 | if (AttrPairs.empty()) |
| 753 | return AttributeList(); |
| 754 | return getImpl(C, AttrPairs); |
| 755 | } |
| 756 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 757 | AttributeList AttributeList::get(LLVMContext &C, unsigned Index, |
| 758 | const AttrBuilder &B) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 759 | if (!B.hasAttributes()) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 760 | return AttributeList(); |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 761 | AttributeSetNode *ASN = AttributeSetNode::get(C, B); |
| 762 | std::pair<unsigned, AttributeSetNode *> Arr[1] = {{Index, ASN}}; |
| 763 | return getImpl(C, Arr); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 766 | AttributeList AttributeList::get(LLVMContext &C, unsigned Index, |
| 767 | ArrayRef<Attribute::AttrKind> Kinds) { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 768 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 769 | for (Attribute::AttrKind K : Kinds) |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 770 | Attrs.emplace_back(Index, Attribute::get(C, K)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 771 | return get(C, Attrs); |
| 772 | } |
| 773 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 774 | AttributeList AttributeList::get(LLVMContext &C, unsigned Index, |
| 775 | ArrayRef<StringRef> Kinds) { |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 776 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
| 777 | for (StringRef K : Kinds) |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 778 | Attrs.emplace_back(Index, Attribute::get(C, K)); |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 779 | return get(C, Attrs); |
| 780 | } |
| 781 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 782 | AttributeList AttributeList::get(LLVMContext &C, |
| 783 | ArrayRef<AttributeList> Attrs) { |
| 784 | if (Attrs.empty()) |
| 785 | return AttributeList(); |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 786 | if (Attrs.size() == 1) return Attrs[0]; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 787 | |
| 788 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 789 | AttributeListImpl *A0 = Attrs[0].pImpl; |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 790 | if (A0) |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 791 | AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumSlots())); |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 792 | // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec |
| 793 | // ordered by index. Because we know that each list in Attrs is ordered by |
| 794 | // index we only need to merge each successive list in rather than doing a |
| 795 | // full sort. |
| 796 | for (unsigned I = 1, E = Attrs.size(); I != E; ++I) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 797 | AttributeListImpl *AS = Attrs[I].pImpl; |
Benjamin Kramer | 741146b | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 798 | if (!AS) continue; |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 799 | SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator |
| 800 | ANVI = AttrNodeVec.begin(), ANVE; |
James Y Knight | aa365b2 | 2015-08-05 22:57:34 +0000 | [diff] [blame] | 801 | for (const IndexAttrPair *AI = AS->getNode(0), |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 802 | *AE = AS->getNode(AS->getNumSlots()); |
Peter Collingbourne | bd6c745 | 2013-08-02 22:29:40 +0000 | [diff] [blame] | 803 | AI != AE; ++AI) { |
| 804 | ANVE = AttrNodeVec.end(); |
| 805 | while (ANVI != ANVE && ANVI->first <= AI->first) |
| 806 | ++ANVI; |
| 807 | ANVI = AttrNodeVec.insert(ANVI, *AI) + 1; |
| 808 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return getImpl(C, AttrNodeVec); |
| 812 | } |
| 813 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 814 | AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, |
| 815 | Attribute::AttrKind Kind) const { |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 816 | if (hasAttribute(Index, Kind)) return *this; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 817 | return addAttributes(C, Index, AttributeList::get(C, Index, Kind)); |
Reed Kotler | 795c7b4 | 2013-03-13 20:20:08 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 820 | AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, |
| 821 | StringRef Kind, |
| 822 | StringRef Value) const { |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 823 | AttrBuilder B; |
Bill Wendling | 3b2f610 | 2013-07-25 18:34:24 +0000 | [diff] [blame] | 824 | B.addAttribute(Kind, Value); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 825 | return addAttributes(C, Index, AttributeList::get(C, Index, B)); |
Bill Wendling | 3b2f610 | 2013-07-25 18:34:24 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 828 | AttributeList AttributeList::addAttribute(LLVMContext &C, |
| 829 | ArrayRef<unsigned> Indices, |
| 830 | Attribute A) const { |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 831 | assert(std::is_sorted(Indices.begin(), Indices.end())); |
Reid Kleckner | 324c99d | 2017-04-10 20:18:10 +0000 | [diff] [blame] | 832 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 833 | unsigned I = 0, E = pImpl ? pImpl->getNumSlots() : 0; |
| 834 | SmallVector<IndexAttrPair, 4> AttrVec; |
| 835 | for (unsigned Index : Indices) { |
| 836 | // Add all attribute slots before the current index. |
| 837 | for (; I < E && getSlotIndex(I) < Index; ++I) |
| 838 | AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I)); |
| 839 | |
| 840 | // Add the attribute at this index. If we already have attributes at this |
| 841 | // index, merge them into a new set. |
| 842 | AttrBuilder B; |
| 843 | if (I < E && getSlotIndex(I) == Index) { |
| 844 | B.merge(AttrBuilder(pImpl->getSlotNode(I))); |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 845 | ++I; |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 846 | } |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 847 | B.addAttribute(A); |
| 848 | AttrVec.emplace_back(Index, AttributeSetNode::get(C, B)); |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 851 | // Add remaining attributes. |
| 852 | for (; I < E; ++I) |
| 853 | AttrVec.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I)); |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 854 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 855 | return get(C, AttrVec); |
Akira Hatanaka | 237916b | 2015-12-02 06:58:49 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 858 | AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, |
| 859 | AttributeList Attrs) const { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 860 | if (!pImpl) return Attrs; |
| 861 | if (!Attrs.pImpl) return *this; |
| 862 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 863 | return addAttributes(C, Index, Attrs.getAttributes(Index)); |
| 864 | } |
| 865 | |
| 866 | AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, |
| 867 | AttributeSetNode *AS) const { |
| 868 | if (!AS) |
| 869 | return *this; |
| 870 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 871 | #ifndef NDEBUG |
| 872 | // FIXME it is not obvious how this should work for alignment. For now, say |
| 873 | // we can't change a known alignment. |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 874 | unsigned OldAlign = getParamAlignment(Index); |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 875 | unsigned NewAlign = AS->getAlignment(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 876 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
| 877 | "Attempt to change alignment!"); |
| 878 | #endif |
| 879 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 880 | SmallVector<std::pair<unsigned, AttributeSetNode *>, 4> AttrSet; |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 881 | uint64_t NumAttrs = pImpl->getNumSlots(); |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 882 | unsigned I; |
| 883 | |
| 884 | // Add all the attribute slots before the one we need to merge. |
| 885 | for (I = 0; I < NumAttrs; ++I) { |
| 886 | if (getSlotIndex(I) >= Index) |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 887 | break; |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 888 | AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 891 | if (I < NumAttrs && getSlotIndex(I) == Index) { |
| 892 | // We need to merge two AttributeSetNodes. |
| 893 | AttributeSetNode *Merged = AttributeSetNode::get( |
| 894 | C, AttrBuilder(pImpl->getSlotNode(I)).merge(AttrBuilder(AS))); |
| 895 | AttrSet.emplace_back(Index, Merged); |
| 896 | ++I; |
| 897 | } else { |
| 898 | // Otherwise, there were no attributes at this position in the original |
| 899 | // list. Add the set as is. |
| 900 | AttrSet.emplace_back(Index, AS); |
| 901 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 902 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 903 | // Add the remaining entries. |
| 904 | for (; I < NumAttrs; ++I) |
| 905 | AttrSet.emplace_back(getSlotIndex(I), pImpl->getSlotNode(I)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 906 | |
| 907 | return get(C, AttrSet); |
| 908 | } |
| 909 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 910 | AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, |
| 911 | const AttrBuilder &B) const { |
| 912 | return get(C, Index, AttributeSetNode::get(C, B)); |
| 913 | } |
| 914 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 915 | AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, |
| 916 | Attribute::AttrKind Kind) const { |
Amaury Sechet | 392638d | 2016-06-14 20:27:35 +0000 | [diff] [blame] | 917 | if (!hasAttribute(Index, Kind)) return *this; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 918 | return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 921 | AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, |
| 922 | StringRef Kind) const { |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 923 | if (!hasAttribute(Index, Kind)) return *this; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 924 | return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); |
Amaury Sechet | 6100adf | 2016-06-15 17:50:39 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 927 | AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index, |
| 928 | AttributeList Attrs) const { |
| 929 | if (!pImpl) |
| 930 | return AttributeList(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 931 | if (!Attrs.pImpl) return *this; |
| 932 | |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 933 | // FIXME it is not obvious how this should work for alignment. |
| 934 | // For now, say we can't pass in alignment, which no current use does. |
| 935 | assert(!Attrs.hasAttribute(Index, Attribute::Alignment) && |
| 936 | "Attempt to change alignment!"); |
| 937 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 938 | // Add the attribute slots before the one we're trying to add. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 939 | SmallVector<AttributeList, 4> AttrSet; |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 940 | uint64_t NumAttrs = pImpl->getNumSlots(); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 941 | AttributeList AS; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 942 | uint64_t LastIndex = 0; |
| 943 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 944 | if (getSlotIndex(I) >= Index) { |
| 945 | if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | LastIndex = I + 1; |
| 949 | AttrSet.push_back(getSlotAttributes(I)); |
| 950 | } |
| 951 | |
Bill Wendling | d219675 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 952 | // Now remove the attribute from the correct slot. There may already be an |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 953 | // AttributeList there. |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 954 | AttrBuilder B(AS, Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 955 | |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 956 | for (unsigned I = 0, E = Attrs.pImpl->getNumSlots(); I != E; ++I) |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 957 | if (Attrs.getSlotIndex(I) == Index) { |
| 958 | B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 962 | AttrSet.push_back(AttributeList::get(C, Index, B)); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 963 | |
| 964 | // Add the remaining attribute slots. |
| 965 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 966 | AttrSet.push_back(getSlotAttributes(I)); |
| 967 | |
| 968 | return get(C, AttrSet); |
| 969 | } |
| 970 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 971 | AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index, |
| 972 | const AttrBuilder &Attrs) const { |
| 973 | if (!pImpl) |
| 974 | return AttributeList(); |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 975 | |
| 976 | // FIXME it is not obvious how this should work for alignment. |
| 977 | // For now, say we can't pass in alignment, which no current use does. |
| 978 | assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!"); |
| 979 | |
| 980 | // Add the attribute slots before the one we're trying to add. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 981 | SmallVector<AttributeList, 4> AttrSet; |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 982 | uint64_t NumAttrs = pImpl->getNumSlots(); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 983 | AttributeList AS; |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 984 | uint64_t LastIndex = 0; |
| 985 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 986 | if (getSlotIndex(I) >= Index) { |
| 987 | if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++); |
| 988 | break; |
| 989 | } |
| 990 | LastIndex = I + 1; |
| 991 | AttrSet.push_back(getSlotAttributes(I)); |
| 992 | } |
| 993 | |
| 994 | // Now remove the attribute from the correct slot. There may already be an |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 995 | // AttributeList there. |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 996 | AttrBuilder B(AS, Index); |
| 997 | B.remove(Attrs); |
| 998 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 999 | AttrSet.push_back(AttributeList::get(C, Index, B)); |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Add the remaining attribute slots. |
| 1002 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 1003 | AttrSet.push_back(getSlotAttributes(I)); |
| 1004 | |
| 1005 | return get(C, AttrSet); |
| 1006 | } |
| 1007 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 1008 | AttributeList AttributeList::removeAttributes(LLVMContext &C, |
| 1009 | unsigned WithoutIndex) const { |
| 1010 | if (!pImpl) |
| 1011 | return AttributeList(); |
| 1012 | |
| 1013 | SmallVector<std::pair<unsigned, AttributeSetNode *>, 4> AttrSet; |
| 1014 | for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) { |
| 1015 | unsigned Index = getSlotIndex(I); |
| 1016 | if (Index != WithoutIndex) |
| 1017 | AttrSet.push_back({Index, pImpl->getSlotNode(I)}); |
| 1018 | } |
| 1019 | return get(C, AttrSet); |
| 1020 | } |
| 1021 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1022 | AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C, |
| 1023 | unsigned Index, |
| 1024 | uint64_t Bytes) const { |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1025 | AttrBuilder B; |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 1026 | B.addDereferenceableAttr(Bytes); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1027 | return addAttributes(C, Index, AttributeList::get(C, Index, B)); |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1030 | AttributeList |
| 1031 | AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, |
| 1032 | uint64_t Bytes) const { |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1033 | AttrBuilder B; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1034 | B.addDereferenceableOrNullAttr(Bytes); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1035 | return addAttributes(C, Index, AttributeList::get(C, Index, B)); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1038 | AttributeList |
| 1039 | AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index, |
| 1040 | unsigned ElemSizeArg, |
| 1041 | const Optional<unsigned> &NumElemsArg) { |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1042 | AttrBuilder B; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1043 | B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1044 | return addAttributes(C, Index, AttributeList::get(C, Index, B)); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1047 | //===----------------------------------------------------------------------===// |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1048 | // AttributeList Accessor Methods |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1049 | //===----------------------------------------------------------------------===// |
| 1050 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1051 | LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); } |
| 1052 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 1053 | AttributeSetNode *AttributeList::getParamAttributes(unsigned Index) const { |
| 1054 | return getAttributes(Index); |
Bill Wendling | 5d020a3 | 2013-02-10 05:00:40 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 1057 | AttributeSetNode *AttributeList::getRetAttributes() const { |
| 1058 | return getAttributes(ReturnIndex); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 1061 | AttributeSetNode *AttributeList::getFnAttributes() const { |
| 1062 | return getAttributes(FunctionIndex); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1065 | bool AttributeList::hasAttribute(unsigned Index, |
| 1066 | Attribute::AttrKind Kind) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1067 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1068 | return ASN && ASN->hasAttribute(Kind); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1071 | bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const { |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1072 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1073 | return ASN && ASN->hasAttribute(Kind); |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1076 | bool AttributeList::hasAttributes(unsigned Index) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1077 | AttributeSetNode *ASN = getAttributes(Index); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1078 | return ASN && ASN->hasAttributes(); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1081 | bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const { |
Matthias Braun | 3328281 | 2016-01-29 22:25:19 +0000 | [diff] [blame] | 1082 | return pImpl && pImpl->hasFnAttribute(Kind); |
| 1083 | } |
| 1084 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1085 | bool AttributeList::hasFnAttribute(StringRef Kind) const { |
| 1086 | return hasAttribute(AttributeList::FunctionIndex, Kind); |
Amaury Sechet | 5f04d81 | 2016-09-09 04:50:38 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1089 | bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr, |
| 1090 | unsigned *Index) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1091 | if (!pImpl) return false; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1092 | |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1093 | for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1094 | for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I); |
| 1095 | II != IE; ++II) |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 1096 | if (II->hasAttribute(Attr)) { |
| 1097 | if (Index) *Index = pImpl->getSlotIndex(I); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1098 | return true; |
Hal Finkel | e87ad54 | 2016-07-10 23:01:32 +0000 | [diff] [blame] | 1099 | } |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1100 | |
| 1101 | return false; |
| 1102 | } |
| 1103 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1104 | Attribute AttributeList::getAttribute(unsigned Index, |
| 1105 | Attribute::AttrKind Kind) const { |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1106 | AttributeSetNode *ASN = getAttributes(Index); |
| 1107 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 1108 | } |
| 1109 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1110 | Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const { |
Bill Wendling | bce7b97 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 1111 | AttributeSetNode *ASN = getAttributes(Index); |
| 1112 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 1113 | } |
| 1114 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1115 | unsigned AttributeList::getParamAlignment(unsigned Index) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1116 | AttributeSetNode *ASN = getAttributes(Index); |
| 1117 | return ASN ? ASN->getAlignment() : 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1120 | unsigned AttributeList::getStackAlignment(unsigned Index) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1121 | AttributeSetNode *ASN = getAttributes(Index); |
| 1122 | return ASN ? ASN->getStackAlignment() : 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1125 | uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const { |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1126 | AttributeSetNode *ASN = getAttributes(Index); |
| 1127 | return ASN ? ASN->getDereferenceableBytes() : 0; |
| 1128 | } |
| 1129 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1130 | uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const { |
Sanjoy Das | 06cf33f | 2015-05-06 17:41:54 +0000 | [diff] [blame] | 1131 | AttributeSetNode *ASN = getAttributes(Index); |
| 1132 | return ASN ? ASN->getDereferenceableOrNullBytes() : 0; |
| 1133 | } |
| 1134 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1135 | std::pair<unsigned, Optional<unsigned>> |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1136 | AttributeList::getAllocSizeArgs(unsigned Index) const { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1137 | AttributeSetNode *ASN = getAttributes(Index); |
Matt Arsenault | 4ced16d | 2016-07-18 22:12:46 +0000 | [diff] [blame] | 1138 | return ASN ? ASN->getAllocSizeArgs() : std::make_pair(0u, Optional<unsigned>(0u)); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1141 | std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const { |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1142 | AttributeSetNode *ASN = getAttributes(Index); |
Rafael Espindola | cbf5a7a | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 1143 | return ASN ? ASN->getAsString(InAttrGrp) : std::string(""); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1146 | AttributeSetNode *AttributeList::getAttributes(unsigned Index) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1147 | if (!pImpl) return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1148 | |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1149 | // Loop through to find the attribute node we want. |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1150 | for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1151 | if (pImpl->getSlotIndex(I) == Index) |
Bill Wendling | f2955aa | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 1152 | return pImpl->getSlotNode(I); |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1153 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1154 | return nullptr; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1157 | AttributeList::iterator AttributeList::begin(unsigned Slot) const { |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1158 | if (!pImpl) |
| 1159 | return ArrayRef<Attribute>().begin(); |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1160 | return pImpl->begin(Slot); |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1163 | AttributeList::iterator AttributeList::end(unsigned Slot) const { |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1164 | if (!pImpl) |
| 1165 | return ArrayRef<Attribute>().end(); |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1166 | return pImpl->end(Slot); |
Bill Wendling | a917486 | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1169 | //===----------------------------------------------------------------------===// |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1170 | // AttributeList Introspection Methods |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1171 | //===----------------------------------------------------------------------===// |
| 1172 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1173 | unsigned AttributeList::getNumSlots() const { |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1174 | return pImpl ? pImpl->getNumSlots() : 0; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1177 | unsigned AttributeList::getSlotIndex(unsigned Slot) const { |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1178 | assert(pImpl && Slot < pImpl->getNumSlots() && |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1179 | "Slot # out of range!"); |
| 1180 | return pImpl->getSlotIndex(Slot); |
| 1181 | } |
| 1182 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1183 | AttributeList AttributeList::getSlotAttributes(unsigned Slot) const { |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1184 | assert(pImpl && Slot < pImpl->getNumSlots() && |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1185 | "Slot # out of range!"); |
| 1186 | return pImpl->getSlotAttributes(Slot); |
| 1187 | } |
| 1188 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1189 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1190 | LLVM_DUMP_METHOD void AttributeList::dump() const { |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1191 | dbgs() << "PAL[\n"; |
| 1192 | |
| 1193 | for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { |
| 1194 | uint64_t Index = getSlotIndex(i); |
| 1195 | dbgs() << " { "; |
| 1196 | if (Index == ~0U) |
| 1197 | dbgs() << "~0U"; |
| 1198 | else |
| 1199 | dbgs() << Index; |
| 1200 | dbgs() << " => " << getAsString(Index) << " }\n"; |
| 1201 | } |
| 1202 | |
| 1203 | dbgs() << "]\n"; |
| 1204 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1205 | #endif |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1206 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1207 | //===----------------------------------------------------------------------===// |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1208 | // AttrBuilder Method Implementations |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1209 | //===----------------------------------------------------------------------===// |
| 1210 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1211 | AttrBuilder::AttrBuilder(AttributeList AS, unsigned Index) { |
| 1212 | AttributeListImpl *pImpl = AS.pImpl; |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1213 | if (!pImpl) return; |
| 1214 | |
Amaury Sechet | 24c84fd | 2016-06-14 22:04:16 +0000 | [diff] [blame] | 1215 | for (unsigned I = 0, E = pImpl->getNumSlots(); I != E; ++I) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1216 | if (pImpl->getSlotIndex(I) != Index) continue; |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1217 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1218 | for (AttributeListImpl::iterator II = pImpl->begin(I), IE = pImpl->end(I); |
| 1219 | II != IE; ++II) |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1220 | addAttribute(*II); |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 1221 | |
| 1222 | break; |
| 1223 | } |
Bill Wendling | 096f544 | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame^] | 1226 | AttrBuilder::AttrBuilder(AttributeSetNode *AS) { |
| 1227 | if (AS) { |
| 1228 | for (const Attribute &A : *AS) |
| 1229 | addAttribute(A); |
| 1230 | } |
| 1231 | } |
| 1232 | |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1233 | void AttrBuilder::clear() { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1234 | Attrs.reset(); |
Sanjoy Das | 88d0fde | 2015-09-03 22:27:42 +0000 | [diff] [blame] | 1235 | TargetDepAttrs.clear(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1236 | Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1237 | AllocSizeArgs = 0; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1241 | assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); |
Bill Wendling | 1c7cc8a | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 1242 | assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1243 | Val != Attribute::Dereferenceable && Val != Attribute::AllocSize && |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1244 | "Adding integer attribute without adding a value!"); |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1245 | Attrs[Val] = true; |
Bill Wendling | 7c04e04 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 1246 | return *this; |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1249 | AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { |
Bill Wendling | 0a43730 | 2013-02-10 10:13:23 +0000 | [diff] [blame] | 1250 | if (Attr.isStringAttribute()) { |
| 1251 | addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); |
| 1252 | return *this; |
| 1253 | } |
| 1254 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1255 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1256 | Attrs[Kind] = true; |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1257 | |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1258 | if (Kind == Attribute::Alignment) |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1259 | Alignment = Attr.getAlignment(); |
Bill Wendling | 3f12ac2 | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 1260 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1261 | StackAlignment = Attr.getStackAlignment(); |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1262 | else if (Kind == Attribute::Dereferenceable) |
| 1263 | DerefBytes = Attr.getDereferenceableBytes(); |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1264 | else if (Kind == Attribute::DereferenceableOrNull) |
| 1265 | DerefOrNullBytes = Attr.getDereferenceableOrNullBytes(); |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1266 | else if (Kind == Attribute::AllocSize) |
| 1267 | AllocSizeArgs = Attr.getValueAsInt(); |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1268 | return *this; |
| 1269 | } |
| 1270 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 1271 | AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { |
| 1272 | TargetDepAttrs[A] = V; |
| 1273 | return *this; |
| 1274 | } |
| 1275 | |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1276 | AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1277 | assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!"); |
| 1278 | Attrs[Val] = false; |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (Val == Attribute::Alignment) |
| 1281 | Alignment = 0; |
| 1282 | else if (Val == Attribute::StackAlignment) |
| 1283 | StackAlignment = 0; |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1284 | else if (Val == Attribute::Dereferenceable) |
| 1285 | DerefBytes = 0; |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1286 | else if (Val == Attribute::DereferenceableOrNull) |
| 1287 | DerefOrNullBytes = 0; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1288 | else if (Val == Attribute::AllocSize) |
| 1289 | AllocSizeArgs = 0; |
Bill Wendling | 23804da | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 1290 | |
| 1291 | return *this; |
| 1292 | } |
| 1293 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1294 | AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1295 | unsigned Slot = ~0U; |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1296 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 1297 | if (A.getSlotIndex(I) == Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1298 | Slot = I; |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1299 | break; |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1300 | } |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1301 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1302 | assert(Slot != ~0U && "Couldn't find index in AttributeList!"); |
Bill Wendling | f1c94e3 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 1303 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1304 | for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E; |
| 1305 | ++I) { |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1306 | Attribute Attr = *I; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1307 | if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1308 | removeAttribute(Attr.getKindAsEnum()); |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | assert(Attr.isStringAttribute() && "Invalid attribute type!"); |
George Burgess IV | 500d303 | 2015-12-16 05:21:02 +0000 | [diff] [blame] | 1311 | removeAttribute(Attr.getKindAsString()); |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1312 | } |
Bill Wendling | 1aa9d9e | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | return *this; |
| 1316 | } |
| 1317 | |
Bill Wendling | b9c5b1a | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 1318 | AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { |
| 1319 | std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A); |
| 1320 | if (I != TargetDepAttrs.end()) |
| 1321 | TargetDepAttrs.erase(I); |
| 1322 | return *this; |
| 1323 | } |
| 1324 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1325 | std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const { |
| 1326 | return unpackAllocSizeArgs(AllocSizeArgs); |
| 1327 | } |
| 1328 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1329 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1330 | if (Align == 0) return *this; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1331 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1332 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1333 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1334 | |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1335 | Attrs[Attribute::Alignment] = true; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1336 | Alignment = Align; |
Bill Wendling | abd5ba2 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1337 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1340 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { |
| 1341 | // Default alignment, allow the target to define how to align it. |
| 1342 | if (Align == 0) return *this; |
| 1343 | |
| 1344 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1345 | assert(Align <= 0x100 && "Alignment too large."); |
| 1346 | |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1347 | Attrs[Attribute::StackAlignment] = true; |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1348 | StackAlignment = Align; |
| 1349 | return *this; |
| 1350 | } |
| 1351 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1352 | AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) { |
| 1353 | if (Bytes == 0) return *this; |
| 1354 | |
| 1355 | Attrs[Attribute::Dereferenceable] = true; |
| 1356 | DerefBytes = Bytes; |
| 1357 | return *this; |
| 1358 | } |
| 1359 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1360 | AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) { |
| 1361 | if (Bytes == 0) |
| 1362 | return *this; |
| 1363 | |
| 1364 | Attrs[Attribute::DereferenceableOrNull] = true; |
| 1365 | DerefOrNullBytes = Bytes; |
| 1366 | return *this; |
| 1367 | } |
| 1368 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1369 | AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize, |
| 1370 | const Optional<unsigned> &NumElems) { |
| 1371 | return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems)); |
| 1372 | } |
| 1373 | |
| 1374 | AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) { |
| 1375 | // (0, 0) is our "not present" value, so we need to check for it here. |
| 1376 | assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)"); |
| 1377 | |
| 1378 | Attrs[Attribute::AllocSize] = true; |
| 1379 | // Reuse existing machinery to store this as a single 64-bit integer so we can |
| 1380 | // save a few bytes over using a pair<unsigned, Optional<unsigned>>. |
| 1381 | AllocSizeArgs = RawArgs; |
| 1382 | return *this; |
| 1383 | } |
| 1384 | |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1385 | AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { |
| 1386 | // FIXME: What if both have alignments, but they don't match?! |
| 1387 | if (!Alignment) |
| 1388 | Alignment = B.Alignment; |
| 1389 | |
| 1390 | if (!StackAlignment) |
| 1391 | StackAlignment = B.StackAlignment; |
| 1392 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1393 | if (!DerefBytes) |
| 1394 | DerefBytes = B.DerefBytes; |
| 1395 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1396 | if (!DerefOrNullBytes) |
| 1397 | DerefOrNullBytes = B.DerefOrNullBytes; |
| 1398 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1399 | if (!AllocSizeArgs) |
| 1400 | AllocSizeArgs = B.AllocSizeArgs; |
| 1401 | |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 1402 | Attrs |= B.Attrs; |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1403 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1404 | for (auto I : B.td_attrs()) |
| 1405 | TargetDepAttrs[I.first] = I.second; |
Bill Wendling | e261492 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1406 | |
| 1407 | return *this; |
| 1408 | } |
| 1409 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1410 | AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) { |
| 1411 | // FIXME: What if both have alignments, but they don't match?! |
| 1412 | if (B.Alignment) |
| 1413 | Alignment = 0; |
| 1414 | |
| 1415 | if (B.StackAlignment) |
| 1416 | StackAlignment = 0; |
| 1417 | |
| 1418 | if (B.DerefBytes) |
| 1419 | DerefBytes = 0; |
| 1420 | |
| 1421 | if (B.DerefOrNullBytes) |
| 1422 | DerefOrNullBytes = 0; |
| 1423 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 1424 | if (B.AllocSizeArgs) |
| 1425 | AllocSizeArgs = 0; |
| 1426 | |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1427 | Attrs &= ~B.Attrs; |
| 1428 | |
| 1429 | for (auto I : B.td_attrs()) |
| 1430 | TargetDepAttrs.erase(I.first); |
| 1431 | |
| 1432 | return *this; |
| 1433 | } |
| 1434 | |
| 1435 | bool AttrBuilder::overlaps(const AttrBuilder &B) const { |
| 1436 | // First check if any of the target independent attributes overlap. |
| 1437 | if ((Attrs & B.Attrs).any()) |
| 1438 | return true; |
| 1439 | |
| 1440 | // Then check if any target dependent ones do. |
Sean Silva | 9011aca | 2017-02-22 06:34:04 +0000 | [diff] [blame] | 1441 | for (const auto &I : td_attrs()) |
Pete Cooper | d2a4461 | 2015-05-06 23:19:43 +0000 | [diff] [blame] | 1442 | if (B.contains(I.first)) |
| 1443 | return true; |
| 1444 | |
| 1445 | return false; |
| 1446 | } |
| 1447 | |
Bill Wendling | 4b00144 | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1448 | bool AttrBuilder::contains(StringRef A) const { |
| 1449 | return TargetDepAttrs.find(A) != TargetDepAttrs.end(); |
| 1450 | } |
| 1451 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1452 | bool AttrBuilder::hasAttributes() const { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1453 | return !Attrs.none() || !TargetDepAttrs.empty(); |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1454 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1455 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1456 | bool AttrBuilder::hasAttributes(AttributeList A, uint64_t Index) const { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1457 | unsigned Slot = ~0U; |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1458 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 1459 | if (A.getSlotIndex(I) == Index) { |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1460 | Slot = I; |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | } |
| 1463 | |
Bill Wendling | 211316c | 2013-04-18 20:17:28 +0000 | [diff] [blame] | 1464 | assert(Slot != ~0U && "Couldn't find the index!"); |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1465 | |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1466 | for (AttributeList::iterator I = A.begin(Slot), E = A.end(Slot); I != E; |
| 1467 | ++I) { |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1468 | Attribute Attr = *I; |
Hal Finkel | e15442c | 2014-07-18 06:51:55 +0000 | [diff] [blame] | 1469 | if (Attr.isEnumAttribute() || Attr.isIntAttribute()) { |
Benjamin Kramer | eaf706b | 2013-02-18 12:09:51 +0000 | [diff] [blame] | 1470 | if (Attrs[I->getKindAsEnum()]) |
Bill Wendling | 7cde51d | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1471 | return true; |
| 1472 | } else { |
| 1473 | assert(Attr.isStringAttribute() && "Invalid attribute kind!"); |
| 1474 | return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end(); |
| 1475 | } |
| 1476 | } |
Bill Wendling | 9ca01da | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1477 | |
| 1478 | return false; |
Bill Wendling | 70f3917 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1479 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1480 | |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1481 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | cd33034 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1482 | return Alignment != 0; |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1485 | bool AttrBuilder::operator==(const AttrBuilder &B) { |
Benjamin Kramer | 45e7d53 | 2013-02-16 19:13:18 +0000 | [diff] [blame] | 1486 | if (Attrs != B.Attrs) |
| 1487 | return false; |
Bill Wendling | 4b00144 | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1488 | |
| 1489 | for (td_const_iterator I = TargetDepAttrs.begin(), |
| 1490 | E = TargetDepAttrs.end(); I != E; ++I) |
| 1491 | if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) |
| 1492 | return false; |
| 1493 | |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1494 | return Alignment == B.Alignment && StackAlignment == B.StackAlignment && |
| 1495 | DerefBytes == B.DerefBytes; |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1498 | //===----------------------------------------------------------------------===// |
| 1499 | // AttributeFuncs Function Defintions |
| 1500 | //===----------------------------------------------------------------------===// |
| 1501 | |
Bill Wendling | c79cdff | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 1502 | /// \brief Which attributes cannot be applied to a type. |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1503 | AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) { |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1504 | AttrBuilder Incompatible; |
| 1505 | |
| 1506 | if (!Ty->isIntegerTy()) |
| 1507 | // Attribute that only apply to integers. |
| 1508 | Incompatible.addAttribute(Attribute::SExt) |
| 1509 | .addAttribute(Attribute::ZExt); |
| 1510 | |
| 1511 | if (!Ty->isPointerTy()) |
| 1512 | // Attribute that only apply to pointers. |
| 1513 | Incompatible.addAttribute(Attribute::ByVal) |
| 1514 | .addAttribute(Attribute::Nest) |
| 1515 | .addAttribute(Attribute::NoAlias) |
| 1516 | .addAttribute(Attribute::NoCapture) |
Nick Lewycky | d52b152 | 2014-05-20 01:23:40 +0000 | [diff] [blame] | 1517 | .addAttribute(Attribute::NonNull) |
Hal Finkel | b0407ba | 2014-07-18 15:51:28 +0000 | [diff] [blame] | 1518 | .addDereferenceableAttr(1) // the int here is ignored |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 1519 | .addDereferenceableOrNullAttr(1) // the int here is ignored |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1520 | .addAttribute(Attribute::ReadNone) |
| 1521 | .addAttribute(Attribute::ReadOnly) |
Reid Kleckner | a534a38 | 2013-12-19 02:14:12 +0000 | [diff] [blame] | 1522 | .addAttribute(Attribute::StructRet) |
| 1523 | .addAttribute(Attribute::InAlloca); |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1524 | |
Pete Cooper | 2777d887 | 2015-05-06 23:19:56 +0000 | [diff] [blame] | 1525 | return Incompatible; |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1526 | } |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1527 | |
| 1528 | template<typename AttrClass> |
| 1529 | static bool isEqual(const Function &Caller, const Function &Callee) { |
| 1530 | return Caller.getFnAttribute(AttrClass::getKind()) == |
| 1531 | Callee.getFnAttribute(AttrClass::getKind()); |
| 1532 | } |
| 1533 | |
| 1534 | /// \brief Compute the logical AND of the attributes of the caller and the |
| 1535 | /// callee. |
| 1536 | /// |
| 1537 | /// This function sets the caller's attribute to false if the callee's attribute |
| 1538 | /// is false. |
| 1539 | template<typename AttrClass> |
| 1540 | static void setAND(Function &Caller, const Function &Callee) { |
| 1541 | if (AttrClass::isSet(Caller, AttrClass::getKind()) && |
| 1542 | !AttrClass::isSet(Callee, AttrClass::getKind())) |
| 1543 | AttrClass::set(Caller, AttrClass::getKind(), false); |
| 1544 | } |
| 1545 | |
| 1546 | /// \brief Compute the logical OR of the attributes of the caller and the |
| 1547 | /// callee. |
| 1548 | /// |
| 1549 | /// This function sets the caller's attribute to true if the callee's attribute |
| 1550 | /// is true. |
| 1551 | template<typename AttrClass> |
| 1552 | static void setOR(Function &Caller, const Function &Callee) { |
| 1553 | if (!AttrClass::isSet(Caller, AttrClass::getKind()) && |
| 1554 | AttrClass::isSet(Callee, AttrClass::getKind())) |
| 1555 | AttrClass::set(Caller, AttrClass::getKind(), true); |
| 1556 | } |
| 1557 | |
| 1558 | /// \brief If the inlined function had a higher stack protection level than the |
| 1559 | /// calling function, then bump up the caller's stack protection level. |
| 1560 | static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) { |
| 1561 | // If upgrading the SSP attribute, clear out the old SSP Attributes first. |
| 1562 | // Having multiple SSP attributes doesn't actually hurt, but it adds useless |
| 1563 | // clutter to the IR. |
| 1564 | AttrBuilder B; |
| 1565 | B.addAttribute(Attribute::StackProtect) |
| 1566 | .addAttribute(Attribute::StackProtectStrong) |
| 1567 | .addAttribute(Attribute::StackProtectReq); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1568 | AttributeList OldSSPAttr = |
| 1569 | AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B); |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1570 | |
Evgeniy Stepanov | f17120a | 2016-04-11 22:27:48 +0000 | [diff] [blame] | 1571 | if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1572 | Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1573 | Caller.addFnAttr(Attribute::StackProtectReq); |
| 1574 | } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) && |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1575 | !Caller.hasFnAttribute(Attribute::StackProtectReq)) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1576 | Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1577 | Caller.addFnAttr(Attribute::StackProtectStrong); |
| 1578 | } else if (Callee.hasFnAttribute(Attribute::StackProtect) && |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1579 | !Caller.hasFnAttribute(Attribute::StackProtectReq) && |
| 1580 | !Caller.hasFnAttribute(Attribute::StackProtectStrong)) |
| 1581 | Caller.addFnAttr(Attribute::StackProtect); |
| 1582 | } |
| 1583 | |
| 1584 | #define GET_ATTR_COMPAT_FUNC |
| 1585 | #include "AttributesCompatFunc.inc" |
| 1586 | |
| 1587 | bool AttributeFuncs::areInlineCompatible(const Function &Caller, |
| 1588 | const Function &Callee) { |
| 1589 | return hasCompatibleFnAttrs(Caller, Callee); |
| 1590 | } |
| 1591 | |
Akira Hatanaka | 1cb242e | 2015-12-22 23:57:37 +0000 | [diff] [blame] | 1592 | void AttributeFuncs::mergeAttributesForInlining(Function &Caller, |
| 1593 | const Function &Callee) { |
| 1594 | mergeFnAttrs(Caller, Callee); |
| 1595 | } |