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