blob: 0625ef3f09c64388d7b8ff0f1e2d683f7525e646 [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
Bill Wendling73ea2de2012-10-08 21:47:17 +000032Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
33
Bill Wendlingc6daefa2012-10-08 23:27:46 +000034Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
35
Bill Wendling68d24012012-10-08 22:20:14 +000036// FIXME: This is temporary until we have implemented the uniquified version of
37// AttributesImpl.
38Attributes Attributes::get(Attributes::Builder &B) {
39 return Attributes(B.Bits);
40}
41
Bill Wendling73ea2de2012-10-08 21:47:17 +000042Attributes 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 Wendlingc9b22d72012-10-09 07:45:08 +000066bool Attributes::hasAttribute(AttrVal Val) const {
67 return Attrs.hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000068}
69
Bill Wendling93f70b72012-10-09 09:11:20 +000070bool Attributes::hasAttributes(const Attributes &A) const {
71 return Attrs.hasAttributes(A);
72}
73
Bill Wendlingabf3feb2012-10-05 06:44:41 +000074/// This returns the alignment field of an attribute as a byte alignment value.
75unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000076 if (!hasAttribute(Attributes::Alignment))
77 return 0;
78 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079}
80
81/// This returns the stack alignment field of an attribute as a byte alignment
82/// value.
83unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000084 if (!hasAttribute(Attributes::StackAlignment))
85 return 0;
86 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000087}
88
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000089bool Attributes::isEmptyOrSingleton() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000090 return Attrs.isEmptyOrSingleton();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000091}
92
Bill Wendling73ea2de2012-10-08 21:47:17 +000093Attributes Attributes::operator | (const Attributes &A) const {
94 return Attributes(Raw() | A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000095}
Bill Wendling73ea2de2012-10-08 21:47:17 +000096Attributes Attributes::operator & (const Attributes &A) const {
97 return Attributes(Raw() & A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000098}
Bill Wendling73ea2de2012-10-08 21:47:17 +000099Attributes Attributes::operator ^ (const Attributes &A) const {
100 return Attributes(Raw() ^ A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000101}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000102Attributes &Attributes::operator |= (const Attributes &A) {
103 Attrs.Bits |= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000104 return *this;
105}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000106Attributes &Attributes::operator &= (const Attributes &A) {
107 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000108 return *this;
109}
110Attributes Attributes::operator ~ () const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000111 return Attributes(~Raw());
112}
113
114uint64_t Attributes::Raw() const {
115 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000116}
117
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000118Attributes Attributes::typeIncompatible(Type *Ty) {
119 Attributes::Builder Incompatible;
120
Bill Wendling7c04e042012-10-09 19:01:18 +0000121 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000122 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000123 Incompatible.addAttribute(Attributes::SExt)
124 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000125
Bill Wendling7c04e042012-10-09 19:01:18 +0000126 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000127 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000128 Incompatible.addAttribute(Attributes::ByVal)
129 .addAttribute(Attributes::Nest)
130 .addAttribute(Attributes::NoAlias)
131 .addAttribute(Attributes::NoCapture)
132 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000133
134 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
135}
136
Bill Wendlingde74cf52012-09-20 14:44:42 +0000137std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000138 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000139 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000148 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000150 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000152 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000156 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000158 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000160 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000162 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000164 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000166 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000168 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000170 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000172 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000174 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000176 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000178 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000180 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000182 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000184 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000186 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000188 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000190 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000191 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000192 Result += ") ";
193 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000194 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000195 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000196 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000197 Result += " ";
198 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000199 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000200 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000201 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000202 return Result;
203}
204
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000205//===----------------------------------------------------------------------===//
206// Attributes::Builder Implementation
207//===----------------------------------------------------------------------===//
208
Bill Wendling7c04e042012-10-09 19:01:18 +0000209Attributes::Builder &Attributes::Builder::
210addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000211 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000212 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000213}
214
215void Attributes::Builder::addAlignmentAttr(unsigned Align) {
216 if (Align == 0) return;
217 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
218 assert(Align <= 0x40000000 && "Alignment too large.");
219 Bits |= (Log2_32(Align) + 1) << 16;
220}
221void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
222 // Default alignment, allow the target to define how to align it.
223 if (Align == 0) return;
224 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
225 assert(Align <= 0x100 && "Alignment too large.");
226 Bits |= (Log2_32(Align) + 1) << 26;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000227}
228
Bill Wendling7c04e042012-10-09 19:01:18 +0000229Attributes::Builder &Attributes::Builder::
230removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000231 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000232 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000233}
234
Bill Wendling70f39172012-10-09 00:01:21 +0000235void Attributes::Builder::removeAttributes(const Attributes &A) {
236 Bits &= ~A.Raw();
237}
238
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000239bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
240 return Bits & AttributesImpl::getAttrMask(A);
241}
242
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000243bool Attributes::Builder::hasAttributes() const {
244 return Bits != 0;
245}
Bill Wendling70f39172012-10-09 00:01:21 +0000246bool Attributes::Builder::hasAttributes(const Attributes &A) const {
247 return Bits & A.Raw();
248}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000249bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000250 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000251}
252
253uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000254 if (!hasAlignmentAttr())
255 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000256 return 1U <<
257 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000258}
259
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000260uint64_t Attributes::Builder::getStackAlignment() const {
261 if (!hasAlignmentAttr())
262 return 0;
263 return 1U <<
264 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
265}
266
Chris Lattner8a923e72008-03-12 17:45:29 +0000267//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000268// AttributeImpl Definition
269//===----------------------------------------------------------------------===//
270
Bill Wendling93f70b72012-10-09 09:11:20 +0000271uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000272 switch (Val) {
273 case Attributes::None: return 0;
274 case Attributes::ZExt: return 1 << 0;
275 case Attributes::SExt: return 1 << 1;
276 case Attributes::NoReturn: return 1 << 2;
277 case Attributes::InReg: return 1 << 3;
278 case Attributes::StructRet: return 1 << 4;
279 case Attributes::NoUnwind: return 1 << 5;
280 case Attributes::NoAlias: return 1 << 6;
281 case Attributes::ByVal: return 1 << 7;
282 case Attributes::Nest: return 1 << 8;
283 case Attributes::ReadNone: return 1 << 9;
284 case Attributes::ReadOnly: return 1 << 10;
285 case Attributes::NoInline: return 1 << 11;
286 case Attributes::AlwaysInline: return 1 << 12;
287 case Attributes::OptimizeForSize: return 1 << 13;
288 case Attributes::StackProtect: return 1 << 14;
289 case Attributes::StackProtectReq: return 1 << 15;
290 case Attributes::Alignment: return 31 << 16;
291 case Attributes::NoCapture: return 1 << 21;
292 case Attributes::NoRedZone: return 1 << 22;
293 case Attributes::NoImplicitFloat: return 1 << 23;
294 case Attributes::Naked: return 1 << 24;
295 case Attributes::InlineHint: return 1 << 25;
296 case Attributes::StackAlignment: return 7 << 26;
297 case Attributes::ReturnsTwice: return 1 << 29;
298 case Attributes::UWTable: return 1 << 30;
299 case Attributes::NonLazyBind: return 1U << 31;
300 case Attributes::AddressSafety: return 1ULL << 32;
301 }
302 llvm_unreachable("Unsupported attribute type");
303}
304
Bill Wendling73ea2de2012-10-08 21:47:17 +0000305bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000306 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000307}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000308
Bill Wendling73ea2de2012-10-08 21:47:17 +0000309bool AttributesImpl::hasAttributes() const {
310 return Bits != 0;
311}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000312
Bill Wendling73ea2de2012-10-08 21:47:17 +0000313bool AttributesImpl::hasAttributes(const Attributes &A) const {
314 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
315}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000316
Bill Wendling73ea2de2012-10-08 21:47:17 +0000317uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000318 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000319}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000320
Bill Wendling73ea2de2012-10-08 21:47:17 +0000321uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000322 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000323}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000324
Bill Wendling73ea2de2012-10-08 21:47:17 +0000325bool AttributesImpl::isEmptyOrSingleton() const {
326 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000327}
328
329//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000330// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000331//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000332
Owen Anderson2e831892010-11-18 18:59:13 +0000333namespace llvm {
334 class AttributeListImpl;
335}
336
337static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000338
Chris Lattner8a923e72008-03-12 17:45:29 +0000339namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000340static ManagedStatic<sys::SmartMutex<true> > ALMutex;
341
Devang Patel00095052008-09-24 00:29:49 +0000342class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000343 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000344
Devang Patel4c758ea2008-09-25 21:00:45 +0000345 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000346 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
347 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000348 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000349public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000350 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000351
Chris Lattner3cb6f832012-05-28 01:47:44 +0000352 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
353 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000354 RefCount = 0;
355 }
356
Owen Anderson9b14a252010-11-09 00:27:03 +0000357 void AddRef() {
358 sys::SmartScopedLock<true> Lock(*ALMutex);
359 ++RefCount;
360 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000361 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000362 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000363 if (!AttributesLists.isConstructed())
364 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000365 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000366 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000367 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000368 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000369
370 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000371 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000372 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000373 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
374 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
375 ID.AddInteger(Attrs[i].Attrs.Raw());
376 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000377 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000378 }
379};
380}
381
Devang Patel00095052008-09-24 00:29:49 +0000382AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000383 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000384 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000385}
386
387
Chris Lattner3cb6f832012-05-28 01:47:44 +0000388AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000389 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000390 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000391 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000392
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000393#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000394 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000395 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000396 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000397 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000398 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000399 }
400#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000401
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000402 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000403 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000404 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000405 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000406
407 sys::SmartScopedLock<true> Lock(*ALMutex);
408
Devang Patel00095052008-09-24 00:29:49 +0000409 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000411
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000412 // If we didn't find any existing attributes of the same shape then
413 // create a new one and insert it.
414 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000415 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000416 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000417 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000418
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 // Return the AttributesList that we found or created.
420 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000421}
422
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000423
Chris Lattner8a923e72008-03-12 17:45:29 +0000424//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000425// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000426//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000427
Devang Patel4c758ea2008-09-25 21:00:45 +0000428AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000429 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000430}
431
Devang Patel4c758ea2008-09-25 21:00:45 +0000432AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
433 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000434}
435
Devang Patel4c758ea2008-09-25 21:00:45 +0000436const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000437 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 if (AttrList == RHS.AttrList) return *this;
439 if (AttrList) AttrList->DropRef();
440 AttrList = RHS.AttrList;
441 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000442 return *this;
443}
444
Devang Patel4c758ea2008-09-25 21:00:45 +0000445AttrListPtr::~AttrListPtr() {
446 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000447}
448
449/// getNumSlots - Return the number of slots used in this attribute list.
450/// This is the number of arguments that have an attribute set on them
451/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000452unsigned AttrListPtr::getNumSlots() const {
453 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000454}
455
Devang Patel4c758ea2008-09-25 21:00:45 +0000456/// getSlot - Return the AttributeWithIndex at the specified slot. This
457/// holds a number plus a set of attributes.
458const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
459 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
460 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000461}
462
463
Devang Patel4c758ea2008-09-25 21:00:45 +0000464/// getAttributes - The attributes for the specified index are
465/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000466/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000467Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000468 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000469
Devang Patel4c758ea2008-09-25 21:00:45 +0000470 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000471 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
472 if (Attrs[i].Index == Idx)
473 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000474
475 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000476}
477
478/// hasAttrSomewhere - Return true if the specified attribute is set for at
479/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000480bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000481 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000482
Devang Patel4c758ea2008-09-25 21:00:45 +0000483 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000484 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000485 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000486 return true;
487 return false;
488}
489
Bill Wendling70f39172012-10-09 00:01:21 +0000490unsigned AttrListPtr::getNumAttrs() const {
491 return AttrList ? AttrList->Attrs.size() : 0;
492}
493
494Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
495 assert(AttrList && "Trying to get an attribute from an empty list!");
496 assert(i < AttrList->Attrs.size() && "Index out of range!");
497 return AttrList->Attrs[i].Attrs;
498}
Chris Lattner8a923e72008-03-12 17:45:29 +0000499
Devang Patel4c758ea2008-09-25 21:00:45 +0000500AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
501 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000502#ifndef NDEBUG
503 // FIXME it is not obvious how this should work for alignment.
504 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000505 unsigned OldAlign = OldAttrs.getAlignment();
506 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000507 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000508 "Attempt to change alignment!");
509#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000510
Devang Patelba3fa6c2008-09-23 23:03:40 +0000511 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000512 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000513 return *this;
514
Devang Patel4c758ea2008-09-25 21:00:45 +0000515 SmallVector<AttributeWithIndex, 8> NewAttrList;
516 if (AttrList == 0)
517 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000518 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000519 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000520 unsigned i = 0, e = OldAttrList.size();
521 // Copy attributes for arguments before this one.
522 for (; i != e && OldAttrList[i].Index < Idx; ++i)
523 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000524
Chris Lattner8a923e72008-03-12 17:45:29 +0000525 // If there are attributes already at this index, merge them in.
526 if (i != e && OldAttrList[i].Index == Idx) {
527 Attrs |= OldAttrList[i].Attrs;
528 ++i;
529 }
530
Devang Patel4c758ea2008-09-25 21:00:45 +0000531 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000532
533 // Copy attributes for arguments after this one.
534 NewAttrList.insert(NewAttrList.end(),
535 OldAttrList.begin()+i, OldAttrList.end());
536 }
537
Chris Lattner3cb6f832012-05-28 01:47:44 +0000538 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000539}
540
Devang Patel4c758ea2008-09-25 21:00:45 +0000541AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000542#ifndef NDEBUG
543 // FIXME it is not obvious how this should work for alignment.
544 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000545 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
546 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000547#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000548 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000551 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000552 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000553 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000554
Devang Patel4c758ea2008-09-25 21:00:45 +0000555 SmallVector<AttributeWithIndex, 8> NewAttrList;
556 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000557 unsigned i = 0, e = OldAttrList.size();
558
559 // Copy attributes for arguments before this one.
560 for (; i != e && OldAttrList[i].Index < Idx; ++i)
561 NewAttrList.push_back(OldAttrList[i]);
562
563 // If there are attributes already at this index, merge them in.
564 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
565 Attrs = OldAttrList[i].Attrs & ~Attrs;
566 ++i;
567 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000568 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000569
570 // Copy attributes for arguments after this one.
571 NewAttrList.insert(NewAttrList.end(),
572 OldAttrList.begin()+i, OldAttrList.end());
573
Chris Lattner3cb6f832012-05-28 01:47:44 +0000574 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000575}
576
Devang Patel4c758ea2008-09-25 21:00:45 +0000577void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000578 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000579 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000580 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000581 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000582 }
583
David Greenef7014732010-01-05 01:29:58 +0000584 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000585}