blob: 780da00ac01689b3d68b8e527eac90859aced8c2 [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 }
556}
557
558AttributeSetImpl::
559AttributeSetImpl(LLVMContext &C,
560 ArrayRef<std::pair<uint64_t, AttributeSetNode*> > attrs)
561 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {
562}
563
564//===----------------------------------------------------------------------===//
565// AttributeSet Method Implementations
566//===----------------------------------------------------------------------===//
567
Bill Wendling28d65722013-01-23 06:14:59 +0000568AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
569 // FIXME: Remove.
570 return AttrList && hasAttributes(Idx) ?
571 AttributeSet::get(AttrList->getContext(),
572 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
573 AttributeSet();
574}
575
Bill Wendling3fc4b962013-01-21 22:44:49 +0000576AttributeSet AttributeSet::getRetAttributes() const {
577 // FIXME: Remove.
578 return AttrList && hasAttributes(ReturnIndex) ?
579 AttributeSet::get(AttrList->getContext(),
580 AttributeWithIndex::get(ReturnIndex,
581 getAttributes(ReturnIndex))) :
582 AttributeSet();
583}
584
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000585AttributeSet AttributeSet::getFnAttributes() const {
586 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000587 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000588 AttributeSet::get(AttrList->getContext(),
589 AttributeWithIndex::get(FunctionIndex,
590 getAttributes(FunctionIndex))) :
591 AttributeSet();
592}
593
Bill Wendling99faa3b2012-12-07 23:16:57 +0000594AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000595 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000596 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000597 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000598 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000599
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000600#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000601 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000602 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000603 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000604 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000605 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000606 }
607#endif
Bill Wendling77898682012-10-16 06:01:44 +0000608
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000609 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000610 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000611 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000612 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000613
Bill Wendling0976e002012-11-20 05:09:20 +0000614 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000615 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000616
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000617 // If we didn't find any existing attributes of the same shape then
618 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000619 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000620 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000621 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000622 }
Bill Wendling77898682012-10-16 06:01:44 +0000623
Devang Patel05988662008-09-25 21:00:45 +0000624 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000625 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000626}
627
Bill Wendling1bbd6442013-01-05 01:36:54 +0000628AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000629 // FIXME: This should be implemented as a loop that creates the
630 // AttributeWithIndexes that then are used to create the AttributeSet.
631 if (!B.hasAttributes())
632 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000633 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000634}
635
Bill Wendling28d65722013-01-23 06:14:59 +0000636AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000637 ArrayRef<Attribute::AttrKind> Kind) {
638 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
639 // replaced by an object that holds multiple Attribute::AttrKinds.
640 AttrBuilder B;
641 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
642 E = Kind.end(); I != E; ++I)
643 B.addAttribute(*I);
644 return get(C, Idx, B);
Bill Wendling28d65722013-01-23 06:14:59 +0000645}
646
Bill Wendling8e47daf2013-01-25 23:09:36 +0000647AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
648 SmallVector<AttributeWithIndex, 8> AttrList;
649 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
650 I != E; ++I) {
651 AttributeSet AS = *I;
652 if (!AS.AttrList) continue;
653 AttrList.append(AS.AttrList->AttrList.begin(), AS.AttrList->AttrList.end());
654 }
655
656 return get(C, AttrList);
657}
658
Bill Wendling99faa3b2012-12-07 23:16:57 +0000659const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000660 AttrList = RHS.AttrList;
661 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000662}
663
Bill Wendling77898682012-10-16 06:01:44 +0000664/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000665/// This is the number of arguments that have an attribute set on them
666/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000667unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000668 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000669}
670
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000671unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
672 assert(AttrList && Slot < AttrList->getNumAttributes() &&
673 "Slot # out of range!");
674 return AttrList->getSlotIndex(Slot);
675}
676
Bill Wendling8e47daf2013-01-25 23:09:36 +0000677AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
678 assert(AttrList && Slot < AttrList->getNumAttributes() &&
679 "Slot # out of range!");
680 return AttrList->getSlotAttributes(Slot);
681}
682
Bill Wendling831737d2012-12-30 10:32:01 +0000683bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
684 return getAttributes(Index).hasAttribute(Kind);
685}
686
687bool AttributeSet::hasAttributes(unsigned Index) const {
688 return getAttributes(Index).hasAttributes();
689}
690
691std::string AttributeSet::getAsString(unsigned Index) const {
692 return getAttributes(Index).getAsString();
693}
694
Bill Wendling956f1342013-01-18 21:11:39 +0000695unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
696 return getAttributes(Idx).getAlignment();
697}
698
Bill Wendling831737d2012-12-30 10:32:01 +0000699unsigned AttributeSet::getStackAlignment(unsigned Index) const {
700 return getAttributes(Index).getStackAlignment();
701}
702
Bill Wendling1db9b692013-01-09 23:36:50 +0000703uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000704 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000705 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000706}
707
Bill Wendling77898682012-10-16 06:01:44 +0000708/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000709Attribute AttributeSet::getAttributes(unsigned Idx) const {
710 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000711
Bill Wendling60507d52013-01-04 20:54:35 +0000712 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000713 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
714 if (Attrs[i].Index == Idx)
715 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000716
Bill Wendling034b94b2012-12-19 07:18:57 +0000717 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000718}
719
720/// hasAttrSomewhere - Return true if the specified attribute is set for at
721/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000722bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000723 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000724
Bill Wendling60507d52013-01-04 20:54:35 +0000725 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000726 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000727 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000728 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000729
Chris Lattner58d74912008-03-12 17:45:29 +0000730 return false;
731}
732
Bill Wendlingdefaca02013-01-22 21:15:51 +0000733AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
734 Attribute::AttrKind Attr) const {
735 return addAttr(C, Idx, Attribute::get(C, Attr));
736}
737
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000738AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
739 AttributeSet Attrs) const {
740 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000741}
742
Bill Wendling99faa3b2012-12-07 23:16:57 +0000743AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000744 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000745 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000746#ifndef NDEBUG
747 // FIXME it is not obvious how this should work for alignment.
748 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000749 unsigned OldAlign = OldAttrs.getAlignment();
750 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000751 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000752 "Attempt to change alignment!");
753#endif
Bill Wendling77898682012-10-16 06:01:44 +0000754
Bill Wendling702cc912012-10-15 20:35:56 +0000755 AttrBuilder NewAttrs =
756 AttrBuilder(OldAttrs).addAttributes(Attrs);
757 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000758 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000759
Devang Patel05988662008-09-25 21:00:45 +0000760 SmallVector<AttributeWithIndex, 8> NewAttrList;
761 if (AttrList == 0)
762 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000763 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000764 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000765 unsigned i = 0, e = OldAttrList.size();
766 // Copy attributes for arguments before this one.
767 for (; i != e && OldAttrList[i].Index < Idx; ++i)
768 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000769
Chris Lattner58d74912008-03-12 17:45:29 +0000770 // If there are attributes already at this index, merge them in.
771 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000772 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000773 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000774 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000775 ++i;
776 }
Bill Wendling77898682012-10-16 06:01:44 +0000777
Devang Patel05988662008-09-25 21:00:45 +0000778 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000779
Chris Lattner58d74912008-03-12 17:45:29 +0000780 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000781 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000782 OldAttrList.begin()+i, OldAttrList.end());
783 }
Bill Wendling77898682012-10-16 06:01:44 +0000784
Bill Wendling0976e002012-11-20 05:09:20 +0000785 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000786}
787
Bill Wendling8246df62013-01-23 00:45:55 +0000788AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
789 Attribute::AttrKind Attr) const {
790 return removeAttr(C, Idx, Attribute::get(C, Attr));
791}
792
793AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
794 AttributeSet Attrs) const {
795 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
796}
797
Bill Wendling99faa3b2012-12-07 23:16:57 +0000798AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000799 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000800#ifndef NDEBUG
801 // FIXME it is not obvious how this should work for alignment.
802 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000803 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000804 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000805#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000806 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000807
Bill Wendling034b94b2012-12-19 07:18:57 +0000808 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000809 AttrBuilder NewAttrs =
810 AttrBuilder(OldAttrs).removeAttributes(Attrs);
811 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000812 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000813
Devang Patel05988662008-09-25 21:00:45 +0000814 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000815 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000816 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000817
Chris Lattner58d74912008-03-12 17:45:29 +0000818 // Copy attributes for arguments before this one.
819 for (; i != e && OldAttrList[i].Index < Idx; ++i)
820 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000821
Chris Lattner58d74912008-03-12 17:45:29 +0000822 // If there are attributes already at this index, merge them in.
823 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000824 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000825 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000826 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000827 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000828 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000829
Chris Lattner58d74912008-03-12 17:45:29 +0000830 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000831 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000832 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000833
Bill Wendling0976e002012-11-20 05:09:20 +0000834 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000835}
836
Bill Wendling99faa3b2012-12-07 23:16:57 +0000837void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000838 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000839 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling85875642013-01-25 21:46:52 +0000840 unsigned Index = getSlotIndex(i);
841 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000842 }
Bill Wendling77898682012-10-16 06:01:44 +0000843
David Greeneef1894e2010-01-05 01:29:58 +0000844 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000845}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000846
847//===----------------------------------------------------------------------===//
848// AttributeFuncs Function Defintions
849//===----------------------------------------------------------------------===//
850
851Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
852 AttrBuilder Incompatible;
853
854 if (!Ty->isIntegerTy())
855 // Attribute that only apply to integers.
856 Incompatible.addAttribute(Attribute::SExt)
857 .addAttribute(Attribute::ZExt);
858
859 if (!Ty->isPointerTy())
860 // Attribute that only apply to pointers.
861 Incompatible.addAttribute(Attribute::ByVal)
862 .addAttribute(Attribute::Nest)
863 .addAttribute(Attribute::NoAlias)
864 .addAttribute(Attribute::NoCapture)
865 .addAttribute(Attribute::StructRet);
866
867 return Attribute::get(Ty->getContext(), Incompatible);
868}
869
870/// encodeLLVMAttributesForBitcode - This returns an integer containing an
871/// encoding of all the LLVM attributes found in the given attribute bitset.
872/// Any change to this encoding is a breaking change to bitcode compatibility.
873uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
874 unsigned Index) {
875 // FIXME: It doesn't make sense to store the alignment information as an
876 // expanded out value, we should store it as a log2 value. However, we can't
877 // just change that here without breaking bitcode compatibility. If this ever
878 // becomes a problem in practice, we should introduce new tag numbers in the
879 // bitcode file and have those tags use a more efficiently encoded alignment
880 // field.
881
882 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
883 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
884 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
885 if (Attrs.hasAttribute(Index, Attribute::Alignment))
886 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
887 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
888 return EncodedAttrs;
889}
890
891/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
892/// the LLVM attributes that have been decoded from the given integer. This
893/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
894Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
895 uint64_t EncodedAttrs){
896 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
897 // the bits above 31 down by 11 bits.
898 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
899 assert((!Alignment || isPowerOf2_32(Alignment)) &&
900 "Alignment must be a power of two.");
901
902 AttrBuilder B(EncodedAttrs & 0xffff);
903 if (Alignment)
904 B.addAlignmentAttr(Alignment);
905 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
906 return Attribute::get(C, B);
907}
908