blob: 3f0038b6fbf6aabaa45b5a6825e661a408ec9815 [file] [log] [blame]
Bill Wendling034b94b2012-12-19 07:18:57 +00001//===-- Attribute.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling034b94b2012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000011// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000030// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling16f95662013-01-27 10:28:39 +000033Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
34 AttrBuilder B;
35 for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
36 I != E; ++I)
37 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000038 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000039}
Bill Wendling8e635db2012-10-08 21:47:17 +000040
Bill Wendling034b94b2012-12-19 07:18:57 +000041Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000043 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000044 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000045
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000049 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000057 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000063}
64
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000065Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
66 AttrBuilder B;
67 return get(Context, B.addAlignmentAttr(Align));
68}
69
70Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
71 uint64_t Align) {
72 AttrBuilder B;
73 return get(Context, B.addStackAlignmentAttr(Align));
74}
75
Bill Wendling629fb822012-12-22 00:37:52 +000076bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000077 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
Bill Wendling034b94b2012-12-19 07:18:57 +000080bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000081 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000082}
83
Bill Wendlinge66f3d32012-10-05 06:44:41 +000084/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000085unsigned Attribute::getAlignment() const {
86 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000087 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000088 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000089}
90
91/// This returns the stack alignment field of an attribute as a byte alignment
92/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000093unsigned Attribute::getStackAlignment() const {
94 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000095 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000096 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000097}
98
Bill Wendling92e287f2012-12-31 11:51:54 +000099bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000100 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +0000101}
Bill Wendling92e287f2012-12-31 11:51:54 +0000102bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000103 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000104}
105
Bill Wendling3467e302013-01-24 00:06:56 +0000106bool Attribute::operator<(Attribute A) const {
107 if (!pImpl && !A.pImpl) return false;
108 if (!pImpl) return true;
109 if (!A.pImpl) return false;
110 return *pImpl < *A.pImpl;
111}
112
Bill Wendling1db9b692013-01-09 23:36:50 +0000113uint64_t Attribute::Raw() const {
114 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000115}
116
Bill Wendling034b94b2012-12-19 07:18:57 +0000117std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000118 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000120 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000122 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000124 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000126 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000128 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000130 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000132 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000134 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000136 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000138 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000140 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000142 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000144 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000146 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000148 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000150 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000152 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000154 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000156 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000158 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000159 if (hasAttribute(Attribute::StackProtectStrong))
160 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000162 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000164 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000166 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000168 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000170 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000172 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000174 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000175 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000176 Result += ") ";
177 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000179 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000180 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000181 Result += " ";
182 }
James Molloy67ae1352012-12-20 16:04:27 +0000183 if (hasAttribute(Attribute::NoDuplicate))
184 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000185 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000186 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000187 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000188 return Result;
189}
190
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000191//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000192// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000193//===----------------------------------------------------------------------===//
194
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000195AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
196 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000197 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000198 if (!pImpl) return;
199
Bill Wendling73bc4522013-01-28 00:21:34 +0000200 AttrBuilder B;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000201
Bill Wendling73bc4522013-01-28 00:21:34 +0000202 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
203 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000204
Bill Wendling73bc4522013-01-28 00:21:34 +0000205 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
206 IE = pImpl->end(I); II != IE; ++II)
207 B.addAttributes(*II);
208
209 break;
210 }
211
212 if (!B.hasAttributes()) return;
213
214 uint64_t Mask = B.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000215
Bill Wendling956f1342013-01-18 21:11:39 +0000216 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
217 I = Attribute::AttrKind(I + 1)) {
218 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
219 Attrs.insert(I);
220
221 if (I == Attribute::Alignment)
222 Alignment = 1ULL << ((A >> 16) - 1);
223 else if (I == Attribute::StackAlignment)
224 StackAlignment = 1ULL << ((A >> 26)-1);
225 }
226 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000227}
228
Bill Wendling03198882013-01-04 23:27:34 +0000229void AttrBuilder::clear() {
230 Attrs.clear();
231 Alignment = StackAlignment = 0;
232}
233
234AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
235 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000236 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000237}
238
Bill Wendling03198882013-01-04 23:27:34 +0000239AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
240 Attrs.erase(Val);
241 if (Val == Attribute::Alignment)
242 Alignment = 0;
243 else if (Val == Attribute::StackAlignment)
244 StackAlignment = 0;
245
Bill Wendlinga19a5302012-10-14 04:10:01 +0000246 return *this;
247}
248
Bill Wendling49f60602013-01-28 05:23:28 +0000249AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
250 uint64_t Mask = Attr.Raw();
251
252 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
253 I = Attribute::AttrKind(I + 1))
254 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
255 Attrs.insert(I);
256
257 if (Attr.getAlignment())
258 Alignment = Attr.getAlignment();
259 if (Attr.getStackAlignment())
260 StackAlignment = Attr.getStackAlignment();
261 return *this;
262}
263
264AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
265 uint64_t Mask = A.Raw();
266
267 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
268 I = Attribute::AttrKind(I + 1)) {
269 if (Mask & AttributeImpl::getAttrMask(I)) {
270 Attrs.erase(I);
271
272 if (I == Attribute::Alignment)
273 Alignment = 0;
274 else if (I == Attribute::StackAlignment)
275 StackAlignment = 0;
276 }
277 }
278
279 return *this;
280}
281
Bill Wendling702cc912012-10-15 20:35:56 +0000282AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000283 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000284
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000285 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
286 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000287
288 Attrs.insert(Attribute::Alignment);
289 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000290 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000291}
292
Bill Wendling03198882013-01-04 23:27:34 +0000293AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
294 // Default alignment, allow the target to define how to align it.
295 if (Align == 0) return *this;
296
297 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
298 assert(Align <= 0x100 && "Alignment too large.");
299
300 Attrs.insert(Attribute::StackAlignment);
301 StackAlignment = Align;
302 return *this;
303}
304
305AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
306 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
307 I = Attribute::AttrKind(I + 1)) {
308 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
309 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000310
Bill Wendling03198882013-01-04 23:27:34 +0000311 if (I == Attribute::Alignment)
312 Alignment = 1ULL << ((A >> 16) - 1);
313 else if (I == Attribute::StackAlignment)
314 StackAlignment = 1ULL << ((A >> 26)-1);
315 }
316 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000317
Bill Wendling3a106e62012-10-09 19:01:18 +0000318 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000319}
320
Bill Wendling22bd6412013-01-03 01:54:39 +0000321bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000322 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000323}
324
Bill Wendling702cc912012-10-15 20:35:56 +0000325bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000326 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000327}
Bill Wendling60507d52013-01-04 20:54:35 +0000328
Bill Wendling034b94b2012-12-19 07:18:57 +0000329bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000330 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000331}
Bill Wendling60507d52013-01-04 20:54:35 +0000332
Bill Wendling702cc912012-10-15 20:35:56 +0000333bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000334 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000335}
336
Bill Wendling1db9b692013-01-09 23:36:50 +0000337uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000338 uint64_t Mask = 0;
339
340 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
341 E = Attrs.end(); I != E; ++I) {
342 Attribute::AttrKind Kind = *I;
343
344 if (Kind == Attribute::Alignment)
345 Mask |= (Log2_32(Alignment) + 1) << 16;
346 else if (Kind == Attribute::StackAlignment)
347 Mask |= (Log2_32(StackAlignment) + 1) << 26;
348 else
349 Mask |= AttributeImpl::getAttrMask(Kind);
350 }
351
352 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000353}
354
Bill Wendling03198882013-01-04 23:27:34 +0000355bool AttrBuilder::operator==(const AttrBuilder &B) {
356 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
357 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
358 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000359}
360
Chris Lattner58d74912008-03-12 17:45:29 +0000361//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000362// AttributeImpl Definition
363//===----------------------------------------------------------------------===//
364
Bill Wendling1bbd6442013-01-05 01:36:54 +0000365AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
366 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000367 Data = ConstantInt::get(Type::getInt64Ty(C), data);
368}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000369AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
370 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000371 Data = ConstantInt::get(Type::getInt64Ty(C), data);
372}
373AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000374 ArrayRef<Constant*> values)
375 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000376 Data = ConstantInt::get(Type::getInt64Ty(C), data);
377 Vals.reserve(values.size());
378 Vals.append(values.begin(), values.end());
379}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000380AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
381 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000382 Data = ConstantDataArray::getString(C, data);
383}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000384
Bill Wendling60507d52013-01-04 20:54:35 +0000385bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000386 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
387 return CI->getZExtValue() == Kind;
388 return false;
389}
Bill Wendling60507d52013-01-04 20:54:35 +0000390bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
391 return !(*this == Kind);
392}
Bill Wendling529ec712012-12-30 01:38:39 +0000393
Bill Wendling60507d52013-01-04 20:54:35 +0000394bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000395 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
396 if (CDA->isString())
397 return CDA->getAsString() == Kind;
398 return false;
399}
Bill Wendling3467e302013-01-24 00:06:56 +0000400
Bill Wendling60507d52013-01-04 20:54:35 +0000401bool AttributeImpl::operator!=(StringRef Kind) const {
402 return !(*this == Kind);
403}
Bill Wendling529ec712012-12-30 01:38:39 +0000404
Bill Wendling3467e302013-01-24 00:06:56 +0000405bool AttributeImpl::operator<(const AttributeImpl &AI) const {
406 if (!Data && !AI.Data) return false;
407 if (!Data && AI.Data) return true;
408 if (Data && !AI.Data) return false;
409
410 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
411 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
412
413 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
414 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
415
416 if (ThisCI && ThatCI)
417 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
418
419 if (ThisCI && ThatCDA)
420 return true;
421
422 if (ThisCDA && ThatCI)
423 return false;
424
425 return ThisCDA->getAsString() < ThatCDA->getAsString();
426}
427
Bill Wendling1db9b692013-01-09 23:36:50 +0000428uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000429 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000430 return cast<ConstantInt>(Data)->getZExtValue();
431}
432
Bill Wendling60507d52013-01-04 20:54:35 +0000433uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000434 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000435 case Attribute::EndAttrKinds:
436 case Attribute::AttrKindEmptyKey:
437 case Attribute::AttrKindTombstoneKey:
438 llvm_unreachable("Synthetic enumerators which should never get here");
439
Bill Wendling034b94b2012-12-19 07:18:57 +0000440 case Attribute::None: return 0;
441 case Attribute::ZExt: return 1 << 0;
442 case Attribute::SExt: return 1 << 1;
443 case Attribute::NoReturn: return 1 << 2;
444 case Attribute::InReg: return 1 << 3;
445 case Attribute::StructRet: return 1 << 4;
446 case Attribute::NoUnwind: return 1 << 5;
447 case Attribute::NoAlias: return 1 << 6;
448 case Attribute::ByVal: return 1 << 7;
449 case Attribute::Nest: return 1 << 8;
450 case Attribute::ReadNone: return 1 << 9;
451 case Attribute::ReadOnly: return 1 << 10;
452 case Attribute::NoInline: return 1 << 11;
453 case Attribute::AlwaysInline: return 1 << 12;
454 case Attribute::OptimizeForSize: return 1 << 13;
455 case Attribute::StackProtect: return 1 << 14;
456 case Attribute::StackProtectReq: return 1 << 15;
457 case Attribute::Alignment: return 31 << 16;
458 case Attribute::NoCapture: return 1 << 21;
459 case Attribute::NoRedZone: return 1 << 22;
460 case Attribute::NoImplicitFloat: return 1 << 23;
461 case Attribute::Naked: return 1 << 24;
462 case Attribute::InlineHint: return 1 << 25;
463 case Attribute::StackAlignment: return 7 << 26;
464 case Attribute::ReturnsTwice: return 1 << 29;
465 case Attribute::UWTable: return 1 << 30;
466 case Attribute::NonLazyBind: return 1U << 31;
467 case Attribute::AddressSafety: return 1ULL << 32;
468 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000469 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000470 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000471 }
472 llvm_unreachable("Unsupported attribute type");
473}
474
Bill Wendling60507d52013-01-04 20:54:35 +0000475bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000476 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000477}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000478
Bill Wendlingf6670722012-12-20 01:36:59 +0000479bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000480 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000481}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000482
Bill Wendlingf6670722012-12-20 01:36:59 +0000483uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000484 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000485 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000486}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000487
Bill Wendlingf6670722012-12-20 01:36:59 +0000488uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000489 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000490 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000491}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000492
Bill Wendling8456efb2013-01-09 00:32:55 +0000493void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
494 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000495 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000496#if 0
497 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000498 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
499 I != E; ++I)
500 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000501#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000502}
503
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000504//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000505// AttributeSetNode Definition
506//===----------------------------------------------------------------------===//
507
508AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
509 ArrayRef<Attribute> Attrs) {
510 if (Attrs.empty())
511 return 0;
512
513 // Otherwise, build a key to look up the existing attributes.
514 LLVMContextImpl *pImpl = C.pImpl;
515 FoldingSetNodeID ID;
516
517 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
518 std::sort(SortedAttrs.begin(), SortedAttrs.end());
519
520 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
521 E = SortedAttrs.end(); I != E; ++I)
522 I->Profile(ID);
523
524 void *InsertPoint;
525 AttributeSetNode *PA =
526 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
527
528 // If we didn't find any existing attributes of the same shape then create a
529 // new one and insert it.
530 if (!PA) {
531 PA = new AttributeSetNode(SortedAttrs);
532 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
533 }
534
535 // Return the AttributesListNode that we found or created.
536 return PA;
537}
538
539//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000540// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000541//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000542
Bill Wendling6a325cc2013-01-27 12:50:02 +0000543AttributeSetImpl::
544AttributeSetImpl(LLVMContext &C,
545 ArrayRef<AttributeWithIndex> attrs)
546 : Context(C), AttrList(attrs.begin(), attrs.end()) {
547 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
548 const AttributeWithIndex &AWI = attrs[I];
549 uint64_t Mask = AWI.Attrs.Raw();
550 SmallVector<Attribute, 8> Attrs;
551
552 for (Attribute::AttrKind II = Attribute::None;
553 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
554 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
555 AttrBuilder B;
556
557 if (II == Attribute::Alignment)
558 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
559 else if (II == Attribute::StackAlignment)
560 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
561 else
562 B.addAttribute(II);
563
564 Attrs.push_back(Attribute::get(C, B));
565 }
566 }
567
568 AttrNodes.push_back(std::make_pair(AWI.Index,
569 AttributeSetNode::get(C, Attrs)));
570 }
Bill Wendling6a325cc2013-01-27 12:50:02 +0000571
Bill Wendlinga5372d22013-01-27 21:20:06 +0000572 assert(AttrNodes.size() == AttrList.size() &&
573 "Number of attributes is different between lists!");
574#ifndef NDEBUG
575 for (unsigned I = 0, E = AttrNodes.size(); I != E; ++I)
576 assert((I == 0 || AttrNodes[I - 1].first < AttrNodes[I].first) &&
577 "Attributes not in ascending order!");
578#endif
Bill Wendling6a325cc2013-01-27 12:50:02 +0000579}
580
Bill Wendlingd05204a2013-01-27 23:41:29 +0000581uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
582 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
583 if (getSlotIndex(I) != Index) continue;
584 const AttributeSetNode *ASN = AttrNodes[I].second;
585 AttrBuilder B;
586
587 for (AttributeSetNode::const_iterator II = ASN->begin(),
588 IE = ASN->end(); II != IE; ++II)
589 B.addAttributes(*II);
590
591 assert(B.Raw() == AttrList[I].Attrs.Raw() &&
592 "Attributes aren't the same!");
593 return B.Raw();
594 }
595
596 return 0;
597}
598
Bill Wendling6a325cc2013-01-27 12:50:02 +0000599//===----------------------------------------------------------------------===//
600// AttributeSet Method Implementations
601//===----------------------------------------------------------------------===//
602
Bill Wendling28d65722013-01-23 06:14:59 +0000603AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
604 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000605 return pImpl && hasAttributes(Idx) ?
606 AttributeSet::get(pImpl->getContext(),
Bill Wendling28d65722013-01-23 06:14:59 +0000607 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
608 AttributeSet();
609}
610
Bill Wendling3fc4b962013-01-21 22:44:49 +0000611AttributeSet AttributeSet::getRetAttributes() const {
612 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000613 return pImpl && hasAttributes(ReturnIndex) ?
614 AttributeSet::get(pImpl->getContext(),
Bill Wendling3fc4b962013-01-21 22:44:49 +0000615 AttributeWithIndex::get(ReturnIndex,
616 getAttributes(ReturnIndex))) :
617 AttributeSet();
618}
619
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000620AttributeSet AttributeSet::getFnAttributes() const {
621 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000622 return pImpl && hasAttributes(FunctionIndex) ?
623 AttributeSet::get(pImpl->getContext(),
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000624 AttributeWithIndex::get(FunctionIndex,
625 getAttributes(FunctionIndex))) :
626 AttributeSet();
627}
628
Bill Wendling99faa3b2012-12-07 23:16:57 +0000629AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000630 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000631 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000632 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000633 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000634
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000635#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000636 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000637 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000638 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000639 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000640 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000641 }
642#endif
Bill Wendling77898682012-10-16 06:01:44 +0000643
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000644 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000645 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000646 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000647 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000648
Bill Wendling0976e002012-11-20 05:09:20 +0000649 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000650 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000651
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000652 // If we didn't find any existing attributes of the same shape then
653 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000654 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000655 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000656 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000657 }
Bill Wendling77898682012-10-16 06:01:44 +0000658
Devang Patel05988662008-09-25 21:00:45 +0000659 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000660 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000661}
662
Bill Wendling1bbd6442013-01-05 01:36:54 +0000663AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000664 // FIXME: This should be implemented as a loop that creates the
665 // AttributeWithIndexes that then are used to create the AttributeSet.
666 if (!B.hasAttributes())
667 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000668 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000669}
670
Bill Wendling28d65722013-01-23 06:14:59 +0000671AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000672 ArrayRef<Attribute::AttrKind> Kind) {
673 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
674 // replaced by an object that holds multiple Attribute::AttrKinds.
675 AttrBuilder B;
676 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
677 E = Kind.end(); I != E; ++I)
678 B.addAttribute(*I);
679 return get(C, Idx, B);
Bill Wendling28d65722013-01-23 06:14:59 +0000680}
681
Bill Wendling8e47daf2013-01-25 23:09:36 +0000682AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
683 SmallVector<AttributeWithIndex, 8> AttrList;
684 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
685 I != E; ++I) {
686 AttributeSet AS = *I;
Bill Wendlingec258982013-01-27 21:23:46 +0000687 if (!AS.pImpl) continue;
688 AttrList.append(AS.pImpl->AttrList.begin(), AS.pImpl->AttrList.end());
Bill Wendling8e47daf2013-01-25 23:09:36 +0000689 }
690
691 return get(C, AttrList);
692}
693
Bill Wendlingd05204a2013-01-27 23:41:29 +0000694/// \brief Return the number of slots used in this attribute list. This is the
695/// number of arguments that have an attribute set on them (including the
696/// function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000697unsigned AttributeSet::getNumSlots() const {
Bill Wendlingec258982013-01-27 21:23:46 +0000698 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000699}
700
Bill Wendling3e3e7892013-01-27 23:50:44 +0000701uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000702 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000703 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000704 return pImpl->getSlotIndex(Slot);
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000705}
706
Bill Wendling8e47daf2013-01-25 23:09:36 +0000707AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000708 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling8e47daf2013-01-25 23:09:36 +0000709 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000710 return pImpl->getSlotAttributes(Slot);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000711}
712
Bill Wendling19d815c2013-01-28 05:51:40 +0000713bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling831737d2012-12-30 10:32:01 +0000714 return getAttributes(Index).hasAttribute(Kind);
715}
716
Bill Wendling19d815c2013-01-28 05:51:40 +0000717bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000718 return getAttributes(Index).hasAttributes();
719}
720
Bill Wendling19d815c2013-01-28 05:51:40 +0000721std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000722 return getAttributes(Index).getAsString();
723}
724
Bill Wendling19d815c2013-01-28 05:51:40 +0000725unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
Bill Wendling956f1342013-01-18 21:11:39 +0000726 return getAttributes(Idx).getAlignment();
727}
728
Bill Wendling19d815c2013-01-28 05:51:40 +0000729unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000730 return getAttributes(Index).getStackAlignment();
731}
732
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000733uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000734 // FIXME: Remove this.
Bill Wendlingd05204a2013-01-27 23:41:29 +0000735 return pImpl ? pImpl->Raw(Index) : 0;
Bill Wendling831737d2012-12-30 10:32:01 +0000736}
737
Bill Wendlinge2501f52013-01-28 01:11:42 +0000738/// \brief The attributes for the specified index are returned.
739///
740/// FIXME: This shouldn't return 'Attribute'.
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000741Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000742 if (pImpl == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000743
Bill Wendlinge2501f52013-01-28 01:11:42 +0000744 // Loop through to find the attribute we want.
745 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
746 if (pImpl->getSlotIndex(I) != Idx) continue;
747
748 AttrBuilder B;
749 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
750 IE = pImpl->end(I); II != IE; ++II)
751 B.addAttributes(*II);
752 return Attribute::get(pImpl->getContext(), B);
753 }
Bill Wendlingef99fe82012-09-21 15:26:31 +0000754
Bill Wendling034b94b2012-12-19 07:18:57 +0000755 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000756}
757
758/// hasAttrSomewhere - Return true if the specified attribute is set for at
759/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000760bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000761 if (pImpl == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000762
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000763 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendlinge2501f52013-01-28 01:11:42 +0000764 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000765 IE = pImpl->end(I); II != IE; ++II)
766 if (II->hasAttribute(Attr))
767 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000768
Chris Lattner58d74912008-03-12 17:45:29 +0000769 return false;
770}
771
Bill Wendlingdefaca02013-01-22 21:15:51 +0000772AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
773 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000774 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendlingdefaca02013-01-22 21:15:51 +0000775}
776
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000777AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
778 AttributeSet Attrs) const {
Bill Wendling49f60602013-01-28 05:23:28 +0000779 if (!pImpl) return Attrs;
780 if (!Attrs.pImpl) return *this;
781
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000782#ifndef NDEBUG
Bill Wendling49f60602013-01-28 05:23:28 +0000783 // FIXME it is not obvious how this should work for alignment. For now, say
784 // we can't change a known alignment.
785 unsigned OldAlign = getParamAlignment(Idx);
786 unsigned NewAlign = Attrs.getParamAlignment(Idx);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000787 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000788 "Attempt to change alignment!");
789#endif
Bill Wendling77898682012-10-16 06:01:44 +0000790
Bill Wendling49f60602013-01-28 05:23:28 +0000791 // Add the attribute slots before the one we're trying to add.
792 SmallVector<AttributeSet, 4> AttrSet;
793 uint64_t NumAttrs = pImpl->getNumAttributes();
794 AttributeSet AS;
795 uint64_t LastIndex = 0;
796 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
797 if (getSlotIndex(I) >= Idx) {
798 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
799 break;
Chris Lattner58d74912008-03-12 17:45:29 +0000800 }
Bill Wendling49f60602013-01-28 05:23:28 +0000801 LastIndex = I + 1;
802 AttrSet.push_back(getSlotAttributes(I));
Chris Lattner58d74912008-03-12 17:45:29 +0000803 }
Bill Wendling77898682012-10-16 06:01:44 +0000804
Bill Wendling49f60602013-01-28 05:23:28 +0000805 // Now add the attribute into the correct slot. There may already be an
806 // AttributeSet there.
807 AttrBuilder B(AS, Idx);
808
809 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
810 if (Attrs.getSlotIndex(I) == Idx) {
811 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
812 IE = Attrs.pImpl->end(I); II != IE; ++II)
813 B.addAttributes(*II);
814 break;
815 }
816
817 AttrSet.push_back(AttributeSet::get(C, Idx, B));
818
819 // Add the remaining attribute slots.
820 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
821 AttrSet.push_back(getSlotAttributes(I));
822
823 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000824}
825
Bill Wendling8246df62013-01-23 00:45:55 +0000826AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
827 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000828 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendling8246df62013-01-23 00:45:55 +0000829}
830
831AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
832 AttributeSet Attrs) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000833 if (!pImpl) return AttributeSet();
834 if (!Attrs.pImpl) return *this;
Bill Wendling8246df62013-01-23 00:45:55 +0000835
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000836#ifndef NDEBUG
837 // FIXME it is not obvious how this should work for alignment.
838 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling98b92f32013-01-28 05:44:14 +0000839 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
840 "Attempt to change alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000841#endif
Bill Wendling77898682012-10-16 06:01:44 +0000842
Bill Wendling98b92f32013-01-28 05:44:14 +0000843 // Add the attribute slots before the one we're trying to add.
844 SmallVector<AttributeSet, 4> AttrSet;
845 uint64_t NumAttrs = pImpl->getNumAttributes();
846 AttributeSet AS;
847 uint64_t LastIndex = 0;
848 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
849 if (getSlotIndex(I) >= Idx) {
850 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
851 break;
852 }
853 LastIndex = I + 1;
854 AttrSet.push_back(getSlotAttributes(I));
855 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000856
Bill Wendling98b92f32013-01-28 05:44:14 +0000857 // Now add the attribute into the correct slot. There may already be an
858 // AttributeSet there.
859 AttrBuilder B(AS, Idx);
Bill Wendling77898682012-10-16 06:01:44 +0000860
Bill Wendling98b92f32013-01-28 05:44:14 +0000861 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
862 if (Attrs.getSlotIndex(I) == Idx) {
863 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
864 IE = Attrs.pImpl->end(I); II != IE; ++II)
865 B.removeAttributes(*II);
866 break;
867 }
Bill Wendling77898682012-10-16 06:01:44 +0000868
Bill Wendling98b92f32013-01-28 05:44:14 +0000869 AttrSet.push_back(AttributeSet::get(C, Idx, B));
Bill Wendling77898682012-10-16 06:01:44 +0000870
Bill Wendling98b92f32013-01-28 05:44:14 +0000871 // Add the remaining attribute slots.
872 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
873 AttrSet.push_back(getSlotAttributes(I));
Bill Wendling77898682012-10-16 06:01:44 +0000874
Bill Wendling98b92f32013-01-28 05:44:14 +0000875 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000876}
877
Bill Wendling99faa3b2012-12-07 23:16:57 +0000878void AttributeSet::dump() const {
Bill Wendling73bc4522013-01-28 00:21:34 +0000879 dbgs() << "PAL[\n";
880 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
Bill Wendling49716e52013-01-27 23:53:56 +0000881 uint64_t Index = getSlotIndex(i);
882 dbgs() << " { ";
883 if (Index == ~0U)
884 dbgs() << "~0U";
885 else
886 dbgs() << Index;
887 dbgs() << " => " << getAsString(Index) << " }\n";
Chris Lattner58d74912008-03-12 17:45:29 +0000888 }
Bill Wendling77898682012-10-16 06:01:44 +0000889
David Greeneef1894e2010-01-05 01:29:58 +0000890 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000891}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000892
893//===----------------------------------------------------------------------===//
894// AttributeFuncs Function Defintions
895//===----------------------------------------------------------------------===//
896
897Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
898 AttrBuilder Incompatible;
899
900 if (!Ty->isIntegerTy())
901 // Attribute that only apply to integers.
902 Incompatible.addAttribute(Attribute::SExt)
903 .addAttribute(Attribute::ZExt);
904
905 if (!Ty->isPointerTy())
906 // Attribute that only apply to pointers.
907 Incompatible.addAttribute(Attribute::ByVal)
908 .addAttribute(Attribute::Nest)
909 .addAttribute(Attribute::NoAlias)
910 .addAttribute(Attribute::NoCapture)
911 .addAttribute(Attribute::StructRet);
912
913 return Attribute::get(Ty->getContext(), Incompatible);
914}
915
916/// encodeLLVMAttributesForBitcode - This returns an integer containing an
917/// encoding of all the LLVM attributes found in the given attribute bitset.
918/// Any change to this encoding is a breaking change to bitcode compatibility.
919uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
920 unsigned Index) {
921 // FIXME: It doesn't make sense to store the alignment information as an
922 // expanded out value, we should store it as a log2 value. However, we can't
923 // just change that here without breaking bitcode compatibility. If this ever
924 // becomes a problem in practice, we should introduce new tag numbers in the
925 // bitcode file and have those tags use a more efficiently encoded alignment
926 // field.
927
928 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
929 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
930 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
931 if (Attrs.hasAttribute(Index, Attribute::Alignment))
932 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
933 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
934 return EncodedAttrs;
935}
936
937/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
938/// the LLVM attributes that have been decoded from the given integer. This
939/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
940Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
941 uint64_t EncodedAttrs){
942 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
943 // the bits above 31 down by 11 bits.
944 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
945 assert((!Alignment || isPowerOf2_32(Alignment)) &&
946 "Alignment must be a power of two.");
947
948 AttrBuilder B(EncodedAttrs & 0xffff);
949 if (Alignment)
950 B.addAlignmentAttr(Alignment);
951 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
952 return Attribute::get(C, B);
953}
954