blob: 635ad1409ffa220825054dccc974c67a83241681 [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 Wendling9d7ba8da2012-10-11 01:10:00 +000032Attributes::Attributes(LLVMContext &C, AttrVal Val)
Bill Wendling13410272012-10-11 01:05:52 +000033 : 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) {
100 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000101 return *this;
102}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000103
104uint64_t Attributes::Raw() const {
105 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000106}
107
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000108Attributes Attributes::typeIncompatible(Type *Ty) {
109 Attributes::Builder Incompatible;
110
Bill Wendling7c04e042012-10-09 19:01:18 +0000111 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000112 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000113 Incompatible.addAttribute(Attributes::SExt)
114 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000115
Bill Wendling7c04e042012-10-09 19:01:18 +0000116 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000117 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000118 Incompatible.addAttribute(Attributes::ByVal)
119 .addAttribute(Attributes::Nest)
120 .addAttribute(Attributes::NoAlias)
121 .addAttribute(Attributes::NoCapture)
122 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000123
124 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
125}
126
Bill Wendlingde74cf52012-09-20 14:44:42 +0000127std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000128 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000129 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000130 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000131 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000133 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000135 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000137 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000138 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000139 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000140 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000146 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000148 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000150 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000152 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000158 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000160 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000162 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000164 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000166 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000168 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000170 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000172 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000174 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000176 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000178 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000180 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000181 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000182 Result += ") ";
183 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000184 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000185 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000186 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000187 Result += " ";
188 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000189 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000190 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000191 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000192 return Result;
193}
194
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000195//===----------------------------------------------------------------------===//
196// Attributes::Builder Implementation
197//===----------------------------------------------------------------------===//
198
Bill Wendling7c04e042012-10-09 19:01:18 +0000199Attributes::Builder &Attributes::Builder::
200addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000201 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000202 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000203}
204
Bill Wendling1fcc8222012-10-14 04:10:01 +0000205Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
206 Bits |= Val;
207 return *this;
208}
209
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000210Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
211 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000212 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
213 assert(Align <= 0x40000000 && "Alignment too large.");
214 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000215 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000216}
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000217Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000218 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000219 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000220 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
221 assert(Align <= 0x100 && "Alignment too large.");
222 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000223 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000224}
225
Bill Wendling7c04e042012-10-09 19:01:18 +0000226Attributes::Builder &Attributes::Builder::
227removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000228 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000229 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000230}
231
Bill Wendling5c407ed2012-10-14 07:17:34 +0000232Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
233 Bits |= A.Raw();
234 return *this;
235}
236
237Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000238 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000239 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000240}
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
Bill Wendling722b26c2012-10-14 07:35:59 +0000503AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
504 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000505 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000506#ifndef NDEBUG
507 // FIXME it is not obvious how this should work for alignment.
508 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000509 unsigned OldAlign = OldAttrs.getAlignment();
510 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000511 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000512 "Attempt to change alignment!");
513#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000514
Bill Wendling5c407ed2012-10-14 07:17:34 +0000515 Attributes::Builder NewAttrs =
516 Attributes::Builder(OldAttrs).addAttributes(Attrs);
517 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000518 return *this;
519
Devang Patel4c758ea2008-09-25 21:00:45 +0000520 SmallVector<AttributeWithIndex, 8> NewAttrList;
521 if (AttrList == 0)
522 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000523 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000524 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000525 unsigned i = 0, e = OldAttrList.size();
526 // Copy attributes for arguments before this one.
527 for (; i != e && OldAttrList[i].Index < Idx; ++i)
528 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000529
Chris Lattner8a923e72008-03-12 17:45:29 +0000530 // If there are attributes already at this index, merge them in.
531 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000532 Attrs =
533 Attributes::get(C, Attributes::Builder(Attrs).
534 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000535 ++i;
536 }
537
Devang Patel4c758ea2008-09-25 21:00:45 +0000538 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000539
540 // Copy attributes for arguments after this one.
541 NewAttrList.insert(NewAttrList.end(),
542 OldAttrList.begin()+i, OldAttrList.end());
543 }
544
Chris Lattner3cb6f832012-05-28 01:47:44 +0000545 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000546}
547
Bill Wendling85a64c22012-10-14 06:39:53 +0000548AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
549 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000550#ifndef NDEBUG
551 // FIXME it is not obvious how this should work for alignment.
552 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000553 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
554 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000555#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000556 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000557
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling85a64c22012-10-14 06:39:53 +0000559 Attributes::Builder NewAttrs =
560 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
561 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000562 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000563
Devang Patel4c758ea2008-09-25 21:00:45 +0000564 SmallVector<AttributeWithIndex, 8> NewAttrList;
565 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000566 unsigned i = 0, e = OldAttrList.size();
567
568 // Copy attributes for arguments before this one.
569 for (; i != e && OldAttrList[i].Index < Idx; ++i)
570 NewAttrList.push_back(OldAttrList[i]);
571
572 // If there are attributes already at this index, merge them in.
573 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling85a64c22012-10-14 06:39:53 +0000574 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
575 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000576 ++i;
577 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000578 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000579
580 // Copy attributes for arguments after this one.
581 NewAttrList.insert(NewAttrList.end(),
582 OldAttrList.begin()+i, OldAttrList.end());
583
Chris Lattner3cb6f832012-05-28 01:47:44 +0000584 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000585}
586
Devang Patel4c758ea2008-09-25 21:00:45 +0000587void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000588 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000589 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000590 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000591 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000592 }
593
David Greenef7014732010-01-05 01:29:58 +0000594 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000595}