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