Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10 | // This file implements the AttributesList class and Attribute utilities. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 14 | #include "llvm/Attributes.h" |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Dan Gohman | fc42961 | 2008-03-10 23:55:07 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Atomic.h" |
| 20 | #include "llvm/Support/Mutex.h" |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Benjamin Kramer | 1a25d73 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 27 | // Attributes Implementation |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | d0e1f10 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 29 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 30 | Attributes::Attributes(uint64_t Val) : Attrs(Val) {} |
| 31 | |
| 32 | Attributes::Attributes(Attribute::AttrConst Val) : Attrs(Val.v) {} |
| 33 | |
| 34 | Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {} |
| 35 | |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame^] | 36 | Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {} |
| 37 | |
Bill Wendling | 68d2401 | 2012-10-08 22:20:14 +0000 | [diff] [blame] | 38 | // FIXME: This is temporary until we have implemented the uniquified version of |
| 39 | // AttributesImpl. |
| 40 | Attributes Attributes::get(Attributes::Builder &B) { |
| 41 | return Attributes(B.Bits); |
| 42 | } |
| 43 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 44 | Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) { |
| 45 | // If there are no attributes, return an empty Attributes class. |
| 46 | if (B.Bits == 0) |
| 47 | return Attributes(); |
| 48 | |
| 49 | // Otherwise, build a key to look up the existing attributes. |
| 50 | LLVMContextImpl *pImpl = Context.pImpl; |
| 51 | FoldingSetNodeID ID; |
| 52 | ID.AddInteger(B.Bits); |
| 53 | |
| 54 | void *InsertPoint; |
| 55 | AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 56 | |
| 57 | if (!PA) { |
| 58 | // If we didn't find any existing attributes of the same shape then create a |
| 59 | // new one and insert it. |
| 60 | PA = new AttributesImpl(B.Bits); |
| 61 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 62 | } |
| 63 | |
| 64 | // Return the AttributesList that we found or created. |
| 65 | return Attributes(PA); |
| 66 | } |
| 67 | |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 68 | bool Attributes::hasAttributes(const Attributes &A) const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 69 | return Attrs.hasAttributes(A); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 70 | } |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 71 | bool Attributes::hasAddressSafetyAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 72 | return Attrs.hasAttribute(Attribute::AddressSafety_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 73 | } |
| 74 | bool Attributes::hasAlignmentAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 75 | return Attrs.hasAttribute(Attribute::Alignment_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 76 | } |
| 77 | bool Attributes::hasAlwaysInlineAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 78 | return Attrs.hasAttribute(Attribute::AlwaysInline_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 79 | } |
| 80 | bool Attributes::hasByValAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 81 | return Attrs.hasAttribute(Attribute::ByVal_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 82 | } |
| 83 | bool Attributes::hasInlineHintAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 84 | return Attrs.hasAttribute(Attribute::InlineHint_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 85 | } |
| 86 | bool Attributes::hasInRegAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 87 | return Attrs.hasAttribute(Attribute::InReg_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 88 | } |
| 89 | bool Attributes::hasNakedAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 90 | return Attrs.hasAttribute(Attribute::Naked_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 91 | } |
| 92 | bool Attributes::hasNestAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 93 | return Attrs.hasAttribute(Attribute::Nest_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 94 | } |
| 95 | bool Attributes::hasNoAliasAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 96 | return Attrs.hasAttribute(Attribute::NoAlias_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 97 | } |
| 98 | bool Attributes::hasNoCaptureAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 99 | return Attrs.hasAttribute(Attribute::NoCapture_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 100 | } |
| 101 | bool Attributes::hasNoImplicitFloatAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 102 | return Attrs.hasAttribute(Attribute::NoImplicitFloat_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 103 | } |
| 104 | bool Attributes::hasNoInlineAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 105 | return Attrs.hasAttribute(Attribute::NoInline_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 106 | } |
| 107 | bool Attributes::hasNonLazyBindAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 108 | return Attrs.hasAttribute(Attribute::NonLazyBind_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 109 | } |
| 110 | bool Attributes::hasNoRedZoneAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 111 | return Attrs.hasAttribute(Attribute::NoRedZone_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 112 | } |
| 113 | bool Attributes::hasNoReturnAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 114 | return Attrs.hasAttribute(Attribute::NoReturn_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 115 | } |
| 116 | bool Attributes::hasNoUnwindAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 117 | return Attrs.hasAttribute(Attribute::NoUnwind_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 118 | } |
| 119 | bool Attributes::hasOptimizeForSizeAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 120 | return Attrs.hasAttribute(Attribute::OptimizeForSize_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 121 | } |
| 122 | bool Attributes::hasReadNoneAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 123 | return Attrs.hasAttribute(Attribute::ReadNone_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 124 | } |
| 125 | bool Attributes::hasReadOnlyAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 126 | return Attrs.hasAttribute(Attribute::ReadOnly_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 127 | } |
| 128 | bool Attributes::hasReturnsTwiceAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 129 | return Attrs.hasAttribute(Attribute::ReturnsTwice_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 130 | } |
| 131 | bool Attributes::hasSExtAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 132 | return Attrs.hasAttribute(Attribute::SExt_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 133 | } |
| 134 | bool Attributes::hasStackAlignmentAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 135 | return Attrs.hasAttribute(Attribute::StackAlignment_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 136 | } |
| 137 | bool Attributes::hasStackProtectAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 138 | return Attrs.hasAttribute(Attribute::StackProtect_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 139 | } |
| 140 | bool Attributes::hasStackProtectReqAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 141 | return Attrs.hasAttribute(Attribute::StackProtectReq_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 142 | } |
| 143 | bool Attributes::hasStructRetAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 144 | return Attrs.hasAttribute(Attribute::StructRet_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 145 | } |
| 146 | bool Attributes::hasUWTableAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 147 | return Attrs.hasAttribute(Attribute::UWTable_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 148 | } |
| 149 | bool Attributes::hasZExtAttr() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 150 | return Attrs.hasAttribute(Attribute::ZExt_i); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /// This returns the alignment field of an attribute as a byte alignment value. |
| 154 | unsigned Attributes::getAlignment() const { |
| 155 | if (!hasAlignmentAttr()) |
| 156 | return 0; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 157 | return 1U << ((Attrs.getAlignment() >> 16) - 1); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 161 | /// value. |
| 162 | unsigned Attributes::getStackAlignment() const { |
| 163 | if (!hasStackAlignmentAttr()) |
| 164 | return 0; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 165 | return 1U << ((Attrs.getStackAlignment() >> 26) - 1); |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 168 | bool Attributes::isEmptyOrSingleton() const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 169 | return Attrs.isEmptyOrSingleton(); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 172 | Attributes Attributes::operator | (const Attributes &A) const { |
| 173 | return Attributes(Raw() | A.Raw()); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 174 | } |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 175 | Attributes Attributes::operator & (const Attributes &A) const { |
| 176 | return Attributes(Raw() & A.Raw()); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 177 | } |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 178 | Attributes Attributes::operator ^ (const Attributes &A) const { |
| 179 | return Attributes(Raw() ^ A.Raw()); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 180 | } |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 181 | Attributes &Attributes::operator |= (const Attributes &A) { |
| 182 | Attrs.Bits |= A.Raw(); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 183 | return *this; |
| 184 | } |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 185 | Attributes &Attributes::operator &= (const Attributes &A) { |
| 186 | Attrs.Bits &= A.Raw(); |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 187 | return *this; |
| 188 | } |
| 189 | Attributes Attributes::operator ~ () const { |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 190 | return Attributes(~Raw()); |
| 191 | } |
| 192 | |
| 193 | uint64_t Attributes::Raw() const { |
| 194 | return Attrs.Bits; |
Bill Wendling | be7c6f2 | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 197 | Attributes Attributes::typeIncompatible(Type *Ty) { |
| 198 | Attributes::Builder Incompatible; |
| 199 | |
| 200 | if (!Ty->isIntegerTy()) { |
| 201 | // Attributes that only apply to integers. |
| 202 | Incompatible.addSExtAttr(); |
| 203 | Incompatible.addZExtAttr(); |
| 204 | } |
| 205 | |
| 206 | if (!Ty->isPointerTy()) { |
| 207 | // Attributes that only apply to pointers. |
| 208 | Incompatible.addByValAttr(); |
| 209 | Incompatible.addNestAttr(); |
| 210 | Incompatible.addNoAliasAttr(); |
| 211 | Incompatible.addNoCaptureAttr(); |
| 212 | Incompatible.addStructRetAttr(); |
| 213 | } |
| 214 | |
| 215 | return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get(). |
| 216 | } |
| 217 | |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 218 | std::string Attributes::getAsString() const { |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 219 | std::string Result; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 220 | if (hasZExtAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 221 | Result += "zeroext "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 222 | if (hasSExtAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 223 | Result += "signext "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 224 | if (hasNoReturnAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 225 | Result += "noreturn "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 226 | if (hasNoUnwindAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 227 | Result += "nounwind "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 228 | if (hasUWTableAttr()) |
Rafael Espindola | fc9bae6 | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 229 | Result += "uwtable "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 230 | if (hasReturnsTwiceAttr()) |
Rafael Espindola | cc349c8 | 2011-10-03 14:45:37 +0000 | [diff] [blame] | 231 | Result += "returns_twice "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 232 | if (hasInRegAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 233 | Result += "inreg "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 234 | if (hasNoAliasAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 235 | Result += "noalias "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 236 | if (hasNoCaptureAttr()) |
Nick Lewycky | 8d69f48 | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 237 | Result += "nocapture "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 238 | if (hasStructRetAttr()) |
Anton Korobeynikov | c8ce7b08 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 239 | Result += "sret "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 240 | if (hasByValAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 241 | Result += "byval "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 242 | if (hasNestAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 243 | Result += "nest "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 244 | if (hasReadNoneAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 245 | Result += "readnone "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 246 | if (hasReadOnlyAttr()) |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 247 | Result += "readonly "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 248 | if (hasOptimizeForSizeAttr()) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 249 | Result += "optsize "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 250 | if (hasNoInlineAttr()) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 251 | Result += "noinline "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 252 | if (hasInlineHintAttr()) |
Jakob Stoklund Olesen | 74bb06c | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 253 | Result += "inlinehint "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 254 | if (hasAlwaysInlineAttr()) |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 255 | Result += "alwaysinline "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 256 | if (hasStackProtectAttr()) |
Bill Wendling | ccb67a3d | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 257 | Result += "ssp "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 258 | if (hasStackProtectReqAttr()) |
Bill Wendling | ccb67a3d | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 259 | Result += "sspreq "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 260 | if (hasNoRedZoneAttr()) |
Devang Patel | 72a4d2f | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 261 | Result += "noredzone "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 262 | if (hasNoImplicitFloatAttr()) |
Devang Patel | d1c7d34 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 263 | Result += "noimplicitfloat "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 264 | if (hasNakedAttr()) |
Anton Korobeynikov | c8ce7b08 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 265 | Result += "naked "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 266 | if (hasNonLazyBindAttr()) |
John McCall | 4b7a8d6 | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 267 | Result += "nonlazybind "; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 268 | if (hasAddressSafetyAttr()) |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 269 | Result += "address_safety "; |
Bill Wendling | b4e211c | 2012-09-20 15:20:36 +0000 | [diff] [blame] | 270 | if (hasStackAlignmentAttr()) { |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 271 | Result += "alignstack("; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 272 | Result += utostr(getStackAlignment()); |
Charles Davis | be5557e | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 273 | Result += ") "; |
| 274 | } |
Bill Wendling | b4e211c | 2012-09-20 15:20:36 +0000 | [diff] [blame] | 275 | if (hasAlignmentAttr()) { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 276 | Result += "align "; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 277 | Result += utostr(getAlignment()); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 278 | Result += " "; |
| 279 | } |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 280 | // Trim the trailing space. |
Nick Lewycky | 8d69f48 | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 281 | assert(!Result.empty() && "Unknown attribute!"); |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 282 | Result.erase(Result.end()-1); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 283 | return Result; |
| 284 | } |
| 285 | |
Bill Wendling | abf3feb | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 286 | //===----------------------------------------------------------------------===// |
| 287 | // Attributes::Builder Implementation |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
| 290 | void Attributes::Builder::addAddressSafetyAttr() { |
| 291 | Bits |= Attribute::AddressSafety_i; |
| 292 | } |
| 293 | void Attributes::Builder::addAlwaysInlineAttr() { |
| 294 | Bits |= Attribute::AlwaysInline_i; |
| 295 | } |
| 296 | void Attributes::Builder::addByValAttr() { |
| 297 | Bits |= Attribute::ByVal_i; |
| 298 | } |
| 299 | void Attributes::Builder::addInlineHintAttr() { |
| 300 | Bits |= Attribute::InlineHint_i; |
| 301 | } |
| 302 | void Attributes::Builder::addInRegAttr() { |
| 303 | Bits |= Attribute::InReg_i; |
| 304 | } |
| 305 | void Attributes::Builder::addNakedAttr() { |
| 306 | Bits |= Attribute::Naked_i; |
| 307 | } |
| 308 | void Attributes::Builder::addNestAttr() { |
| 309 | Bits |= Attribute::Nest_i; |
| 310 | } |
| 311 | void Attributes::Builder::addNoAliasAttr() { |
| 312 | Bits |= Attribute::NoAlias_i; |
| 313 | } |
| 314 | void Attributes::Builder::addNoCaptureAttr() { |
| 315 | Bits |= Attribute::NoCapture_i; |
| 316 | } |
| 317 | void Attributes::Builder::addNoImplicitFloatAttr() { |
| 318 | Bits |= Attribute::NoImplicitFloat_i; |
| 319 | } |
| 320 | void Attributes::Builder::addNoInlineAttr() { |
| 321 | Bits |= Attribute::NoInline_i; |
| 322 | } |
| 323 | void Attributes::Builder::addNonLazyBindAttr() { |
| 324 | Bits |= Attribute::NonLazyBind_i; |
| 325 | } |
| 326 | void Attributes::Builder::addNoRedZoneAttr() { |
| 327 | Bits |= Attribute::NoRedZone_i; |
| 328 | } |
| 329 | void Attributes::Builder::addNoReturnAttr() { |
| 330 | Bits |= Attribute::NoReturn_i; |
| 331 | } |
| 332 | void Attributes::Builder::addNoUnwindAttr() { |
| 333 | Bits |= Attribute::NoUnwind_i; |
| 334 | } |
| 335 | void Attributes::Builder::addOptimizeForSizeAttr() { |
| 336 | Bits |= Attribute::OptimizeForSize_i; |
| 337 | } |
| 338 | void Attributes::Builder::addReadNoneAttr() { |
| 339 | Bits |= Attribute::ReadNone_i; |
| 340 | } |
| 341 | void Attributes::Builder::addReadOnlyAttr() { |
| 342 | Bits |= Attribute::ReadOnly_i; |
| 343 | } |
| 344 | void Attributes::Builder::addReturnsTwiceAttr() { |
| 345 | Bits |= Attribute::ReturnsTwice_i; |
| 346 | } |
| 347 | void Attributes::Builder::addSExtAttr() { |
| 348 | Bits |= Attribute::SExt_i; |
| 349 | } |
| 350 | void Attributes::Builder::addStackProtectAttr() { |
| 351 | Bits |= Attribute::StackProtect_i; |
| 352 | } |
| 353 | void Attributes::Builder::addStackProtectReqAttr() { |
| 354 | Bits |= Attribute::StackProtectReq_i; |
| 355 | } |
| 356 | void Attributes::Builder::addStructRetAttr() { |
| 357 | Bits |= Attribute::StructRet_i; |
| 358 | } |
| 359 | void Attributes::Builder::addUWTableAttr() { |
| 360 | Bits |= Attribute::UWTable_i; |
| 361 | } |
| 362 | void Attributes::Builder::addZExtAttr() { |
| 363 | Bits |= Attribute::ZExt_i; |
| 364 | } |
| 365 | |
| 366 | void Attributes::Builder::addAlignmentAttr(unsigned Align) { |
| 367 | if (Align == 0) return; |
| 368 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 369 | assert(Align <= 0x40000000 && "Alignment too large."); |
| 370 | Bits |= (Log2_32(Align) + 1) << 16; |
| 371 | } |
| 372 | void Attributes::Builder::addStackAlignmentAttr(unsigned Align) { |
| 373 | // Default alignment, allow the target to define how to align it. |
| 374 | if (Align == 0) return; |
| 375 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 376 | assert(Align <= 0x100 && "Alignment too large."); |
| 377 | Bits |= (Log2_32(Align) + 1) << 26; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 380 | void Attributes::Builder::removeAddressSafetyAttr() { |
| 381 | Bits &= ~Attribute::AddressSafety_i; |
| 382 | } |
| 383 | void Attributes::Builder::removeAlwaysInlineAttr() { |
| 384 | Bits &= ~Attribute::AlwaysInline_i; |
| 385 | } |
| 386 | void Attributes::Builder::removeByValAttr() { |
| 387 | Bits &= ~Attribute::ByVal_i; |
| 388 | } |
| 389 | void Attributes::Builder::removeInlineHintAttr() { |
| 390 | Bits &= ~Attribute::InlineHint_i; |
| 391 | } |
| 392 | void Attributes::Builder::removeInRegAttr() { |
| 393 | Bits &= ~Attribute::InReg_i; |
| 394 | } |
| 395 | void Attributes::Builder::removeNakedAttr() { |
| 396 | Bits &= ~Attribute::Naked_i; |
| 397 | } |
| 398 | void Attributes::Builder::removeNestAttr() { |
| 399 | Bits &= ~Attribute::Nest_i; |
| 400 | } |
| 401 | void Attributes::Builder::removeNoAliasAttr() { |
| 402 | Bits &= ~Attribute::NoAlias_i; |
| 403 | } |
| 404 | void Attributes::Builder::removeNoCaptureAttr() { |
| 405 | Bits &= ~Attribute::NoCapture_i; |
| 406 | } |
| 407 | void Attributes::Builder::removeNoImplicitFloatAttr() { |
| 408 | Bits &= ~Attribute::NoImplicitFloat_i; |
| 409 | } |
| 410 | void Attributes::Builder::removeNoInlineAttr() { |
| 411 | Bits &= ~Attribute::NoInline_i; |
| 412 | } |
| 413 | void Attributes::Builder::removeNonLazyBindAttr() { |
| 414 | Bits &= ~Attribute::NonLazyBind_i; |
| 415 | } |
| 416 | void Attributes::Builder::removeNoRedZoneAttr() { |
| 417 | Bits &= ~Attribute::NoRedZone_i; |
| 418 | } |
| 419 | void Attributes::Builder::removeNoReturnAttr() { |
| 420 | Bits &= ~Attribute::NoReturn_i; |
| 421 | } |
| 422 | void Attributes::Builder::removeNoUnwindAttr() { |
| 423 | Bits &= ~Attribute::NoUnwind_i; |
| 424 | } |
| 425 | void Attributes::Builder::removeOptimizeForSizeAttr() { |
| 426 | Bits &= ~Attribute::OptimizeForSize_i; |
| 427 | } |
| 428 | void Attributes::Builder::removeReadNoneAttr() { |
| 429 | Bits &= ~Attribute::ReadNone_i; |
| 430 | } |
| 431 | void Attributes::Builder::removeReadOnlyAttr() { |
| 432 | Bits &= ~Attribute::ReadOnly_i; |
| 433 | } |
| 434 | void Attributes::Builder::removeReturnsTwiceAttr() { |
| 435 | Bits &= ~Attribute::ReturnsTwice_i; |
| 436 | } |
| 437 | void Attributes::Builder::removeSExtAttr() { |
| 438 | Bits &= ~Attribute::SExt_i; |
| 439 | } |
| 440 | void Attributes::Builder::removeStackProtectAttr() { |
| 441 | Bits &= ~Attribute::StackProtect_i; |
| 442 | } |
| 443 | void Attributes::Builder::removeStackProtectReqAttr() { |
| 444 | Bits &= ~Attribute::StackProtectReq_i; |
| 445 | } |
| 446 | void Attributes::Builder::removeStructRetAttr() { |
| 447 | Bits &= ~Attribute::StructRet_i; |
| 448 | } |
| 449 | void Attributes::Builder::removeUWTableAttr() { |
| 450 | Bits &= ~Attribute::UWTable_i; |
| 451 | } |
| 452 | void Attributes::Builder::removeZExtAttr() { |
| 453 | Bits &= ~Attribute::ZExt_i; |
| 454 | } |
| 455 | |
Bill Wendling | c6daefa | 2012-10-08 23:27:46 +0000 | [diff] [blame^] | 456 | void Attributes::Builder::removeAlignmentAttr() { |
| 457 | Bits &= ~Attribute::Alignment_i; |
| 458 | } |
| 459 | void Attributes::Builder::removeStackAlignmentAttr() { |
| 460 | Bits &= ~Attribute::StackAlignment_i; |
| 461 | } |
| 462 | |
| 463 | bool Attributes::Builder::hasAttributes() const { |
| 464 | return Bits != 0; |
| 465 | } |
| 466 | bool Attributes::Builder::hasAlignmentAttr() const { |
| 467 | return Bits & Attribute::Alignment_i; |
| 468 | } |
| 469 | |
| 470 | uint64_t Attributes::Builder::getAlignment() const { |
| 471 | if (!hasAlignmentAttr()) |
| 472 | return 0; |
| 473 | return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1); |
| 474 | } |
| 475 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 476 | //===----------------------------------------------------------------------===// |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 477 | // AttributeImpl Definition |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 480 | bool AttributesImpl::hasAttribute(uint64_t A) const { |
| 481 | return (Bits & A) != 0; |
| 482 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 483 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 484 | bool AttributesImpl::hasAttributes() const { |
| 485 | return Bits != 0; |
| 486 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 487 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 488 | bool AttributesImpl::hasAttributes(const Attributes &A) const { |
| 489 | return Bits & A.Raw(); // FIXME: Raw() won't work here in the future. |
| 490 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 491 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 492 | uint64_t AttributesImpl::getAlignment() const { |
| 493 | return Bits & Attribute::Alignment_i; |
| 494 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 495 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 496 | uint64_t AttributesImpl::getStackAlignment() const { |
| 497 | return Bits & Attribute::StackAlignment_i; |
| 498 | } |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 499 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 500 | bool AttributesImpl::isEmptyOrSingleton() const { |
| 501 | return (Bits & (Bits - 1)) == 0; |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | //===----------------------------------------------------------------------===// |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 505 | // AttributeListImpl Definition |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 506 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 507 | |
Owen Anderson | 2e83189 | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 508 | namespace llvm { |
| 509 | class AttributeListImpl; |
| 510 | } |
| 511 | |
| 512 | static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists; |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 513 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 514 | namespace llvm { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 515 | static ManagedStatic<sys::SmartMutex<true> > ALMutex; |
| 516 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 517 | class AttributeListImpl : public FoldingSetNode { |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 518 | sys::cas_flag RefCount; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 519 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 520 | // AttributesList is uniqued, these should not be publicly available. |
Craig Topper | b1d83e8 | 2012-09-18 02:01:41 +0000 | [diff] [blame] | 521 | void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
| 522 | AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 523 | ~AttributeListImpl(); // Private implementation |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 524 | public: |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 525 | SmallVector<AttributeWithIndex, 4> Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 527 | AttributeListImpl(ArrayRef<AttributeWithIndex> attrs) |
| 528 | : Attrs(attrs.begin(), attrs.end()) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 529 | RefCount = 0; |
| 530 | } |
| 531 | |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 532 | void AddRef() { |
| 533 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 534 | ++RefCount; |
| 535 | } |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 536 | void DropRef() { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 537 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Owen Anderson | 2e83189 | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 538 | if (!AttributesLists.isConstructed()) |
| 539 | return; |
Owen Anderson | 91bfeb1 | 2010-11-09 17:47:10 +0000 | [diff] [blame] | 540 | sys::cas_flag new_val = --RefCount; |
Owen Anderson | 2d33543 | 2010-11-09 17:46:38 +0000 | [diff] [blame] | 541 | if (new_val == 0) |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 542 | delete this; |
Owen Anderson | c1a3a47 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 544 | |
| 545 | void Profile(FoldingSetNodeID &ID) const { |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 546 | Profile(ID, Attrs); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 548 | static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){ |
| 549 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 550 | ID.AddInteger(Attrs[i].Attrs.Raw()); |
| 551 | ID.AddInteger(Attrs[i].Index); |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 552 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 553 | } |
| 554 | }; |
| 555 | } |
| 556 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 557 | AttributeListImpl::~AttributeListImpl() { |
Owen Anderson | 9b14a25 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 558 | // NOTE: Lock must be acquired by caller. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 559 | AttributesLists->RemoveNode(this); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 563 | AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 564 | // If there are no attributes then return a null AttributesList pointer. |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 565 | if (Attrs.empty()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 566 | return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 568 | #ifndef NDEBUG |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 569 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 570 | assert(Attrs[i].Attrs.hasAttributes() && |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 571 | "Pointless attribute!"); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 572 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 573 | "Misordered AttributesList!"); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 574 | } |
| 575 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 577 | // Otherwise, build a key to look up the existing attributes. |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 578 | FoldingSetNodeID ID; |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 579 | AttributeListImpl::Profile(ID, Attrs); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 580 | void *InsertPos; |
Owen Anderson | d91e6b0 | 2009-08-17 17:10:58 +0000 | [diff] [blame] | 581 | |
| 582 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 583 | |
Devang Patel | 0009505 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 584 | AttributeListImpl *PAL = |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 585 | AttributesLists->FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 587 | // If we didn't find any existing attributes of the same shape then |
| 588 | // create a new one and insert it. |
| 589 | if (!PAL) { |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 590 | PAL = new AttributeListImpl(Attrs); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 591 | AttributesLists->InsertNode(PAL, InsertPos); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 593 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 594 | // Return the AttributesList that we found or created. |
| 595 | return AttrListPtr(PAL); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 599 | //===----------------------------------------------------------------------===// |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 600 | // AttrListPtr Method Implementations |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 601 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 602 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 603 | AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 604 | if (LI) LI->AddRef(); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 607 | AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) { |
| 608 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 611 | const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) { |
Owen Anderson | 8dc0b04 | 2010-09-16 00:27:35 +0000 | [diff] [blame] | 612 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 613 | if (AttrList == RHS.AttrList) return *this; |
| 614 | if (AttrList) AttrList->DropRef(); |
| 615 | AttrList = RHS.AttrList; |
| 616 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 617 | return *this; |
| 618 | } |
| 619 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 620 | AttrListPtr::~AttrListPtr() { |
| 621 | if (AttrList) AttrList->DropRef(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /// getNumSlots - Return the number of slots used in this attribute list. |
| 625 | /// This is the number of arguments that have an attribute set on them |
| 626 | /// (including the function itself). |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 627 | unsigned AttrListPtr::getNumSlots() const { |
| 628 | return AttrList ? AttrList->Attrs.size() : 0; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 631 | /// getSlot - Return the AttributeWithIndex at the specified slot. This |
| 632 | /// holds a number plus a set of attributes. |
| 633 | const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const { |
| 634 | assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!"); |
| 635 | return AttrList->Attrs[Slot]; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 639 | /// getAttributes - The attributes for the specified index are |
| 640 | /// returned. Attributes for the result are denoted with Idx = 0. |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 641 | /// Function notes are denoted with idx = ~0. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 642 | Attributes AttrListPtr::getAttributes(unsigned Idx) const { |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 643 | if (AttrList == 0) return Attributes(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 644 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 645 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 646 | for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i) |
| 647 | if (Attrs[i].Index == Idx) |
| 648 | return Attrs[i].Attrs; |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 649 | |
| 650 | return Attributes(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 654 | /// least one parameter or for the return value. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 655 | bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const { |
| 656 | if (AttrList == 0) return false; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 657 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 658 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 659 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) |
Bill Wendling | b4e211c | 2012-09-20 15:20:36 +0000 | [diff] [blame] | 660 | if (Attrs[i].Attrs.hasAttributes(Attr)) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 661 | return true; |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 666 | AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const { |
| 667 | Attributes OldAttrs = getAttributes(Idx); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 668 | #ifndef NDEBUG |
| 669 | // FIXME it is not obvious how this should work for alignment. |
| 670 | // For now, say we can't change a known alignment. |
Bill Wendling | 9be7759 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 671 | unsigned OldAlign = OldAttrs.getAlignment(); |
| 672 | unsigned NewAlign = Attrs.getAlignment(); |
Anton Korobeynikov | 18991d7 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 673 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 674 | "Attempt to change alignment!"); |
| 675 | #endif |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 676 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 677 | Attributes NewAttrs = OldAttrs | Attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 678 | if (NewAttrs == OldAttrs) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 679 | return *this; |
| 680 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 681 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 682 | if (AttrList == 0) |
| 683 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 684 | else { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 685 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 686 | unsigned i = 0, e = OldAttrList.size(); |
| 687 | // Copy attributes for arguments before this one. |
| 688 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 689 | NewAttrList.push_back(OldAttrList[i]); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 691 | // If there are attributes already at this index, merge them in. |
| 692 | if (i != e && OldAttrList[i].Index == Idx) { |
| 693 | Attrs |= OldAttrList[i].Attrs; |
| 694 | ++i; |
| 695 | } |
| 696 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 697 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 698 | |
| 699 | // Copy attributes for arguments after this one. |
| 700 | NewAttrList.insert(NewAttrList.end(), |
| 701 | OldAttrList.begin()+i, OldAttrList.end()); |
| 702 | } |
| 703 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 704 | return get(NewAttrList); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 707 | AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const { |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 708 | #ifndef NDEBUG |
| 709 | // FIXME it is not obvious how this should work for alignment. |
| 710 | // For now, say we can't pass in alignment, which no current use does. |
Bill Wendling | b4e211c | 2012-09-20 15:20:36 +0000 | [diff] [blame] | 711 | assert(!Attrs.hasAlignmentAttr() && "Attempt to exclude alignment!"); |
Dale Johannesen | 11a555e | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 712 | #endif |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 713 | if (AttrList == 0) return AttrListPtr(); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 714 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 715 | Attributes OldAttrs = getAttributes(Idx); |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 716 | Attributes NewAttrs = OldAttrs & ~Attrs; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 717 | if (NewAttrs == OldAttrs) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 718 | return *this; |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 719 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 720 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 721 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 722 | unsigned i = 0, e = OldAttrList.size(); |
| 723 | |
| 724 | // Copy attributes for arguments before this one. |
| 725 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 726 | NewAttrList.push_back(OldAttrList[i]); |
| 727 | |
| 728 | // If there are attributes already at this index, merge them in. |
| 729 | assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); |
| 730 | Attrs = OldAttrList[i].Attrs & ~Attrs; |
| 731 | ++i; |
| 732 | if (Attrs) // If any attributes left for this parameter, add them. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 733 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 734 | |
| 735 | // Copy attributes for arguments after this one. |
| 736 | NewAttrList.insert(NewAttrList.end(), |
| 737 | OldAttrList.begin()+i, OldAttrList.end()); |
| 738 | |
Chris Lattner | 3cb6f83 | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 739 | return get(NewAttrList); |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 742 | void AttrListPtr::dump() const { |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 743 | dbgs() << "PAL[ "; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 744 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 745 | const AttributeWithIndex &PAWI = getSlot(i); |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 746 | dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} "; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 747 | } |
| 748 | |
David Greene | f701473 | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 749 | dbgs() << "]\n"; |
Duncan Sands | 404eb05 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 750 | } |