blob: 81390f0bc1432e6260340abd8cec9b7864491335 [file] [log] [blame]
Bill Wendling034b94b2012-12-19 07:18:57 +00001//===-- Attribute.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-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 Wendling034b94b2012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000011// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000030// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling16f95662013-01-27 10:28:39 +000033Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
34 AttrBuilder B;
35 for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
36 I != E; ++I)
37 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000038 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000039}
Bill Wendling8e635db2012-10-08 21:47:17 +000040
Bill Wendling034b94b2012-12-19 07:18:57 +000041Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000043 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000044 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000045
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000049 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000057 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000063}
64
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000065Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
66 AttrBuilder B;
67 return get(Context, B.addAlignmentAttr(Align));
68}
69
70Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
71 uint64_t Align) {
72 AttrBuilder B;
73 return get(Context, B.addStackAlignmentAttr(Align));
74}
75
Bill Wendling629fb822012-12-22 00:37:52 +000076bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000077 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
Bill Wendling034b94b2012-12-19 07:18:57 +000080bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000081 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000082}
83
Bill Wendlinge66f3d32012-10-05 06:44:41 +000084/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000085unsigned Attribute::getAlignment() const {
86 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000087 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000088 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000089}
90
91/// This returns the stack alignment field of an attribute as a byte alignment
92/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000093unsigned Attribute::getStackAlignment() const {
94 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000095 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000096 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000097}
98
Bill Wendling92e287f2012-12-31 11:51:54 +000099bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000100 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +0000101}
Bill Wendling92e287f2012-12-31 11:51:54 +0000102bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000103 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000104}
105
Bill Wendling3467e302013-01-24 00:06:56 +0000106bool Attribute::operator<(Attribute A) const {
107 if (!pImpl && !A.pImpl) return false;
108 if (!pImpl) return true;
109 if (!A.pImpl) return false;
110 return *pImpl < *A.pImpl;
111}
112
Bill Wendling1db9b692013-01-09 23:36:50 +0000113uint64_t Attribute::Raw() const {
114 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000115}
116
Bill Wendling034b94b2012-12-19 07:18:57 +0000117std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000118 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000120 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000122 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000124 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000126 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000128 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000130 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000132 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000134 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000136 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000138 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000140 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000142 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000144 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000146 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000148 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000150 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000152 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000154 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000156 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000158 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000159 if (hasAttribute(Attribute::StackProtectStrong))
160 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000162 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000164 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000166 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000168 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000170 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000172 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000174 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000175 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000176 Result += ") ";
177 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000179 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000180 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000181 Result += " ";
182 }
James Molloy67ae1352012-12-20 16:04:27 +0000183 if (hasAttribute(Attribute::NoDuplicate))
184 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000185 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000186 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000187 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000188 return Result;
189}
190
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000191//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000192// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000193//===----------------------------------------------------------------------===//
194
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000195AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
196 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000197 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000198 if (!pImpl) return;
199
200 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
201 const AttributeWithIndex *AWI = 0;
202 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
203 if (AttrList[I].Index == Idx) {
204 AWI = &AttrList[I];
205 break;
206 }
207
Bill Wendlingaec71062013-01-18 21:56:07 +0000208 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000209
Bill Wendling956f1342013-01-18 21:11:39 +0000210 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000211
Bill Wendling956f1342013-01-18 21:11:39 +0000212 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
213 I = Attribute::AttrKind(I + 1)) {
214 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
215 Attrs.insert(I);
216
217 if (I == Attribute::Alignment)
218 Alignment = 1ULL << ((A >> 16) - 1);
219 else if (I == Attribute::StackAlignment)
220 StackAlignment = 1ULL << ((A >> 26)-1);
221 }
222 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000223}
224
Bill Wendling03198882013-01-04 23:27:34 +0000225void AttrBuilder::clear() {
226 Attrs.clear();
227 Alignment = StackAlignment = 0;
228}
229
230AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
231 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000232 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000233}
234
Bill Wendling03198882013-01-04 23:27:34 +0000235AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
236 Attrs.erase(Val);
237 if (Val == Attribute::Alignment)
238 Alignment = 0;
239 else if (Val == Attribute::StackAlignment)
240 StackAlignment = 0;
241
Bill Wendlinga19a5302012-10-14 04:10:01 +0000242 return *this;
243}
244
Bill Wendling702cc912012-10-15 20:35:56 +0000245AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000246 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000247
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000248 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
249 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000250
251 Attrs.insert(Attribute::Alignment);
252 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000253 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000254}
255
Bill Wendling03198882013-01-04 23:27:34 +0000256AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
257 // Default alignment, allow the target to define how to align it.
258 if (Align == 0) return *this;
259
260 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
261 assert(Align <= 0x100 && "Alignment too large.");
262
263 Attrs.insert(Attribute::StackAlignment);
264 StackAlignment = Align;
265 return *this;
266}
267
268AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
269 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
270 I = Attribute::AttrKind(I + 1)) {
271 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
272 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000273
Bill Wendling03198882013-01-04 23:27:34 +0000274 if (I == Attribute::Alignment)
275 Alignment = 1ULL << ((A >> 16) - 1);
276 else if (I == Attribute::StackAlignment)
277 StackAlignment = 1ULL << ((A >> 26)-1);
278 }
279 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000280
Bill Wendling3a106e62012-10-09 19:01:18 +0000281 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000282}
283
Bill Wendlingdefaca02013-01-22 21:15:51 +0000284AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
285 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000286
287 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000288 I = Attribute::AttrKind(I + 1))
289 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000290 Attrs.insert(I);
291
Bill Wendlingdefaca02013-01-22 21:15:51 +0000292 if (Attr.getAlignment())
293 Alignment = Attr.getAlignment();
294 if (Attr.getStackAlignment())
295 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000296 return *this;
297}
298
Bill Wendling034b94b2012-12-19 07:18:57 +0000299AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000300 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000301
302 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
303 I = Attribute::AttrKind(I + 1)) {
304 if (Mask & AttributeImpl::getAttrMask(I)) {
305 Attrs.erase(I);
306
307 if (I == Attribute::Alignment)
308 Alignment = 0;
309 else if (I == Attribute::StackAlignment)
310 StackAlignment = 0;
311 }
312 }
313
Bill Wendling5886b7b2012-10-14 06:39:53 +0000314 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000315}
316
Bill Wendling22bd6412013-01-03 01:54:39 +0000317bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000318 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000319}
320
Bill Wendling702cc912012-10-15 20:35:56 +0000321bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000322 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000323}
Bill Wendling60507d52013-01-04 20:54:35 +0000324
Bill Wendling034b94b2012-12-19 07:18:57 +0000325bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000326 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000327}
Bill Wendling60507d52013-01-04 20:54:35 +0000328
Bill Wendling702cc912012-10-15 20:35:56 +0000329bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000330 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000331}
332
Bill Wendling1db9b692013-01-09 23:36:50 +0000333uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000334 uint64_t Mask = 0;
335
336 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
337 E = Attrs.end(); I != E; ++I) {
338 Attribute::AttrKind Kind = *I;
339
340 if (Kind == Attribute::Alignment)
341 Mask |= (Log2_32(Alignment) + 1) << 16;
342 else if (Kind == Attribute::StackAlignment)
343 Mask |= (Log2_32(StackAlignment) + 1) << 26;
344 else
345 Mask |= AttributeImpl::getAttrMask(Kind);
346 }
347
348 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000349}
350
Bill Wendling03198882013-01-04 23:27:34 +0000351bool AttrBuilder::operator==(const AttrBuilder &B) {
352 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
353 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
354 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000355}
356
Chris Lattner58d74912008-03-12 17:45:29 +0000357//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000358// AttributeImpl Definition
359//===----------------------------------------------------------------------===//
360
Bill Wendling1bbd6442013-01-05 01:36:54 +0000361AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
362 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000363 Data = ConstantInt::get(Type::getInt64Ty(C), data);
364}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000365AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
366 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000367 Data = ConstantInt::get(Type::getInt64Ty(C), data);
368}
369AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000370 ArrayRef<Constant*> values)
371 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000372 Data = ConstantInt::get(Type::getInt64Ty(C), data);
373 Vals.reserve(values.size());
374 Vals.append(values.begin(), values.end());
375}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000376AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
377 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000378 Data = ConstantDataArray::getString(C, data);
379}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000380
Bill Wendling60507d52013-01-04 20:54:35 +0000381bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000382 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
383 return CI->getZExtValue() == Kind;
384 return false;
385}
Bill Wendling60507d52013-01-04 20:54:35 +0000386bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
387 return !(*this == Kind);
388}
Bill Wendling529ec712012-12-30 01:38:39 +0000389
Bill Wendling60507d52013-01-04 20:54:35 +0000390bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000391 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
392 if (CDA->isString())
393 return CDA->getAsString() == Kind;
394 return false;
395}
Bill Wendling3467e302013-01-24 00:06:56 +0000396
Bill Wendling60507d52013-01-04 20:54:35 +0000397bool AttributeImpl::operator!=(StringRef Kind) const {
398 return !(*this == Kind);
399}
Bill Wendling529ec712012-12-30 01:38:39 +0000400
Bill Wendling3467e302013-01-24 00:06:56 +0000401bool AttributeImpl::operator<(const AttributeImpl &AI) const {
402 if (!Data && !AI.Data) return false;
403 if (!Data && AI.Data) return true;
404 if (Data && !AI.Data) return false;
405
406 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
407 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
408
409 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
410 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
411
412 if (ThisCI && ThatCI)
413 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
414
415 if (ThisCI && ThatCDA)
416 return true;
417
418 if (ThisCDA && ThatCI)
419 return false;
420
421 return ThisCDA->getAsString() < ThatCDA->getAsString();
422}
423
Bill Wendling1db9b692013-01-09 23:36:50 +0000424uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000425 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000426 return cast<ConstantInt>(Data)->getZExtValue();
427}
428
Bill Wendling60507d52013-01-04 20:54:35 +0000429uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000430 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000431 case Attribute::EndAttrKinds:
432 case Attribute::AttrKindEmptyKey:
433 case Attribute::AttrKindTombstoneKey:
434 llvm_unreachable("Synthetic enumerators which should never get here");
435
Bill Wendling034b94b2012-12-19 07:18:57 +0000436 case Attribute::None: return 0;
437 case Attribute::ZExt: return 1 << 0;
438 case Attribute::SExt: return 1 << 1;
439 case Attribute::NoReturn: return 1 << 2;
440 case Attribute::InReg: return 1 << 3;
441 case Attribute::StructRet: return 1 << 4;
442 case Attribute::NoUnwind: return 1 << 5;
443 case Attribute::NoAlias: return 1 << 6;
444 case Attribute::ByVal: return 1 << 7;
445 case Attribute::Nest: return 1 << 8;
446 case Attribute::ReadNone: return 1 << 9;
447 case Attribute::ReadOnly: return 1 << 10;
448 case Attribute::NoInline: return 1 << 11;
449 case Attribute::AlwaysInline: return 1 << 12;
450 case Attribute::OptimizeForSize: return 1 << 13;
451 case Attribute::StackProtect: return 1 << 14;
452 case Attribute::StackProtectReq: return 1 << 15;
453 case Attribute::Alignment: return 31 << 16;
454 case Attribute::NoCapture: return 1 << 21;
455 case Attribute::NoRedZone: return 1 << 22;
456 case Attribute::NoImplicitFloat: return 1 << 23;
457 case Attribute::Naked: return 1 << 24;
458 case Attribute::InlineHint: return 1 << 25;
459 case Attribute::StackAlignment: return 7 << 26;
460 case Attribute::ReturnsTwice: return 1 << 29;
461 case Attribute::UWTable: return 1 << 30;
462 case Attribute::NonLazyBind: return 1U << 31;
463 case Attribute::AddressSafety: return 1ULL << 32;
464 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000465 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000466 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000467 }
468 llvm_unreachable("Unsupported attribute type");
469}
470
Bill Wendling60507d52013-01-04 20:54:35 +0000471bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000472 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000473}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000474
Bill Wendlingf6670722012-12-20 01:36:59 +0000475bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000476 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000477}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000478
Bill Wendlingf6670722012-12-20 01:36:59 +0000479uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000480 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000481 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000482}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000483
Bill Wendlingf6670722012-12-20 01:36:59 +0000484uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000485 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000486 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000487}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000488
Bill Wendling8456efb2013-01-09 00:32:55 +0000489void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
490 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000491 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000492#if 0
493 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000494 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
495 I != E; ++I)
496 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000497#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000498}
499
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000500//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000501// AttributeSetNode Definition
502//===----------------------------------------------------------------------===//
503
504AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
505 ArrayRef<Attribute> Attrs) {
506 if (Attrs.empty())
507 return 0;
508
509 // Otherwise, build a key to look up the existing attributes.
510 LLVMContextImpl *pImpl = C.pImpl;
511 FoldingSetNodeID ID;
512
513 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
514 std::sort(SortedAttrs.begin(), SortedAttrs.end());
515
516 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
517 E = SortedAttrs.end(); I != E; ++I)
518 I->Profile(ID);
519
520 void *InsertPoint;
521 AttributeSetNode *PA =
522 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
523
524 // If we didn't find any existing attributes of the same shape then create a
525 // new one and insert it.
526 if (!PA) {
527 PA = new AttributeSetNode(SortedAttrs);
528 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
529 }
530
531 // Return the AttributesListNode that we found or created.
532 return PA;
533}
534
535//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000536// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000537//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000538
Bill Wendling6a325cc2013-01-27 12:50:02 +0000539AttributeSetImpl::
540AttributeSetImpl(LLVMContext &C,
541 ArrayRef<AttributeWithIndex> attrs)
542 : Context(C), AttrList(attrs.begin(), attrs.end()) {
543 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
544 const AttributeWithIndex &AWI = attrs[I];
545 uint64_t Mask = AWI.Attrs.Raw();
546 SmallVector<Attribute, 8> Attrs;
547
548 for (Attribute::AttrKind II = Attribute::None;
549 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
550 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
551 AttrBuilder B;
552
553 if (II == Attribute::Alignment)
554 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
555 else if (II == Attribute::StackAlignment)
556 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
557 else
558 B.addAttribute(II);
559
560 Attrs.push_back(Attribute::get(C, B));
561 }
562 }
563
564 AttrNodes.push_back(std::make_pair(AWI.Index,
565 AttributeSetNode::get(C, Attrs)));
566 }
Bill Wendling6a325cc2013-01-27 12:50:02 +0000567
Bill Wendlinga5372d22013-01-27 21:20:06 +0000568 assert(AttrNodes.size() == AttrList.size() &&
569 "Number of attributes is different between lists!");
570#ifndef NDEBUG
571 for (unsigned I = 0, E = AttrNodes.size(); I != E; ++I)
572 assert((I == 0 || AttrNodes[I - 1].first < AttrNodes[I].first) &&
573 "Attributes not in ascending order!");
574#endif
Bill Wendling6a325cc2013-01-27 12:50:02 +0000575}
576
Bill Wendlingd05204a2013-01-27 23:41:29 +0000577uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
578 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
579 if (getSlotIndex(I) != Index) continue;
580 const AttributeSetNode *ASN = AttrNodes[I].second;
581 AttrBuilder B;
582
583 for (AttributeSetNode::const_iterator II = ASN->begin(),
584 IE = ASN->end(); II != IE; ++II)
585 B.addAttributes(*II);
586
587 assert(B.Raw() == AttrList[I].Attrs.Raw() &&
588 "Attributes aren't the same!");
589 return B.Raw();
590 }
591
592 return 0;
593}
594
Bill Wendling6a325cc2013-01-27 12:50:02 +0000595//===----------------------------------------------------------------------===//
596// AttributeSet Method Implementations
597//===----------------------------------------------------------------------===//
598
Bill Wendling28d65722013-01-23 06:14:59 +0000599AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
600 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000601 return pImpl && hasAttributes(Idx) ?
602 AttributeSet::get(pImpl->getContext(),
Bill Wendling28d65722013-01-23 06:14:59 +0000603 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
604 AttributeSet();
605}
606
Bill Wendling3fc4b962013-01-21 22:44:49 +0000607AttributeSet AttributeSet::getRetAttributes() const {
608 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000609 return pImpl && hasAttributes(ReturnIndex) ?
610 AttributeSet::get(pImpl->getContext(),
Bill Wendling3fc4b962013-01-21 22:44:49 +0000611 AttributeWithIndex::get(ReturnIndex,
612 getAttributes(ReturnIndex))) :
613 AttributeSet();
614}
615
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000616AttributeSet AttributeSet::getFnAttributes() const {
617 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000618 return pImpl && hasAttributes(FunctionIndex) ?
619 AttributeSet::get(pImpl->getContext(),
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000620 AttributeWithIndex::get(FunctionIndex,
621 getAttributes(FunctionIndex))) :
622 AttributeSet();
623}
624
Bill Wendling99faa3b2012-12-07 23:16:57 +0000625AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000626 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000627 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000628 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000629 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000630
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000631#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000632 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000633 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000634 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000635 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000636 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000637 }
638#endif
Bill Wendling77898682012-10-16 06:01:44 +0000639
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000640 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000641 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000642 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000643 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000644
Bill Wendling0976e002012-11-20 05:09:20 +0000645 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000646 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000647
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000648 // If we didn't find any existing attributes of the same shape then
649 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000650 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000651 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000652 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000653 }
Bill Wendling77898682012-10-16 06:01:44 +0000654
Devang Patel05988662008-09-25 21:00:45 +0000655 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000656 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000657}
658
Bill Wendling1bbd6442013-01-05 01:36:54 +0000659AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000660 // FIXME: This should be implemented as a loop that creates the
661 // AttributeWithIndexes that then are used to create the AttributeSet.
662 if (!B.hasAttributes())
663 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000664 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000665}
666
Bill Wendling28d65722013-01-23 06:14:59 +0000667AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000668 ArrayRef<Attribute::AttrKind> Kind) {
669 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
670 // replaced by an object that holds multiple Attribute::AttrKinds.
671 AttrBuilder B;
672 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
673 E = Kind.end(); I != E; ++I)
674 B.addAttribute(*I);
675 return get(C, Idx, B);
Bill Wendling28d65722013-01-23 06:14:59 +0000676}
677
Bill Wendling8e47daf2013-01-25 23:09:36 +0000678AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
679 SmallVector<AttributeWithIndex, 8> AttrList;
680 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
681 I != E; ++I) {
682 AttributeSet AS = *I;
Bill Wendlingec258982013-01-27 21:23:46 +0000683 if (!AS.pImpl) continue;
684 AttrList.append(AS.pImpl->AttrList.begin(), AS.pImpl->AttrList.end());
Bill Wendling8e47daf2013-01-25 23:09:36 +0000685 }
686
687 return get(C, AttrList);
688}
689
Bill Wendlingd05204a2013-01-27 23:41:29 +0000690/// \brief Return the number of slots used in this attribute list. This is the
691/// number of arguments that have an attribute set on them (including the
692/// function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000693unsigned AttributeSet::getNumSlots() const {
Bill Wendlingec258982013-01-27 21:23:46 +0000694 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000695}
696
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000697unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000698 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000699 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000700 return pImpl->getSlotIndex(Slot);
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000701}
702
Bill Wendling8e47daf2013-01-25 23:09:36 +0000703AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000704 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling8e47daf2013-01-25 23:09:36 +0000705 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000706 return pImpl->getSlotAttributes(Slot);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000707}
708
Bill Wendling831737d2012-12-30 10:32:01 +0000709bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
710 return getAttributes(Index).hasAttribute(Kind);
711}
712
713bool AttributeSet::hasAttributes(unsigned Index) const {
714 return getAttributes(Index).hasAttributes();
715}
716
717std::string AttributeSet::getAsString(unsigned Index) const {
718 return getAttributes(Index).getAsString();
719}
720
Bill Wendling956f1342013-01-18 21:11:39 +0000721unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
722 return getAttributes(Idx).getAlignment();
723}
724
Bill Wendling831737d2012-12-30 10:32:01 +0000725unsigned AttributeSet::getStackAlignment(unsigned Index) const {
726 return getAttributes(Index).getStackAlignment();
727}
728
Bill Wendling1db9b692013-01-09 23:36:50 +0000729uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000730 // FIXME: Remove this.
Bill Wendlingd05204a2013-01-27 23:41:29 +0000731 return pImpl ? pImpl->Raw(Index) : 0;
Bill Wendling831737d2012-12-30 10:32:01 +0000732}
733
Bill Wendling77898682012-10-16 06:01:44 +0000734/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000735Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000736 if (pImpl == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000737
Bill Wendlingec258982013-01-27 21:23:46 +0000738 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000739 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
740 if (Attrs[i].Index == Idx)
741 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000742
Bill Wendling034b94b2012-12-19 07:18:57 +0000743 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000744}
745
746/// hasAttrSomewhere - Return true if the specified attribute is set for at
747/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000748bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000749 if (pImpl == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000750
Bill Wendlingec258982013-01-27 21:23:46 +0000751 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000752 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000753 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000754 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000755
Chris Lattner58d74912008-03-12 17:45:29 +0000756 return false;
757}
758
Bill Wendlingdefaca02013-01-22 21:15:51 +0000759AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
760 Attribute::AttrKind Attr) const {
761 return addAttr(C, Idx, Attribute::get(C, Attr));
762}
763
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000764AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
765 AttributeSet Attrs) const {
766 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000767}
768
Bill Wendling99faa3b2012-12-07 23:16:57 +0000769AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000770 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000771 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000772#ifndef NDEBUG
773 // FIXME it is not obvious how this should work for alignment.
774 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000775 unsigned OldAlign = OldAttrs.getAlignment();
776 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000777 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000778 "Attempt to change alignment!");
779#endif
Bill Wendling77898682012-10-16 06:01:44 +0000780
Bill Wendling702cc912012-10-15 20:35:56 +0000781 AttrBuilder NewAttrs =
782 AttrBuilder(OldAttrs).addAttributes(Attrs);
783 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000784 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000785
Devang Patel05988662008-09-25 21:00:45 +0000786 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendlingec258982013-01-27 21:23:46 +0000787 if (pImpl == 0)
Devang Patel05988662008-09-25 21:00:45 +0000788 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000789 else {
Bill Wendlingec258982013-01-27 21:23:46 +0000790 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000791 unsigned i = 0, e = OldAttrList.size();
792 // Copy attributes for arguments before this one.
793 for (; i != e && OldAttrList[i].Index < Idx; ++i)
794 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000795
Chris Lattner58d74912008-03-12 17:45:29 +0000796 // If there are attributes already at this index, merge them in.
797 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000798 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000799 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000800 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000801 ++i;
802 }
Bill Wendling77898682012-10-16 06:01:44 +0000803
Devang Patel05988662008-09-25 21:00:45 +0000804 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000805
Chris Lattner58d74912008-03-12 17:45:29 +0000806 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000807 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000808 OldAttrList.begin()+i, OldAttrList.end());
809 }
Bill Wendling77898682012-10-16 06:01:44 +0000810
Bill Wendling0976e002012-11-20 05:09:20 +0000811 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000812}
813
Bill Wendling8246df62013-01-23 00:45:55 +0000814AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
815 Attribute::AttrKind Attr) const {
816 return removeAttr(C, Idx, Attribute::get(C, Attr));
817}
818
819AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
820 AttributeSet Attrs) const {
821 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
822}
823
Bill Wendling99faa3b2012-12-07 23:16:57 +0000824AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000825 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000826#ifndef NDEBUG
827 // FIXME it is not obvious how this should work for alignment.
828 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000829 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000830 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000831#endif
Bill Wendlingec258982013-01-27 21:23:46 +0000832 if (pImpl == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000833
Bill Wendling034b94b2012-12-19 07:18:57 +0000834 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000835 AttrBuilder NewAttrs =
836 AttrBuilder(OldAttrs).removeAttributes(Attrs);
837 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000838 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000839
Devang Patel05988662008-09-25 21:00:45 +0000840 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendlingec258982013-01-27 21:23:46 +0000841 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000842 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000843
Chris Lattner58d74912008-03-12 17:45:29 +0000844 // Copy attributes for arguments before this one.
845 for (; i != e && OldAttrList[i].Index < Idx; ++i)
846 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000847
Chris Lattner58d74912008-03-12 17:45:29 +0000848 // If there are attributes already at this index, merge them in.
849 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000850 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000851 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000852 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000853 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000854 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000855
Chris Lattner58d74912008-03-12 17:45:29 +0000856 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000857 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000858 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000859
Bill Wendling0976e002012-11-20 05:09:20 +0000860 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000861}
862
Bill Wendling99faa3b2012-12-07 23:16:57 +0000863void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000864 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000865 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling85875642013-01-25 21:46:52 +0000866 unsigned Index = getSlotIndex(i);
867 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000868 }
Bill Wendling77898682012-10-16 06:01:44 +0000869
David Greeneef1894e2010-01-05 01:29:58 +0000870 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000871}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000872
873//===----------------------------------------------------------------------===//
874// AttributeFuncs Function Defintions
875//===----------------------------------------------------------------------===//
876
877Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
878 AttrBuilder Incompatible;
879
880 if (!Ty->isIntegerTy())
881 // Attribute that only apply to integers.
882 Incompatible.addAttribute(Attribute::SExt)
883 .addAttribute(Attribute::ZExt);
884
885 if (!Ty->isPointerTy())
886 // Attribute that only apply to pointers.
887 Incompatible.addAttribute(Attribute::ByVal)
888 .addAttribute(Attribute::Nest)
889 .addAttribute(Attribute::NoAlias)
890 .addAttribute(Attribute::NoCapture)
891 .addAttribute(Attribute::StructRet);
892
893 return Attribute::get(Ty->getContext(), Incompatible);
894}
895
896/// encodeLLVMAttributesForBitcode - This returns an integer containing an
897/// encoding of all the LLVM attributes found in the given attribute bitset.
898/// Any change to this encoding is a breaking change to bitcode compatibility.
899uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
900 unsigned Index) {
901 // FIXME: It doesn't make sense to store the alignment information as an
902 // expanded out value, we should store it as a log2 value. However, we can't
903 // just change that here without breaking bitcode compatibility. If this ever
904 // becomes a problem in practice, we should introduce new tag numbers in the
905 // bitcode file and have those tags use a more efficiently encoded alignment
906 // field.
907
908 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
909 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
910 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
911 if (Attrs.hasAttribute(Index, Attribute::Alignment))
912 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
913 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
914 return EncodedAttrs;
915}
916
917/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
918/// the LLVM attributes that have been decoded from the given integer. This
919/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
920Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
921 uint64_t EncodedAttrs){
922 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
923 // the bits above 31 down by 11 bits.
924 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
925 assert((!Alignment || isPowerOf2_32(Alignment)) &&
926 "Alignment must be a power of two.");
927
928 AttrBuilder B(EncodedAttrs & 0xffff);
929 if (Alignment)
930 B.addAlignmentAttr(Alignment);
931 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
932 return Attribute::get(C, B);
933}
934