blob: 4f55fb203bda93a6766047eb96e79d6cde49a68f [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//
Bill Wendling118a78b2012-10-16 06:10:45 +000010// This file implements the Attributes, AttributeImpl, AttrBuilder,
11// AttributeListImpl, and AttrListPtr classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Patelba3fa6c2008-09-23 23:03:40 +000015#include "llvm/Attributes.h"
Bill Wendling8c3e65d2012-10-15 05:40:12 +000016#include "AttributesImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000018#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000019#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000020#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
22#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000023#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026using namespace llvm;
27
Chris Lattner8a923e72008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Bill Wendling73ea2de2012-10-08 21:47:17 +000029// Attributes Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000031
Bill Wendling79d45db2012-10-15 06:53:28 +000032Attributes Attributes::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
Bill Wendling50d27842012-10-15 20:35:56 +000033 AttrBuilder B;
Bill Wendlingd079a442012-10-15 04:46:55 +000034 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
35 I != E; ++I)
36 B.addAttribute(*I);
Bill Wendling79d45db2012-10-15 06:53:28 +000037 return Attributes::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000038}
Bill Wendling73ea2de2012-10-08 21:47:17 +000039
Bill Wendling50d27842012-10-15 20:35:56 +000040Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000041 // If there are no attributes, return an empty Attributes class.
Bill Wendling3ffbac42012-10-16 05:55:09 +000042 if (!B.hasAttributes())
Bill Wendling73ea2de2012-10-08 21:47:17 +000043 return Attributes();
44
45 // Otherwise, build a key to look up the existing attributes.
46 LLVMContextImpl *pImpl = Context.pImpl;
47 FoldingSetNodeID ID;
Bill Wendling3ffbac42012-10-16 05:55:09 +000048 ID.AddInteger(B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000049
50 void *InsertPoint;
51 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
52
53 if (!PA) {
54 // If we didn't find any existing attributes of the same shape then create a
55 // new one and insert it.
Bill Wendling3ffbac42012-10-16 05:55:09 +000056 PA = new AttributesImpl(B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000057 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
58 }
59
60 // Return the AttributesList that we found or created.
61 return Attributes(PA);
62}
63
Bill Wendlingc9b22d72012-10-09 07:45:08 +000064bool Attributes::hasAttribute(AttrVal Val) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000065 return Attrs && Attrs->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000066}
67
Bill Wendling8c3e65d2012-10-15 05:40:12 +000068bool Attributes::hasAttributes() const {
69 return Attrs && Attrs->hasAttributes();
70}
71
Bill Wendling93f70b72012-10-09 09:11:20 +000072bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000073 return Attrs && Attrs->hasAttributes(A);
Bill Wendling93f70b72012-10-09 09:11:20 +000074}
75
Bill Wendlingabf3feb2012-10-05 06:44:41 +000076/// This returns the alignment field of an attribute as a byte alignment value.
77unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000078 if (!hasAttribute(Attributes::Alignment))
79 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000080 return 1U << ((Attrs->getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000081}
82
83/// This returns the stack alignment field of an attribute as a byte alignment
84/// value.
85unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000086 if (!hasAttribute(Attributes::StackAlignment))
87 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000088 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000089}
90
Bill Wendling73ea2de2012-10-08 21:47:17 +000091uint64_t Attributes::Raw() const {
Bill Wendling147ee8e2012-10-16 05:57:28 +000092 return Attrs ? Attrs->Raw() : 0;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000093}
94
Bill Wendlingabf3feb2012-10-05 06:44:41 +000095Attributes Attributes::typeIncompatible(Type *Ty) {
Bill Wendling50d27842012-10-15 20:35:56 +000096 AttrBuilder Incompatible;
Bill Wendlinga529ade2012-10-16 06:01:44 +000097
Bill Wendling7c04e042012-10-09 19:01:18 +000098 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +000099 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000100 Incompatible.addAttribute(Attributes::SExt)
101 .addAttribute(Attributes::ZExt);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000102
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000104 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000105 Incompatible.addAttribute(Attributes::ByVal)
106 .addAttribute(Attributes::Nest)
107 .addAttribute(Attributes::NoAlias)
108 .addAttribute(Attributes::NoCapture)
109 .addAttribute(Attributes::StructRet);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000110
Bill Wendlingd079a442012-10-15 04:46:55 +0000111 return Attributes::get(Ty->getContext(), Incompatible);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000112}
113
Bill Wendling50d27842012-10-15 20:35:56 +0000114/// encodeLLVMAttributesForBitcode - This returns an integer containing an
115/// encoding of all the LLVM attributes found in the given attribute bitset.
116/// Any change to this encoding is a breaking change to bitcode compatibility.
117uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
118 // FIXME: It doesn't make sense to store the alignment information as an
119 // expanded out value, we should store it as a log2 value. However, we can't
120 // just change that here without breaking bitcode compatibility. If this ever
121 // becomes a problem in practice, we should introduce new tag numbers in the
122 // bitcode file and have those tags use a more efficiently encoded alignment
123 // field.
124
125 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
126 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
127 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
128 if (Attrs.hasAttribute(Attributes::Alignment))
129 EncodedAttrs |= Attrs.getAlignment() << 16;
130 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
131 return EncodedAttrs;
132}
133
134/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
135/// the LLVM attributes that have been decoded from the given integer. This
136/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
137Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
138 uint64_t EncodedAttrs) {
139 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
140 // the bits above 31 down by 11 bits.
141 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
142 assert((!Alignment || isPowerOf2_32(Alignment)) &&
143 "Alignment must be a power of two.");
144
145 AttrBuilder B(EncodedAttrs & 0xffff);
146 if (Alignment)
147 B.addAlignmentAttr(Alignment);
148 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
149 return Attributes::get(C, B);
150}
151
Bill Wendlingde74cf52012-09-20 14:44:42 +0000152std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000153 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000154 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000155 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000156 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000157 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000158 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000159 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000160 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000161 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000162 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000163 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000164 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000165 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000166 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000167 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000168 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000169 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000170 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000171 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000172 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000173 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000174 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000175 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000176 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000177 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000178 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000179 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000180 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000181 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000182 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000183 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000184 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000185 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000186 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000187 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000188 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000189 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000190 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000191 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000192 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000193 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000194 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000195 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000196 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000197 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000198 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000199 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000200 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000201 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000202 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000203 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000204 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000205 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000206 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000207 Result += ") ";
208 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000209 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000210 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000211 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000212 Result += " ";
213 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000214 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000215 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000216 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000217 return Result;
218}
219
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000220//===----------------------------------------------------------------------===//
Bill Wendling50d27842012-10-15 20:35:56 +0000221// AttrBuilder Implementation
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000222//===----------------------------------------------------------------------===//
223
Bill Wendling50d27842012-10-15 20:35:56 +0000224AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
Bill Wendling93f70b72012-10-09 09:11:20 +0000225 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000226 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000227}
228
Bill Wendling50d27842012-10-15 20:35:56 +0000229AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling1fcc8222012-10-14 04:10:01 +0000230 Bits |= Val;
231 return *this;
232}
233
Bill Wendling50d27842012-10-15 20:35:56 +0000234AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000235 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000236 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
237 assert(Align <= 0x40000000 && "Alignment too large.");
238 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000239 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000240}
Bill Wendling50d27842012-10-15 20:35:56 +0000241AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000242 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000243 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000244 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
245 assert(Align <= 0x100 && "Alignment too large.");
246 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000247 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000248}
249
Bill Wendlinga517c302012-10-16 05:22:28 +0000250AttrBuilder &AttrBuilder::removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000251 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000252 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000253}
254
Bill Wendling50d27842012-10-15 20:35:56 +0000255AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
Bill Wendling5c407ed2012-10-14 07:17:34 +0000256 Bits |= A.Raw();
257 return *this;
258}
259
Bill Wendling50d27842012-10-15 20:35:56 +0000260AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000261 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000262 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000263}
264
Bill Wendling50d27842012-10-15 20:35:56 +0000265bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000266 return Bits & AttributesImpl::getAttrMask(A);
267}
268
Bill Wendling50d27842012-10-15 20:35:56 +0000269bool AttrBuilder::hasAttributes() const {
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000270 return Bits != 0;
271}
Bill Wendling50d27842012-10-15 20:35:56 +0000272bool AttrBuilder::hasAttributes(const Attributes &A) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000273 return Bits & A.Raw();
274}
Bill Wendling50d27842012-10-15 20:35:56 +0000275bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000276 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000277}
278
Bill Wendling50d27842012-10-15 20:35:56 +0000279uint64_t AttrBuilder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000280 if (!hasAlignmentAttr())
281 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000282 return 1U <<
283 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000284}
285
Bill Wendling50d27842012-10-15 20:35:56 +0000286uint64_t AttrBuilder::getStackAlignment() const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000287 if (!hasAlignmentAttr())
288 return 0;
289 return 1U <<
290 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
291}
292
Chris Lattner8a923e72008-03-12 17:45:29 +0000293//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000294// AttributeImpl Definition
295//===----------------------------------------------------------------------===//
296
Bill Wendling93f70b72012-10-09 09:11:20 +0000297uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000298 switch (Val) {
299 case Attributes::None: return 0;
300 case Attributes::ZExt: return 1 << 0;
301 case Attributes::SExt: return 1 << 1;
302 case Attributes::NoReturn: return 1 << 2;
303 case Attributes::InReg: return 1 << 3;
304 case Attributes::StructRet: return 1 << 4;
305 case Attributes::NoUnwind: return 1 << 5;
306 case Attributes::NoAlias: return 1 << 6;
307 case Attributes::ByVal: return 1 << 7;
308 case Attributes::Nest: return 1 << 8;
309 case Attributes::ReadNone: return 1 << 9;
310 case Attributes::ReadOnly: return 1 << 10;
311 case Attributes::NoInline: return 1 << 11;
312 case Attributes::AlwaysInline: return 1 << 12;
313 case Attributes::OptimizeForSize: return 1 << 13;
314 case Attributes::StackProtect: return 1 << 14;
315 case Attributes::StackProtectReq: return 1 << 15;
316 case Attributes::Alignment: return 31 << 16;
317 case Attributes::NoCapture: return 1 << 21;
318 case Attributes::NoRedZone: return 1 << 22;
319 case Attributes::NoImplicitFloat: return 1 << 23;
320 case Attributes::Naked: return 1 << 24;
321 case Attributes::InlineHint: return 1 << 25;
322 case Attributes::StackAlignment: return 7 << 26;
323 case Attributes::ReturnsTwice: return 1 << 29;
324 case Attributes::UWTable: return 1 << 30;
325 case Attributes::NonLazyBind: return 1U << 31;
326 case Attributes::AddressSafety: return 1ULL << 32;
327 }
328 llvm_unreachable("Unsupported attribute type");
329}
330
Bill Wendling73ea2de2012-10-08 21:47:17 +0000331bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000332 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000333}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000334
Bill Wendling73ea2de2012-10-08 21:47:17 +0000335bool AttributesImpl::hasAttributes() const {
336 return Bits != 0;
337}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000338
Bill Wendling73ea2de2012-10-08 21:47:17 +0000339bool AttributesImpl::hasAttributes(const Attributes &A) const {
340 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
341}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000342
Bill Wendling73ea2de2012-10-08 21:47:17 +0000343uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000344 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000345}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000346
Bill Wendling73ea2de2012-10-08 21:47:17 +0000347uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000348 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000349}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000350
Bill Wendlinge38b8042012-09-26 21:07:29 +0000351//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000352// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000353//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000354
Owen Anderson2e831892010-11-18 18:59:13 +0000355namespace llvm {
356 class AttributeListImpl;
357}
358
359static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000360
Chris Lattner8a923e72008-03-12 17:45:29 +0000361namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000362static ManagedStatic<sys::SmartMutex<true> > ALMutex;
363
Devang Patel00095052008-09-24 00:29:49 +0000364class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000365 sys::cas_flag RefCount;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000366
Devang Patel4c758ea2008-09-25 21:00:45 +0000367 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000368 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
369 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000370 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000371public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000372 SmallVector<AttributeWithIndex, 4> Attrs;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000373
Chris Lattner3cb6f832012-05-28 01:47:44 +0000374 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
375 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000376 RefCount = 0;
377 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000378
Owen Anderson9b14a252010-11-09 00:27:03 +0000379 void AddRef() {
380 sys::SmartScopedLock<true> Lock(*ALMutex);
381 ++RefCount;
382 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000383 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000384 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000385 if (!AttributesLists.isConstructed())
386 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000387 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000388 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000389 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000390 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000391
Chris Lattner8a923e72008-03-12 17:45:29 +0000392 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000393 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000394 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000395 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
396 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
397 ID.AddInteger(Attrs[i].Attrs.Raw());
398 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000399 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 }
401};
Bill Wendlinga529ade2012-10-16 06:01:44 +0000402
403} // end llvm namespace
Chris Lattner8a923e72008-03-12 17:45:29 +0000404
Devang Patel00095052008-09-24 00:29:49 +0000405AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000406 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000407 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000408}
409
Chris Lattner3cb6f832012-05-28 01:47:44 +0000410AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000412 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000413 return AttrListPtr();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000414
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000415#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000416 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000417 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000418 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000419 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000420 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000421 }
422#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000423
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000424 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000425 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000426 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000427 void *InsertPos;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000428
Owen Andersond91e6b02009-08-17 17:10:58 +0000429 sys::SmartScopedLock<true> Lock(*ALMutex);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000430
Devang Patel00095052008-09-24 00:29:49 +0000431 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000432 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000433
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000434 // If we didn't find any existing attributes of the same shape then
435 // create a new one and insert it.
436 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000437 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000439 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000440
Devang Patel4c758ea2008-09-25 21:00:45 +0000441 // Return the AttributesList that we found or created.
442 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000443}
444
Chris Lattner8a923e72008-03-12 17:45:29 +0000445//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000446// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000447//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000448
Devang Patel4c758ea2008-09-25 21:00:45 +0000449AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000450 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000451}
452
Devang Patel4c758ea2008-09-25 21:00:45 +0000453AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000454 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000455}
456
Devang Patel4c758ea2008-09-25 21:00:45 +0000457const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000458 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000459 if (AttrList == RHS.AttrList) return *this;
460 if (AttrList) AttrList->DropRef();
461 AttrList = RHS.AttrList;
462 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000463 return *this;
464}
465
Devang Patel4c758ea2008-09-25 21:00:45 +0000466AttrListPtr::~AttrListPtr() {
467 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000468}
469
Bill Wendlinga529ade2012-10-16 06:01:44 +0000470/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner8a923e72008-03-12 17:45:29 +0000471/// This is the number of arguments that have an attribute set on them
472/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000473unsigned AttrListPtr::getNumSlots() const {
474 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000475}
476
Devang Patel4c758ea2008-09-25 21:00:45 +0000477/// getSlot - Return the AttributeWithIndex at the specified slot. This
478/// holds a number plus a set of attributes.
479const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
480 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
481 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000482}
483
Bill Wendlinga529ade2012-10-16 06:01:44 +0000484/// getAttributes - The attributes for the specified index are returned.
485/// Attributes for the result are denoted with Idx = 0. Function notes are
486/// denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000487Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000488 if (AttrList == 0) return Attributes();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000491 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
492 if (Attrs[i].Index == Idx)
493 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000494
495 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000496}
497
498/// hasAttrSomewhere - Return true if the specified attribute is set for at
499/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000500bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000501 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000502
Devang Patel4c758ea2008-09-25 21:00:45 +0000503 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000504 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000505 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000506 return true;
507 return false;
508}
509
Bill Wendling70f39172012-10-09 00:01:21 +0000510unsigned AttrListPtr::getNumAttrs() const {
511 return AttrList ? AttrList->Attrs.size() : 0;
512}
513
514Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
515 assert(AttrList && "Trying to get an attribute from an empty list!");
516 assert(i < AttrList->Attrs.size() && "Index out of range!");
517 return AttrList->Attrs[i].Attrs;
518}
Chris Lattner8a923e72008-03-12 17:45:29 +0000519
Bill Wendling722b26c2012-10-14 07:35:59 +0000520AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
521 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000522 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000523#ifndef NDEBUG
524 // FIXME it is not obvious how this should work for alignment.
525 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000526 unsigned OldAlign = OldAttrs.getAlignment();
527 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000528 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000529 "Attempt to change alignment!");
530#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000531
Bill Wendling50d27842012-10-15 20:35:56 +0000532 AttrBuilder NewAttrs =
533 AttrBuilder(OldAttrs).addAttributes(Attrs);
534 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000535 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000536
Devang Patel4c758ea2008-09-25 21:00:45 +0000537 SmallVector<AttributeWithIndex, 8> NewAttrList;
538 if (AttrList == 0)
539 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000540 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000541 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000542 unsigned i = 0, e = OldAttrList.size();
543 // Copy attributes for arguments before this one.
544 for (; i != e && OldAttrList[i].Index < Idx; ++i)
545 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000546
Chris Lattner8a923e72008-03-12 17:45:29 +0000547 // If there are attributes already at this index, merge them in.
548 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000549 Attrs =
Bill Wendling50d27842012-10-15 20:35:56 +0000550 Attributes::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000551 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000552 ++i;
553 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000554
Devang Patel4c758ea2008-09-25 21:00:45 +0000555 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000556
Chris Lattner8a923e72008-03-12 17:45:29 +0000557 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000558 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000559 OldAttrList.begin()+i, OldAttrList.end());
560 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000561
Chris Lattner3cb6f832012-05-28 01:47:44 +0000562 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000563}
564
Bill Wendling85a64c22012-10-14 06:39:53 +0000565AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
566 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000567#ifndef NDEBUG
568 // FIXME it is not obvious how this should work for alignment.
569 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000570 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
571 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000572#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000573 if (AttrList == 0) return AttrListPtr();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000574
Devang Patel4c758ea2008-09-25 21:00:45 +0000575 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000576 AttrBuilder NewAttrs =
577 AttrBuilder(OldAttrs).removeAttributes(Attrs);
578 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000579 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000580
Devang Patel4c758ea2008-09-25 21:00:45 +0000581 SmallVector<AttributeWithIndex, 8> NewAttrList;
582 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000583 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000584
Chris Lattner8a923e72008-03-12 17:45:29 +0000585 // Copy attributes for arguments before this one.
586 for (; i != e && OldAttrList[i].Index < Idx; ++i)
587 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000588
Chris Lattner8a923e72008-03-12 17:45:29 +0000589 // If there are attributes already at this index, merge them in.
590 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling50d27842012-10-15 20:35:56 +0000591 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000592 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000593 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000594 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000595 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000596
Chris Lattner8a923e72008-03-12 17:45:29 +0000597 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000598 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000599 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000600
Chris Lattner3cb6f832012-05-28 01:47:44 +0000601 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000602}
603
Devang Patel4c758ea2008-09-25 21:00:45 +0000604void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000605 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000606 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000607 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000608 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000609 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000610
David Greenef7014732010-01-05 01:29:58 +0000611 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000612}