blob: ac394b77e7e5715c56bad1d6ca5c5591c1a60523 [file] [log] [blame]
Bill Wendling87e10df2013-01-28 21:55:20 +00001//===-- Attributes.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 Wendling87e10df2013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000019#include "llvm/ADT/FoldingSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000022#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000023#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000024#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000026#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000027#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000028using namespace llvm;
29
Chris Lattner58d74912008-03-12 17:45:29 +000030//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000031// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000032//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000033
Bill Wendling6bdbf062013-01-28 22:33:39 +000034Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
Bill Wendling16f95662013-01-27 10:28:39 +000035 AttrBuilder B;
Bill Wendling6bdbf062013-01-28 22:33:39 +000036 return Attribute::get(Context, B.addAttribute(Kind));
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000037}
Bill Wendling8e635db2012-10-08 21:47:17 +000038
Bill Wendling034b94b2012-12-19 07:18:57 +000039Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
40 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000041 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000042 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000043
44 // Otherwise, build a key to look up the existing attributes.
45 LLVMContextImpl *pImpl = Context.pImpl;
46 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000047 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000048
49 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000050 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 if (!PA) {
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000055 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000056 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
57 }
58
59 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000060 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000061}
62
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000063Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
64 AttrBuilder B;
65 return get(Context, B.addAlignmentAttr(Align));
66}
67
68Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
69 uint64_t Align) {
70 AttrBuilder B;
71 return get(Context, B.addStackAlignmentAttr(Align));
72}
73
Bill Wendling629fb822012-12-22 00:37:52 +000074bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000075 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000076}
77
Bill Wendling034b94b2012-12-19 07:18:57 +000078bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000079 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000080}
81
Bill Wendlinge66f3d32012-10-05 06:44:41 +000082/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000083unsigned Attribute::getAlignment() const {
84 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000085 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000086 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000087}
88
89/// This returns the stack alignment field of an attribute as a byte alignment
90/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000091unsigned Attribute::getStackAlignment() const {
92 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000093 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000094 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000095}
96
Bill Wendling92e287f2012-12-31 11:51:54 +000097bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000098 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000099}
Bill Wendling92e287f2012-12-31 11:51:54 +0000100bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000101 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000102}
103
Bill Wendling3467e302013-01-24 00:06:56 +0000104bool Attribute::operator<(Attribute A) const {
105 if (!pImpl && !A.pImpl) return false;
106 if (!pImpl) return true;
107 if (!A.pImpl) return false;
108 return *pImpl < *A.pImpl;
109}
110
Bill Wendling1db9b692013-01-09 23:36:50 +0000111uint64_t Attribute::Raw() const {
112 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000113}
114
Bill Wendling034b94b2012-12-19 07:18:57 +0000115std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000116 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000118 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000120 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000122 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000124 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000126 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000128 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000130 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000132 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000134 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000136 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000138 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000140 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000142 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000144 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000146 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000148 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000150 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000152 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000154 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000156 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000157 if (hasAttribute(Attribute::StackProtectStrong))
158 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000159 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000160 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000162 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000164 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000166 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000168 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000170 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000172 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000173 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000174 Result += ") ";
175 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000177 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000178 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000179 Result += " ";
180 }
James Molloy67ae1352012-12-20 16:04:27 +0000181 if (hasAttribute(Attribute::NoDuplicate))
182 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000183 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000184 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000185 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000186 return Result;
187}
188
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000189//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000190// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000191//===----------------------------------------------------------------------===//
192
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000193AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
194 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000195 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000196 if (!pImpl) return;
197
Bill Wendling73bc4522013-01-28 00:21:34 +0000198 AttrBuilder B;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000199
Bill Wendling73bc4522013-01-28 00:21:34 +0000200 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
201 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000202
Bill Wendling73bc4522013-01-28 00:21:34 +0000203 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
204 IE = pImpl->end(I); II != IE; ++II)
205 B.addAttributes(*II);
206
207 break;
208 }
209
210 if (!B.hasAttributes()) return;
211
212 uint64_t Mask = B.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000213
Bill Wendling956f1342013-01-18 21:11:39 +0000214 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
215 I = Attribute::AttrKind(I + 1)) {
216 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
217 Attrs.insert(I);
218
219 if (I == Attribute::Alignment)
220 Alignment = 1ULL << ((A >> 16) - 1);
221 else if (I == Attribute::StackAlignment)
222 StackAlignment = 1ULL << ((A >> 26)-1);
223 }
224 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000225}
226
Bill Wendling03198882013-01-04 23:27:34 +0000227void AttrBuilder::clear() {
228 Attrs.clear();
229 Alignment = StackAlignment = 0;
230}
231
232AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
233 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000234 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000235}
236
Bill Wendling03198882013-01-04 23:27:34 +0000237AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
238 Attrs.erase(Val);
239 if (Val == Attribute::Alignment)
240 Alignment = 0;
241 else if (Val == Attribute::StackAlignment)
242 StackAlignment = 0;
243
Bill Wendlinga19a5302012-10-14 04:10:01 +0000244 return *this;
245}
246
Bill Wendling49f60602013-01-28 05:23:28 +0000247AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
248 uint64_t Mask = Attr.Raw();
249
250 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
251 I = Attribute::AttrKind(I + 1))
252 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
253 Attrs.insert(I);
254
255 if (Attr.getAlignment())
256 Alignment = Attr.getAlignment();
257 if (Attr.getStackAlignment())
258 StackAlignment = Attr.getStackAlignment();
259 return *this;
260}
261
262AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
263 uint64_t Mask = A.Raw();
264
265 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
266 I = Attribute::AttrKind(I + 1)) {
267 if (Mask & AttributeImpl::getAttrMask(I)) {
268 Attrs.erase(I);
269
270 if (I == Attribute::Alignment)
271 Alignment = 0;
272 else if (I == Attribute::StackAlignment)
273 StackAlignment = 0;
274 }
275 }
276
277 return *this;
278}
279
Bill Wendling702cc912012-10-15 20:35:56 +0000280AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000281 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000282
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000283 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
284 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000285
286 Attrs.insert(Attribute::Alignment);
287 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000288 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000289}
290
Bill Wendling03198882013-01-04 23:27:34 +0000291AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
292 // Default alignment, allow the target to define how to align it.
293 if (Align == 0) return *this;
294
295 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
296 assert(Align <= 0x100 && "Alignment too large.");
297
298 Attrs.insert(Attribute::StackAlignment);
299 StackAlignment = Align;
300 return *this;
301}
302
303AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
304 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
305 I = Attribute::AttrKind(I + 1)) {
306 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
307 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000308
Bill Wendling03198882013-01-04 23:27:34 +0000309 if (I == Attribute::Alignment)
310 Alignment = 1ULL << ((A >> 16) - 1);
311 else if (I == Attribute::StackAlignment)
312 StackAlignment = 1ULL << ((A >> 26)-1);
313 }
314 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000315
Bill Wendling3a106e62012-10-09 19:01:18 +0000316 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000317}
318
Bill Wendling22bd6412013-01-03 01:54:39 +0000319bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000320 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000321}
322
Bill Wendling702cc912012-10-15 20:35:56 +0000323bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000324 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000325}
Bill Wendling60507d52013-01-04 20:54:35 +0000326
Bill Wendling034b94b2012-12-19 07:18:57 +0000327bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000328 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000329}
Bill Wendling60507d52013-01-04 20:54:35 +0000330
Bill Wendling702cc912012-10-15 20:35:56 +0000331bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000332 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000333}
334
Bill Wendling1db9b692013-01-09 23:36:50 +0000335uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000336 uint64_t Mask = 0;
337
338 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
339 E = Attrs.end(); I != E; ++I) {
340 Attribute::AttrKind Kind = *I;
341
342 if (Kind == Attribute::Alignment)
343 Mask |= (Log2_32(Alignment) + 1) << 16;
344 else if (Kind == Attribute::StackAlignment)
345 Mask |= (Log2_32(StackAlignment) + 1) << 26;
346 else
347 Mask |= AttributeImpl::getAttrMask(Kind);
348 }
349
350 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000351}
352
Bill Wendling03198882013-01-04 23:27:34 +0000353bool AttrBuilder::operator==(const AttrBuilder &B) {
354 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
355 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
356 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000357}
358
Chris Lattner58d74912008-03-12 17:45:29 +0000359//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000360// AttributeImpl Definition
361//===----------------------------------------------------------------------===//
362
Bill Wendling1bbd6442013-01-05 01:36:54 +0000363AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
364 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000365 Data = ConstantInt::get(Type::getInt64Ty(C), data);
366}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000367AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
368 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000369 Data = ConstantInt::get(Type::getInt64Ty(C), data);
370}
371AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000372 ArrayRef<Constant*> values)
373 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000374 Data = ConstantInt::get(Type::getInt64Ty(C), data);
375 Vals.reserve(values.size());
376 Vals.append(values.begin(), values.end());
377}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000378AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
379 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000380 Data = ConstantDataArray::getString(C, data);
381}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000382
Bill Wendling60507d52013-01-04 20:54:35 +0000383bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000384 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
385 return CI->getZExtValue() == Kind;
386 return false;
387}
Bill Wendling60507d52013-01-04 20:54:35 +0000388bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
389 return !(*this == Kind);
390}
Bill Wendling529ec712012-12-30 01:38:39 +0000391
Bill Wendling60507d52013-01-04 20:54:35 +0000392bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000393 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
394 if (CDA->isString())
395 return CDA->getAsString() == Kind;
396 return false;
397}
Bill Wendling3467e302013-01-24 00:06:56 +0000398
Bill Wendling60507d52013-01-04 20:54:35 +0000399bool AttributeImpl::operator!=(StringRef Kind) const {
400 return !(*this == Kind);
401}
Bill Wendling529ec712012-12-30 01:38:39 +0000402
Bill Wendling3467e302013-01-24 00:06:56 +0000403bool AttributeImpl::operator<(const AttributeImpl &AI) const {
404 if (!Data && !AI.Data) return false;
405 if (!Data && AI.Data) return true;
406 if (Data && !AI.Data) return false;
407
408 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
409 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
410
411 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
412 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
413
414 if (ThisCI && ThatCI)
415 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
416
417 if (ThisCI && ThatCDA)
418 return true;
419
420 if (ThisCDA && ThatCI)
421 return false;
422
423 return ThisCDA->getAsString() < ThatCDA->getAsString();
424}
425
Bill Wendling1db9b692013-01-09 23:36:50 +0000426uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000427 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000428 return cast<ConstantInt>(Data)->getZExtValue();
429}
430
Bill Wendling60507d52013-01-04 20:54:35 +0000431uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000432 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000433 case Attribute::EndAttrKinds:
434 case Attribute::AttrKindEmptyKey:
435 case Attribute::AttrKindTombstoneKey:
436 llvm_unreachable("Synthetic enumerators which should never get here");
437
Bill Wendling034b94b2012-12-19 07:18:57 +0000438 case Attribute::None: return 0;
439 case Attribute::ZExt: return 1 << 0;
440 case Attribute::SExt: return 1 << 1;
441 case Attribute::NoReturn: return 1 << 2;
442 case Attribute::InReg: return 1 << 3;
443 case Attribute::StructRet: return 1 << 4;
444 case Attribute::NoUnwind: return 1 << 5;
445 case Attribute::NoAlias: return 1 << 6;
446 case Attribute::ByVal: return 1 << 7;
447 case Attribute::Nest: return 1 << 8;
448 case Attribute::ReadNone: return 1 << 9;
449 case Attribute::ReadOnly: return 1 << 10;
450 case Attribute::NoInline: return 1 << 11;
451 case Attribute::AlwaysInline: return 1 << 12;
452 case Attribute::OptimizeForSize: return 1 << 13;
453 case Attribute::StackProtect: return 1 << 14;
454 case Attribute::StackProtectReq: return 1 << 15;
455 case Attribute::Alignment: return 31 << 16;
456 case Attribute::NoCapture: return 1 << 21;
457 case Attribute::NoRedZone: return 1 << 22;
458 case Attribute::NoImplicitFloat: return 1 << 23;
459 case Attribute::Naked: return 1 << 24;
460 case Attribute::InlineHint: return 1 << 25;
461 case Attribute::StackAlignment: return 7 << 26;
462 case Attribute::ReturnsTwice: return 1 << 29;
463 case Attribute::UWTable: return 1 << 30;
464 case Attribute::NonLazyBind: return 1U << 31;
465 case Attribute::AddressSafety: return 1ULL << 32;
466 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000467 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000468 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000469 }
470 llvm_unreachable("Unsupported attribute type");
471}
472
Bill Wendling60507d52013-01-04 20:54:35 +0000473bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000474 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000475}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000476
Bill Wendlingf6670722012-12-20 01:36:59 +0000477bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000478 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000479}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000480
Bill Wendlingf6670722012-12-20 01:36:59 +0000481uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000482 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000483 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000484}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000485
Bill Wendlingf6670722012-12-20 01:36:59 +0000486uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000487 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000488 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000489}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000490
Bill Wendling8456efb2013-01-09 00:32:55 +0000491void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
492 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000493 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000494#if 0
495 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000496 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
497 I != E; ++I)
498 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000499#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000500}
501
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000502//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000503// AttributeSetNode Definition
504//===----------------------------------------------------------------------===//
505
506AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
507 ArrayRef<Attribute> Attrs) {
508 if (Attrs.empty())
509 return 0;
510
511 // Otherwise, build a key to look up the existing attributes.
512 LLVMContextImpl *pImpl = C.pImpl;
513 FoldingSetNodeID ID;
514
515 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
516 std::sort(SortedAttrs.begin(), SortedAttrs.end());
517
518 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
519 E = SortedAttrs.end(); I != E; ++I)
520 I->Profile(ID);
521
522 void *InsertPoint;
523 AttributeSetNode *PA =
524 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
525
526 // If we didn't find any existing attributes of the same shape then create a
527 // new one and insert it.
528 if (!PA) {
529 PA = new AttributeSetNode(SortedAttrs);
530 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
531 }
532
533 // Return the AttributesListNode that we found or created.
534 return PA;
535}
536
537//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000538// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000539//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000540
Bill Wendlingd05204a2013-01-27 23:41:29 +0000541uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
542 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
543 if (getSlotIndex(I) != Index) continue;
544 const AttributeSetNode *ASN = AttrNodes[I].second;
545 AttrBuilder B;
546
547 for (AttributeSetNode::const_iterator II = ASN->begin(),
548 IE = ASN->end(); II != IE; ++II)
549 B.addAttributes(*II);
Bill Wendlingd05204a2013-01-27 23:41:29 +0000550 return B.Raw();
551 }
552
553 return 0;
554}
555
Bill Wendling6a325cc2013-01-27 12:50:02 +0000556//===----------------------------------------------------------------------===//
557// AttributeSet Method Implementations
558//===----------------------------------------------------------------------===//
559
Bill Wendling28d65722013-01-23 06:14:59 +0000560AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
561 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000562 return pImpl && hasAttributes(Idx) ?
563 AttributeSet::get(pImpl->getContext(),
Bill Wendling6bdbf062013-01-28 22:33:39 +0000564 ArrayRef<std::pair<unsigned, Attribute> >(
Bill Wendling87e10df2013-01-28 21:55:20 +0000565 std::make_pair(Idx, getAttributes(Idx)))) :
Bill Wendling28d65722013-01-23 06:14:59 +0000566 AttributeSet();
567}
568
Bill Wendling3fc4b962013-01-21 22:44:49 +0000569AttributeSet AttributeSet::getRetAttributes() const {
570 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000571 return pImpl && hasAttributes(ReturnIndex) ?
572 AttributeSet::get(pImpl->getContext(),
Bill Wendling6bdbf062013-01-28 22:33:39 +0000573 ArrayRef<std::pair<unsigned, Attribute> >(
Bill Wendling87e10df2013-01-28 21:55:20 +0000574 std::make_pair(ReturnIndex,
575 getAttributes(ReturnIndex)))) :
Bill Wendling3fc4b962013-01-21 22:44:49 +0000576 AttributeSet();
577}
578
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000579AttributeSet AttributeSet::getFnAttributes() const {
580 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000581 return pImpl && hasAttributes(FunctionIndex) ?
582 AttributeSet::get(pImpl->getContext(),
Bill Wendling6bdbf062013-01-28 22:33:39 +0000583 ArrayRef<std::pair<unsigned, Attribute> >(
Bill Wendling87e10df2013-01-28 21:55:20 +0000584 std::make_pair(FunctionIndex,
585 getAttributes(FunctionIndex)))) :
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000586 AttributeSet();
587}
588
Bill Wendling87e10df2013-01-28 21:55:20 +0000589AttributeSet AttributeSet::getImpl(LLVMContext &C,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000590 ArrayRef<std::pair<unsigned,
Bill Wendling87e10df2013-01-28 21:55:20 +0000591 AttributeSetNode*> > Attrs) {
Bill Wendling0976e002012-11-20 05:09:20 +0000592 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000593 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000594 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000595
Bill Wendling0976e002012-11-20 05:09:20 +0000596 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000597 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000598
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000599 // If we didn't find any existing attributes of the same shape then
600 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000601 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000602 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000603 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000604 }
Bill Wendling77898682012-10-16 06:01:44 +0000605
Devang Patel05988662008-09-25 21:00:45 +0000606 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000607 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000608}
609
Bill Wendling87e10df2013-01-28 21:55:20 +0000610AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000611 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
Bill Wendling87e10df2013-01-28 21:55:20 +0000612 // If there are no attributes then return a null AttributesList pointer.
613 if (Attrs.empty())
614 return AttributeSet();
615
616#ifndef NDEBUG
617 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
618 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
619 "Misordered Attributes list!");
620 assert(Attrs[i].second.hasAttributes() &&
621 "Pointless attribute!");
622 }
623#endif
624
Bill Wendling6bdbf062013-01-28 22:33:39 +0000625 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
Bill Wendling87e10df2013-01-28 21:55:20 +0000626 // list.
Bill Wendling6bdbf062013-01-28 22:33:39 +0000627 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
628 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
Bill Wendling87e10df2013-01-28 21:55:20 +0000629 E = Attrs.end(); I != E; ) {
Bill Wendling6bdbf062013-01-28 22:33:39 +0000630 unsigned Index = I->first;
Bill Wendling87e10df2013-01-28 21:55:20 +0000631 SmallVector<Attribute, 4> AttrVec;
632 while (I->first == Index && I != E) {
633 AttrVec.push_back(I->second);
634 ++I;
635 }
636
637 AttrPairVec.push_back(std::make_pair(Index,
638 AttributeSetNode::get(C, AttrVec)));
639 }
640
641 return getImpl(C, AttrPairVec);
642}
643
644AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000645 ArrayRef<std::pair<unsigned,
Bill Wendling87e10df2013-01-28 21:55:20 +0000646 AttributeSetNode*> > Attrs) {
647 // If there are no attributes then return a null AttributesList pointer.
648 if (Attrs.empty())
649 return AttributeSet();
650
651 return getImpl(C, Attrs);
652}
653
Bill Wendling1bbd6442013-01-05 01:36:54 +0000654AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000655 if (!B.hasAttributes())
656 return AttributeSet();
Bill Wendling6bdbf062013-01-28 22:33:39 +0000657 return get(C, ArrayRef<std::pair<unsigned, Attribute> >(
Bill Wendling87e10df2013-01-28 21:55:20 +0000658 std::make_pair(Idx, Attribute::get(C, B))));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000659}
660
Bill Wendling28d65722013-01-23 06:14:59 +0000661AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000662 ArrayRef<Attribute::AttrKind> Kind) {
Bill Wendling6bdbf062013-01-28 22:33:39 +0000663 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Bill Wendling32a57952013-01-26 00:03:11 +0000664 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
665 E = Kind.end(); I != E; ++I)
Bill Wendling87e10df2013-01-28 21:55:20 +0000666 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
667 return get(C, Attrs);
Bill Wendling28d65722013-01-23 06:14:59 +0000668}
669
Bill Wendling8e47daf2013-01-25 23:09:36 +0000670AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
Bill Wendling6bdbf062013-01-28 22:33:39 +0000671 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
Bill Wendling87e10df2013-01-28 21:55:20 +0000672 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
673 AttributeSet AS = Attrs[I];
Bill Wendlingec258982013-01-27 21:23:46 +0000674 if (!AS.pImpl) continue;
Bill Wendling87e10df2013-01-28 21:55:20 +0000675 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
Bill Wendling8e47daf2013-01-25 23:09:36 +0000676 }
677
Bill Wendling87e10df2013-01-28 21:55:20 +0000678 return get(C, AttrNodeVec);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000679}
680
Bill Wendlingd05204a2013-01-27 23:41:29 +0000681/// \brief Return the number of slots used in this attribute list. This is the
682/// number of arguments that have an attribute set on them (including the
683/// function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000684unsigned AttributeSet::getNumSlots() const {
Bill Wendlingec258982013-01-27 21:23:46 +0000685 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000686}
687
Bill Wendling3e3e7892013-01-27 23:50:44 +0000688uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000689 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000690 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000691 return pImpl->getSlotIndex(Slot);
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000692}
693
Bill Wendling8e47daf2013-01-25 23:09:36 +0000694AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000695 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling8e47daf2013-01-25 23:09:36 +0000696 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000697 return pImpl->getSlotAttributes(Slot);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000698}
699
Bill Wendling19d815c2013-01-28 05:51:40 +0000700bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling831737d2012-12-30 10:32:01 +0000701 return getAttributes(Index).hasAttribute(Kind);
702}
703
Bill Wendling19d815c2013-01-28 05:51:40 +0000704bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000705 return getAttributes(Index).hasAttributes();
706}
707
Bill Wendling19d815c2013-01-28 05:51:40 +0000708std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000709 return getAttributes(Index).getAsString();
710}
711
Bill Wendling19d815c2013-01-28 05:51:40 +0000712unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
Bill Wendling956f1342013-01-18 21:11:39 +0000713 return getAttributes(Idx).getAlignment();
714}
715
Bill Wendling19d815c2013-01-28 05:51:40 +0000716unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000717 return getAttributes(Index).getStackAlignment();
718}
719
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000720uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000721 // FIXME: Remove this.
Bill Wendlingd05204a2013-01-27 23:41:29 +0000722 return pImpl ? pImpl->Raw(Index) : 0;
Bill Wendling831737d2012-12-30 10:32:01 +0000723}
724
Bill Wendlinge2501f52013-01-28 01:11:42 +0000725/// \brief The attributes for the specified index are returned.
726///
727/// FIXME: This shouldn't return 'Attribute'.
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000728Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000729 if (pImpl == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000730
Bill Wendlinge2501f52013-01-28 01:11:42 +0000731 // Loop through to find the attribute we want.
732 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
733 if (pImpl->getSlotIndex(I) != Idx) continue;
734
735 AttrBuilder B;
736 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
737 IE = pImpl->end(I); II != IE; ++II)
738 B.addAttributes(*II);
739 return Attribute::get(pImpl->getContext(), B);
740 }
Bill Wendlingef99fe82012-09-21 15:26:31 +0000741
Bill Wendling034b94b2012-12-19 07:18:57 +0000742 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000743}
744
745/// hasAttrSomewhere - Return true if the specified attribute is set for at
746/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000747bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000748 if (pImpl == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000749
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000750 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendlinge2501f52013-01-28 01:11:42 +0000751 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000752 IE = pImpl->end(I); II != IE; ++II)
753 if (II->hasAttribute(Attr))
754 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000755
Chris Lattner58d74912008-03-12 17:45:29 +0000756 return false;
757}
758
Bill Wendlingdefaca02013-01-22 21:15:51 +0000759AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
760 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000761 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendlingdefaca02013-01-22 21:15:51 +0000762}
763
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000764AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
765 AttributeSet Attrs) const {
Bill Wendling49f60602013-01-28 05:23:28 +0000766 if (!pImpl) return Attrs;
767 if (!Attrs.pImpl) return *this;
768
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000769#ifndef NDEBUG
Bill Wendling49f60602013-01-28 05:23:28 +0000770 // FIXME it is not obvious how this should work for alignment. For now, say
771 // we can't change a known alignment.
772 unsigned OldAlign = getParamAlignment(Idx);
773 unsigned NewAlign = Attrs.getParamAlignment(Idx);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000774 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000775 "Attempt to change alignment!");
776#endif
Bill Wendling77898682012-10-16 06:01:44 +0000777
Bill Wendling49f60602013-01-28 05:23:28 +0000778 // Add the attribute slots before the one we're trying to add.
779 SmallVector<AttributeSet, 4> AttrSet;
780 uint64_t NumAttrs = pImpl->getNumAttributes();
781 AttributeSet AS;
782 uint64_t LastIndex = 0;
783 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
784 if (getSlotIndex(I) >= Idx) {
785 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
786 break;
Chris Lattner58d74912008-03-12 17:45:29 +0000787 }
Bill Wendling49f60602013-01-28 05:23:28 +0000788 LastIndex = I + 1;
789 AttrSet.push_back(getSlotAttributes(I));
Chris Lattner58d74912008-03-12 17:45:29 +0000790 }
Bill Wendling77898682012-10-16 06:01:44 +0000791
Bill Wendling49f60602013-01-28 05:23:28 +0000792 // Now add the attribute into the correct slot. There may already be an
793 // AttributeSet there.
794 AttrBuilder B(AS, Idx);
795
796 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
797 if (Attrs.getSlotIndex(I) == Idx) {
798 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
799 IE = Attrs.pImpl->end(I); II != IE; ++II)
800 B.addAttributes(*II);
801 break;
802 }
803
804 AttrSet.push_back(AttributeSet::get(C, Idx, B));
805
806 // Add the remaining attribute slots.
807 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
808 AttrSet.push_back(getSlotAttributes(I));
809
810 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000811}
812
Bill Wendling8246df62013-01-23 00:45:55 +0000813AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
814 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000815 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendling8246df62013-01-23 00:45:55 +0000816}
817
818AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
819 AttributeSet Attrs) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000820 if (!pImpl) return AttributeSet();
821 if (!Attrs.pImpl) return *this;
Bill Wendling8246df62013-01-23 00:45:55 +0000822
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000823#ifndef NDEBUG
824 // FIXME it is not obvious how this should work for alignment.
825 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling98b92f32013-01-28 05:44:14 +0000826 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
827 "Attempt to change alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000828#endif
Bill Wendling77898682012-10-16 06:01:44 +0000829
Bill Wendling98b92f32013-01-28 05:44:14 +0000830 // Add the attribute slots before the one we're trying to add.
831 SmallVector<AttributeSet, 4> AttrSet;
832 uint64_t NumAttrs = pImpl->getNumAttributes();
833 AttributeSet AS;
834 uint64_t LastIndex = 0;
835 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
836 if (getSlotIndex(I) >= Idx) {
837 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
838 break;
839 }
840 LastIndex = I + 1;
841 AttrSet.push_back(getSlotAttributes(I));
842 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000843
Bill Wendling98b92f32013-01-28 05:44:14 +0000844 // Now add the attribute into the correct slot. There may already be an
845 // AttributeSet there.
846 AttrBuilder B(AS, Idx);
Bill Wendling77898682012-10-16 06:01:44 +0000847
Bill Wendling98b92f32013-01-28 05:44:14 +0000848 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
849 if (Attrs.getSlotIndex(I) == Idx) {
850 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
851 IE = Attrs.pImpl->end(I); II != IE; ++II)
852 B.removeAttributes(*II);
853 break;
854 }
Bill Wendling77898682012-10-16 06:01:44 +0000855
Bill Wendling98b92f32013-01-28 05:44:14 +0000856 AttrSet.push_back(AttributeSet::get(C, Idx, B));
Bill Wendling77898682012-10-16 06:01:44 +0000857
Bill Wendling98b92f32013-01-28 05:44:14 +0000858 // Add the remaining attribute slots.
859 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
860 AttrSet.push_back(getSlotAttributes(I));
Bill Wendling77898682012-10-16 06:01:44 +0000861
Bill Wendling98b92f32013-01-28 05:44:14 +0000862 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000863}
864
Bill Wendling99faa3b2012-12-07 23:16:57 +0000865void AttributeSet::dump() const {
Bill Wendling73bc4522013-01-28 00:21:34 +0000866 dbgs() << "PAL[\n";
867 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
Bill Wendling49716e52013-01-27 23:53:56 +0000868 uint64_t Index = getSlotIndex(i);
869 dbgs() << " { ";
870 if (Index == ~0U)
871 dbgs() << "~0U";
872 else
873 dbgs() << Index;
874 dbgs() << " => " << getAsString(Index) << " }\n";
Chris Lattner58d74912008-03-12 17:45:29 +0000875 }
Bill Wendling77898682012-10-16 06:01:44 +0000876
David Greeneef1894e2010-01-05 01:29:58 +0000877 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000878}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000879
880//===----------------------------------------------------------------------===//
881// AttributeFuncs Function Defintions
882//===----------------------------------------------------------------------===//
883
884Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
885 AttrBuilder Incompatible;
886
887 if (!Ty->isIntegerTy())
888 // Attribute that only apply to integers.
889 Incompatible.addAttribute(Attribute::SExt)
890 .addAttribute(Attribute::ZExt);
891
892 if (!Ty->isPointerTy())
893 // Attribute that only apply to pointers.
894 Incompatible.addAttribute(Attribute::ByVal)
895 .addAttribute(Attribute::Nest)
896 .addAttribute(Attribute::NoAlias)
897 .addAttribute(Attribute::NoCapture)
898 .addAttribute(Attribute::StructRet);
899
900 return Attribute::get(Ty->getContext(), Incompatible);
901}
902
903/// encodeLLVMAttributesForBitcode - This returns an integer containing an
904/// encoding of all the LLVM attributes found in the given attribute bitset.
905/// Any change to this encoding is a breaking change to bitcode compatibility.
906uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
907 unsigned Index) {
908 // FIXME: It doesn't make sense to store the alignment information as an
909 // expanded out value, we should store it as a log2 value. However, we can't
910 // just change that here without breaking bitcode compatibility. If this ever
911 // becomes a problem in practice, we should introduce new tag numbers in the
912 // bitcode file and have those tags use a more efficiently encoded alignment
913 // field.
914
915 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
916 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
917 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
918 if (Attrs.hasAttribute(Index, Attribute::Alignment))
919 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
920 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
921 return EncodedAttrs;
922}
923
924/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
925/// the LLVM attributes that have been decoded from the given integer. This
926/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
927Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
928 uint64_t EncodedAttrs){
929 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
930 // the bits above 31 down by 11 bits.
931 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
932 assert((!Alignment || isPowerOf2_32(Alignment)) &&
933 "Alignment must be a power of two.");
934
935 AttrBuilder B(EncodedAttrs & 0xffff);
936 if (Alignment)
937 B.addAlignmentAttr(Alignment);
938 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
939 return Attribute::get(C, B);
940}
941