blob: 8e64a49c47a6a7cddc4a66139821cc84e7a85925 [file] [log] [blame]
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001//===-- Attribute.cpp - Implement AttributesList -------------------------===//
Chris Lattner3e13b8c2008-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 Wendling3d7b0b82012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling6848e382012-12-19 22:42:22 +000011// AttributeSetImpl, and AttributeSet classes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Attributes.h"
Bill Wendling4607f4b2012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greenef7014732010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendlingd2e493b2013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner8a923e72008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling3d7b0b82012-12-19 07:18:57 +000030// Attribute Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000032
Bill Wendlingf5c1f552013-01-27 10:28:39 +000033Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Kinds) {
34 AttrBuilder B;
35 for (ArrayRef<AttrKind>::iterator I = Kinds.begin(), E = Kinds.end();
36 I != E; ++I)
37 B.addAttribute(*I);
Bill Wendling3d7b0b82012-12-19 07:18:57 +000038 return Attribute::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000039}
Bill Wendling73ea2de2012-10-08 21:47:17 +000040
Bill Wendling3d7b0b82012-12-19 07:18:57 +000041Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
Bill Wendling3ffbac42012-10-16 05:55:09 +000043 if (!B.hasAttributes())
Bill Wendling3d7b0b82012-12-19 07:18:57 +000044 return Attribute();
Bill Wendling73ea2de2012-10-08 21:47:17 +000045
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendling3b651872013-01-09 23:36:50 +000049 ID.AddInteger(B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendling4607f4b2012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling73ea2de2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendling3b651872013-01-09 23:36:50 +000057 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling73ea2de2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling73ea2de2012-10-08 21:47:17 +000063}
64
Bill Wendling4bbe9db2013-01-27 22:43:04 +000065Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
66 AttrBuilder B;
67 return get(Context, B.addAlignmentAttr(Align));
68}
69
70Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
71 uint64_t Align) {
72 AttrBuilder B;
73 return get(Context, B.addStackAlignmentAttr(Align));
74}
75
Bill Wendlingc79e42c2012-12-22 00:37:52 +000076bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling66e978f2012-12-20 21:28:43 +000077 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000078}
79
Bill Wendling3d7b0b82012-12-19 07:18:57 +000080bool Attribute::hasAttributes() const {
Bill Wendling66e978f2012-12-20 21:28:43 +000081 return pImpl && pImpl->hasAttributes();
Bill Wendling8c3e65d2012-10-15 05:40:12 +000082}
83
Bill Wendlingabf3feb2012-10-05 06:44:41 +000084/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000085unsigned Attribute::getAlignment() const {
86 if (!hasAttribute(Attribute::Alignment))
Bill Wendlingb3723342012-10-09 20:56:48 +000087 return 0;
Bill Wendling6442a972013-01-23 23:00:05 +000088 return pImpl->getAlignment();
Bill Wendlingabf3feb2012-10-05 06:44:41 +000089}
90
91/// This returns the stack alignment field of an attribute as a byte alignment
92/// value.
Bill Wendling3d7b0b82012-12-19 07:18:57 +000093unsigned Attribute::getStackAlignment() const {
94 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendlingb3723342012-10-09 20:56:48 +000095 return 0;
Bill Wendling6442a972013-01-23 23:00:05 +000096 return pImpl->getStackAlignment();
Bill Wendlingabf3feb2012-10-05 06:44:41 +000097}
98
Bill Wendlinge10f76c2012-12-31 11:51:54 +000099bool Attribute::operator==(AttrKind K) const {
Bill Wendling9ac69f92013-01-04 20:54:35 +0000100 return pImpl && *pImpl == K;
Bill Wendlinge10f76c2012-12-31 11:51:54 +0000101}
Bill Wendlinge10f76c2012-12-31 11:51:54 +0000102bool Attribute::operator!=(AttrKind K) const {
Bill Wendling9ac69f92013-01-04 20:54:35 +0000103 return !(*this == K);
Bill Wendlinge10f76c2012-12-31 11:51:54 +0000104}
105
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000106bool Attribute::operator<(Attribute A) const {
107 if (!pImpl && !A.pImpl) return false;
108 if (!pImpl) return true;
109 if (!A.pImpl) return false;
110 return *pImpl < *A.pImpl;
111}
112
Bill Wendling3b651872013-01-09 23:36:50 +0000113uint64_t Attribute::Raw() const {
114 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000115}
116
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000117std::string Attribute::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000118 std::string Result;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000120 Result += "zeroext ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000122 Result += "signext ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124 Result += "noreturn ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000126 Result += "nounwind ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000128 Result += "uwtable ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000130 Result += "returns_twice ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132 Result += "inreg ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134 Result += "noalias ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000136 Result += "nocapture ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000138 Result += "sret ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 Result += "byval ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "nest ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000143 if (hasAttribute(Attribute::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "readnone ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "readonly ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000148 Result += "optsize ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000150 Result += "noinline ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000152 Result += "inlinehint ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000154 Result += "alwaysinline ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000156 Result += "ssp ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000158 Result += "sspreq ";
Bill Wendlingd154e2832013-01-23 06:41:41 +0000159 if (hasAttribute(Attribute::StackProtectStrong))
160 Result += "sspstrong ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000161 if (hasAttribute(Attribute::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000162 Result += "noredzone ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000163 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000164 Result += "noimplicitfloat ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000165 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000166 Result += "naked ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000168 Result += "nonlazybind ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000169 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000170 Result += "address_safety ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000171 if (hasAttribute(Attribute::MinSize))
Quentin Colombet5799e9f2012-10-30 16:32:52 +0000172 Result += "minsize ";
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000173 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000174 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000175 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000176 Result += ") ";
177 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000178 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000179 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000180 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000181 Result += " ";
182 }
James Molloy4f6fb952012-12-20 16:04:27 +0000183 if (hasAttribute(Attribute::NoDuplicate))
184 Result += "noduplicate ";
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000185 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000186 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000187 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000188 return Result;
189}
190
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000191//===----------------------------------------------------------------------===//
Bill Wendlingcd330342013-01-04 23:27:34 +0000192// AttrBuilder Method Implementations
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000193//===----------------------------------------------------------------------===//
194
Bill Wendling096f5442013-01-07 08:24:35 +0000195AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
196 : Alignment(0), StackAlignment(0) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000197 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendling096f5442013-01-07 08:24:35 +0000198 if (!pImpl) return;
199
200 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
201 const AttributeWithIndex *AWI = 0;
202 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
203 if (AttrList[I].Index == Idx) {
204 AWI = &AttrList[I];
205 break;
206 }
207
Bill Wendling7df048e2013-01-18 21:56:07 +0000208 if (!AWI) return;
Bill Wendling096f5442013-01-07 08:24:35 +0000209
Bill Wendling77543892013-01-18 21:11:39 +0000210 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendling096f5442013-01-07 08:24:35 +0000211
Bill Wendling77543892013-01-18 21:11:39 +0000212 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
213 I = Attribute::AttrKind(I + 1)) {
214 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
215 Attrs.insert(I);
216
217 if (I == Attribute::Alignment)
218 Alignment = 1ULL << ((A >> 16) - 1);
219 else if (I == Attribute::StackAlignment)
220 StackAlignment = 1ULL << ((A >> 26)-1);
221 }
222 }
Bill Wendling096f5442013-01-07 08:24:35 +0000223}
224
Bill Wendlingcd330342013-01-04 23:27:34 +0000225void AttrBuilder::clear() {
226 Attrs.clear();
227 Alignment = StackAlignment = 0;
228}
229
230AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
231 Attrs.insert(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000232 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000233}
234
Bill Wendlingcd330342013-01-04 23:27:34 +0000235AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
236 Attrs.erase(Val);
237 if (Val == Attribute::Alignment)
238 Alignment = 0;
239 else if (Val == Attribute::StackAlignment)
240 StackAlignment = 0;
241
Bill Wendling1fcc8222012-10-14 04:10:01 +0000242 return *this;
243}
244
Bill Wendling50d27842012-10-15 20:35:56 +0000245AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000246 if (Align == 0) return *this;
Bill Wendlingcd330342013-01-04 23:27:34 +0000247
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000248 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
249 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendlingcd330342013-01-04 23:27:34 +0000250
251 Attrs.insert(Attribute::Alignment);
252 Alignment = Align;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000253 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000254}
255
Bill Wendlingcd330342013-01-04 23:27:34 +0000256AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
257 // Default alignment, allow the target to define how to align it.
258 if (Align == 0) return *this;
259
260 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
261 assert(Align <= 0x100 && "Alignment too large.");
262
263 Attrs.insert(Attribute::StackAlignment);
264 StackAlignment = Align;
265 return *this;
266}
267
268AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
269 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
270 I = Attribute::AttrKind(I + 1)) {
271 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
272 Attrs.insert(I);
Bill Wendling09175b32013-01-22 21:15:51 +0000273
Bill Wendlingcd330342013-01-04 23:27:34 +0000274 if (I == Attribute::Alignment)
275 Alignment = 1ULL << ((A >> 16) - 1);
276 else if (I == Attribute::StackAlignment)
277 StackAlignment = 1ULL << ((A >> 26)-1);
278 }
279 }
Bill Wendling09175b32013-01-22 21:15:51 +0000280
Bill Wendling7c04e042012-10-09 19:01:18 +0000281 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000282}
283
Bill Wendling09175b32013-01-22 21:15:51 +0000284AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
285 uint64_t Mask = Attr.Raw();
Bill Wendlingcd330342013-01-04 23:27:34 +0000286
287 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendling09175b32013-01-22 21:15:51 +0000288 I = Attribute::AttrKind(I + 1))
289 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendlingcd330342013-01-04 23:27:34 +0000290 Attrs.insert(I);
291
Bill Wendling09175b32013-01-22 21:15:51 +0000292 if (Attr.getAlignment())
293 Alignment = Attr.getAlignment();
294 if (Attr.getStackAlignment())
295 StackAlignment = Attr.getStackAlignment();
Bill Wendling5c407ed2012-10-14 07:17:34 +0000296 return *this;
297}
298
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000299AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling3b651872013-01-09 23:36:50 +0000300 uint64_t Mask = A.Raw();
Bill Wendlingcd330342013-01-04 23:27:34 +0000301
302 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
303 I = Attribute::AttrKind(I + 1)) {
304 if (Mask & AttributeImpl::getAttrMask(I)) {
305 Attrs.erase(I);
306
307 if (I == Attribute::Alignment)
308 Alignment = 0;
309 else if (I == Attribute::StackAlignment)
310 StackAlignment = 0;
311 }
312 }
313
Bill Wendling85a64c22012-10-14 06:39:53 +0000314 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000315}
316
Bill Wendling04949fa92013-01-03 01:54:39 +0000317bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendlingcd330342013-01-04 23:27:34 +0000318 return Attrs.count(A);
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000319}
320
Bill Wendling50d27842012-10-15 20:35:56 +0000321bool AttrBuilder::hasAttributes() const {
Bill Wendlingcd330342013-01-04 23:27:34 +0000322 return !Attrs.empty();
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000323}
Bill Wendling9ac69f92013-01-04 20:54:35 +0000324
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000325bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling3b651872013-01-09 23:36:50 +0000326 return Raw() & A.Raw();
Bill Wendling70f39172012-10-09 00:01:21 +0000327}
Bill Wendling9ac69f92013-01-04 20:54:35 +0000328
Bill Wendling50d27842012-10-15 20:35:56 +0000329bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendlingcd330342013-01-04 23:27:34 +0000330 return Alignment != 0;
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000331}
332
Bill Wendling3b651872013-01-09 23:36:50 +0000333uint64_t AttrBuilder::Raw() const {
Bill Wendlingcd330342013-01-04 23:27:34 +0000334 uint64_t Mask = 0;
335
336 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
337 E = Attrs.end(); I != E; ++I) {
338 Attribute::AttrKind Kind = *I;
339
340 if (Kind == Attribute::Alignment)
341 Mask |= (Log2_32(Alignment) + 1) << 16;
342 else if (Kind == Attribute::StackAlignment)
343 Mask |= (Log2_32(StackAlignment) + 1) << 26;
344 else
345 Mask |= AttributeImpl::getAttrMask(Kind);
346 }
347
348 return Mask;
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000349}
350
Bill Wendlingcd330342013-01-04 23:27:34 +0000351bool AttrBuilder::operator==(const AttrBuilder &B) {
352 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
353 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
354 return This == That;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000355}
356
Chris Lattner8a923e72008-03-12 17:45:29 +0000357//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000358// AttributeImpl Definition
359//===----------------------------------------------------------------------===//
360
Bill Wendling960f52a2013-01-05 01:36:54 +0000361AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
362 : Context(C) {
Bill Wendling0cd0f7f2012-12-29 12:29:38 +0000363 Data = ConstantInt::get(Type::getInt64Ty(C), data);
364}
Bill Wendling960f52a2013-01-05 01:36:54 +0000365AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
366 : Context(C) {
Bill Wendling3a554ac2012-12-30 02:22:16 +0000367 Data = ConstantInt::get(Type::getInt64Ty(C), data);
368}
369AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling960f52a2013-01-05 01:36:54 +0000370 ArrayRef<Constant*> values)
371 : Context(C) {
Bill Wendling3a554ac2012-12-30 02:22:16 +0000372 Data = ConstantInt::get(Type::getInt64Ty(C), data);
373 Vals.reserve(values.size());
374 Vals.append(values.begin(), values.end());
375}
Bill Wendling960f52a2013-01-05 01:36:54 +0000376AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
377 : Context(C) {
Bill Wendling3a554ac2012-12-30 02:22:16 +0000378 Data = ConstantDataArray::getString(C, data);
379}
Bill Wendling0cd0f7f2012-12-29 12:29:38 +0000380
Bill Wendling9ac69f92013-01-04 20:54:35 +0000381bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendlingb1d12612012-12-30 01:38:39 +0000382 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
383 return CI->getZExtValue() == Kind;
384 return false;
385}
Bill Wendling9ac69f92013-01-04 20:54:35 +0000386bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
387 return !(*this == Kind);
388}
Bill Wendlingb1d12612012-12-30 01:38:39 +0000389
Bill Wendling9ac69f92013-01-04 20:54:35 +0000390bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendlingb1d12612012-12-30 01:38:39 +0000391 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
392 if (CDA->isString())
393 return CDA->getAsString() == Kind;
394 return false;
395}
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000396
Bill Wendling9ac69f92013-01-04 20:54:35 +0000397bool AttributeImpl::operator!=(StringRef Kind) const {
398 return !(*this == Kind);
399}
Bill Wendlingb1d12612012-12-30 01:38:39 +0000400
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000401bool AttributeImpl::operator<(const AttributeImpl &AI) const {
402 if (!Data && !AI.Data) return false;
403 if (!Data && AI.Data) return true;
404 if (Data && !AI.Data) return false;
405
406 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
407 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
408
409 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
410 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
411
412 if (ThisCI && ThatCI)
413 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
414
415 if (ThisCI && ThatCDA)
416 return true;
417
418 if (ThisCDA && ThatCI)
419 return false;
420
421 return ThisCDA->getAsString() < ThatCDA->getAsString();
422}
423
Bill Wendling3b651872013-01-09 23:36:50 +0000424uint64_t AttributeImpl::Raw() const {
Bill Wendlingb1d12612012-12-30 01:38:39 +0000425 // FIXME: Remove this.
Bill Wendling0cd0f7f2012-12-29 12:29:38 +0000426 return cast<ConstantInt>(Data)->getZExtValue();
427}
428
Bill Wendling9ac69f92013-01-04 20:54:35 +0000429uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000430 switch (Val) {
Chandler Carruth5394e112013-01-05 08:47:26 +0000431 case Attribute::EndAttrKinds:
432 case Attribute::AttrKindEmptyKey:
433 case Attribute::AttrKindTombstoneKey:
434 llvm_unreachable("Synthetic enumerators which should never get here");
435
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000436 case Attribute::None: return 0;
437 case Attribute::ZExt: return 1 << 0;
438 case Attribute::SExt: return 1 << 1;
439 case Attribute::NoReturn: return 1 << 2;
440 case Attribute::InReg: return 1 << 3;
441 case Attribute::StructRet: return 1 << 4;
442 case Attribute::NoUnwind: return 1 << 5;
443 case Attribute::NoAlias: return 1 << 6;
444 case Attribute::ByVal: return 1 << 7;
445 case Attribute::Nest: return 1 << 8;
446 case Attribute::ReadNone: return 1 << 9;
447 case Attribute::ReadOnly: return 1 << 10;
448 case Attribute::NoInline: return 1 << 11;
449 case Attribute::AlwaysInline: return 1 << 12;
450 case Attribute::OptimizeForSize: return 1 << 13;
451 case Attribute::StackProtect: return 1 << 14;
452 case Attribute::StackProtectReq: return 1 << 15;
453 case Attribute::Alignment: return 31 << 16;
454 case Attribute::NoCapture: return 1 << 21;
455 case Attribute::NoRedZone: return 1 << 22;
456 case Attribute::NoImplicitFloat: return 1 << 23;
457 case Attribute::Naked: return 1 << 24;
458 case Attribute::InlineHint: return 1 << 25;
459 case Attribute::StackAlignment: return 7 << 26;
460 case Attribute::ReturnsTwice: return 1 << 29;
461 case Attribute::UWTable: return 1 << 30;
462 case Attribute::NonLazyBind: return 1U << 31;
463 case Attribute::AddressSafety: return 1ULL << 32;
464 case Attribute::MinSize: return 1ULL << 33;
James Molloy4f6fb952012-12-20 16:04:27 +0000465 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendlingd154e2832013-01-23 06:41:41 +0000466 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000467 }
468 llvm_unreachable("Unsupported attribute type");
469}
470
Bill Wendling9ac69f92013-01-04 20:54:35 +0000471bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling3b651872013-01-09 23:36:50 +0000472 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000473}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000474
Bill Wendling4607f4b2012-12-20 01:36:59 +0000475bool AttributeImpl::hasAttributes() const {
Bill Wendling3b651872013-01-09 23:36:50 +0000476 return Raw() != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000477}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000478
Bill Wendling4607f4b2012-12-20 01:36:59 +0000479uint64_t AttributeImpl::getAlignment() const {
Bill Wendling6442a972013-01-23 23:00:05 +0000480 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Kleckner8978e922013-01-25 15:35:56 +0000481 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000482}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000483
Bill Wendling4607f4b2012-12-20 01:36:59 +0000484uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendling6442a972013-01-23 23:00:05 +0000485 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Kleckner8978e922013-01-25 15:35:56 +0000486 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000487}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000488
Bill Wendling0ec3c5c2013-01-09 00:32:55 +0000489void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
490 ArrayRef<Constant*> Vals) {
Bill Wendling69fa5c02013-01-09 00:32:08 +0000491 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling3b651872013-01-09 23:36:50 +0000492#if 0
493 // FIXME: Not yet supported.
Bill Wendling69fa5c02013-01-09 00:32:08 +0000494 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
495 I != E; ++I)
496 ID.AddPointer(*I);
Bill Wendling3b651872013-01-09 23:36:50 +0000497#endif
Bill Wendling69fa5c02013-01-09 00:32:08 +0000498}
499
Bill Wendlinge38b8042012-09-26 21:07:29 +0000500//===----------------------------------------------------------------------===//
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000501// AttributeSetNode Definition
502//===----------------------------------------------------------------------===//
503
504AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
505 ArrayRef<Attribute> Attrs) {
506 if (Attrs.empty())
507 return 0;
508
509 // Otherwise, build a key to look up the existing attributes.
510 LLVMContextImpl *pImpl = C.pImpl;
511 FoldingSetNodeID ID;
512
513 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
514 std::sort(SortedAttrs.begin(), SortedAttrs.end());
515
516 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
517 E = SortedAttrs.end(); I != E; ++I)
518 I->Profile(ID);
519
520 void *InsertPoint;
521 AttributeSetNode *PA =
522 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
523
524 // If we didn't find any existing attributes of the same shape then create a
525 // new one and insert it.
526 if (!PA) {
527 PA = new AttributeSetNode(SortedAttrs);
528 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
529 }
530
531 // Return the AttributesListNode that we found or created.
532 return PA;
533}
534
535//===----------------------------------------------------------------------===//
Bill Wendling6848e382012-12-19 22:42:22 +0000536// AttributeSetImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000537//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000538
Bill Wendling152289c2013-01-27 12:50:02 +0000539AttributeSetImpl::
540AttributeSetImpl(LLVMContext &C,
541 ArrayRef<AttributeWithIndex> attrs)
542 : Context(C), AttrList(attrs.begin(), attrs.end()) {
543 for (unsigned I = 0, E = attrs.size(); I != E; ++I) {
544 const AttributeWithIndex &AWI = attrs[I];
545 uint64_t Mask = AWI.Attrs.Raw();
546 SmallVector<Attribute, 8> Attrs;
547
548 for (Attribute::AttrKind II = Attribute::None;
549 II != Attribute::EndAttrKinds; II = Attribute::AttrKind(II + 1)) {
550 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(II))) {
551 AttrBuilder B;
552
553 if (II == Attribute::Alignment)
554 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
555 else if (II == Attribute::StackAlignment)
556 B.addStackAlignmentAttr(1ULL << ((A >> 26) - 1));
557 else
558 B.addAttribute(II);
559
560 Attrs.push_back(Attribute::get(C, B));
561 }
562 }
563
564 AttrNodes.push_back(std::make_pair(AWI.Index,
565 AttributeSetNode::get(C, Attrs)));
566 }
Bill Wendling152289c2013-01-27 12:50:02 +0000567
Bill Wendling72223f22013-01-27 21:20:06 +0000568 assert(AttrNodes.size() == AttrList.size() &&
569 "Number of attributes is different between lists!");
570#ifndef NDEBUG
571 for (unsigned I = 0, E = AttrNodes.size(); I != E; ++I)
572 assert((I == 0 || AttrNodes[I - 1].first < AttrNodes[I].first) &&
573 "Attributes not in ascending order!");
574#endif
Bill Wendling152289c2013-01-27 12:50:02 +0000575}
576
Bill Wendling1f786a72013-01-27 23:41:29 +0000577uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
578 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
579 if (getSlotIndex(I) != Index) continue;
580 const AttributeSetNode *ASN = AttrNodes[I].second;
581 AttrBuilder B;
582
583 for (AttributeSetNode::const_iterator II = ASN->begin(),
584 IE = ASN->end(); II != IE; ++II)
585 B.addAttributes(*II);
586
587 assert(B.Raw() == AttrList[I].Attrs.Raw() &&
588 "Attributes aren't the same!");
589 return B.Raw();
590 }
591
592 return 0;
593}
594
Bill Wendling152289c2013-01-27 12:50:02 +0000595//===----------------------------------------------------------------------===//
596// AttributeSet Method Implementations
597//===----------------------------------------------------------------------===//
598
Bill Wendling49bc76c2013-01-23 06:14:59 +0000599AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
600 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000601 return pImpl && hasAttributes(Idx) ?
602 AttributeSet::get(pImpl->getContext(),
Bill Wendling49bc76c2013-01-23 06:14:59 +0000603 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
604 AttributeSet();
605}
606
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000607AttributeSet AttributeSet::getRetAttributes() const {
608 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000609 return pImpl && hasAttributes(ReturnIndex) ?
610 AttributeSet::get(pImpl->getContext(),
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000611 AttributeWithIndex::get(ReturnIndex,
612 getAttributes(ReturnIndex))) :
613 AttributeSet();
614}
615
Bill Wendlingbd4ea162013-01-21 21:57:28 +0000616AttributeSet AttributeSet::getFnAttributes() const {
617 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000618 return pImpl && hasAttributes(FunctionIndex) ?
619 AttributeSet::get(pImpl->getContext(),
Bill Wendlingbd4ea162013-01-21 21:57:28 +0000620 AttributeWithIndex::get(FunctionIndex,
621 getAttributes(FunctionIndex))) :
622 AttributeSet();
623}
624
Bill Wendlinge94d8432012-12-07 23:16:57 +0000625AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling623d8ce2012-12-12 19:21:53 +0000626 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000627 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000628 if (Attrs.empty())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000629 return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000630
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000631#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000632 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000633 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000634 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000635 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000636 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000637 }
638#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000639
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000640 // Otherwise, build a key to look up the existing attributes.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000641 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000642 FoldingSetNodeID ID;
Bill Wendling6848e382012-12-19 22:42:22 +0000643 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000644
Bill Wendlingf86efb92012-11-20 05:09:20 +0000645 void *InsertPoint;
Bill Wendling960f52a2013-01-05 01:36:54 +0000646 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000647
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000648 // If we didn't find any existing attributes of the same shape then
649 // create a new one and insert it.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000650 if (!PA) {
Bill Wendling6ad6c3b2012-12-19 23:55:43 +0000651 PA = new AttributeSetImpl(C, Attrs);
Bill Wendlingf86efb92012-11-20 05:09:20 +0000652 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000653 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000654
Devang Patel4c758ea2008-09-25 21:00:45 +0000655 // Return the AttributesList that we found or created.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000656 return AttributeSet(PA);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000657}
658
Bill Wendling960f52a2013-01-05 01:36:54 +0000659AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000660 // FIXME: This should be implemented as a loop that creates the
661 // AttributeWithIndexes that then are used to create the AttributeSet.
662 if (!B.hasAttributes())
663 return AttributeSet();
Bill Wendling09175b32013-01-22 21:15:51 +0000664 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling960f52a2013-01-05 01:36:54 +0000665}
666
Bill Wendling49bc76c2013-01-23 06:14:59 +0000667AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling201d7b22013-01-26 00:03:11 +0000668 ArrayRef<Attribute::AttrKind> Kind) {
669 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
670 // replaced by an object that holds multiple Attribute::AttrKinds.
671 AttrBuilder B;
672 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
673 E = Kind.end(); I != E; ++I)
674 B.addAttribute(*I);
675 return get(C, Idx, B);
Bill Wendling49bc76c2013-01-23 06:14:59 +0000676}
677
Bill Wendling57625a42013-01-25 23:09:36 +0000678AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
679 SmallVector<AttributeWithIndex, 8> AttrList;
680 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
681 I != E; ++I) {
682 AttributeSet AS = *I;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000683 if (!AS.pImpl) continue;
684 AttrList.append(AS.pImpl->AttrList.begin(), AS.pImpl->AttrList.end());
Bill Wendling57625a42013-01-25 23:09:36 +0000685 }
686
687 return get(C, AttrList);
688}
689
Bill Wendling1f786a72013-01-27 23:41:29 +0000690/// \brief Return the number of slots used in this attribute list. This is the
691/// number of arguments that have an attribute set on them (including the
692/// function itself).
Bill Wendlinge94d8432012-12-07 23:16:57 +0000693unsigned AttributeSet::getNumSlots() const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000694 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000695}
696
Bill Wendling3217eb62013-01-27 23:50:44 +0000697uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000698 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling25e65a62013-01-25 21:30:53 +0000699 "Slot # out of range!");
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000700 return pImpl->getSlotIndex(Slot);
Bill Wendling25e65a62013-01-25 21:30:53 +0000701}
702
Bill Wendling57625a42013-01-25 23:09:36 +0000703AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000704 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling57625a42013-01-25 23:09:36 +0000705 "Slot # out of range!");
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000706 return pImpl->getSlotAttributes(Slot);
Bill Wendling57625a42013-01-25 23:09:36 +0000707}
708
Bill Wendling698e84f2012-12-30 10:32:01 +0000709bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
710 return getAttributes(Index).hasAttribute(Kind);
711}
712
713bool AttributeSet::hasAttributes(unsigned Index) const {
714 return getAttributes(Index).hasAttributes();
715}
716
717std::string AttributeSet::getAsString(unsigned Index) const {
718 return getAttributes(Index).getAsString();
719}
720
Bill Wendling77543892013-01-18 21:11:39 +0000721unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
722 return getAttributes(Idx).getAlignment();
723}
724
Bill Wendling698e84f2012-12-30 10:32:01 +0000725unsigned AttributeSet::getStackAlignment(unsigned Index) const {
726 return getAttributes(Index).getStackAlignment();
727}
728
Bill Wendling3b651872013-01-09 23:36:50 +0000729uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling698e84f2012-12-30 10:32:01 +0000730 // FIXME: Remove this.
Bill Wendling1f786a72013-01-27 23:41:29 +0000731 return pImpl ? pImpl->Raw(Index) : 0;
Bill Wendling698e84f2012-12-30 10:32:01 +0000732}
733
Bill Wendlinga529ade2012-10-16 06:01:44 +0000734/// getAttributes - The attributes for the specified index are returned.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000735Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000736 if (pImpl == 0) return Attribute();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000737
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000738 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000739 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
740 if (Attrs[i].Index == Idx)
741 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000742
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000743 return Attribute();
Chris Lattner8a923e72008-03-12 17:45:29 +0000744}
745
746/// hasAttrSomewhere - Return true if the specified attribute is set for at
747/// least one parameter or for the return value.
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000748bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000749 if (pImpl == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000750
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000751 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000752 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000753 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000754 return true;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000755
Chris Lattner8a923e72008-03-12 17:45:29 +0000756 return false;
757}
758
Bill Wendling09175b32013-01-22 21:15:51 +0000759AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
760 Attribute::AttrKind Attr) const {
761 return addAttr(C, Idx, Attribute::get(C, Attr));
762}
763
Bill Wendlingeb9c0ec2013-01-22 00:53:12 +0000764AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
765 AttributeSet Attrs) const {
766 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling77543892013-01-18 21:11:39 +0000767}
768
Bill Wendlinge94d8432012-12-07 23:16:57 +0000769AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling6e95ae82012-12-31 00:49:59 +0000770 Attribute Attrs) const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000771 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000772#ifndef NDEBUG
773 // FIXME it is not obvious how this should work for alignment.
774 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000775 unsigned OldAlign = OldAttrs.getAlignment();
776 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000777 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000778 "Attempt to change alignment!");
779#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000780
Bill Wendling50d27842012-10-15 20:35:56 +0000781 AttrBuilder NewAttrs =
782 AttrBuilder(OldAttrs).addAttributes(Attrs);
783 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000784 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000785
Devang Patel4c758ea2008-09-25 21:00:45 +0000786 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000787 if (pImpl == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000788 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000789 else {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000790 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000791 unsigned i = 0, e = OldAttrList.size();
792 // Copy attributes for arguments before this one.
793 for (; i != e && OldAttrList[i].Index < Idx; ++i)
794 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000795
Chris Lattner8a923e72008-03-12 17:45:29 +0000796 // If there are attributes already at this index, merge them in.
797 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000798 Attrs =
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000799 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000800 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000801 ++i;
802 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000803
Devang Patel4c758ea2008-09-25 21:00:45 +0000804 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000805
Chris Lattner8a923e72008-03-12 17:45:29 +0000806 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000807 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000808 OldAttrList.begin()+i, OldAttrList.end());
809 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000810
Bill Wendlingf86efb92012-11-20 05:09:20 +0000811 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000812}
813
Bill Wendling430fa9b2013-01-23 00:45:55 +0000814AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
815 Attribute::AttrKind Attr) const {
816 return removeAttr(C, Idx, Attribute::get(C, Attr));
817}
818
819AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
820 AttributeSet Attrs) const {
821 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
822}
823
Bill Wendlinge94d8432012-12-07 23:16:57 +0000824AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling6e95ae82012-12-31 00:49:59 +0000825 Attribute Attrs) const {
Dale Johannesen11a555e2008-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 Wendling3d7b0b82012-12-19 07:18:57 +0000829 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000830 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000831#endif
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000832 if (pImpl == 0) return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000833
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000834 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000835 AttrBuilder NewAttrs =
836 AttrBuilder(OldAttrs).removeAttributes(Attrs);
837 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000838 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000839
Devang Patel4c758ea2008-09-25 21:00:45 +0000840 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000841 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000842 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000843
Chris Lattner8a923e72008-03-12 17:45:29 +0000844 // Copy attributes for arguments before this one.
845 for (; i != e && OldAttrList[i].Index < Idx; ++i)
846 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000847
Chris Lattner8a923e72008-03-12 17:45:29 +0000848 // If there are attributes already at this index, merge them in.
849 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000850 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000851 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000852 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000853 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000854 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000855
Chris Lattner8a923e72008-03-12 17:45:29 +0000856 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000857 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000858 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000859
Bill Wendlingf86efb92012-11-20 05:09:20 +0000860 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000861}
862
Bill Wendlinge94d8432012-12-07 23:16:57 +0000863void AttributeSet::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000864 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000865 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling86492832013-01-25 21:46:52 +0000866 unsigned Index = getSlotIndex(i);
867 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000868 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000869
David Greenef7014732010-01-05 01:29:58 +0000870 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000871}
Bill Wendling57625a42013-01-25 23:09:36 +0000872
873//===----------------------------------------------------------------------===//
874// AttributeFuncs Function Defintions
875//===----------------------------------------------------------------------===//
876
877Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
878 AttrBuilder Incompatible;
879
880 if (!Ty->isIntegerTy())
881 // Attribute that only apply to integers.
882 Incompatible.addAttribute(Attribute::SExt)
883 .addAttribute(Attribute::ZExt);
884
885 if (!Ty->isPointerTy())
886 // Attribute that only apply to pointers.
887 Incompatible.addAttribute(Attribute::ByVal)
888 .addAttribute(Attribute::Nest)
889 .addAttribute(Attribute::NoAlias)
890 .addAttribute(Attribute::NoCapture)
891 .addAttribute(Attribute::StructRet);
892
893 return Attribute::get(Ty->getContext(), Incompatible);
894}
895
896/// encodeLLVMAttributesForBitcode - This returns an integer containing an
897/// encoding of all the LLVM attributes found in the given attribute bitset.
898/// Any change to this encoding is a breaking change to bitcode compatibility.
899uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
900 unsigned Index) {
901 // FIXME: It doesn't make sense to store the alignment information as an
902 // expanded out value, we should store it as a log2 value. However, we can't
903 // just change that here without breaking bitcode compatibility. If this ever
904 // becomes a problem in practice, we should introduce new tag numbers in the
905 // bitcode file and have those tags use a more efficiently encoded alignment
906 // field.
907
908 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
909 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
910 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
911 if (Attrs.hasAttribute(Index, Attribute::Alignment))
912 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
913 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
914 return EncodedAttrs;
915}
916
917/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
918/// the LLVM attributes that have been decoded from the given integer. This
919/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
920Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
921 uint64_t EncodedAttrs){
922 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
923 // the bits above 31 down by 11 bits.
924 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
925 assert((!Alignment || isPowerOf2_32(Alignment)) &&
926 "Alignment must be a power of two.");
927
928 AttrBuilder B(EncodedAttrs & 0xffff);
929 if (Alignment)
930 B.addAlignmentAttr(Alignment);
931 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
932 return Attribute::get(C, B);
933}
934