blob: 4f3200b002f59c817ec12d024930f1cba7b70df5 [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) 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
Bill Wendling1fcc8222012-10-14 04:10:01 +0000218Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
219 Bits |= Val;
220 return *this;
221}
222
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000223Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
224 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000225 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
226 assert(Align <= 0x40000000 && "Alignment too large.");
227 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000228 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000229}
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000230Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000231 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000232 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000233 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
234 assert(Align <= 0x100 && "Alignment too large.");
235 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000236 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000237}
238
Bill Wendling7c04e042012-10-09 19:01:18 +0000239Attributes::Builder &Attributes::Builder::
240removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000241 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000242 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000243}
244
Bill Wendling70f39172012-10-09 00:01:21 +0000245void Attributes::Builder::removeAttributes(const Attributes &A) {
246 Bits &= ~A.Raw();
247}
248
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000249bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
250 return Bits & AttributesImpl::getAttrMask(A);
251}
252
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000253bool Attributes::Builder::hasAttributes() const {
254 return Bits != 0;
255}
Bill Wendling70f39172012-10-09 00:01:21 +0000256bool Attributes::Builder::hasAttributes(const Attributes &A) const {
257 return Bits & A.Raw();
258}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000259bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000260 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000261}
262
263uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000264 if (!hasAlignmentAttr())
265 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000266 return 1U <<
267 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000268}
269
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000270uint64_t Attributes::Builder::getStackAlignment() const {
271 if (!hasAlignmentAttr())
272 return 0;
273 return 1U <<
274 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
275}
276
Chris Lattner8a923e72008-03-12 17:45:29 +0000277//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000278// AttributeImpl Definition
279//===----------------------------------------------------------------------===//
280
Bill Wendling93f70b72012-10-09 09:11:20 +0000281uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000282 switch (Val) {
283 case Attributes::None: return 0;
284 case Attributes::ZExt: return 1 << 0;
285 case Attributes::SExt: return 1 << 1;
286 case Attributes::NoReturn: return 1 << 2;
287 case Attributes::InReg: return 1 << 3;
288 case Attributes::StructRet: return 1 << 4;
289 case Attributes::NoUnwind: return 1 << 5;
290 case Attributes::NoAlias: return 1 << 6;
291 case Attributes::ByVal: return 1 << 7;
292 case Attributes::Nest: return 1 << 8;
293 case Attributes::ReadNone: return 1 << 9;
294 case Attributes::ReadOnly: return 1 << 10;
295 case Attributes::NoInline: return 1 << 11;
296 case Attributes::AlwaysInline: return 1 << 12;
297 case Attributes::OptimizeForSize: return 1 << 13;
298 case Attributes::StackProtect: return 1 << 14;
299 case Attributes::StackProtectReq: return 1 << 15;
300 case Attributes::Alignment: return 31 << 16;
301 case Attributes::NoCapture: return 1 << 21;
302 case Attributes::NoRedZone: return 1 << 22;
303 case Attributes::NoImplicitFloat: return 1 << 23;
304 case Attributes::Naked: return 1 << 24;
305 case Attributes::InlineHint: return 1 << 25;
306 case Attributes::StackAlignment: return 7 << 26;
307 case Attributes::ReturnsTwice: return 1 << 29;
308 case Attributes::UWTable: return 1 << 30;
309 case Attributes::NonLazyBind: return 1U << 31;
310 case Attributes::AddressSafety: return 1ULL << 32;
311 }
312 llvm_unreachable("Unsupported attribute type");
313}
314
Bill Wendling73ea2de2012-10-08 21:47:17 +0000315bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000316 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000317}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000318
Bill Wendling73ea2de2012-10-08 21:47:17 +0000319bool AttributesImpl::hasAttributes() const {
320 return Bits != 0;
321}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000322
Bill Wendling73ea2de2012-10-08 21:47:17 +0000323bool AttributesImpl::hasAttributes(const Attributes &A) const {
324 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
325}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000326
Bill Wendling73ea2de2012-10-08 21:47:17 +0000327uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000328 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000329}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000330
Bill Wendling73ea2de2012-10-08 21:47:17 +0000331uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000332 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000333}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000334
Bill Wendling73ea2de2012-10-08 21:47:17 +0000335bool AttributesImpl::isEmptyOrSingleton() const {
336 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000337}
338
339//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000340// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000341//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000342
Owen Anderson2e831892010-11-18 18:59:13 +0000343namespace llvm {
344 class AttributeListImpl;
345}
346
347static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000348
Chris Lattner8a923e72008-03-12 17:45:29 +0000349namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000350static ManagedStatic<sys::SmartMutex<true> > ALMutex;
351
Devang Patel00095052008-09-24 00:29:49 +0000352class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000353 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000354
Devang Patel4c758ea2008-09-25 21:00:45 +0000355 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000356 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
357 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000358 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000359public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000360 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000361
Chris Lattner3cb6f832012-05-28 01:47:44 +0000362 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
363 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000364 RefCount = 0;
365 }
366
Owen Anderson9b14a252010-11-09 00:27:03 +0000367 void AddRef() {
368 sys::SmartScopedLock<true> Lock(*ALMutex);
369 ++RefCount;
370 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000371 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000372 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000373 if (!AttributesLists.isConstructed())
374 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000375 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000376 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000377 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000378 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000379
380 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000381 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000382 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000383 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
384 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
385 ID.AddInteger(Attrs[i].Attrs.Raw());
386 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000387 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000388 }
389};
390}
391
Devang Patel00095052008-09-24 00:29:49 +0000392AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000393 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000394 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000395}
396
397
Chris Lattner3cb6f832012-05-28 01:47:44 +0000398AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000399 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000400 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000401 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000402
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000403#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000404 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000405 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000406 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000407 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000408 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000409 }
410#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000411
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000412 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000413 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000414 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000415 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000416
417 sys::SmartScopedLock<true> Lock(*ALMutex);
418
Devang Patel00095052008-09-24 00:29:49 +0000419 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000420 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000421
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000422 // If we didn't find any existing attributes of the same shape then
423 // create a new one and insert it.
424 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000425 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000426 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000427 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000428
Devang Patel4c758ea2008-09-25 21:00:45 +0000429 // Return the AttributesList that we found or created.
430 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000431}
432
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000433
Chris Lattner8a923e72008-03-12 17:45:29 +0000434//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000435// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000436//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000437
Devang Patel4c758ea2008-09-25 21:00:45 +0000438AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000439 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000440}
441
Devang Patel4c758ea2008-09-25 21:00:45 +0000442AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
443 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000444}
445
Devang Patel4c758ea2008-09-25 21:00:45 +0000446const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000447 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000448 if (AttrList == RHS.AttrList) return *this;
449 if (AttrList) AttrList->DropRef();
450 AttrList = RHS.AttrList;
451 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000452 return *this;
453}
454
Devang Patel4c758ea2008-09-25 21:00:45 +0000455AttrListPtr::~AttrListPtr() {
456 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000457}
458
459/// getNumSlots - Return the number of slots used in this attribute list.
460/// This is the number of arguments that have an attribute set on them
461/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000462unsigned AttrListPtr::getNumSlots() const {
463 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000464}
465
Devang Patel4c758ea2008-09-25 21:00:45 +0000466/// getSlot - Return the AttributeWithIndex at the specified slot. This
467/// holds a number plus a set of attributes.
468const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
469 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
470 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000471}
472
473
Devang Patel4c758ea2008-09-25 21:00:45 +0000474/// getAttributes - The attributes for the specified index are
475/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000476/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000477Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000478 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000479
Devang Patel4c758ea2008-09-25 21:00:45 +0000480 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000481 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
482 if (Attrs[i].Index == Idx)
483 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000484
485 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000486}
487
488/// hasAttrSomewhere - Return true if the specified attribute is set for at
489/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000490bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000491 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000492
Devang Patel4c758ea2008-09-25 21:00:45 +0000493 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000494 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000495 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000496 return true;
497 return false;
498}
499
Bill Wendling70f39172012-10-09 00:01:21 +0000500unsigned AttrListPtr::getNumAttrs() const {
501 return AttrList ? AttrList->Attrs.size() : 0;
502}
503
504Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
505 assert(AttrList && "Trying to get an attribute from an empty list!");
506 assert(i < AttrList->Attrs.size() && "Index out of range!");
507 return AttrList->Attrs[i].Attrs;
508}
Chris Lattner8a923e72008-03-12 17:45:29 +0000509
Devang Patel4c758ea2008-09-25 21:00:45 +0000510AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
511 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000512#ifndef NDEBUG
513 // FIXME it is not obvious how this should work for alignment.
514 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000515 unsigned OldAlign = OldAttrs.getAlignment();
516 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000517 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000518 "Attempt to change alignment!");
519#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000520
Devang Patelba3fa6c2008-09-23 23:03:40 +0000521 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000522 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000523 return *this;
524
Devang Patel4c758ea2008-09-25 21:00:45 +0000525 SmallVector<AttributeWithIndex, 8> NewAttrList;
526 if (AttrList == 0)
527 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000528 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000529 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000530 unsigned i = 0, e = OldAttrList.size();
531 // Copy attributes for arguments before this one.
532 for (; i != e && OldAttrList[i].Index < Idx; ++i)
533 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000534
Chris Lattner8a923e72008-03-12 17:45:29 +0000535 // If there are attributes already at this index, merge them in.
536 if (i != e && OldAttrList[i].Index == Idx) {
537 Attrs |= OldAttrList[i].Attrs;
538 ++i;
539 }
540
Devang Patel4c758ea2008-09-25 21:00:45 +0000541 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000542
543 // Copy attributes for arguments after this one.
544 NewAttrList.insert(NewAttrList.end(),
545 OldAttrList.begin()+i, OldAttrList.end());
546 }
547
Chris Lattner3cb6f832012-05-28 01:47:44 +0000548 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000549}
550
Devang Patel4c758ea2008-09-25 21:00:45 +0000551AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000552#ifndef NDEBUG
553 // FIXME it is not obvious how this should work for alignment.
554 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000555 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
556 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000557#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000559
Devang Patel4c758ea2008-09-25 21:00:45 +0000560 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000561 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000562 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000563 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000564
Devang Patel4c758ea2008-09-25 21:00:45 +0000565 SmallVector<AttributeWithIndex, 8> NewAttrList;
566 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000567 unsigned i = 0, e = OldAttrList.size();
568
569 // Copy attributes for arguments before this one.
570 for (; i != e && OldAttrList[i].Index < Idx; ++i)
571 NewAttrList.push_back(OldAttrList[i]);
572
573 // If there are attributes already at this index, merge them in.
574 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
575 Attrs = OldAttrList[i].Attrs & ~Attrs;
576 ++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}