blob: 45972635cb04dee9cf15206dab37d4332a06f7df [file] [log] [blame]
Devang Patel4c758ea2008-09-25 21:00:45 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
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 Patel4c758ea2008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Patelba3fa6c2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000015#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000016#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Atomic.h"
20#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000021#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000022#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024using namespace llvm;
25
Chris Lattner8a923e72008-03-12 17:45:29 +000026//===----------------------------------------------------------------------===//
Bill Wendling73ea2de2012-10-08 21:47:17 +000027// Attributes Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000029
Bill Wendling73ea2de2012-10-08 21:47:17 +000030Attributes::Attributes(uint64_t Val) : Attrs(Val) {}
31
32Attributes::Attributes(Attribute::AttrConst Val) : Attrs(Val.v) {}
33
34Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
35
Bill Wendlingc6daefa2012-10-08 23:27:46 +000036Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
37
Bill Wendling68d24012012-10-08 22:20:14 +000038// FIXME: This is temporary until we have implemented the uniquified version of
39// AttributesImpl.
40Attributes Attributes::get(Attributes::Builder &B) {
41 return Attributes(B.Bits);
42}
43
Bill Wendling73ea2de2012-10-08 21:47:17 +000044Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
45 // If there are no attributes, return an empty Attributes class.
46 if (B.Bits == 0)
47 return Attributes();
48
49 // Otherwise, build a key to look up the existing attributes.
50 LLVMContextImpl *pImpl = Context.pImpl;
51 FoldingSetNodeID ID;
52 ID.AddInteger(B.Bits);
53
54 void *InsertPoint;
55 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
56
57 if (!PA) {
58 // If we didn't find any existing attributes of the same shape then create a
59 // new one and insert it.
60 PA = new AttributesImpl(B.Bits);
61 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
62 }
63
64 // Return the AttributesList that we found or created.
65 return Attributes(PA);
66}
67
Bill Wendlingc9b22d72012-10-09 07:45:08 +000068bool Attributes::hasAttribute(AttrVal Val) const {
69 return Attrs.hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000070}
71
Bill Wendling93f70b72012-10-09 09:11:20 +000072bool Attributes::hasAttributes(const Attributes &A) const {
73 return Attrs.hasAttributes(A);
74}
75
Bill Wendlingabf3feb2012-10-05 06:44:41 +000076/// This returns the alignment field of an attribute as a byte alignment value.
77unsigned Attributes::getAlignment() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +000078 if (!hasAttribute(Attributes::Alignment))
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +000080 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000081}
82
83/// This returns the stack alignment field of an attribute as a byte alignment
84/// value.
85unsigned Attributes::getStackAlignment() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +000086 if (!hasAttribute(Attributes::StackAlignment))
Bill Wendlingabf3feb2012-10-05 06:44:41 +000087 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +000088 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000089}
90
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000091bool Attributes::isEmptyOrSingleton() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000092 return Attrs.isEmptyOrSingleton();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000093}
94
Bill Wendling73ea2de2012-10-08 21:47:17 +000095Attributes Attributes::operator | (const Attributes &A) const {
96 return Attributes(Raw() | A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000097}
Bill Wendling73ea2de2012-10-08 21:47:17 +000098Attributes Attributes::operator & (const Attributes &A) const {
99 return Attributes(Raw() & A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000100}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000101Attributes Attributes::operator ^ (const Attributes &A) const {
102 return Attributes(Raw() ^ A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000103}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000104Attributes &Attributes::operator |= (const Attributes &A) {
105 Attrs.Bits |= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000106 return *this;
107}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000108Attributes &Attributes::operator &= (const Attributes &A) {
109 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000110 return *this;
111}
112Attributes Attributes::operator ~ () const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000113 return Attributes(~Raw());
114}
115
116uint64_t Attributes::Raw() const {
117 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000118}
119
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000120Attributes Attributes::typeIncompatible(Type *Ty) {
121 Attributes::Builder Incompatible;
122
Bill Wendling7c04e042012-10-09 19:01:18 +0000123 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000124 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000125 Incompatible.addAttribute(Attributes::SExt)
126 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000127
Bill Wendling7c04e042012-10-09 19:01:18 +0000128 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000129 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000130 Incompatible.addAttribute(Attributes::ByVal)
131 .addAttribute(Attributes::Nest)
132 .addAttribute(Attributes::NoAlias)
133 .addAttribute(Attributes::NoCapture)
134 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000135
136 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
137}
138
Bill Wendlingde74cf52012-09-20 14:44:42 +0000139std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000150 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000152 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000158 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000160 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000162 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000164 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000166 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000168 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000170 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000172 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000174 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000176 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000178 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000180 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000182 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000184 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000186 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000188 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000190 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000191 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000192 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000193 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000194 Result += ") ";
195 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000196 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000197 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000198 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000199 Result += " ";
200 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000201 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000202 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000203 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000204 return Result;
205}
206
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000207//===----------------------------------------------------------------------===//
208// Attributes::Builder Implementation
209//===----------------------------------------------------------------------===//
210
Bill Wendling7c04e042012-10-09 19:01:18 +0000211Attributes::Builder &Attributes::Builder::
212addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000213 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000214 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000215}
216
217void Attributes::Builder::addAlignmentAttr(unsigned Align) {
218 if (Align == 0) return;
219 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
220 assert(Align <= 0x40000000 && "Alignment too large.");
221 Bits |= (Log2_32(Align) + 1) << 16;
222}
223void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
224 // Default alignment, allow the target to define how to align it.
225 if (Align == 0) return;
226 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
227 assert(Align <= 0x100 && "Alignment too large.");
228 Bits |= (Log2_32(Align) + 1) << 26;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000229}
230
Bill Wendling7c04e042012-10-09 19:01:18 +0000231Attributes::Builder &Attributes::Builder::
232removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000233 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000234 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000235}
236
Bill Wendling70f39172012-10-09 00:01:21 +0000237void Attributes::Builder::removeAttributes(const Attributes &A) {
238 Bits &= ~A.Raw();
239}
240
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000241bool Attributes::Builder::hasAttributes() const {
242 return Bits != 0;
243}
Bill Wendling70f39172012-10-09 00:01:21 +0000244bool Attributes::Builder::hasAttributes(const Attributes &A) const {
245 return Bits & A.Raw();
246}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000247bool Attributes::Builder::hasAlignmentAttr() const {
248 return Bits & Attribute::Alignment_i;
249}
250
251uint64_t Attributes::Builder::getAlignment() const {
252 if (!hasAlignmentAttr())
253 return 0;
254 return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
255}
256
Chris Lattner8a923e72008-03-12 17:45:29 +0000257//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000258// AttributeImpl Definition
259//===----------------------------------------------------------------------===//
260
Bill Wendling93f70b72012-10-09 09:11:20 +0000261uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000262 switch (Val) {
263 case Attributes::None: return 0;
264 case Attributes::ZExt: return 1 << 0;
265 case Attributes::SExt: return 1 << 1;
266 case Attributes::NoReturn: return 1 << 2;
267 case Attributes::InReg: return 1 << 3;
268 case Attributes::StructRet: return 1 << 4;
269 case Attributes::NoUnwind: return 1 << 5;
270 case Attributes::NoAlias: return 1 << 6;
271 case Attributes::ByVal: return 1 << 7;
272 case Attributes::Nest: return 1 << 8;
273 case Attributes::ReadNone: return 1 << 9;
274 case Attributes::ReadOnly: return 1 << 10;
275 case Attributes::NoInline: return 1 << 11;
276 case Attributes::AlwaysInline: return 1 << 12;
277 case Attributes::OptimizeForSize: return 1 << 13;
278 case Attributes::StackProtect: return 1 << 14;
279 case Attributes::StackProtectReq: return 1 << 15;
280 case Attributes::Alignment: return 31 << 16;
281 case Attributes::NoCapture: return 1 << 21;
282 case Attributes::NoRedZone: return 1 << 22;
283 case Attributes::NoImplicitFloat: return 1 << 23;
284 case Attributes::Naked: return 1 << 24;
285 case Attributes::InlineHint: return 1 << 25;
286 case Attributes::StackAlignment: return 7 << 26;
287 case Attributes::ReturnsTwice: return 1 << 29;
288 case Attributes::UWTable: return 1 << 30;
289 case Attributes::NonLazyBind: return 1U << 31;
290 case Attributes::AddressSafety: return 1ULL << 32;
291 }
292 llvm_unreachable("Unsupported attribute type");
293}
294
Bill Wendling73ea2de2012-10-08 21:47:17 +0000295bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000296 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000297}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000298
Bill Wendling73ea2de2012-10-08 21:47:17 +0000299bool AttributesImpl::hasAttributes() const {
300 return Bits != 0;
301}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000302
Bill Wendling73ea2de2012-10-08 21:47:17 +0000303bool AttributesImpl::hasAttributes(const Attributes &A) const {
304 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
305}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000306
Bill Wendling73ea2de2012-10-08 21:47:17 +0000307uint64_t AttributesImpl::getAlignment() const {
308 return Bits & Attribute::Alignment_i;
309}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000310
Bill Wendling73ea2de2012-10-08 21:47:17 +0000311uint64_t AttributesImpl::getStackAlignment() const {
312 return Bits & Attribute::StackAlignment_i;
313}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000314
Bill Wendling73ea2de2012-10-08 21:47:17 +0000315bool AttributesImpl::isEmptyOrSingleton() const {
316 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000317}
318
319//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000320// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000321//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000322
Owen Anderson2e831892010-11-18 18:59:13 +0000323namespace llvm {
324 class AttributeListImpl;
325}
326
327static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000328
Chris Lattner8a923e72008-03-12 17:45:29 +0000329namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000330static ManagedStatic<sys::SmartMutex<true> > ALMutex;
331
Devang Patel00095052008-09-24 00:29:49 +0000332class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000333 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000334
Devang Patel4c758ea2008-09-25 21:00:45 +0000335 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000336 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
337 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000338 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000339public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000340 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000341
Chris Lattner3cb6f832012-05-28 01:47:44 +0000342 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
343 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000344 RefCount = 0;
345 }
346
Owen Anderson9b14a252010-11-09 00:27:03 +0000347 void AddRef() {
348 sys::SmartScopedLock<true> Lock(*ALMutex);
349 ++RefCount;
350 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000351 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000352 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000353 if (!AttributesLists.isConstructed())
354 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000355 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000356 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000357 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000358 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000359
360 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000361 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000362 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000363 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
364 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
365 ID.AddInteger(Attrs[i].Attrs.Raw());
366 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000367 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000368 }
369};
370}
371
Devang Patel00095052008-09-24 00:29:49 +0000372AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000373 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000374 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000375}
376
377
Chris Lattner3cb6f832012-05-28 01:47:44 +0000378AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000379 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000380 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000381 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000382
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000383#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000384 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000385 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000386 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000387 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000388 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000389 }
390#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000391
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000392 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000393 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000394 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000395 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000396
397 sys::SmartScopedLock<true> Lock(*ALMutex);
398
Devang Patel00095052008-09-24 00:29:49 +0000399 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000401
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000402 // If we didn't find any existing attributes of the same shape then
403 // create a new one and insert it.
404 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000405 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000406 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000407 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000408
Devang Patel4c758ea2008-09-25 21:00:45 +0000409 // Return the AttributesList that we found or created.
410 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000411}
412
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000413
Chris Lattner8a923e72008-03-12 17:45:29 +0000414//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000415// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000416//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000417
Devang Patel4c758ea2008-09-25 21:00:45 +0000418AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000419 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000420}
421
Devang Patel4c758ea2008-09-25 21:00:45 +0000422AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
423 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000424}
425
Devang Patel4c758ea2008-09-25 21:00:45 +0000426const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000427 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000428 if (AttrList == RHS.AttrList) return *this;
429 if (AttrList) AttrList->DropRef();
430 AttrList = RHS.AttrList;
431 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000432 return *this;
433}
434
Devang Patel4c758ea2008-09-25 21:00:45 +0000435AttrListPtr::~AttrListPtr() {
436 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000437}
438
439/// getNumSlots - Return the number of slots used in this attribute list.
440/// This is the number of arguments that have an attribute set on them
441/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000442unsigned AttrListPtr::getNumSlots() const {
443 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000444}
445
Devang Patel4c758ea2008-09-25 21:00:45 +0000446/// getSlot - Return the AttributeWithIndex at the specified slot. This
447/// holds a number plus a set of attributes.
448const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
449 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
450 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000451}
452
453
Devang Patel4c758ea2008-09-25 21:00:45 +0000454/// getAttributes - The attributes for the specified index are
455/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000456/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000457Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000458 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000459
Devang Patel4c758ea2008-09-25 21:00:45 +0000460 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000461 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
462 if (Attrs[i].Index == Idx)
463 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000464
465 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000466}
467
468/// hasAttrSomewhere - Return true if the specified attribute is set for at
469/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000470bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
471 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000472
Devang Patel4c758ea2008-09-25 21:00:45 +0000473 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000474 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000475 if (Attrs[i].Attrs.hasAttributes(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000476 return true;
477 return false;
478}
479
Bill Wendling70f39172012-10-09 00:01:21 +0000480unsigned AttrListPtr::getNumAttrs() const {
481 return AttrList ? AttrList->Attrs.size() : 0;
482}
483
484Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
485 assert(AttrList && "Trying to get an attribute from an empty list!");
486 assert(i < AttrList->Attrs.size() && "Index out of range!");
487 return AttrList->Attrs[i].Attrs;
488}
Chris Lattner8a923e72008-03-12 17:45:29 +0000489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
491 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000492#ifndef NDEBUG
493 // FIXME it is not obvious how this should work for alignment.
494 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000495 unsigned OldAlign = OldAttrs.getAlignment();
496 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000497 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000498 "Attempt to change alignment!");
499#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000500
Devang Patelba3fa6c2008-09-23 23:03:40 +0000501 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000502 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000503 return *this;
504
Devang Patel4c758ea2008-09-25 21:00:45 +0000505 SmallVector<AttributeWithIndex, 8> NewAttrList;
506 if (AttrList == 0)
507 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000508 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000509 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000510 unsigned i = 0, e = OldAttrList.size();
511 // Copy attributes for arguments before this one.
512 for (; i != e && OldAttrList[i].Index < Idx; ++i)
513 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000514
Chris Lattner8a923e72008-03-12 17:45:29 +0000515 // If there are attributes already at this index, merge them in.
516 if (i != e && OldAttrList[i].Index == Idx) {
517 Attrs |= OldAttrList[i].Attrs;
518 ++i;
519 }
520
Devang Patel4c758ea2008-09-25 21:00:45 +0000521 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000522
523 // Copy attributes for arguments after this one.
524 NewAttrList.insert(NewAttrList.end(),
525 OldAttrList.begin()+i, OldAttrList.end());
526 }
527
Chris Lattner3cb6f832012-05-28 01:47:44 +0000528 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000529}
530
Devang Patel4c758ea2008-09-25 21:00:45 +0000531AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000532#ifndef NDEBUG
533 // FIXME it is not obvious how this should work for alignment.
534 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000535 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
536 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000537#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000538 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000539
Devang Patel4c758ea2008-09-25 21:00:45 +0000540 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000541 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000542 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000543 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000544
Devang Patel4c758ea2008-09-25 21:00:45 +0000545 SmallVector<AttributeWithIndex, 8> NewAttrList;
546 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000547 unsigned i = 0, e = OldAttrList.size();
548
549 // Copy attributes for arguments before this one.
550 for (; i != e && OldAttrList[i].Index < Idx; ++i)
551 NewAttrList.push_back(OldAttrList[i]);
552
553 // If there are attributes already at this index, merge them in.
554 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
555 Attrs = OldAttrList[i].Attrs & ~Attrs;
556 ++i;
557 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000559
560 // Copy attributes for arguments after this one.
561 NewAttrList.insert(NewAttrList.end(),
562 OldAttrList.begin()+i, OldAttrList.end());
563
Chris Lattner3cb6f832012-05-28 01:47:44 +0000564 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000565}
566
Devang Patel4c758ea2008-09-25 21:00:45 +0000567void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000568 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000569 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000570 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000571 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000572 }
573
David Greenef7014732010-01-05 01:29:58 +0000574 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000575}