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