blob: e81bf3c83a7eb75c9a6d385fda7f12adcc3f3024 [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 Wendling8c3e65d2012-10-15 05:40:12 +000015#include "AttributesImpl.h"
Bill Wendlinge38b8042012-09-26 21:07:29 +000016#include "LLVMContextImpl.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000017#include "llvm/Type.h"
Dan Gohmanfc429612008-03-10 23:55:07 +000018#include "llvm/ADT/StringExtras.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000019#include "llvm/ADT/FoldingSet.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Atomic.h"
21#include "llvm/Support/Mutex.h"
David Greenef7014732010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Benjamin Kramer1a25d732009-08-23 11:37:21 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025using namespace llvm;
26
Chris Lattner8a923e72008-03-12 17:45:29 +000027//===----------------------------------------------------------------------===//
Bill Wendling73ea2de2012-10-08 21:47:17 +000028// Attributes Implementation
Chris Lattner8a923e72008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Chris Lattnerd0e1f102008-01-03 00:10:22 +000030
Bill Wendling79d45db2012-10-15 06:53:28 +000031Attributes::Attributes(AttributesImpl *A) : Attrs(A) {}
32
33Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
34
35Attributes Attributes::get(LLVMContext &Context, ArrayRef<AttrVal> Vals) {
Bill Wendlingd079a442012-10-15 04:46:55 +000036 Attributes::Builder B;
37 for (ArrayRef<AttrVal>::iterator I = Vals.begin(), E = Vals.end();
38 I != E; ++I)
39 B.addAttribute(*I);
Bill Wendling79d45db2012-10-15 06:53:28 +000040 return Attributes::get(Context, B);
Bill Wendlingd079a442012-10-15 04:46:55 +000041}
Bill Wendling73ea2de2012-10-08 21:47:17 +000042
Bill Wendling73ea2de2012-10-08 21:47:17 +000043Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
44 // If there are no attributes, return an empty Attributes class.
45 if (B.Bits == 0)
46 return Attributes();
47
48 // Otherwise, build a key to look up the existing attributes.
49 LLVMContextImpl *pImpl = Context.pImpl;
50 FoldingSetNodeID ID;
51 ID.AddInteger(B.Bits);
52
53 void *InsertPoint;
54 AttributesImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
55
56 if (!PA) {
57 // If we didn't find any existing attributes of the same shape then create a
58 // new one and insert it.
59 PA = new AttributesImpl(B.Bits);
60 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
61 }
62
63 // Return the AttributesList that we found or created.
64 return Attributes(PA);
65}
66
Bill Wendlingc9b22d72012-10-09 07:45:08 +000067bool Attributes::hasAttribute(AttrVal Val) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000068 return Attrs && Attrs->hasAttribute(Val);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000069}
70
Bill Wendling8c3e65d2012-10-15 05:40:12 +000071bool Attributes::hasAttributes() const {
72 return Attrs && Attrs->hasAttributes();
73}
74
Bill Wendling93f70b72012-10-09 09:11:20 +000075bool Attributes::hasAttributes(const Attributes &A) const {
Bill Wendlingd079a442012-10-15 04:46:55 +000076 return Attrs && Attrs->hasAttributes(A);
Bill Wendling93f70b72012-10-09 09:11:20 +000077}
78
Bill Wendlingabf3feb2012-10-05 06:44:41 +000079/// This returns the alignment field of an attribute as a byte alignment value.
80unsigned Attributes::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000081 if (!hasAttribute(Attributes::Alignment))
82 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000083 return 1U << ((Attrs->getAlignment() >> 16) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000084}
85
86/// This returns the stack alignment field of an attribute as a byte alignment
87/// value.
88unsigned Attributes::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +000089 if (!hasAttribute(Attributes::StackAlignment))
90 return 0;
Bill Wendlingd079a442012-10-15 04:46:55 +000091 return 1U << ((Attrs->getStackAlignment() >> 26) - 1);
Bill Wendlingabf3feb2012-10-05 06:44:41 +000092}
93
Bill Wendling73ea2de2012-10-08 21:47:17 +000094uint64_t Attributes::Raw() const {
Bill Wendlingd079a442012-10-15 04:46:55 +000095 return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly!
Bill Wendlingbe7c6f22012-10-07 08:55:05 +000096}
97
Bill Wendlingabf3feb2012-10-05 06:44:41 +000098Attributes Attributes::typeIncompatible(Type *Ty) {
99 Attributes::Builder Incompatible;
100
Bill Wendling7c04e042012-10-09 19:01:18 +0000101 if (!Ty->isIntegerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000102 // Attributes that only apply to integers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000103 Incompatible.addAttribute(Attributes::SExt)
104 .addAttribute(Attributes::ZExt);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000105
Bill Wendling7c04e042012-10-09 19:01:18 +0000106 if (!Ty->isPointerTy())
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000107 // Attributes that only apply to pointers.
Bill Wendling7c04e042012-10-09 19:01:18 +0000108 Incompatible.addAttribute(Attributes::ByVal)
109 .addAttribute(Attributes::Nest)
110 .addAttribute(Attributes::NoAlias)
111 .addAttribute(Attributes::NoCapture)
112 .addAttribute(Attributes::StructRet);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000113
Bill Wendlingd079a442012-10-15 04:46:55 +0000114 return Attributes::get(Ty->getContext(), Incompatible);
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000115}
116
Bill Wendlingde74cf52012-09-20 14:44:42 +0000117std::string Attributes::getAsString() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000118 std::string Result;
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000119 if (hasAttribute(Attributes::ZExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000120 Result += "zeroext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000121 if (hasAttribute(Attributes::SExt))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000122 Result += "signext ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000123 if (hasAttribute(Attributes::NoReturn))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000124 Result += "noreturn ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000125 if (hasAttribute(Attributes::NoUnwind))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000126 Result += "nounwind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000127 if (hasAttribute(Attributes::UWTable))
Rafael Espindolafc9bae62011-05-25 03:44:17 +0000128 Result += "uwtable ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000129 if (hasAttribute(Attributes::ReturnsTwice))
Rafael Espindolacc349c82011-10-03 14:45:37 +0000130 Result += "returns_twice ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000131 if (hasAttribute(Attributes::InReg))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000132 Result += "inreg ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000133 if (hasAttribute(Attributes::NoAlias))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000134 Result += "noalias ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000135 if (hasAttribute(Attributes::NoCapture))
Nick Lewycky8d69f482008-12-19 09:38:31 +0000136 Result += "nocapture ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000137 if (hasAttribute(Attributes::StructRet))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000138 Result += "sret ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000139 if (hasAttribute(Attributes::ByVal))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000140 Result += "byval ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000141 if (hasAttribute(Attributes::Nest))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000142 Result += "nest ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000143 if (hasAttribute(Attributes::ReadNone))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000144 Result += "readnone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000145 if (hasAttribute(Attributes::ReadOnly))
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146 Result += "readonly ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000147 if (hasAttribute(Attributes::OptimizeForSize))
Devang Patela05633e2008-09-26 22:53:05 +0000148 Result += "optsize ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000149 if (hasAttribute(Attributes::NoInline))
Devang Patela05633e2008-09-26 22:53:05 +0000150 Result += "noinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000151 if (hasAttribute(Attributes::InlineHint))
Jakob Stoklund Olesen74bb06c2010-02-06 01:16:28 +0000152 Result += "inlinehint ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000153 if (hasAttribute(Attributes::AlwaysInline))
Devang Patela05633e2008-09-26 22:53:05 +0000154 Result += "alwaysinline ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000155 if (hasAttribute(Attributes::StackProtect))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000156 Result += "ssp ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000157 if (hasAttribute(Attributes::StackProtectReq))
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000158 Result += "sspreq ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000159 if (hasAttribute(Attributes::NoRedZone))
Devang Patel72a4d2f2009-06-04 22:05:33 +0000160 Result += "noredzone ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000161 if (hasAttribute(Attributes::NoImplicitFloat))
Devang Pateld1c7d342009-06-05 21:57:13 +0000162 Result += "noimplicitfloat ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000163 if (hasAttribute(Attributes::Naked))
Anton Korobeynikovc8ce7b082009-07-17 18:07:26 +0000164 Result += "naked ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000165 if (hasAttribute(Attributes::NonLazyBind))
John McCall4b7a8d62011-06-15 20:36:13 +0000166 Result += "nonlazybind ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000167 if (hasAttribute(Attributes::AddressSafety))
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000168 Result += "address_safety ";
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000169 if (hasAttribute(Attributes::StackAlignment)) {
Charles Davisbe5557e2010-02-12 00:31:15 +0000170 Result += "alignstack(";
Bill Wendling9be77592012-09-21 15:26:31 +0000171 Result += utostr(getStackAlignment());
Charles Davisbe5557e2010-02-12 00:31:15 +0000172 Result += ") ";
173 }
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000174 if (hasAttribute(Attributes::Alignment)) {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000175 Result += "align ";
Bill Wendling9be77592012-09-21 15:26:31 +0000176 Result += utostr(getAlignment());
Dale Johannesen11a555e2008-02-19 23:51:49 +0000177 Result += " ";
178 }
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000179 // Trim the trailing space.
Nick Lewycky8d69f482008-12-19 09:38:31 +0000180 assert(!Result.empty() && "Unknown attribute!");
Dan Gohman1a70bcc2008-08-05 15:51:44 +0000181 Result.erase(Result.end()-1);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000182 return Result;
183}
184
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000185//===----------------------------------------------------------------------===//
186// Attributes::Builder Implementation
187//===----------------------------------------------------------------------===//
188
Bill Wendling79d45db2012-10-15 06:53:28 +0000189Attributes::Builder &Attributes::Builder::addAttribute(Attributes::AttrVal Val){
Bill Wendling93f70b72012-10-09 09:11:20 +0000190 Bits |= AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000191 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000192}
193
Bill Wendling1fcc8222012-10-14 04:10:01 +0000194Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
195 Bits |= Val;
196 return *this;
197}
198
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000199Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
200 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000201 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
202 assert(Align <= 0x40000000 && "Alignment too large.");
203 Bits |= (Log2_32(Align) + 1) << 16;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000204 return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000205}
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000206Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000207 // Default alignment, allow the target to define how to align it.
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000208 if (Align == 0) return *this;
Bill Wendlingabf3feb2012-10-05 06:44:41 +0000209 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
210 assert(Align <= 0x100 && "Alignment too large.");
211 Bits |= (Log2_32(Align) + 1) << 26;
Bill Wendlingabd5ba22012-10-14 03:58:29 +0000212 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000213}
214
Bill Wendling7c04e042012-10-09 19:01:18 +0000215Attributes::Builder &Attributes::Builder::
216removeAttribute(Attributes::AttrVal Val) {
Bill Wendling93f70b72012-10-09 09:11:20 +0000217 Bits &= ~AttributesImpl::getAttrMask(Val);
Bill Wendling7c04e042012-10-09 19:01:18 +0000218 return *this;
Bill Wendling93f70b72012-10-09 09:11:20 +0000219}
220
Bill Wendling5c407ed2012-10-14 07:17:34 +0000221Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
222 Bits |= A.Raw();
223 return *this;
224}
225
226Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
Bill Wendling70f39172012-10-09 00:01:21 +0000227 Bits &= ~A.Raw();
Bill Wendling85a64c22012-10-14 06:39:53 +0000228 return *this;
Bill Wendling70f39172012-10-09 00:01:21 +0000229}
230
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000231bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
232 return Bits & AttributesImpl::getAttrMask(A);
233}
234
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000235bool Attributes::Builder::hasAttributes() const {
236 return Bits != 0;
237}
Bill Wendling70f39172012-10-09 00:01:21 +0000238bool Attributes::Builder::hasAttributes(const Attributes &A) const {
239 return Bits & A.Raw();
240}
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000241bool Attributes::Builder::hasAlignmentAttr() const {
Bill Wendling4caad412012-10-09 20:28:54 +0000242 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000243}
244
245uint64_t Attributes::Builder::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000246 if (!hasAlignmentAttr())
247 return 0;
Bill Wendling4caad412012-10-09 20:28:54 +0000248 return 1U <<
249 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
Bill Wendlingc6daefa2012-10-08 23:27:46 +0000250}
251
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000252uint64_t Attributes::Builder::getStackAlignment() const {
253 if (!hasAlignmentAttr())
254 return 0;
255 return 1U <<
256 (((Bits & AttributesImpl::getAttrMask(Attributes::StackAlignment))>>26)-1);
257}
258
Chris Lattner8a923e72008-03-12 17:45:29 +0000259//===----------------------------------------------------------------------===//
Bill Wendlinge38b8042012-09-26 21:07:29 +0000260// AttributeImpl Definition
261//===----------------------------------------------------------------------===//
262
Bill Wendling93f70b72012-10-09 09:11:20 +0000263uint64_t AttributesImpl::getAttrMask(uint64_t Val) {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000264 switch (Val) {
265 case Attributes::None: return 0;
266 case Attributes::ZExt: return 1 << 0;
267 case Attributes::SExt: return 1 << 1;
268 case Attributes::NoReturn: return 1 << 2;
269 case Attributes::InReg: return 1 << 3;
270 case Attributes::StructRet: return 1 << 4;
271 case Attributes::NoUnwind: return 1 << 5;
272 case Attributes::NoAlias: return 1 << 6;
273 case Attributes::ByVal: return 1 << 7;
274 case Attributes::Nest: return 1 << 8;
275 case Attributes::ReadNone: return 1 << 9;
276 case Attributes::ReadOnly: return 1 << 10;
277 case Attributes::NoInline: return 1 << 11;
278 case Attributes::AlwaysInline: return 1 << 12;
279 case Attributes::OptimizeForSize: return 1 << 13;
280 case Attributes::StackProtect: return 1 << 14;
281 case Attributes::StackProtectReq: return 1 << 15;
282 case Attributes::Alignment: return 31 << 16;
283 case Attributes::NoCapture: return 1 << 21;
284 case Attributes::NoRedZone: return 1 << 22;
285 case Attributes::NoImplicitFloat: return 1 << 23;
286 case Attributes::Naked: return 1 << 24;
287 case Attributes::InlineHint: return 1 << 25;
288 case Attributes::StackAlignment: return 7 << 26;
289 case Attributes::ReturnsTwice: return 1 << 29;
290 case Attributes::UWTable: return 1 << 30;
291 case Attributes::NonLazyBind: return 1U << 31;
292 case Attributes::AddressSafety: return 1ULL << 32;
293 }
294 llvm_unreachable("Unsupported attribute type");
295}
296
Bill Wendling73ea2de2012-10-08 21:47:17 +0000297bool AttributesImpl::hasAttribute(uint64_t A) const {
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000298 return (Bits & getAttrMask(A)) != 0;
Bill Wendling73ea2de2012-10-08 21:47:17 +0000299}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000300
Bill Wendling73ea2de2012-10-08 21:47:17 +0000301bool AttributesImpl::hasAttributes() const {
302 return Bits != 0;
303}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000304
Bill Wendling73ea2de2012-10-08 21:47:17 +0000305bool AttributesImpl::hasAttributes(const Attributes &A) const {
306 return Bits & A.Raw(); // FIXME: Raw() won't work here in the future.
307}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000308
Bill Wendling73ea2de2012-10-08 21:47:17 +0000309uint64_t AttributesImpl::getAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000310 return Bits & getAttrMask(Attributes::Alignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000311}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000312
Bill Wendling73ea2de2012-10-08 21:47:17 +0000313uint64_t AttributesImpl::getStackAlignment() const {
Bill Wendlingb3723342012-10-09 20:56:48 +0000314 return Bits & getAttrMask(Attributes::StackAlignment);
Bill Wendling73ea2de2012-10-08 21:47:17 +0000315}
Bill Wendlinge38b8042012-09-26 21:07:29 +0000316
Bill Wendlinge38b8042012-09-26 21:07:29 +0000317//===----------------------------------------------------------------------===//
Devang Patel00095052008-09-24 00:29:49 +0000318// AttributeListImpl Definition
Chris Lattner8a923e72008-03-12 17:45:29 +0000319//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000320
Owen Anderson2e831892010-11-18 18:59:13 +0000321namespace llvm {
322 class AttributeListImpl;
323}
324
325static ManagedStatic<FoldingSet<AttributeListImpl> > AttributesLists;
Owen Anderson9b14a252010-11-09 00:27:03 +0000326
Chris Lattner8a923e72008-03-12 17:45:29 +0000327namespace llvm {
Owen Anderson9b14a252010-11-09 00:27:03 +0000328static ManagedStatic<sys::SmartMutex<true> > ALMutex;
329
Devang Patel00095052008-09-24 00:29:49 +0000330class AttributeListImpl : public FoldingSetNode {
Owen Andersonc1a3a472009-08-20 19:03:20 +0000331 sys::cas_flag RefCount;
Chris Lattner8a923e72008-03-12 17:45:29 +0000332
Devang Patel4c758ea2008-09-25 21:00:45 +0000333 // AttributesList is uniqued, these should not be publicly available.
Craig Topperb1d83e82012-09-18 02:01:41 +0000334 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
335 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
Devang Patel00095052008-09-24 00:29:49 +0000336 ~AttributeListImpl(); // Private implementation
Chris Lattner8a923e72008-03-12 17:45:29 +0000337public:
Devang Patel4c758ea2008-09-25 21:00:45 +0000338 SmallVector<AttributeWithIndex, 4> Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000339
Chris Lattner3cb6f832012-05-28 01:47:44 +0000340 AttributeListImpl(ArrayRef<AttributeWithIndex> attrs)
341 : Attrs(attrs.begin(), attrs.end()) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000342 RefCount = 0;
343 }
344
Owen Anderson9b14a252010-11-09 00:27:03 +0000345 void AddRef() {
346 sys::SmartScopedLock<true> Lock(*ALMutex);
347 ++RefCount;
348 }
Owen Andersonc1a3a472009-08-20 19:03:20 +0000349 void DropRef() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000350 sys::SmartScopedLock<true> Lock(*ALMutex);
Owen Anderson2e831892010-11-18 18:59:13 +0000351 if (!AttributesLists.isConstructed())
352 return;
Owen Anderson91bfeb12010-11-09 17:47:10 +0000353 sys::cas_flag new_val = --RefCount;
Owen Anderson2d335432010-11-09 17:46:38 +0000354 if (new_val == 0)
Owen Anderson9b14a252010-11-09 00:27:03 +0000355 delete this;
Owen Andersonc1a3a472009-08-20 19:03:20 +0000356 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000357
358 void Profile(FoldingSetNodeID &ID) const {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000359 Profile(ID, Attrs);
Chris Lattner8a923e72008-03-12 17:45:29 +0000360 }
Chris Lattner3cb6f832012-05-28 01:47:44 +0000361 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
362 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
363 ID.AddInteger(Attrs[i].Attrs.Raw());
364 ID.AddInteger(Attrs[i].Index);
Kostya Serebryanya5054ad2012-01-20 17:56:17 +0000365 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000366 }
367};
368}
369
Devang Patel00095052008-09-24 00:29:49 +0000370AttributeListImpl::~AttributeListImpl() {
Owen Anderson9b14a252010-11-09 00:27:03 +0000371 // NOTE: Lock must be acquired by caller.
Devang Patel4c758ea2008-09-25 21:00:45 +0000372 AttributesLists->RemoveNode(this);
Chris Lattner8a923e72008-03-12 17:45:29 +0000373}
374
375
Chris Lattner3cb6f832012-05-28 01:47:44 +0000376AttrListPtr AttrListPtr::get(ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000377 // If there are no attributes then return a null AttributesList pointer.
Chris Lattner3cb6f832012-05-28 01:47:44 +0000378 if (Attrs.empty())
Devang Patel4c758ea2008-09-25 21:00:45 +0000379 return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000380
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000381#ifndef NDEBUG
Chris Lattner3cb6f832012-05-28 01:47:44 +0000382 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling9be77592012-09-21 15:26:31 +0000383 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000384 "Pointless attribute!");
Chris Lattner8a923e72008-03-12 17:45:29 +0000385 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel4c758ea2008-09-25 21:00:45 +0000386 "Misordered AttributesList!");
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000387 }
388#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000389
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000390 // Otherwise, build a key to look up the existing attributes.
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000391 FoldingSetNodeID ID;
Chris Lattner3cb6f832012-05-28 01:47:44 +0000392 AttributeListImpl::Profile(ID, Attrs);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000393 void *InsertPos;
Owen Andersond91e6b02009-08-17 17:10:58 +0000394
395 sys::SmartScopedLock<true> Lock(*ALMutex);
396
Devang Patel00095052008-09-24 00:29:49 +0000397 AttributeListImpl *PAL =
Devang Patel4c758ea2008-09-25 21:00:45 +0000398 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner8a923e72008-03-12 17:45:29 +0000399
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000400 // If we didn't find any existing attributes of the same shape then
401 // create a new one and insert it.
402 if (!PAL) {
Chris Lattner3cb6f832012-05-28 01:47:44 +0000403 PAL = new AttributeListImpl(Attrs);
Devang Patel4c758ea2008-09-25 21:00:45 +0000404 AttributesLists->InsertNode(PAL, InsertPos);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000405 }
Chris Lattner8a923e72008-03-12 17:45:29 +0000406
Devang Patel4c758ea2008-09-25 21:00:45 +0000407 // Return the AttributesList that we found or created.
408 return AttrListPtr(PAL);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000409}
410
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000411
Chris Lattner8a923e72008-03-12 17:45:29 +0000412//===----------------------------------------------------------------------===//
Devang Patel4c758ea2008-09-25 21:00:45 +0000413// AttrListPtr Method Implementations
Chris Lattner8a923e72008-03-12 17:45:29 +0000414//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000415
Devang Patel4c758ea2008-09-25 21:00:45 +0000416AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000417 if (LI) LI->AddRef();
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000418}
419
Devang Patel4c758ea2008-09-25 21:00:45 +0000420AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
421 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000422}
423
Devang Patel4c758ea2008-09-25 21:00:45 +0000424const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
Owen Anderson8dc0b042010-09-16 00:27:35 +0000425 sys::SmartScopedLock<true> Lock(*ALMutex);
Devang Patel4c758ea2008-09-25 21:00:45 +0000426 if (AttrList == RHS.AttrList) return *this;
427 if (AttrList) AttrList->DropRef();
428 AttrList = RHS.AttrList;
429 if (AttrList) AttrList->AddRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000430 return *this;
431}
432
Devang Patel4c758ea2008-09-25 21:00:45 +0000433AttrListPtr::~AttrListPtr() {
434 if (AttrList) AttrList->DropRef();
Chris Lattner8a923e72008-03-12 17:45:29 +0000435}
436
437/// getNumSlots - Return the number of slots used in this attribute list.
438/// This is the number of arguments that have an attribute set on them
439/// (including the function itself).
Devang Patel4c758ea2008-09-25 21:00:45 +0000440unsigned AttrListPtr::getNumSlots() const {
441 return AttrList ? AttrList->Attrs.size() : 0;
Chris Lattner8a923e72008-03-12 17:45:29 +0000442}
443
Devang Patel4c758ea2008-09-25 21:00:45 +0000444/// getSlot - Return the AttributeWithIndex at the specified slot. This
445/// holds a number plus a set of attributes.
446const AttributeWithIndex &AttrListPtr::getSlot(unsigned Slot) const {
447 assert(AttrList && Slot < AttrList->Attrs.size() && "Slot # out of range!");
448 return AttrList->Attrs[Slot];
Chris Lattner8a923e72008-03-12 17:45:29 +0000449}
450
451
Devang Patel4c758ea2008-09-25 21:00:45 +0000452/// getAttributes - The attributes for the specified index are
453/// returned. Attributes for the result are denoted with Idx = 0.
Devang Patel82fed672008-09-23 22:35:17 +0000454/// Function notes are denoted with idx = ~0.
Devang Patel4c758ea2008-09-25 21:00:45 +0000455Attributes AttrListPtr::getAttributes(unsigned Idx) const {
Bill Wendling9be77592012-09-21 15:26:31 +0000456 if (AttrList == 0) return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000457
Devang Patel4c758ea2008-09-25 21:00:45 +0000458 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000459 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
460 if (Attrs[i].Index == Idx)
461 return Attrs[i].Attrs;
Bill Wendling9be77592012-09-21 15:26:31 +0000462
463 return Attributes();
Chris Lattner8a923e72008-03-12 17:45:29 +0000464}
465
466/// hasAttrSomewhere - Return true if the specified attribute is set for at
467/// least one parameter or for the return value.
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000468bool AttrListPtr::hasAttrSomewhere(Attributes::AttrVal Attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000469 if (AttrList == 0) return false;
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000470
Devang Patel4c758ea2008-09-25 21:00:45 +0000471 const SmallVector<AttributeWithIndex, 4> &Attrs = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000472 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000473 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner8a923e72008-03-12 17:45:29 +0000474 return true;
475 return false;
476}
477
Bill Wendling70f39172012-10-09 00:01:21 +0000478unsigned AttrListPtr::getNumAttrs() const {
479 return AttrList ? AttrList->Attrs.size() : 0;
480}
481
482Attributes &AttrListPtr::getAttributesAtIndex(unsigned i) const {
483 assert(AttrList && "Trying to get an attribute from an empty list!");
484 assert(i < AttrList->Attrs.size() && "Index out of range!");
485 return AttrList->Attrs[i].Attrs;
486}
Chris Lattner8a923e72008-03-12 17:45:29 +0000487
Bill Wendling722b26c2012-10-14 07:35:59 +0000488AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx,
489 Attributes Attrs) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 Attributes OldAttrs = getAttributes(Idx);
Dale Johannesen11a555e2008-02-19 23:51:49 +0000491#ifndef NDEBUG
492 // FIXME it is not obvious how this should work for alignment.
493 // For now, say we can't change a known alignment.
Bill Wendling9be77592012-09-21 15:26:31 +0000494 unsigned OldAlign = OldAttrs.getAlignment();
495 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov18991d72008-02-20 12:07:57 +0000496 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen11a555e2008-02-19 23:51:49 +0000497 "Attempt to change alignment!");
498#endif
Chris Lattner8a923e72008-03-12 17:45:29 +0000499
Bill Wendling5c407ed2012-10-14 07:17:34 +0000500 Attributes::Builder NewAttrs =
501 Attributes::Builder(OldAttrs).addAttributes(Attrs);
502 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000503 return *this;
504
Devang Patel4c758ea2008-09-25 21:00:45 +0000505 SmallVector<AttributeWithIndex, 8> NewAttrList;
506 if (AttrList == 0)
507 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000508 else {
Devang Patel4c758ea2008-09-25 21:00:45 +0000509 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000510 unsigned i = 0, e = OldAttrList.size();
511 // Copy attributes for arguments before this one.
512 for (; i != e && OldAttrList[i].Index < Idx; ++i)
513 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000514
Chris Lattner8a923e72008-03-12 17:45:29 +0000515 // If there are attributes already at this index, merge them in.
516 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendling722b26c2012-10-14 07:35:59 +0000517 Attrs =
Bill Wendlingd079a442012-10-15 04:46:55 +0000518 Attributes::get(C, Attributes::Builder(Attrs).
Bill Wendling722b26c2012-10-14 07:35:59 +0000519 addAttributes(OldAttrList[i].Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000520 ++i;
521 }
522
Devang Patel4c758ea2008-09-25 21:00:45 +0000523 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000524
525 // Copy attributes for arguments after this one.
526 NewAttrList.insert(NewAttrList.end(),
527 OldAttrList.begin()+i, OldAttrList.end());
528 }
529
Chris Lattner3cb6f832012-05-28 01:47:44 +0000530 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000531}
532
Bill Wendling85a64c22012-10-14 06:39:53 +0000533AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
534 Attributes Attrs) const {
Dale Johannesen11a555e2008-02-19 23:51:49 +0000535#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.
Bill Wendlingc9b22d72012-10-09 07:45:08 +0000538 assert(!Attrs.hasAttribute(Attributes::Alignment) &&
539 "Attempt to exclude alignment!");
Dale Johannesen11a555e2008-02-19 23:51:49 +0000540#endif
Devang Patel4c758ea2008-09-25 21:00:45 +0000541 if (AttrList == 0) return AttrListPtr();
Chris Lattner8a923e72008-03-12 17:45:29 +0000542
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 Attributes OldAttrs = getAttributes(Idx);
Bill Wendling85a64c22012-10-14 06:39:53 +0000544 Attributes::Builder NewAttrs =
545 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
546 if (NewAttrs == Attributes::Builder(OldAttrs))
Chris Lattner8a923e72008-03-12 17:45:29 +0000547 return *this;
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000548
Devang Patel4c758ea2008-09-25 21:00:45 +0000549 SmallVector<AttributeWithIndex, 8> NewAttrList;
550 const SmallVector<AttributeWithIndex, 4> &OldAttrList = AttrList->Attrs;
Chris Lattner8a923e72008-03-12 17:45:29 +0000551 unsigned i = 0, e = OldAttrList.size();
552
553 // Copy attributes for arguments before this one.
554 for (; i != e && OldAttrList[i].Index < Idx; ++i)
555 NewAttrList.push_back(OldAttrList[i]);
556
557 // If there are attributes already at this index, merge them in.
558 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendlingd079a442012-10-15 04:46:55 +0000559 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
Bill Wendling85a64c22012-10-14 06:39:53 +0000560 removeAttributes(Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000561 ++i;
Bill Wendling76d2cd22012-10-14 08:54:26 +0000562 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel4c758ea2008-09-25 21:00:45 +0000563 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner8a923e72008-03-12 17:45:29 +0000564
565 // Copy attributes for arguments after this one.
566 NewAttrList.insert(NewAttrList.end(),
567 OldAttrList.begin()+i, OldAttrList.end());
568
Chris Lattner3cb6f832012-05-28 01:47:44 +0000569 return get(NewAttrList);
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000570}
571
Devang Patel4c758ea2008-09-25 21:00:45 +0000572void AttrListPtr::dump() const {
David Greenef7014732010-01-05 01:29:58 +0000573 dbgs() << "PAL[ ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000574 for (unsigned i = 0; i < getNumSlots(); ++i) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000575 const AttributeWithIndex &PAWI = getSlot(i);
Bill Wendling76d2cd22012-10-14 08:54:26 +0000576 dbgs() << "{" << PAWI.Index << "," << PAWI.Attrs.getAsString() << "} ";
Chris Lattner8a923e72008-03-12 17:45:29 +0000577 }
578
David Greenef7014732010-01-05 01:29:58 +0000579 dbgs() << "]\n";
Duncan Sands404eb052008-01-06 18:27:01 +0000580}