blob: 02c8aaeb530f2ad12d33ed4ced5fc85eb92e40af [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,
Bill Wendlinge94d8432012-12-07 23:16:57 +000011// AttributeListImpl, and AttributeSet 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/ADT/FoldingSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Atomic.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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Type.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;
Nadav Rotemdbf47832012-10-22 17:33:31 +0000130 EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
Bill Wendling50d27842012-10-15 20:35:56 +0000131 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);
Nadav Rotemdbf47832012-10-22 17:33:31 +0000148 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
Bill Wendling50d27842012-10-15 20:35:56 +0000149 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 ";
Quentin Colombet5799e9f2012-10-30 16:32:52 +0000204 if (hasAttribute(Attributes::MinSize))
205 Result += "minsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000206 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000207 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000208 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000209 Result += ") ";
210 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000211 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000212 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000213 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000214 Result += " ";
215 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000216 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000217 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000218 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000219 return Result;
220}
221
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000222//===----------------------------------------------------------------------===//
Bill Wendling50d27842012-10-15 20:35:56 +0000223// AttrBuilder Implementation
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000224//===----------------------------------------------------------------------===//
225
Bill Wendling50d27842012-10-15 20:35:56 +0000226AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
Bill Wendling93f70b72012-10-09 09:11:20 +0000227 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000228 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000229}
230
Bill Wendling50d27842012-10-15 20:35:56 +0000231AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling1fcc8222012-10-14 04:10:01 +0000232 Bits |= Val;
233 return *this;
234}
235
Bill Wendling50d27842012-10-15 20:35:56 +0000236AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000237 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000238 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
239 assert(Align <= 0x40000000 && "Alignment too large.");
240 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000241 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000242}
Bill Wendling50d27842012-10-15 20:35:56 +0000243AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000244 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000245 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000246 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
247 assert(Align <= 0x100 && "Alignment too large.");
248 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000249 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000250}
251
Bill Wendlinga517c302012-10-16 05:22:28 +0000252AttrBuilder &AttrBuilder::removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000253 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000254 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000255}
256
Bill Wendling50d27842012-10-15 20:35:56 +0000257AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
Bill Wendling5c407ed2012-10-14 07:17:34 +0000258 Bits |= A.Raw();
259 return *this;
260}
261
Bill Wendling50d27842012-10-15 20:35:56 +0000262AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000263 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000264 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000265}
266
Bill Wendling50d27842012-10-15 20:35:56 +0000267bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000268 return Bits & AttributesImpl::getAttrMask(A);
269}
270
Bill Wendling50d27842012-10-15 20:35:56 +0000271bool AttrBuilder::hasAttributes() const {
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000272 return Bits != 0;
273}
Bill Wendling50d27842012-10-15 20:35:56 +0000274bool AttrBuilder::hasAttributes(const Attributes &A) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000275 return Bits & A.Raw();
276}
Bill Wendling50d27842012-10-15 20:35:56 +0000277bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000278 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000279}
280
Bill Wendling50d27842012-10-15 20:35:56 +0000281uint64_t AttrBuilder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000282 if (!hasAlignmentAttr())
283 return 0;
NAKAMURA Takumib54ac212012-11-19 10:03:09 +0000284 return 1ULL <<
Bill Wendling4caad412012-10-09 20:28:54 +0000285 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000286}
287
Bill Wendling50d27842012-10-15 20:35:56 +0000288uint64_t AttrBuilder::getStackAlignment() const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000289 if (!hasAlignmentAttr())
290 return 0;
NAKAMURA Takumib54ac212012-11-19 10:03:09 +0000291 return 1ULL <<
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000292 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
293}
294
Chris Lattner8a923e72008-03-12 17:45:29 +0000295//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000296// AttributeImpl Definition
297//===----------------------------------------------------------------------===//
298
Bill Wendling93f70b72012-10-09 09:11:20 +0000299uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000300 switch (Val) {
301 case Attributes::None: return 0;
302 case Attributes::ZExt: return 1 << 0;
303 case Attributes::SExt: return 1 << 1;
304 case Attributes::NoReturn: return 1 << 2;
305 case Attributes::InReg: return 1 << 3;
306 case Attributes::StructRet: return 1 << 4;
307 case Attributes::NoUnwind: return 1 << 5;
308 case Attributes::NoAlias: return 1 << 6;
309 case Attributes::ByVal: return 1 << 7;
310 case Attributes::Nest: return 1 << 8;
311 case Attributes::ReadNone: return 1 << 9;
312 case Attributes::ReadOnly: return 1 << 10;
313 case Attributes::NoInline: return 1 << 11;
314 case Attributes::AlwaysInline: return 1 << 12;
315 case Attributes::OptimizeForSize: return 1 << 13;
316 case Attributes::StackProtect: return 1 << 14;
317 case Attributes::StackProtectReq: return 1 << 15;
318 case Attributes::Alignment: return 31 << 16;
319 case Attributes::NoCapture: return 1 << 21;
320 case Attributes::NoRedZone: return 1 << 22;
321 case Attributes::NoImplicitFloat: return 1 << 23;
322 case Attributes::Naked: return 1 << 24;
323 case Attributes::InlineHint: return 1 << 25;
324 case Attributes::StackAlignment: return 7 << 26;
325 case Attributes::ReturnsTwice: return 1 << 29;
326 case Attributes::UWTable: return 1 << 30;
327 case Attributes::NonLazyBind: return 1U << 31;
328 case Attributes::AddressSafety: return 1ULL << 32;
Quentin Colombet5799e9f2012-10-30 16:32:52 +0000329 case Attributes::MinSize: return 1ULL << 33;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000330 }
331 llvm_unreachable("Unsupported attribute type");
332}
333
Bill Wendling73ea2de2012-10-08 21:47:17 +0000334bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000335 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000336}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000337
Bill Wendling73ea2de2012-10-08 21:47:17 +0000338bool AttributesImpl::hasAttributes() const {
339 return Bits != 0;
340}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000341
Bill Wendling73ea2de2012-10-08 21:47:17 +0000342bool AttributesImpl::hasAttributes(const Attributes &A) const {
343 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
344}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000345
Bill Wendling73ea2de2012-10-08 21:47:17 +0000346uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000347 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000348}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000349
Bill Wendling73ea2de2012-10-08 21:47:17 +0000350uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000351 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000352}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000353
Bill Wendlinge38b8042012-09-26 21:07:29 +0000354//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000355// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000356//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000357
Bill Wendlinge94d8432012-12-07 23:16:57 +0000358AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling623d8ce2012-12-12 19:21:53 +0000359 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000360 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000361 if (Attrs.empty())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000362 return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000363
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000364#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000365 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000366 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000367 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000368 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000369 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000370 }
371#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000372
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000373 // Otherwise, build a key to look up the existing attributes.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000374 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000375 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000376 AttributeListImpl::Profile(ID, Attrs);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000377
Bill Wendlingf86efb92012-11-20 05:09:20 +0000378 void *InsertPoint;
379 AttributeListImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
380 InsertPoint);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000381
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000382 // If we didn't find any existing attributes of the same shape then
383 // create a new one and insert it.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000384 if (!PA) {
385 PA = new AttributeListImpl(Attrs);
386 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000387 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000388
Devang Patel4c758ea2008-09-25 21:00:45 +0000389 // Return the AttributesList that we found or created.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000390 return AttributeSet(PA);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000391}
392
Chris Lattner8a923e72008-03-12 17:45:29 +0000393//===----------------------------------------------------------------------===//
Bill Wendlinge94d8432012-12-07 23:16:57 +0000394// AttributeSet Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000395//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000396
Bill Wendlinge94d8432012-12-07 23:16:57 +0000397const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000398 if (AttrList == RHS.AttrList) return *this;
Chris Lattner8a923e72008-03-12 17:45:29 +0000399
Bill Wendlingf86efb92012-11-20 05:09:20 +0000400 AttrList = RHS.AttrList;
401 return *this;
Chris Lattner8a923e72008-03-12 17:45:29 +0000402}
403
Bill Wendlinga529ade2012-10-16 06:01:44 +0000404/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner8a923e72008-03-12 17:45:29 +0000405/// This is the number of arguments that have an attribute set on them
406/// (including the function itself).
Bill Wendlinge94d8432012-12-07 23:16:57 +0000407unsigned AttributeSet::getNumSlots() const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000408 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000409}
410
Devang Patel4c758ea2008-09-25 21:00:45 +0000411/// getSlot - Return the AttributeWithIndex at the specified slot. This
412/// holds a number plus a set of attributes.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000413const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000414 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
415 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000416}
417
Bill Wendlinga529ade2012-10-16 06:01:44 +0000418/// getAttributes - The attributes for the specified index are returned.
419/// Attributes for the result are denoted with Idx = 0. Function notes are
420/// denoted with idx = ~0.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000421Attributes AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000422 if (AttrList == 0) return Attributes();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000423
Devang Patel4c758ea2008-09-25 21:00:45 +0000424 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000425 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
426 if (Attrs[i].Index == Idx)
427 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000428
429 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000430}
431
432/// hasAttrSomewhere - Return true if the specified attribute is set for at
433/// least one parameter or for the return value.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000434bool AttributeSet::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000435 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000436
Devang Patel4c758ea2008-09-25 21:00:45 +0000437 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000438 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000439 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000440 return true;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000441
Chris Lattner8a923e72008-03-12 17:45:29 +0000442 return false;
443}
444
Bill Wendlinge94d8432012-12-07 23:16:57 +0000445unsigned AttributeSet::getNumAttrs() const {
Bill Wendling70f39172012-10-09 00:01:21 +0000446 return AttrList ? AttrList->Attrs.size() : 0;
447}
448
Bill Wendlinge94d8432012-12-07 23:16:57 +0000449Attributes &AttributeSet::getAttributesAtIndex(unsigned i) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000450 assert(AttrList && "Trying to get an attribute from an empty list!");
451 assert(i < AttrList->Attrs.size() && "Index out of range!");
452 return AttrList->Attrs[i].Attrs;
453}
Chris Lattner8a923e72008-03-12 17:45:29 +0000454
Bill Wendlinge94d8432012-12-07 23:16:57 +0000455AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling722b26c2012-10-14 07:35:59 +0000456 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000457 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000458#ifndef NDEBUG
459 // FIXME it is not obvious how this should work for alignment.
460 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000461 unsigned OldAlign = OldAttrs.getAlignment();
462 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000463 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000464 "Attempt to change alignment!");
465#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000466
Bill Wendling50d27842012-10-15 20:35:56 +0000467 AttrBuilder NewAttrs =
468 AttrBuilder(OldAttrs).addAttributes(Attrs);
469 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000470 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000471
Devang Patel4c758ea2008-09-25 21:00:45 +0000472 SmallVector<AttributeWithIndex, 8> NewAttrList;
473 if (AttrList == 0)
474 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000475 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000476 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000477 unsigned i = 0, e = OldAttrList.size();
478 // Copy attributes for arguments before this one.
479 for (; i != e && OldAttrList[i].Index < Idx; ++i)
480 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000481
Chris Lattner8a923e72008-03-12 17:45:29 +0000482 // If there are attributes already at this index, merge them in.
483 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000484 Attrs =
Bill Wendling50d27842012-10-15 20:35:56 +0000485 Attributes::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000486 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000487 ++i;
488 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000491
Chris Lattner8a923e72008-03-12 17:45:29 +0000492 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000493 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000494 OldAttrList.begin()+i, OldAttrList.end());
495 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000496
Bill Wendlingf86efb92012-11-20 05:09:20 +0000497 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000498}
499
Bill Wendlinge94d8432012-12-07 23:16:57 +0000500AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling85a64c22012-10-14 06:39:53 +0000501 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000502#ifndef NDEBUG
503 // FIXME it is not obvious how this should work for alignment.
504 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000505 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
506 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000507#endif
Bill Wendlinge94d8432012-12-07 23:16:57 +0000508 if (AttrList == 0) return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000509
Devang Patel4c758ea2008-09-25 21:00:45 +0000510 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000511 AttrBuilder NewAttrs =
512 AttrBuilder(OldAttrs).removeAttributes(Attrs);
513 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000514 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000515
Devang Patel4c758ea2008-09-25 21:00:45 +0000516 SmallVector<AttributeWithIndex, 8> NewAttrList;
517 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000518 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000519
Chris Lattner8a923e72008-03-12 17:45:29 +0000520 // Copy attributes for arguments before this one.
521 for (; i != e && OldAttrList[i].Index < Idx; ++i)
522 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000523
Chris Lattner8a923e72008-03-12 17:45:29 +0000524 // If there are attributes already at this index, merge them in.
525 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling50d27842012-10-15 20:35:56 +0000526 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000527 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000528 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000529 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000530 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000531
Chris Lattner8a923e72008-03-12 17:45:29 +0000532 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000533 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000534 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000535
Bill Wendlingf86efb92012-11-20 05:09:20 +0000536 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000537}
538
Bill Wendlinge94d8432012-12-07 23:16:57 +0000539void AttributeSet::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000540 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000541 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000542 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000543 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000544 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000545
David Greenef7014732010-01-05 01:29:58 +0000546 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000547}