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