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