blob: 326afc7ba93ad8f3e4883d3abc3e62b71a1fac87 [file] [log] [blame]
Devang Patel05988662008-09-25 21:00:45 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-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 Patel05988662008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Pateleaf42ab2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000015#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000016#include "llvm/Type.h"
Dan Gohmanac9dff62008-03-10 23:55:07 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000019#include "llvm/Support/Atomic.h"
20#include "llvm/Support/Mutex.h"
David Greeneef1894e2010-01-05 01:29:58 +000021#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000022#include "llvm/Support/ManagedStatic.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000024using namespace llvm;
25
Chris Lattner58d74912008-03-12 17:45:29 +000026//===----------------------------------------------------------------------===//
Bill Wendling8e635db2012-10-08 21:47:17 +000027// Attributes Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000029
Bill Wendling8e635db2012-10-08 21:47:17 +000030Attributes::Attributes(uint64_t Val) : Attrs(Val) {}
31
Bill Wendling96a27942012-10-11 01:10:00 +000032Attributes::Attributes(LLVMContext &C, AttrVal Val)
Bill Wendling31839512012-10-11 01:05:52 +000033 : Attrs(Attributes::get(Attributes::Builder().addAttribute(Val)).Attrs) {}
34
Bill Wendling8e635db2012-10-08 21:47:17 +000035Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
36
Bill Wendlingf385f4c2012-10-08 23:27:46 +000037Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
38
Bill Wendling03272442012-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 Wendling8e635db2012-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 Wendling67658342012-10-09 07:45:08 +000069bool Attributes::hasAttribute(AttrVal Val) const {
70 return Attrs.hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000071}
72
Bill Wendling2e879bc2012-10-09 09:11:20 +000073bool Attributes::hasAttributes(const Attributes &A) const {
74 return Attrs.hasAttributes(A);
75}
76
Bill Wendlinge66f3d32012-10-05 06:44:41 +000077/// This returns the alignment field of an attribute as a byte alignment value.
78unsigned Attributes::getAlignment() const {
Bill Wendling9ef99c92012-10-09 20:56:48 +000079 if (!hasAttribute(Attributes::Alignment))
80 return 0;
81 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlinge66f3d32012-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 Wendling9ef99c92012-10-09 20:56:48 +000087 if (!hasAttribute(Attributes::StackAlignment))
88 return 0;
89 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000090}
91
Bill Wendlingb10c88f2012-10-07 08:55:05 +000092bool Attributes::isEmptyOrSingleton() const {
Bill Wendling8e635db2012-10-08 21:47:17 +000093 return Attrs.isEmptyOrSingleton();
Bill Wendlingb10c88f2012-10-07 08:55:05 +000094}
95
Bill Wendling8e635db2012-10-08 21:47:17 +000096Attributes Attributes::operator | (const Attributes &A) const {
97 return Attributes(Raw() | A.Raw());
Bill Wendlingb10c88f2012-10-07 08:55:05 +000098}
Bill Wendling8e635db2012-10-08 21:47:17 +000099Attributes Attributes::operator & (const Attributes &A) const {
100 return Attributes(Raw() & A.Raw());
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000101}
Bill Wendling8e635db2012-10-08 21:47:17 +0000102Attributes Attributes::operator ^ (const Attributes &A) const {
103 return Attributes(Raw() ^ A.Raw());
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000104}
Bill Wendling8e635db2012-10-08 21:47:17 +0000105Attributes &Attributes::operator |= (const Attributes &A) {
106 Attrs.Bits |= A.Raw();
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000107 return *this;
108}
Bill Wendling8e635db2012-10-08 21:47:17 +0000109Attributes &Attributes::operator &= (const Attributes &A) {
110 Attrs.Bits &= A.Raw();
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000111 return *this;
112}
Bill Wendling8e635db2012-10-08 21:47:17 +0000113
114uint64_t Attributes::Raw() const {
115 return Attrs.Bits;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000116}
117
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000118Attributes Attributes::typeIncompatible(Type *Ty) {
119 Attributes::Builder Incompatible;
120
Bill Wendling3a106e62012-10-09 19:01:18 +0000121 if (!Ty->isIntegerTy())
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000122 // Attributes that only apply to integers.
Bill Wendling3a106e62012-10-09 19:01:18 +0000123 Incompatible.addAttribute(Attributes::SExt)
124 .addAttribute(Attributes::ZExt);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000125
Bill Wendling3a106e62012-10-09 19:01:18 +0000126 if (!Ty->isPointerTy())
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000127 // Attributes that only apply to pointers.
Bill Wendling3a106e62012-10-09 19:01:18 +0000128 Incompatible.addAttribute(Attributes::ByVal)
129 .addAttribute(Attributes::Nest)
130 .addAttribute(Attributes::NoAlias)
131 .addAttribute(Attributes::NoCapture)
132 .addAttribute(Attributes::StructRet);
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000133
134 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
135}
136
Bill Wendling8ce1e432012-09-20 14:44:42 +0000137std::string Attributes::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000138 std::string Result;
Bill Wendling67658342012-10-09 07:45:08 +0000139 if (hasAttribute(Attributes::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000140 Result += "zeroext ";
Bill Wendling67658342012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000142 Result += "signext ";
Bill Wendling67658342012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000144 Result += "noreturn ";
Bill Wendling67658342012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000146 Result += "nounwind ";
Bill Wendling67658342012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000148 Result += "uwtable ";
Bill Wendling67658342012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000150 Result += "returns_twice ";
Bill Wendling67658342012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000152 Result += "inreg ";
Bill Wendling67658342012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000154 Result += "noalias ";
Bill Wendling67658342012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000156 Result += "nocapture ";
Bill Wendling67658342012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000158 Result += "sret ";
Bill Wendling67658342012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000160 Result += "byval ";
Bill Wendling67658342012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000162 Result += "nest ";
Bill Wendling67658342012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000164 Result += "readnone ";
Bill Wendling67658342012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000166 Result += "readonly ";
Bill Wendling67658342012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000168 Result += "optsize ";
Bill Wendling67658342012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000170 Result += "noinline ";
Bill Wendling67658342012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000172 Result += "inlinehint ";
Bill Wendling67658342012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000174 Result += "alwaysinline ";
Bill Wendling67658342012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000176 Result += "ssp ";
Bill Wendling67658342012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000178 Result += "sspreq ";
Bill Wendling67658342012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000180 Result += "noredzone ";
Bill Wendling67658342012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000182 Result += "noimplicitfloat ";
Bill Wendling67658342012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000184 Result += "naked ";
Bill Wendling67658342012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000186 Result += "nonlazybind ";
Bill Wendling67658342012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000188 Result += "address_safety ";
Bill Wendling67658342012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000190 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000191 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000192 Result += ") ";
193 }
Bill Wendling67658342012-10-09 07:45:08 +0000194 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000195 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000196 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000197 Result += " ";
198 }
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000199 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000200 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000201 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000202 return Result;
203}
204
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000205//===----------------------------------------------------------------------===//
206// Attributes::Builder Implementation
207//===----------------------------------------------------------------------===//
208
Bill Wendling3a106e62012-10-09 19:01:18 +0000209Attributes::Builder &Attributes::Builder::
210addAttribute(Attributes::AttrVal Val) {
Bill Wendling2e879bc2012-10-09 09:11:20 +0000211 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000212 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000213}
214
Bill Wendlinga19a5302012-10-14 04:10:01 +0000215Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
216 Bits |= Val;
217 return *this;
218}
219
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000220Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
221 if (Align == 0) return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000222 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
223 assert(Align <= 0x40000000 && "Alignment too large.");
224 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000225 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000226}
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000227Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000228 // Default alignment, allow the target to define how to align it.
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000229 if (Align == 0) return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000230 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
231 assert(Align <= 0x100 && "Alignment too large.");
232 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000233 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000234}
235
Bill Wendling3a106e62012-10-09 19:01:18 +0000236Attributes::Builder &Attributes::Builder::
237removeAttribute(Attributes::AttrVal Val) {
Bill Wendling2e879bc2012-10-09 09:11:20 +0000238 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000239 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000240}
241
Bill Wendling5886b7b2012-10-14 06:39:53 +0000242Attributes::Builder &Attributes::Builder::
243removeAttributes(const Attributes &A) {
Bill Wendling8831c062012-10-09 00:01:21 +0000244 Bits &= ~A.Raw();
Bill Wendling5886b7b2012-10-14 06:39:53 +0000245 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000246}
247
Bill Wendling7d2f2492012-10-10 07:36:45 +0000248bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
249 return Bits & AttributesImpl::getAttrMask(A);
250}
251
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000252bool Attributes::Builder::hasAttributes() const {
253 return Bits != 0;
254}
Bill Wendling8831c062012-10-09 00:01:21 +0000255bool Attributes::Builder::hasAttributes(const Attributes &A) const {
256 return Bits & A.Raw();
257}
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000258bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling6b73e8a2012-10-09 20:28:54 +0000259 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000260}
261
262uint64_t Attributes::Builder::getAlignment() const {
Bill Wendling9ef99c92012-10-09 20:56:48 +0000263 if (!hasAlignmentAttr())
264 return 0;
Bill Wendling6b73e8a2012-10-09 20:28:54 +0000265 return 1U <<
266 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000267}
268
Bill Wendling7d2f2492012-10-10 07:36:45 +0000269uint64_t Attributes::Builder::getStackAlignment() const {
270 if (!hasAlignmentAttr())
271 return 0;
272 return 1U <<
273 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
274}
275
Chris Lattner58d74912008-03-12 17:45:29 +0000276//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000277// AttributeImpl Definition
278//===----------------------------------------------------------------------===//
279
Bill Wendling2e879bc2012-10-09 09:11:20 +0000280uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000281 switch (Val) {
282 case Attributes::None: return 0;
283 case Attributes::ZExt: return 1 << 0;
284 case Attributes::SExt: return 1 << 1;
285 case Attributes::NoReturn: return 1 << 2;
286 case Attributes::InReg: return 1 << 3;
287 case Attributes::StructRet: return 1 << 4;
288 case Attributes::NoUnwind: return 1 << 5;
289 case Attributes::NoAlias: return 1 << 6;
290 case Attributes::ByVal: return 1 << 7;
291 case Attributes::Nest: return 1 << 8;
292 case Attributes::ReadNone: return 1 << 9;
293 case Attributes::ReadOnly: return 1 << 10;
294 case Attributes::NoInline: return 1 << 11;
295 case Attributes::AlwaysInline: return 1 << 12;
296 case Attributes::OptimizeForSize: return 1 << 13;
297 case Attributes::StackProtect: return 1 << 14;
298 case Attributes::StackProtectReq: return 1 << 15;
299 case Attributes::Alignment: return 31 << 16;
300 case Attributes::NoCapture: return 1 << 21;
301 case Attributes::NoRedZone: return 1 << 22;
302 case Attributes::NoImplicitFloat: return 1 << 23;
303 case Attributes::Naked: return 1 << 24;
304 case Attributes::InlineHint: return 1 << 25;
305 case Attributes::StackAlignment: return 7 << 26;
306 case Attributes::ReturnsTwice: return 1 << 29;
307 case Attributes::UWTable: return 1 << 30;
308 case Attributes::NonLazyBind: return 1U << 31;
309 case Attributes::AddressSafety: return 1ULL << 32;
310 }
311 llvm_unreachable("Unsupported attribute type");
312}
313
Bill Wendling8e635db2012-10-08 21:47:17 +0000314bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendling67658342012-10-09 07:45:08 +0000315 return (Bits & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000316}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000317
Bill Wendling8e635db2012-10-08 21:47:17 +0000318bool AttributesImpl::hasAttributes() const {
319 return Bits != 0;
320}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000321
Bill Wendling8e635db2012-10-08 21:47:17 +0000322bool AttributesImpl::hasAttributes(const Attributes &A) const {
323 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
324}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000325
Bill Wendling8e635db2012-10-08 21:47:17 +0000326uint64_t AttributesImpl::getAlignment() const {
Bill Wendling9ef99c92012-10-09 20:56:48 +0000327 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000328}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000329
Bill Wendling8e635db2012-10-08 21:47:17 +0000330uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendling9ef99c92012-10-09 20:56:48 +0000331 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling8e635db2012-10-08 21:47:17 +0000332}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000333
Bill Wendling8e635db2012-10-08 21:47:17 +0000334bool AttributesImpl::isEmptyOrSingleton() const {
335 return (Bits & (Bits - 1)) == 0;
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000336}
337
338//===----------------------------------------------------------------------===//
Devang Patel1e480002008-09-24 00:29:49 +0000339// AttributeListImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000340//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000341
Owen Anderson9fe010e2010-11-18 18:59:13 +0000342namespace llvm {
343 class AttributeListImpl;
344}
345
346static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson3afb0242010-11-09 00:27:03 +0000347
Chris Lattner58d74912008-03-12 17:45:29 +0000348namespace llvm {
Owen Anderson3afb0242010-11-09 00:27:03 +0000349static ManagedStatic<sys::SmartMutex<true> > ALMutex;
350
Devang Patel1e480002008-09-24 00:29:49 +0000351class AttributeListImpl : public FoldingSetNode {
Owen Anderson72689b42009-08-20 19:03:20 +0000352 sys::cas_flag RefCount;
Chris Lattner58d74912008-03-12 17:45:29 +0000353
Devang Patel05988662008-09-25 21:00:45 +0000354 // AttributesList is uniqued, these should not be publicly available.
Craig Topperc2945e42012-09-18 02:01:41 +0000355 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
356 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel1e480002008-09-24 00:29:49 +0000357 ~AttributeListImpl(); // Private implementation
Chris Lattner58d74912008-03-12 17:45:29 +0000358public:
Devang Patel05988662008-09-25 21:00:45 +0000359 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000360
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000361 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
362 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner58d74912008-03-12 17:45:29 +0000363 RefCount = 0;
364 }
365
Owen Anderson3afb0242010-11-09 00:27:03 +0000366 void AddRef() {
367 sys::SmartScopedLock<true> Lock(*ALMutex);
368 ++RefCount;
369 }
Owen Anderson72689b42009-08-20 19:03:20 +0000370 void DropRef() {
Owen Anderson3afb0242010-11-09 00:27:03 +0000371 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson9fe010e2010-11-18 18:59:13 +0000372 if (!AttributesLists.isConstructed())
373 return;
Owen Anderson80f3d782010-11-09 17:47:10 +0000374 sys::cas_flag new_val = --RefCount;
Owen Andersond35b1a22010-11-09 17:46:38 +0000375 if (new_val == 0)
Owen Anderson3afb0242010-11-09 00:27:03 +0000376 delete this;
Owen Anderson72689b42009-08-20 19:03:20 +0000377 }
Chris Lattner58d74912008-03-12 17:45:29 +0000378
379 void Profile(FoldingSetNodeID &ID) const {
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000380 Profile(ID, Attrs);
Chris Lattner58d74912008-03-12 17:45:29 +0000381 }
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000382 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
383 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
384 ID.AddInteger(Attrs[i].Attrs.Raw());
385 ID.AddInteger(Attrs[i].Index);
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000386 }
Chris Lattner58d74912008-03-12 17:45:29 +0000387 }
388};
389}
390
Devang Patel1e480002008-09-24 00:29:49 +0000391AttributeListImpl::~AttributeListImpl() {
Owen Anderson3afb0242010-11-09 00:27:03 +0000392 // NOTE: Lock must be acquired by caller.
Devang Patel05988662008-09-25 21:00:45 +0000393 AttributesLists->RemoveNode(this);
Chris Lattner58d74912008-03-12 17:45:29 +0000394}
395
396
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000397AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000398 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000399 if (Attrs.empty())
Devang Patel05988662008-09-25 21:00:45 +0000400 return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000401
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000402#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000403 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlingef99fe82012-09-21 15:26:31 +0000404 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000405 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000406 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000407 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000408 }
409#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000410
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000411 // Otherwise, build a key to look up the existing attributes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000412 FoldingSetNodeID ID;
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000413 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000414 void *InsertPos;
Owen Anderson596c1dc2009-08-17 17:10:58 +0000415
416 sys::SmartScopedLock<true> Lock(*ALMutex);
417
Devang Patel1e480002008-09-24 00:29:49 +0000418 AttributeListImpl *PAL =
Devang Patel05988662008-09-25 21:00:45 +0000419 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner58d74912008-03-12 17:45:29 +0000420
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000421 // If we didn't find any existing attributes of the same shape then
422 // create a new one and insert it.
423 if (!PAL) {
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000424 PAL = new AttributeListImpl(Attrs);
Devang Patel05988662008-09-25 21:00:45 +0000425 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000426 }
Chris Lattner58d74912008-03-12 17:45:29 +0000427
Devang Patel05988662008-09-25 21:00:45 +0000428 // Return the AttributesList that we found or created.
429 return AttrListPtr(PAL);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000430}
431
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000432
Chris Lattner58d74912008-03-12 17:45:29 +0000433//===----------------------------------------------------------------------===//
Devang Patel05988662008-09-25 21:00:45 +0000434// AttrListPtr Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000435//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000436
Devang Patel05988662008-09-25 21:00:45 +0000437AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner58d74912008-03-12 17:45:29 +0000438 if (LI) LI->AddRef();
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000439}
440
Devang Patel05988662008-09-25 21:00:45 +0000441AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
442 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000443}
444
Devang Patel05988662008-09-25 21:00:45 +0000445const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Andersoncb86def2010-09-16 00:27:35 +0000446 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel05988662008-09-25 21:00:45 +0000447 if (AttrList == RHS.AttrList) return *this;
448 if (AttrList) AttrList->DropRef();
449 AttrList = RHS.AttrList;
450 if (AttrList) AttrList->AddRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000451 return *this;
452}
453
Devang Patel05988662008-09-25 21:00:45 +0000454AttrListPtr::~AttrListPtr() {
455 if (AttrList) AttrList->DropRef();
Chris Lattner58d74912008-03-12 17:45:29 +0000456}
457
458/// getNumSlots - Return the number of slots used in this attribute list.
459/// This is the number of arguments that have an attribute set on them
460/// (including the function itself).
Devang Patel05988662008-09-25 21:00:45 +0000461unsigned AttrListPtr::getNumSlots() const {
462 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000463}
464
Devang Patel05988662008-09-25 21:00:45 +0000465/// getSlot - Return the AttributeWithIndex at the specified slot. This
466/// holds a number plus a set of attributes.
467const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
468 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
469 return AttrList->Attrs[Slot];
Chris Lattner58d74912008-03-12 17:45:29 +0000470}
471
472
Devang Patel05988662008-09-25 21:00:45 +0000473/// getAttributes - The attributes for the specified index are
474/// returned. Attributes for the result are denoted with Idx = 0.
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000475/// Function notes are denoted with idx = ~0.
Devang Patel05988662008-09-25 21:00:45 +0000476Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendlingef99fe82012-09-21 15:26:31 +0000477 if (AttrList == 0) return Attributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000478
Devang Patel05988662008-09-25 21:00:45 +0000479 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000480 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
481 if (Attrs[i].Index == Idx)
482 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000483
484 return Attributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000485}
486
487/// hasAttrSomewhere - Return true if the specified attribute is set for at
488/// least one parameter or for the return value.
Bill Wendling7d2f2492012-10-10 07:36:45 +0000489bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000490 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000491
Devang Patel05988662008-09-25 21:00:45 +0000492 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000493 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000494 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000495 return true;
496 return false;
497}
498
Bill Wendling8831c062012-10-09 00:01:21 +0000499unsigned AttrListPtr::getNumAttrs() const {
500 return AttrList ? AttrList->Attrs.size() : 0;
501}
502
503Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
504 assert(AttrList && "Trying to get an attribute from an empty list!");
505 assert(i < AttrList->Attrs.size() && "Index out of range!");
506 return AttrList->Attrs[i].Attrs;
507}
Chris Lattner58d74912008-03-12 17:45:29 +0000508
Devang Patel05988662008-09-25 21:00:45 +0000509AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
510 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000511#ifndef NDEBUG
512 // FIXME it is not obvious how this should work for alignment.
513 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000514 unsigned OldAlign = OldAttrs.getAlignment();
515 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000516 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000517 "Attempt to change alignment!");
518#endif
Chris Lattner58d74912008-03-12 17:45:29 +0000519
Devang Pateleaf42ab2008-09-23 23:03:40 +0000520 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000521 if (NewAttrs == OldAttrs)
Chris Lattner58d74912008-03-12 17:45:29 +0000522 return *this;
523
Devang Patel05988662008-09-25 21:00:45 +0000524 SmallVector<AttributeWithIndex, 8> NewAttrList;
525 if (AttrList == 0)
526 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000527 else {
Devang Patel05988662008-09-25 21:00:45 +0000528 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000529 unsigned i = 0, e = OldAttrList.size();
530 // Copy attributes for arguments before this one.
531 for (; i != e && OldAttrList[i].Index < Idx; ++i)
532 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000533
Chris Lattner58d74912008-03-12 17:45:29 +0000534 // If there are attributes already at this index, merge them in.
535 if (i != e && OldAttrList[i].Index == Idx) {
536 Attrs |= OldAttrList[i].Attrs;
537 ++i;
538 }
539
Devang Patel05988662008-09-25 21:00:45 +0000540 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000541
542 // Copy attributes for arguments after this one.
543 NewAttrList.insert(NewAttrList.end(),
544 OldAttrList.begin()+i, OldAttrList.end());
545 }
546
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000547 return get(NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000548}
549
Bill Wendling5886b7b2012-10-14 06:39:53 +0000550AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
551 Attributes Attrs) const {
Dale Johannesen6167c3f2008-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 Wendling67658342012-10-09 07:45:08 +0000555 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
556 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000557#endif
Devang Patel05988662008-09-25 21:00:45 +0000558 if (AttrList == 0) return AttrListPtr();
Chris Lattner58d74912008-03-12 17:45:29 +0000559
Devang Patel05988662008-09-25 21:00:45 +0000560 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling5886b7b2012-10-14 06:39:53 +0000561 Attributes::Builder NewAttrs =
562 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
563 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000564 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000565
Devang Patel05988662008-09-25 21:00:45 +0000566 SmallVector<AttributeWithIndex, 8> NewAttrList;
567 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner58d74912008-03-12 17:45:29 +0000568 unsigned i = 0, e = OldAttrList.size();
569
570 // Copy attributes for arguments before this one.
571 for (; i != e && OldAttrList[i].Index < Idx; ++i)
572 NewAttrList.push_back(OldAttrList[i]);
573
574 // If there are attributes already at this index, merge them in.
575 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling5886b7b2012-10-14 06:39:53 +0000576 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
577 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000578 ++i;
579 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel05988662008-09-25 21:00:45 +0000580 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000581
582 // Copy attributes for arguments after this one.
583 NewAttrList.insert(NewAttrList.end(),
584 OldAttrList.begin()+i, OldAttrList.end());
585
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000586 return get(NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000587}
588
Devang Patel05988662008-09-25 21:00:45 +0000589void AttrListPtr::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000590 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000591 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000592 const AttributeWithIndex &PAWI = getSlot(i);
David Greeneef1894e2010-01-05 01:29:58 +0000593 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner58d74912008-03-12 17:45:29 +0000594 }
595
David Greeneef1894e2010-01-05 01:29:58 +0000596 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000597}