blob: 1b057bbbb9799f7e74a6d3a161e84b7430de6847 [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
577//===----------------------------------------------------------------------===//
578// AttributeSet Method Implementations
579//===----------------------------------------------------------------------===//
580
Bill Wendling49bc76c2013-01-23 06:14:59 +0000581AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
582 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000583 return pImpl && hasAttributes(Idx) ?
584 AttributeSet::get(pImpl->getContext(),
Bill Wendling49bc76c2013-01-23 06:14:59 +0000585 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
586 AttributeSet();
587}
588
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000589AttributeSet AttributeSet::getRetAttributes() const {
590 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000591 return pImpl && hasAttributes(ReturnIndex) ?
592 AttributeSet::get(pImpl->getContext(),
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000593 AttributeWithIndex::get(ReturnIndex,
594 getAttributes(ReturnIndex))) :
595 AttributeSet();
596}
597
Bill Wendlingbd4ea162013-01-21 21:57:28 +0000598AttributeSet AttributeSet::getFnAttributes() const {
599 // FIXME: Remove.
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000600 return pImpl && hasAttributes(FunctionIndex) ?
601 AttributeSet::get(pImpl->getContext(),
Bill Wendlingbd4ea162013-01-21 21:57:28 +0000602 AttributeWithIndex::get(FunctionIndex,
603 getAttributes(FunctionIndex))) :
604 AttributeSet();
605}
606
Bill Wendlinge94d8432012-12-07 23:16:57 +0000607AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling623d8ce2012-12-12 19:21:53 +0000608 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000609 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000610 if (Attrs.empty())
Bill Wendlinge94d8432012-12-07 23:16:57 +0000611 return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000612
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000613#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000614 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendlinga529ade2012-10-16 06:01:44 +0000615 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000616 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000617 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000618 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000619 }
620#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000621
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000622 // Otherwise, build a key to look up the existing attributes.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000623 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000624 FoldingSetNodeID ID;
Bill Wendling6848e382012-12-19 22:42:22 +0000625 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000626
Bill Wendlingf86efb92012-11-20 05:09:20 +0000627 void *InsertPoint;
Bill Wendling960f52a2013-01-05 01:36:54 +0000628 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000629
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000630 // If we didn't find any existing attributes of the same shape then
631 // create a new one and insert it.
Bill Wendlingf86efb92012-11-20 05:09:20 +0000632 if (!PA) {
Bill Wendling6ad6c3b2012-12-19 23:55:43 +0000633 PA = new AttributeSetImpl(C, Attrs);
Bill Wendlingf86efb92012-11-20 05:09:20 +0000634 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000635 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000636
Devang Patel4c758ea2008-09-25 21:00:45 +0000637 // Return the AttributesList that we found or created.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000638 return AttributeSet(PA);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000639}
640
Bill Wendling960f52a2013-01-05 01:36:54 +0000641AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendlingc90d9f82013-01-21 22:44:49 +0000642 // FIXME: This should be implemented as a loop that creates the
643 // AttributeWithIndexes that then are used to create the AttributeSet.
644 if (!B.hasAttributes())
645 return AttributeSet();
Bill Wendling09175b32013-01-22 21:15:51 +0000646 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling960f52a2013-01-05 01:36:54 +0000647}
648
Bill Wendling49bc76c2013-01-23 06:14:59 +0000649AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
Bill Wendling201d7b22013-01-26 00:03:11 +0000650 ArrayRef<Attribute::AttrKind> Kind) {
651 // FIXME: This is temporary. Ultimately, the AttributeWithIndex will be
652 // replaced by an object that holds multiple Attribute::AttrKinds.
653 AttrBuilder B;
654 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
655 E = Kind.end(); I != E; ++I)
656 B.addAttribute(*I);
657 return get(C, Idx, B);
Bill Wendling49bc76c2013-01-23 06:14:59 +0000658}
659
Bill Wendling57625a42013-01-25 23:09:36 +0000660AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
661 SmallVector<AttributeWithIndex, 8> AttrList;
662 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
663 I != E; ++I) {
664 AttributeSet AS = *I;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000665 if (!AS.pImpl) continue;
666 AttrList.append(AS.pImpl->AttrList.begin(), AS.pImpl->AttrList.end());
Bill Wendling57625a42013-01-25 23:09:36 +0000667 }
668
669 return get(C, AttrList);
670}
671
Bill Wendlinge94d8432012-12-07 23:16:57 +0000672const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000673 pImpl = RHS.pImpl;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000674 return *this;
Chris Lattner8a923e72008-03-12 17:45:29 +0000675}
676
Bill Wendlinga529ade2012-10-16 06:01:44 +0000677/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner8a923e72008-03-12 17:45:29 +0000678/// This is the number of arguments that have an attribute set on them
679/// (including the function itself).
Bill Wendlinge94d8432012-12-07 23:16:57 +0000680unsigned AttributeSet::getNumSlots() const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000681 return pImpl ? pImpl->getNumAttributes() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000682}
683
Bill Wendling25e65a62013-01-25 21:30:53 +0000684unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000685 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling25e65a62013-01-25 21:30:53 +0000686 "Slot # out of range!");
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000687 return pImpl->getSlotIndex(Slot);
Bill Wendling25e65a62013-01-25 21:30:53 +0000688}
689
Bill Wendling57625a42013-01-25 23:09:36 +0000690AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000691 assert(pImpl && Slot < pImpl->getNumAttributes() &&
Bill Wendling57625a42013-01-25 23:09:36 +0000692 "Slot # out of range!");
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000693 return pImpl->getSlotAttributes(Slot);
Bill Wendling57625a42013-01-25 23:09:36 +0000694}
695
Bill Wendling698e84f2012-12-30 10:32:01 +0000696bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
697 return getAttributes(Index).hasAttribute(Kind);
698}
699
700bool AttributeSet::hasAttributes(unsigned Index) const {
701 return getAttributes(Index).hasAttributes();
702}
703
704std::string AttributeSet::getAsString(unsigned Index) const {
705 return getAttributes(Index).getAsString();
706}
707
Bill Wendling77543892013-01-18 21:11:39 +0000708unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
709 return getAttributes(Idx).getAlignment();
710}
711
Bill Wendling698e84f2012-12-30 10:32:01 +0000712unsigned AttributeSet::getStackAlignment(unsigned Index) const {
713 return getAttributes(Index).getStackAlignment();
714}
715
Bill Wendling3b651872013-01-09 23:36:50 +0000716uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling698e84f2012-12-30 10:32:01 +0000717 // FIXME: Remove this.
Bill Wendling3b651872013-01-09 23:36:50 +0000718 return getAttributes(Index).Raw();
Bill Wendling698e84f2012-12-30 10:32:01 +0000719}
720
Bill Wendlinga529ade2012-10-16 06:01:44 +0000721/// getAttributes - The attributes for the specified index are returned.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000722Attribute AttributeSet::getAttributes(unsigned Idx) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000723 if (pImpl == 0) return Attribute();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000724
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000725 ArrayRef<AttributeWithIndex> Attrs = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000726 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
727 if (Attrs[i].Index == Idx)
728 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000729
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000730 return Attribute();
Chris Lattner8a923e72008-03-12 17:45:29 +0000731}
732
733/// hasAttrSomewhere - Return true if the specified attribute is set for at
734/// least one parameter or for the return value.
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000735bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000736 if (pImpl == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +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; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000740 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000741 return true;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000742
Chris Lattner8a923e72008-03-12 17:45:29 +0000743 return false;
744}
745
Bill Wendling09175b32013-01-22 21:15:51 +0000746AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
747 Attribute::AttrKind Attr) const {
748 return addAttr(C, Idx, Attribute::get(C, Attr));
749}
750
Bill Wendlingeb9c0ec2013-01-22 00:53:12 +0000751AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
752 AttributeSet Attrs) const {
753 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling77543892013-01-18 21:11:39 +0000754}
755
Bill Wendlinge94d8432012-12-07 23:16:57 +0000756AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling6e95ae82012-12-31 00:49:59 +0000757 Attribute Attrs) const {
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000758 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000759#ifndef NDEBUG
760 // FIXME it is not obvious how this should work for alignment.
761 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000762 unsigned OldAlign = OldAttrs.getAlignment();
763 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000764 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000765 "Attempt to change alignment!");
766#endif
Bill Wendlinga529ade2012-10-16 06:01:44 +0000767
Bill Wendling50d27842012-10-15 20:35:56 +0000768 AttrBuilder NewAttrs =
769 AttrBuilder(OldAttrs).addAttributes(Attrs);
770 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000771 return *this;
Bill Wendlinga529ade2012-10-16 06:01:44 +0000772
Devang Patel4c758ea2008-09-25 21:00:45 +0000773 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000774 if (pImpl == 0)
Devang Patel4c758ea2008-09-25 21:00:45 +0000775 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000776 else {
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000777 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000778 unsigned i = 0, e = OldAttrList.size();
779 // Copy attributes for arguments before this one.
780 for (; i != e && OldAttrList[i].Index < Idx; ++i)
781 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000782
Chris Lattner8a923e72008-03-12 17:45:29 +0000783 // If there are attributes already at this index, merge them in.
784 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000785 Attrs =
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000786 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000787 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000788 ++i;
789 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000790
Devang Patel4c758ea2008-09-25 21:00:45 +0000791 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000792
Chris Lattner8a923e72008-03-12 17:45:29 +0000793 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000794 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000795 OldAttrList.begin()+i, OldAttrList.end());
796 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000797
Bill Wendlingf86efb92012-11-20 05:09:20 +0000798 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000799}
800
Bill Wendling430fa9b2013-01-23 00:45:55 +0000801AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
802 Attribute::AttrKind Attr) const {
803 return removeAttr(C, Idx, Attribute::get(C, Attr));
804}
805
806AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
807 AttributeSet Attrs) const {
808 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
809}
810
Bill Wendlinge94d8432012-12-07 23:16:57 +0000811AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling6e95ae82012-12-31 00:49:59 +0000812 Attribute Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000813#ifndef NDEBUG
814 // FIXME it is not obvious how this should work for alignment.
815 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000816 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000817 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000818#endif
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000819 if (pImpl == 0) return AttributeSet();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000820
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000821 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling50d27842012-10-15 20:35:56 +0000822 AttrBuilder NewAttrs =
823 AttrBuilder(OldAttrs).removeAttributes(Attrs);
824 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000825 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000826
Devang Patel4c758ea2008-09-25 21:00:45 +0000827 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling56b0b2a2013-01-27 21:23:46 +0000828 ArrayRef<AttributeWithIndex> OldAttrList = pImpl->getAttributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000829 unsigned i = 0, e = OldAttrList.size();
Bill Wendlinga529ade2012-10-16 06:01:44 +0000830
Chris Lattner8a923e72008-03-12 17:45:29 +0000831 // Copy attributes for arguments before this one.
832 for (; i != e && OldAttrList[i].Index < Idx; ++i)
833 NewAttrList.push_back(OldAttrList[i]);
Bill Wendlinga529ade2012-10-16 06:01:44 +0000834
Chris Lattner8a923e72008-03-12 17:45:29 +0000835 // If there are attributes already at this index, merge them in.
836 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000837 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000838 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000839 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000840 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000841 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendlinga529ade2012-10-16 06:01:44 +0000842
Chris Lattner8a923e72008-03-12 17:45:29 +0000843 // Copy attributes for arguments after this one.
Bill Wendlinga529ade2012-10-16 06:01:44 +0000844 NewAttrList.insert(NewAttrList.end(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000845 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendlinga529ade2012-10-16 06:01:44 +0000846
Bill Wendlingf86efb92012-11-20 05:09:20 +0000847 return get(C, NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000848}
849
Bill Wendlinge94d8432012-12-07 23:16:57 +0000850void AttributeSet::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000851 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000852 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling86492832013-01-25 21:46:52 +0000853 unsigned Index = getSlotIndex(i);
854 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000855 }
Bill Wendlinga529ade2012-10-16 06:01:44 +0000856
David Greenef7014732010-01-05 01:29:58 +0000857 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000858}
Bill Wendling57625a42013-01-25 23:09:36 +0000859
860//===----------------------------------------------------------------------===//
861// AttributeFuncs Function Defintions
862//===----------------------------------------------------------------------===//
863
864Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
865 AttrBuilder Incompatible;
866
867 if (!Ty->isIntegerTy())
868 // Attribute that only apply to integers.
869 Incompatible.addAttribute(Attribute::SExt)
870 .addAttribute(Attribute::ZExt);
871
872 if (!Ty->isPointerTy())
873 // Attribute that only apply to pointers.
874 Incompatible.addAttribute(Attribute::ByVal)
875 .addAttribute(Attribute::Nest)
876 .addAttribute(Attribute::NoAlias)
877 .addAttribute(Attribute::NoCapture)
878 .addAttribute(Attribute::StructRet);
879
880 return Attribute::get(Ty->getContext(), Incompatible);
881}
882
883/// encodeLLVMAttributesForBitcode - This returns an integer containing an
884/// encoding of all the LLVM attributes found in the given attribute bitset.
885/// Any change to this encoding is a breaking change to bitcode compatibility.
886uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
887 unsigned Index) {
888 // FIXME: It doesn't make sense to store the alignment information as an
889 // expanded out value, we should store it as a log2 value. However, we can't
890 // just change that here without breaking bitcode compatibility. If this ever
891 // becomes a problem in practice, we should introduce new tag numbers in the
892 // bitcode file and have those tags use a more efficiently encoded alignment
893 // field.
894
895 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
896 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
897 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
898 if (Attrs.hasAttribute(Index, Attribute::Alignment))
899 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
900 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
901 return EncodedAttrs;
902}
903
904/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
905/// the LLVM attributes that have been decoded from the given integer. This
906/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
907Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
908 uint64_t EncodedAttrs){
909 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
910 // the bits above 31 down by 11 bits.
911 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
912 assert((!Alignment || isPowerOf2_32(Alignment)) &&
913 "Alignment must be a power of two.");
914
915 AttrBuilder B(EncodedAttrs & 0xffff);
916 if (Alignment)
917 B.addAlignmentAttr(Alignment);
918 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
919 return Attribute::get(C, B);
920}
921