blob: 010a356dea7233205f826e7d500b1a0514e6461b [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 Wendlingd079a442012-10-15 04:46:55 +000030Attributes::Attributes(LLVMContext &C, ArrayRef<AttrVal> Vals) {
31 Attributes::Builder B;
32 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
33 I != E; ++I)
34 B.addAttribute(*I);
35 Attrs = Attributes::get(C, B).Attrs;
36}
Bill Wendling73ea2de2012-10-08 21:47:17 +000037
Bill Wendlingd079a442012-10-15 04:46:55 +000038Attributes::Attributes(AttributesImpl *A) : Attrs(A) {}
Bill Wendling73ea2de2012-10-08 21:47:17 +000039
Bill Wendlingc6daefa2012-10-08 23:27:46 +000040Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
41
Bill Wendling73ea2de2012-10-08 21:47:17 +000042Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
43 // If there are no attributes, return an empty Attributes class.
44 if (B.Bits == 0)
45 return Attributes();
46
47 // Otherwise, build a key to look up the existing attributes.
48 LLVMContextImpl *pImpl = Context.pImpl;
49 FoldingSetNodeID ID;
50 ID.AddInteger(B.Bits);
51
52 void *InsertPoint;
53 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
54
55 if (!PA) {
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
58 PA = new AttributesImpl(B.Bits);
59 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 }
61
62 // Return the AttributesList that we found or created.
63 return Attributes(PA);
64}
65
Bill Wendlingc9b22d72012-10-09 07:45:08 +000066bool Attributes::hasAttribute(AttrVal Val) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000067 return Attrs && Attrs->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000068}
69
Bill Wendling93f70b72012-10-09 09:11:20 +000070bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000071 return Attrs && Attrs->hasAttributes(A);
Bill Wendling93f70b72012-10-09 09:11:20 +000072}
73
Bill Wendlingabf3feb2012-10-05 06:44:41 +000074/// This returns the alignment field of an attribute as a byte alignment value.
75unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000076 if (!hasAttribute(Attributes::Alignment))
77 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000078 return 1U << ((Attrs->getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079}
80
81/// This returns the stack alignment field of an attribute as a byte alignment
82/// value.
83unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000084 if (!hasAttribute(Attributes::StackAlignment))
85 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000086 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000087}
88
Bill Wendling73ea2de2012-10-08 21:47:17 +000089uint64_t Attributes::Raw() const {
Bill Wendlingd079a442012-10-15 04:46:55 +000090 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000091}
92
Bill Wendlingabf3feb2012-10-05 06:44:41 +000093Attributes Attributes::typeIncompatible(Type *Ty) {
94 Attributes::Builder Incompatible;
95
Bill Wendling7c04e042012-10-09 19:01:18 +000096 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +000097 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +000098 Incompatible.addAttribute(Attributes::SExt)
99 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000100
Bill Wendling7c04e042012-10-09 19:01:18 +0000101 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000102 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 Incompatible.addAttribute(Attributes::ByVal)
104 .addAttribute(Attributes::Nest)
105 .addAttribute(Attributes::NoAlias)
106 .addAttribute(Attributes::NoCapture)
107 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000108
Bill Wendlingd079a442012-10-15 04:46:55 +0000109 return Attributes::get(Ty->getContext(), Incompatible);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000110}
111
Bill Wendlingde74cf52012-09-20 14:44:42 +0000112std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000113 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000114 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000115 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000116 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000117 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000118 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000119 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000120 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000121 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000122 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000123 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000124 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000125 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000126 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000127 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000128 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000129 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000130 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000131 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000132 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000133 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000134 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000135 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000136 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000137 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000138 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000139 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000140 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000142 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000143 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000144 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000145 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000146 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000147 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000148 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000149 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000150 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000151 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000152 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000153 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000154 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000155 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000156 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000157 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000158 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000159 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000160 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000161 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000162 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000163 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000164 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000165 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000166 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000167 Result += ") ";
168 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000170 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000171 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000172 Result += " ";
173 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000174 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000175 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000176 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000177 return Result;
178}
179
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000180//===----------------------------------------------------------------------===//
181// Attributes::Builder Implementation
182//===----------------------------------------------------------------------===//
183
Bill Wendling7c04e042012-10-09 19:01:18 +0000184Attributes::Builder &Attributes::Builder::
185addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000186 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000187 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000188}
189
Bill Wendling1fcc8222012-10-14 04:10:01 +0000190Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
191 Bits |= Val;
192 return *this;
193}
194
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000195Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
196 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000197 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
198 assert(Align <= 0x40000000 && "Alignment too large.");
199 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000200 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000201}
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000202Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000203 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000204 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000205 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
206 assert(Align <= 0x100 && "Alignment too large.");
207 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000208 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000209}
210
Bill Wendling7c04e042012-10-09 19:01:18 +0000211Attributes::Builder &Attributes::Builder::
212removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000213 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000214 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000215}
216
Bill Wendling5c407ed2012-10-14 07:17:34 +0000217Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
218 Bits |= A.Raw();
219 return *this;
220}
221
222Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000223 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000224 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000225}
226
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000227bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
228 return Bits & AttributesImpl::getAttrMask(A);
229}
230
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000231bool Attributes::Builder::hasAttributes() const {
232 return Bits != 0;
233}
Bill Wendling70f39172012-10-09 00:01:21 +0000234bool Attributes::Builder::hasAttributes(const Attributes &A) const {
235 return Bits & A.Raw();
236}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000237bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000238 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000239}
240
241uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000242 if (!hasAlignmentAttr())
243 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000244 return 1U <<
245 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000246}
247
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000248uint64_t Attributes::Builder::getStackAlignment() const {
249 if (!hasAlignmentAttr())
250 return 0;
251 return 1U <<
252 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
253}
254
Chris Lattner8a923e72008-03-12 17:45:29 +0000255//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000256// AttributeImpl Definition
257//===----------------------------------------------------------------------===//
258
Bill Wendling93f70b72012-10-09 09:11:20 +0000259uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000260 switch (Val) {
261 case Attributes::None: return 0;
262 case Attributes::ZExt: return 1 << 0;
263 case Attributes::SExt: return 1 << 1;
264 case Attributes::NoReturn: return 1 << 2;
265 case Attributes::InReg: return 1 << 3;
266 case Attributes::StructRet: return 1 << 4;
267 case Attributes::NoUnwind: return 1 << 5;
268 case Attributes::NoAlias: return 1 << 6;
269 case Attributes::ByVal: return 1 << 7;
270 case Attributes::Nest: return 1 << 8;
271 case Attributes::ReadNone: return 1 << 9;
272 case Attributes::ReadOnly: return 1 << 10;
273 case Attributes::NoInline: return 1 << 11;
274 case Attributes::AlwaysInline: return 1 << 12;
275 case Attributes::OptimizeForSize: return 1 << 13;
276 case Attributes::StackProtect: return 1 << 14;
277 case Attributes::StackProtectReq: return 1 << 15;
278 case Attributes::Alignment: return 31 << 16;
279 case Attributes::NoCapture: return 1 << 21;
280 case Attributes::NoRedZone: return 1 << 22;
281 case Attributes::NoImplicitFloat: return 1 << 23;
282 case Attributes::Naked: return 1 << 24;
283 case Attributes::InlineHint: return 1 << 25;
284 case Attributes::StackAlignment: return 7 << 26;
285 case Attributes::ReturnsTwice: return 1 << 29;
286 case Attributes::UWTable: return 1 << 30;
287 case Attributes::NonLazyBind: return 1U << 31;
288 case Attributes::AddressSafety: return 1ULL << 32;
289 }
290 llvm_unreachable("Unsupported attribute type");
291}
292
Bill Wendling73ea2de2012-10-08 21:47:17 +0000293bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000294 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000295}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000296
Bill Wendling73ea2de2012-10-08 21:47:17 +0000297bool AttributesImpl::hasAttributes() const {
298 return Bits != 0;
299}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000300
Bill Wendling73ea2de2012-10-08 21:47:17 +0000301bool AttributesImpl::hasAttributes(const Attributes &A) const {
302 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
303}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000304
Bill Wendling73ea2de2012-10-08 21:47:17 +0000305uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000306 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000307}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000308
Bill Wendling73ea2de2012-10-08 21:47:17 +0000309uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000310 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000311}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000312
Bill Wendlinge38b8042012-09-26 21:07:29 +0000313//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000314// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000315//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000316
Owen Anderson2e831892010-11-18 18:59:13 +0000317namespace llvm {
318 class AttributeListImpl;
319}
320
321static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000322
Chris Lattner8a923e72008-03-12 17:45:29 +0000323namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000324static ManagedStatic<sys::SmartMutex<true> > ALMutex;
325
Devang Patel00095052008-09-24 00:29:49 +0000326class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000327 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000328
Devang Patel4c758ea2008-09-25 21:00:45 +0000329 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000330 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
331 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000332 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000333public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000334 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000335
Chris Lattner3cb6f832012-05-28 01:47:44 +0000336 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
337 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000338 RefCount = 0;
339 }
340
Owen Anderson9b14a252010-11-09 00:27:03 +0000341 void AddRef() {
342 sys::SmartScopedLock<true> Lock(*ALMutex);
343 ++RefCount;
344 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000345 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000346 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000347 if (!AttributesLists.isConstructed())
348 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000349 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000350 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000351 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000352 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000353
354 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000355 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000356 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000357 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
358 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
359 ID.AddInteger(Attrs[i].Attrs.Raw());
360 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000361 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000362 }
363};
364}
365
Devang Patel00095052008-09-24 00:29:49 +0000366AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000367 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000368 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000369}
370
371
Chris Lattner3cb6f832012-05-28 01:47:44 +0000372AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000373 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000374 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000375 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000376
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000377#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000378 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000379 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000380 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000381 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000382 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000383 }
384#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000385
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000386 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000387 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000388 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000389 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000390
391 sys::SmartScopedLock<true> Lock(*ALMutex);
392
Devang Patel00095052008-09-24 00:29:49 +0000393 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000394 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000395
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000396 // If we didn't find any existing attributes of the same shape then
397 // create a new one and insert it.
398 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000399 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000401 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000402
Devang Patel4c758ea2008-09-25 21:00:45 +0000403 // Return the AttributesList that we found or created.
404 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000405}
406
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000407
Chris Lattner8a923e72008-03-12 17:45:29 +0000408//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000409// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000410//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000411
Devang Patel4c758ea2008-09-25 21:00:45 +0000412AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000413 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000414}
415
Devang Patel4c758ea2008-09-25 21:00:45 +0000416AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
417 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000418}
419
Devang Patel4c758ea2008-09-25 21:00:45 +0000420const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000421 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000422 if (AttrList == RHS.AttrList) return *this;
423 if (AttrList) AttrList->DropRef();
424 AttrList = RHS.AttrList;
425 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000426 return *this;
427}
428
Devang Patel4c758ea2008-09-25 21:00:45 +0000429AttrListPtr::~AttrListPtr() {
430 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000431}
432
433/// getNumSlots - Return the number of slots used in this attribute list.
434/// This is the number of arguments that have an attribute set on them
435/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000436unsigned AttrListPtr::getNumSlots() const {
437 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000438}
439
Devang Patel4c758ea2008-09-25 21:00:45 +0000440/// getSlot - Return the AttributeWithIndex at the specified slot. This
441/// holds a number plus a set of attributes.
442const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
443 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
444 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000445}
446
447
Devang Patel4c758ea2008-09-25 21:00:45 +0000448/// getAttributes - The attributes for the specified index are
449/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000450/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000451Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000452 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000453
Devang Patel4c758ea2008-09-25 21:00:45 +0000454 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000455 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
456 if (Attrs[i].Index == Idx)
457 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000458
459 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000460}
461
462/// hasAttrSomewhere - Return true if the specified attribute is set for at
463/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000464bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000465 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000466
Devang Patel4c758ea2008-09-25 21:00:45 +0000467 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000468 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000469 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000470 return true;
471 return false;
472}
473
Bill Wendling70f39172012-10-09 00:01:21 +0000474unsigned AttrListPtr::getNumAttrs() const {
475 return AttrList ? AttrList->Attrs.size() : 0;
476}
477
478Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
479 assert(AttrList && "Trying to get an attribute from an empty list!");
480 assert(i < AttrList->Attrs.size() && "Index out of range!");
481 return AttrList->Attrs[i].Attrs;
482}
Chris Lattner8a923e72008-03-12 17:45:29 +0000483
Bill Wendling722b26c2012-10-14 07:35:59 +0000484AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
485 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000486 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000487#ifndef NDEBUG
488 // FIXME it is not obvious how this should work for alignment.
489 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000490 unsigned OldAlign = OldAttrs.getAlignment();
491 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000492 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000493 "Attempt to change alignment!");
494#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000495
Bill Wendling5c407ed2012-10-14 07:17:34 +0000496 Attributes::Builder NewAttrs =
497 Attributes::Builder(OldAttrs).addAttributes(Attrs);
498 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000499 return *this;
500
Devang Patel4c758ea2008-09-25 21:00:45 +0000501 SmallVector<AttributeWithIndex, 8> NewAttrList;
502 if (AttrList == 0)
503 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000504 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000505 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000506 unsigned i = 0, e = OldAttrList.size();
507 // Copy attributes for arguments before this one.
508 for (; i != e && OldAttrList[i].Index < Idx; ++i)
509 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000510
Chris Lattner8a923e72008-03-12 17:45:29 +0000511 // If there are attributes already at this index, merge them in.
512 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000513 Attrs =
Bill Wendlingd079a442012-10-15 04:46:55 +0000514 Attributes::get(C, Attributes::Builder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000515 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000516 ++i;
517 }
518
Devang Patel4c758ea2008-09-25 21:00:45 +0000519 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000520
521 // Copy attributes for arguments after this one.
522 NewAttrList.insert(NewAttrList.end(),
523 OldAttrList.begin()+i, OldAttrList.end());
524 }
525
Chris Lattner3cb6f832012-05-28 01:47:44 +0000526 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000527}
528
Bill Wendling85a64c22012-10-14 06:39:53 +0000529AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
530 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000531#ifndef NDEBUG
532 // FIXME it is not obvious how this should work for alignment.
533 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000534 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
535 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000536#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000537 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000538
Devang Patel4c758ea2008-09-25 21:00:45 +0000539 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling85a64c22012-10-14 06:39:53 +0000540 Attributes::Builder NewAttrs =
541 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
542 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000543 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000544
Devang Patel4c758ea2008-09-25 21:00:45 +0000545 SmallVector<AttributeWithIndex, 8> NewAttrList;
546 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000547 unsigned i = 0, e = OldAttrList.size();
548
549 // Copy attributes for arguments before this one.
550 for (; i != e && OldAttrList[i].Index < Idx; ++i)
551 NewAttrList.push_back(OldAttrList[i]);
552
553 // If there are attributes already at this index, merge them in.
554 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendlingd079a442012-10-15 04:46:55 +0000555 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000556 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000557 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000558 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000560
561 // Copy attributes for arguments after this one.
562 NewAttrList.insert(NewAttrList.end(),
563 OldAttrList.begin()+i, OldAttrList.end());
564
Chris Lattner3cb6f832012-05-28 01:47:44 +0000565 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000566}
567
Devang Patel4c758ea2008-09-25 21:00:45 +0000568void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000569 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000570 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000571 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000572 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000573 }
574
David Greenef7014732010-01-05 01:29:58 +0000575 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000576}