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