blob: 4024410b42f6b58f04ad18285dfd23b53a09ea45 [file] [log] [blame]
Devang Patel4c758ea2008-09-25 21:00:45 +00001//===-- Attributes.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//
Devang Patel4c758ea2008-09-25 21:00:45 +000010// This file implements the AttributesList class and Attribute utilities.
Chris Lattner3e13b8c2008-01-02 23:42:30 +000011//
12//===----------------------------------------------------------------------===//
13
Devang Patelba3fa6c2008-09-23 23:03:40 +000014#include "llvm/Attributes.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000015#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000016#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Atomic.h"
20#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000021#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000022#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024using namespace llvm;
25
Chris Lattner8a923e72008-03-12 17:45:29 +000026//===----------------------------------------------------------------------===//
Bill Wendling73ea2de2012-10-08 21:47:17 +000027// Attributes Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000028//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000029
Bill Wendling73ea2de2012-10-08 21:47:17 +000030Attributes::Attributes(uint64_t Val) : Attrs(Val) {}
31
32Attributes::Attributes(Attribute::AttrConst Val) : Attrs(Val.v) {}
33
34Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {}
35
Bill Wendlingc6daefa2012-10-08 23:27:46 +000036Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
37
Bill Wendling68d24012012-10-08 22:20:14 +000038// FIXME: This is temporary until we have implemented the uniquified version of
39// AttributesImpl.
40Attributes Attributes::get(Attributes::Builder &B) {
41 return Attributes(B.Bits);
42}
43
Bill Wendling73ea2de2012-10-08 21:47:17 +000044Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
45 // If there are no attributes, return an empty Attributes class.
46 if (B.Bits == 0)
47 return Attributes();
48
49 // Otherwise, build a key to look up the existing attributes.
50 LLVMContextImpl *pImpl = Context.pImpl;
51 FoldingSetNodeID ID;
52 ID.AddInteger(B.Bits);
53
54 void *InsertPoint;
55 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
56
57 if (!PA) {
58 // If we didn't find any existing attributes of the same shape then create a
59 // new one and insert it.
60 PA = new AttributesImpl(B.Bits);
61 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
62 }
63
64 // Return the AttributesList that we found or created.
65 return Attributes(PA);
66}
67
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000068bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000069 return Attrs.hasAttributes(A);
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000070}
Bill Wendlingc9b22d72012-10-09 07:45:08 +000071
72bool Attributes::hasAttribute(AttrVal Val) const {
73 return Attrs.hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000074}
75
76/// This returns the alignment field of an attribute as a byte alignment value.
77unsigned Attributes::getAlignment() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +000078 if (!hasAttribute(Attributes::Alignment))
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +000080 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000081}
82
83/// This returns the stack alignment field of an attribute as a byte alignment
84/// value.
85unsigned Attributes::getStackAlignment() const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +000086 if (!hasAttribute(Attributes::StackAlignment))
Bill Wendlingabf3feb2012-10-05 06:44:41 +000087 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +000088 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000089}
90
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000091bool Attributes::isEmptyOrSingleton() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000092 return Attrs.isEmptyOrSingleton();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000093}
94
Bill Wendling73ea2de2012-10-08 21:47:17 +000095Attributes Attributes::operator | (const Attributes &A) const {
96 return Attributes(Raw() | A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000097}
Bill Wendling73ea2de2012-10-08 21:47:17 +000098Attributes Attributes::operator & (const Attributes &A) const {
99 return Attributes(Raw() & A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000100}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000101Attributes Attributes::operator ^ (const Attributes &A) const {
102 return Attributes(Raw() ^ A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000103}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000104Attributes &Attributes::operator |= (const Attributes &A) {
105 Attrs.Bits |= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000106 return *this;
107}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000108Attributes &Attributes::operator &= (const Attributes &A) {
109 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000110 return *this;
111}
112Attributes Attributes::operator ~ () const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000113 return Attributes(~Raw());
114}
115
116uint64_t Attributes::Raw() const {
117 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000118}
119
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000120Attributes Attributes::typeIncompatible(Type *Ty) {
121 Attributes::Builder Incompatible;
122
123 if (!Ty->isIntegerTy()) {
124 // Attributes that only apply to integers.
125 Incompatible.addSExtAttr();
126 Incompatible.addZExtAttr();
127 }
128
129 if (!Ty->isPointerTy()) {
130 // Attributes that only apply to pointers.
131 Incompatible.addByValAttr();
132 Incompatible.addNestAttr();
133 Incompatible.addNoAliasAttr();
134 Incompatible.addNoCaptureAttr();
135 Incompatible.addStructRetAttr();
136 }
137
138 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
139}
140
Bill Wendlingde74cf52012-09-20 14:44:42 +0000141std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000148 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000150 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000152 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000154 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000158 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000160 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000162 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000164 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000166 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000168 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000170 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000171 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000172 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000173 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000174 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000175 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000176 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000177 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000178 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000179 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000180 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000181 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000182 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000183 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000184 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000185 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000186 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000187 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000188 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000189 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000190 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000191 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000192 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000193 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000194 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000195 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000196 Result += ") ";
197 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000198 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000199 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000200 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000201 Result += " ";
202 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000203 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000204 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000205 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000206 return Result;
207}
208
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000209//===----------------------------------------------------------------------===//
210// Attributes::Builder Implementation
211//===----------------------------------------------------------------------===//
212
213void Attributes::Builder::addAddressSafetyAttr() {
214 Bits |= Attribute::AddressSafety_i;
215}
216void Attributes::Builder::addAlwaysInlineAttr() {
217 Bits |= Attribute::AlwaysInline_i;
218}
219void Attributes::Builder::addByValAttr() {
220 Bits |= Attribute::ByVal_i;
221}
222void Attributes::Builder::addInlineHintAttr() {
223 Bits |= Attribute::InlineHint_i;
224}
225void Attributes::Builder::addInRegAttr() {
226 Bits |= Attribute::InReg_i;
227}
228void Attributes::Builder::addNakedAttr() {
229 Bits |= Attribute::Naked_i;
230}
231void Attributes::Builder::addNestAttr() {
232 Bits |= Attribute::Nest_i;
233}
234void Attributes::Builder::addNoAliasAttr() {
235 Bits |= Attribute::NoAlias_i;
236}
237void Attributes::Builder::addNoCaptureAttr() {
238 Bits |= Attribute::NoCapture_i;
239}
240void Attributes::Builder::addNoImplicitFloatAttr() {
241 Bits |= Attribute::NoImplicitFloat_i;
242}
243void Attributes::Builder::addNoInlineAttr() {
244 Bits |= Attribute::NoInline_i;
245}
246void Attributes::Builder::addNonLazyBindAttr() {
247 Bits |= Attribute::NonLazyBind_i;
248}
249void Attributes::Builder::addNoRedZoneAttr() {
250 Bits |= Attribute::NoRedZone_i;
251}
252void Attributes::Builder::addNoReturnAttr() {
253 Bits |= Attribute::NoReturn_i;
254}
255void Attributes::Builder::addNoUnwindAttr() {
256 Bits |= Attribute::NoUnwind_i;
257}
258void Attributes::Builder::addOptimizeForSizeAttr() {
259 Bits |= Attribute::OptimizeForSize_i;
260}
261void Attributes::Builder::addReadNoneAttr() {
262 Bits |= Attribute::ReadNone_i;
263}
264void Attributes::Builder::addReadOnlyAttr() {
265 Bits |= Attribute::ReadOnly_i;
266}
267void Attributes::Builder::addReturnsTwiceAttr() {
268 Bits |= Attribute::ReturnsTwice_i;
269}
270void Attributes::Builder::addSExtAttr() {
271 Bits |= Attribute::SExt_i;
272}
273void Attributes::Builder::addStackProtectAttr() {
274 Bits |= Attribute::StackProtect_i;
275}
276void Attributes::Builder::addStackProtectReqAttr() {
277 Bits |= Attribute::StackProtectReq_i;
278}
279void Attributes::Builder::addStructRetAttr() {
280 Bits |= Attribute::StructRet_i;
281}
282void Attributes::Builder::addUWTableAttr() {
283 Bits |= Attribute::UWTable_i;
284}
285void Attributes::Builder::addZExtAttr() {
286 Bits |= Attribute::ZExt_i;
287}
288
289void Attributes::Builder::addAlignmentAttr(unsigned Align) {
290 if (Align == 0) return;
291 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
292 assert(Align <= 0x40000000 && "Alignment too large.");
293 Bits |= (Log2_32(Align) + 1) << 16;
294}
295void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
296 // Default alignment, allow the target to define how to align it.
297 if (Align == 0) return;
298 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
299 assert(Align <= 0x100 && "Alignment too large.");
300 Bits |= (Log2_32(Align) + 1) << 26;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000301}
302
Bill Wendling70f39172012-10-09 00:01:21 +0000303void Attributes::Builder::removeAttributes(const Attributes &A) {
304 Bits &= ~A.Raw();
305}
306
Bill Wendling73ea2de2012-10-08 21:47:17 +0000307void Attributes::Builder::removeAddressSafetyAttr() {
308 Bits &= ~Attribute::AddressSafety_i;
309}
310void Attributes::Builder::removeAlwaysInlineAttr() {
311 Bits &= ~Attribute::AlwaysInline_i;
312}
313void Attributes::Builder::removeByValAttr() {
314 Bits &= ~Attribute::ByVal_i;
315}
316void Attributes::Builder::removeInlineHintAttr() {
317 Bits &= ~Attribute::InlineHint_i;
318}
319void Attributes::Builder::removeInRegAttr() {
320 Bits &= ~Attribute::InReg_i;
321}
322void Attributes::Builder::removeNakedAttr() {
323 Bits &= ~Attribute::Naked_i;
324}
325void Attributes::Builder::removeNestAttr() {
326 Bits &= ~Attribute::Nest_i;
327}
328void Attributes::Builder::removeNoAliasAttr() {
329 Bits &= ~Attribute::NoAlias_i;
330}
331void Attributes::Builder::removeNoCaptureAttr() {
332 Bits &= ~Attribute::NoCapture_i;
333}
334void Attributes::Builder::removeNoImplicitFloatAttr() {
335 Bits &= ~Attribute::NoImplicitFloat_i;
336}
337void Attributes::Builder::removeNoInlineAttr() {
338 Bits &= ~Attribute::NoInline_i;
339}
340void Attributes::Builder::removeNonLazyBindAttr() {
341 Bits &= ~Attribute::NonLazyBind_i;
342}
343void Attributes::Builder::removeNoRedZoneAttr() {
344 Bits &= ~Attribute::NoRedZone_i;
345}
346void Attributes::Builder::removeNoReturnAttr() {
347 Bits &= ~Attribute::NoReturn_i;
348}
349void Attributes::Builder::removeNoUnwindAttr() {
350 Bits &= ~Attribute::NoUnwind_i;
351}
352void Attributes::Builder::removeOptimizeForSizeAttr() {
353 Bits &= ~Attribute::OptimizeForSize_i;
354}
355void Attributes::Builder::removeReadNoneAttr() {
356 Bits &= ~Attribute::ReadNone_i;
357}
358void Attributes::Builder::removeReadOnlyAttr() {
359 Bits &= ~Attribute::ReadOnly_i;
360}
361void Attributes::Builder::removeReturnsTwiceAttr() {
362 Bits &= ~Attribute::ReturnsTwice_i;
363}
364void Attributes::Builder::removeSExtAttr() {
365 Bits &= ~Attribute::SExt_i;
366}
367void Attributes::Builder::removeStackProtectAttr() {
368 Bits &= ~Attribute::StackProtect_i;
369}
370void Attributes::Builder::removeStackProtectReqAttr() {
371 Bits &= ~Attribute::StackProtectReq_i;
372}
373void Attributes::Builder::removeStructRetAttr() {
374 Bits &= ~Attribute::StructRet_i;
375}
376void Attributes::Builder::removeUWTableAttr() {
377 Bits &= ~Attribute::UWTable_i;
378}
379void Attributes::Builder::removeZExtAttr() {
380 Bits &= ~Attribute::ZExt_i;
381}
382
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000383void Attributes::Builder::removeAlignmentAttr() {
384 Bits &= ~Attribute::Alignment_i;
385}
386void Attributes::Builder::removeStackAlignmentAttr() {
387 Bits &= ~Attribute::StackAlignment_i;
388}
389
390bool Attributes::Builder::hasAttributes() const {
391 return Bits != 0;
392}
Bill Wendling70f39172012-10-09 00:01:21 +0000393bool Attributes::Builder::hasAttributes(const Attributes &A) const {
394 return Bits & A.Raw();
395}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000396bool Attributes::Builder::hasAlignmentAttr() const {
397 return Bits & Attribute::Alignment_i;
398}
399
400uint64_t Attributes::Builder::getAlignment() const {
401 if (!hasAlignmentAttr())
402 return 0;
403 return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
404}
405
Chris Lattner8a923e72008-03-12 17:45:29 +0000406//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000407// AttributeImpl Definition
408//===----------------------------------------------------------------------===//
409
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000410uint64_t AttributesImpl::getAttrMask(uint64_t Val) const {
411 switch (Val) {
412 case Attributes::None: return 0;
413 case Attributes::ZExt: return 1 << 0;
414 case Attributes::SExt: return 1 << 1;
415 case Attributes::NoReturn: return 1 << 2;
416 case Attributes::InReg: return 1 << 3;
417 case Attributes::StructRet: return 1 << 4;
418 case Attributes::NoUnwind: return 1 << 5;
419 case Attributes::NoAlias: return 1 << 6;
420 case Attributes::ByVal: return 1 << 7;
421 case Attributes::Nest: return 1 << 8;
422 case Attributes::ReadNone: return 1 << 9;
423 case Attributes::ReadOnly: return 1 << 10;
424 case Attributes::NoInline: return 1 << 11;
425 case Attributes::AlwaysInline: return 1 << 12;
426 case Attributes::OptimizeForSize: return 1 << 13;
427 case Attributes::StackProtect: return 1 << 14;
428 case Attributes::StackProtectReq: return 1 << 15;
429 case Attributes::Alignment: return 31 << 16;
430 case Attributes::NoCapture: return 1 << 21;
431 case Attributes::NoRedZone: return 1 << 22;
432 case Attributes::NoImplicitFloat: return 1 << 23;
433 case Attributes::Naked: return 1 << 24;
434 case Attributes::InlineHint: return 1 << 25;
435 case Attributes::StackAlignment: return 7 << 26;
436 case Attributes::ReturnsTwice: return 1 << 29;
437 case Attributes::UWTable: return 1 << 30;
438 case Attributes::NonLazyBind: return 1U << 31;
439 case Attributes::AddressSafety: return 1ULL << 32;
440 }
441 llvm_unreachable("Unsupported attribute type");
442}
443
Bill Wendling73ea2de2012-10-08 21:47:17 +0000444bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000445 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000446}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000447
Bill Wendling73ea2de2012-10-08 21:47:17 +0000448bool AttributesImpl::hasAttributes() const {
449 return Bits != 0;
450}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000451
Bill Wendling73ea2de2012-10-08 21:47:17 +0000452bool AttributesImpl::hasAttributes(const Attributes &A) const {
453 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
454}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000455
Bill Wendling73ea2de2012-10-08 21:47:17 +0000456uint64_t AttributesImpl::getAlignment() const {
457 return Bits & Attribute::Alignment_i;
458}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000459
Bill Wendling73ea2de2012-10-08 21:47:17 +0000460uint64_t AttributesImpl::getStackAlignment() const {
461 return Bits & Attribute::StackAlignment_i;
462}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000463
Bill Wendling73ea2de2012-10-08 21:47:17 +0000464bool AttributesImpl::isEmptyOrSingleton() const {
465 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000466}
467
468//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000469// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000470//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000471
Owen Anderson2e831892010-11-18 18:59:13 +0000472namespace llvm {
473 class AttributeListImpl;
474}
475
476static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000477
Chris Lattner8a923e72008-03-12 17:45:29 +0000478namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000479static ManagedStatic<sys::SmartMutex<true> > ALMutex;
480
Devang Patel00095052008-09-24 00:29:49 +0000481class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000482 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000483
Devang Patel4c758ea2008-09-25 21:00:45 +0000484 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000485 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
486 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000487 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000488public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000489 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000490
Chris Lattner3cb6f832012-05-28 01:47:44 +0000491 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
492 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000493 RefCount = 0;
494 }
495
Owen Anderson9b14a252010-11-09 00:27:03 +0000496 void AddRef() {
497 sys::SmartScopedLock<true> Lock(*ALMutex);
498 ++RefCount;
499 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000500 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000501 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000502 if (!AttributesLists.isConstructed())
503 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000504 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000505 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000506 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000507 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000508
509 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000510 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000511 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000512 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
513 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
514 ID.AddInteger(Attrs[i].Attrs.Raw());
515 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000516 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000517 }
518};
519}
520
Devang Patel00095052008-09-24 00:29:49 +0000521AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000522 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000523 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000524}
525
526
Chris Lattner3cb6f832012-05-28 01:47:44 +0000527AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000528 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000529 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000530 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000531
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000532#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000533 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000534 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000535 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000536 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000537 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000538 }
539#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000540
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000541 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000542 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000543 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000544 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000545
546 sys::SmartScopedLock<true> Lock(*ALMutex);
547
Devang Patel00095052008-09-24 00:29:49 +0000548 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000549 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000550
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000551 // If we didn't find any existing attributes of the same shape then
552 // create a new one and insert it.
553 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000554 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000555 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000556 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000557
Devang Patel4c758ea2008-09-25 21:00:45 +0000558 // Return the AttributesList that we found or created.
559 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000560}
561
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000562
Chris Lattner8a923e72008-03-12 17:45:29 +0000563//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000564// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000565//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000566
Devang Patel4c758ea2008-09-25 21:00:45 +0000567AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000568 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000569}
570
Devang Patel4c758ea2008-09-25 21:00:45 +0000571AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
572 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000573}
574
Devang Patel4c758ea2008-09-25 21:00:45 +0000575const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000576 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000577 if (AttrList == RHS.AttrList) return *this;
578 if (AttrList) AttrList->DropRef();
579 AttrList = RHS.AttrList;
580 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000581 return *this;
582}
583
Devang Patel4c758ea2008-09-25 21:00:45 +0000584AttrListPtr::~AttrListPtr() {
585 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000586}
587
588/// getNumSlots - Return the number of slots used in this attribute list.
589/// This is the number of arguments that have an attribute set on them
590/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000591unsigned AttrListPtr::getNumSlots() const {
592 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000593}
594
Devang Patel4c758ea2008-09-25 21:00:45 +0000595/// getSlot - Return the AttributeWithIndex at the specified slot. This
596/// holds a number plus a set of attributes.
597const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
598 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
599 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000600}
601
602
Devang Patel4c758ea2008-09-25 21:00:45 +0000603/// getAttributes - The attributes for the specified index are
604/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000605/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000606Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000607 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000608
Devang Patel4c758ea2008-09-25 21:00:45 +0000609 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000610 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
611 if (Attrs[i].Index == Idx)
612 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000613
614 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000615}
616
617/// hasAttrSomewhere - Return true if the specified attribute is set for at
618/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000619bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
620 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000621
Devang Patel4c758ea2008-09-25 21:00:45 +0000622 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000623 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000624 if (Attrs[i].Attrs.hasAttributes(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000625 return true;
626 return false;
627}
628
Bill Wendling70f39172012-10-09 00:01:21 +0000629unsigned AttrListPtr::getNumAttrs() const {
630 return AttrList ? AttrList->Attrs.size() : 0;
631}
632
633Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
634 assert(AttrList && "Trying to get an attribute from an empty list!");
635 assert(i < AttrList->Attrs.size() && "Index out of range!");
636 return AttrList->Attrs[i].Attrs;
637}
Chris Lattner8a923e72008-03-12 17:45:29 +0000638
Devang Patel4c758ea2008-09-25 21:00:45 +0000639AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
640 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000641#ifndef NDEBUG
642 // FIXME it is not obvious how this should work for alignment.
643 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000644 unsigned OldAlign = OldAttrs.getAlignment();
645 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000646 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000647 "Attempt to change alignment!");
648#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000649
Devang Patelba3fa6c2008-09-23 23:03:40 +0000650 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000651 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000652 return *this;
653
Devang Patel4c758ea2008-09-25 21:00:45 +0000654 SmallVector<AttributeWithIndex, 8> NewAttrList;
655 if (AttrList == 0)
656 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000657 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000658 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000659 unsigned i = 0, e = OldAttrList.size();
660 // Copy attributes for arguments before this one.
661 for (; i != e && OldAttrList[i].Index < Idx; ++i)
662 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000663
Chris Lattner8a923e72008-03-12 17:45:29 +0000664 // If there are attributes already at this index, merge them in.
665 if (i != e && OldAttrList[i].Index == Idx) {
666 Attrs |= OldAttrList[i].Attrs;
667 ++i;
668 }
669
Devang Patel4c758ea2008-09-25 21:00:45 +0000670 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000671
672 // Copy attributes for arguments after this one.
673 NewAttrList.insert(NewAttrList.end(),
674 OldAttrList.begin()+i, OldAttrList.end());
675 }
676
Chris Lattner3cb6f832012-05-28 01:47:44 +0000677 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000678}
679
Devang Patel4c758ea2008-09-25 21:00:45 +0000680AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000681#ifndef NDEBUG
682 // FIXME it is not obvious how this should work for alignment.
683 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000684 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
685 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000686#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000687 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000688
Devang Patel4c758ea2008-09-25 21:00:45 +0000689 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000690 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000691 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000692 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000693
Devang Patel4c758ea2008-09-25 21:00:45 +0000694 SmallVector<AttributeWithIndex, 8> NewAttrList;
695 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000696 unsigned i = 0, e = OldAttrList.size();
697
698 // Copy attributes for arguments before this one.
699 for (; i != e && OldAttrList[i].Index < Idx; ++i)
700 NewAttrList.push_back(OldAttrList[i]);
701
702 // If there are attributes already at this index, merge them in.
703 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
704 Attrs = OldAttrList[i].Attrs & ~Attrs;
705 ++i;
706 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000707 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000708
709 // Copy attributes for arguments after this one.
710 NewAttrList.insert(NewAttrList.end(),
711 OldAttrList.begin()+i, OldAttrList.end());
712
Chris Lattner3cb6f832012-05-28 01:47:44 +0000713 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000714}
715
Devang Patel4c758ea2008-09-25 21:00:45 +0000716void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000717 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000718 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000719 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000720 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000721 }
722
David Greenef7014732010-01-05 01:29:58 +0000723 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000724}