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