blob: 7ae823214bbd869f43e849c4e7f00229b115e565 [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 Wendling13410272012-10-11 01:05:52 +000032Attributes::Attributes(AttrVal Val)
33 : Attrs(Attributes::get(Attributes::Builder().addAttribute(Val)).Attrs) {}
34
Bill Wendling73ea2de2012-10-08 21:47:17 +000035Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
36
Bill Wendlingc6daefa2012-10-08 23:27:46 +000037Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
38
Bill Wendling68d24012012-10-08 22:20:14 +000039// FIXME: This is temporary until we have implemented the uniquified version of
40// AttributesImpl.
41Attributes Attributes::get(Attributes::Builder &B) {
42 return Attributes(B.Bits);
43}
44
Bill Wendling73ea2de2012-10-08 21:47:17 +000045Attributes 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 Wendlingc9b22d72012-10-09 07:45:08 +000069bool Attributes::hasAttribute(AttrVal Val) const {
70 return Attrs.hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000071}
72
Bill Wendling93f70b72012-10-09 09:11:20 +000073bool Attributes::hasAttributes(const Attributes &A) const {
74 return Attrs.hasAttributes(A);
75}
76
Bill Wendlingabf3feb2012-10-05 06:44:41 +000077/// This returns the alignment field of an attribute as a byte alignment value.
78unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000079 if (!hasAttribute(Attributes::Alignment))
80 return 0;
81 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000082}
83
84/// This returns the stack alignment field of an attribute as a byte alignment
85/// value.
86unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000087 if (!hasAttribute(Attributes::StackAlignment))
88 return 0;
89 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000090}
91
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000092bool Attributes::isEmptyOrSingleton() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000093 return Attrs.isEmptyOrSingleton();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000094}
95
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) const {
103 return Attributes(Raw() ^ A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000104}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000105Attributes &Attributes::operator |= (const Attributes &A) {
106 Attrs.Bits |= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000107 return *this;
108}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000109Attributes &Attributes::operator &= (const Attributes &A) {
110 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000111 return *this;
112}
113Attributes Attributes::operator ~ () const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000114 return Attributes(~Raw());
115}
116
117uint64_t Attributes::Raw() const {
118 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000119}
120
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000121Attributes Attributes::typeIncompatible(Type *Ty) {
122 Attributes::Builder Incompatible;
123
Bill Wendling7c04e042012-10-09 19:01:18 +0000124 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000125 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000126 Incompatible.addAttribute(Attributes::SExt)
127 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000128
Bill Wendling7c04e042012-10-09 19:01:18 +0000129 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000130 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000131 Incompatible.addAttribute(Attributes::ByVal)
132 .addAttribute(Attributes::Nest)
133 .addAttribute(Attributes::NoAlias)
134 .addAttribute(Attributes::NoCapture)
135 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000136
137 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
138}
139
Bill Wendlingde74cf52012-09-20 14:44:42 +0000140std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000142 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000143 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000144 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000145 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000146 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000147 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000148 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000149 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000150 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000151 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000152 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000153 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000154 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000155 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000156 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000157 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000158 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000159 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000160 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000161 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000162 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000163 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000164 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000165 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000166 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000167 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000168 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000169 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000170 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000171 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000172 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000173 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000174 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000175 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000176 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000177 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000178 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000179 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000180 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000181 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000182 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000183 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000184 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000185 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000186 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000187 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000188 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000189 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000190 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000191 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000192 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000193 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000194 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000195 Result += ") ";
196 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000197 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000198 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000199 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000200 Result += " ";
201 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000202 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000203 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000204 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000205 return Result;
206}
207
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000208//===----------------------------------------------------------------------===//
209// Attributes::Builder Implementation
210//===----------------------------------------------------------------------===//
211
Bill Wendling7c04e042012-10-09 19:01:18 +0000212Attributes::Builder &Attributes::Builder::
213addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000214 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000215 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000216}
217
218void Attributes::Builder::addAlignmentAttr(unsigned Align) {
219 if (Align == 0) return;
220 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
221 assert(Align <= 0x40000000 && "Alignment too large.");
222 Bits |= (Log2_32(Align) + 1) << 16;
223}
224void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
225 // Default alignment, allow the target to define how to align it.
226 if (Align == 0) return;
227 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
228 assert(Align <= 0x100 && "Alignment too large.");
229 Bits |= (Log2_32(Align) + 1) << 26;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000230}
231
Bill Wendling7c04e042012-10-09 19:01:18 +0000232Attributes::Builder &Attributes::Builder::
233removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000234 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000235 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000236}
237
Bill Wendling70f39172012-10-09 00:01:21 +0000238void Attributes::Builder::removeAttributes(const Attributes &A) {
239 Bits &= ~A.Raw();
240}
241
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000242bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
243 return Bits & AttributesImpl::getAttrMask(A);
244}
245
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000246bool Attributes::Builder::hasAttributes() const {
247 return Bits != 0;
248}
Bill Wendling70f39172012-10-09 00:01:21 +0000249bool Attributes::Builder::hasAttributes(const Attributes &A) const {
250 return Bits & A.Raw();
251}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000252bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000253 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000254}
255
256uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000257 if (!hasAlignmentAttr())
258 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000259 return 1U <<
260 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000261}
262
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000263uint64_t Attributes::Builder::getStackAlignment() const {
264 if (!hasAlignmentAttr())
265 return 0;
266 return 1U <<
267 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
268}
269
Chris Lattner8a923e72008-03-12 17:45:29 +0000270//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000271// AttributeImpl Definition
272//===----------------------------------------------------------------------===//
273
Bill Wendling93f70b72012-10-09 09:11:20 +0000274uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000275 switch (Val) {
276 case Attributes::None: return 0;
277 case Attributes::ZExt: return 1 << 0;
278 case Attributes::SExt: return 1 << 1;
279 case Attributes::NoReturn: return 1 << 2;
280 case Attributes::InReg: return 1 << 3;
281 case Attributes::StructRet: return 1 << 4;
282 case Attributes::NoUnwind: return 1 << 5;
283 case Attributes::NoAlias: return 1 << 6;
284 case Attributes::ByVal: return 1 << 7;
285 case Attributes::Nest: return 1 << 8;
286 case Attributes::ReadNone: return 1 << 9;
287 case Attributes::ReadOnly: return 1 << 10;
288 case Attributes::NoInline: return 1 << 11;
289 case Attributes::AlwaysInline: return 1 << 12;
290 case Attributes::OptimizeForSize: return 1 << 13;
291 case Attributes::StackProtect: return 1 << 14;
292 case Attributes::StackProtectReq: return 1 << 15;
293 case Attributes::Alignment: return 31 << 16;
294 case Attributes::NoCapture: return 1 << 21;
295 case Attributes::NoRedZone: return 1 << 22;
296 case Attributes::NoImplicitFloat: return 1 << 23;
297 case Attributes::Naked: return 1 << 24;
298 case Attributes::InlineHint: return 1 << 25;
299 case Attributes::StackAlignment: return 7 << 26;
300 case Attributes::ReturnsTwice: return 1 << 29;
301 case Attributes::UWTable: return 1 << 30;
302 case Attributes::NonLazyBind: return 1U << 31;
303 case Attributes::AddressSafety: return 1ULL << 32;
304 }
305 llvm_unreachable("Unsupported attribute type");
306}
307
Bill Wendling73ea2de2012-10-08 21:47:17 +0000308bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000309 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000310}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000311
Bill Wendling73ea2de2012-10-08 21:47:17 +0000312bool AttributesImpl::hasAttributes() const {
313 return Bits != 0;
314}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000315
Bill Wendling73ea2de2012-10-08 21:47:17 +0000316bool AttributesImpl::hasAttributes(const Attributes &A) const {
317 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
318}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000319
Bill Wendling73ea2de2012-10-08 21:47:17 +0000320uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000321 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000322}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000323
Bill Wendling73ea2de2012-10-08 21:47:17 +0000324uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000325 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000326}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000327
Bill Wendling73ea2de2012-10-08 21:47:17 +0000328bool AttributesImpl::isEmptyOrSingleton() const {
329 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000330}
331
332//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000333// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000334//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000335
Owen Anderson2e831892010-11-18 18:59:13 +0000336namespace llvm {
337 class AttributeListImpl;
338}
339
340static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000341
Chris Lattner8a923e72008-03-12 17:45:29 +0000342namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000343static ManagedStatic<sys::SmartMutex<true> > ALMutex;
344
Devang Patel00095052008-09-24 00:29:49 +0000345class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000346 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000347
Devang Patel4c758ea2008-09-25 21:00:45 +0000348 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000349 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
350 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000351 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000352public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000353 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000354
Chris Lattner3cb6f832012-05-28 01:47:44 +0000355 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
356 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000357 RefCount = 0;
358 }
359
Owen Anderson9b14a252010-11-09 00:27:03 +0000360 void AddRef() {
361 sys::SmartScopedLock<true> Lock(*ALMutex);
362 ++RefCount;
363 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000364 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000365 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000366 if (!AttributesLists.isConstructed())
367 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000368 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000369 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000370 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000371 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000372
373 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000374 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000375 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000376 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
377 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
378 ID.AddInteger(Attrs[i].Attrs.Raw());
379 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000380 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000381 }
382};
383}
384
Devang Patel00095052008-09-24 00:29:49 +0000385AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000386 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000387 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000388}
389
390
Chris Lattner3cb6f832012-05-28 01:47:44 +0000391AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000392 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000393 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000394 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000395
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000396#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000397 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000398 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000399 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000401 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000402 }
403#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000404
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000405 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000406 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000407 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000408 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000409
410 sys::SmartScopedLock<true> Lock(*ALMutex);
411
Devang Patel00095052008-09-24 00:29:49 +0000412 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000413 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000414
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000415 // If we didn't find any existing attributes of the same shape then
416 // create a new one and insert it.
417 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000418 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000420 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000421
Devang Patel4c758ea2008-09-25 21:00:45 +0000422 // Return the AttributesList that we found or created.
423 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000424}
425
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000426
Chris Lattner8a923e72008-03-12 17:45:29 +0000427//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000428// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000429//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000430
Devang Patel4c758ea2008-09-25 21:00:45 +0000431AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000432 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000433}
434
Devang Patel4c758ea2008-09-25 21:00:45 +0000435AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
436 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000437}
438
Devang Patel4c758ea2008-09-25 21:00:45 +0000439const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000440 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000441 if (AttrList == RHS.AttrList) return *this;
442 if (AttrList) AttrList->DropRef();
443 AttrList = RHS.AttrList;
444 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000445 return *this;
446}
447
Devang Patel4c758ea2008-09-25 21:00:45 +0000448AttrListPtr::~AttrListPtr() {
449 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000450}
451
452/// getNumSlots - Return the number of slots used in this attribute list.
453/// This is the number of arguments that have an attribute set on them
454/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000455unsigned AttrListPtr::getNumSlots() const {
456 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000457}
458
Devang Patel4c758ea2008-09-25 21:00:45 +0000459/// getSlot - Return the AttributeWithIndex at the specified slot. This
460/// holds a number plus a set of attributes.
461const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
462 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
463 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000464}
465
466
Devang Patel4c758ea2008-09-25 21:00:45 +0000467/// getAttributes - The attributes for the specified index are
468/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000469/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000470Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000471 if (AttrList == 0) return Attributes();
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 && Attrs[i].Index <= Idx; ++i)
475 if (Attrs[i].Index == Idx)
476 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000477
478 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000479}
480
481/// hasAttrSomewhere - Return true if the specified attribute is set for at
482/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000483bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000484 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000485
Devang Patel4c758ea2008-09-25 21:00:45 +0000486 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000487 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000488 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000489 return true;
490 return false;
491}
492
Bill Wendling70f39172012-10-09 00:01:21 +0000493unsigned AttrListPtr::getNumAttrs() const {
494 return AttrList ? AttrList->Attrs.size() : 0;
495}
496
497Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
498 assert(AttrList && "Trying to get an attribute from an empty list!");
499 assert(i < AttrList->Attrs.size() && "Index out of range!");
500 return AttrList->Attrs[i].Attrs;
501}
Chris Lattner8a923e72008-03-12 17:45:29 +0000502
Devang Patel4c758ea2008-09-25 21:00:45 +0000503AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
504 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000505#ifndef NDEBUG
506 // FIXME it is not obvious how this should work for alignment.
507 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000508 unsigned OldAlign = OldAttrs.getAlignment();
509 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000510 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000511 "Attempt to change alignment!");
512#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000513
Devang Patelba3fa6c2008-09-23 23:03:40 +0000514 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000515 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000516 return *this;
517
Devang Patel4c758ea2008-09-25 21:00:45 +0000518 SmallVector<AttributeWithIndex, 8> NewAttrList;
519 if (AttrList == 0)
520 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000521 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000522 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000523 unsigned i = 0, e = OldAttrList.size();
524 // Copy attributes for arguments before this one.
525 for (; i != e && OldAttrList[i].Index < Idx; ++i)
526 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000527
Chris Lattner8a923e72008-03-12 17:45:29 +0000528 // If there are attributes already at this index, merge them in.
529 if (i != e && OldAttrList[i].Index == Idx) {
530 Attrs |= OldAttrList[i].Attrs;
531 ++i;
532 }
533
Devang Patel4c758ea2008-09-25 21:00:45 +0000534 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000535
536 // Copy attributes for arguments after this one.
537 NewAttrList.insert(NewAttrList.end(),
538 OldAttrList.begin()+i, OldAttrList.end());
539 }
540
Chris Lattner3cb6f832012-05-28 01:47:44 +0000541 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000542}
543
Devang Patel4c758ea2008-09-25 21:00:45 +0000544AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000545#ifndef NDEBUG
546 // FIXME it is not obvious how this should work for alignment.
547 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000548 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
549 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000550#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000551 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000552
Devang Patel4c758ea2008-09-25 21:00:45 +0000553 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000554 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000555 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000556 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000557
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 SmallVector<AttributeWithIndex, 8> NewAttrList;
559 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000560 unsigned i = 0, e = OldAttrList.size();
561
562 // Copy attributes for arguments before this one.
563 for (; i != e && OldAttrList[i].Index < Idx; ++i)
564 NewAttrList.push_back(OldAttrList[i]);
565
566 // If there are attributes already at this index, merge them in.
567 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
568 Attrs = OldAttrList[i].Attrs & ~Attrs;
569 ++i;
570 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000571 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000572
573 // Copy attributes for arguments after this one.
574 NewAttrList.insert(NewAttrList.end(),
575 OldAttrList.begin()+i, OldAttrList.end());
576
Chris Lattner3cb6f832012-05-28 01:47:44 +0000577 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000578}
579
Devang Patel4c758ea2008-09-25 21:00:45 +0000580void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000581 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000582 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000583 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000584 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000585 }
586
David Greenef7014732010-01-05 01:29:58 +0000587 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000588}