blob: fadb8ca6c1dbf1c612c9b47b4e40d3ff06d01ec2 [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 +000096uint64_t Attributes::Raw() const {
97 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000098}
99
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000100Attributes Attributes::typeIncompatible(Type *Ty) {
101 Attributes::Builder Incompatible;
102
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000104 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000105 Incompatible.addAttribute(Attributes::SExt)
106 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000107
Bill Wendling7c04e042012-10-09 19:01:18 +0000108 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000109 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000110 Incompatible.addAttribute(Attributes::ByVal)
111 .addAttribute(Attributes::Nest)
112 .addAttribute(Attributes::NoAlias)
113 .addAttribute(Attributes::NoCapture)
114 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000115
116 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
117}
118
Bill Wendlingde74cf52012-09-20 14:44:42 +0000119std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000120 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000121 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000122 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000123 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000125 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000126 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000127 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000128 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000129 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000130 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000131 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000132 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000133 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000135 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000136 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000137 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000138 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000139 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000140 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000150 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000152 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000154 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000156 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000158 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000160 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000162 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000164 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000166 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000168 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000170 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000172 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000173 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000174 Result += ") ";
175 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000176 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000177 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000178 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000179 Result += " ";
180 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000181 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000182 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000183 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000184 return Result;
185}
186
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000187//===----------------------------------------------------------------------===//
188// Attributes::Builder Implementation
189//===----------------------------------------------------------------------===//
190
Bill Wendling7c04e042012-10-09 19:01:18 +0000191Attributes::Builder &Attributes::Builder::
192addAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000193 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000194 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000195}
196
Bill Wendling1fcc8222012-10-14 04:10:01 +0000197Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
198 Bits |= Val;
199 return *this;
200}
201
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000202Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
203 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000204 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
205 assert(Align <= 0x40000000 && "Alignment too large.");
206 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000207 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000208}
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000209Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000210 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000211 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 <= 0x100 && "Alignment too large.");
214 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000215 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000216}
217
Bill Wendling7c04e042012-10-09 19:01:18 +0000218Attributes::Builder &Attributes::Builder::
219removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000220 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000221 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000222}
223
Bill Wendling5c407ed2012-10-14 07:17:34 +0000224Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
225 Bits |= A.Raw();
226 return *this;
227}
228
229Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000230 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000231 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000232}
233
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000234bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
235 return Bits & AttributesImpl::getAttrMask(A);
236}
237
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000238bool Attributes::Builder::hasAttributes() const {
239 return Bits != 0;
240}
Bill Wendling70f39172012-10-09 00:01:21 +0000241bool Attributes::Builder::hasAttributes(const Attributes &A) const {
242 return Bits & A.Raw();
243}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000244bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000245 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000246}
247
248uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000249 if (!hasAlignmentAttr())
250 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000251 return 1U <<
252 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000253}
254
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000255uint64_t Attributes::Builder::getStackAlignment() const {
256 if (!hasAlignmentAttr())
257 return 0;
258 return 1U <<
259 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
260}
261
Chris Lattner8a923e72008-03-12 17:45:29 +0000262//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000263// AttributeImpl Definition
264//===----------------------------------------------------------------------===//
265
Bill Wendling93f70b72012-10-09 09:11:20 +0000266uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000267 switch (Val) {
268 case Attributes::None: return 0;
269 case Attributes::ZExt: return 1 << 0;
270 case Attributes::SExt: return 1 << 1;
271 case Attributes::NoReturn: return 1 << 2;
272 case Attributes::InReg: return 1 << 3;
273 case Attributes::StructRet: return 1 << 4;
274 case Attributes::NoUnwind: return 1 << 5;
275 case Attributes::NoAlias: return 1 << 6;
276 case Attributes::ByVal: return 1 << 7;
277 case Attributes::Nest: return 1 << 8;
278 case Attributes::ReadNone: return 1 << 9;
279 case Attributes::ReadOnly: return 1 << 10;
280 case Attributes::NoInline: return 1 << 11;
281 case Attributes::AlwaysInline: return 1 << 12;
282 case Attributes::OptimizeForSize: return 1 << 13;
283 case Attributes::StackProtect: return 1 << 14;
284 case Attributes::StackProtectReq: return 1 << 15;
285 case Attributes::Alignment: return 31 << 16;
286 case Attributes::NoCapture: return 1 << 21;
287 case Attributes::NoRedZone: return 1 << 22;
288 case Attributes::NoImplicitFloat: return 1 << 23;
289 case Attributes::Naked: return 1 << 24;
290 case Attributes::InlineHint: return 1 << 25;
291 case Attributes::StackAlignment: return 7 << 26;
292 case Attributes::ReturnsTwice: return 1 << 29;
293 case Attributes::UWTable: return 1 << 30;
294 case Attributes::NonLazyBind: return 1U << 31;
295 case Attributes::AddressSafety: return 1ULL << 32;
296 }
297 llvm_unreachable("Unsupported attribute type");
298}
299
Bill Wendling73ea2de2012-10-08 21:47:17 +0000300bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000301 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000302}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000303
Bill Wendling73ea2de2012-10-08 21:47:17 +0000304bool AttributesImpl::hasAttributes() const {
305 return Bits != 0;
306}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000307
Bill Wendling73ea2de2012-10-08 21:47:17 +0000308bool AttributesImpl::hasAttributes(const Attributes &A) const {
309 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
310}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000311
Bill Wendling73ea2de2012-10-08 21:47:17 +0000312uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000313 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000314}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000315
Bill Wendling73ea2de2012-10-08 21:47:17 +0000316uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000317 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000318}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000319
Bill Wendling73ea2de2012-10-08 21:47:17 +0000320bool AttributesImpl::isEmptyOrSingleton() const {
321 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000322}
323
324//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000325// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000326//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000327
Owen Anderson2e831892010-11-18 18:59:13 +0000328namespace llvm {
329 class AttributeListImpl;
330}
331
332static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000333
Chris Lattner8a923e72008-03-12 17:45:29 +0000334namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000335static ManagedStatic<sys::SmartMutex<true> > ALMutex;
336
Devang Patel00095052008-09-24 00:29:49 +0000337class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000338 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000339
Devang Patel4c758ea2008-09-25 21:00:45 +0000340 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000341 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
342 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000343 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000344public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000345 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000346
Chris Lattner3cb6f832012-05-28 01:47:44 +0000347 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
348 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000349 RefCount = 0;
350 }
351
Owen Anderson9b14a252010-11-09 00:27:03 +0000352 void AddRef() {
353 sys::SmartScopedLock<true> Lock(*ALMutex);
354 ++RefCount;
355 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000356 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000357 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000358 if (!AttributesLists.isConstructed())
359 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000360 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000361 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000362 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000363 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000364
365 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000366 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000367 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000368 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
369 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
370 ID.AddInteger(Attrs[i].Attrs.Raw());
371 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000372 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000373 }
374};
375}
376
Devang Patel00095052008-09-24 00:29:49 +0000377AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000378 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000379 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000380}
381
382
Chris Lattner3cb6f832012-05-28 01:47:44 +0000383AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000384 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000385 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000386 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000387
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000388#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000389 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000390 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000391 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000392 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000393 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000394 }
395#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000396
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000397 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000398 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000399 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000400 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000401
402 sys::SmartScopedLock<true> Lock(*ALMutex);
403
Devang Patel00095052008-09-24 00:29:49 +0000404 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000405 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000406
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000407 // If we didn't find any existing attributes of the same shape then
408 // create a new one and insert it.
409 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000410 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000412 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000413
Devang Patel4c758ea2008-09-25 21:00:45 +0000414 // Return the AttributesList that we found or created.
415 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000416}
417
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000418
Chris Lattner8a923e72008-03-12 17:45:29 +0000419//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000420// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000421//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000422
Devang Patel4c758ea2008-09-25 21:00:45 +0000423AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000424 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000425}
426
Devang Patel4c758ea2008-09-25 21:00:45 +0000427AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
428 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000429}
430
Devang Patel4c758ea2008-09-25 21:00:45 +0000431const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000432 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000433 if (AttrList == RHS.AttrList) return *this;
434 if (AttrList) AttrList->DropRef();
435 AttrList = RHS.AttrList;
436 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000437 return *this;
438}
439
Devang Patel4c758ea2008-09-25 21:00:45 +0000440AttrListPtr::~AttrListPtr() {
441 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000442}
443
444/// getNumSlots - Return the number of slots used in this attribute list.
445/// This is the number of arguments that have an attribute set on them
446/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000447unsigned AttrListPtr::getNumSlots() const {
448 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000449}
450
Devang Patel4c758ea2008-09-25 21:00:45 +0000451/// getSlot - Return the AttributeWithIndex at the specified slot. This
452/// holds a number plus a set of attributes.
453const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
454 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
455 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000456}
457
458
Devang Patel4c758ea2008-09-25 21:00:45 +0000459/// getAttributes - The attributes for the specified index are
460/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000461/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000462Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000463 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000464
Devang Patel4c758ea2008-09-25 21:00:45 +0000465 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000466 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
467 if (Attrs[i].Index == Idx)
468 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000469
470 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000471}
472
473/// hasAttrSomewhere - Return true if the specified attribute is set for at
474/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000475bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000476 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000477
Devang Patel4c758ea2008-09-25 21:00:45 +0000478 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000479 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000480 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000481 return true;
482 return false;
483}
484
Bill Wendling70f39172012-10-09 00:01:21 +0000485unsigned AttrListPtr::getNumAttrs() const {
486 return AttrList ? AttrList->Attrs.size() : 0;
487}
488
489Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
490 assert(AttrList && "Trying to get an attribute from an empty list!");
491 assert(i < AttrList->Attrs.size() && "Index out of range!");
492 return AttrList->Attrs[i].Attrs;
493}
Chris Lattner8a923e72008-03-12 17:45:29 +0000494
Bill Wendling722b26c2012-10-14 07:35:59 +0000495AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
496 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000497 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000498#ifndef NDEBUG
499 // FIXME it is not obvious how this should work for alignment.
500 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000501 unsigned OldAlign = OldAttrs.getAlignment();
502 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000503 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000504 "Attempt to change alignment!");
505#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000506
Bill Wendling5c407ed2012-10-14 07:17:34 +0000507 Attributes::Builder NewAttrs =
508 Attributes::Builder(OldAttrs).addAttributes(Attrs);
509 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000510 return *this;
511
Devang Patel4c758ea2008-09-25 21:00:45 +0000512 SmallVector<AttributeWithIndex, 8> NewAttrList;
513 if (AttrList == 0)
514 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000515 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000516 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000517 unsigned i = 0, e = OldAttrList.size();
518 // Copy attributes for arguments before this one.
519 for (; i != e && OldAttrList[i].Index < Idx; ++i)
520 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000521
Chris Lattner8a923e72008-03-12 17:45:29 +0000522 // If there are attributes already at this index, merge them in.
523 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000524 Attrs =
525 Attributes::get(C, Attributes::Builder(Attrs).
526 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000527 ++i;
528 }
529
Devang Patel4c758ea2008-09-25 21:00:45 +0000530 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000531
532 // Copy attributes for arguments after this one.
533 NewAttrList.insert(NewAttrList.end(),
534 OldAttrList.begin()+i, OldAttrList.end());
535 }
536
Chris Lattner3cb6f832012-05-28 01:47:44 +0000537 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000538}
539
Bill Wendling85a64c22012-10-14 06:39:53 +0000540AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
541 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000542#ifndef NDEBUG
543 // FIXME it is not obvious how this should work for alignment.
544 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000545 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
546 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000547#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000548 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling85a64c22012-10-14 06:39:53 +0000551 Attributes::Builder NewAttrs =
552 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
553 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000554 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000555
Devang Patel4c758ea2008-09-25 21:00:45 +0000556 SmallVector<AttributeWithIndex, 8> NewAttrList;
557 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000558 unsigned i = 0, e = OldAttrList.size();
559
560 // Copy attributes for arguments before this one.
561 for (; i != e && OldAttrList[i].Index < Idx; ++i)
562 NewAttrList.push_back(OldAttrList[i]);
563
564 // If there are attributes already at this index, merge them in.
565 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling85a64c22012-10-14 06:39:53 +0000566 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
567 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000568 ++i;
569 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000570 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000571
572 // Copy attributes for arguments after this one.
573 NewAttrList.insert(NewAttrList.end(),
574 OldAttrList.begin()+i, OldAttrList.end());
575
Chris Lattner3cb6f832012-05-28 01:47:44 +0000576 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000577}
578
Devang Patel4c758ea2008-09-25 21:00:45 +0000579void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000580 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000581 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000582 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000583 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000584 }
585
David Greenef7014732010-01-05 01:29:58 +0000586 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000587}