blob: cfa276ebd66f85af3106bde313d29764e0764f89 [file] [log] [blame]
Devang Patel4c758ea2008-09-25 21:00:45 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Devang Patel4c758ea2008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Patelba3fa6c2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Bill Wendling8c3e65d2012-10-15 05:40:12 +000015#include "AttributesImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000016#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000017#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000019#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Atomic.h"
21#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025using namespace llvm;
26
Chris Lattner8a923e72008-03-12 17:45:29 +000027//===----------------------------------------------------------------------===//
Bill Wendling73ea2de2012-10-08 21:47:17 +000028// Attributes Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000030
Bill Wendling79d45db2012-10-15 06:53:28 +000031Attributes::Attributes(AttributesImpl *A) : Attrs(A) {}
32
33Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
34
35Attributes Attributes::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
Bill Wendling50d27842012-10-15 20:35:56 +000036 AttrBuilder B;
Bill Wendlingd079a442012-10-15 04:46:55 +000037 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
38 I != E; ++I)
39 B.addAttribute(*I);
Bill Wendling79d45db2012-10-15 06:53:28 +000040 return Attributes::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000041}
Bill Wendling73ea2de2012-10-08 21:47:17 +000042
Bill Wendling50d27842012-10-15 20:35:56 +000043Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000044 // If there are no attributes, return an empty Attributes class.
45 if (B.Bits == 0)
46 return Attributes();
47
48 // Otherwise, build a key to look up the existing attributes.
49 LLVMContextImpl *pImpl = Context.pImpl;
50 FoldingSetNodeID ID;
51 ID.AddInteger(B.Bits);
52
53 void *InsertPoint;
54 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
55
56 if (!PA) {
57 // If we didn't find any existing attributes of the same shape then create a
58 // new one and insert it.
59 PA = new AttributesImpl(B.Bits);
60 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
61 }
62
63 // Return the AttributesList that we found or created.
64 return Attributes(PA);
65}
66
Bill Wendlingc9b22d72012-10-09 07:45:08 +000067bool Attributes::hasAttribute(AttrVal Val) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000068 return Attrs && Attrs->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000069}
70
Bill Wendling8c3e65d2012-10-15 05:40:12 +000071bool Attributes::hasAttributes() const {
72 return Attrs && Attrs->hasAttributes();
73}
74
Bill Wendling93f70b72012-10-09 09:11:20 +000075bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000076 return Attrs && Attrs->hasAttributes(A);
Bill Wendling93f70b72012-10-09 09:11:20 +000077}
78
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079/// This returns the alignment field of an attribute as a byte alignment value.
80unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000081 if (!hasAttribute(Attributes::Alignment))
82 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000083 return 1U << ((Attrs->getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000084}
85
86/// This returns the stack alignment field of an attribute as a byte alignment
87/// value.
88unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000089 if (!hasAttribute(Attributes::StackAlignment))
90 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000091 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000092}
93
Bill Wendling73ea2de2012-10-08 21:47:17 +000094uint64_t Attributes::Raw() const {
Bill Wendlingd079a442012-10-15 04:46:55 +000095 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000096}
97
Bill Wendlingabf3feb2012-10-05 06:44:41 +000098Attributes Attributes::typeIncompatible(Type *Ty) {
Bill Wendling50d27842012-10-15 20:35:56 +000099 AttrBuilder Incompatible;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000100
Bill Wendling7c04e042012-10-09 19:01:18 +0000101 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000102 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 Incompatible.addAttribute(Attributes::SExt)
104 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000105
Bill Wendling7c04e042012-10-09 19:01:18 +0000106 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000107 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000108 Incompatible.addAttribute(Attributes::ByVal)
109 .addAttribute(Attributes::Nest)
110 .addAttribute(Attributes::NoAlias)
111 .addAttribute(Attributes::NoCapture)
112 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000113
Bill Wendlingd079a442012-10-15 04:46:55 +0000114 return Attributes::get(Ty->getContext(), Incompatible);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000115}
116
Bill Wendling50d27842012-10-15 20:35:56 +0000117/// encodeLLVMAttributesForBitcode - This returns an integer containing an
118/// encoding of all the LLVM attributes found in the given attribute bitset.
119/// Any change to this encoding is a breaking change to bitcode compatibility.
120uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
121 // FIXME: It doesn't make sense to store the alignment information as an
122 // expanded out value, we should store it as a log2 value. However, we can't
123 // just change that here without breaking bitcode compatibility. If this ever
124 // becomes a problem in practice, we should introduce new tag numbers in the
125 // bitcode file and have those tags use a more efficiently encoded alignment
126 // field.
127
128 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
129 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
130 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
131 if (Attrs.hasAttribute(Attributes::Alignment))
132 EncodedAttrs |= Attrs.getAlignment() << 16;
133 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
134 return EncodedAttrs;
135}
136
137/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
138/// the LLVM attributes that have been decoded from the given integer. This
139/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
140Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
141 uint64_t EncodedAttrs) {
142 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
143 // the bits above 31 down by 11 bits.
144 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
145 assert((!Alignment || isPowerOf2_32(Alignment)) &&
146 "Alignment must be a power of two.");
147
148 AttrBuilder B(EncodedAttrs & 0xffff);
149 if (Alignment)
150 B.addAlignmentAttr(Alignment);
151 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
152 return Attributes::get(C, B);
153}
154
Bill Wendlingde74cf52012-09-20 14:44:42 +0000155std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000158 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000160 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000162 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000164 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000166 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000168 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000170 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000172 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000174 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000176 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000178 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000180 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000182 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000184 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000186 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000188 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000190 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000191 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000192 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000193 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000194 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000195 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000196 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000197 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000198 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000199 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000200 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000201 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000202 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000203 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000204 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000205 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000206 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000207 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000208 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000209 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000210 Result += ") ";
211 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000212 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000213 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000214 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000215 Result += " ";
216 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000217 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000218 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000219 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000220 return Result;
221}
222
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000223//===----------------------------------------------------------------------===//
Bill Wendling50d27842012-10-15 20:35:56 +0000224// AttrBuilder Implementation
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000225//===----------------------------------------------------------------------===//
226
Bill Wendling50d27842012-10-15 20:35:56 +0000227AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
Bill Wendling93f70b72012-10-09 09:11:20 +0000228 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000229 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000230}
231
Bill Wendling50d27842012-10-15 20:35:56 +0000232AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling1fcc8222012-10-14 04:10:01 +0000233 Bits |= Val;
234 return *this;
235}
236
Bill Wendling50d27842012-10-15 20:35:56 +0000237AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000238 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000239 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
240 assert(Align <= 0x40000000 && "Alignment too large.");
241 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000242 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000243}
Bill Wendling50d27842012-10-15 20:35:56 +0000244AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000245 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000246 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000247 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
248 assert(Align <= 0x100 && "Alignment too large.");
249 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000250 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000251}
252
Bill Wendling50d27842012-10-15 20:35:56 +0000253AttrBuilder &AttrBuilder::
Bill Wendling7c04e042012-10-09 19:01:18 +0000254removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000255 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000256 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000257}
258
Bill Wendling50d27842012-10-15 20:35:56 +0000259AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
Bill Wendling5c407ed2012-10-14 07:17:34 +0000260 Bits |= A.Raw();
261 return *this;
262}
263
Bill Wendling50d27842012-10-15 20:35:56 +0000264AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000265 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000266 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000267}
268
Bill Wendling50d27842012-10-15 20:35:56 +0000269bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000270 return Bits & AttributesImpl::getAttrMask(A);
271}
272
Bill Wendling50d27842012-10-15 20:35:56 +0000273bool AttrBuilder::hasAttributes() const {
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000274 return Bits != 0;
275}
Bill Wendling50d27842012-10-15 20:35:56 +0000276bool AttrBuilder::hasAttributes(const Attributes &A) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000277 return Bits & A.Raw();
278}
Bill Wendling50d27842012-10-15 20:35:56 +0000279bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000280 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000281}
282
Bill Wendling50d27842012-10-15 20:35:56 +0000283uint64_t AttrBuilder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000284 if (!hasAlignmentAttr())
285 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000286 return 1U <<
287 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000288}
289
Bill Wendling50d27842012-10-15 20:35:56 +0000290uint64_t AttrBuilder::getStackAlignment() const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000291 if (!hasAlignmentAttr())
292 return 0;
293 return 1U <<
294 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
295}
296
Chris Lattner8a923e72008-03-12 17:45:29 +0000297//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000298// AttributeImpl Definition
299//===----------------------------------------------------------------------===//
300
Bill Wendling93f70b72012-10-09 09:11:20 +0000301uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000302 switch (Val) {
303 case Attributes::None: return 0;
304 case Attributes::ZExt: return 1 << 0;
305 case Attributes::SExt: return 1 << 1;
306 case Attributes::NoReturn: return 1 << 2;
307 case Attributes::InReg: return 1 << 3;
308 case Attributes::StructRet: return 1 << 4;
309 case Attributes::NoUnwind: return 1 << 5;
310 case Attributes::NoAlias: return 1 << 6;
311 case Attributes::ByVal: return 1 << 7;
312 case Attributes::Nest: return 1 << 8;
313 case Attributes::ReadNone: return 1 << 9;
314 case Attributes::ReadOnly: return 1 << 10;
315 case Attributes::NoInline: return 1 << 11;
316 case Attributes::AlwaysInline: return 1 << 12;
317 case Attributes::OptimizeForSize: return 1 << 13;
318 case Attributes::StackProtect: return 1 << 14;
319 case Attributes::StackProtectReq: return 1 << 15;
320 case Attributes::Alignment: return 31 << 16;
321 case Attributes::NoCapture: return 1 << 21;
322 case Attributes::NoRedZone: return 1 << 22;
323 case Attributes::NoImplicitFloat: return 1 << 23;
324 case Attributes::Naked: return 1 << 24;
325 case Attributes::InlineHint: return 1 << 25;
326 case Attributes::StackAlignment: return 7 << 26;
327 case Attributes::ReturnsTwice: return 1 << 29;
328 case Attributes::UWTable: return 1 << 30;
329 case Attributes::NonLazyBind: return 1U << 31;
330 case Attributes::AddressSafety: return 1ULL << 32;
331 }
332 llvm_unreachable("Unsupported attribute type");
333}
334
Bill Wendling73ea2de2012-10-08 21:47:17 +0000335bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000336 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000337}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000338
Bill Wendling73ea2de2012-10-08 21:47:17 +0000339bool AttributesImpl::hasAttributes() const {
340 return Bits != 0;
341}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000342
Bill Wendling73ea2de2012-10-08 21:47:17 +0000343bool AttributesImpl::hasAttributes(const Attributes &A) const {
344 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
345}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000346
Bill Wendling73ea2de2012-10-08 21:47:17 +0000347uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000348 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000349}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000350
Bill Wendling73ea2de2012-10-08 21:47:17 +0000351uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000352 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000353}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000354
Bill Wendlinge38b8042012-09-26 21:07:29 +0000355//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000356// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000357//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000358
Owen Anderson2e831892010-11-18 18:59:13 +0000359namespace llvm {
360 class AttributeListImpl;
361}
362
363static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000364
Chris Lattner8a923e72008-03-12 17:45:29 +0000365namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000366static ManagedStatic<sys::SmartMutex<true> > ALMutex;
367
Devang Patel00095052008-09-24 00:29:49 +0000368class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000369 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000370
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000372 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
373 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000374 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000375public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000376 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000377
Chris Lattner3cb6f832012-05-28 01:47:44 +0000378 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
379 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000380 RefCount = 0;
381 }
382
Owen Anderson9b14a252010-11-09 00:27:03 +0000383 void AddRef() {
384 sys::SmartScopedLock<true> Lock(*ALMutex);
385 ++RefCount;
386 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000387 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000388 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000389 if (!AttributesLists.isConstructed())
390 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000391 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000392 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000393 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000394 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000395
396 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000397 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000398 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000399 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
400 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
401 ID.AddInteger(Attrs[i].Attrs.Raw());
402 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000403 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000404 }
405};
406}
407
Devang Patel00095052008-09-24 00:29:49 +0000408AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000409 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000411}
412
413
Chris Lattner3cb6f832012-05-28 01:47:44 +0000414AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000415 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000416 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000418
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000419#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000420 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000421 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000422 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000423 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000424 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000425 }
426#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000427
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000428 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000429 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000430 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000431 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000432
433 sys::SmartScopedLock<true> Lock(*ALMutex);
434
Devang Patel00095052008-09-24 00:29:49 +0000435 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000436 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000437
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000438 // If we didn't find any existing attributes of the same shape then
439 // create a new one and insert it.
440 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000441 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000442 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000443 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000444
Devang Patel4c758ea2008-09-25 21:00:45 +0000445 // Return the AttributesList that we found or created.
446 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000447}
448
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000449
Chris Lattner8a923e72008-03-12 17:45:29 +0000450//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000451// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000452//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000453
Devang Patel4c758ea2008-09-25 21:00:45 +0000454AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000455 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000456}
457
Devang Patel4c758ea2008-09-25 21:00:45 +0000458AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
459 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000460}
461
Devang Patel4c758ea2008-09-25 21:00:45 +0000462const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000463 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000464 if (AttrList == RHS.AttrList) return *this;
465 if (AttrList) AttrList->DropRef();
466 AttrList = RHS.AttrList;
467 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000468 return *this;
469}
470
Devang Patel4c758ea2008-09-25 21:00:45 +0000471AttrListPtr::~AttrListPtr() {
472 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000473}
474
475/// getNumSlots - Return the number of slots used in this attribute list.
476/// This is the number of arguments that have an attribute set on them
477/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000478unsigned AttrListPtr::getNumSlots() const {
479 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000480}
481
Devang Patel4c758ea2008-09-25 21:00:45 +0000482/// getSlot - Return the AttributeWithIndex at the specified slot. This
483/// holds a number plus a set of attributes.
484const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
485 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
486 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000487}
488
489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490/// getAttributes - The attributes for the specified index are
491/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000492/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000493Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000494 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000495
Devang Patel4c758ea2008-09-25 21:00:45 +0000496 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000497 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
498 if (Attrs[i].Index == Idx)
499 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000500
501 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000502}
503
504/// hasAttrSomewhere - Return true if the specified attribute is set for at
505/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000506bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000507 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000508
Devang Patel4c758ea2008-09-25 21:00:45 +0000509 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000510 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000511 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000512 return true;
513 return false;
514}
515
Bill Wendling70f39172012-10-09 00:01:21 +0000516unsigned AttrListPtr::getNumAttrs() const {
517 return AttrList ? AttrList->Attrs.size() : 0;
518}
519
520Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
521 assert(AttrList && "Trying to get an attribute from an empty list!");
522 assert(i < AttrList->Attrs.size() && "Index out of range!");
523 return AttrList->Attrs[i].Attrs;
524}
Chris Lattner8a923e72008-03-12 17:45:29 +0000525
Bill Wendling722b26c2012-10-14 07:35:59 +0000526AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
527 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000528 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000529#ifndef NDEBUG
530 // FIXME it is not obvious how this should work for alignment.
531 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000532 unsigned OldAlign = OldAttrs.getAlignment();
533 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000534 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000535 "Attempt to change alignment!");
536#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000537
Bill Wendling50d27842012-10-15 20:35:56 +0000538 AttrBuilder NewAttrs =
539 AttrBuilder(OldAttrs).addAttributes(Attrs);
540 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000541 return *this;
542
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 SmallVector<AttributeWithIndex, 8> NewAttrList;
544 if (AttrList == 0)
545 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000546 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000547 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000548 unsigned i = 0, e = OldAttrList.size();
549 // Copy attributes for arguments before this one.
550 for (; i != e && OldAttrList[i].Index < Idx; ++i)
551 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000552
Chris Lattner8a923e72008-03-12 17:45:29 +0000553 // If there are attributes already at this index, merge them in.
554 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000555 Attrs =
Bill Wendling50d27842012-10-15 20:35:56 +0000556 Attributes::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000557 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000558 ++i;
559 }
560
Devang Patel4c758ea2008-09-25 21:00:45 +0000561 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000562
563 // Copy attributes for arguments after this one.
564 NewAttrList.insert(NewAttrList.end(),
565 OldAttrList.begin()+i, OldAttrList.end());
566 }
567
Chris Lattner3cb6f832012-05-28 01:47:44 +0000568 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000569}
570
Bill Wendling85a64c22012-10-14 06:39:53 +0000571AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
572 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000573#ifndef NDEBUG
574 // FIXME it is not obvious how this should work for alignment.
575 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000576 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
577 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000578#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000579 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000580
Devang Patel4c758ea2008-09-25 21:00:45 +0000581 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000582 AttrBuilder NewAttrs =
583 AttrBuilder(OldAttrs).removeAttributes(Attrs);
584 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000585 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000586
Devang Patel4c758ea2008-09-25 21:00:45 +0000587 SmallVector<AttributeWithIndex, 8> NewAttrList;
588 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000589 unsigned i = 0, e = OldAttrList.size();
590
591 // Copy attributes for arguments before this one.
592 for (; i != e && OldAttrList[i].Index < Idx; ++i)
593 NewAttrList.push_back(OldAttrList[i]);
594
595 // If there are attributes already at this index, merge them in.
596 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling50d27842012-10-15 20:35:56 +0000597 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000598 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000599 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000600 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000601 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000602
603 // Copy attributes for arguments after this one.
604 NewAttrList.insert(NewAttrList.end(),
605 OldAttrList.begin()+i, OldAttrList.end());
606
Chris Lattner3cb6f832012-05-28 01:47:44 +0000607 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000608}
609
Devang Patel4c758ea2008-09-25 21:00:45 +0000610void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000611 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000612 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000613 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000614 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000615 }
616
David Greenef7014732010-01-05 01:29:58 +0000617 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000618}