blob: 00b542dbc1da2d85c8a9e0e6d02981a84149fa9b [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 Wendling629fb822012-12-22 00:37:52 +000065bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000066 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000067}
68
Bill Wendling034b94b2012-12-19 07:18:57 +000069bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000070 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000071}
72
Bill Wendlinge66f3d32012-10-05 06:44:41 +000073/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000074unsigned Attribute::getAlignment() const {
75 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000076 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000077 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
80/// This returns the stack alignment field of an attribute as a byte alignment
81/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000082unsigned Attribute::getStackAlignment() const {
83 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000084 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000085 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000086}
87
Bill Wendling92e287f2012-12-31 11:51:54 +000088bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000089 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000090}
Bill Wendling92e287f2012-12-31 11:51:54 +000091bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000092 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +000093}
94
Bill Wendling3467e302013-01-24 00:06:56 +000095bool Attribute::operator<(Attribute A) const {
96 if (!pImpl && !A.pImpl) return false;
97 if (!pImpl) return true;
98 if (!A.pImpl) return false;
99 return *pImpl < *A.pImpl;
100}
101
Bill Wendling1db9b692013-01-09 23:36:50 +0000102uint64_t Attribute::Raw() const {
103 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000104}
105
Bill Wendling034b94b2012-12-19 07:18:57 +0000106std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000107 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000108 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000109 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000110 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000111 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000112 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000113 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000115 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000116 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000117 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000118 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000119 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000121 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000123 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000124 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000125 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000127 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000128 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000129 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000130 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000131 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000132 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000133 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000134 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000135 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000137 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000138 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000139 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000140 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000141 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000143 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000145 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000146 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000147 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000148 if (hasAttribute(Attribute::StackProtectStrong))
149 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000150 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000151 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000153 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000155 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000157 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000159 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000161 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000163 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000164 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000165 Result += ") ";
166 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000168 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000169 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000170 Result += " ";
171 }
James Molloy67ae1352012-12-20 16:04:27 +0000172 if (hasAttribute(Attribute::NoDuplicate))
173 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000174 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000175 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000176 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000177 return Result;
178}
179
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000180//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000181// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000182//===----------------------------------------------------------------------===//
183
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000184AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
185 : Alignment(0), StackAlignment(0) {
186 AttributeSetImpl *pImpl = AS.AttrList;
187 if (!pImpl) return;
188
189 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
190 const AttributeWithIndex *AWI = 0;
191 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
192 if (AttrList[I].Index == Idx) {
193 AWI = &AttrList[I];
194 break;
195 }
196
Bill Wendlingaec71062013-01-18 21:56:07 +0000197 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000198
Bill Wendling956f1342013-01-18 21:11:39 +0000199 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000200
Bill Wendling956f1342013-01-18 21:11:39 +0000201 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
202 I = Attribute::AttrKind(I + 1)) {
203 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
204 Attrs.insert(I);
205
206 if (I == Attribute::Alignment)
207 Alignment = 1ULL << ((A >> 16) - 1);
208 else if (I == Attribute::StackAlignment)
209 StackAlignment = 1ULL << ((A >> 26)-1);
210 }
211 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000212}
213
Bill Wendling03198882013-01-04 23:27:34 +0000214void AttrBuilder::clear() {
215 Attrs.clear();
216 Alignment = StackAlignment = 0;
217}
218
219AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
220 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000221 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000222}
223
Bill Wendling03198882013-01-04 23:27:34 +0000224AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
225 Attrs.erase(Val);
226 if (Val == Attribute::Alignment)
227 Alignment = 0;
228 else if (Val == Attribute::StackAlignment)
229 StackAlignment = 0;
230
Bill Wendlinga19a5302012-10-14 04:10:01 +0000231 return *this;
232}
233
Bill Wendling702cc912012-10-15 20:35:56 +0000234AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000235 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000236
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000237 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
238 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000239
240 Attrs.insert(Attribute::Alignment);
241 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000242 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000243}
244
Bill Wendling03198882013-01-04 23:27:34 +0000245AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
246 // Default alignment, allow the target to define how to align it.
247 if (Align == 0) return *this;
248
249 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
250 assert(Align <= 0x100 && "Alignment too large.");
251
252 Attrs.insert(Attribute::StackAlignment);
253 StackAlignment = Align;
254 return *this;
255}
256
257AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
258 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
259 I = Attribute::AttrKind(I + 1)) {
260 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
261 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000262
Bill Wendling03198882013-01-04 23:27:34 +0000263 if (I == Attribute::Alignment)
264 Alignment = 1ULL << ((A >> 16) - 1);
265 else if (I == Attribute::StackAlignment)
266 StackAlignment = 1ULL << ((A >> 26)-1);
267 }
268 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000269
Bill Wendling3a106e62012-10-09 19:01:18 +0000270 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000271}
272
Bill Wendlingdefaca02013-01-22 21:15:51 +0000273AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
274 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000275
276 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000277 I = Attribute::AttrKind(I + 1))
278 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000279 Attrs.insert(I);
280
Bill Wendlingdefaca02013-01-22 21:15:51 +0000281 if (Attr.getAlignment())
282 Alignment = Attr.getAlignment();
283 if (Attr.getStackAlignment())
284 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000285 return *this;
286}
287
Bill Wendling034b94b2012-12-19 07:18:57 +0000288AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000289 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000290
291 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
292 I = Attribute::AttrKind(I + 1)) {
293 if (Mask & AttributeImpl::getAttrMask(I)) {
294 Attrs.erase(I);
295
296 if (I == Attribute::Alignment)
297 Alignment = 0;
298 else if (I == Attribute::StackAlignment)
299 StackAlignment = 0;
300 }
301 }
302
Bill Wendling5886b7b2012-10-14 06:39:53 +0000303 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000304}
305
Bill Wendling22bd6412013-01-03 01:54:39 +0000306bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000307 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000308}
309
Bill Wendling702cc912012-10-15 20:35:56 +0000310bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000311 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000312}
Bill Wendling60507d52013-01-04 20:54:35 +0000313
Bill Wendling034b94b2012-12-19 07:18:57 +0000314bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000315 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000316}
Bill Wendling60507d52013-01-04 20:54:35 +0000317
Bill Wendling702cc912012-10-15 20:35:56 +0000318bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000319 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000320}
321
Bill Wendling1db9b692013-01-09 23:36:50 +0000322uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000323 uint64_t Mask = 0;
324
325 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
326 E = Attrs.end(); I != E; ++I) {
327 Attribute::AttrKind Kind = *I;
328
329 if (Kind == Attribute::Alignment)
330 Mask |= (Log2_32(Alignment) + 1) << 16;
331 else if (Kind == Attribute::StackAlignment)
332 Mask |= (Log2_32(StackAlignment) + 1) << 26;
333 else
334 Mask |= AttributeImpl::getAttrMask(Kind);
335 }
336
337 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000338}
339
Bill Wendling03198882013-01-04 23:27:34 +0000340bool AttrBuilder::operator==(const AttrBuilder &B) {
341 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
342 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
343 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000344}
345
Chris Lattner58d74912008-03-12 17:45:29 +0000346//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000347// AttributeImpl Definition
348//===----------------------------------------------------------------------===//
349
Bill Wendling1bbd6442013-01-05 01:36:54 +0000350AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
351 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000352 Data = ConstantInt::get(Type::getInt64Ty(C), data);
353}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000354AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
355 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000356 Data = ConstantInt::get(Type::getInt64Ty(C), data);
357}
358AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000359 ArrayRef<Constant*> values)
360 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000361 Data = ConstantInt::get(Type::getInt64Ty(C), data);
362 Vals.reserve(values.size());
363 Vals.append(values.begin(), values.end());
364}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000365AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
366 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000367 Data = ConstantDataArray::getString(C, data);
368}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000369
Bill Wendling60507d52013-01-04 20:54:35 +0000370bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000371 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
372 return CI->getZExtValue() == Kind;
373 return false;
374}
Bill Wendling60507d52013-01-04 20:54:35 +0000375bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
376 return !(*this == Kind);
377}
Bill Wendling529ec712012-12-30 01:38:39 +0000378
Bill Wendling60507d52013-01-04 20:54:35 +0000379bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000380 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
381 if (CDA->isString())
382 return CDA->getAsString() == Kind;
383 return false;
384}
Bill Wendling3467e302013-01-24 00:06:56 +0000385
Bill Wendling60507d52013-01-04 20:54:35 +0000386bool AttributeImpl::operator!=(StringRef Kind) const {
387 return !(*this == Kind);
388}
Bill Wendling529ec712012-12-30 01:38:39 +0000389
Bill Wendling3467e302013-01-24 00:06:56 +0000390bool AttributeImpl::operator<(const AttributeImpl &AI) const {
391 if (!Data && !AI.Data) return false;
392 if (!Data && AI.Data) return true;
393 if (Data && !AI.Data) return false;
394
395 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
396 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
397
398 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
399 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
400
401 if (ThisCI && ThatCI)
402 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
403
404 if (ThisCI && ThatCDA)
405 return true;
406
407 if (ThisCDA && ThatCI)
408 return false;
409
410 return ThisCDA->getAsString() < ThatCDA->getAsString();
411}
412
Bill Wendling1db9b692013-01-09 23:36:50 +0000413uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000414 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000415 return cast<ConstantInt>(Data)->getZExtValue();
416}
417
Bill Wendling60507d52013-01-04 20:54:35 +0000418uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000419 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000420 case Attribute::EndAttrKinds:
421 case Attribute::AttrKindEmptyKey:
422 case Attribute::AttrKindTombstoneKey:
423 llvm_unreachable("Synthetic enumerators which should never get here");
424
Bill Wendling034b94b2012-12-19 07:18:57 +0000425 case Attribute::None: return 0;
426 case Attribute::ZExt: return 1 << 0;
427 case Attribute::SExt: return 1 << 1;
428 case Attribute::NoReturn: return 1 << 2;
429 case Attribute::InReg: return 1 << 3;
430 case Attribute::StructRet: return 1 << 4;
431 case Attribute::NoUnwind: return 1 << 5;
432 case Attribute::NoAlias: return 1 << 6;
433 case Attribute::ByVal: return 1 << 7;
434 case Attribute::Nest: return 1 << 8;
435 case Attribute::ReadNone: return 1 << 9;
436 case Attribute::ReadOnly: return 1 << 10;
437 case Attribute::NoInline: return 1 << 11;
438 case Attribute::AlwaysInline: return 1 << 12;
439 case Attribute::OptimizeForSize: return 1 << 13;
440 case Attribute::StackProtect: return 1 << 14;
441 case Attribute::StackProtectReq: return 1 << 15;
442 case Attribute::Alignment: return 31 << 16;
443 case Attribute::NoCapture: return 1 << 21;
444 case Attribute::NoRedZone: return 1 << 22;
445 case Attribute::NoImplicitFloat: return 1 << 23;
446 case Attribute::Naked: return 1 << 24;
447 case Attribute::InlineHint: return 1 << 25;
448 case Attribute::StackAlignment: return 7 << 26;
449 case Attribute::ReturnsTwice: return 1 << 29;
450 case Attribute::UWTable: return 1 << 30;
451 case Attribute::NonLazyBind: return 1U << 31;
452 case Attribute::AddressSafety: return 1ULL << 32;
453 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000454 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000455 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000456 }
457 llvm_unreachable("Unsupported attribute type");
458}
459
Bill Wendling60507d52013-01-04 20:54:35 +0000460bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000461 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000462}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000463
Bill Wendlingf6670722012-12-20 01:36:59 +0000464bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000465 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000466}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000467
Bill Wendlingf6670722012-12-20 01:36:59 +0000468uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000469 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000470 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000471}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000472
Bill Wendlingf6670722012-12-20 01:36:59 +0000473uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000474 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000475 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000476}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000477
Bill Wendling8456efb2013-01-09 00:32:55 +0000478void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
479 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000480 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000481#if 0
482 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000483 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
484 I != E; ++I)
485 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000486#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000487}
488
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000489//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000490// AttributeSetNode Definition
491//===----------------------------------------------------------------------===//
492
493AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
494 ArrayRef<Attribute> Attrs) {
495 if (Attrs.empty())
496 return 0;
497
498 // Otherwise, build a key to look up the existing attributes.
499 LLVMContextImpl *pImpl = C.pImpl;
500 FoldingSetNodeID ID;
501
502 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
503 std::sort(SortedAttrs.begin(), SortedAttrs.end());
504
505 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
506 E = SortedAttrs.end(); I != E; ++I)
507 I->Profile(ID);
508
509 void *InsertPoint;
510 AttributeSetNode *PA =
511 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
512
513 // If we didn't find any existing attributes of the same shape then create a
514 // new one and insert it.
515 if (!PA) {
516 PA = new AttributeSetNode(SortedAttrs);
517 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
518 }
519
520 // Return the AttributesListNode that we found or created.
521 return PA;
522}
523
524//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000525// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000526//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000527
Bill Wendling6a325cc2013-01-27 12:50:02 +0000528AttributeSetImpl::
529AttributeSetImpl(LLVMContext &C,
530 ArrayRef<AttributeWithIndex> attrs)
531 : Context(C), AttrList(attrs.begin(), attrs.end()) {
532 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
533 const AttributeWithIndex &AWI = attrs[I];
534 uint64_t Mask = AWI.Attrs.Raw();
535 SmallVector<Attribute, 8> Attrs;
536
537 for (Attribute::AttrKind II = Attribute::None;
538 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
539 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
540 AttrBuilder B;
541
542 if (II == Attribute::Alignment)
543 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
544 else if (II == Attribute::StackAlignment)
545 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
546 else
547 B.addAttribute(II);
548
549 Attrs.push_back(Attribute::get(C, B));
550 }
551 }
552
553 AttrNodes.push_back(std::make_pair(AWI.Index,
554 AttributeSetNode::get(C, Attrs)));
555 }
Bill Wendling6a325cc2013-01-27 12:50:02 +0000556
Bill Wendlinga5372d22013-01-27 21:20:06 +0000557 assert(AttrNodes.size() == AttrList.size() &&
558 "Number of attributes is different between lists!");
559#ifndef NDEBUG
560 for (unsigned I = 0, E = AttrNodes.size(); I != E; ++I)
561 assert((I == 0 || AttrNodes[I - 1].first < AttrNodes[I].first) &&
562 "Attributes not in ascending order!");
563#endif
Bill Wendling6a325cc2013-01-27 12:50:02 +0000564}
565
566//===----------------------------------------------------------------------===//
567// AttributeSet Method Implementations
568//===----------------------------------------------------------------------===//
569
Bill Wendling28d65722013-01-23 06:14:59 +0000570AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
571 // FIXME: Remove.
572 return AttrList && hasAttributes(Idx) ?
573 AttributeSet::get(AttrList->getContext(),
574 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
575 AttributeSet();
576}
577
Bill Wendling3fc4b962013-01-21 22:44:49 +0000578AttributeSet AttributeSet::getRetAttributes() const {
579 // FIXME: Remove.
580 return AttrList && hasAttributes(ReturnIndex) ?
581 AttributeSet::get(AttrList->getContext(),
582 AttributeWithIndex::get(ReturnIndex,
583 getAttributes(ReturnIndex))) :
584 AttributeSet();
585}
586
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000587AttributeSet AttributeSet::getFnAttributes() const {
588 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000589 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000590 AttributeSet::get(AttrList->getContext(),
591 AttributeWithIndex::get(FunctionIndex,
592 getAttributes(FunctionIndex))) :
593 AttributeSet();
594}
595
Bill Wendling99faa3b2012-12-07 23:16:57 +0000596AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000597 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000598 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000599 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000600 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000601
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000602#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000603 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000604 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000605 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000606 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000607 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000608 }
609#endif
Bill Wendling77898682012-10-16 06:01:44 +0000610
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000611 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000612 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000613 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000614 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000615
Bill Wendling0976e002012-11-20 05:09:20 +0000616 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000617 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000618
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000619 // If we didn't find any existing attributes of the same shape then
620 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000621 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000622 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000623 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000624 }
Bill Wendling77898682012-10-16 06:01:44 +0000625
Devang Patel05988662008-09-25 21:00:45 +0000626 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000627 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000628}
629
Bill Wendling1bbd6442013-01-05 01:36:54 +0000630AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000631 // FIXME: This should be implemented as a loop that creates the
632 // AttributeWithIndexes that then are used to create the AttributeSet.
633 if (!B.hasAttributes())
634 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000635 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000636}
637
Bill Wendling28d65722013-01-23 06:14:59 +0000638AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000639 ArrayRef<Attribute::AttrKind> Kind) {
640 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
641 // replaced by an object that holds multiple Attribute::AttrKinds.
642 AttrBuilder B;
643 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
644 E = Kind.end(); I != E; ++I)
645 B.addAttribute(*I);
646 return get(C, Idx, B);
Bill Wendling28d65722013-01-23 06:14:59 +0000647}
648
Bill Wendling8e47daf2013-01-25 23:09:36 +0000649AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
650 SmallVector<AttributeWithIndex, 8> AttrList;
651 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
652 I != E; ++I) {
653 AttributeSet AS = *I;
654 if (!AS.AttrList) continue;
655 AttrList.append(AS.AttrList->AttrList.begin(), AS.AttrList->AttrList.end());
656 }
657
658 return get(C, AttrList);
659}
660
Bill Wendling99faa3b2012-12-07 23:16:57 +0000661const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000662 AttrList = RHS.AttrList;
663 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000664}
665
Bill Wendling77898682012-10-16 06:01:44 +0000666/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000667/// This is the number of arguments that have an attribute set on them
668/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000669unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000670 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000671}
672
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000673unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
674 assert(AttrList && Slot < AttrList->getNumAttributes() &&
675 "Slot # out of range!");
676 return AttrList->getSlotIndex(Slot);
677}
678
Bill Wendling8e47daf2013-01-25 23:09:36 +0000679AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
680 assert(AttrList && Slot < AttrList->getNumAttributes() &&
681 "Slot # out of range!");
682 return AttrList->getSlotAttributes(Slot);
683}
684
Bill Wendling831737d2012-12-30 10:32:01 +0000685bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
686 return getAttributes(Index).hasAttribute(Kind);
687}
688
689bool AttributeSet::hasAttributes(unsigned Index) const {
690 return getAttributes(Index).hasAttributes();
691}
692
693std::string AttributeSet::getAsString(unsigned Index) const {
694 return getAttributes(Index).getAsString();
695}
696
Bill Wendling956f1342013-01-18 21:11:39 +0000697unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
698 return getAttributes(Idx).getAlignment();
699}
700
Bill Wendling831737d2012-12-30 10:32:01 +0000701unsigned AttributeSet::getStackAlignment(unsigned Index) const {
702 return getAttributes(Index).getStackAlignment();
703}
704
Bill Wendling1db9b692013-01-09 23:36:50 +0000705uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000706 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000707 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000708}
709
Bill Wendling77898682012-10-16 06:01:44 +0000710/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000711Attribute AttributeSet::getAttributes(unsigned Idx) const {
712 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000713
Bill Wendling60507d52013-01-04 20:54:35 +0000714 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000715 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
716 if (Attrs[i].Index == Idx)
717 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000718
Bill Wendling034b94b2012-12-19 07:18:57 +0000719 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000720}
721
722/// hasAttrSomewhere - Return true if the specified attribute is set for at
723/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000724bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000725 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000726
Bill Wendling60507d52013-01-04 20:54:35 +0000727 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000728 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000729 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000730 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000731
Chris Lattner58d74912008-03-12 17:45:29 +0000732 return false;
733}
734
Bill Wendlingdefaca02013-01-22 21:15:51 +0000735AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
736 Attribute::AttrKind Attr) const {
737 return addAttr(C, Idx, Attribute::get(C, Attr));
738}
739
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000740AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
741 AttributeSet Attrs) const {
742 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000743}
744
Bill Wendling99faa3b2012-12-07 23:16:57 +0000745AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000746 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000747 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000748#ifndef NDEBUG
749 // FIXME it is not obvious how this should work for alignment.
750 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000751 unsigned OldAlign = OldAttrs.getAlignment();
752 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000753 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000754 "Attempt to change alignment!");
755#endif
Bill Wendling77898682012-10-16 06:01:44 +0000756
Bill Wendling702cc912012-10-15 20:35:56 +0000757 AttrBuilder NewAttrs =
758 AttrBuilder(OldAttrs).addAttributes(Attrs);
759 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000760 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000761
Devang Patel05988662008-09-25 21:00:45 +0000762 SmallVector<AttributeWithIndex, 8> NewAttrList;
763 if (AttrList == 0)
764 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000765 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000766 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000767 unsigned i = 0, e = OldAttrList.size();
768 // Copy attributes for arguments before this one.
769 for (; i != e && OldAttrList[i].Index < Idx; ++i)
770 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000771
Chris Lattner58d74912008-03-12 17:45:29 +0000772 // If there are attributes already at this index, merge them in.
773 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000774 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000775 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000776 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000777 ++i;
778 }
Bill Wendling77898682012-10-16 06:01:44 +0000779
Devang Patel05988662008-09-25 21:00:45 +0000780 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000781
Chris Lattner58d74912008-03-12 17:45:29 +0000782 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000783 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000784 OldAttrList.begin()+i, OldAttrList.end());
785 }
Bill Wendling77898682012-10-16 06:01:44 +0000786
Bill Wendling0976e002012-11-20 05:09:20 +0000787 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000788}
789
Bill Wendling8246df62013-01-23 00:45:55 +0000790AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
791 Attribute::AttrKind Attr) const {
792 return removeAttr(C, Idx, Attribute::get(C, Attr));
793}
794
795AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
796 AttributeSet Attrs) const {
797 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
798}
799
Bill Wendling99faa3b2012-12-07 23:16:57 +0000800AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000801 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000802#ifndef NDEBUG
803 // FIXME it is not obvious how this should work for alignment.
804 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000805 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000806 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000807#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000808 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000809
Bill Wendling034b94b2012-12-19 07:18:57 +0000810 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000811 AttrBuilder NewAttrs =
812 AttrBuilder(OldAttrs).removeAttributes(Attrs);
813 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000814 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000815
Devang Patel05988662008-09-25 21:00:45 +0000816 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000817 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000818 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000819
Chris Lattner58d74912008-03-12 17:45:29 +0000820 // Copy attributes for arguments before this one.
821 for (; i != e && OldAttrList[i].Index < Idx; ++i)
822 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000823
Chris Lattner58d74912008-03-12 17:45:29 +0000824 // If there are attributes already at this index, merge them in.
825 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000826 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000827 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000828 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000829 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000830 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000831
Chris Lattner58d74912008-03-12 17:45:29 +0000832 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000833 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000834 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000835
Bill Wendling0976e002012-11-20 05:09:20 +0000836 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000837}
838
Bill Wendling99faa3b2012-12-07 23:16:57 +0000839void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000840 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000841 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling85875642013-01-25 21:46:52 +0000842 unsigned Index = getSlotIndex(i);
843 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000844 }
Bill Wendling77898682012-10-16 06:01:44 +0000845
David Greeneef1894e2010-01-05 01:29:58 +0000846 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000847}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000848
849//===----------------------------------------------------------------------===//
850// AttributeFuncs Function Defintions
851//===----------------------------------------------------------------------===//
852
853Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
854 AttrBuilder Incompatible;
855
856 if (!Ty->isIntegerTy())
857 // Attribute that only apply to integers.
858 Incompatible.addAttribute(Attribute::SExt)
859 .addAttribute(Attribute::ZExt);
860
861 if (!Ty->isPointerTy())
862 // Attribute that only apply to pointers.
863 Incompatible.addAttribute(Attribute::ByVal)
864 .addAttribute(Attribute::Nest)
865 .addAttribute(Attribute::NoAlias)
866 .addAttribute(Attribute::NoCapture)
867 .addAttribute(Attribute::StructRet);
868
869 return Attribute::get(Ty->getContext(), Incompatible);
870}
871
872/// encodeLLVMAttributesForBitcode - This returns an integer containing an
873/// encoding of all the LLVM attributes found in the given attribute bitset.
874/// Any change to this encoding is a breaking change to bitcode compatibility.
875uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
876 unsigned Index) {
877 // FIXME: It doesn't make sense to store the alignment information as an
878 // expanded out value, we should store it as a log2 value. However, we can't
879 // just change that here without breaking bitcode compatibility. If this ever
880 // becomes a problem in practice, we should introduce new tag numbers in the
881 // bitcode file and have those tags use a more efficiently encoded alignment
882 // field.
883
884 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
885 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
886 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
887 if (Attrs.hasAttribute(Index, Attribute::Alignment))
888 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
889 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
890 return EncodedAttrs;
891}
892
893/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
894/// the LLVM attributes that have been decoded from the given integer. This
895/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
896Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
897 uint64_t EncodedAttrs){
898 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
899 // the bits above 31 down by 11 bits.
900 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
901 assert((!Alignment || isPowerOf2_32(Alignment)) &&
902 "Alignment must be a power of two.");
903
904 AttrBuilder B(EncodedAttrs & 0xffff);
905 if (Alignment)
906 B.addAlignmentAttr(Alignment);
907 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
908 return Attribute::get(C, B);
909}
910