Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1 | //===-- Attributes.cpp - Implement AttributesList -------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10 | // This file implements the AttributesList class and Attribute utilities. |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 14 | #include "llvm/Attributes.h" |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Dan Gohman | ac9dff6 | 2008-03-10 23:55:07 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Atomic.h" |
| 20 | #include "llvm/Support/Mutex.h" |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 27 | // Attributes Implementation |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | fabfde3 | 2008-01-03 00:10:22 +0000 | [diff] [blame] | 29 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 30 | Attributes::Attributes(uint64_t Val) : Attrs(Val) {} |
| 31 | |
Bill Wendling | 96a2794 | 2012-10-11 01:10:00 +0000 | [diff] [blame] | 32 | Attributes::Attributes(LLVMContext &C, AttrVal Val) |
Bill Wendling | 3183951 | 2012-10-11 01:05:52 +0000 | [diff] [blame] | 33 | : Attrs(Attributes::get(Attributes::Builder().addAttribute(Val)).Attrs) {} |
| 34 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 35 | Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {} |
| 36 | |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 37 | Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {} |
| 38 | |
Bill Wendling | 0327244 | 2012-10-08 22:20:14 +0000 | [diff] [blame] | 39 | // FIXME: This is temporary until we have implemented the uniquified version of |
| 40 | // AttributesImpl. |
| 41 | Attributes Attributes::get(Attributes::Builder &B) { |
| 42 | return Attributes(B.Bits); |
| 43 | } |
| 44 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 45 | Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) { |
| 46 | // If there are no attributes, return an empty Attributes class. |
| 47 | if (B.Bits == 0) |
| 48 | return Attributes(); |
| 49 | |
| 50 | // Otherwise, build a key to look up the existing attributes. |
| 51 | LLVMContextImpl *pImpl = Context.pImpl; |
| 52 | FoldingSetNodeID ID; |
| 53 | ID.AddInteger(B.Bits); |
| 54 | |
| 55 | void *InsertPoint; |
| 56 | AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint); |
| 57 | |
| 58 | if (!PA) { |
| 59 | // If we didn't find any existing attributes of the same shape then create a |
| 60 | // new one and insert it. |
| 61 | PA = new AttributesImpl(B.Bits); |
| 62 | pImpl->AttrsSet.InsertNode(PA, InsertPoint); |
| 63 | } |
| 64 | |
| 65 | // Return the AttributesList that we found or created. |
| 66 | return Attributes(PA); |
| 67 | } |
| 68 | |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 69 | bool Attributes::hasAttribute(AttrVal Val) const { |
| 70 | return Attrs.hasAttribute(Val); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 73 | bool Attributes::hasAttributes(const Attributes &A) const { |
| 74 | return Attrs.hasAttributes(A); |
| 75 | } |
| 76 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 77 | /// This returns the alignment field of an attribute as a byte alignment value. |
| 78 | unsigned Attributes::getAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 79 | if (!hasAttribute(Attributes::Alignment)) |
| 80 | return 0; |
| 81 | return 1U << ((Attrs.getAlignment() >> 16) - 1); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// This returns the stack alignment field of an attribute as a byte alignment |
| 85 | /// value. |
| 86 | unsigned Attributes::getStackAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 87 | if (!hasAttribute(Attributes::StackAlignment)) |
| 88 | return 0; |
| 89 | return 1U << ((Attrs.getStackAlignment() >> 26) - 1); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 92 | bool Attributes::isEmptyOrSingleton() const { |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 93 | return Attrs.isEmptyOrSingleton(); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 96 | Attributes Attributes::operator | (const Attributes &A) const { |
| 97 | return Attributes(Raw() | A.Raw()); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 98 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 99 | Attributes Attributes::operator & (const Attributes &A) const { |
| 100 | return Attributes(Raw() & A.Raw()); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 101 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 102 | Attributes Attributes::operator ^ (const Attributes &A) const { |
| 103 | return Attributes(Raw() ^ A.Raw()); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 104 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 105 | Attributes &Attributes::operator |= (const Attributes &A) { |
| 106 | Attrs.Bits |= A.Raw(); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 107 | return *this; |
| 108 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 109 | Attributes &Attributes::operator &= (const Attributes &A) { |
| 110 | Attrs.Bits &= A.Raw(); |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 111 | return *this; |
| 112 | } |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 113 | |
| 114 | uint64_t Attributes::Raw() const { |
| 115 | return Attrs.Bits; |
Bill Wendling | b10c88f | 2012-10-07 08:55:05 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 118 | Attributes Attributes::typeIncompatible(Type *Ty) { |
| 119 | Attributes::Builder Incompatible; |
| 120 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 121 | if (!Ty->isIntegerTy()) |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 122 | // Attributes that only apply to integers. |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 123 | Incompatible.addAttribute(Attributes::SExt) |
| 124 | .addAttribute(Attributes::ZExt); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 125 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 126 | if (!Ty->isPointerTy()) |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 127 | // Attributes that only apply to pointers. |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 128 | Incompatible.addAttribute(Attributes::ByVal) |
| 129 | .addAttribute(Attributes::Nest) |
| 130 | .addAttribute(Attributes::NoAlias) |
| 131 | .addAttribute(Attributes::NoCapture) |
| 132 | .addAttribute(Attributes::StructRet); |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 133 | |
| 134 | return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get(). |
| 135 | } |
| 136 | |
Bill Wendling | 8ce1e43 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 137 | std::string Attributes::getAsString() const { |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 138 | std::string Result; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 139 | if (hasAttribute(Attributes::ZExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 140 | Result += "zeroext "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 141 | if (hasAttribute(Attributes::SExt)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 142 | Result += "signext "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 143 | if (hasAttribute(Attributes::NoReturn)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 144 | Result += "noreturn "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 145 | if (hasAttribute(Attributes::NoUnwind)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 146 | Result += "nounwind "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 147 | if (hasAttribute(Attributes::UWTable)) |
Rafael Espindola | fc2bb8c | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 148 | Result += "uwtable "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 149 | if (hasAttribute(Attributes::ReturnsTwice)) |
Rafael Espindola | 25456ef | 2011-10-03 14:45:37 +0000 | [diff] [blame] | 150 | Result += "returns_twice "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 151 | if (hasAttribute(Attributes::InReg)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 152 | Result += "inreg "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 153 | if (hasAttribute(Attributes::NoAlias)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 154 | Result += "noalias "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 155 | if (hasAttribute(Attributes::NoCapture)) |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 156 | Result += "nocapture "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 157 | if (hasAttribute(Attributes::StructRet)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 158 | Result += "sret "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 159 | if (hasAttribute(Attributes::ByVal)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 160 | Result += "byval "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 161 | if (hasAttribute(Attributes::Nest)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 162 | Result += "nest "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 163 | if (hasAttribute(Attributes::ReadNone)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 164 | Result += "readnone "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 165 | if (hasAttribute(Attributes::ReadOnly)) |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 166 | Result += "readonly "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 167 | if (hasAttribute(Attributes::OptimizeForSize)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 168 | Result += "optsize "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 169 | if (hasAttribute(Attributes::NoInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 170 | Result += "noinline "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 171 | if (hasAttribute(Attributes::InlineHint)) |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 172 | Result += "inlinehint "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 173 | if (hasAttribute(Attributes::AlwaysInline)) |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 174 | Result += "alwaysinline "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 175 | if (hasAttribute(Attributes::StackProtect)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 176 | Result += "ssp "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 177 | if (hasAttribute(Attributes::StackProtectReq)) |
Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 178 | Result += "sspreq "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 179 | if (hasAttribute(Attributes::NoRedZone)) |
Devang Patel | d18e31a | 2009-06-04 22:05:33 +0000 | [diff] [blame] | 180 | Result += "noredzone "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 181 | if (hasAttribute(Attributes::NoImplicitFloat)) |
Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 182 | Result += "noimplicitfloat "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 183 | if (hasAttribute(Attributes::Naked)) |
Anton Korobeynikov | c5ec8a7 | 2009-07-17 18:07:26 +0000 | [diff] [blame] | 184 | Result += "naked "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 185 | if (hasAttribute(Attributes::NonLazyBind)) |
John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 186 | Result += "nonlazybind "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 187 | if (hasAttribute(Attributes::AddressSafety)) |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 188 | Result += "address_safety "; |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 189 | if (hasAttribute(Attributes::StackAlignment)) { |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 190 | Result += "alignstack("; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 191 | Result += utostr(getStackAlignment()); |
Charles Davis | 1e063d1 | 2010-02-12 00:31:15 +0000 | [diff] [blame] | 192 | Result += ") "; |
| 193 | } |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 194 | if (hasAttribute(Attributes::Alignment)) { |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 195 | Result += "align "; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 196 | Result += utostr(getAlignment()); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 197 | Result += " "; |
| 198 | } |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 199 | // Trim the trailing space. |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 200 | assert(!Result.empty() && "Unknown attribute!"); |
Dan Gohman | c3be0fd | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 201 | Result.erase(Result.end()-1); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 202 | return Result; |
| 203 | } |
| 204 | |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | // Attributes::Builder Implementation |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 209 | Attributes::Builder &Attributes::Builder:: |
| 210 | addAttribute(Attributes::AttrVal Val) { |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 211 | Bits |= AttributesImpl::getAttrMask(Val); |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 212 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Bill Wendling | a19a530 | 2012-10-14 04:10:01 +0000 | [diff] [blame] | 215 | Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) { |
| 216 | Bits |= Val; |
| 217 | return *this; |
| 218 | } |
| 219 | |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 220 | Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) { |
| 221 | if (Align == 0) return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 222 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 223 | assert(Align <= 0x40000000 && "Alignment too large."); |
| 224 | Bits |= (Log2_32(Align) + 1) << 16; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 225 | return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 226 | } |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 227 | Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){ |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 228 | // Default alignment, allow the target to define how to align it. |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 229 | if (Align == 0) return *this; |
Bill Wendling | e66f3d3 | 2012-10-05 06:44:41 +0000 | [diff] [blame] | 230 | assert(isPowerOf2_32(Align) && "Alignment must be a power of two."); |
| 231 | assert(Align <= 0x100 && "Alignment too large."); |
| 232 | Bits |= (Log2_32(Align) + 1) << 26; |
Bill Wendling | da3f9d8 | 2012-10-14 03:58:29 +0000 | [diff] [blame] | 233 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 236 | Attributes::Builder &Attributes::Builder:: |
| 237 | removeAttribute(Attributes::AttrVal Val) { |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 238 | Bits &= ~AttributesImpl::getAttrMask(Val); |
Bill Wendling | 3a106e6 | 2012-10-09 19:01:18 +0000 | [diff] [blame] | 239 | return *this; |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame^] | 242 | Attributes::Builder &Attributes::Builder:: |
| 243 | removeAttributes(const Attributes &A) { |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 244 | Bits &= ~A.Raw(); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame^] | 245 | return *this; |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 248 | bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const { |
| 249 | return Bits & AttributesImpl::getAttrMask(A); |
| 250 | } |
| 251 | |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 252 | bool Attributes::Builder::hasAttributes() const { |
| 253 | return Bits != 0; |
| 254 | } |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 255 | bool Attributes::Builder::hasAttributes(const Attributes &A) const { |
| 256 | return Bits & A.Raw(); |
| 257 | } |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 258 | bool Attributes::Builder::hasAlignmentAttr() const { |
Bill Wendling | 6b73e8a | 2012-10-09 20:28:54 +0000 | [diff] [blame] | 259 | return Bits & AttributesImpl::getAttrMask(Attributes::Alignment); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | uint64_t Attributes::Builder::getAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 263 | if (!hasAlignmentAttr()) |
| 264 | return 0; |
Bill Wendling | 6b73e8a | 2012-10-09 20:28:54 +0000 | [diff] [blame] | 265 | return 1U << |
| 266 | (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1); |
Bill Wendling | f385f4c | 2012-10-08 23:27:46 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 269 | uint64_t Attributes::Builder::getStackAlignment() const { |
| 270 | if (!hasAlignmentAttr()) |
| 271 | return 0; |
| 272 | return 1U << |
| 273 | (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1); |
| 274 | } |
| 275 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 276 | //===----------------------------------------------------------------------===// |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 277 | // AttributeImpl Definition |
| 278 | //===----------------------------------------------------------------------===// |
| 279 | |
Bill Wendling | 2e879bc | 2012-10-09 09:11:20 +0000 | [diff] [blame] | 280 | uint64_t AttributesImpl::getAttrMask(uint64_t Val) { |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 281 | switch (Val) { |
| 282 | case Attributes::None: return 0; |
| 283 | case Attributes::ZExt: return 1 << 0; |
| 284 | case Attributes::SExt: return 1 << 1; |
| 285 | case Attributes::NoReturn: return 1 << 2; |
| 286 | case Attributes::InReg: return 1 << 3; |
| 287 | case Attributes::StructRet: return 1 << 4; |
| 288 | case Attributes::NoUnwind: return 1 << 5; |
| 289 | case Attributes::NoAlias: return 1 << 6; |
| 290 | case Attributes::ByVal: return 1 << 7; |
| 291 | case Attributes::Nest: return 1 << 8; |
| 292 | case Attributes::ReadNone: return 1 << 9; |
| 293 | case Attributes::ReadOnly: return 1 << 10; |
| 294 | case Attributes::NoInline: return 1 << 11; |
| 295 | case Attributes::AlwaysInline: return 1 << 12; |
| 296 | case Attributes::OptimizeForSize: return 1 << 13; |
| 297 | case Attributes::StackProtect: return 1 << 14; |
| 298 | case Attributes::StackProtectReq: return 1 << 15; |
| 299 | case Attributes::Alignment: return 31 << 16; |
| 300 | case Attributes::NoCapture: return 1 << 21; |
| 301 | case Attributes::NoRedZone: return 1 << 22; |
| 302 | case Attributes::NoImplicitFloat: return 1 << 23; |
| 303 | case Attributes::Naked: return 1 << 24; |
| 304 | case Attributes::InlineHint: return 1 << 25; |
| 305 | case Attributes::StackAlignment: return 7 << 26; |
| 306 | case Attributes::ReturnsTwice: return 1 << 29; |
| 307 | case Attributes::UWTable: return 1 << 30; |
| 308 | case Attributes::NonLazyBind: return 1U << 31; |
| 309 | case Attributes::AddressSafety: return 1ULL << 32; |
| 310 | } |
| 311 | llvm_unreachable("Unsupported attribute type"); |
| 312 | } |
| 313 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 314 | bool AttributesImpl::hasAttribute(uint64_t A) const { |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 315 | return (Bits & getAttrMask(A)) != 0; |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 316 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 317 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 318 | bool AttributesImpl::hasAttributes() const { |
| 319 | return Bits != 0; |
| 320 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 321 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 322 | bool AttributesImpl::hasAttributes(const Attributes &A) const { |
| 323 | return Bits & A.Raw(); // FIXME: Raw() won't work here in the future. |
| 324 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 325 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 326 | uint64_t AttributesImpl::getAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 327 | return Bits & getAttrMask(Attributes::Alignment); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 328 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 329 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 330 | uint64_t AttributesImpl::getStackAlignment() const { |
Bill Wendling | 9ef99c9 | 2012-10-09 20:56:48 +0000 | [diff] [blame] | 331 | return Bits & getAttrMask(Attributes::StackAlignment); |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 332 | } |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 333 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 334 | bool AttributesImpl::isEmptyOrSingleton() const { |
| 335 | return (Bits & (Bits - 1)) == 0; |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | //===----------------------------------------------------------------------===// |
Devang Patel | 1e48000 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 339 | // AttributeListImpl Definition |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 340 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 341 | |
Owen Anderson | 9fe010e | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 342 | namespace llvm { |
| 343 | class AttributeListImpl; |
| 344 | } |
| 345 | |
| 346 | static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists; |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 348 | namespace llvm { |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 349 | static ManagedStatic<sys::SmartMutex<true> > ALMutex; |
| 350 | |
Devang Patel | 1e48000 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 351 | class AttributeListImpl : public FoldingSetNode { |
Owen Anderson | 72689b4 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 352 | sys::cas_flag RefCount; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 353 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 354 | // AttributesList is uniqued, these should not be publicly available. |
Craig Topper | c2945e4 | 2012-09-18 02:01:41 +0000 | [diff] [blame] | 355 | void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
| 356 | AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION; |
Devang Patel | 1e48000 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 357 | ~AttributeListImpl(); // Private implementation |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 358 | public: |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 359 | SmallVector<AttributeWithIndex, 4> Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 360 | |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 361 | AttributeListImpl(ArrayRef<AttributeWithIndex> attrs) |
| 362 | : Attrs(attrs.begin(), attrs.end()) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 363 | RefCount = 0; |
| 364 | } |
| 365 | |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 366 | void AddRef() { |
| 367 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 368 | ++RefCount; |
| 369 | } |
Owen Anderson | 72689b4 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 370 | void DropRef() { |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 371 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Owen Anderson | 9fe010e | 2010-11-18 18:59:13 +0000 | [diff] [blame] | 372 | if (!AttributesLists.isConstructed()) |
| 373 | return; |
Owen Anderson | 80f3d78 | 2010-11-09 17:47:10 +0000 | [diff] [blame] | 374 | sys::cas_flag new_val = --RefCount; |
Owen Anderson | d35b1a2 | 2010-11-09 17:46:38 +0000 | [diff] [blame] | 375 | if (new_val == 0) |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 376 | delete this; |
Owen Anderson | 72689b4 | 2009-08-20 19:03:20 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 378 | |
| 379 | void Profile(FoldingSetNodeID &ID) const { |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 380 | Profile(ID, Attrs); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 382 | static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){ |
| 383 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
| 384 | ID.AddInteger(Attrs[i].Attrs.Raw()); |
| 385 | ID.AddInteger(Attrs[i].Index); |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 387 | } |
| 388 | }; |
| 389 | } |
| 390 | |
Devang Patel | 1e48000 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 391 | AttributeListImpl::~AttributeListImpl() { |
Owen Anderson | 3afb024 | 2010-11-09 00:27:03 +0000 | [diff] [blame] | 392 | // NOTE: Lock must be acquired by caller. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 393 | AttributesLists->RemoveNode(this); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 397 | AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 398 | // If there are no attributes then return a null AttributesList pointer. |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 399 | if (Attrs.empty()) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 400 | return AttrListPtr(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 402 | #ifndef NDEBUG |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 404 | assert(Attrs[i].Attrs.hasAttributes() && |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 405 | "Pointless attribute!"); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 406 | assert((!i || Attrs[i-1].Index < Attrs[i].Index) && |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 407 | "Misordered AttributesList!"); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 408 | } |
| 409 | #endif |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 411 | // Otherwise, build a key to look up the existing attributes. |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 412 | FoldingSetNodeID ID; |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 413 | AttributeListImpl::Profile(ID, Attrs); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 414 | void *InsertPos; |
Owen Anderson | 596c1dc | 2009-08-17 17:10:58 +0000 | [diff] [blame] | 415 | |
| 416 | sys::SmartScopedLock<true> Lock(*ALMutex); |
| 417 | |
Devang Patel | 1e48000 | 2008-09-24 00:29:49 +0000 | [diff] [blame] | 418 | AttributeListImpl *PAL = |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 419 | AttributesLists->FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 421 | // If we didn't find any existing attributes of the same shape then |
| 422 | // create a new one and insert it. |
| 423 | if (!PAL) { |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 424 | PAL = new AttributeListImpl(Attrs); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 425 | AttributesLists->InsertNode(PAL, InsertPos); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 427 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 428 | // Return the AttributesList that we found or created. |
| 429 | return AttrListPtr(PAL); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 434 | // AttrListPtr Method Implementations |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 435 | //===----------------------------------------------------------------------===// |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 436 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 437 | AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 438 | if (LI) LI->AddRef(); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 441 | AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) { |
| 442 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 445 | const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) { |
Owen Anderson | cb86def | 2010-09-16 00:27:35 +0000 | [diff] [blame] | 446 | sys::SmartScopedLock<true> Lock(*ALMutex); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 447 | if (AttrList == RHS.AttrList) return *this; |
| 448 | if (AttrList) AttrList->DropRef(); |
| 449 | AttrList = RHS.AttrList; |
| 450 | if (AttrList) AttrList->AddRef(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 451 | return *this; |
| 452 | } |
| 453 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 454 | AttrListPtr::~AttrListPtr() { |
| 455 | if (AttrList) AttrList->DropRef(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// getNumSlots - Return the number of slots used in this attribute list. |
| 459 | /// This is the number of arguments that have an attribute set on them |
| 460 | /// (including the function itself). |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 461 | unsigned AttrListPtr::getNumSlots() const { |
| 462 | return AttrList ? AttrList->Attrs.size() : 0; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 465 | /// getSlot - Return the AttributeWithIndex at the specified slot. This |
| 466 | /// holds a number plus a set of attributes. |
| 467 | const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const { |
| 468 | assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!"); |
| 469 | return AttrList->Attrs[Slot]; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 473 | /// getAttributes - The attributes for the specified index are |
| 474 | /// returned. Attributes for the result are denoted with Idx = 0. |
Devang Patel | d9b4a5f | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 475 | /// Function notes are denoted with idx = ~0. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 476 | Attributes AttrListPtr::getAttributes(unsigned Idx) const { |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 477 | if (AttrList == 0) return Attributes(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 478 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 479 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 480 | for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i) |
| 481 | if (Attrs[i].Index == Idx) |
| 482 | return Attrs[i].Attrs; |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 483 | |
| 484 | return Attributes(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /// hasAttrSomewhere - Return true if the specified attribute is set for at |
| 488 | /// least one parameter or for the return value. |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 489 | bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 490 | if (AttrList == 0) return false; |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 491 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 492 | const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 493 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 494 | if (Attrs[i].Attrs.hasAttribute(Attr)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 495 | return true; |
| 496 | return false; |
| 497 | } |
| 498 | |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 499 | unsigned AttrListPtr::getNumAttrs() const { |
| 500 | return AttrList ? AttrList->Attrs.size() : 0; |
| 501 | } |
| 502 | |
| 503 | Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const { |
| 504 | assert(AttrList && "Trying to get an attribute from an empty list!"); |
| 505 | assert(i < AttrList->Attrs.size() && "Index out of range!"); |
| 506 | return AttrList->Attrs[i].Attrs; |
| 507 | } |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 508 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 509 | AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const { |
| 510 | Attributes OldAttrs = getAttributes(Idx); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 511 | #ifndef NDEBUG |
| 512 | // FIXME it is not obvious how this should work for alignment. |
| 513 | // For now, say we can't change a known alignment. |
Bill Wendling | ef99fe8 | 2012-09-21 15:26:31 +0000 | [diff] [blame] | 514 | unsigned OldAlign = OldAttrs.getAlignment(); |
| 515 | unsigned NewAlign = Attrs.getAlignment(); |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 516 | assert((!OldAlign || !NewAlign || OldAlign == NewAlign) && |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 517 | "Attempt to change alignment!"); |
| 518 | #endif |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 519 | |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 520 | Attributes NewAttrs = OldAttrs | Attrs; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 521 | if (NewAttrs == OldAttrs) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 522 | return *this; |
| 523 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 524 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 525 | if (AttrList == 0) |
| 526 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 527 | else { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 528 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 529 | unsigned i = 0, e = OldAttrList.size(); |
| 530 | // Copy attributes for arguments before this one. |
| 531 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 532 | NewAttrList.push_back(OldAttrList[i]); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 534 | // If there are attributes already at this index, merge them in. |
| 535 | if (i != e && OldAttrList[i].Index == Idx) { |
| 536 | Attrs |= OldAttrList[i].Attrs; |
| 537 | ++i; |
| 538 | } |
| 539 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 540 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 541 | |
| 542 | // Copy attributes for arguments after this one. |
| 543 | NewAttrList.insert(NewAttrList.end(), |
| 544 | OldAttrList.begin()+i, OldAttrList.end()); |
| 545 | } |
| 546 | |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 547 | return get(NewAttrList); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame^] | 550 | AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx, |
| 551 | Attributes Attrs) const { |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 552 | #ifndef NDEBUG |
| 553 | // FIXME it is not obvious how this should work for alignment. |
| 554 | // For now, say we can't pass in alignment, which no current use does. |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 555 | assert(!Attrs.hasAttribute(Attributes::Alignment) && |
| 556 | "Attempt to exclude alignment!"); |
Dale Johannesen | 6167c3f | 2008-02-19 23:51:49 +0000 | [diff] [blame] | 557 | #endif |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 558 | if (AttrList == 0) return AttrListPtr(); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 559 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 560 | Attributes OldAttrs = getAttributes(Idx); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame^] | 561 | Attributes::Builder NewAttrs = |
| 562 | Attributes::Builder(OldAttrs).removeAttributes(Attrs); |
| 563 | if (NewAttrs == Attributes::Builder(OldAttrs)) |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 564 | return *this; |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 565 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 566 | SmallVector<AttributeWithIndex, 8> NewAttrList; |
| 567 | const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 568 | unsigned i = 0, e = OldAttrList.size(); |
| 569 | |
| 570 | // Copy attributes for arguments before this one. |
| 571 | for (; i != e && OldAttrList[i].Index < Idx; ++i) |
| 572 | NewAttrList.push_back(OldAttrList[i]); |
| 573 | |
| 574 | // If there are attributes already at this index, merge them in. |
| 575 | assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame^] | 576 | Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs). |
| 577 | removeAttributes(Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 578 | ++i; |
| 579 | if (Attrs) // If any attributes left for this parameter, add them. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 580 | NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs)); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 581 | |
| 582 | // Copy attributes for arguments after this one. |
| 583 | NewAttrList.insert(NewAttrList.end(), |
| 584 | OldAttrList.begin()+i, OldAttrList.end()); |
| 585 | |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 586 | return get(NewAttrList); |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 589 | void AttrListPtr::dump() const { |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 590 | dbgs() << "PAL[ "; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 591 | for (unsigned i = 0; i < getNumSlots(); ++i) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 592 | const AttributeWithIndex &PAWI = getSlot(i); |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 593 | dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} "; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 594 | } |
| 595 | |
David Greene | ef1894e | 2010-01-05 01:29:58 +0000 | [diff] [blame] | 596 | dbgs() << "]\n"; |
Duncan Sands | ad9a9e1 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 597 | } |