blob: 1fcebdf5ba281eb8c28da65730c5e38afb3d44cd [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 Wendling68d24012012-10-08 22:20:14 +000036// FIXME: This is temporary until we have implemented the uniquified version of
37// AttributesImpl.
38Attributes Attributes::get(Attributes::Builder &B) {
39 return Attributes(B.Bits);
40}
41
Bill Wendling73ea2de2012-10-08 21:47:17 +000042Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
43 // If there are no attributes, return an empty Attributes class.
44 if (B.Bits == 0)
45 return Attributes();
46
47 // Otherwise, build a key to look up the existing attributes.
48 LLVMContextImpl *pImpl = Context.pImpl;
49 FoldingSetNodeID ID;
50 ID.AddInteger(B.Bits);
51
52 void *InsertPoint;
53 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
54
55 if (!PA) {
56 // If we didn't find any existing attributes of the same shape then create a
57 // new one and insert it.
58 PA = new AttributesImpl(B.Bits);
59 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
60 }
61
62 // Return the AttributesList that we found or created.
63 return Attributes(PA);
64}
65
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000066bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000067 return Attrs.hasAttributes(A);
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000068}
Bill Wendlingabf3feb2012-10-05 06:44:41 +000069bool Attributes::hasAddressSafetyAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000070 return Attrs.hasAttribute(Attribute::AddressSafety_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000071}
72bool Attributes::hasAlignmentAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000073 return Attrs.hasAttribute(Attribute::Alignment_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000074}
75bool Attributes::hasAlwaysInlineAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000076 return Attrs.hasAttribute(Attribute::AlwaysInline_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000077}
78bool Attributes::hasByValAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000079 return Attrs.hasAttribute(Attribute::ByVal_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000080}
81bool Attributes::hasInlineHintAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000082 return Attrs.hasAttribute(Attribute::InlineHint_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000083}
84bool Attributes::hasInRegAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000085 return Attrs.hasAttribute(Attribute::InReg_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000086}
87bool Attributes::hasNakedAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000088 return Attrs.hasAttribute(Attribute::Naked_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000089}
90bool Attributes::hasNestAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000091 return Attrs.hasAttribute(Attribute::Nest_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000092}
93bool Attributes::hasNoAliasAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000094 return Attrs.hasAttribute(Attribute::NoAlias_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000095}
96bool Attributes::hasNoCaptureAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +000097 return Attrs.hasAttribute(Attribute::NoCapture_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000098}
99bool Attributes::hasNoImplicitFloatAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000100 return Attrs.hasAttribute(Attribute::NoImplicitFloat_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000101}
102bool Attributes::hasNoInlineAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000103 return Attrs.hasAttribute(Attribute::NoInline_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000104}
105bool Attributes::hasNonLazyBindAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000106 return Attrs.hasAttribute(Attribute::NonLazyBind_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000107}
108bool Attributes::hasNoRedZoneAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000109 return Attrs.hasAttribute(Attribute::NoRedZone_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000110}
111bool Attributes::hasNoReturnAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000112 return Attrs.hasAttribute(Attribute::NoReturn_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000113}
114bool Attributes::hasNoUnwindAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000115 return Attrs.hasAttribute(Attribute::NoUnwind_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000116}
117bool Attributes::hasOptimizeForSizeAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000118 return Attrs.hasAttribute(Attribute::OptimizeForSize_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000119}
120bool Attributes::hasReadNoneAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000121 return Attrs.hasAttribute(Attribute::ReadNone_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000122}
123bool Attributes::hasReadOnlyAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000124 return Attrs.hasAttribute(Attribute::ReadOnly_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000125}
126bool Attributes::hasReturnsTwiceAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000127 return Attrs.hasAttribute(Attribute::ReturnsTwice_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000128}
129bool Attributes::hasSExtAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000130 return Attrs.hasAttribute(Attribute::SExt_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000131}
132bool Attributes::hasStackAlignmentAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000133 return Attrs.hasAttribute(Attribute::StackAlignment_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000134}
135bool Attributes::hasStackProtectAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000136 return Attrs.hasAttribute(Attribute::StackProtect_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000137}
138bool Attributes::hasStackProtectReqAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000139 return Attrs.hasAttribute(Attribute::StackProtectReq_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000140}
141bool Attributes::hasStructRetAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000142 return Attrs.hasAttribute(Attribute::StructRet_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000143}
144bool Attributes::hasUWTableAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000145 return Attrs.hasAttribute(Attribute::UWTable_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000146}
147bool Attributes::hasZExtAttr() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000148 return Attrs.hasAttribute(Attribute::ZExt_i);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000149}
150
151/// This returns the alignment field of an attribute as a byte alignment value.
152unsigned Attributes::getAlignment() const {
153 if (!hasAlignmentAttr())
154 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000155 return 1U << ((Attrs.getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000156}
157
158/// This returns the stack alignment field of an attribute as a byte alignment
159/// value.
160unsigned Attributes::getStackAlignment() const {
161 if (!hasStackAlignmentAttr())
162 return 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000163 return 1U << ((Attrs.getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000164}
165
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000166bool Attributes::isEmptyOrSingleton() const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000167 return Attrs.isEmptyOrSingleton();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000168}
169
Bill Wendling73ea2de2012-10-08 21:47:17 +0000170Attributes Attributes::operator | (const Attributes &A) const {
171 return Attributes(Raw() | A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000172}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000173Attributes Attributes::operator & (const Attributes &A) const {
174 return Attributes(Raw() & A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000175}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000176Attributes Attributes::operator ^ (const Attributes &A) const {
177 return Attributes(Raw() ^ A.Raw());
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000178}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000179Attributes &Attributes::operator |= (const Attributes &A) {
180 Attrs.Bits |= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000181 return *this;
182}
Bill Wendling73ea2de2012-10-08 21:47:17 +0000183Attributes &Attributes::operator &= (const Attributes &A) {
184 Attrs.Bits &= A.Raw();
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000185 return *this;
186}
187Attributes Attributes::operator ~ () const {
Bill Wendling73ea2de2012-10-08 21:47:17 +0000188 return Attributes(~Raw());
189}
190
191uint64_t Attributes::Raw() const {
192 return Attrs.Bits;
Bill Wendlingbe7c6f22012-10-07 08:55:05 +0000193}
194
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000195Attributes Attributes::typeIncompatible(Type *Ty) {
196 Attributes::Builder Incompatible;
197
198 if (!Ty->isIntegerTy()) {
199 // Attributes that only apply to integers.
200 Incompatible.addSExtAttr();
201 Incompatible.addZExtAttr();
202 }
203
204 if (!Ty->isPointerTy()) {
205 // Attributes that only apply to pointers.
206 Incompatible.addByValAttr();
207 Incompatible.addNestAttr();
208 Incompatible.addNoAliasAttr();
209 Incompatible.addNoCaptureAttr();
210 Incompatible.addStructRetAttr();
211 }
212
213 return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get().
214}
215
Bill Wendlingde74cf52012-09-20 14:44:42 +0000216std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000217 std::string Result;
Bill Wendlingde74cf52012-09-20 14:44:42 +0000218 if (hasZExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000219 Result += "zeroext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000220 if (hasSExtAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000221 Result += "signext ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000222 if (hasNoReturnAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000223 Result += "noreturn ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000224 if (hasNoUnwindAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000225 Result += "nounwind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000226 if (hasUWTableAttr())
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000227 Result += "uwtable ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000228 if (hasReturnsTwiceAttr())
Rafael Espindolacc349c82011-10-03 14:45:37 +0000229 Result += "returns_twice ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000230 if (hasInRegAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000231 Result += "inreg ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000232 if (hasNoAliasAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000233 Result += "noalias ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000234 if (hasNoCaptureAttr())
Nick Lewycky8d69f482008-12-19 09:38:31 +0000235 Result += "nocapture ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000236 if (hasStructRetAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000237 Result += "sret ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000238 if (hasByValAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000239 Result += "byval ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000240 if (hasNestAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000241 Result += "nest ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000242 if (hasReadNoneAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000243 Result += "readnone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000244 if (hasReadOnlyAttr())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000245 Result += "readonly ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000246 if (hasOptimizeForSizeAttr())
Devang Patela05633e2008-09-26 22:53:05 +0000247 Result += "optsize ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000248 if (hasNoInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +0000249 Result += "noinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000250 if (hasInlineHintAttr())
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000251 Result += "inlinehint ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000252 if (hasAlwaysInlineAttr())
Devang Patela05633e2008-09-26 22:53:05 +0000253 Result += "alwaysinline ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000254 if (hasStackProtectAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000255 Result += "ssp ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000256 if (hasStackProtectReqAttr())
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000257 Result += "sspreq ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000258 if (hasNoRedZoneAttr())
Devang Patel72a4d2f2009-06-04 22:05:33 +0000259 Result += "noredzone ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000260 if (hasNoImplicitFloatAttr())
Devang Pateld1c7d342009-06-05 21:57:13 +0000261 Result += "noimplicitfloat ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000262 if (hasNakedAttr())
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000263 Result += "naked ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000264 if (hasNonLazyBindAttr())
John McCall4b7a8d62011-06-15 20:36:13 +0000265 Result += "nonlazybind ";
Bill Wendlingde74cf52012-09-20 14:44:42 +0000266 if (hasAddressSafetyAttr())
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000267 Result += "address_safety ";
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000268 if (hasStackAlignmentAttr()) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000269 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000270 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000271 Result += ") ";
272 }
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000273 if (hasAlignmentAttr()) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000274 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000275 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000276 Result += " ";
277 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000278 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000279 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000280 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000281 return Result;
282}
283
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000284//===----------------------------------------------------------------------===//
285// Attributes::Builder Implementation
286//===----------------------------------------------------------------------===//
287
288void Attributes::Builder::addAddressSafetyAttr() {
289 Bits |= Attribute::AddressSafety_i;
290}
291void Attributes::Builder::addAlwaysInlineAttr() {
292 Bits |= Attribute::AlwaysInline_i;
293}
294void Attributes::Builder::addByValAttr() {
295 Bits |= Attribute::ByVal_i;
296}
297void Attributes::Builder::addInlineHintAttr() {
298 Bits |= Attribute::InlineHint_i;
299}
300void Attributes::Builder::addInRegAttr() {
301 Bits |= Attribute::InReg_i;
302}
303void Attributes::Builder::addNakedAttr() {
304 Bits |= Attribute::Naked_i;
305}
306void Attributes::Builder::addNestAttr() {
307 Bits |= Attribute::Nest_i;
308}
309void Attributes::Builder::addNoAliasAttr() {
310 Bits |= Attribute::NoAlias_i;
311}
312void Attributes::Builder::addNoCaptureAttr() {
313 Bits |= Attribute::NoCapture_i;
314}
315void Attributes::Builder::addNoImplicitFloatAttr() {
316 Bits |= Attribute::NoImplicitFloat_i;
317}
318void Attributes::Builder::addNoInlineAttr() {
319 Bits |= Attribute::NoInline_i;
320}
321void Attributes::Builder::addNonLazyBindAttr() {
322 Bits |= Attribute::NonLazyBind_i;
323}
324void Attributes::Builder::addNoRedZoneAttr() {
325 Bits |= Attribute::NoRedZone_i;
326}
327void Attributes::Builder::addNoReturnAttr() {
328 Bits |= Attribute::NoReturn_i;
329}
330void Attributes::Builder::addNoUnwindAttr() {
331 Bits |= Attribute::NoUnwind_i;
332}
333void Attributes::Builder::addOptimizeForSizeAttr() {
334 Bits |= Attribute::OptimizeForSize_i;
335}
336void Attributes::Builder::addReadNoneAttr() {
337 Bits |= Attribute::ReadNone_i;
338}
339void Attributes::Builder::addReadOnlyAttr() {
340 Bits |= Attribute::ReadOnly_i;
341}
342void Attributes::Builder::addReturnsTwiceAttr() {
343 Bits |= Attribute::ReturnsTwice_i;
344}
345void Attributes::Builder::addSExtAttr() {
346 Bits |= Attribute::SExt_i;
347}
348void Attributes::Builder::addStackProtectAttr() {
349 Bits |= Attribute::StackProtect_i;
350}
351void Attributes::Builder::addStackProtectReqAttr() {
352 Bits |= Attribute::StackProtectReq_i;
353}
354void Attributes::Builder::addStructRetAttr() {
355 Bits |= Attribute::StructRet_i;
356}
357void Attributes::Builder::addUWTableAttr() {
358 Bits |= Attribute::UWTable_i;
359}
360void Attributes::Builder::addZExtAttr() {
361 Bits |= Attribute::ZExt_i;
362}
363
364void Attributes::Builder::addAlignmentAttr(unsigned Align) {
365 if (Align == 0) return;
366 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
367 assert(Align <= 0x40000000 && "Alignment too large.");
368 Bits |= (Log2_32(Align) + 1) << 16;
369}
370void Attributes::Builder::addStackAlignmentAttr(unsigned Align) {
371 // Default alignment, allow the target to define how to align it.
372 if (Align == 0) return;
373 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
374 assert(Align <= 0x100 && "Alignment too large.");
375 Bits |= (Log2_32(Align) + 1) << 26;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000376}
377
Bill Wendling73ea2de2012-10-08 21:47:17 +0000378void Attributes::Builder::removeAddressSafetyAttr() {
379 Bits &= ~Attribute::AddressSafety_i;
380}
381void Attributes::Builder::removeAlwaysInlineAttr() {
382 Bits &= ~Attribute::AlwaysInline_i;
383}
384void Attributes::Builder::removeByValAttr() {
385 Bits &= ~Attribute::ByVal_i;
386}
387void Attributes::Builder::removeInlineHintAttr() {
388 Bits &= ~Attribute::InlineHint_i;
389}
390void Attributes::Builder::removeInRegAttr() {
391 Bits &= ~Attribute::InReg_i;
392}
393void Attributes::Builder::removeNakedAttr() {
394 Bits &= ~Attribute::Naked_i;
395}
396void Attributes::Builder::removeNestAttr() {
397 Bits &= ~Attribute::Nest_i;
398}
399void Attributes::Builder::removeNoAliasAttr() {
400 Bits &= ~Attribute::NoAlias_i;
401}
402void Attributes::Builder::removeNoCaptureAttr() {
403 Bits &= ~Attribute::NoCapture_i;
404}
405void Attributes::Builder::removeNoImplicitFloatAttr() {
406 Bits &= ~Attribute::NoImplicitFloat_i;
407}
408void Attributes::Builder::removeNoInlineAttr() {
409 Bits &= ~Attribute::NoInline_i;
410}
411void Attributes::Builder::removeNonLazyBindAttr() {
412 Bits &= ~Attribute::NonLazyBind_i;
413}
414void Attributes::Builder::removeNoRedZoneAttr() {
415 Bits &= ~Attribute::NoRedZone_i;
416}
417void Attributes::Builder::removeNoReturnAttr() {
418 Bits &= ~Attribute::NoReturn_i;
419}
420void Attributes::Builder::removeNoUnwindAttr() {
421 Bits &= ~Attribute::NoUnwind_i;
422}
423void Attributes::Builder::removeOptimizeForSizeAttr() {
424 Bits &= ~Attribute::OptimizeForSize_i;
425}
426void Attributes::Builder::removeReadNoneAttr() {
427 Bits &= ~Attribute::ReadNone_i;
428}
429void Attributes::Builder::removeReadOnlyAttr() {
430 Bits &= ~Attribute::ReadOnly_i;
431}
432void Attributes::Builder::removeReturnsTwiceAttr() {
433 Bits &= ~Attribute::ReturnsTwice_i;
434}
435void Attributes::Builder::removeSExtAttr() {
436 Bits &= ~Attribute::SExt_i;
437}
438void Attributes::Builder::removeStackProtectAttr() {
439 Bits &= ~Attribute::StackProtect_i;
440}
441void Attributes::Builder::removeStackProtectReqAttr() {
442 Bits &= ~Attribute::StackProtectReq_i;
443}
444void Attributes::Builder::removeStructRetAttr() {
445 Bits &= ~Attribute::StructRet_i;
446}
447void Attributes::Builder::removeUWTableAttr() {
448 Bits &= ~Attribute::UWTable_i;
449}
450void Attributes::Builder::removeZExtAttr() {
451 Bits &= ~Attribute::ZExt_i;
452}
453
Chris Lattner8a923e72008-03-12 17:45:29 +0000454//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000455// AttributeImpl Definition
456//===----------------------------------------------------------------------===//
457
Bill Wendling73ea2de2012-10-08 21:47:17 +0000458bool AttributesImpl::hasAttribute(uint64_t A) const {
459 return (Bits & A) != 0;
460}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000461
Bill Wendling73ea2de2012-10-08 21:47:17 +0000462bool AttributesImpl::hasAttributes() const {
463 return Bits != 0;
464}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000465
Bill Wendling73ea2de2012-10-08 21:47:17 +0000466bool AttributesImpl::hasAttributes(const Attributes &A) const {
467 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
468}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000469
Bill Wendling73ea2de2012-10-08 21:47:17 +0000470uint64_t AttributesImpl::getAlignment() const {
471 return Bits & Attribute::Alignment_i;
472}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000473
Bill Wendling73ea2de2012-10-08 21:47:17 +0000474uint64_t AttributesImpl::getStackAlignment() const {
475 return Bits & Attribute::StackAlignment_i;
476}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000477
Bill Wendling73ea2de2012-10-08 21:47:17 +0000478bool AttributesImpl::isEmptyOrSingleton() const {
479 return (Bits & (Bits - 1)) == 0;
Bill Wendlinge38b8042012-09-26 21:07:29 +0000480}
481
482//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000483// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000484//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000485
Owen Anderson2e831892010-11-18 18:59:13 +0000486namespace llvm {
487 class AttributeListImpl;
488}
489
490static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000491
Chris Lattner8a923e72008-03-12 17:45:29 +0000492namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000493static ManagedStatic<sys::SmartMutex<true> > ALMutex;
494
Devang Patel00095052008-09-24 00:29:49 +0000495class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000496 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000497
Devang Patel4c758ea2008-09-25 21:00:45 +0000498 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000499 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
500 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000501 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000502public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000503 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000504
Chris Lattner3cb6f832012-05-28 01:47:44 +0000505 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
506 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000507 RefCount = 0;
508 }
509
Owen Anderson9b14a252010-11-09 00:27:03 +0000510 void AddRef() {
511 sys::SmartScopedLock<true> Lock(*ALMutex);
512 ++RefCount;
513 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000514 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000515 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000516 if (!AttributesLists.isConstructed())
517 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000518 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000519 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000520 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000521 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000522
523 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000524 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000525 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000526 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
527 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
528 ID.AddInteger(Attrs[i].Attrs.Raw());
529 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000530 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000531 }
532};
533}
534
Devang Patel00095052008-09-24 00:29:49 +0000535AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000536 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000537 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000538}
539
540
Chris Lattner3cb6f832012-05-28 01:47:44 +0000541AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000542 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000543 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000544 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000545
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000546#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000547 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000548 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000549 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000550 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000551 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000552 }
553#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000554
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000555 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000556 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000557 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000558 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000559
560 sys::SmartScopedLock<true> Lock(*ALMutex);
561
Devang Patel00095052008-09-24 00:29:49 +0000562 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000563 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000564
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000565 // If we didn't find any existing attributes of the same shape then
566 // create a new one and insert it.
567 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000568 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000569 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000570 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000571
Devang Patel4c758ea2008-09-25 21:00:45 +0000572 // Return the AttributesList that we found or created.
573 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000574}
575
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000576
Chris Lattner8a923e72008-03-12 17:45:29 +0000577//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000578// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000579//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000580
Devang Patel4c758ea2008-09-25 21:00:45 +0000581AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000582 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000583}
584
Devang Patel4c758ea2008-09-25 21:00:45 +0000585AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
586 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000587}
588
Devang Patel4c758ea2008-09-25 21:00:45 +0000589const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000590 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000591 if (AttrList == RHS.AttrList) return *this;
592 if (AttrList) AttrList->DropRef();
593 AttrList = RHS.AttrList;
594 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000595 return *this;
596}
597
Devang Patel4c758ea2008-09-25 21:00:45 +0000598AttrListPtr::~AttrListPtr() {
599 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000600}
601
602/// getNumSlots - Return the number of slots used in this attribute list.
603/// This is the number of arguments that have an attribute set on them
604/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000605unsigned AttrListPtr::getNumSlots() const {
606 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000607}
608
Devang Patel4c758ea2008-09-25 21:00:45 +0000609/// getSlot - Return the AttributeWithIndex at the specified slot. This
610/// holds a number plus a set of attributes.
611const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
612 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
613 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000614}
615
616
Devang Patel4c758ea2008-09-25 21:00:45 +0000617/// getAttributes - The attributes for the specified index are
618/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000619/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000620Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000621 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000622
Devang Patel4c758ea2008-09-25 21:00:45 +0000623 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000624 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
625 if (Attrs[i].Index == Idx)
626 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000627
628 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000629}
630
631/// hasAttrSomewhere - Return true if the specified attribute is set for at
632/// least one parameter or for the return value.
Devang Patel4c758ea2008-09-25 21:00:45 +0000633bool AttrListPtr::hasAttrSomewhere(Attributes Attr) const {
634 if (AttrList == 0) return false;
Chris Lattner8a923e72008-03-12 17:45:29 +0000635
Devang Patel4c758ea2008-09-25 21:00:45 +0000636 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000637 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000638 if (Attrs[i].Attrs.hasAttributes(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000639 return true;
640 return false;
641}
642
643
Devang Patel4c758ea2008-09-25 21:00:45 +0000644AttrListPtr AttrListPtr::addAttr(unsigned Idx, Attributes Attrs) const {
645 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000646#ifndef NDEBUG
647 // FIXME it is not obvious how this should work for alignment.
648 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000649 unsigned OldAlign = OldAttrs.getAlignment();
650 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000651 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000652 "Attempt to change alignment!");
653#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000654
Devang Patelba3fa6c2008-09-23 23:03:40 +0000655 Attributes NewAttrs = OldAttrs | Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000656 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000657 return *this;
658
Devang Patel4c758ea2008-09-25 21:00:45 +0000659 SmallVector<AttributeWithIndex, 8> NewAttrList;
660 if (AttrList == 0)
661 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000662 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000663 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000664 unsigned i = 0, e = OldAttrList.size();
665 // Copy attributes for arguments before this one.
666 for (; i != e && OldAttrList[i].Index < Idx; ++i)
667 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000668
Chris Lattner8a923e72008-03-12 17:45:29 +0000669 // If there are attributes already at this index, merge them in.
670 if (i != e && OldAttrList[i].Index == Idx) {
671 Attrs |= OldAttrList[i].Attrs;
672 ++i;
673 }
674
Devang Patel4c758ea2008-09-25 21:00:45 +0000675 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000676
677 // Copy attributes for arguments after this one.
678 NewAttrList.insert(NewAttrList.end(),
679 OldAttrList.begin()+i, OldAttrList.end());
680 }
681
Chris Lattner3cb6f832012-05-28 01:47:44 +0000682 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000683}
684
Devang Patel4c758ea2008-09-25 21:00:45 +0000685AttrListPtr AttrListPtr::removeAttr(unsigned Idx, Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000686#ifndef NDEBUG
687 // FIXME it is not obvious how this should work for alignment.
688 // For now, say we can't pass in alignment, which no current use does.
Bill Wendlingb4e211c2012-09-20 15:20:36 +0000689 assert(!Attrs.hasAlignmentAttr() && "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000690#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000691 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000692
Devang Patel4c758ea2008-09-25 21:00:45 +0000693 Attributes OldAttrs = getAttributes(Idx);
Devang Patelba3fa6c2008-09-23 23:03:40 +0000694 Attributes NewAttrs = OldAttrs & ~Attrs;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000695 if (NewAttrs == OldAttrs)
Chris Lattner8a923e72008-03-12 17:45:29 +0000696 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000697
Devang Patel4c758ea2008-09-25 21:00:45 +0000698 SmallVector<AttributeWithIndex, 8> NewAttrList;
699 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000700 unsigned i = 0, e = OldAttrList.size();
701
702 // Copy attributes for arguments before this one.
703 for (; i != e && OldAttrList[i].Index < Idx; ++i)
704 NewAttrList.push_back(OldAttrList[i]);
705
706 // If there are attributes already at this index, merge them in.
707 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
708 Attrs = OldAttrList[i].Attrs & ~Attrs;
709 ++i;
710 if (Attrs) // If any attributes left for this parameter, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000711 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000712
713 // Copy attributes for arguments after this one.
714 NewAttrList.insert(NewAttrList.end(),
715 OldAttrList.begin()+i, OldAttrList.end());
716
Chris Lattner3cb6f832012-05-28 01:47:44 +0000717 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000718}
719
Devang Patel4c758ea2008-09-25 21:00:45 +0000720void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000721 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000722 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000723 const AttributeWithIndex &PAWI = getSlot(i);
David Greenef7014732010-01-05 01:29:58 +0000724 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000725 }
726
David Greenef7014732010-01-05 01:29:58 +0000727 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000728}