blob: 937514a0a4a8af3ee29759ec6d2108bda1fc5053 [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 Wendling09dda442013-01-27 09:55:44 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
34 AttrBuilder B(Kind);
Bill Wendling034b94b2012-12-19 07:18:57 +000035 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000036}
Bill Wendling8e635db2012-10-08 21:47:17 +000037
Bill Wendling034b94b2012-12-19 07:18:57 +000038Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000040 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000041 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000042
43 // Otherwise, build a key to look up the existing attributes.
44 LLVMContextImpl *pImpl = Context.pImpl;
45 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000046 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000047
48 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000049 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 if (!PA) {
52 // If we didn't find any existing attributes of the same shape then create a
53 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000054 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000055 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
56 }
57
58 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000059 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000060}
61
Bill Wendling629fb822012-12-22 00:37:52 +000062bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000063 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000064}
65
Bill Wendling034b94b2012-12-19 07:18:57 +000066bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000067 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000068}
69
Bill Wendlinge66f3d32012-10-05 06:44:41 +000070/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000071unsigned Attribute::getAlignment() const {
72 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000073 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000074 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000075}
76
77/// This returns the stack alignment field of an attribute as a byte alignment
78/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000079unsigned Attribute::getStackAlignment() const {
80 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000081 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000082 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000083}
84
Bill Wendling92e287f2012-12-31 11:51:54 +000085bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000086 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000087}
Bill Wendling92e287f2012-12-31 11:51:54 +000088bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000089 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +000090}
91
Bill Wendling3467e302013-01-24 00:06:56 +000092bool Attribute::operator<(Attribute A) const {
93 if (!pImpl && !A.pImpl) return false;
94 if (!pImpl) return true;
95 if (!A.pImpl) return false;
96 return *pImpl < *A.pImpl;
97}
98
Bill Wendling1db9b692013-01-09 23:36:50 +000099uint64_t Attribute::Raw() const {
100 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000101}
102
Bill Wendling034b94b2012-12-19 07:18:57 +0000103std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000104 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000105 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000106 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000107 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000108 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000109 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000110 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000111 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000112 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000113 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000114 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000116 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000118 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000120 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000122 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000124 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000126 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000128 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000130 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000132 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000134 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000136 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000138 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000140 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000142 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000144 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000145 if (hasAttribute(Attribute::StackProtectStrong))
146 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000148 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000150 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000152 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000154 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000156 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000158 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000159 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000160 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000161 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000162 Result += ") ";
163 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000165 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000166 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000167 Result += " ";
168 }
James Molloy67ae1352012-12-20 16:04:27 +0000169 if (hasAttribute(Attribute::NoDuplicate))
170 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000171 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000172 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000173 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000174 return Result;
175}
176
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000177//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000178// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000179//===----------------------------------------------------------------------===//
180
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000181AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
182 : Alignment(0), StackAlignment(0) {
183 AttributeSetImpl *pImpl = AS.AttrList;
184 if (!pImpl) return;
185
186 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
187 const AttributeWithIndex *AWI = 0;
188 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
189 if (AttrList[I].Index == Idx) {
190 AWI = &AttrList[I];
191 break;
192 }
193
Bill Wendlingaec71062013-01-18 21:56:07 +0000194 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000195
Bill Wendling956f1342013-01-18 21:11:39 +0000196 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000197
Bill Wendling956f1342013-01-18 21:11:39 +0000198 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
199 I = Attribute::AttrKind(I + 1)) {
200 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
201 Attrs.insert(I);
202
203 if (I == Attribute::Alignment)
204 Alignment = 1ULL << ((A >> 16) - 1);
205 else if (I == Attribute::StackAlignment)
206 StackAlignment = 1ULL << ((A >> 26)-1);
207 }
208 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000209}
210
Bill Wendling03198882013-01-04 23:27:34 +0000211void AttrBuilder::clear() {
212 Attrs.clear();
213 Alignment = StackAlignment = 0;
214}
215
216AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
217 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000218 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000219}
220
Bill Wendling03198882013-01-04 23:27:34 +0000221AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
222 Attrs.erase(Val);
223 if (Val == Attribute::Alignment)
224 Alignment = 0;
225 else if (Val == Attribute::StackAlignment)
226 StackAlignment = 0;
227
Bill Wendlinga19a5302012-10-14 04:10:01 +0000228 return *this;
229}
230
Bill Wendling702cc912012-10-15 20:35:56 +0000231AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000232 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000233
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000234 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
235 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000236
237 Attrs.insert(Attribute::Alignment);
238 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000239 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000240}
241
Bill Wendling03198882013-01-04 23:27:34 +0000242AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
243 // Default alignment, allow the target to define how to align it.
244 if (Align == 0) return *this;
245
246 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
247 assert(Align <= 0x100 && "Alignment too large.");
248
249 Attrs.insert(Attribute::StackAlignment);
250 StackAlignment = Align;
251 return *this;
252}
253
254AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
255 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
256 I = Attribute::AttrKind(I + 1)) {
257 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
258 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000259
Bill Wendling03198882013-01-04 23:27:34 +0000260 if (I == Attribute::Alignment)
261 Alignment = 1ULL << ((A >> 16) - 1);
262 else if (I == Attribute::StackAlignment)
263 StackAlignment = 1ULL << ((A >> 26)-1);
264 }
265 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000266
Bill Wendling3a106e62012-10-09 19:01:18 +0000267 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000268}
269
Bill Wendlingdefaca02013-01-22 21:15:51 +0000270AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
271 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000272
273 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000274 I = Attribute::AttrKind(I + 1))
275 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000276 Attrs.insert(I);
277
Bill Wendlingdefaca02013-01-22 21:15:51 +0000278 if (Attr.getAlignment())
279 Alignment = Attr.getAlignment();
280 if (Attr.getStackAlignment())
281 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000282 return *this;
283}
284
Bill Wendling034b94b2012-12-19 07:18:57 +0000285AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000286 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000287
288 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
289 I = Attribute::AttrKind(I + 1)) {
290 if (Mask & AttributeImpl::getAttrMask(I)) {
291 Attrs.erase(I);
292
293 if (I == Attribute::Alignment)
294 Alignment = 0;
295 else if (I == Attribute::StackAlignment)
296 StackAlignment = 0;
297 }
298 }
299
Bill Wendling5886b7b2012-10-14 06:39:53 +0000300 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000301}
302
Bill Wendling22bd6412013-01-03 01:54:39 +0000303bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000304 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000305}
306
Bill Wendling702cc912012-10-15 20:35:56 +0000307bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000308 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000309}
Bill Wendling60507d52013-01-04 20:54:35 +0000310
Bill Wendling034b94b2012-12-19 07:18:57 +0000311bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000312 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000313}
Bill Wendling60507d52013-01-04 20:54:35 +0000314
Bill Wendling702cc912012-10-15 20:35:56 +0000315bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000316 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000317}
318
Bill Wendling1db9b692013-01-09 23:36:50 +0000319uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000320 uint64_t Mask = 0;
321
322 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
323 E = Attrs.end(); I != E; ++I) {
324 Attribute::AttrKind Kind = *I;
325
326 if (Kind == Attribute::Alignment)
327 Mask |= (Log2_32(Alignment) + 1) << 16;
328 else if (Kind == Attribute::StackAlignment)
329 Mask |= (Log2_32(StackAlignment) + 1) << 26;
330 else
331 Mask |= AttributeImpl::getAttrMask(Kind);
332 }
333
334 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000335}
336
Bill Wendling03198882013-01-04 23:27:34 +0000337bool AttrBuilder::operator==(const AttrBuilder &B) {
338 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
339 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
340 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000341}
342
Chris Lattner58d74912008-03-12 17:45:29 +0000343//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000344// AttributeImpl Definition
345//===----------------------------------------------------------------------===//
346
Bill Wendling1bbd6442013-01-05 01:36:54 +0000347AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
348 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000349 Data = ConstantInt::get(Type::getInt64Ty(C), data);
350}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000351AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
352 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000353 Data = ConstantInt::get(Type::getInt64Ty(C), data);
354}
355AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000356 ArrayRef<Constant*> values)
357 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000358 Data = ConstantInt::get(Type::getInt64Ty(C), data);
359 Vals.reserve(values.size());
360 Vals.append(values.begin(), values.end());
361}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000362AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
363 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000364 Data = ConstantDataArray::getString(C, data);
365}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000366
Bill Wendling60507d52013-01-04 20:54:35 +0000367bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000368 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
369 return CI->getZExtValue() == Kind;
370 return false;
371}
Bill Wendling60507d52013-01-04 20:54:35 +0000372bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
373 return !(*this == Kind);
374}
Bill Wendling529ec712012-12-30 01:38:39 +0000375
Bill Wendling60507d52013-01-04 20:54:35 +0000376bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000377 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
378 if (CDA->isString())
379 return CDA->getAsString() == Kind;
380 return false;
381}
Bill Wendling3467e302013-01-24 00:06:56 +0000382
Bill Wendling60507d52013-01-04 20:54:35 +0000383bool AttributeImpl::operator!=(StringRef Kind) const {
384 return !(*this == Kind);
385}
Bill Wendling529ec712012-12-30 01:38:39 +0000386
Bill Wendling3467e302013-01-24 00:06:56 +0000387bool AttributeImpl::operator<(const AttributeImpl &AI) const {
388 if (!Data && !AI.Data) return false;
389 if (!Data && AI.Data) return true;
390 if (Data && !AI.Data) return false;
391
392 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
393 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
394
395 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
396 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
397
398 if (ThisCI && ThatCI)
399 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
400
401 if (ThisCI && ThatCDA)
402 return true;
403
404 if (ThisCDA && ThatCI)
405 return false;
406
407 return ThisCDA->getAsString() < ThatCDA->getAsString();
408}
409
Bill Wendling1db9b692013-01-09 23:36:50 +0000410uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000411 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000412 return cast<ConstantInt>(Data)->getZExtValue();
413}
414
Bill Wendling60507d52013-01-04 20:54:35 +0000415uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000416 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000417 case Attribute::EndAttrKinds:
418 case Attribute::AttrKindEmptyKey:
419 case Attribute::AttrKindTombstoneKey:
420 llvm_unreachable("Synthetic enumerators which should never get here");
421
Bill Wendling034b94b2012-12-19 07:18:57 +0000422 case Attribute::None: return 0;
423 case Attribute::ZExt: return 1 << 0;
424 case Attribute::SExt: return 1 << 1;
425 case Attribute::NoReturn: return 1 << 2;
426 case Attribute::InReg: return 1 << 3;
427 case Attribute::StructRet: return 1 << 4;
428 case Attribute::NoUnwind: return 1 << 5;
429 case Attribute::NoAlias: return 1 << 6;
430 case Attribute::ByVal: return 1 << 7;
431 case Attribute::Nest: return 1 << 8;
432 case Attribute::ReadNone: return 1 << 9;
433 case Attribute::ReadOnly: return 1 << 10;
434 case Attribute::NoInline: return 1 << 11;
435 case Attribute::AlwaysInline: return 1 << 12;
436 case Attribute::OptimizeForSize: return 1 << 13;
437 case Attribute::StackProtect: return 1 << 14;
438 case Attribute::StackProtectReq: return 1 << 15;
439 case Attribute::Alignment: return 31 << 16;
440 case Attribute::NoCapture: return 1 << 21;
441 case Attribute::NoRedZone: return 1 << 22;
442 case Attribute::NoImplicitFloat: return 1 << 23;
443 case Attribute::Naked: return 1 << 24;
444 case Attribute::InlineHint: return 1 << 25;
445 case Attribute::StackAlignment: return 7 << 26;
446 case Attribute::ReturnsTwice: return 1 << 29;
447 case Attribute::UWTable: return 1 << 30;
448 case Attribute::NonLazyBind: return 1U << 31;
449 case Attribute::AddressSafety: return 1ULL << 32;
450 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000451 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000452 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000453 }
454 llvm_unreachable("Unsupported attribute type");
455}
456
Bill Wendling60507d52013-01-04 20:54:35 +0000457bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000458 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000459}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000460
Bill Wendlingf6670722012-12-20 01:36:59 +0000461bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000462 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000463}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000464
Bill Wendlingf6670722012-12-20 01:36:59 +0000465uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000466 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000467 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000468}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000469
Bill Wendlingf6670722012-12-20 01:36:59 +0000470uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000471 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000472 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000473}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000474
Bill Wendling8456efb2013-01-09 00:32:55 +0000475void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
476 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000477 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000478#if 0
479 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000480 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
481 I != E; ++I)
482 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000483#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000484}
485
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000486//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000487// AttributeSetNode Definition
488//===----------------------------------------------------------------------===//
489
490AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
491 ArrayRef<Attribute> Attrs) {
492 if (Attrs.empty())
493 return 0;
494
495 // Otherwise, build a key to look up the existing attributes.
496 LLVMContextImpl *pImpl = C.pImpl;
497 FoldingSetNodeID ID;
498
499 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
500 std::sort(SortedAttrs.begin(), SortedAttrs.end());
501
502 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
503 E = SortedAttrs.end(); I != E; ++I)
504 I->Profile(ID);
505
506 void *InsertPoint;
507 AttributeSetNode *PA =
508 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
509
510 // If we didn't find any existing attributes of the same shape then create a
511 // new one and insert it.
512 if (!PA) {
513 PA = new AttributeSetNode(SortedAttrs);
514 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
515 }
516
517 // Return the AttributesListNode that we found or created.
518 return PA;
519}
520
521//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000522// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000523//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000524
Bill Wendling28d65722013-01-23 06:14:59 +0000525AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
526 // FIXME: Remove.
527 return AttrList && hasAttributes(Idx) ?
528 AttributeSet::get(AttrList->getContext(),
529 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
530 AttributeSet();
531}
532
Bill Wendling3fc4b962013-01-21 22:44:49 +0000533AttributeSet AttributeSet::getRetAttributes() const {
534 // FIXME: Remove.
535 return AttrList && hasAttributes(ReturnIndex) ?
536 AttributeSet::get(AttrList->getContext(),
537 AttributeWithIndex::get(ReturnIndex,
538 getAttributes(ReturnIndex))) :
539 AttributeSet();
540}
541
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000542AttributeSet AttributeSet::getFnAttributes() const {
543 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000544 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000545 AttributeSet::get(AttrList->getContext(),
546 AttributeWithIndex::get(FunctionIndex,
547 getAttributes(FunctionIndex))) :
548 AttributeSet();
549}
550
Bill Wendling99faa3b2012-12-07 23:16:57 +0000551AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000552 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000553 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000554 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000555 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000556
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000557#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000558 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000559 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000560 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000561 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000562 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000563 }
564#endif
Bill Wendling77898682012-10-16 06:01:44 +0000565
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000566 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000567 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000568 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000569 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000570
Bill Wendling0976e002012-11-20 05:09:20 +0000571 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000572 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000573
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000574 // If we didn't find any existing attributes of the same shape then
575 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000576 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000577 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000578 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000579 }
Bill Wendling77898682012-10-16 06:01:44 +0000580
Devang Patel05988662008-09-25 21:00:45 +0000581 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000582 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000583}
584
Bill Wendling1bbd6442013-01-05 01:36:54 +0000585AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000586 // FIXME: This should be implemented as a loop that creates the
587 // AttributeWithIndexes that then are used to create the AttributeSet.
588 if (!B.hasAttributes())
589 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000590 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000591}
592
Bill Wendling28d65722013-01-23 06:14:59 +0000593AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000594 ArrayRef<Attribute::AttrKind> Kind) {
595 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
596 // replaced by an object that holds multiple Attribute::AttrKinds.
597 AttrBuilder B;
598 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
599 E = Kind.end(); I != E; ++I)
600 B.addAttribute(*I);
601 return get(C, Idx, B);
Bill Wendling28d65722013-01-23 06:14:59 +0000602}
603
Bill Wendling8e47daf2013-01-25 23:09:36 +0000604AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
605 SmallVector<AttributeWithIndex, 8> AttrList;
606 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
607 I != E; ++I) {
608 AttributeSet AS = *I;
609 if (!AS.AttrList) continue;
610 AttrList.append(AS.AttrList->AttrList.begin(), AS.AttrList->AttrList.end());
611 }
612
613 return get(C, AttrList);
614}
615
Chris Lattner58d74912008-03-12 17:45:29 +0000616//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000617// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000618//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000619
Bill Wendling99faa3b2012-12-07 23:16:57 +0000620const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000621 AttrList = RHS.AttrList;
622 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000623}
624
Bill Wendling77898682012-10-16 06:01:44 +0000625/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000626/// This is the number of arguments that have an attribute set on them
627/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000628unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000629 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000630}
631
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000632unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
633 assert(AttrList && Slot < AttrList->getNumAttributes() &&
634 "Slot # out of range!");
635 return AttrList->getSlotIndex(Slot);
636}
637
Bill Wendling8e47daf2013-01-25 23:09:36 +0000638AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
639 assert(AttrList && Slot < AttrList->getNumAttributes() &&
640 "Slot # out of range!");
641 return AttrList->getSlotAttributes(Slot);
642}
643
Bill Wendling831737d2012-12-30 10:32:01 +0000644bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
645 return getAttributes(Index).hasAttribute(Kind);
646}
647
648bool AttributeSet::hasAttributes(unsigned Index) const {
649 return getAttributes(Index).hasAttributes();
650}
651
652std::string AttributeSet::getAsString(unsigned Index) const {
653 return getAttributes(Index).getAsString();
654}
655
Bill Wendling956f1342013-01-18 21:11:39 +0000656unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
657 return getAttributes(Idx).getAlignment();
658}
659
Bill Wendling831737d2012-12-30 10:32:01 +0000660unsigned AttributeSet::getStackAlignment(unsigned Index) const {
661 return getAttributes(Index).getStackAlignment();
662}
663
Bill Wendling1db9b692013-01-09 23:36:50 +0000664uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000665 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000666 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000667}
668
Bill Wendling77898682012-10-16 06:01:44 +0000669/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000670Attribute AttributeSet::getAttributes(unsigned Idx) const {
671 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000672
Bill Wendling60507d52013-01-04 20:54:35 +0000673 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000674 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
675 if (Attrs[i].Index == Idx)
676 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000677
Bill Wendling034b94b2012-12-19 07:18:57 +0000678 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000679}
680
681/// hasAttrSomewhere - Return true if the specified attribute is set for at
682/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000683bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000684 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000685
Bill Wendling60507d52013-01-04 20:54:35 +0000686 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000687 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000688 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000689 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000690
Chris Lattner58d74912008-03-12 17:45:29 +0000691 return false;
692}
693
Bill Wendlingdefaca02013-01-22 21:15:51 +0000694AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
695 Attribute::AttrKind Attr) const {
696 return addAttr(C, Idx, Attribute::get(C, Attr));
697}
698
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000699AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
700 AttributeSet Attrs) const {
701 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000702}
703
Bill Wendling99faa3b2012-12-07 23:16:57 +0000704AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000705 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000706 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000707#ifndef NDEBUG
708 // FIXME it is not obvious how this should work for alignment.
709 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000710 unsigned OldAlign = OldAttrs.getAlignment();
711 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000712 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000713 "Attempt to change alignment!");
714#endif
Bill Wendling77898682012-10-16 06:01:44 +0000715
Bill Wendling702cc912012-10-15 20:35:56 +0000716 AttrBuilder NewAttrs =
717 AttrBuilder(OldAttrs).addAttributes(Attrs);
718 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000719 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000720
Devang Patel05988662008-09-25 21:00:45 +0000721 SmallVector<AttributeWithIndex, 8> NewAttrList;
722 if (AttrList == 0)
723 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000724 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000725 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000726 unsigned i = 0, e = OldAttrList.size();
727 // Copy attributes for arguments before this one.
728 for (; i != e && OldAttrList[i].Index < Idx; ++i)
729 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000730
Chris Lattner58d74912008-03-12 17:45:29 +0000731 // If there are attributes already at this index, merge them in.
732 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000733 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000734 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000735 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000736 ++i;
737 }
Bill Wendling77898682012-10-16 06:01:44 +0000738
Devang Patel05988662008-09-25 21:00:45 +0000739 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000740
Chris Lattner58d74912008-03-12 17:45:29 +0000741 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000742 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000743 OldAttrList.begin()+i, OldAttrList.end());
744 }
Bill Wendling77898682012-10-16 06:01:44 +0000745
Bill Wendling0976e002012-11-20 05:09:20 +0000746 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000747}
748
Bill Wendling8246df62013-01-23 00:45:55 +0000749AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
750 Attribute::AttrKind Attr) const {
751 return removeAttr(C, Idx, Attribute::get(C, Attr));
752}
753
754AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
755 AttributeSet Attrs) const {
756 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
757}
758
Bill Wendling99faa3b2012-12-07 23:16:57 +0000759AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000760 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000761#ifndef NDEBUG
762 // FIXME it is not obvious how this should work for alignment.
763 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000764 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000765 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000766#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000767 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000768
Bill Wendling034b94b2012-12-19 07:18:57 +0000769 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000770 AttrBuilder NewAttrs =
771 AttrBuilder(OldAttrs).removeAttributes(Attrs);
772 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000773 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000774
Devang Patel05988662008-09-25 21:00:45 +0000775 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000776 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000777 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000778
Chris Lattner58d74912008-03-12 17:45:29 +0000779 // Copy attributes for arguments before this one.
780 for (; i != e && OldAttrList[i].Index < Idx; ++i)
781 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000782
Chris Lattner58d74912008-03-12 17:45:29 +0000783 // If there are attributes already at this index, merge them in.
784 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000785 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000786 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000787 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000788 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000789 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000790
Chris Lattner58d74912008-03-12 17:45:29 +0000791 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000792 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000793 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000794
Bill Wendling0976e002012-11-20 05:09:20 +0000795 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000796}
797
Bill Wendling99faa3b2012-12-07 23:16:57 +0000798void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000799 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000800 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling85875642013-01-25 21:46:52 +0000801 unsigned Index = getSlotIndex(i);
802 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000803 }
Bill Wendling77898682012-10-16 06:01:44 +0000804
David Greeneef1894e2010-01-05 01:29:58 +0000805 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000806}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000807
808//===----------------------------------------------------------------------===//
809// AttributeFuncs Function Defintions
810//===----------------------------------------------------------------------===//
811
812Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
813 AttrBuilder Incompatible;
814
815 if (!Ty->isIntegerTy())
816 // Attribute that only apply to integers.
817 Incompatible.addAttribute(Attribute::SExt)
818 .addAttribute(Attribute::ZExt);
819
820 if (!Ty->isPointerTy())
821 // Attribute that only apply to pointers.
822 Incompatible.addAttribute(Attribute::ByVal)
823 .addAttribute(Attribute::Nest)
824 .addAttribute(Attribute::NoAlias)
825 .addAttribute(Attribute::NoCapture)
826 .addAttribute(Attribute::StructRet);
827
828 return Attribute::get(Ty->getContext(), Incompatible);
829}
830
831/// encodeLLVMAttributesForBitcode - This returns an integer containing an
832/// encoding of all the LLVM attributes found in the given attribute bitset.
833/// Any change to this encoding is a breaking change to bitcode compatibility.
834uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
835 unsigned Index) {
836 // FIXME: It doesn't make sense to store the alignment information as an
837 // expanded out value, we should store it as a log2 value. However, we can't
838 // just change that here without breaking bitcode compatibility. If this ever
839 // becomes a problem in practice, we should introduce new tag numbers in the
840 // bitcode file and have those tags use a more efficiently encoded alignment
841 // field.
842
843 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
844 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
845 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
846 if (Attrs.hasAttribute(Index, Attribute::Alignment))
847 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
848 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
849 return EncodedAttrs;
850}
851
852/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
853/// the LLVM attributes that have been decoded from the given integer. This
854/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
855Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
856 uint64_t EncodedAttrs){
857 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
858 // the bits above 31 down by 11 bits.
859 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
860 assert((!Alignment || isPowerOf2_32(Alignment)) &&
861 "Alignment must be a power of two.");
862
863 AttrBuilder B(EncodedAttrs & 0xffff);
864 if (Alignment)
865 B.addAlignmentAttr(Alignment);
866 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
867 return Attribute::get(C, B);
868}
869