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