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