blob: 1ac66d56affc47822a5b3601bbe76096b6eb68a5 [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 Wendling16f95662013-01-27 10:28:39 +000034Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
35 AttrBuilder B;
36 for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
37 I != E; ++I)
38 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000039 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000040}
Bill Wendling8e635db2012-10-08 21:47:17 +000041
Bill Wendling034b94b2012-12-19 07:18:57 +000042Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
43 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000044 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000045 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000046
47 // Otherwise, build a key to look up the existing attributes.
48 LLVMContextImpl *pImpl = Context.pImpl;
49 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000050 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000053 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000054
55 if (!PA) {
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000058 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000059 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 }
61
62 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000063 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000064}
65
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000066Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
67 AttrBuilder B;
68 return get(Context, B.addAlignmentAttr(Align));
69}
70
71Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
72 uint64_t Align) {
73 AttrBuilder B;
74 return get(Context, B.addStackAlignmentAttr(Align));
75}
76
Bill Wendling629fb822012-12-22 00:37:52 +000077bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000078 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000079}
80
Bill Wendling034b94b2012-12-19 07:18:57 +000081bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000082 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000083}
84
Bill Wendlinge66f3d32012-10-05 06:44:41 +000085/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000086unsigned Attribute::getAlignment() const {
87 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000088 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000089 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000090}
91
92/// This returns the stack alignment field of an attribute as a byte alignment
93/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000094unsigned Attribute::getStackAlignment() const {
95 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000096 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000097 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000098}
99
Bill Wendling92e287f2012-12-31 11:51:54 +0000100bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000101 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +0000102}
Bill Wendling92e287f2012-12-31 11:51:54 +0000103bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000104 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +0000105}
106
Bill Wendling3467e302013-01-24 00:06:56 +0000107bool Attribute::operator<(Attribute A) const {
108 if (!pImpl && !A.pImpl) return false;
109 if (!pImpl) return true;
110 if (!A.pImpl) return false;
111 return *pImpl < *A.pImpl;
112}
113
Bill Wendling1db9b692013-01-09 23:36:50 +0000114uint64_t Attribute::Raw() const {
115 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000116}
117
Bill Wendling034b94b2012-12-19 07:18:57 +0000118std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000119 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000121 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000123 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000124 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000125 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000127 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000128 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000129 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000130 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000131 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000132 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000133 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000134 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000135 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000137 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000138 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000139 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000140 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000141 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000143 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000145 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000146 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000147 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000148 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000149 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000150 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000151 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000153 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000155 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000157 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000159 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000160 if (hasAttribute(Attribute::StackProtectStrong))
161 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000163 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000165 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000166 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000167 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000168 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000169 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000170 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000171 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000173 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000175 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000176 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000177 Result += ") ";
178 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000179 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000180 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000181 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000182 Result += " ";
183 }
James Molloy67ae1352012-12-20 16:04:27 +0000184 if (hasAttribute(Attribute::NoDuplicate))
185 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000186 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000187 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000188 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000189 return Result;
190}
191
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000192//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000193// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000194//===----------------------------------------------------------------------===//
195
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000196AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
197 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000198 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000199 if (!pImpl) return;
200
Bill Wendling73bc4522013-01-28 00:21:34 +0000201 AttrBuilder B;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000202
Bill Wendling73bc4522013-01-28 00:21:34 +0000203 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
204 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000205
Bill Wendling73bc4522013-01-28 00:21:34 +0000206 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
207 IE = pImpl->end(I); II != IE; ++II)
208 B.addAttributes(*II);
209
210 break;
211 }
212
213 if (!B.hasAttributes()) return;
214
215 uint64_t Mask = B.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000216
Bill Wendling956f1342013-01-18 21:11:39 +0000217 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
218 I = Attribute::AttrKind(I + 1)) {
219 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
220 Attrs.insert(I);
221
222 if (I == Attribute::Alignment)
223 Alignment = 1ULL << ((A >> 16) - 1);
224 else if (I == Attribute::StackAlignment)
225 StackAlignment = 1ULL << ((A >> 26)-1);
226 }
227 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000228}
229
Bill Wendling03198882013-01-04 23:27:34 +0000230void AttrBuilder::clear() {
231 Attrs.clear();
232 Alignment = StackAlignment = 0;
233}
234
235AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
236 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000237 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000238}
239
Bill Wendling03198882013-01-04 23:27:34 +0000240AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
241 Attrs.erase(Val);
242 if (Val == Attribute::Alignment)
243 Alignment = 0;
244 else if (Val == Attribute::StackAlignment)
245 StackAlignment = 0;
246
Bill Wendlinga19a5302012-10-14 04:10:01 +0000247 return *this;
248}
249
Bill Wendling49f60602013-01-28 05:23:28 +0000250AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
251 uint64_t Mask = Attr.Raw();
252
253 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
254 I = Attribute::AttrKind(I + 1))
255 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
256 Attrs.insert(I);
257
258 if (Attr.getAlignment())
259 Alignment = Attr.getAlignment();
260 if (Attr.getStackAlignment())
261 StackAlignment = Attr.getStackAlignment();
262 return *this;
263}
264
265AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
266 uint64_t Mask = A.Raw();
267
268 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
269 I = Attribute::AttrKind(I + 1)) {
270 if (Mask & AttributeImpl::getAttrMask(I)) {
271 Attrs.erase(I);
272
273 if (I == Attribute::Alignment)
274 Alignment = 0;
275 else if (I == Attribute::StackAlignment)
276 StackAlignment = 0;
277 }
278 }
279
280 return *this;
281}
282
Bill Wendling702cc912012-10-15 20:35:56 +0000283AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000284 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000285
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000286 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
287 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000288
289 Attrs.insert(Attribute::Alignment);
290 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000291 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000292}
293
Bill Wendling03198882013-01-04 23:27:34 +0000294AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
295 // Default alignment, allow the target to define how to align it.
296 if (Align == 0) return *this;
297
298 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299 assert(Align <= 0x100 && "Alignment too large.");
300
301 Attrs.insert(Attribute::StackAlignment);
302 StackAlignment = Align;
303 return *this;
304}
305
306AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
307 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
308 I = Attribute::AttrKind(I + 1)) {
309 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
310 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000311
Bill Wendling03198882013-01-04 23:27:34 +0000312 if (I == Attribute::Alignment)
313 Alignment = 1ULL << ((A >> 16) - 1);
314 else if (I == Attribute::StackAlignment)
315 StackAlignment = 1ULL << ((A >> 26)-1);
316 }
317 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000318
Bill Wendling3a106e62012-10-09 19:01:18 +0000319 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000320}
321
Bill Wendling22bd6412013-01-03 01:54:39 +0000322bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000323 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000324}
325
Bill Wendling702cc912012-10-15 20:35:56 +0000326bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000327 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000328}
Bill Wendling60507d52013-01-04 20:54:35 +0000329
Bill Wendling034b94b2012-12-19 07:18:57 +0000330bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000331 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000332}
Bill Wendling60507d52013-01-04 20:54:35 +0000333
Bill Wendling702cc912012-10-15 20:35:56 +0000334bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000335 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000336}
337
Bill Wendling1db9b692013-01-09 23:36:50 +0000338uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000339 uint64_t Mask = 0;
340
341 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
342 E = Attrs.end(); I != E; ++I) {
343 Attribute::AttrKind Kind = *I;
344
345 if (Kind == Attribute::Alignment)
346 Mask |= (Log2_32(Alignment) + 1) << 16;
347 else if (Kind == Attribute::StackAlignment)
348 Mask |= (Log2_32(StackAlignment) + 1) << 26;
349 else
350 Mask |= AttributeImpl::getAttrMask(Kind);
351 }
352
353 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000354}
355
Bill Wendling03198882013-01-04 23:27:34 +0000356bool AttrBuilder::operator==(const AttrBuilder &B) {
357 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
358 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
359 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000360}
361
Chris Lattner58d74912008-03-12 17:45:29 +0000362//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000363// AttributeImpl Definition
364//===----------------------------------------------------------------------===//
365
Bill Wendling1bbd6442013-01-05 01:36:54 +0000366AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
367 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000368 Data = ConstantInt::get(Type::getInt64Ty(C), data);
369}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000370AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
371 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000372 Data = ConstantInt::get(Type::getInt64Ty(C), data);
373}
374AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000375 ArrayRef<Constant*> values)
376 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000377 Data = ConstantInt::get(Type::getInt64Ty(C), data);
378 Vals.reserve(values.size());
379 Vals.append(values.begin(), values.end());
380}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000381AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
382 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000383 Data = ConstantDataArray::getString(C, data);
384}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000385
Bill Wendling60507d52013-01-04 20:54:35 +0000386bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000387 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
388 return CI->getZExtValue() == Kind;
389 return false;
390}
Bill Wendling60507d52013-01-04 20:54:35 +0000391bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
392 return !(*this == Kind);
393}
Bill Wendling529ec712012-12-30 01:38:39 +0000394
Bill Wendling60507d52013-01-04 20:54:35 +0000395bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000396 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
397 if (CDA->isString())
398 return CDA->getAsString() == Kind;
399 return false;
400}
Bill Wendling3467e302013-01-24 00:06:56 +0000401
Bill Wendling60507d52013-01-04 20:54:35 +0000402bool AttributeImpl::operator!=(StringRef Kind) const {
403 return !(*this == Kind);
404}
Bill Wendling529ec712012-12-30 01:38:39 +0000405
Bill Wendling3467e302013-01-24 00:06:56 +0000406bool AttributeImpl::operator<(const AttributeImpl &AI) const {
407 if (!Data && !AI.Data) return false;
408 if (!Data && AI.Data) return true;
409 if (Data && !AI.Data) return false;
410
411 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
412 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
413
414 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
415 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
416
417 if (ThisCI && ThatCI)
418 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
419
420 if (ThisCI && ThatCDA)
421 return true;
422
423 if (ThisCDA && ThatCI)
424 return false;
425
426 return ThisCDA->getAsString() < ThatCDA->getAsString();
427}
428
Bill Wendling1db9b692013-01-09 23:36:50 +0000429uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000430 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000431 return cast<ConstantInt>(Data)->getZExtValue();
432}
433
Bill Wendling60507d52013-01-04 20:54:35 +0000434uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000435 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000436 case Attribute::EndAttrKinds:
437 case Attribute::AttrKindEmptyKey:
438 case Attribute::AttrKindTombstoneKey:
439 llvm_unreachable("Synthetic enumerators which should never get here");
440
Bill Wendling034b94b2012-12-19 07:18:57 +0000441 case Attribute::None: return 0;
442 case Attribute::ZExt: return 1 << 0;
443 case Attribute::SExt: return 1 << 1;
444 case Attribute::NoReturn: return 1 << 2;
445 case Attribute::InReg: return 1 << 3;
446 case Attribute::StructRet: return 1 << 4;
447 case Attribute::NoUnwind: return 1 << 5;
448 case Attribute::NoAlias: return 1 << 6;
449 case Attribute::ByVal: return 1 << 7;
450 case Attribute::Nest: return 1 << 8;
451 case Attribute::ReadNone: return 1 << 9;
452 case Attribute::ReadOnly: return 1 << 10;
453 case Attribute::NoInline: return 1 << 11;
454 case Attribute::AlwaysInline: return 1 << 12;
455 case Attribute::OptimizeForSize: return 1 << 13;
456 case Attribute::StackProtect: return 1 << 14;
457 case Attribute::StackProtectReq: return 1 << 15;
458 case Attribute::Alignment: return 31 << 16;
459 case Attribute::NoCapture: return 1 << 21;
460 case Attribute::NoRedZone: return 1 << 22;
461 case Attribute::NoImplicitFloat: return 1 << 23;
462 case Attribute::Naked: return 1 << 24;
463 case Attribute::InlineHint: return 1 << 25;
464 case Attribute::StackAlignment: return 7 << 26;
465 case Attribute::ReturnsTwice: return 1 << 29;
466 case Attribute::UWTable: return 1 << 30;
467 case Attribute::NonLazyBind: return 1U << 31;
468 case Attribute::AddressSafety: return 1ULL << 32;
469 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000470 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000471 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000472 }
473 llvm_unreachable("Unsupported attribute type");
474}
475
Bill Wendling60507d52013-01-04 20:54:35 +0000476bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000477 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000478}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000479
Bill Wendlingf6670722012-12-20 01:36:59 +0000480bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000481 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000482}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000483
Bill Wendlingf6670722012-12-20 01:36:59 +0000484uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000485 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000486 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000487}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000488
Bill Wendlingf6670722012-12-20 01:36:59 +0000489uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000490 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000491 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000492}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000493
Bill Wendling8456efb2013-01-09 00:32:55 +0000494void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
495 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000496 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000497#if 0
498 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000499 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
500 I != E; ++I)
501 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000502#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000503}
504
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000505//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000506// AttributeSetNode Definition
507//===----------------------------------------------------------------------===//
508
509AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
510 ArrayRef<Attribute> Attrs) {
511 if (Attrs.empty())
512 return 0;
513
514 // Otherwise, build a key to look up the existing attributes.
515 LLVMContextImpl *pImpl = C.pImpl;
516 FoldingSetNodeID ID;
517
518 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
519 std::sort(SortedAttrs.begin(), SortedAttrs.end());
520
521 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
522 E = SortedAttrs.end(); I != E; ++I)
523 I->Profile(ID);
524
525 void *InsertPoint;
526 AttributeSetNode *PA =
527 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
528
529 // If we didn't find any existing attributes of the same shape then create a
530 // new one and insert it.
531 if (!PA) {
532 PA = new AttributeSetNode(SortedAttrs);
533 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
534 }
535
536 // Return the AttributesListNode that we found or created.
537 return PA;
538}
539
540//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000541// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000542//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000543
Bill Wendlingd05204a2013-01-27 23:41:29 +0000544uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
545 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
546 if (getSlotIndex(I) != Index) continue;
547 const AttributeSetNode *ASN = AttrNodes[I].second;
548 AttrBuilder B;
549
550 for (AttributeSetNode::const_iterator II = ASN->begin(),
551 IE = ASN->end(); II != IE; ++II)
552 B.addAttributes(*II);
Bill Wendlingd05204a2013-01-27 23:41:29 +0000553 return B.Raw();
554 }
555
556 return 0;
557}
558
Bill Wendling6a325cc2013-01-27 12:50:02 +0000559//===----------------------------------------------------------------------===//
560// AttributeSet Method Implementations
561//===----------------------------------------------------------------------===//
562
Bill Wendling28d65722013-01-23 06:14:59 +0000563AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
564 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000565 return pImpl && hasAttributes(Idx) ?
566 AttributeSet::get(pImpl->getContext(),
Bill Wendling87e10df2013-01-28 21:55:20 +0000567 ArrayRef<std::pair<uint64_t, Attribute> >(
568 std::make_pair(Idx, getAttributes(Idx)))) :
Bill Wendling28d65722013-01-23 06:14:59 +0000569 AttributeSet();
570}
571
Bill Wendling3fc4b962013-01-21 22:44:49 +0000572AttributeSet AttributeSet::getRetAttributes() const {
573 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000574 return pImpl && hasAttributes(ReturnIndex) ?
575 AttributeSet::get(pImpl->getContext(),
Bill Wendling87e10df2013-01-28 21:55:20 +0000576 ArrayRef<std::pair<uint64_t, Attribute> >(
577 std::make_pair(ReturnIndex,
578 getAttributes(ReturnIndex)))) :
Bill Wendling3fc4b962013-01-21 22:44:49 +0000579 AttributeSet();
580}
581
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000582AttributeSet AttributeSet::getFnAttributes() const {
583 // FIXME: Remove.
Bill Wendlingec258982013-01-27 21:23:46 +0000584 return pImpl && hasAttributes(FunctionIndex) ?
585 AttributeSet::get(pImpl->getContext(),
Bill Wendling87e10df2013-01-28 21:55:20 +0000586 ArrayRef<std::pair<uint64_t, Attribute> >(
587 std::make_pair(FunctionIndex,
588 getAttributes(FunctionIndex)))) :
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000589 AttributeSet();
590}
591
Bill Wendling87e10df2013-01-28 21:55:20 +0000592AttributeSet AttributeSet::getImpl(LLVMContext &C,
593 ArrayRef<std::pair<uint64_t,
594 AttributeSetNode*> > Attrs) {
Bill Wendling0976e002012-11-20 05:09:20 +0000595 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000596 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000597 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000598
Bill Wendling0976e002012-11-20 05:09:20 +0000599 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000600 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000601
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000602 // If we didn't find any existing attributes of the same shape then
603 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000604 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000605 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000606 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000607 }
Bill Wendling77898682012-10-16 06:01:44 +0000608
Devang Patel05988662008-09-25 21:00:45 +0000609 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000610 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000611}
612
Bill Wendling87e10df2013-01-28 21:55:20 +0000613AttributeSet AttributeSet::get(LLVMContext &C,
614 ArrayRef<std::pair<uint64_t, Attribute> > Attrs){
615 // If there are no attributes then return a null AttributesList pointer.
616 if (Attrs.empty())
617 return AttributeSet();
618
619#ifndef NDEBUG
620 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
621 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
622 "Misordered Attributes list!");
623 assert(Attrs[i].second.hasAttributes() &&
624 "Pointless attribute!");
625 }
626#endif
627
628 // Create a vector if (uint64_t, AttributeSetNode*) pairs from the attributes
629 // list.
630 SmallVector<std::pair<uint64_t, AttributeSetNode*>, 8> AttrPairVec;
631 for (ArrayRef<std::pair<uint64_t, Attribute> >::iterator I = Attrs.begin(),
632 E = Attrs.end(); I != E; ) {
633 uint64_t Index = I->first;
634 SmallVector<Attribute, 4> AttrVec;
635 while (I->first == Index && I != E) {
636 AttrVec.push_back(I->second);
637 ++I;
638 }
639
640 AttrPairVec.push_back(std::make_pair(Index,
641 AttributeSetNode::get(C, AttrVec)));
642 }
643
644 return getImpl(C, AttrPairVec);
645}
646
647AttributeSet AttributeSet::get(LLVMContext &C,
648 ArrayRef<std::pair<uint64_t,
649 AttributeSetNode*> > Attrs) {
650 // If there are no attributes then return a null AttributesList pointer.
651 if (Attrs.empty())
652 return AttributeSet();
653
654 return getImpl(C, Attrs);
655}
656
Bill Wendling1bbd6442013-01-05 01:36:54 +0000657AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000658 if (!B.hasAttributes())
659 return AttributeSet();
Bill Wendling87e10df2013-01-28 21:55:20 +0000660 return get(C, ArrayRef<std::pair<uint64_t, Attribute> >(
661 std::make_pair(Idx, Attribute::get(C, B))));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000662}
663
Bill Wendling28d65722013-01-23 06:14:59 +0000664AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling32a57952013-01-26 00:03:11 +0000665 ArrayRef<Attribute::AttrKind> Kind) {
Bill Wendling87e10df2013-01-28 21:55:20 +0000666 SmallVector<std::pair<uint64_t, Attribute>, 8> Attrs;
Bill Wendling32a57952013-01-26 00:03:11 +0000667 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
668 E = Kind.end(); I != E; ++I)
Bill Wendling87e10df2013-01-28 21:55:20 +0000669 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
670 return get(C, Attrs);
Bill Wendling28d65722013-01-23 06:14:59 +0000671}
672
Bill Wendling8e47daf2013-01-25 23:09:36 +0000673AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
Bill Wendling87e10df2013-01-28 21:55:20 +0000674 SmallVector<std::pair<uint64_t, AttributeSetNode*>, 8> AttrNodeVec;
675 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
676 AttributeSet AS = Attrs[I];
Bill Wendlingec258982013-01-27 21:23:46 +0000677 if (!AS.pImpl) continue;
Bill Wendling87e10df2013-01-28 21:55:20 +0000678 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
Bill Wendling8e47daf2013-01-25 23:09:36 +0000679 }
680
Bill Wendling87e10df2013-01-28 21:55:20 +0000681 return get(C, AttrNodeVec);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000682}
683
Bill Wendlingd05204a2013-01-27 23:41:29 +0000684/// \brief Return the number of slots used in this attribute list. This is the
685/// number of arguments that have an attribute set on them (including the
686/// function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000687unsigned AttributeSet::getNumSlots() const {
Bill Wendlingec258982013-01-27 21:23:46 +0000688 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000689}
690
Bill Wendling3e3e7892013-01-27 23:50:44 +0000691uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000692 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000693 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000694 return pImpl->getSlotIndex(Slot);
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000695}
696
Bill Wendling8e47daf2013-01-25 23:09:36 +0000697AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000698 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling8e47daf2013-01-25 23:09:36 +0000699 "Slot # out of range!");
Bill Wendlingec258982013-01-27 21:23:46 +0000700 return pImpl->getSlotAttributes(Slot);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000701}
702
Bill Wendling19d815c2013-01-28 05:51:40 +0000703bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling831737d2012-12-30 10:32:01 +0000704 return getAttributes(Index).hasAttribute(Kind);
705}
706
Bill Wendling19d815c2013-01-28 05:51:40 +0000707bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000708 return getAttributes(Index).hasAttributes();
709}
710
Bill Wendling19d815c2013-01-28 05:51:40 +0000711std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000712 return getAttributes(Index).getAsString();
713}
714
Bill Wendling19d815c2013-01-28 05:51:40 +0000715unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
Bill Wendling956f1342013-01-18 21:11:39 +0000716 return getAttributes(Idx).getAlignment();
717}
718
Bill Wendling19d815c2013-01-28 05:51:40 +0000719unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000720 return getAttributes(Index).getStackAlignment();
721}
722
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000723uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000724 // FIXME: Remove this.
Bill Wendlingd05204a2013-01-27 23:41:29 +0000725 return pImpl ? pImpl->Raw(Index) : 0;
Bill Wendling831737d2012-12-30 10:32:01 +0000726}
727
Bill Wendlinge2501f52013-01-28 01:11:42 +0000728/// \brief The attributes for the specified index are returned.
729///
730/// FIXME: This shouldn't return 'Attribute'.
NAKAMURA Takumi53ff78b2013-01-28 04:29:01 +0000731Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000732 if (pImpl == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000733
Bill Wendlinge2501f52013-01-28 01:11:42 +0000734 // Loop through to find the attribute we want.
735 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
736 if (pImpl->getSlotIndex(I) != Idx) continue;
737
738 AttrBuilder B;
739 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
740 IE = pImpl->end(I); II != IE; ++II)
741 B.addAttributes(*II);
742 return Attribute::get(pImpl->getContext(), B);
743 }
Bill Wendlingef99fe82012-09-21 15:26:31 +0000744
Bill Wendling034b94b2012-12-19 07:18:57 +0000745 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000746}
747
748/// hasAttrSomewhere - Return true if the specified attribute is set for at
749/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000750bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendlingec258982013-01-27 21:23:46 +0000751 if (pImpl == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000752
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000753 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
Bill Wendlinge2501f52013-01-28 01:11:42 +0000754 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling70cdaaa2013-01-28 00:46:02 +0000755 IE = pImpl->end(I); II != IE; ++II)
756 if (II->hasAttribute(Attr))
757 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000758
Chris Lattner58d74912008-03-12 17:45:29 +0000759 return false;
760}
761
Bill Wendlingdefaca02013-01-22 21:15:51 +0000762AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
763 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000764 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendlingdefaca02013-01-22 21:15:51 +0000765}
766
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000767AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
768 AttributeSet Attrs) const {
Bill Wendling49f60602013-01-28 05:23:28 +0000769 if (!pImpl) return Attrs;
770 if (!Attrs.pImpl) return *this;
771
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000772#ifndef NDEBUG
Bill Wendling49f60602013-01-28 05:23:28 +0000773 // FIXME it is not obvious how this should work for alignment. For now, say
774 // we can't change a known alignment.
775 unsigned OldAlign = getParamAlignment(Idx);
776 unsigned NewAlign = Attrs.getParamAlignment(Idx);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000777 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000778 "Attempt to change alignment!");
779#endif
Bill Wendling77898682012-10-16 06:01:44 +0000780
Bill Wendling49f60602013-01-28 05:23:28 +0000781 // Add the attribute slots before the one we're trying to add.
782 SmallVector<AttributeSet, 4> AttrSet;
783 uint64_t NumAttrs = pImpl->getNumAttributes();
784 AttributeSet AS;
785 uint64_t LastIndex = 0;
786 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
787 if (getSlotIndex(I) >= Idx) {
788 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
789 break;
Chris Lattner58d74912008-03-12 17:45:29 +0000790 }
Bill Wendling49f60602013-01-28 05:23:28 +0000791 LastIndex = I + 1;
792 AttrSet.push_back(getSlotAttributes(I));
Chris Lattner58d74912008-03-12 17:45:29 +0000793 }
Bill Wendling77898682012-10-16 06:01:44 +0000794
Bill Wendling49f60602013-01-28 05:23:28 +0000795 // Now add the attribute into the correct slot. There may already be an
796 // AttributeSet there.
797 AttrBuilder B(AS, Idx);
798
799 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
800 if (Attrs.getSlotIndex(I) == Idx) {
801 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
802 IE = Attrs.pImpl->end(I); II != IE; ++II)
803 B.addAttributes(*II);
804 break;
805 }
806
807 AttrSet.push_back(AttributeSet::get(C, Idx, B));
808
809 // Add the remaining attribute slots.
810 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
811 AttrSet.push_back(getSlotAttributes(I));
812
813 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000814}
815
Bill Wendling8246df62013-01-23 00:45:55 +0000816AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
817 Attribute::AttrKind Attr) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000818 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
Bill Wendling8246df62013-01-23 00:45:55 +0000819}
820
821AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
822 AttributeSet Attrs) const {
Bill Wendling98b92f32013-01-28 05:44:14 +0000823 if (!pImpl) return AttributeSet();
824 if (!Attrs.pImpl) return *this;
Bill Wendling8246df62013-01-23 00:45:55 +0000825
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000826#ifndef NDEBUG
827 // FIXME it is not obvious how this should work for alignment.
828 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling98b92f32013-01-28 05:44:14 +0000829 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
830 "Attempt to change alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000831#endif
Bill Wendling77898682012-10-16 06:01:44 +0000832
Bill Wendling98b92f32013-01-28 05:44:14 +0000833 // Add the attribute slots before the one we're trying to add.
834 SmallVector<AttributeSet, 4> AttrSet;
835 uint64_t NumAttrs = pImpl->getNumAttributes();
836 AttributeSet AS;
837 uint64_t LastIndex = 0;
838 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
839 if (getSlotIndex(I) >= Idx) {
840 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
841 break;
842 }
843 LastIndex = I + 1;
844 AttrSet.push_back(getSlotAttributes(I));
845 }
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000846
Bill Wendling98b92f32013-01-28 05:44:14 +0000847 // Now add the attribute into the correct slot. There may already be an
848 // AttributeSet there.
849 AttrBuilder B(AS, Idx);
Bill Wendling77898682012-10-16 06:01:44 +0000850
Bill Wendling98b92f32013-01-28 05:44:14 +0000851 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
852 if (Attrs.getSlotIndex(I) == Idx) {
853 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
854 IE = Attrs.pImpl->end(I); II != IE; ++II)
855 B.removeAttributes(*II);
856 break;
857 }
Bill Wendling77898682012-10-16 06:01:44 +0000858
Bill Wendling98b92f32013-01-28 05:44:14 +0000859 AttrSet.push_back(AttributeSet::get(C, Idx, B));
Bill Wendling77898682012-10-16 06:01:44 +0000860
Bill Wendling98b92f32013-01-28 05:44:14 +0000861 // Add the remaining attribute slots.
862 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
863 AttrSet.push_back(getSlotAttributes(I));
Bill Wendling77898682012-10-16 06:01:44 +0000864
Bill Wendling98b92f32013-01-28 05:44:14 +0000865 return get(C, AttrSet);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000866}
867
Bill Wendling99faa3b2012-12-07 23:16:57 +0000868void AttributeSet::dump() const {
Bill Wendling73bc4522013-01-28 00:21:34 +0000869 dbgs() << "PAL[\n";
870 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
Bill Wendling49716e52013-01-27 23:53:56 +0000871 uint64_t Index = getSlotIndex(i);
872 dbgs() << " { ";
873 if (Index == ~0U)
874 dbgs() << "~0U";
875 else
876 dbgs() << Index;
877 dbgs() << " => " << getAsString(Index) << " }\n";
Chris Lattner58d74912008-03-12 17:45:29 +0000878 }
Bill Wendling77898682012-10-16 06:01:44 +0000879
David Greeneef1894e2010-01-05 01:29:58 +0000880 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000881}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000882
883//===----------------------------------------------------------------------===//
884// AttributeFuncs Function Defintions
885//===----------------------------------------------------------------------===//
886
887Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
888 AttrBuilder Incompatible;
889
890 if (!Ty->isIntegerTy())
891 // Attribute that only apply to integers.
892 Incompatible.addAttribute(Attribute::SExt)
893 .addAttribute(Attribute::ZExt);
894
895 if (!Ty->isPointerTy())
896 // Attribute that only apply to pointers.
897 Incompatible.addAttribute(Attribute::ByVal)
898 .addAttribute(Attribute::Nest)
899 .addAttribute(Attribute::NoAlias)
900 .addAttribute(Attribute::NoCapture)
901 .addAttribute(Attribute::StructRet);
902
903 return Attribute::get(Ty->getContext(), Incompatible);
904}
905
906/// encodeLLVMAttributesForBitcode - This returns an integer containing an
907/// encoding of all the LLVM attributes found in the given attribute bitset.
908/// Any change to this encoding is a breaking change to bitcode compatibility.
909uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
910 unsigned Index) {
911 // FIXME: It doesn't make sense to store the alignment information as an
912 // expanded out value, we should store it as a log2 value. However, we can't
913 // just change that here without breaking bitcode compatibility. If this ever
914 // becomes a problem in practice, we should introduce new tag numbers in the
915 // bitcode file and have those tags use a more efficiently encoded alignment
916 // field.
917
918 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
919 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
920 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
921 if (Attrs.hasAttribute(Index, Attribute::Alignment))
922 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
923 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
924 return EncodedAttrs;
925}
926
927/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
928/// the LLVM attributes that have been decoded from the given integer. This
929/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
930Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
931 uint64_t EncodedAttrs){
932 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
933 // the bits above 31 down by 11 bits.
934 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
935 assert((!Alignment || isPowerOf2_32(Alignment)) &&
936 "Alignment must be a power of two.");
937
938 AttrBuilder B(EncodedAttrs & 0xffff);
939 if (Alignment)
940 B.addAlignmentAttr(Alignment);
941 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
942 return Attribute::get(C, B);
943}
944