blob: 0bc926271aeb5f6836bab22a2a9ed66df4e6cac8 [file] [log] [blame]
Bill Wendling87e10df2013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling87e10df2013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling817abdd2013-01-29 00:48:16 +000030// Attribute Construction Methods
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling6bdbf062013-01-28 22:33:39 +000033Attribute Attribute::get(LLVMContext &Context, AttrKind Kind) {
Bill Wendling16f95662013-01-27 10:28:39 +000034 AttrBuilder B;
Bill Wendling6bdbf062013-01-28 22:33:39 +000035 return Attribute::get(Context, B.addAttribute(Kind));
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000036}
Bill Wendling8e635db2012-10-08 21:47:17 +000037
Bill Wendling034b94b2012-12-19 07:18:57 +000038Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
39 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000040 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000041 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000042
43 // Otherwise, build a key to look up the existing attributes.
44 LLVMContextImpl *pImpl = Context.pImpl;
45 FoldingSetNodeID ID;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000046 ConstantInt *CI = ConstantInt::get(Type::getInt64Ty(Context), B.Raw());
47 ID.AddPointer(CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000048
49 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000050 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000051
52 if (!PA) {
53 // If we didn't find any existing attributes of the same shape then create a
54 // new one and insert it.
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000055 PA = new AttributeImpl(Context, CI);
Bill Wendling8e635db2012-10-08 21:47:17 +000056 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
57 }
58
59 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000060 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000061}
62
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000063Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
64 AttrBuilder B;
65 return get(Context, B.addAlignmentAttr(Align));
66}
67
68Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
69 uint64_t Align) {
70 AttrBuilder B;
71 return get(Context, B.addStackAlignmentAttr(Align));
72}
73
Bill Wendling817abdd2013-01-29 00:48:16 +000074//===----------------------------------------------------------------------===//
75// Attribute Accessor Methods
76//===----------------------------------------------------------------------===//
77
Bill Wendling629fb822012-12-22 00:37:52 +000078bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000079 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000080}
81
Bill Wendling034b94b2012-12-19 07:18:57 +000082bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000083 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000084}
85
Bill Wendlinge66f3d32012-10-05 06:44:41 +000086/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000087unsigned Attribute::getAlignment() const {
88 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000089 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000090 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000091}
92
93/// This returns the stack alignment field of an attribute as a byte alignment
94/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000095unsigned Attribute::getStackAlignment() const {
96 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000097 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000098 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000099}
100
Bill Wendling034b94b2012-12-19 07:18:57 +0000101std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000102 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000103 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000104 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000105 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000106 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000107 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000108 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000109 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000110 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000111 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000112 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000113 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000114 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000115 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000116 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000117 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000118 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000119 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000120 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000121 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000122 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000123 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000124 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000125 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000126 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000128 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000129 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000130 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000132 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000133 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000134 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000135 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000136 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000137 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000138 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000140 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000141 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000142 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000143 if (hasAttribute(Attribute::StackProtectStrong))
144 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000145 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000146 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000147 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000148 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000149 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000150 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000151 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000152 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000153 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000154 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000155 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000156 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000157 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000158 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000159 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000160 Result += ") ";
161 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000163 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000164 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000165 Result += " ";
166 }
James Molloy67ae1352012-12-20 16:04:27 +0000167 if (hasAttribute(Attribute::NoDuplicate))
168 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000169 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000170 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000171 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000172 return Result;
173}
174
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000175bool Attribute::operator==(AttrKind K) const {
176 return pImpl && *pImpl == K;
177}
178bool Attribute::operator!=(AttrKind K) const {
179 return !(*this == K);
180}
181
182bool Attribute::operator<(Attribute A) const {
183 if (!pImpl && !A.pImpl) return false;
184 if (!pImpl) return true;
185 if (!A.pImpl) return false;
186 return *pImpl < *A.pImpl;
187}
188
189uint64_t Attribute::Raw() const {
190 return pImpl ? pImpl->Raw() : 0;
191}
192
193//===----------------------------------------------------------------------===//
194// AttributeImpl Definition
195//===----------------------------------------------------------------------===//
196
197AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
198 : Context(C) {
199 Data = ConstantInt::get(Type::getInt64Ty(C), data);
200}
201AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
202 ArrayRef<Constant*> values)
203 : Context(C) {
204 Data = ConstantInt::get(Type::getInt64Ty(C), data);
205 Vals.reserve(values.size());
206 Vals.append(values.begin(), values.end());
207}
208AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
209 : Context(C) {
210 Data = ConstantDataArray::getString(C, data);
211}
212
213bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
214 return (Raw() & getAttrMask(A)) != 0;
215}
216
217bool AttributeImpl::hasAttributes() const {
218 return Raw() != 0;
219}
220
221uint64_t AttributeImpl::getAlignment() const {
222 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
223 return 1ULL << ((Mask >> 16) - 1);
224}
225
226uint64_t AttributeImpl::getStackAlignment() const {
227 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
228 return 1ULL << ((Mask >> 26) - 1);
229}
230
231bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
232 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
233 return CI->getZExtValue() == Kind;
234 return false;
235}
236bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
237 return !(*this == Kind);
238}
239
240bool AttributeImpl::operator==(StringRef Kind) const {
241 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
242 if (CDA->isString())
243 return CDA->getAsString() == Kind;
244 return false;
245}
246
247bool AttributeImpl::operator!=(StringRef Kind) const {
248 return !(*this == Kind);
249}
250
251bool AttributeImpl::operator<(const AttributeImpl &AI) const {
252 if (!Data && !AI.Data) return false;
253 if (!Data && AI.Data) return true;
254 if (Data && !AI.Data) return false;
255
256 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
257 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
258
259 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
260 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
261
262 if (ThisCI && ThatCI)
263 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
264
265 if (ThisCI && ThatCDA)
266 return true;
267
268 if (ThisCDA && ThatCI)
269 return false;
270
271 return ThisCDA->getAsString() < ThatCDA->getAsString();
272}
273
274uint64_t AttributeImpl::Raw() const {
275 // FIXME: Remove this.
276 return cast<ConstantInt>(Data)->getZExtValue();
277}
278
279uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
280 // FIXME: Remove this.
281 switch (Val) {
282 case Attribute::EndAttrKinds:
283 case Attribute::AttrKindEmptyKey:
284 case Attribute::AttrKindTombstoneKey:
285 llvm_unreachable("Synthetic enumerators which should never get here");
286
287 case Attribute::None: return 0;
288 case Attribute::ZExt: return 1 << 0;
289 case Attribute::SExt: return 1 << 1;
290 case Attribute::NoReturn: return 1 << 2;
291 case Attribute::InReg: return 1 << 3;
292 case Attribute::StructRet: return 1 << 4;
293 case Attribute::NoUnwind: return 1 << 5;
294 case Attribute::NoAlias: return 1 << 6;
295 case Attribute::ByVal: return 1 << 7;
296 case Attribute::Nest: return 1 << 8;
297 case Attribute::ReadNone: return 1 << 9;
298 case Attribute::ReadOnly: return 1 << 10;
299 case Attribute::NoInline: return 1 << 11;
300 case Attribute::AlwaysInline: return 1 << 12;
301 case Attribute::OptimizeForSize: return 1 << 13;
302 case Attribute::StackProtect: return 1 << 14;
303 case Attribute::StackProtectReq: return 1 << 15;
304 case Attribute::Alignment: return 31 << 16;
305 case Attribute::NoCapture: return 1 << 21;
306 case Attribute::NoRedZone: return 1 << 22;
307 case Attribute::NoImplicitFloat: return 1 << 23;
308 case Attribute::Naked: return 1 << 24;
309 case Attribute::InlineHint: return 1 << 25;
310 case Attribute::StackAlignment: return 7 << 26;
311 case Attribute::ReturnsTwice: return 1 << 29;
312 case Attribute::UWTable: return 1 << 30;
313 case Attribute::NonLazyBind: return 1U << 31;
314 case Attribute::AddressSafety: return 1ULL << 32;
315 case Attribute::MinSize: return 1ULL << 33;
316 case Attribute::NoDuplicate: return 1ULL << 34;
317 case Attribute::StackProtectStrong: return 1ULL << 35;
318 }
319 llvm_unreachable("Unsupported attribute type");
320}
321
322//===----------------------------------------------------------------------===//
323// AttributeSetNode Definition
324//===----------------------------------------------------------------------===//
325
326AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
327 ArrayRef<Attribute> Attrs) {
328 if (Attrs.empty())
329 return 0;
330
331 // Otherwise, build a key to look up the existing attributes.
332 LLVMContextImpl *pImpl = C.pImpl;
333 FoldingSetNodeID ID;
334
335 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
336 std::sort(SortedAttrs.begin(), SortedAttrs.end());
337
338 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
339 E = SortedAttrs.end(); I != E; ++I)
340 I->Profile(ID);
341
342 void *InsertPoint;
343 AttributeSetNode *PA =
344 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
345
346 // If we didn't find any existing attributes of the same shape then create a
347 // new one and insert it.
348 if (!PA) {
349 PA = new AttributeSetNode(SortedAttrs);
350 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
351 }
352
353 // Return the AttributesListNode that we found or created.
354 return PA;
355}
356
357//===----------------------------------------------------------------------===//
358// AttributeSetImpl Definition
359//===----------------------------------------------------------------------===//
360
361uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
362 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
363 if (getSlotIndex(I) != Index) continue;
364 const AttributeSetNode *ASN = AttrNodes[I].second;
365 AttrBuilder B;
366
367 for (AttributeSetNode::const_iterator II = ASN->begin(),
368 IE = ASN->end(); II != IE; ++II)
369 B.addAttributes(*II);
370 return B.Raw();
371 }
372
373 return 0;
374}
375
376//===----------------------------------------------------------------------===//
377// AttributeSet Construction and Mutation Methods
378//===----------------------------------------------------------------------===//
379
380AttributeSet AttributeSet::getImpl(LLVMContext &C,
381 ArrayRef<std::pair<unsigned,
382 AttributeSetNode*> > Attrs) {
383 LLVMContextImpl *pImpl = C.pImpl;
384 FoldingSetNodeID ID;
385 AttributeSetImpl::Profile(ID, Attrs);
386
387 void *InsertPoint;
388 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
389
390 // If we didn't find any existing attributes of the same shape then
391 // create a new one and insert it.
392 if (!PA) {
393 PA = new AttributeSetImpl(C, Attrs);
394 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
395 }
396
397 // Return the AttributesList that we found or created.
398 return AttributeSet(PA);
399}
400
401AttributeSet AttributeSet::get(LLVMContext &C,
402 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
403 // If there are no attributes then return a null AttributesList pointer.
404 if (Attrs.empty())
405 return AttributeSet();
406
407#ifndef NDEBUG
408 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
409 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
410 "Misordered Attributes list!");
411 assert(Attrs[i].second.hasAttributes() &&
412 "Pointless attribute!");
413 }
414#endif
415
416 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
417 // list.
418 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
419 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
420 E = Attrs.end(); I != E; ) {
421 unsigned Index = I->first;
422 SmallVector<Attribute, 4> AttrVec;
423 while (I->first == Index && I != E) {
424 AttrVec.push_back(I->second);
425 ++I;
426 }
427
428 AttrPairVec.push_back(std::make_pair(Index,
429 AttributeSetNode::get(C, AttrVec)));
430 }
431
432 return getImpl(C, AttrPairVec);
433}
434
435AttributeSet AttributeSet::get(LLVMContext &C,
436 ArrayRef<std::pair<unsigned,
437 AttributeSetNode*> > Attrs) {
438 // If there are no attributes then return a null AttributesList pointer.
439 if (Attrs.empty())
440 return AttributeSet();
441
442 return getImpl(C, Attrs);
443}
444
445AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
446 if (!B.hasAttributes())
447 return AttributeSet();
448 return get(C, ArrayRef<std::pair<unsigned, Attribute> >(
449 std::make_pair(Idx, Attribute::get(C, B))));
450}
451
452AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
453 ArrayRef<Attribute::AttrKind> Kind) {
454 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
455 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
456 E = Kind.end(); I != E; ++I)
457 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
458 return get(C, Attrs);
459}
460
461AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
462 if (Attrs.empty()) return AttributeSet();
463
464 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
465 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
466 AttributeSet AS = Attrs[I];
467 if (!AS.pImpl) continue;
468 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
469 }
470
471 return getImpl(C, AttrNodeVec);
472}
473
474AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
475 Attribute::AttrKind Attr) const {
476 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
477}
478
479AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
480 AttributeSet Attrs) const {
481 if (!pImpl) return Attrs;
482 if (!Attrs.pImpl) return *this;
483
484#ifndef NDEBUG
485 // FIXME it is not obvious how this should work for alignment. For now, say
486 // we can't change a known alignment.
487 unsigned OldAlign = getParamAlignment(Idx);
488 unsigned NewAlign = Attrs.getParamAlignment(Idx);
489 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
490 "Attempt to change alignment!");
491#endif
492
493 // Add the attribute slots before the one we're trying to add.
494 SmallVector<AttributeSet, 4> AttrSet;
495 uint64_t NumAttrs = pImpl->getNumAttributes();
496 AttributeSet AS;
497 uint64_t LastIndex = 0;
498 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
499 if (getSlotIndex(I) >= Idx) {
500 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
501 break;
502 }
503 LastIndex = I + 1;
504 AttrSet.push_back(getSlotAttributes(I));
505 }
506
507 // Now add the attribute into the correct slot. There may already be an
508 // AttributeSet there.
509 AttrBuilder B(AS, Idx);
510
511 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
512 if (Attrs.getSlotIndex(I) == Idx) {
513 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
514 IE = Attrs.pImpl->end(I); II != IE; ++II)
515 B.addAttributes(*II);
516 break;
517 }
518
519 AttrSet.push_back(AttributeSet::get(C, Idx, B));
520
521 // Add the remaining attribute slots.
522 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
523 AttrSet.push_back(getSlotAttributes(I));
524
525 return get(C, AttrSet);
526}
527
528AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
529 Attribute::AttrKind Attr) const {
530 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
531}
532
533AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
534 AttributeSet Attrs) const {
535 if (!pImpl) return AttributeSet();
536 if (!Attrs.pImpl) return *this;
537
538#ifndef NDEBUG
539 // FIXME it is not obvious how this should work for alignment.
540 // For now, say we can't pass in alignment, which no current use does.
541 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
542 "Attempt to change alignment!");
543#endif
544
545 // Add the attribute slots before the one we're trying to add.
546 SmallVector<AttributeSet, 4> AttrSet;
547 uint64_t NumAttrs = pImpl->getNumAttributes();
548 AttributeSet AS;
549 uint64_t LastIndex = 0;
550 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
551 if (getSlotIndex(I) >= Idx) {
552 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
553 break;
554 }
555 LastIndex = I + 1;
556 AttrSet.push_back(getSlotAttributes(I));
557 }
558
559 // Now add the attribute into the correct slot. There may already be an
560 // AttributeSet there.
561 AttrBuilder B(AS, Idx);
562
563 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
564 if (Attrs.getSlotIndex(I) == Idx) {
565 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
566 IE = Attrs.pImpl->end(I); II != IE; ++II)
567 B.removeAttributes(*II);
568 break;
569 }
570
571 AttrSet.push_back(AttributeSet::get(C, Idx, B));
572
573 // Add the remaining attribute slots.
574 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
575 AttrSet.push_back(getSlotAttributes(I));
576
577 return get(C, AttrSet);
578}
579
580//===----------------------------------------------------------------------===//
581// AttributeSet Accessor Methods
582//===----------------------------------------------------------------------===//
583
584AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
585 return pImpl && hasAttributes(Idx) ?
586 AttributeSet::get(pImpl->getContext(),
587 ArrayRef<std::pair<unsigned, Attribute> >(
588 std::make_pair(Idx, getAttributes(Idx)))) :
589 AttributeSet();
590}
591
592AttributeSet AttributeSet::getRetAttributes() const {
593 return pImpl && hasAttributes(ReturnIndex) ?
594 AttributeSet::get(pImpl->getContext(),
595 ArrayRef<std::pair<unsigned, Attribute> >(
596 std::make_pair(ReturnIndex,
597 getAttributes(ReturnIndex)))) :
598 AttributeSet();
599}
600
601AttributeSet AttributeSet::getFnAttributes() const {
602 return pImpl && hasAttributes(FunctionIndex) ?
603 AttributeSet::get(pImpl->getContext(),
604 ArrayRef<std::pair<unsigned, Attribute> >(
605 std::make_pair(FunctionIndex,
606 getAttributes(FunctionIndex)))) :
607 AttributeSet();
608}
609
610bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
611 return getAttributes(Index).hasAttribute(Kind);
612}
613
614bool AttributeSet::hasAttributes(unsigned Index) const {
615 return getAttributes(Index).hasAttributes();
616}
617
618/// \brief Return true if the specified attribute is set for at least one
619/// parameter or for the return value.
620bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
621 if (pImpl == 0) return false;
622
623 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
624 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
625 IE = pImpl->end(I); II != IE; ++II)
626 if (II->hasAttribute(Attr))
627 return true;
628
629 return false;
630}
631
632unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
633 return getAttributes(Idx).getAlignment();
634}
635
636unsigned AttributeSet::getStackAlignment(unsigned Index) const {
637 return getAttributes(Index).getStackAlignment();
638}
639
640std::string AttributeSet::getAsString(unsigned Index) const {
641 return getAttributes(Index).getAsString();
642}
643
644/// \brief The attributes for the specified index are returned.
645///
646/// FIXME: This shouldn't return 'Attribute'.
647Attribute AttributeSet::getAttributes(unsigned Idx) const {
648 if (pImpl == 0) return Attribute();
649
650 // Loop through to find the attribute we want.
651 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
652 if (pImpl->getSlotIndex(I) != Idx) continue;
653
654 AttrBuilder B;
655 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
656 IE = pImpl->end(I); II != IE; ++II)
657 B.addAttributes(*II);
658 return Attribute::get(pImpl->getContext(), B);
659 }
660
661 return Attribute();
662}
663
664//===----------------------------------------------------------------------===//
665// AttributeSet Introspection Methods
666//===----------------------------------------------------------------------===//
667
668/// \brief Return the number of slots used in this attribute list. This is the
669/// number of arguments that have an attribute set on them (including the
670/// function itself).
671unsigned AttributeSet::getNumSlots() const {
672 return pImpl ? pImpl->getNumAttributes() : 0;
673}
674
675uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
676 assert(pImpl && Slot < pImpl->getNumAttributes() &&
677 "Slot # out of range!");
678 return pImpl->getSlotIndex(Slot);
679}
680
681AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
682 assert(pImpl && Slot < pImpl->getNumAttributes() &&
683 "Slot # out of range!");
684 return pImpl->getSlotAttributes(Slot);
685}
686
687uint64_t AttributeSet::Raw(unsigned Index) const {
688 // FIXME: Remove this.
689 return pImpl ? pImpl->Raw(Index) : 0;
690}
691
692void AttributeSet::dump() const {
693 dbgs() << "PAL[\n";
694
695 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
696 uint64_t Index = getSlotIndex(i);
697 dbgs() << " { ";
698 if (Index == ~0U)
699 dbgs() << "~0U";
700 else
701 dbgs() << Index;
702 dbgs() << " => " << getAsString(Index) << " }\n";
703 }
704
705 dbgs() << "]\n";
706}
707
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000708//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000709// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000710//===----------------------------------------------------------------------===//
711
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000712AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
713 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000714 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000715 if (!pImpl) return;
716
Bill Wendling73bc4522013-01-28 00:21:34 +0000717 AttrBuilder B;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000718
Bill Wendling73bc4522013-01-28 00:21:34 +0000719 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
720 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000721
Bill Wendling73bc4522013-01-28 00:21:34 +0000722 for (AttributeSetNode::const_iterator II = pImpl->begin(I),
723 IE = pImpl->end(I); II != IE; ++II)
724 B.addAttributes(*II);
725
726 break;
727 }
728
729 if (!B.hasAttributes()) return;
730
731 uint64_t Mask = B.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000732
Bill Wendling956f1342013-01-18 21:11:39 +0000733 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
734 I = Attribute::AttrKind(I + 1)) {
735 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
736 Attrs.insert(I);
737
738 if (I == Attribute::Alignment)
739 Alignment = 1ULL << ((A >> 16) - 1);
740 else if (I == Attribute::StackAlignment)
741 StackAlignment = 1ULL << ((A >> 26)-1);
742 }
743 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000744}
745
Bill Wendling03198882013-01-04 23:27:34 +0000746void AttrBuilder::clear() {
747 Attrs.clear();
748 Alignment = StackAlignment = 0;
749}
750
751AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
752 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000753 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000754}
755
Bill Wendling03198882013-01-04 23:27:34 +0000756AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
757 Attrs.erase(Val);
758 if (Val == Attribute::Alignment)
759 Alignment = 0;
760 else if (Val == Attribute::StackAlignment)
761 StackAlignment = 0;
762
Bill Wendlinga19a5302012-10-14 04:10:01 +0000763 return *this;
764}
765
Bill Wendling49f60602013-01-28 05:23:28 +0000766AttrBuilder &AttrBuilder::addAttributes(Attribute Attr) {
767 uint64_t Mask = Attr.Raw();
768
769 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
770 I = Attribute::AttrKind(I + 1))
771 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
772 Attrs.insert(I);
773
774 if (Attr.getAlignment())
775 Alignment = Attr.getAlignment();
776 if (Attr.getStackAlignment())
777 StackAlignment = Attr.getStackAlignment();
778 return *this;
779}
780
781AttrBuilder &AttrBuilder::removeAttributes(Attribute A) {
782 uint64_t Mask = A.Raw();
783
784 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
785 I = Attribute::AttrKind(I + 1)) {
786 if (Mask & AttributeImpl::getAttrMask(I)) {
787 Attrs.erase(I);
788
789 if (I == Attribute::Alignment)
790 Alignment = 0;
791 else if (I == Attribute::StackAlignment)
792 StackAlignment = 0;
793 }
794 }
795
796 return *this;
797}
798
Bill Wendling702cc912012-10-15 20:35:56 +0000799AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000800 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000801
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000802 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
803 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000804
805 Attrs.insert(Attribute::Alignment);
806 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000807 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000808}
809
Bill Wendling03198882013-01-04 23:27:34 +0000810AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
811 // Default alignment, allow the target to define how to align it.
812 if (Align == 0) return *this;
813
814 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
815 assert(Align <= 0x100 && "Alignment too large.");
816
817 Attrs.insert(Attribute::StackAlignment);
818 StackAlignment = Align;
819 return *this;
820}
821
Bill Wendling22bd6412013-01-03 01:54:39 +0000822bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000823 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000824}
825
Bill Wendling702cc912012-10-15 20:35:56 +0000826bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000827 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000828}
Bill Wendling60507d52013-01-04 20:54:35 +0000829
Bill Wendling034b94b2012-12-19 07:18:57 +0000830bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000831 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000832}
Bill Wendling60507d52013-01-04 20:54:35 +0000833
Bill Wendling702cc912012-10-15 20:35:56 +0000834bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000835 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000836}
837
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000838bool AttrBuilder::operator==(const AttrBuilder &B) {
839 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
840 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
841 return This == That;
842}
843
844AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
845 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
846 I = Attribute::AttrKind(I + 1)) {
847 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
848 Attrs.insert(I);
849
850 if (I == Attribute::Alignment)
851 Alignment = 1ULL << ((A >> 16) - 1);
852 else if (I == Attribute::StackAlignment)
853 StackAlignment = 1ULL << ((A >> 26)-1);
854 }
855 }
856
857 return *this;
858}
859
Bill Wendling1db9b692013-01-09 23:36:50 +0000860uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000861 uint64_t Mask = 0;
862
863 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
864 E = Attrs.end(); I != E; ++I) {
865 Attribute::AttrKind Kind = *I;
866
867 if (Kind == Attribute::Alignment)
868 Mask |= (Log2_32(Alignment) + 1) << 16;
869 else if (Kind == Attribute::StackAlignment)
870 Mask |= (Log2_32(StackAlignment) + 1) << 26;
871 else
872 Mask |= AttributeImpl::getAttrMask(Kind);
873 }
874
875 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000876}
877
Bill Wendling8e47daf2013-01-25 23:09:36 +0000878//===----------------------------------------------------------------------===//
879// AttributeFuncs Function Defintions
880//===----------------------------------------------------------------------===//
881
882Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
883 AttrBuilder Incompatible;
884
885 if (!Ty->isIntegerTy())
886 // Attribute that only apply to integers.
887 Incompatible.addAttribute(Attribute::SExt)
888 .addAttribute(Attribute::ZExt);
889
890 if (!Ty->isPointerTy())
891 // Attribute that only apply to pointers.
892 Incompatible.addAttribute(Attribute::ByVal)
893 .addAttribute(Attribute::Nest)
894 .addAttribute(Attribute::NoAlias)
895 .addAttribute(Attribute::NoCapture)
896 .addAttribute(Attribute::StructRet);
897
898 return Attribute::get(Ty->getContext(), Incompatible);
899}
900
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000901/// \brief This returns an integer containing an encoding of all the LLVM
902/// attributes found in the given attribute bitset. Any change to this encoding
903/// is a breaking change to bitcode compatibility.
Bill Wendling8e47daf2013-01-25 23:09:36 +0000904uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
905 unsigned Index) {
906 // FIXME: It doesn't make sense to store the alignment information as an
907 // expanded out value, we should store it as a log2 value. However, we can't
908 // just change that here without breaking bitcode compatibility. If this ever
909 // becomes a problem in practice, we should introduce new tag numbers in the
910 // bitcode file and have those tags use a more efficiently encoded alignment
911 // field.
912
913 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
914 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
915 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
916 if (Attrs.hasAttribute(Index, Attribute::Alignment))
917 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
918 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
919 return EncodedAttrs;
920}
921
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000922/// \brief This returns an attribute bitset containing the LLVM attributes that
923/// have been decoded from the given integer. This function must stay in sync
924/// with 'encodeLLVMAttributesForBitcode'.
Bill Wendling8e47daf2013-01-25 23:09:36 +0000925Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
926 uint64_t EncodedAttrs){
927 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
928 // the bits above 31 down by 11 bits.
929 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
930 assert((!Alignment || isPowerOf2_32(Alignment)) &&
931 "Alignment must be a power of two.");
932
933 AttrBuilder B(EncodedAttrs & 0xffff);
934 if (Alignment)
935 B.addAlignmentAttr(Alignment);
936 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
937 return Attribute::get(C, B);
938}
939