blob: 5bde5638a1e89f2e1cb946262eb09509cc9ca521 [file] [log] [blame]
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001//===-- Attribute.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 Wendling3d7b0b82012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling6848e382012-12-19 22:42:22 +000011// AttributeSetImpl, 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 Wendling4607f4b2012-12-20 01:36:59 +000016#include "AttributeImpl.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 Wendling3d7b0b82012-12-19 07:18:57 +000029// Attribute Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000031
Bill Wendling3d7b0b82012-12-19 07:18:57 +000032Attribute Attribute::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 Wendling3d7b0b82012-12-19 07:18:57 +000037 return Attribute::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000038}
Bill Wendling73ea2de2012-10-08 21:47:17 +000039
Bill Wendling3d7b0b82012-12-19 07:18:57 +000040Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
41 // If there are no attributes, return an empty Attribute class.
Bill Wendling3ffbac42012-10-16 05:55:09 +000042 if (!B.hasAttributes())
Bill Wendling3d7b0b82012-12-19 07:18:57 +000043 return Attribute();
Bill Wendling73ea2de2012-10-08 21:47:17 +000044
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;
Bill Wendling4607f4b2012-12-20 01:36:59 +000051 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000052
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 Wendling4607f4b2012-12-20 01:36:59 +000056 PA = new AttributeImpl(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.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000061 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000062}
63
Bill Wendling3d7b0b82012-12-19 07:18:57 +000064bool Attribute::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 Wendling3d7b0b82012-12-19 07:18:57 +000068bool Attribute::hasAttributes() const {
Bill Wendling8c3e65d2012-10-15 05:40:12 +000069 return Attrs && Attrs->hasAttributes();
70}
71
Bill Wendling3d7b0b82012-12-19 07:18:57 +000072bool Attribute::hasAttributes(const Attribute &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.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000077unsigned Attribute::getAlignment() const {
78 if (!hasAttribute(Attribute::Alignment))
Bill Wendlingb3723342012-10-09 20:56:48 +000079 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.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000085unsigned Attribute::getStackAlignment() const {
86 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendlingb3723342012-10-09 20:56:48 +000087 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 Wendling3d7b0b82012-12-19 07:18:57 +000091uint64_t Attribute::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 Wendling3d7b0b82012-12-19 07:18:57 +000095Attribute Attribute::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 Wendling3d7b0b82012-12-19 07:18:57 +000099 // Attribute that only apply to integers.
100 Incompatible.addAttribute(Attribute::SExt)
101 .addAttribute(Attribute::ZExt);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000102
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 if (!Ty->isPointerTy())
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000104 // Attribute that only apply to pointers.
105 Incompatible.addAttribute(Attribute::ByVal)
106 .addAttribute(Attribute::Nest)
107 .addAttribute(Attribute::NoAlias)
108 .addAttribute(Attribute::NoCapture)
109 .addAttribute(Attribute::StructRet);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000110
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000111 return Attribute::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.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000117uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
Bill Wendling50d27842012-10-15 20:35:56 +0000118 // 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;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000128 if (Attrs.hasAttribute(Attribute::Alignment))
Bill Wendling50d27842012-10-15 20:35:56 +0000129 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'.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000137Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
Bill Wendling50d27842012-10-15 20:35:56 +0000138 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 Wendling3d7b0b82012-12-19 07:18:57 +0000149 return Attribute::get(C, B);
Bill Wendling50d27842012-10-15 20:35:56 +0000150}
151
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000152std::string Attribute::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000153 std::string Result;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000155 Result += "zeroext ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000157 Result += "signext ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000159 Result += "noreturn ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000160 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000161 Result += "nounwind ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000163 Result += "uwtable ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000165 Result += "returns_twice ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000166 if (hasAttribute(Attribute::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000167 Result += "inreg ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000168 if (hasAttribute(Attribute::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000169 Result += "noalias ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000170 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000171 Result += "nocapture ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000173 Result += "sret ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000175 Result += "byval ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000177 Result += "nest ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000179 Result += "readnone ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000180 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000181 Result += "readonly ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000182 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000183 Result += "optsize ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000184 if (hasAttribute(Attribute::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000185 Result += "noinline ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000186 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000187 Result += "inlinehint ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000188 if (hasAttribute(Attribute::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000189 Result += "alwaysinline ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000190 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000191 Result += "ssp ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000192 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000193 Result += "sspreq ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000195 Result += "noredzone ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000197 Result += "noimplicitfloat ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000198 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000199 Result += "naked ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000200 if (hasAttribute(Attribute::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000201 Result += "nonlazybind ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000203 Result += "address_safety ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::MinSize))
Quentin Colombet5799e9f2012-10-30 16:32:52 +0000205 Result += "minsize ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000206 if (hasAttribute(Attribute::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 Wendling3d7b0b82012-12-19 07:18:57 +0000211 if (hasAttribute(Attribute::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 }
James Molloy4f6fb952012-12-20 16:04:27 +0000216 if (hasAttribute(Attribute::NoDuplicate))
217 Result += "noduplicate ";
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000218 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000219 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000220 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000221 return Result;
222}
223
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000224//===----------------------------------------------------------------------===//
Bill Wendling50d27842012-10-15 20:35:56 +0000225// AttrBuilder Implementation
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000226//===----------------------------------------------------------------------===//
227
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000228AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrVal Val){
Bill Wendling4607f4b2012-12-20 01:36:59 +0000229 Bits |= AttributeImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000230 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000231}
232
Bill Wendling50d27842012-10-15 20:35:56 +0000233AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendling1fcc8222012-10-14 04:10:01 +0000234 Bits |= Val;
235 return *this;
236}
237
Bill Wendling50d27842012-10-15 20:35:56 +0000238AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000239 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000240 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
241 assert(Align <= 0x40000000 && "Alignment too large.");
242 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000243 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000244}
Bill Wendling50d27842012-10-15 20:35:56 +0000245AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000246 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000247 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000248 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
249 assert(Align <= 0x100 && "Alignment too large.");
250 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000251 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000252}
253
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000254AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrVal Val) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000255 Bits &= ~AttributeImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000256 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000257}
258
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000259AttrBuilder &AttrBuilder::addAttributes(const Attribute &A) {
Bill Wendling5c407ed2012-10-14 07:17:34 +0000260 Bits |= A.Raw();
261 return *this;
262}
263
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000264AttrBuilder &AttrBuilder::removeAttributes(const Attribute &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 Wendling3d7b0b82012-12-19 07:18:57 +0000269bool AttrBuilder::hasAttribute(Attribute::AttrVal A) const {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000270 return Bits & AttributeImpl::getAttrMask(A);
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000271}
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 Wendling3d7b0b82012-12-19 07:18:57 +0000276bool AttrBuilder::hasAttributes(const Attribute &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 Wendling4607f4b2012-12-20 01:36:59 +0000280 return Bits & AttributeImpl::getAttrMask(Attribute::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;
NAKAMURA Takumib54ac212012-11-19 10:03:09 +0000286 return 1ULL <<
Bill Wendling4607f4b2012-12-20 01:36:59 +0000287 (((Bits & AttributeImpl::getAttrMask(Attribute::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;
NAKAMURA Takumib54ac212012-11-19 10:03:09 +0000293 return 1ULL <<
Bill Wendling4607f4b2012-12-20 01:36:59 +0000294 (((Bits & AttributeImpl::getAttrMask(Attribute::StackAlignment))>>26)-1);
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000295}
296
Chris Lattner8a923e72008-03-12 17:45:29 +0000297//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000298// AttributeImpl Definition
299//===----------------------------------------------------------------------===//
300
Bill Wendling4607f4b2012-12-20 01:36:59 +0000301uint64_t AttributeImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000302 switch (Val) {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000303 case Attribute::None: return 0;
304 case Attribute::ZExt: return 1 << 0;
305 case Attribute::SExt: return 1 << 1;
306 case Attribute::NoReturn: return 1 << 2;
307 case Attribute::InReg: return 1 << 3;
308 case Attribute::StructRet: return 1 << 4;
309 case Attribute::NoUnwind: return 1 << 5;
310 case Attribute::NoAlias: return 1 << 6;
311 case Attribute::ByVal: return 1 << 7;
312 case Attribute::Nest: return 1 << 8;
313 case Attribute::ReadNone: return 1 << 9;
314 case Attribute::ReadOnly: return 1 << 10;
315 case Attribute::NoInline: return 1 << 11;
316 case Attribute::AlwaysInline: return 1 << 12;
317 case Attribute::OptimizeForSize: return 1 << 13;
318 case Attribute::StackProtect: return 1 << 14;
319 case Attribute::StackProtectReq: return 1 << 15;
320 case Attribute::Alignment: return 31 << 16;
321 case Attribute::NoCapture: return 1 << 21;
322 case Attribute::NoRedZone: return 1 << 22;
323 case Attribute::NoImplicitFloat: return 1 << 23;
324 case Attribute::Naked: return 1 << 24;
325 case Attribute::InlineHint: return 1 << 25;
326 case Attribute::StackAlignment: return 7 << 26;
327 case Attribute::ReturnsTwice: return 1 << 29;
328 case Attribute::UWTable: return 1 << 30;
329 case Attribute::NonLazyBind: return 1U << 31;
330 case Attribute::AddressSafety: return 1ULL << 32;
331 case Attribute::MinSize: return 1ULL << 33;
James Molloy4f6fb952012-12-20 16:04:27 +0000332 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000333 }
334 llvm_unreachable("Unsupported attribute type");
335}
336
Bill Wendling4607f4b2012-12-20 01:36:59 +0000337bool AttributeImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000338 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000339}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000340
Bill Wendling4607f4b2012-12-20 01:36:59 +0000341bool AttributeImpl::hasAttributes() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000342 return Bits != 0;
343}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000344
Bill Wendling4607f4b2012-12-20 01:36:59 +0000345bool AttributeImpl::hasAttributes(const Attribute &A) const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000346 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
347}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000348
Bill Wendling4607f4b2012-12-20 01:36:59 +0000349uint64_t AttributeImpl::getAlignment() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000350 return Bits & getAttrMask(Attribute::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000351}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000352
Bill Wendling4607f4b2012-12-20 01:36:59 +0000353uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000354 return Bits & getAttrMask(Attribute::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000355}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000356
Bill Wendlinge38b8042012-09-26 21:07:29 +0000357//===----------------------------------------------------------------------===//
Bill Wendling6848e382012-12-19 22:42:22 +0000358// AttributeSetImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000359//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000360
Bill Wendlinge94d8432012-12-07 23:16:57 +0000361AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling623d8ce2012-12-12 19:21:53 +0000362 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000363 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000364 if (Attrs.empty())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000365 return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000366
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000367#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000368 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000369 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000370 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000371 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000372 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000373 }
374#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000375
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000376 // Otherwise, build a key to look up the existing attributes.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000377 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000378 FoldingSetNodeID ID;
Bill Wendling6848e382012-12-19 22:42:22 +0000379 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000380
Bill Wendlingf86efb92012-11-20 05:09:20 +0000381 void *InsertPoint;
Bill Wendling6848e382012-12-19 22:42:22 +0000382 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
Bill Wendlingf86efb92012-11-20 05:09:20 +0000383 InsertPoint);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000384
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000385 // If we didn't find any existing attributes of the same shape then
386 // create a new one and insert it.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000387 if (!PA) {
Bill Wendling6ad6c3b2012-12-19 23:55:43 +0000388 PA = new AttributeSetImpl(C, Attrs);
Bill Wendlingf86efb92012-11-20 05:09:20 +0000389 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000390 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000391
Devang Patel4c758ea2008-09-25 21:00:45 +0000392 // Return the AttributesList that we found or created.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000393 return AttributeSet(PA);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000394}
395
Chris Lattner8a923e72008-03-12 17:45:29 +0000396//===----------------------------------------------------------------------===//
Bill Wendlinge94d8432012-12-07 23:16:57 +0000397// AttributeSet Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000398//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000399
Bill Wendlinge94d8432012-12-07 23:16:57 +0000400const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000401 if (AttrList == RHS.AttrList) return *this;
Chris Lattner8a923e72008-03-12 17:45:29 +0000402
Bill Wendlingf86efb92012-11-20 05:09:20 +0000403 AttrList = RHS.AttrList;
404 return *this;
Chris Lattner8a923e72008-03-12 17:45:29 +0000405}
406
Bill Wendlinga529ade2012-10-16 06:01:44 +0000407/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner8a923e72008-03-12 17:45:29 +0000408/// This is the number of arguments that have an attribute set on them
409/// (including the function itself).
Bill Wendlinge94d8432012-12-07 23:16:57 +0000410unsigned AttributeSet::getNumSlots() const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000412}
413
Devang Patel4c758ea2008-09-25 21:00:45 +0000414/// getSlot - Return the AttributeWithIndex at the specified slot. This
415/// holds a number plus a set of attributes.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000416const AttributeWithIndex &AttributeSet::getSlot(unsigned Slot) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
418 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000419}
420
Bill Wendlinga529ade2012-10-16 06:01:44 +0000421/// getAttributes - The attributes for the specified index are returned.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000422/// Attribute for the result are denoted with Idx = 0. Function notes are
Bill Wendlinga529ade2012-10-16 06:01:44 +0000423/// denoted with idx = ~0.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000424Attribute AttributeSet::getAttributes(unsigned Idx) const {
425 if (AttrList == 0) return Attribute();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000426
Devang Patel4c758ea2008-09-25 21:00:45 +0000427 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000428 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
429 if (Attrs[i].Index == Idx)
430 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000431
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000432 return Attribute();
Chris Lattner8a923e72008-03-12 17:45:29 +0000433}
434
435/// hasAttrSomewhere - Return true if the specified attribute is set for at
436/// least one parameter or for the return value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000437bool AttributeSet::hasAttrSomewhere(Attribute::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000439
Devang Patel4c758ea2008-09-25 21:00:45 +0000440 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000441 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000442 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000443 return true;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000444
Chris Lattner8a923e72008-03-12 17:45:29 +0000445 return false;
446}
447
Bill Wendlinge94d8432012-12-07 23:16:57 +0000448unsigned AttributeSet::getNumAttrs() const {
Bill Wendling70f39172012-10-09 00:01:21 +0000449 return AttrList ? AttrList->Attrs.size() : 0;
450}
451
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000452Attribute &AttributeSet::getAttributesAtIndex(unsigned i) const {
Bill Wendling70f39172012-10-09 00:01:21 +0000453 assert(AttrList && "Trying to get an attribute from an empty list!");
454 assert(i < AttrList->Attrs.size() && "Index out of range!");
455 return AttrList->Attrs[i].Attrs;
456}
Chris Lattner8a923e72008-03-12 17:45:29 +0000457
Bill Wendlinge94d8432012-12-07 23:16:57 +0000458AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000459 Attribute Attrs) const {
460 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000461#ifndef NDEBUG
462 // FIXME it is not obvious how this should work for alignment.
463 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000464 unsigned OldAlign = OldAttrs.getAlignment();
465 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000466 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000467 "Attempt to change alignment!");
468#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000469
Bill Wendling50d27842012-10-15 20:35:56 +0000470 AttrBuilder NewAttrs =
471 AttrBuilder(OldAttrs).addAttributes(Attrs);
472 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000473 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000474
Devang Patel4c758ea2008-09-25 21:00:45 +0000475 SmallVector<AttributeWithIndex, 8> NewAttrList;
476 if (AttrList == 0)
477 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000478 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000479 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000480 unsigned i = 0, e = OldAttrList.size();
481 // Copy attributes for arguments before this one.
482 for (; i != e && OldAttrList[i].Index < Idx; ++i)
483 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000484
Chris Lattner8a923e72008-03-12 17:45:29 +0000485 // If there are attributes already at this index, merge them in.
486 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000487 Attrs =
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000488 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000489 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000490 ++i;
491 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000492
Devang Patel4c758ea2008-09-25 21:00:45 +0000493 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000494
Chris Lattner8a923e72008-03-12 17:45:29 +0000495 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000496 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000497 OldAttrList.begin()+i, OldAttrList.end());
498 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000499
Bill Wendlingf86efb92012-11-20 05:09:20 +0000500 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000501}
502
Bill Wendlinge94d8432012-12-07 23:16:57 +0000503AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000504 Attribute Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000505#ifndef NDEBUG
506 // FIXME it is not obvious how this should work for alignment.
507 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000508 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000509 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000510#endif
Bill Wendlinge94d8432012-12-07 23:16:57 +0000511 if (AttrList == 0) return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000512
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000513 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000514 AttrBuilder NewAttrs =
515 AttrBuilder(OldAttrs).removeAttributes(Attrs);
516 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000517 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000518
Devang Patel4c758ea2008-09-25 21:00:45 +0000519 SmallVector<AttributeWithIndex, 8> NewAttrList;
520 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000521 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000522
Chris Lattner8a923e72008-03-12 17:45:29 +0000523 // Copy attributes for arguments before this one.
524 for (; i != e && OldAttrList[i].Index < Idx; ++i)
525 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000526
Chris Lattner8a923e72008-03-12 17:45:29 +0000527 // If there are attributes already at this index, merge them in.
528 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000529 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000530 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000531 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000532 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000533 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000534
Chris Lattner8a923e72008-03-12 17:45:29 +0000535 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000536 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000537 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000538
Bill Wendlingf86efb92012-11-20 05:09:20 +0000539 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000540}
541
Bill Wendlinge94d8432012-12-07 23:16:57 +0000542void AttributeSet::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000543 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000544 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000545 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000546 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000547 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000548
David Greenef7014732010-01-05 01:29:58 +0000549 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000550}