blob: c79d0006857a506858c0e4fd4c8788312dd035e6 [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::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
Bill Wendling50d27842012-10-15 20:35:56 +000032 AttrBuilder B;
Bill Wendlingd079a442012-10-15 04:46:55 +000033 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
34 I != E; ++I)
35 B.addAttribute(*I);
Bill Wendling79d45db2012-10-15 06:53:28 +000036 return Attributes::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000037}
Bill Wendling73ea2de2012-10-08 21:47:17 +000038
Bill Wendling50d27842012-10-15 20:35:56 +000039Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
Bill Wendling73ea2de2012-10-08 21:47:17 +000040 // If there are no attributes, return an empty Attributes class.
Bill Wendling3ffbac42012-10-16 05:55:09 +000041 if (!B.hasAttributes())
Bill Wendling73ea2de2012-10-08 21:47:17 +000042 return Attributes();
43
44 // Otherwise, build a key to look up the existing attributes.
45 LLVMContextImpl *pImpl = Context.pImpl;
46 FoldingSetNodeID ID;
Bill Wendling3ffbac42012-10-16 05:55:09 +000047 ID.AddInteger(B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000048
49 void *InsertPoint;
50 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
51
52 if (!PA) {
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
Bill Wendling3ffbac42012-10-16 05:55:09 +000055 PA = new AttributesImpl(B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000056 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
57 }
58
59 // Return the AttributesList that we found or created.
60 return Attributes(PA);
61}
62
Bill Wendlingc9b22d72012-10-09 07:45:08 +000063bool Attributes::hasAttribute(AttrVal Val) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000064 return Attrs && Attrs->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000065}
66
Bill Wendling8c3e65d2012-10-15 05:40:12 +000067bool Attributes::hasAttributes() const {
68 return Attrs && Attrs->hasAttributes();
69}
70
Bill Wendling93f70b72012-10-09 09:11:20 +000071bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000072 return Attrs && Attrs->hasAttributes(A);
Bill Wendling93f70b72012-10-09 09:11:20 +000073}
74
Bill Wendlingabf3feb2012-10-05 06:44:41 +000075/// This returns the alignment field of an attribute as a byte alignment value.
76unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000077 if (!hasAttribute(Attributes::Alignment))
78 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000079 return 1U << ((Attrs->getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000080}
81
82/// This returns the stack alignment field of an attribute as a byte alignment
83/// value.
84unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000085 if (!hasAttribute(Attributes::StackAlignment))
86 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000087 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000088}
89
Bill Wendling73ea2de2012-10-08 21:47:17 +000090uint64_t Attributes::Raw() const {
Bill Wendling147ee8e2012-10-16 05:57:28 +000091 return Attrs ? Attrs->Raw() : 0;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000092}
93
Bill Wendlingabf3feb2012-10-05 06:44:41 +000094Attributes Attributes::typeIncompatible(Type *Ty) {
Bill Wendling50d27842012-10-15 20:35:56 +000095 AttrBuilder Incompatible;
Bill Wendlinga529ade2012-10-16 06:01:44 +000096
Bill Wendling7c04e042012-10-09 19:01:18 +000097 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +000098 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +000099 Incompatible.addAttribute(Attributes::SExt)
100 .addAttribute(Attributes::ZExt);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000101
Bill Wendling7c04e042012-10-09 19:01:18 +0000102 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000103 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000104 Incompatible.addAttribute(Attributes::ByVal)
105 .addAttribute(Attributes::Nest)
106 .addAttribute(Attributes::NoAlias)
107 .addAttribute(Attributes::NoCapture)
108 .addAttribute(Attributes::StructRet);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000109
Bill Wendlingd079a442012-10-15 04:46:55 +0000110 return Attributes::get(Ty->getContext(), Incompatible);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000111}
112
Bill Wendling50d27842012-10-15 20:35:56 +0000113/// encodeLLVMAttributesForBitcode - This returns an integer containing an
114/// encoding of all the LLVM attributes found in the given attribute bitset.
115/// Any change to this encoding is a breaking change to bitcode compatibility.
116uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
117 // FIXME: It doesn't make sense to store the alignment information as an
118 // expanded out value, we should store it as a log2 value. However, we can't
119 // just change that here without breaking bitcode compatibility. If this ever
120 // becomes a problem in practice, we should introduce new tag numbers in the
121 // bitcode file and have those tags use a more efficiently encoded alignment
122 // field.
123
124 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
125 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
126 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
127 if (Attrs.hasAttribute(Attributes::Alignment))
128 EncodedAttrs |= Attrs.getAlignment() << 16;
129 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
130 return EncodedAttrs;
131}
132
133/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
134/// the LLVM attributes that have been decoded from the given integer. This
135/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
136Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
137 uint64_t EncodedAttrs) {
138 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
139 // the bits above 31 down by 11 bits.
140 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
141 assert((!Alignment || isPowerOf2_32(Alignment)) &&
142 "Alignment must be a power of two.");
143
144 AttrBuilder B(EncodedAttrs & 0xffff);
145 if (Alignment)
146 B.addAlignmentAttr(Alignment);
147 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
148 return Attributes::get(C, B);
149}
150
Bill Wendlingde74cf52012-09-20 14:44:42 +0000151std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000152 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000154 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000158 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000160 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000162 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000164 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000166 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000168 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000170 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000172 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000174 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000176 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000178 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000180 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000182 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000184 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000186 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000188 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000190 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000191 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000192 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000193 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000194 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000195 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000196 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000197 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000198 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000199 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000200 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000201 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000202 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000203 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000204 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000205 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000206 Result += ") ";
207 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000208 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000209 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000210 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000211 Result += " ";
212 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000213 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000214 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000215 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000216 return Result;
217}
218
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000219//===----------------------------------------------------------------------===//
Bill Wendling50d27842012-10-15 20:35:56 +0000220// AttrBuilder Implementation
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000221//===----------------------------------------------------------------------===//
222
Bill Wendling50d27842012-10-15 20:35:56 +0000223AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
Bill Wendling93f70b72012-10-09 09:11:20 +0000224 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000225 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000226}
227
Bill Wendling50d27842012-10-15 20:35:56 +0000228AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling1fcc8222012-10-14 04:10:01 +0000229 Bits |= Val;
230 return *this;
231}
232
Bill Wendling50d27842012-10-15 20:35:56 +0000233AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000234 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000235 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
236 assert(Align <= 0x40000000 && "Alignment too large.");
237 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000238 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000239}
Bill Wendling50d27842012-10-15 20:35:56 +0000240AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000241 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000242 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000243 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
244 assert(Align <= 0x100 && "Alignment too large.");
245 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000246 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000247}
248
Bill Wendlinga517c302012-10-16 05:22:28 +0000249AttrBuilder &AttrBuilder::removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000250 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000251 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000252}
253
Bill Wendling50d27842012-10-15 20:35:56 +0000254AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
Bill Wendling5c407ed2012-10-14 07:17:34 +0000255 Bits |= A.Raw();
256 return *this;
257}
258
Bill Wendling50d27842012-10-15 20:35:56 +0000259AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000260 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000261 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000262}
263
Bill Wendling50d27842012-10-15 20:35:56 +0000264bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000265 return Bits & AttributesImpl::getAttrMask(A);
266}
267
Bill Wendling50d27842012-10-15 20:35:56 +0000268bool AttrBuilder::hasAttributes() const {
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000269 return Bits != 0;
270}
Bill Wendling50d27842012-10-15 20:35:56 +0000271bool AttrBuilder::hasAttributes(const Attributes &A) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000272 return Bits & A.Raw();
273}
Bill Wendling50d27842012-10-15 20:35:56 +0000274bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000275 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000276}
277
Bill Wendling50d27842012-10-15 20:35:56 +0000278uint64_t AttrBuilder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000279 if (!hasAlignmentAttr())
280 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000281 return 1U <<
282 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000283}
284
Bill Wendling50d27842012-10-15 20:35:56 +0000285uint64_t AttrBuilder::getStackAlignment() const {
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000286 if (!hasAlignmentAttr())
287 return 0;
288 return 1U <<
289 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
290}
291
Chris Lattner8a923e72008-03-12 17:45:29 +0000292//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000293// AttributeImpl Definition
294//===----------------------------------------------------------------------===//
295
Bill Wendling93f70b72012-10-09 09:11:20 +0000296uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000297 switch (Val) {
298 case Attributes::None: return 0;
299 case Attributes::ZExt: return 1 << 0;
300 case Attributes::SExt: return 1 << 1;
301 case Attributes::NoReturn: return 1 << 2;
302 case Attributes::InReg: return 1 << 3;
303 case Attributes::StructRet: return 1 << 4;
304 case Attributes::NoUnwind: return 1 << 5;
305 case Attributes::NoAlias: return 1 << 6;
306 case Attributes::ByVal: return 1 << 7;
307 case Attributes::Nest: return 1 << 8;
308 case Attributes::ReadNone: return 1 << 9;
309 case Attributes::ReadOnly: return 1 << 10;
310 case Attributes::NoInline: return 1 << 11;
311 case Attributes::AlwaysInline: return 1 << 12;
312 case Attributes::OptimizeForSize: return 1 << 13;
313 case Attributes::StackProtect: return 1 << 14;
314 case Attributes::StackProtectReq: return 1 << 15;
315 case Attributes::Alignment: return 31 << 16;
316 case Attributes::NoCapture: return 1 << 21;
317 case Attributes::NoRedZone: return 1 << 22;
318 case Attributes::NoImplicitFloat: return 1 << 23;
319 case Attributes::Naked: return 1 << 24;
320 case Attributes::InlineHint: return 1 << 25;
321 case Attributes::StackAlignment: return 7 << 26;
322 case Attributes::ReturnsTwice: return 1 << 29;
323 case Attributes::UWTable: return 1 << 30;
324 case Attributes::NonLazyBind: return 1U << 31;
325 case Attributes::AddressSafety: return 1ULL << 32;
326 }
327 llvm_unreachable("Unsupported attribute type");
328}
329
Bill Wendling73ea2de2012-10-08 21:47:17 +0000330bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000331 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000332}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000333
Bill Wendling73ea2de2012-10-08 21:47:17 +0000334bool AttributesImpl::hasAttributes() const {
335 return Bits != 0;
336}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000337
Bill Wendling73ea2de2012-10-08 21:47:17 +0000338bool AttributesImpl::hasAttributes(const Attributes &A) const {
339 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
340}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000341
Bill Wendling73ea2de2012-10-08 21:47:17 +0000342uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000343 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000344}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000345
Bill Wendling73ea2de2012-10-08 21:47:17 +0000346uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000347 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000348}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000349
Bill Wendlinge38b8042012-09-26 21:07:29 +0000350//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000351// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000352//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000353
Owen Anderson2e831892010-11-18 18:59:13 +0000354namespace llvm {
355 class AttributeListImpl;
356}
357
358static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000359
Chris Lattner8a923e72008-03-12 17:45:29 +0000360namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000361static ManagedStatic<sys::SmartMutex<true> > ALMutex;
362
Devang Patel00095052008-09-24 00:29:49 +0000363class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000364 sys::cas_flag RefCount;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000365
Devang Patel4c758ea2008-09-25 21:00:45 +0000366 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000367 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
368 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000369 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000370public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 SmallVector<AttributeWithIndex, 4> Attrs;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000372
Chris Lattner3cb6f832012-05-28 01:47:44 +0000373 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
374 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000375 RefCount = 0;
376 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000377
Owen Anderson9b14a252010-11-09 00:27:03 +0000378 void AddRef() {
379 sys::SmartScopedLock<true> Lock(*ALMutex);
380 ++RefCount;
381 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000382 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000383 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000384 if (!AttributesLists.isConstructed())
385 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000386 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000387 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000388 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000389 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000390
Chris Lattner8a923e72008-03-12 17:45:29 +0000391 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000392 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000393 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000394 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
395 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
396 ID.AddInteger(Attrs[i].Attrs.Raw());
397 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000398 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000399 }
400};
Bill Wendlinga529ade2012-10-16 06:01:44 +0000401
402} // end llvm namespace
Chris Lattner8a923e72008-03-12 17:45:29 +0000403
Devang Patel00095052008-09-24 00:29:49 +0000404AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000405 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000406 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000407}
408
Chris Lattner3cb6f832012-05-28 01:47:44 +0000409AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000411 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000412 return AttrListPtr();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000413
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000414#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000415 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000416 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000418 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000420 }
421#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000422
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000423 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000424 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000425 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000426 void *InsertPos;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000427
Owen Andersond91e6b02009-08-17 17:10:58 +0000428 sys::SmartScopedLock<true> Lock(*ALMutex);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000429
Devang Patel00095052008-09-24 00:29:49 +0000430 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000431 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000432
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000433 // If we didn't find any existing attributes of the same shape then
434 // create a new one and insert it.
435 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000436 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000437 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000438 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000439
Devang Patel4c758ea2008-09-25 21:00:45 +0000440 // Return the AttributesList that we found or created.
441 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000442}
443
Chris Lattner8a923e72008-03-12 17:45:29 +0000444//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000445// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000446//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000447
Devang Patel4c758ea2008-09-25 21:00:45 +0000448AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000449 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000450}
451
Devang Patel4c758ea2008-09-25 21:00:45 +0000452AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000453 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000454}
455
Devang Patel4c758ea2008-09-25 21:00:45 +0000456const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000457 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000458 if (AttrList == RHS.AttrList) return *this;
459 if (AttrList) AttrList->DropRef();
460 AttrList = RHS.AttrList;
461 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000462 return *this;
463}
464
Devang Patel4c758ea2008-09-25 21:00:45 +0000465AttrListPtr::~AttrListPtr() {
466 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000467}
468
Bill Wendlinga529ade2012-10-16 06:01:44 +0000469/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner8a923e72008-03-12 17:45:29 +0000470/// This is the number of arguments that have an attribute set on them
471/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000472unsigned AttrListPtr::getNumSlots() const {
473 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000474}
475
Devang Patel4c758ea2008-09-25 21:00:45 +0000476/// getSlot - Return the AttributeWithIndex at the specified slot. This
477/// holds a number plus a set of attributes.
478const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
479 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
480 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000481}
482
Bill Wendlinga529ade2012-10-16 06:01:44 +0000483/// getAttributes - The attributes for the specified index are returned.
484/// Attributes for the result are denoted with Idx = 0. Function notes are
485/// denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000486Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000487 if (AttrList == 0) return Attributes();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000488
Devang Patel4c758ea2008-09-25 21:00:45 +0000489 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000490 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
491 if (Attrs[i].Index == Idx)
492 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000493
494 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000495}
496
497/// hasAttrSomewhere - Return true if the specified attribute is set for at
498/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000499bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000500 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000501
Devang Patel4c758ea2008-09-25 21:00:45 +0000502 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000503 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000504 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000505 return true;
506 return false;
507}
508
Bill Wendling70f39172012-10-09 00:01:21 +0000509unsigned AttrListPtr::getNumAttrs() const {
510 return AttrList ? AttrList->Attrs.size() : 0;
511}
512
513Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
514 assert(AttrList && "Trying to get an attribute from an empty list!");
515 assert(i < AttrList->Attrs.size() && "Index out of range!");
516 return AttrList->Attrs[i].Attrs;
517}
Chris Lattner8a923e72008-03-12 17:45:29 +0000518
Bill Wendling722b26c2012-10-14 07:35:59 +0000519AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
520 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000521 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000522#ifndef NDEBUG
523 // FIXME it is not obvious how this should work for alignment.
524 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000525 unsigned OldAlign = OldAttrs.getAlignment();
526 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000527 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000528 "Attempt to change alignment!");
529#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000530
Bill Wendling50d27842012-10-15 20:35:56 +0000531 AttrBuilder NewAttrs =
532 AttrBuilder(OldAttrs).addAttributes(Attrs);
533 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000534 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000535
Devang Patel4c758ea2008-09-25 21:00:45 +0000536 SmallVector<AttributeWithIndex, 8> NewAttrList;
537 if (AttrList == 0)
538 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000539 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000540 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000541 unsigned i = 0, e = OldAttrList.size();
542 // Copy attributes for arguments before this one.
543 for (; i != e && OldAttrList[i].Index < Idx; ++i)
544 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000545
Chris Lattner8a923e72008-03-12 17:45:29 +0000546 // If there are attributes already at this index, merge them in.
547 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000548 Attrs =
Bill Wendling50d27842012-10-15 20:35:56 +0000549 Attributes::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000550 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000551 ++i;
552 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000553
Devang Patel4c758ea2008-09-25 21:00:45 +0000554 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000555
Chris Lattner8a923e72008-03-12 17:45:29 +0000556 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000557 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000558 OldAttrList.begin()+i, OldAttrList.end());
559 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000560
Chris Lattner3cb6f832012-05-28 01:47:44 +0000561 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000562}
563
Bill Wendling85a64c22012-10-14 06:39:53 +0000564AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
565 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000566#ifndef NDEBUG
567 // FIXME it is not obvious how this should work for alignment.
568 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000569 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
570 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000571#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000572 if (AttrList == 0) return AttrListPtr();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000573
Devang Patel4c758ea2008-09-25 21:00:45 +0000574 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000575 AttrBuilder NewAttrs =
576 AttrBuilder(OldAttrs).removeAttributes(Attrs);
577 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000578 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000579
Devang Patel4c758ea2008-09-25 21:00:45 +0000580 SmallVector<AttributeWithIndex, 8> NewAttrList;
581 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000582 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000583
Chris Lattner8a923e72008-03-12 17:45:29 +0000584 // Copy attributes for arguments before this one.
585 for (; i != e && OldAttrList[i].Index < Idx; ++i)
586 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000587
Chris Lattner8a923e72008-03-12 17:45:29 +0000588 // If there are attributes already at this index, merge them in.
589 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling50d27842012-10-15 20:35:56 +0000590 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000591 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000592 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000593 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000594 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000595
Chris Lattner8a923e72008-03-12 17:45:29 +0000596 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000597 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000598 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000599
Chris Lattner3cb6f832012-05-28 01:47:44 +0000600 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000601}
602
Devang Patel4c758ea2008-09-25 21:00:45 +0000603void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000604 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000605 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000606 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000607 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000608 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000609
David Greenef7014732010-01-05 01:29:58 +0000610 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000611}