Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 10 | // \file |
| 11 | // \brief This file implements the Attribute, AttributeImpl, AttrBuilder, |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 12 | // AttributeSetImpl, and AttributeSet classes. |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 17 | #include "AttributeImpl.h" |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 18 | #include "LLVMContextImpl.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Type.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Atomic.h" |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mutex.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Bill Wendling | 817abdd | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 30 | // Attribute Construction Methods |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | fabfde3 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 32 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 33 | Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind, |
| 34 | uint64_t Val) { |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 35 | LLVMContextImpl *pImpl = Context.pImpl; |
| 36 | FoldingSetNodeID ID; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 37 | ID.AddInteger(Kind); |
| 38 | if (Val) ID.AddInteger(Val); |
| 39 | |
| 40 | void *InsertPoint; |
| 41 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 42 | |
| 43 | if (!PA) { |
| 44 | // If we didn't find any existing attributes of the same shape then create a |
| 45 | // new one and insert it. |
| 46 | PA = !Val ? |
| 47 | new AttributeImpl(Context, Kind) : |
| 48 | new AttributeImpl(Context, Kind, Val); |
| 49 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 50 | } |
| 51 | |
| 52 | // Return the Attribute that we found or created. |
| 53 | return Attribute(PA); |
| 54 | } |
| 55 | |
| 56 | Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) { |
| 57 | LLVMContextImpl *pImpl = Context.pImpl; |
| 58 | FoldingSetNodeID ID; |
| 59 | ID.AddString(Kind); |
| 60 | if (!Val.empty()) ID.AddString(Val); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 61 | |
| 62 | void *InsertPoint; |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 63 | AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 64 | |
| 65 | if (!PA) { |
| 66 | // If we didn't find any existing attributes of the same shape then create a |
| 67 | // new one and insert it. |
Bill Wendling | bdcbccc | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 68 | PA = new AttributeImpl(Context, Kind, Val); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 69 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 70 | } |
| 71 | |
Bill Wendling | ea59f89 | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 72 | // Return the Attribute that we found or created. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 73 | return Attribute(PA); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Bill Wendling | c08a5ef | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 76 | Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) { |
Bill Wendling | 169d527 | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 77 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 78 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 79 | return get(Context, Alignment, Align); |
Bill Wendling | c08a5ef | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | Attribute Attribute::getWithStackAlignment(LLVMContext &Context, |
| 83 | uint64_t Align) { |
Bill Wendling | 169d527 | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 84 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 85 | assert(Align <= 0x100 && "Alignment too large."); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 86 | return get(Context, StackAlignment, Align); |
Bill Wendling | c08a5ef | 2013-01-27 22:43:04 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Bill Wendling | 817abdd | 2013-01-29 00:48:16 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // Attribute Accessor Methods |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 93 | bool Attribute::isEnumAttribute() const { |
| 94 | return pImpl && pImpl->isEnumAttribute(); |
| 95 | } |
| 96 | |
| 97 | bool Attribute::isAlignAttribute() const { |
| 98 | return pImpl && pImpl->isAlignAttribute(); |
| 99 | } |
| 100 | |
| 101 | bool Attribute::isStringAttribute() const { |
| 102 | return pImpl && pImpl->isStringAttribute(); |
| 103 | } |
| 104 | |
| 105 | Attribute::AttrKind Attribute::getKindAsEnum() const { |
| 106 | assert((isEnumAttribute() || isAlignAttribute()) && |
| 107 | "Invalid attribute type to get the kind as an enum!"); |
| 108 | return pImpl ? pImpl->getKindAsEnum() : None; |
| 109 | } |
| 110 | |
| 111 | uint64_t Attribute::getValueAsInt() const { |
| 112 | assert(isAlignAttribute() && |
| 113 | "Expected the attribute to be an alignment attribute!"); |
| 114 | return pImpl ? pImpl->getValueAsInt() : 0; |
| 115 | } |
| 116 | |
| 117 | StringRef Attribute::getKindAsString() const { |
| 118 | assert(isStringAttribute() && |
| 119 | "Invalid attribute type to get the kind as a string!"); |
| 120 | return pImpl ? pImpl->getKindAsString() : StringRef(); |
| 121 | } |
| 122 | |
| 123 | StringRef Attribute::getValueAsString() const { |
| 124 | assert(isStringAttribute() && |
| 125 | "Invalid attribute type to get the value as a string!"); |
| 126 | return pImpl ? pImpl->getValueAsString() : StringRef(); |
| 127 | } |
| 128 | |
Bill Wendling | 64754f4 | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 129 | bool Attribute::hasAttribute(AttrKind Kind) const { |
| 130 | return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None); |
| 131 | } |
| 132 | |
| 133 | bool Attribute::hasAttribute(StringRef Kind) const { |
| 134 | if (!isStringAttribute()) return false; |
| 135 | return pImpl && pImpl->hasAttribute(Kind); |
Bill Wendling | 6dc3781 | 2013-01-29 20:45:34 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 138 | /// This returns the alignment field of an attribute as a byte alignment value. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 139 | unsigned Attribute::getAlignment() const { |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 140 | assert(hasAttribute(Attribute::Alignment) && |
| 141 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 142 | return pImpl->getValueAsInt(); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 146 | /// value. |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 147 | unsigned Attribute::getStackAlignment() const { |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 148 | assert(hasAttribute(Attribute::StackAlignment) && |
| 149 | "Trying to get alignment from non-alignment attribute!"); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 150 | return pImpl->getValueAsInt(); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 153 | std::string Attribute::getAsString(bool InAttrGrp) const { |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 154 | if (!pImpl) return ""; |
| 155 | |
| 156 | if (hasAttribute(Attribute::AddressSafety)) |
| 157 | return "address_safety"; |
| 158 | if (hasAttribute(Attribute::AlwaysInline)) |
| 159 | return "alwaysinline"; |
| 160 | if (hasAttribute(Attribute::ByVal)) |
| 161 | return "byval"; |
| 162 | if (hasAttribute(Attribute::InlineHint)) |
| 163 | return "inlinehint"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 164 | if (hasAttribute(Attribute::InReg)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 165 | return "inreg"; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 166 | if (hasAttribute(Attribute::MinSize)) |
| 167 | return "minsize"; |
| 168 | if (hasAttribute(Attribute::Naked)) |
| 169 | return "naked"; |
| 170 | if (hasAttribute(Attribute::Nest)) |
| 171 | return "nest"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 172 | if (hasAttribute(Attribute::NoAlias)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 173 | return "noalias"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 174 | if (hasAttribute(Attribute::NoCapture)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 175 | return "nocapture"; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 176 | if (hasAttribute(Attribute::NoDuplicate)) |
| 177 | return "noduplicate"; |
| 178 | if (hasAttribute(Attribute::NoImplicitFloat)) |
| 179 | return "noimplicitfloat"; |
| 180 | if (hasAttribute(Attribute::NoInline)) |
| 181 | return "noinline"; |
| 182 | if (hasAttribute(Attribute::NonLazyBind)) |
| 183 | return "nonlazybind"; |
| 184 | if (hasAttribute(Attribute::NoRedZone)) |
| 185 | return "noredzone"; |
| 186 | if (hasAttribute(Attribute::NoReturn)) |
| 187 | return "noreturn"; |
| 188 | if (hasAttribute(Attribute::NoUnwind)) |
| 189 | return "nounwind"; |
| 190 | if (hasAttribute(Attribute::OptimizeForSize)) |
| 191 | return "optsize"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 192 | if (hasAttribute(Attribute::ReadNone)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 193 | return "readnone"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 194 | if (hasAttribute(Attribute::ReadOnly)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 195 | return "readonly"; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 196 | if (hasAttribute(Attribute::ReturnsTwice)) |
| 197 | return "returns_twice"; |
| 198 | if (hasAttribute(Attribute::SExt)) |
| 199 | return "signext"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 200 | if (hasAttribute(Attribute::StackProtect)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 201 | return "ssp"; |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 202 | if (hasAttribute(Attribute::StackProtectReq)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 203 | return "sspreq"; |
Bill Wendling | 114baee | 2013-01-23 06:41:41 +0000 | [diff] [blame] | 204 | if (hasAttribute(Attribute::StackProtectStrong)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 205 | return "sspstrong"; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 206 | if (hasAttribute(Attribute::StructRet)) |
| 207 | return "sret"; |
Kostya Serebryany | ab39afa | 2013-02-11 08:13:54 +0000 | [diff] [blame] | 208 | if (hasAttribute(Attribute::ThreadSafety)) |
| 209 | return "thread_safety"; |
| 210 | if (hasAttribute(Attribute::UninitializedChecks)) |
| 211 | return "uninitialized_checks"; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 212 | if (hasAttribute(Attribute::UWTable)) |
| 213 | return "uwtable"; |
| 214 | if (hasAttribute(Attribute::ZExt)) |
| 215 | return "zeroext"; |
| 216 | |
| 217 | // FIXME: These should be output like this: |
| 218 | // |
| 219 | // align=4 |
| 220 | // alignstack=8 |
| 221 | // |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 222 | if (hasAttribute(Attribute::Alignment)) { |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 223 | std::string Result; |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 224 | Result += "align"; |
| 225 | Result += (InAttrGrp) ? "=" : " "; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 226 | Result += utostr(getValueAsInt()); |
| 227 | return Result; |
| 228 | } |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 229 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 230 | if (hasAttribute(Attribute::StackAlignment)) { |
| 231 | std::string Result; |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 232 | Result += "alignstack"; |
| 233 | if (InAttrGrp) { |
| 234 | Result += "="; |
| 235 | Result += utostr(getValueAsInt()); |
| 236 | } else { |
| 237 | Result += "("; |
| 238 | Result += utostr(getValueAsInt()); |
| 239 | Result += ")"; |
| 240 | } |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 241 | return Result; |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 242 | } |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 243 | |
| 244 | // Convert target-dependent attributes to strings of the form: |
| 245 | // |
| 246 | // "kind" |
| 247 | // "kind" = "value" |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 248 | // |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 249 | if (isStringAttribute()) { |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 250 | std::string Result; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 251 | Result += '\"' + getKindAsString().str() + '"'; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 252 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 253 | StringRef Val = pImpl->getValueAsString(); |
| 254 | if (Val.empty()) return Result; |
Bill Wendling | 5a4041e | 2013-02-01 22:32:30 +0000 | [diff] [blame] | 255 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 256 | Result += "=\"" + Val.str() + '"'; |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 257 | return Result; |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 258 | } |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 259 | |
| 260 | llvm_unreachable("Unknown attribute"); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 263 | bool Attribute::operator<(Attribute A) const { |
| 264 | if (!pImpl && !A.pImpl) return false; |
| 265 | if (!pImpl) return true; |
| 266 | if (!A.pImpl) return false; |
| 267 | return *pImpl < *A.pImpl; |
| 268 | } |
| 269 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 270 | //===----------------------------------------------------------------------===// |
| 271 | // AttributeImpl Definition |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 274 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind) |
| 275 | : Context(C), Entry(new EnumAttributeEntry(Kind)) {} |
| 276 | |
| 277 | AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind, |
| 278 | unsigned Align) |
| 279 | : Context(C) { |
| 280 | assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) && |
| 281 | "Wrong kind for alignment attribute!"); |
| 282 | Entry = new AlignAttributeEntry(Kind, Align); |
| 283 | } |
| 284 | |
| 285 | AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val) |
| 286 | : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {} |
| 287 | |
| 288 | AttributeImpl::~AttributeImpl() { |
| 289 | delete Entry; |
| 290 | } |
| 291 | |
| 292 | bool AttributeImpl::isEnumAttribute() const { |
| 293 | return isa<EnumAttributeEntry>(Entry); |
| 294 | } |
| 295 | |
| 296 | bool AttributeImpl::isAlignAttribute() const { |
| 297 | return isa<AlignAttributeEntry>(Entry); |
| 298 | } |
| 299 | |
| 300 | bool AttributeImpl::isStringAttribute() const { |
| 301 | return isa<StringAttributeEntry>(Entry); |
| 302 | } |
| 303 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 304 | bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const { |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 305 | if (isStringAttribute()) return false; |
| 306 | return getKindAsEnum() == A; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 309 | bool AttributeImpl::hasAttribute(StringRef Kind) const { |
| 310 | if (!isStringAttribute()) return false; |
| 311 | return getKindAsString() == Kind; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 314 | Attribute::AttrKind AttributeImpl::getKindAsEnum() const { |
| 315 | if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry)) |
| 316 | return E->getEnumKind(); |
| 317 | return cast<AlignAttributeEntry>(Entry)->getEnumKind(); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 320 | uint64_t AttributeImpl::getValueAsInt() const { |
| 321 | return cast<AlignAttributeEntry>(Entry)->getAlignment(); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 324 | StringRef AttributeImpl::getKindAsString() const { |
| 325 | return cast<StringAttributeEntry>(Entry)->getStringKind(); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 328 | StringRef AttributeImpl::getValueAsString() const { |
| 329 | return cast<StringAttributeEntry>(Entry)->getStringValue(); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | bool AttributeImpl::operator<(const AttributeImpl &AI) const { |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 333 | // This sorts the attributes with Attribute::AttrKinds coming first (sorted |
| 334 | // relative to their enum value) and then strings. |
Bill Wendling | 94328f4 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 335 | if (isEnumAttribute()) { |
| 336 | if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum(); |
| 337 | if (AI.isAlignAttribute()) return true; |
| 338 | if (AI.isStringAttribute()) return true; |
| 339 | } |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 340 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 341 | if (isAlignAttribute()) { |
Bill Wendling | 94328f4 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 342 | if (AI.isEnumAttribute()) return false; |
| 343 | if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt(); |
| 344 | if (AI.isStringAttribute()) return true; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 345 | } |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 346 | |
Bill Wendling | 94328f4 | 2013-02-15 05:25:26 +0000 | [diff] [blame] | 347 | if (AI.isEnumAttribute()) return false; |
| 348 | if (AI.isAlignAttribute()) return false; |
| 349 | if (getKindAsString() == AI.getKindAsString()) |
| 350 | return getValueAsString() < AI.getValueAsString(); |
| 351 | return getKindAsString() < AI.getKindAsString(); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 354 | uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) { |
| 355 | // FIXME: Remove this. |
| 356 | switch (Val) { |
| 357 | case Attribute::EndAttrKinds: |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 358 | llvm_unreachable("Synthetic enumerators which should never get here"); |
| 359 | |
| 360 | case Attribute::None: return 0; |
| 361 | case Attribute::ZExt: return 1 << 0; |
| 362 | case Attribute::SExt: return 1 << 1; |
| 363 | case Attribute::NoReturn: return 1 << 2; |
| 364 | case Attribute::InReg: return 1 << 3; |
| 365 | case Attribute::StructRet: return 1 << 4; |
| 366 | case Attribute::NoUnwind: return 1 << 5; |
| 367 | case Attribute::NoAlias: return 1 << 6; |
| 368 | case Attribute::ByVal: return 1 << 7; |
| 369 | case Attribute::Nest: return 1 << 8; |
| 370 | case Attribute::ReadNone: return 1 << 9; |
| 371 | case Attribute::ReadOnly: return 1 << 10; |
| 372 | case Attribute::NoInline: return 1 << 11; |
| 373 | case Attribute::AlwaysInline: return 1 << 12; |
| 374 | case Attribute::OptimizeForSize: return 1 << 13; |
| 375 | case Attribute::StackProtect: return 1 << 14; |
| 376 | case Attribute::StackProtectReq: return 1 << 15; |
| 377 | case Attribute::Alignment: return 31 << 16; |
| 378 | case Attribute::NoCapture: return 1 << 21; |
| 379 | case Attribute::NoRedZone: return 1 << 22; |
| 380 | case Attribute::NoImplicitFloat: return 1 << 23; |
| 381 | case Attribute::Naked: return 1 << 24; |
| 382 | case Attribute::InlineHint: return 1 << 25; |
| 383 | case Attribute::StackAlignment: return 7 << 26; |
| 384 | case Attribute::ReturnsTwice: return 1 << 29; |
| 385 | case Attribute::UWTable: return 1 << 30; |
| 386 | case Attribute::NonLazyBind: return 1U << 31; |
| 387 | case Attribute::AddressSafety: return 1ULL << 32; |
| 388 | case Attribute::MinSize: return 1ULL << 33; |
| 389 | case Attribute::NoDuplicate: return 1ULL << 34; |
| 390 | case Attribute::StackProtectStrong: return 1ULL << 35; |
Kostya Serebryany | ab39afa | 2013-02-11 08:13:54 +0000 | [diff] [blame] | 391 | case Attribute::ThreadSafety: return 1ULL << 36; |
| 392 | case Attribute::UninitializedChecks: return 1ULL << 37; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 393 | } |
| 394 | llvm_unreachable("Unsupported attribute type"); |
| 395 | } |
| 396 | |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | // AttributeSetNode Definition |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
| 401 | AttributeSetNode *AttributeSetNode::get(LLVMContext &C, |
| 402 | ArrayRef<Attribute> Attrs) { |
| 403 | if (Attrs.empty()) |
| 404 | return 0; |
| 405 | |
| 406 | // Otherwise, build a key to look up the existing attributes. |
| 407 | LLVMContextImpl *pImpl = C.pImpl; |
| 408 | FoldingSetNodeID ID; |
| 409 | |
| 410 | SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end()); |
Bill Wendling | f107e6c | 2013-02-13 09:26:26 +0000 | [diff] [blame] | 411 | array_pod_sort(SortedAttrs.begin(), SortedAttrs.end()); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 412 | |
| 413 | for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(), |
| 414 | E = SortedAttrs.end(); I != E; ++I) |
| 415 | I->Profile(ID); |
| 416 | |
| 417 | void *InsertPoint; |
| 418 | AttributeSetNode *PA = |
| 419 | pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint); |
| 420 | |
| 421 | // If we didn't find any existing attributes of the same shape then create a |
| 422 | // new one and insert it. |
| 423 | if (!PA) { |
| 424 | PA = new AttributeSetNode(SortedAttrs); |
| 425 | pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint); |
| 426 | } |
| 427 | |
| 428 | // Return the AttributesListNode that we found or created. |
| 429 | return PA; |
| 430 | } |
| 431 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 432 | bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const { |
| 433 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 434 | E = AttrList.end(); I != E; ++I) |
NAKAMURA Takumi | eddab15 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 435 | if (I->hasAttribute(Kind)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 436 | return true; |
| 437 | return false; |
| 438 | } |
| 439 | |
Bill Wendling | 0e9d5d0 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 440 | bool AttributeSetNode::hasAttribute(StringRef Kind) const { |
| 441 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 442 | E = AttrList.end(); I != E; ++I) |
| 443 | if (I->hasAttribute(Kind)) |
| 444 | return true; |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const { |
| 449 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 450 | E = AttrList.end(); I != E; ++I) |
| 451 | if (I->hasAttribute(Kind)) |
| 452 | return *I; |
| 453 | return Attribute(); |
| 454 | } |
| 455 | |
| 456 | Attribute AttributeSetNode::getAttribute(StringRef Kind) const { |
| 457 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 458 | E = AttrList.end(); I != E; ++I) |
| 459 | if (I->hasAttribute(Kind)) |
| 460 | return *I; |
| 461 | return Attribute(); |
| 462 | } |
| 463 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 464 | unsigned AttributeSetNode::getAlignment() const { |
| 465 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 466 | E = AttrList.end(); I != E; ++I) |
NAKAMURA Takumi | eddab15 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 467 | if (I->hasAttribute(Attribute::Alignment)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 468 | return I->getAlignment(); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | unsigned AttributeSetNode::getStackAlignment() const { |
| 473 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
| 474 | E = AttrList.end(); I != E; ++I) |
NAKAMURA Takumi | eddab15 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 475 | if (I->hasAttribute(Attribute::StackAlignment)) |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 476 | return I->getStackAlignment(); |
| 477 | return 0; |
| 478 | } |
| 479 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 480 | std::string AttributeSetNode::getAsString(bool InAttrGrp) const { |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 481 | std::string Str = ""; |
| 482 | for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(), |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 483 | E = AttrList.end(); I != E; ) { |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 484 | Str += I->getAsString(InAttrGrp); |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 485 | if (++I != E) Str += " "; |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 486 | } |
| 487 | return Str; |
| 488 | } |
| 489 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 490 | //===----------------------------------------------------------------------===// |
| 491 | // AttributeSetImpl Definition |
| 492 | //===----------------------------------------------------------------------===// |
| 493 | |
| 494 | uint64_t AttributeSetImpl::Raw(uint64_t Index) const { |
| 495 | for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) { |
| 496 | if (getSlotIndex(I) != Index) continue; |
| 497 | const AttributeSetNode *ASN = AttrNodes[I].second; |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 498 | uint64_t Mask = 0; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 499 | |
| 500 | for (AttributeSetNode::const_iterator II = ASN->begin(), |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 501 | IE = ASN->end(); II != IE; ++II) { |
| 502 | Attribute Attr = *II; |
Bill Wendling | 2153691 | 2013-02-10 23:18:05 +0000 | [diff] [blame] | 503 | |
| 504 | // This cannot handle string attributes. |
| 505 | if (Attr.isStringAttribute()) continue; |
| 506 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 507 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 508 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 509 | if (Kind == Attribute::Alignment) |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 510 | Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 511 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 512 | Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26; |
| 513 | else |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 514 | Mask |= AttributeImpl::getAttrMask(Kind); |
Bill Wendling | fca0ed2 | 2013-02-02 00:52:44 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return Mask; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | //===----------------------------------------------------------------------===// |
| 524 | // AttributeSet Construction and Mutation Methods |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | |
Bill Wendling | 8232ece | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 527 | AttributeSet |
| 528 | AttributeSet::getImpl(LLVMContext &C, |
| 529 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) { |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 530 | LLVMContextImpl *pImpl = C.pImpl; |
| 531 | FoldingSetNodeID ID; |
| 532 | AttributeSetImpl::Profile(ID, Attrs); |
| 533 | |
| 534 | void *InsertPoint; |
| 535 | AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint); |
| 536 | |
| 537 | // If we didn't find any existing attributes of the same shape then |
| 538 | // create a new one and insert it. |
| 539 | if (!PA) { |
| 540 | PA = new AttributeSetImpl(C, Attrs); |
| 541 | pImpl->AttrsLists.InsertNode(PA, InsertPoint); |
| 542 | } |
| 543 | |
| 544 | // Return the AttributesList that we found or created. |
| 545 | return AttributeSet(PA); |
| 546 | } |
| 547 | |
| 548 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 549 | ArrayRef<std::pair<unsigned, Attribute> > Attrs){ |
| 550 | // If there are no attributes then return a null AttributesList pointer. |
| 551 | if (Attrs.empty()) |
| 552 | return AttributeSet(); |
| 553 | |
| 554 | #ifndef NDEBUG |
| 555 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 556 | assert((!i || Attrs[i-1].first <= Attrs[i].first) && |
| 557 | "Misordered Attributes list!"); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 558 | assert(!Attrs[i].second.hasAttribute(Attribute::None) && |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 559 | "Pointless attribute!"); |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes |
| 564 | // list. |
| 565 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec; |
| 566 | for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(), |
| 567 | E = Attrs.end(); I != E; ) { |
| 568 | unsigned Index = I->first; |
| 569 | SmallVector<Attribute, 4> AttrVec; |
NAKAMURA Takumi | 3ba51ce | 2013-01-29 15:18:16 +0000 | [diff] [blame] | 570 | while (I != E && I->first == Index) { |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 571 | AttrVec.push_back(I->second); |
| 572 | ++I; |
| 573 | } |
| 574 | |
| 575 | AttrPairVec.push_back(std::make_pair(Index, |
| 576 | AttributeSetNode::get(C, AttrVec))); |
| 577 | } |
| 578 | |
| 579 | return getImpl(C, AttrPairVec); |
| 580 | } |
| 581 | |
| 582 | AttributeSet AttributeSet::get(LLVMContext &C, |
| 583 | ArrayRef<std::pair<unsigned, |
| 584 | AttributeSetNode*> > Attrs) { |
| 585 | // If there are no attributes then return a null AttributesList pointer. |
| 586 | if (Attrs.empty()) |
| 587 | return AttributeSet(); |
| 588 | |
| 589 | return getImpl(C, Attrs); |
| 590 | } |
| 591 | |
| 592 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) { |
| 593 | if (!B.hasAttributes()) |
| 594 | return AttributeSet(); |
Bill Wendling | 8fbc0c2 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 595 | |
Bill Wendling | 64754f4 | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 596 | // Add target-independent attributes. |
Bill Wendling | 8fbc0c2 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 597 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 598 | for (Attribute::AttrKind Kind = Attribute::None; |
| 599 | Kind != Attribute::EndAttrKinds; ++Kind) { |
| 600 | if (!B.contains(Kind)) |
| 601 | continue; |
| 602 | |
Bill Wendling | 8fbc0c2 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 603 | if (Kind == Attribute::Alignment) |
| 604 | Attrs.push_back(std::make_pair(Idx, Attribute:: |
| 605 | getWithAlignment(C, B.getAlignment()))); |
| 606 | else if (Kind == Attribute::StackAlignment) |
| 607 | Attrs.push_back(std::make_pair(Idx, Attribute:: |
| 608 | getWithStackAlignment(C, B.getStackAlignment()))); |
| 609 | else |
| 610 | Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind))); |
| 611 | } |
| 612 | |
Bill Wendling | 64754f4 | 2013-02-05 23:48:36 +0000 | [diff] [blame] | 613 | // Add target-dependent (string) attributes. |
| 614 | for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end(); |
| 615 | I != E; ++I) |
| 616 | Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second))); |
| 617 | |
Bill Wendling | 8fbc0c2 | 2013-01-29 01:02:03 +0000 | [diff] [blame] | 618 | return get(C, Attrs); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, |
| 622 | ArrayRef<Attribute::AttrKind> Kind) { |
| 623 | SmallVector<std::pair<unsigned, Attribute>, 8> Attrs; |
| 624 | for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(), |
| 625 | E = Kind.end(); I != E; ++I) |
| 626 | Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I))); |
| 627 | return get(C, Attrs); |
| 628 | } |
| 629 | |
| 630 | AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) { |
| 631 | if (Attrs.empty()) return AttributeSet(); |
| 632 | |
| 633 | SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec; |
| 634 | for (unsigned I = 0, E = Attrs.size(); I != E; ++I) { |
| 635 | AttributeSet AS = Attrs[I]; |
| 636 | if (!AS.pImpl) continue; |
| 637 | AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end()); |
| 638 | } |
| 639 | |
| 640 | return getImpl(C, AttrNodeVec); |
| 641 | } |
| 642 | |
| 643 | AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx, |
| 644 | Attribute::AttrKind Attr) const { |
| 645 | return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr)); |
| 646 | } |
| 647 | |
| 648 | AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx, |
| 649 | AttributeSet Attrs) const { |
| 650 | if (!pImpl) return Attrs; |
| 651 | if (!Attrs.pImpl) return *this; |
| 652 | |
| 653 | #ifndef NDEBUG |
| 654 | // FIXME it is not obvious how this should work for alignment. For now, say |
| 655 | // we can't change a known alignment. |
| 656 | unsigned OldAlign = getParamAlignment(Idx); |
| 657 | unsigned NewAlign = Attrs.getParamAlignment(Idx); |
| 658 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
| 659 | "Attempt to change alignment!"); |
| 660 | #endif |
| 661 | |
| 662 | // Add the attribute slots before the one we're trying to add. |
| 663 | SmallVector<AttributeSet, 4> AttrSet; |
| 664 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 665 | AttributeSet AS; |
| 666 | uint64_t LastIndex = 0; |
| 667 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 668 | if (getSlotIndex(I) >= Idx) { |
| 669 | if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++); |
| 670 | break; |
| 671 | } |
| 672 | LastIndex = I + 1; |
| 673 | AttrSet.push_back(getSlotAttributes(I)); |
| 674 | } |
| 675 | |
| 676 | // Now add the attribute into the correct slot. There may already be an |
| 677 | // AttributeSet there. |
| 678 | AttrBuilder B(AS, Idx); |
| 679 | |
| 680 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
| 681 | if (Attrs.getSlotIndex(I) == Idx) { |
| 682 | for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I), |
| 683 | IE = Attrs.pImpl->end(I); II != IE; ++II) |
Bill Wendling | 39da078 | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 684 | B.addAttribute(*II); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | |
| 688 | AttrSet.push_back(AttributeSet::get(C, Idx, B)); |
| 689 | |
| 690 | // Add the remaining attribute slots. |
| 691 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 692 | AttrSet.push_back(getSlotAttributes(I)); |
| 693 | |
| 694 | return get(C, AttrSet); |
| 695 | } |
| 696 | |
| 697 | AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx, |
| 698 | Attribute::AttrKind Attr) const { |
| 699 | return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr)); |
| 700 | } |
| 701 | |
| 702 | AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx, |
| 703 | AttributeSet Attrs) const { |
| 704 | if (!pImpl) return AttributeSet(); |
| 705 | if (!Attrs.pImpl) return *this; |
| 706 | |
| 707 | #ifndef NDEBUG |
| 708 | // FIXME it is not obvious how this should work for alignment. |
| 709 | // For now, say we can't pass in alignment, which no current use does. |
| 710 | assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) && |
| 711 | "Attempt to change alignment!"); |
| 712 | #endif |
| 713 | |
| 714 | // Add the attribute slots before the one we're trying to add. |
| 715 | SmallVector<AttributeSet, 4> AttrSet; |
| 716 | uint64_t NumAttrs = pImpl->getNumAttributes(); |
| 717 | AttributeSet AS; |
| 718 | uint64_t LastIndex = 0; |
| 719 | for (unsigned I = 0, E = NumAttrs; I != E; ++I) { |
| 720 | if (getSlotIndex(I) >= Idx) { |
| 721 | if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++); |
| 722 | break; |
| 723 | } |
| 724 | LastIndex = I + 1; |
| 725 | AttrSet.push_back(getSlotAttributes(I)); |
| 726 | } |
| 727 | |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 728 | // Now remove the attribute from the correct slot. There may already be an |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 729 | // AttributeSet there. |
| 730 | AttrBuilder B(AS, Idx); |
| 731 | |
| 732 | for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I) |
| 733 | if (Attrs.getSlotIndex(I) == Idx) { |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 734 | B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
| 738 | AttrSet.push_back(AttributeSet::get(C, Idx, B)); |
| 739 | |
| 740 | // Add the remaining attribute slots. |
| 741 | for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I) |
| 742 | AttrSet.push_back(getSlotAttributes(I)); |
| 743 | |
| 744 | return get(C, AttrSet); |
| 745 | } |
| 746 | |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | // AttributeSet Accessor Methods |
| 749 | //===----------------------------------------------------------------------===// |
| 750 | |
Bill Wendling | 85b3fbe | 2013-02-10 05:00:40 +0000 | [diff] [blame] | 751 | LLVMContext &AttributeSet::getContext() const { |
| 752 | return pImpl->getContext(); |
| 753 | } |
| 754 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 755 | AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const { |
| 756 | return pImpl && hasAttributes(Idx) ? |
| 757 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 758 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 759 | std::make_pair(Idx, getAttributes(Idx)))) : |
| 760 | AttributeSet(); |
| 761 | } |
| 762 | |
| 763 | AttributeSet AttributeSet::getRetAttributes() const { |
| 764 | return pImpl && hasAttributes(ReturnIndex) ? |
| 765 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 766 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 767 | std::make_pair(ReturnIndex, |
| 768 | getAttributes(ReturnIndex)))) : |
| 769 | AttributeSet(); |
| 770 | } |
| 771 | |
| 772 | AttributeSet AttributeSet::getFnAttributes() const { |
| 773 | return pImpl && hasAttributes(FunctionIndex) ? |
| 774 | AttributeSet::get(pImpl->getContext(), |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 775 | ArrayRef<std::pair<unsigned, AttributeSetNode*> >( |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 776 | std::make_pair(FunctionIndex, |
| 777 | getAttributes(FunctionIndex)))) : |
| 778 | AttributeSet(); |
| 779 | } |
| 780 | |
| 781 | bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{ |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 782 | AttributeSetNode *ASN = getAttributes(Index); |
| 783 | return ASN ? ASN->hasAttribute(Kind) : false; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Bill Wendling | 0e9d5d0 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 786 | bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const { |
| 787 | AttributeSetNode *ASN = getAttributes(Index); |
| 788 | return ASN ? ASN->hasAttribute(Kind) : false; |
| 789 | } |
| 790 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 791 | bool AttributeSet::hasAttributes(unsigned Index) const { |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 792 | AttributeSetNode *ASN = getAttributes(Index); |
| 793 | return ASN ? ASN->hasAttributes() : false; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /// \brief Return true if the specified attribute is set for at least one |
| 797 | /// parameter or for the return value. |
| 798 | bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const { |
| 799 | if (pImpl == 0) return false; |
| 800 | |
| 801 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) |
| 802 | for (AttributeSetImpl::const_iterator II = pImpl->begin(I), |
| 803 | IE = pImpl->end(I); II != IE; ++II) |
NAKAMURA Takumi | eddab15 | 2013-01-31 03:47:28 +0000 | [diff] [blame] | 804 | if (II->hasAttribute(Attr)) |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 805 | return true; |
| 806 | |
| 807 | return false; |
| 808 | } |
| 809 | |
Bill Wendling | 0e9d5d0 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 810 | Attribute AttributeSet::getAttribute(unsigned Index, |
| 811 | Attribute::AttrKind Kind) const { |
| 812 | AttributeSetNode *ASN = getAttributes(Index); |
| 813 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 814 | } |
| 815 | |
| 816 | Attribute AttributeSet::getAttribute(unsigned Index, |
| 817 | StringRef Kind) const { |
| 818 | AttributeSetNode *ASN = getAttributes(Index); |
| 819 | return ASN ? ASN->getAttribute(Kind) : Attribute(); |
| 820 | } |
| 821 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 822 | unsigned AttributeSet::getParamAlignment(unsigned Index) const { |
| 823 | AttributeSetNode *ASN = getAttributes(Index); |
| 824 | return ASN ? ASN->getAlignment() : 0; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | unsigned AttributeSet::getStackAlignment(unsigned Index) const { |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 828 | AttributeSetNode *ASN = getAttributes(Index); |
| 829 | return ASN ? ASN->getStackAlignment() : 0; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 832 | std::string AttributeSet::getAsString(unsigned Index, |
| 833 | bool InAttrGrp) const { |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 834 | AttributeSetNode *ASN = getAttributes(Index); |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 835 | return ASN ? ASN->getAsString(InAttrGrp) : std::string(""); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /// \brief The attributes for the specified index are returned. |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 839 | AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const { |
| 840 | if (!pImpl) return 0; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 841 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 842 | // Loop through to find the attribute node we want. |
| 843 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) |
| 844 | if (pImpl->getSlotIndex(I) == Idx) |
| 845 | return pImpl->getSlotNode(I); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 846 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 847 | return 0; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Bill Wendling | f715dbd | 2013-02-01 00:48:14 +0000 | [diff] [blame] | 850 | AttributeSet::iterator AttributeSet::begin(unsigned Idx) const { |
Bill Wendling | 16c4b3c | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 851 | if (!pImpl) |
| 852 | return ArrayRef<Attribute>().begin(); |
| 853 | return pImpl->begin(Idx); |
| 854 | } |
| 855 | |
Bill Wendling | f715dbd | 2013-02-01 00:48:14 +0000 | [diff] [blame] | 856 | AttributeSet::iterator AttributeSet::end(unsigned Idx) const { |
Bill Wendling | 16c4b3c | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 857 | if (!pImpl) |
| 858 | return ArrayRef<Attribute>().end(); |
Bill Wendling | 30d2c76 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 859 | return pImpl->end(Idx); |
Bill Wendling | 16c4b3c | 2013-01-31 23:53:05 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 862 | //===----------------------------------------------------------------------===// |
| 863 | // AttributeSet Introspection Methods |
| 864 | //===----------------------------------------------------------------------===// |
| 865 | |
| 866 | /// \brief Return the number of slots used in this attribute list. This is the |
| 867 | /// number of arguments that have an attribute set on them (including the |
| 868 | /// function itself). |
| 869 | unsigned AttributeSet::getNumSlots() const { |
| 870 | return pImpl ? pImpl->getNumAttributes() : 0; |
| 871 | } |
| 872 | |
| 873 | uint64_t AttributeSet::getSlotIndex(unsigned Slot) const { |
| 874 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 875 | "Slot # out of range!"); |
| 876 | return pImpl->getSlotIndex(Slot); |
| 877 | } |
| 878 | |
| 879 | AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const { |
| 880 | assert(pImpl && Slot < pImpl->getNumAttributes() && |
| 881 | "Slot # out of range!"); |
| 882 | return pImpl->getSlotAttributes(Slot); |
| 883 | } |
| 884 | |
| 885 | uint64_t AttributeSet::Raw(unsigned Index) const { |
| 886 | // FIXME: Remove this. |
| 887 | return pImpl ? pImpl->Raw(Index) : 0; |
| 888 | } |
| 889 | |
| 890 | void AttributeSet::dump() const { |
| 891 | dbgs() << "PAL[\n"; |
| 892 | |
| 893 | for (unsigned i = 0, e = getNumSlots(); i < e; ++i) { |
| 894 | uint64_t Index = getSlotIndex(i); |
| 895 | dbgs() << " { "; |
| 896 | if (Index == ~0U) |
| 897 | dbgs() << "~0U"; |
| 898 | else |
| 899 | dbgs() << Index; |
| 900 | dbgs() << " => " << getAsString(Index) << " }\n"; |
| 901 | } |
| 902 | |
| 903 | dbgs() << "]\n"; |
| 904 | } |
| 905 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 906 | //===----------------------------------------------------------------------===// |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 907 | // AttrBuilder Method Implementations |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 908 | //===----------------------------------------------------------------------===// |
| 909 | |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 910 | AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx) |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 911 | : Attrs(0), Alignment(0), StackAlignment(0) { |
Bill Wendling | ec25898 | 2013-01-27 21:23:46 +0000 | [diff] [blame] | 912 | AttributeSetImpl *pImpl = AS.pImpl; |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 913 | if (!pImpl) return; |
| 914 | |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 915 | for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) { |
| 916 | if (pImpl->getSlotIndex(I) != Idx) continue; |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 917 | |
Bill Wendling | 383da6b | 2013-01-30 21:22:59 +0000 | [diff] [blame] | 918 | for (AttributeSetImpl::const_iterator II = pImpl->begin(I), |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 919 | IE = pImpl->end(I); II != IE; ++II) |
Bill Wendling | 39da078 | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 920 | addAttribute(*II); |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 921 | |
| 922 | break; |
| 923 | } |
Bill Wendling | a90a99a | 2013-01-07 08:24:35 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 926 | void AttrBuilder::clear() { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 927 | Attrs = 0; |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 928 | Alignment = StackAlignment = 0; |
| 929 | } |
| 930 | |
| 931 | AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 932 | assert((unsigned)Val < 64 && Val < Attribute::EndAttrKinds && |
| 933 | "Attribute out of range!"); |
Bill Wendling | 169d527 | 2013-01-31 23:16:25 +0000 | [diff] [blame] | 934 | assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment && |
| 935 | "Adding alignment attribute without adding alignment value!"); |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 936 | Attrs |= 1ULL << Val; |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 937 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Bill Wendling | 39da078 | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 940 | AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) { |
Bill Wendling | 09ed910 | 2013-02-10 10:13:23 +0000 | [diff] [blame] | 941 | if (Attr.isStringAttribute()) { |
| 942 | addAttribute(Attr.getKindAsString(), Attr.getValueAsString()); |
| 943 | return *this; |
| 944 | } |
| 945 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 946 | Attribute::AttrKind Kind = Attr.getKindAsEnum(); |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 947 | Attrs |= 1ULL << Kind; |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 948 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 949 | if (Kind == Attribute::Alignment) |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 950 | Alignment = Attr.getAlignment(); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 951 | else if (Kind == Attribute::StackAlignment) |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 952 | StackAlignment = Attr.getStackAlignment(); |
| 953 | return *this; |
| 954 | } |
| 955 | |
Bill Wendling | ea59f89 | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 956 | AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) { |
| 957 | TargetDepAttrs[A] = V; |
| 958 | return *this; |
| 959 | } |
| 960 | |
Bill Wendling | 39da078 | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 961 | AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 962 | assert((unsigned)Val < 64 && Val < Attribute::EndAttrKinds && |
| 963 | "Attribute out of range!"); |
| 964 | Attrs &= ~(1ULL << Val); |
Bill Wendling | 39da078 | 2013-01-31 23:38:01 +0000 | [diff] [blame] | 965 | |
| 966 | if (Val == Attribute::Alignment) |
| 967 | Alignment = 0; |
| 968 | else if (Val == Attribute::StackAlignment) |
| 969 | StackAlignment = 0; |
| 970 | |
| 971 | return *this; |
| 972 | } |
| 973 | |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 974 | AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) { |
Bill Wendling | 30d2c76 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 975 | unsigned Idx = ~0U; |
| 976 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 977 | if (A.getSlotIndex(I) == Index) { |
| 978 | Idx = I; |
| 979 | break; |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 980 | } |
Bill Wendling | 30d2c76 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 981 | |
| 982 | assert(Idx != ~0U && "Couldn't find index in AttributeSet!"); |
| 983 | |
| 984 | for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) { |
Bill Wendling | 74fe825 | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 985 | Attribute Attr = *I; |
| 986 | if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) { |
| 987 | Attribute::AttrKind Kind = I->getKindAsEnum(); |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 988 | Attrs &= ~(1ULL << Kind); |
Bill Wendling | 30d2c76 | 2013-02-01 00:13:50 +0000 | [diff] [blame] | 989 | |
Bill Wendling | 74fe825 | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 990 | if (Kind == Attribute::Alignment) |
| 991 | Alignment = 0; |
| 992 | else if (Kind == Attribute::StackAlignment) |
| 993 | StackAlignment = 0; |
| 994 | } else { |
| 995 | assert(Attr.isStringAttribute() && "Invalid attribute type!"); |
| 996 | std::map<std::string, std::string>::iterator |
| 997 | Iter = TargetDepAttrs.find(Attr.getKindAsString()); |
| 998 | if (Iter != TargetDepAttrs.end()) |
| 999 | TargetDepAttrs.erase(Iter); |
| 1000 | } |
Bill Wendling | 49f6060 | 2013-01-28 05:23:28 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | return *this; |
| 1004 | } |
| 1005 | |
Bill Wendling | ea59f89 | 2013-02-05 08:09:32 +0000 | [diff] [blame] | 1006 | AttrBuilder &AttrBuilder::removeAttribute(StringRef A) { |
| 1007 | std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A); |
| 1008 | if (I != TargetDepAttrs.end()) |
| 1009 | TargetDepAttrs.erase(I); |
| 1010 | return *this; |
| 1011 | } |
| 1012 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1013 | AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) { |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1014 | if (Align == 0) return *this; |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1015 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 1016 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1017 | assert(Align <= 0x40000000 && "Alignment too large."); |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1018 | |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1019 | Attrs |= 1ULL << Attribute::Alignment; |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1020 | Alignment = Align; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 1021 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1024 | AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) { |
| 1025 | // Default alignment, allow the target to define how to align it. |
| 1026 | if (Align == 0) return *this; |
| 1027 | |
| 1028 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 1029 | assert(Align <= 0x100 && "Alignment too large."); |
| 1030 | |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1031 | Attrs |= 1ULL << Attribute::StackAlignment; |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1032 | StackAlignment = Align; |
| 1033 | return *this; |
| 1034 | } |
| 1035 | |
Bill Wendling | 85df6b4 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1036 | AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) { |
| 1037 | // FIXME: What if both have alignments, but they don't match?! |
| 1038 | if (!Alignment) |
| 1039 | Alignment = B.Alignment; |
| 1040 | |
| 1041 | if (!StackAlignment) |
| 1042 | StackAlignment = B.StackAlignment; |
| 1043 | |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1044 | Attrs |= B.Attrs; |
Bill Wendling | 85df6b4 | 2013-02-06 01:16:00 +0000 | [diff] [blame] | 1045 | |
| 1046 | for (td_const_iterator I = B.TargetDepAttrs.begin(), |
| 1047 | E = B.TargetDepAttrs.end(); I != E; ++I) |
| 1048 | TargetDepAttrs[I->first] = I->second; |
| 1049 | |
| 1050 | return *this; |
| 1051 | } |
| 1052 | |
Bill Wendling | c342d9d | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1053 | bool AttrBuilder::contains(StringRef A) const { |
| 1054 | return TargetDepAttrs.find(A) != TargetDepAttrs.end(); |
| 1055 | } |
| 1056 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1057 | bool AttrBuilder::hasAttributes() const { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1058 | return Attrs != 0 || !TargetDepAttrs.empty(); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1059 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1060 | |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 1061 | bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const { |
Bill Wendling | bdcbccc | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1062 | unsigned Idx = ~0U; |
| 1063 | for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I) |
| 1064 | if (A.getSlotIndex(I) == Index) { |
| 1065 | Idx = I; |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | assert(Idx != ~0U && "Couldn't find the index!"); |
| 1070 | |
| 1071 | for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); |
Bill Wendling | 74fe825 | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1072 | I != E; ++I) { |
| 1073 | Attribute Attr = *I; |
| 1074 | if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1075 | if (Attrs & (1ULL << I->getKindAsEnum())) |
Bill Wendling | 74fe825 | 2013-02-12 07:56:49 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } else { |
| 1078 | assert(Attr.isStringAttribute() && "Invalid attribute kind!"); |
| 1079 | return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end(); |
| 1080 | } |
| 1081 | } |
Bill Wendling | bdcbccc | 2013-02-02 00:42:06 +0000 | [diff] [blame] | 1082 | |
| 1083 | return false; |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1084 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 1085 | |
Bill Wendling | 702cc91 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 1086 | bool AttrBuilder::hasAlignmentAttr() const { |
Bill Wendling | 0319888 | 2013-01-04 23:27:34 +0000 | [diff] [blame] | 1087 | return Alignment != 0; |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1090 | bool AttrBuilder::operator==(const AttrBuilder &B) { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1091 | if (Attrs != B.Attrs) |
| 1092 | return false; |
Bill Wendling | c342d9d | 2013-02-06 01:33:42 +0000 | [diff] [blame] | 1093 | |
| 1094 | for (td_const_iterator I = TargetDepAttrs.begin(), |
| 1095 | E = TargetDepAttrs.end(); I != E; ++I) |
| 1096 | if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end()) |
| 1097 | return false; |
| 1098 | |
| 1099 | return Alignment == B.Alignment && StackAlignment == B.StackAlignment; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) { |
Bill Wendling | f9271ea | 2013-02-04 23:32:23 +0000 | [diff] [blame] | 1103 | // FIXME: Remove this in 4.0. |
Bill Wendling | 8232ece | 2013-01-29 01:43:29 +0000 | [diff] [blame] | 1104 | if (!Val) return *this; |
| 1105 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1106 | for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| 1107 | I = Attribute::AttrKind(I + 1)) { |
| 1108 | if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) { |
Benjamin Kramer | c835b8c | 2013-02-16 19:13:18 +0000 | [diff] [blame^] | 1109 | Attrs |= 1ULL << I; |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (I == Attribute::Alignment) |
| 1112 | Alignment = 1ULL << ((A >> 16) - 1); |
| 1113 | else if (I == Attribute::StackAlignment) |
| 1114 | StackAlignment = 1ULL << ((A >> 26)-1); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | return *this; |
| 1119 | } |
| 1120 | |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1121 | //===----------------------------------------------------------------------===// |
| 1122 | // AttributeFuncs Function Defintions |
| 1123 | //===----------------------------------------------------------------------===// |
| 1124 | |
Bill Wendling | 7beee28 | 2013-02-01 01:04:27 +0000 | [diff] [blame] | 1125 | /// \brief Which attributes cannot be applied to a type. |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 1126 | AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) { |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1127 | AttrBuilder Incompatible; |
| 1128 | |
| 1129 | if (!Ty->isIntegerTy()) |
| 1130 | // Attribute that only apply to integers. |
| 1131 | Incompatible.addAttribute(Attribute::SExt) |
| 1132 | .addAttribute(Attribute::ZExt); |
| 1133 | |
| 1134 | if (!Ty->isPointerTy()) |
| 1135 | // Attribute that only apply to pointers. |
| 1136 | Incompatible.addAttribute(Attribute::ByVal) |
| 1137 | .addAttribute(Attribute::Nest) |
| 1138 | .addAttribute(Attribute::NoAlias) |
| 1139 | .addAttribute(Attribute::NoCapture) |
| 1140 | .addAttribute(Attribute::StructRet); |
| 1141 | |
Bill Wendling | e743654 | 2013-01-30 23:07:40 +0000 | [diff] [blame] | 1142 | return AttributeSet::get(Ty->getContext(), Index, Incompatible); |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 1143 | } |