blob: 45f81844a17b091c387faa7993af8307eb0813bf [file] [log] [blame]
Bill Wendling034b94b2012-12-19 07:18:57 +00001//===-- Attribute.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling034b94b2012-12-19 07:18:57 +000010// This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000011// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000016#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000017#include "LLVMContextImpl.h"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ADT/FoldingSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Atomic.h"
David Greeneef1894e2010-01-05 01:29:58 +000022#include "llvm/Support/Debug.h"
Chris Lattner50ee9dd2008-01-02 23:42:30 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/Mutex.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Bill Wendling3467e302013-01-24 00:06:56 +000026#include <algorithm>
Chris Lattner50ee9dd2008-01-02 23:42:30 +000027using namespace llvm;
28
Chris Lattner58d74912008-03-12 17:45:29 +000029//===----------------------------------------------------------------------===//
Bill Wendling034b94b2012-12-19 07:18:57 +000030// Attribute Implementation
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling629fb822012-12-22 00:37:52 +000033Attribute Attribute::get(LLVMContext &Context, ArrayRef<AttrKind> Vals) {
Bill Wendling702cc912012-10-15 20:35:56 +000034 AttrBuilder B;
Bill Wendling629fb822012-12-22 00:37:52 +000035 for (ArrayRef<AttrKind>::iterator I = Vals.begin(), E = Vals.end();
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000036 I != E; ++I)
37 B.addAttribute(*I);
Bill Wendling034b94b2012-12-19 07:18:57 +000038 return Attribute::get(Context, B);
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000039}
Bill Wendling8e635db2012-10-08 21:47:17 +000040
Bill Wendling034b94b2012-12-19 07:18:57 +000041Attribute Attribute::get(LLVMContext &Context, AttrBuilder &B) {
42 // If there are no attributes, return an empty Attribute class.
Bill Wendlinga5c699d2012-10-16 05:55:09 +000043 if (!B.hasAttributes())
Bill Wendling034b94b2012-12-19 07:18:57 +000044 return Attribute();
Bill Wendling8e635db2012-10-08 21:47:17 +000045
46 // Otherwise, build a key to look up the existing attributes.
47 LLVMContextImpl *pImpl = Context.pImpl;
48 FoldingSetNodeID ID;
Bill Wendling1db9b692013-01-09 23:36:50 +000049 ID.AddInteger(B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000050
51 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000052 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 if (!PA) {
55 // If we didn't find any existing attributes of the same shape then create a
56 // new one and insert it.
Bill Wendling1db9b692013-01-09 23:36:50 +000057 PA = new AttributeImpl(Context, B.Raw());
Bill Wendling8e635db2012-10-08 21:47:17 +000058 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
59 }
60
61 // Return the AttributesList that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000062 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000063}
64
Bill Wendling629fb822012-12-22 00:37:52 +000065bool Attribute::hasAttribute(AttrKind Val) const {
Bill Wendling27107f62012-12-20 21:28:43 +000066 return pImpl && pImpl->hasAttribute(Val);
Bill Wendlinge66f3d32012-10-05 06:44:41 +000067}
68
Bill Wendling034b94b2012-12-19 07:18:57 +000069bool Attribute::hasAttributes() const {
Bill Wendling27107f62012-12-20 21:28:43 +000070 return pImpl && pImpl->hasAttributes();
Bill Wendling05cc40d2012-10-15 05:40:12 +000071}
72
Bill Wendlinge66f3d32012-10-05 06:44:41 +000073/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +000074unsigned Attribute::getAlignment() const {
75 if (!hasAttribute(Attribute::Alignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000076 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000077 return pImpl->getAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000078}
79
80/// This returns the stack alignment field of an attribute as a byte alignment
81/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +000082unsigned Attribute::getStackAlignment() const {
83 if (!hasAttribute(Attribute::StackAlignment))
Bill Wendling9ef99c92012-10-09 20:56:48 +000084 return 0;
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +000085 return pImpl->getStackAlignment();
Bill Wendlinge66f3d32012-10-05 06:44:41 +000086}
87
Bill Wendling92e287f2012-12-31 11:51:54 +000088bool Attribute::operator==(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000089 return pImpl && *pImpl == K;
Bill Wendling92e287f2012-12-31 11:51:54 +000090}
Bill Wendling92e287f2012-12-31 11:51:54 +000091bool Attribute::operator!=(AttrKind K) const {
Bill Wendling60507d52013-01-04 20:54:35 +000092 return !(*this == K);
Bill Wendling92e287f2012-12-31 11:51:54 +000093}
94
Bill Wendling3467e302013-01-24 00:06:56 +000095bool Attribute::operator<(Attribute A) const {
96 if (!pImpl && !A.pImpl) return false;
97 if (!pImpl) return true;
98 if (!A.pImpl) return false;
99 return *pImpl < *A.pImpl;
100}
101
Bill Wendling1db9b692013-01-09 23:36:50 +0000102uint64_t Attribute::Raw() const {
103 return pImpl ? pImpl->Raw() : 0;
Bill Wendlingb10c88f2012-10-07 08:55:05 +0000104}
105
Bill Wendling034b94b2012-12-19 07:18:57 +0000106std::string Attribute::getAsString() const {
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000107 std::string Result;
Bill Wendling034b94b2012-12-19 07:18:57 +0000108 if (hasAttribute(Attribute::ZExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000109 Result += "zeroext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000110 if (hasAttribute(Attribute::SExt))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000111 Result += "signext ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000112 if (hasAttribute(Attribute::NoReturn))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000113 Result += "noreturn ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000114 if (hasAttribute(Attribute::NoUnwind))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000115 Result += "nounwind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000116 if (hasAttribute(Attribute::UWTable))
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000117 Result += "uwtable ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000118 if (hasAttribute(Attribute::ReturnsTwice))
Rafael Espindola25456ef2011-10-03 14:45:37 +0000119 Result += "returns_twice ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000120 if (hasAttribute(Attribute::InReg))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000121 Result += "inreg ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000122 if (hasAttribute(Attribute::NoAlias))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000123 Result += "noalias ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000124 if (hasAttribute(Attribute::NoCapture))
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000125 Result += "nocapture ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000126 if (hasAttribute(Attribute::StructRet))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000127 Result += "sret ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000128 if (hasAttribute(Attribute::ByVal))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000129 Result += "byval ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000130 if (hasAttribute(Attribute::Nest))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000131 Result += "nest ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000132 if (hasAttribute(Attribute::ReadNone))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000133 Result += "readnone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000134 if (hasAttribute(Attribute::ReadOnly))
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000135 Result += "readonly ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000136 if (hasAttribute(Attribute::OptimizeForSize))
Devang Patel19c87462008-09-26 22:53:05 +0000137 Result += "optsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000138 if (hasAttribute(Attribute::NoInline))
Devang Patel19c87462008-09-26 22:53:05 +0000139 Result += "noinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000140 if (hasAttribute(Attribute::InlineHint))
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000141 Result += "inlinehint ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 if (hasAttribute(Attribute::AlwaysInline))
Devang Patel19c87462008-09-26 22:53:05 +0000143 Result += "alwaysinline ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000144 if (hasAttribute(Attribute::StackProtect))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000145 Result += "ssp ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000146 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000147 Result += "sspreq ";
Bill Wendling114baee2013-01-23 06:41:41 +0000148 if (hasAttribute(Attribute::StackProtectStrong))
149 Result += "sspstrong ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000150 if (hasAttribute(Attribute::NoRedZone))
Devang Pateld18e31a2009-06-04 22:05:33 +0000151 Result += "noredzone ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000152 if (hasAttribute(Attribute::NoImplicitFloat))
Devang Patel578efa92009-06-05 21:57:13 +0000153 Result += "noimplicitfloat ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000154 if (hasAttribute(Attribute::Naked))
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000155 Result += "naked ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000156 if (hasAttribute(Attribute::NonLazyBind))
John McCall3a3465b2011-06-15 20:36:13 +0000157 Result += "nonlazybind ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000158 if (hasAttribute(Attribute::AddressSafety))
Kostya Serebryany164b86b2012-01-20 17:56:17 +0000159 Result += "address_safety ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 if (hasAttribute(Attribute::MinSize))
Quentin Colombet9a419f62012-10-30 16:32:52 +0000161 Result += "minsize ";
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 if (hasAttribute(Attribute::StackAlignment)) {
Charles Davis1e063d12010-02-12 00:31:15 +0000163 Result += "alignstack(";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000164 Result += utostr(getStackAlignment());
Charles Davis1e063d12010-02-12 00:31:15 +0000165 Result += ") ";
166 }
Bill Wendling034b94b2012-12-19 07:18:57 +0000167 if (hasAttribute(Attribute::Alignment)) {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000168 Result += "align ";
Bill Wendlingef99fe82012-09-21 15:26:31 +0000169 Result += utostr(getAlignment());
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000170 Result += " ";
171 }
James Molloy67ae1352012-12-20 16:04:27 +0000172 if (hasAttribute(Attribute::NoDuplicate))
173 Result += "noduplicate ";
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000174 // Trim the trailing space.
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000175 assert(!Result.empty() && "Unknown attribute!");
Dan Gohmanc3be0fd2008-08-05 15:51:44 +0000176 Result.erase(Result.end()-1);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000177 return Result;
178}
179
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000180//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000181// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000182//===----------------------------------------------------------------------===//
183
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000184AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
185 : Alignment(0), StackAlignment(0) {
186 AttributeSetImpl *pImpl = AS.AttrList;
187 if (!pImpl) return;
188
189 ArrayRef<AttributeWithIndex> AttrList = pImpl->getAttributes();
190 const AttributeWithIndex *AWI = 0;
191 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
192 if (AttrList[I].Index == Idx) {
193 AWI = &AttrList[I];
194 break;
195 }
196
Bill Wendlingaec71062013-01-18 21:56:07 +0000197 if (!AWI) return;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000198
Bill Wendling956f1342013-01-18 21:11:39 +0000199 uint64_t Mask = AWI->Attrs.Raw();
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000200
Bill Wendling956f1342013-01-18 21:11:39 +0000201 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
202 I = Attribute::AttrKind(I + 1)) {
203 if (uint64_t A = (Mask & AttributeImpl::getAttrMask(I))) {
204 Attrs.insert(I);
205
206 if (I == Attribute::Alignment)
207 Alignment = 1ULL << ((A >> 16) - 1);
208 else if (I == Attribute::StackAlignment)
209 StackAlignment = 1ULL << ((A >> 26)-1);
210 }
211 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000212}
213
Bill Wendling03198882013-01-04 23:27:34 +0000214void AttrBuilder::clear() {
215 Attrs.clear();
216 Alignment = StackAlignment = 0;
217}
218
219AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
220 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000221 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000222}
223
Bill Wendling03198882013-01-04 23:27:34 +0000224AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
225 Attrs.erase(Val);
226 if (Val == Attribute::Alignment)
227 Alignment = 0;
228 else if (Val == Attribute::StackAlignment)
229 StackAlignment = 0;
230
Bill Wendlinga19a5302012-10-14 04:10:01 +0000231 return *this;
232}
233
Bill Wendling702cc912012-10-15 20:35:56 +0000234AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000235 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000236
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000237 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
238 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000239
240 Attrs.insert(Attribute::Alignment);
241 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000242 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000243}
244
Bill Wendling03198882013-01-04 23:27:34 +0000245AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
246 // Default alignment, allow the target to define how to align it.
247 if (Align == 0) return *this;
248
249 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
250 assert(Align <= 0x100 && "Alignment too large.");
251
252 Attrs.insert(Attribute::StackAlignment);
253 StackAlignment = Align;
254 return *this;
255}
256
257AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
258 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
259 I = Attribute::AttrKind(I + 1)) {
260 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
261 Attrs.insert(I);
Bill Wendlingdefaca02013-01-22 21:15:51 +0000262
Bill Wendling03198882013-01-04 23:27:34 +0000263 if (I == Attribute::Alignment)
264 Alignment = 1ULL << ((A >> 16) - 1);
265 else if (I == Attribute::StackAlignment)
266 StackAlignment = 1ULL << ((A >> 26)-1);
267 }
268 }
Bill Wendlingdefaca02013-01-22 21:15:51 +0000269
Bill Wendling3a106e62012-10-09 19:01:18 +0000270 return *this;
Bill Wendling2e879bc2012-10-09 09:11:20 +0000271}
272
Bill Wendlingdefaca02013-01-22 21:15:51 +0000273AttrBuilder &AttrBuilder::addAttributes(const Attribute &Attr) {
274 uint64_t Mask = Attr.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000275
276 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
Bill Wendlingdefaca02013-01-22 21:15:51 +0000277 I = Attribute::AttrKind(I + 1))
278 if ((Mask & AttributeImpl::getAttrMask(I)) != 0)
Bill Wendling03198882013-01-04 23:27:34 +0000279 Attrs.insert(I);
280
Bill Wendlingdefaca02013-01-22 21:15:51 +0000281 if (Attr.getAlignment())
282 Alignment = Attr.getAlignment();
283 if (Attr.getStackAlignment())
284 StackAlignment = Attr.getStackAlignment();
Bill Wendling432e6062012-10-14 07:17:34 +0000285 return *this;
286}
287
Bill Wendling034b94b2012-12-19 07:18:57 +0000288AttrBuilder &AttrBuilder::removeAttributes(const Attribute &A){
Bill Wendling1db9b692013-01-09 23:36:50 +0000289 uint64_t Mask = A.Raw();
Bill Wendling03198882013-01-04 23:27:34 +0000290
291 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
292 I = Attribute::AttrKind(I + 1)) {
293 if (Mask & AttributeImpl::getAttrMask(I)) {
294 Attrs.erase(I);
295
296 if (I == Attribute::Alignment)
297 Alignment = 0;
298 else if (I == Attribute::StackAlignment)
299 StackAlignment = 0;
300 }
301 }
302
Bill Wendling5886b7b2012-10-14 06:39:53 +0000303 return *this;
Bill Wendling8831c062012-10-09 00:01:21 +0000304}
305
Bill Wendling22bd6412013-01-03 01:54:39 +0000306bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000307 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000308}
309
Bill Wendling702cc912012-10-15 20:35:56 +0000310bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000311 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000312}
Bill Wendling60507d52013-01-04 20:54:35 +0000313
Bill Wendling034b94b2012-12-19 07:18:57 +0000314bool AttrBuilder::hasAttributes(const Attribute &A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000315 return Raw() & A.Raw();
Bill Wendling8831c062012-10-09 00:01:21 +0000316}
Bill Wendling60507d52013-01-04 20:54:35 +0000317
Bill Wendling702cc912012-10-15 20:35:56 +0000318bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +0000319 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000320}
321
Bill Wendling1db9b692013-01-09 23:36:50 +0000322uint64_t AttrBuilder::Raw() const {
Bill Wendling03198882013-01-04 23:27:34 +0000323 uint64_t Mask = 0;
324
325 for (DenseSet<Attribute::AttrKind>::const_iterator I = Attrs.begin(),
326 E = Attrs.end(); I != E; ++I) {
327 Attribute::AttrKind Kind = *I;
328
329 if (Kind == Attribute::Alignment)
330 Mask |= (Log2_32(Alignment) + 1) << 16;
331 else if (Kind == Attribute::StackAlignment)
332 Mask |= (Log2_32(StackAlignment) + 1) << 26;
333 else
334 Mask |= AttributeImpl::getAttrMask(Kind);
335 }
336
337 return Mask;
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000338}
339
Bill Wendling03198882013-01-04 23:27:34 +0000340bool AttrBuilder::operator==(const AttrBuilder &B) {
341 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
342 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
343 return This == That;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000344}
345
Chris Lattner58d74912008-03-12 17:45:29 +0000346//===----------------------------------------------------------------------===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000347// AttributeImpl Definition
348//===----------------------------------------------------------------------===//
349
Bill Wendling1bbd6442013-01-05 01:36:54 +0000350AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
351 : Context(C) {
Bill Wendling7c1683d2012-12-29 12:29:38 +0000352 Data = ConstantInt::get(Type::getInt64Ty(C), data);
353}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000354AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
355 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000356 Data = ConstantInt::get(Type::getInt64Ty(C), data);
357}
358AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
Bill Wendling1bbd6442013-01-05 01:36:54 +0000359 ArrayRef<Constant*> values)
360 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000361 Data = ConstantInt::get(Type::getInt64Ty(C), data);
362 Vals.reserve(values.size());
363 Vals.append(values.begin(), values.end());
364}
Bill Wendling1bbd6442013-01-05 01:36:54 +0000365AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
366 : Context(C) {
Bill Wendling979aff62012-12-30 02:22:16 +0000367 Data = ConstantDataArray::getString(C, data);
368}
Bill Wendling7c1683d2012-12-29 12:29:38 +0000369
Bill Wendling60507d52013-01-04 20:54:35 +0000370bool AttributeImpl::operator==(Attribute::AttrKind Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000371 if (ConstantInt *CI = dyn_cast<ConstantInt>(Data))
372 return CI->getZExtValue() == Kind;
373 return false;
374}
Bill Wendling60507d52013-01-04 20:54:35 +0000375bool AttributeImpl::operator!=(Attribute::AttrKind Kind) const {
376 return !(*this == Kind);
377}
Bill Wendling529ec712012-12-30 01:38:39 +0000378
Bill Wendling60507d52013-01-04 20:54:35 +0000379bool AttributeImpl::operator==(StringRef Kind) const {
Bill Wendling529ec712012-12-30 01:38:39 +0000380 if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Data))
381 if (CDA->isString())
382 return CDA->getAsString() == Kind;
383 return false;
384}
Bill Wendling3467e302013-01-24 00:06:56 +0000385
Bill Wendling60507d52013-01-04 20:54:35 +0000386bool AttributeImpl::operator!=(StringRef Kind) const {
387 return !(*this == Kind);
388}
Bill Wendling529ec712012-12-30 01:38:39 +0000389
Bill Wendling3467e302013-01-24 00:06:56 +0000390bool AttributeImpl::operator<(const AttributeImpl &AI) const {
391 if (!Data && !AI.Data) return false;
392 if (!Data && AI.Data) return true;
393 if (Data && !AI.Data) return false;
394
395 ConstantInt *ThisCI = dyn_cast<ConstantInt>(Data);
396 ConstantInt *ThatCI = dyn_cast<ConstantInt>(AI.Data);
397
398 ConstantDataArray *ThisCDA = dyn_cast<ConstantDataArray>(Data);
399 ConstantDataArray *ThatCDA = dyn_cast<ConstantDataArray>(AI.Data);
400
401 if (ThisCI && ThatCI)
402 return ThisCI->getZExtValue() < ThatCI->getZExtValue();
403
404 if (ThisCI && ThatCDA)
405 return true;
406
407 if (ThisCDA && ThatCI)
408 return false;
409
410 return ThisCDA->getAsString() < ThatCDA->getAsString();
411}
412
Bill Wendling1db9b692013-01-09 23:36:50 +0000413uint64_t AttributeImpl::Raw() const {
Bill Wendling529ec712012-12-30 01:38:39 +0000414 // FIXME: Remove this.
Bill Wendling7c1683d2012-12-29 12:29:38 +0000415 return cast<ConstantInt>(Data)->getZExtValue();
416}
417
Bill Wendling60507d52013-01-04 20:54:35 +0000418uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
Bill Wendling67658342012-10-09 07:45:08 +0000419 switch (Val) {
Chandler Carruth6f78fbb2013-01-05 08:47:26 +0000420 case Attribute::EndAttrKinds:
421 case Attribute::AttrKindEmptyKey:
422 case Attribute::AttrKindTombstoneKey:
423 llvm_unreachable("Synthetic enumerators which should never get here");
424
Bill Wendling034b94b2012-12-19 07:18:57 +0000425 case Attribute::None: return 0;
426 case Attribute::ZExt: return 1 << 0;
427 case Attribute::SExt: return 1 << 1;
428 case Attribute::NoReturn: return 1 << 2;
429 case Attribute::InReg: return 1 << 3;
430 case Attribute::StructRet: return 1 << 4;
431 case Attribute::NoUnwind: return 1 << 5;
432 case Attribute::NoAlias: return 1 << 6;
433 case Attribute::ByVal: return 1 << 7;
434 case Attribute::Nest: return 1 << 8;
435 case Attribute::ReadNone: return 1 << 9;
436 case Attribute::ReadOnly: return 1 << 10;
437 case Attribute::NoInline: return 1 << 11;
438 case Attribute::AlwaysInline: return 1 << 12;
439 case Attribute::OptimizeForSize: return 1 << 13;
440 case Attribute::StackProtect: return 1 << 14;
441 case Attribute::StackProtectReq: return 1 << 15;
442 case Attribute::Alignment: return 31 << 16;
443 case Attribute::NoCapture: return 1 << 21;
444 case Attribute::NoRedZone: return 1 << 22;
445 case Attribute::NoImplicitFloat: return 1 << 23;
446 case Attribute::Naked: return 1 << 24;
447 case Attribute::InlineHint: return 1 << 25;
448 case Attribute::StackAlignment: return 7 << 26;
449 case Attribute::ReturnsTwice: return 1 << 29;
450 case Attribute::UWTable: return 1 << 30;
451 case Attribute::NonLazyBind: return 1U << 31;
452 case Attribute::AddressSafety: return 1ULL << 32;
453 case Attribute::MinSize: return 1ULL << 33;
James Molloy67ae1352012-12-20 16:04:27 +0000454 case Attribute::NoDuplicate: return 1ULL << 34;
Bill Wendling114baee2013-01-23 06:41:41 +0000455 case Attribute::StackProtectStrong: return 1ULL << 35;
Bill Wendling67658342012-10-09 07:45:08 +0000456 }
457 llvm_unreachable("Unsupported attribute type");
458}
459
Bill Wendling60507d52013-01-04 20:54:35 +0000460bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000461 return (Raw() & getAttrMask(A)) != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000462}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000463
Bill Wendlingf6670722012-12-20 01:36:59 +0000464bool AttributeImpl::hasAttributes() const {
Bill Wendling1db9b692013-01-09 23:36:50 +0000465 return Raw() != 0;
Bill Wendling8e635db2012-10-08 21:47:17 +0000466}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000467
Bill Wendlingf6670722012-12-20 01:36:59 +0000468uint64_t AttributeImpl::getAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000469 uint64_t Mask = Raw() & getAttrMask(Attribute::Alignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000470 return 1ULL << ((Mask >> 16) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000471}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000472
Bill Wendlingf6670722012-12-20 01:36:59 +0000473uint64_t AttributeImpl::getStackAlignment() const {
Bill Wendlinga8ab5fc2013-01-23 23:00:05 +0000474 uint64_t Mask = Raw() & getAttrMask(Attribute::StackAlignment);
Reid Klecknerf86c9322013-01-25 15:35:56 +0000475 return 1ULL << ((Mask >> 26) - 1);
Bill Wendling8e635db2012-10-08 21:47:17 +0000476}
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000477
Bill Wendling8456efb2013-01-09 00:32:55 +0000478void AttributeImpl::Profile(FoldingSetNodeID &ID, Constant *Data,
479 ArrayRef<Constant*> Vals) {
Bill Wendlingff887162013-01-09 00:32:08 +0000480 ID.AddInteger(cast<ConstantInt>(Data)->getZExtValue());
Bill Wendling1db9b692013-01-09 23:36:50 +0000481#if 0
482 // FIXME: Not yet supported.
Bill Wendlingff887162013-01-09 00:32:08 +0000483 for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
484 I != E; ++I)
485 ID.AddPointer(*I);
Bill Wendling1db9b692013-01-09 23:36:50 +0000486#endif
Bill Wendlingff887162013-01-09 00:32:08 +0000487}
488
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000489//===----------------------------------------------------------------------===//
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000490// AttributeWithIndex Definition
491//===----------------------------------------------------------------------===//
492
493AttributeWithIndex AttributeWithIndex::get(LLVMContext &C, unsigned Idx,
494 AttributeSet AS) {
495 // FIXME: This is temporary, but necessary for the conversion.
496 AttrBuilder B(AS, Idx);
497 return get(Idx, Attribute::get(C, B));
498}
499
500//===----------------------------------------------------------------------===//
Bill Wendling3467e302013-01-24 00:06:56 +0000501// AttributeSetNode Definition
502//===----------------------------------------------------------------------===//
503
504AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
505 ArrayRef<Attribute> Attrs) {
506 if (Attrs.empty())
507 return 0;
508
509 // Otherwise, build a key to look up the existing attributes.
510 LLVMContextImpl *pImpl = C.pImpl;
511 FoldingSetNodeID ID;
512
513 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
514 std::sort(SortedAttrs.begin(), SortedAttrs.end());
515
516 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
517 E = SortedAttrs.end(); I != E; ++I)
518 I->Profile(ID);
519
520 void *InsertPoint;
521 AttributeSetNode *PA =
522 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
523
524 // If we didn't find any existing attributes of the same shape then create a
525 // new one and insert it.
526 if (!PA) {
527 PA = new AttributeSetNode(SortedAttrs);
528 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
529 }
530
531 // Return the AttributesListNode that we found or created.
532 return PA;
533}
534
535//===----------------------------------------------------------------------===//
Bill Wendling18e72112012-12-19 22:42:22 +0000536// AttributeSetImpl Definition
Chris Lattner58d74912008-03-12 17:45:29 +0000537//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000538
Bill Wendling28d65722013-01-23 06:14:59 +0000539AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
540 // FIXME: Remove.
541 return AttrList && hasAttributes(Idx) ?
542 AttributeSet::get(AttrList->getContext(),
543 AttributeWithIndex::get(Idx, getAttributes(Idx))) :
544 AttributeSet();
545}
546
Bill Wendling3fc4b962013-01-21 22:44:49 +0000547AttributeSet AttributeSet::getRetAttributes() const {
548 // FIXME: Remove.
549 return AttrList && hasAttributes(ReturnIndex) ?
550 AttributeSet::get(AttrList->getContext(),
551 AttributeWithIndex::get(ReturnIndex,
552 getAttributes(ReturnIndex))) :
553 AttributeSet();
554}
555
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000556AttributeSet AttributeSet::getFnAttributes() const {
557 // FIXME: Remove.
Bill Wendling3fc4b962013-01-21 22:44:49 +0000558 return AttrList && hasAttributes(FunctionIndex) ?
Bill Wendlingc5f1bc82013-01-21 21:57:28 +0000559 AttributeSet::get(AttrList->getContext(),
560 AttributeWithIndex::get(FunctionIndex,
561 getAttributes(FunctionIndex))) :
562 AttributeSet();
563}
564
Bill Wendling99faa3b2012-12-07 23:16:57 +0000565AttributeSet AttributeSet::get(LLVMContext &C,
Bill Wendling1ce47ac2012-12-12 19:21:53 +0000566 ArrayRef<AttributeWithIndex> Attrs) {
Devang Patel05988662008-09-25 21:00:45 +0000567 // If there are no attributes then return a null AttributesList pointer.
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000568 if (Attrs.empty())
Bill Wendling99faa3b2012-12-07 23:16:57 +0000569 return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000570
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000571#ifndef NDEBUG
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000572 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Bill Wendling77898682012-10-16 06:01:44 +0000573 assert(Attrs[i].Attrs.hasAttributes() &&
Devang Patel05988662008-09-25 21:00:45 +0000574 "Pointless attribute!");
Chris Lattner58d74912008-03-12 17:45:29 +0000575 assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
Devang Patel05988662008-09-25 21:00:45 +0000576 "Misordered AttributesList!");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000577 }
578#endif
Bill Wendling77898682012-10-16 06:01:44 +0000579
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000580 // Otherwise, build a key to look up the existing attributes.
Bill Wendling0976e002012-11-20 05:09:20 +0000581 LLVMContextImpl *pImpl = C.pImpl;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000582 FoldingSetNodeID ID;
Bill Wendling18e72112012-12-19 22:42:22 +0000583 AttributeSetImpl::Profile(ID, Attrs);
Bill Wendling77898682012-10-16 06:01:44 +0000584
Bill Wendling0976e002012-11-20 05:09:20 +0000585 void *InsertPoint;
Bill Wendling1bbd6442013-01-05 01:36:54 +0000586 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling77898682012-10-16 06:01:44 +0000587
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000588 // If we didn't find any existing attributes of the same shape then
589 // create a new one and insert it.
Bill Wendling0976e002012-11-20 05:09:20 +0000590 if (!PA) {
Bill Wendling5f93e2b2012-12-19 23:55:43 +0000591 PA = new AttributeSetImpl(C, Attrs);
Bill Wendling0976e002012-11-20 05:09:20 +0000592 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000593 }
Bill Wendling77898682012-10-16 06:01:44 +0000594
Devang Patel05988662008-09-25 21:00:45 +0000595 // Return the AttributesList that we found or created.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000596 return AttributeSet(PA);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000597}
598
Bill Wendling1bbd6442013-01-05 01:36:54 +0000599AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
Bill Wendling3fc4b962013-01-21 22:44:49 +0000600 // FIXME: This should be implemented as a loop that creates the
601 // AttributeWithIndexes that then are used to create the AttributeSet.
602 if (!B.hasAttributes())
603 return AttributeSet();
Bill Wendlingdefaca02013-01-22 21:15:51 +0000604 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, B)));
Bill Wendling1bbd6442013-01-05 01:36:54 +0000605}
606
Bill Wendling28d65722013-01-23 06:14:59 +0000607AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
608 Attribute::AttrKind Kind) {
609 return get(C, AttributeWithIndex::get(Idx, Attribute::get(C, Kind)));
610}
611
Bill Wendling8e47daf2013-01-25 23:09:36 +0000612AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
613 SmallVector<AttributeWithIndex, 8> AttrList;
614 for (ArrayRef<AttributeSet>::iterator I = Attrs.begin(), E = Attrs.end();
615 I != E; ++I) {
616 AttributeSet AS = *I;
617 if (!AS.AttrList) continue;
618 AttrList.append(AS.AttrList->AttrList.begin(), AS.AttrList->AttrList.end());
619 }
620
621 return get(C, AttrList);
622}
623
Chris Lattner58d74912008-03-12 17:45:29 +0000624//===----------------------------------------------------------------------===//
Bill Wendling99faa3b2012-12-07 23:16:57 +0000625// AttributeSet Method Implementations
Chris Lattner58d74912008-03-12 17:45:29 +0000626//===----------------------------------------------------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000627
Bill Wendling99faa3b2012-12-07 23:16:57 +0000628const AttributeSet &AttributeSet::operator=(const AttributeSet &RHS) {
Bill Wendling0976e002012-11-20 05:09:20 +0000629 AttrList = RHS.AttrList;
630 return *this;
Chris Lattner58d74912008-03-12 17:45:29 +0000631}
632
Bill Wendling77898682012-10-16 06:01:44 +0000633/// getNumSlots - Return the number of slots used in this attribute list.
Chris Lattner58d74912008-03-12 17:45:29 +0000634/// This is the number of arguments that have an attribute set on them
635/// (including the function itself).
Bill Wendling99faa3b2012-12-07 23:16:57 +0000636unsigned AttributeSet::getNumSlots() const {
Bill Wendling60507d52013-01-04 20:54:35 +0000637 return AttrList ? AttrList->getNumAttributes() : 0;
Chris Lattner58d74912008-03-12 17:45:29 +0000638}
639
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000640unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
641 assert(AttrList && Slot < AttrList->getNumAttributes() &&
642 "Slot # out of range!");
643 return AttrList->getSlotIndex(Slot);
644}
645
Bill Wendling8e47daf2013-01-25 23:09:36 +0000646AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
647 assert(AttrList && Slot < AttrList->getNumAttributes() &&
648 "Slot # out of range!");
649 return AttrList->getSlotAttributes(Slot);
650}
651
Bill Wendling831737d2012-12-30 10:32:01 +0000652bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
653 return getAttributes(Index).hasAttribute(Kind);
654}
655
656bool AttributeSet::hasAttributes(unsigned Index) const {
657 return getAttributes(Index).hasAttributes();
658}
659
660std::string AttributeSet::getAsString(unsigned Index) const {
661 return getAttributes(Index).getAsString();
662}
663
Bill Wendling956f1342013-01-18 21:11:39 +0000664unsigned AttributeSet::getParamAlignment(unsigned Idx) const {
665 return getAttributes(Idx).getAlignment();
666}
667
Bill Wendling831737d2012-12-30 10:32:01 +0000668unsigned AttributeSet::getStackAlignment(unsigned Index) const {
669 return getAttributes(Index).getStackAlignment();
670}
671
Bill Wendling1db9b692013-01-09 23:36:50 +0000672uint64_t AttributeSet::Raw(unsigned Index) const {
Bill Wendling831737d2012-12-30 10:32:01 +0000673 // FIXME: Remove this.
Bill Wendling1db9b692013-01-09 23:36:50 +0000674 return getAttributes(Index).Raw();
Bill Wendling831737d2012-12-30 10:32:01 +0000675}
676
Bill Wendling77898682012-10-16 06:01:44 +0000677/// getAttributes - The attributes for the specified index are returned.
Bill Wendling034b94b2012-12-19 07:18:57 +0000678Attribute AttributeSet::getAttributes(unsigned Idx) const {
679 if (AttrList == 0) return Attribute();
Bill Wendling77898682012-10-16 06:01:44 +0000680
Bill Wendling60507d52013-01-04 20:54:35 +0000681 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000682 for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
683 if (Attrs[i].Index == Idx)
684 return Attrs[i].Attrs;
Bill Wendlingef99fe82012-09-21 15:26:31 +0000685
Bill Wendling034b94b2012-12-19 07:18:57 +0000686 return Attribute();
Chris Lattner58d74912008-03-12 17:45:29 +0000687}
688
689/// hasAttrSomewhere - Return true if the specified attribute is set for at
690/// least one parameter or for the return value.
Bill Wendling629fb822012-12-22 00:37:52 +0000691bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
Devang Patel05988662008-09-25 21:00:45 +0000692 if (AttrList == 0) return false;
Bill Wendling7d2f2492012-10-10 07:36:45 +0000693
Bill Wendling60507d52013-01-04 20:54:35 +0000694 ArrayRef<AttributeWithIndex> Attrs = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000695 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
Bill Wendling7d2f2492012-10-10 07:36:45 +0000696 if (Attrs[i].Attrs.hasAttribute(Attr))
Chris Lattner58d74912008-03-12 17:45:29 +0000697 return true;
Bill Wendling0976e002012-11-20 05:09:20 +0000698
Chris Lattner58d74912008-03-12 17:45:29 +0000699 return false;
700}
701
Bill Wendlingdefaca02013-01-22 21:15:51 +0000702AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
703 Attribute::AttrKind Attr) const {
704 return addAttr(C, Idx, Attribute::get(C, Attr));
705}
706
Bill Wendlinge4e85f12013-01-22 00:53:12 +0000707AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
708 AttributeSet Attrs) const {
709 return addAttr(C, Idx, Attrs.getAttributes(Idx));
Bill Wendling956f1342013-01-18 21:11:39 +0000710}
711
Bill Wendling99faa3b2012-12-07 23:16:57 +0000712AttributeSet AttributeSet::addAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000713 Attribute Attrs) const {
Bill Wendling034b94b2012-12-19 07:18:57 +0000714 Attribute OldAttrs = getAttributes(Idx);
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000715#ifndef NDEBUG
716 // FIXME it is not obvious how this should work for alignment.
717 // For now, say we can't change a known alignment.
Bill Wendlingef99fe82012-09-21 15:26:31 +0000718 unsigned OldAlign = OldAttrs.getAlignment();
719 unsigned NewAlign = Attrs.getAlignment();
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +0000720 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000721 "Attempt to change alignment!");
722#endif
Bill Wendling77898682012-10-16 06:01:44 +0000723
Bill Wendling702cc912012-10-15 20:35:56 +0000724 AttrBuilder NewAttrs =
725 AttrBuilder(OldAttrs).addAttributes(Attrs);
726 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000727 return *this;
Bill Wendling77898682012-10-16 06:01:44 +0000728
Devang Patel05988662008-09-25 21:00:45 +0000729 SmallVector<AttributeWithIndex, 8> NewAttrList;
730 if (AttrList == 0)
731 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000732 else {
Bill Wendling60507d52013-01-04 20:54:35 +0000733 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000734 unsigned i = 0, e = OldAttrList.size();
735 // Copy attributes for arguments before this one.
736 for (; i != e && OldAttrList[i].Index < Idx; ++i)
737 NewAttrList.push_back(OldAttrList[i]);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000738
Chris Lattner58d74912008-03-12 17:45:29 +0000739 // If there are attributes already at this index, merge them in.
740 if (i != e && OldAttrList[i].Index == Idx) {
Bill Wendlingc4167952012-10-14 07:35:59 +0000741 Attrs =
Bill Wendling034b94b2012-12-19 07:18:57 +0000742 Attribute::get(C, AttrBuilder(Attrs).
Bill Wendlingc4167952012-10-14 07:35:59 +0000743 addAttributes(OldAttrList[i].Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000744 ++i;
745 }
Bill Wendling77898682012-10-16 06:01:44 +0000746
Devang Patel05988662008-09-25 21:00:45 +0000747 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000748
Chris Lattner58d74912008-03-12 17:45:29 +0000749 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000750 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000751 OldAttrList.begin()+i, OldAttrList.end());
752 }
Bill Wendling77898682012-10-16 06:01:44 +0000753
Bill Wendling0976e002012-11-20 05:09:20 +0000754 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000755}
756
Bill Wendling8246df62013-01-23 00:45:55 +0000757AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
758 Attribute::AttrKind Attr) const {
759 return removeAttr(C, Idx, Attribute::get(C, Attr));
760}
761
762AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
763 AttributeSet Attrs) const {
764 return removeAttr(C, Idx, Attrs.getAttributes(Idx));
765}
766
Bill Wendling99faa3b2012-12-07 23:16:57 +0000767AttributeSet AttributeSet::removeAttr(LLVMContext &C, unsigned Idx,
Bill Wendling9d30e722012-12-31 00:49:59 +0000768 Attribute Attrs) const {
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000769#ifndef NDEBUG
770 // FIXME it is not obvious how this should work for alignment.
771 // For now, say we can't pass in alignment, which no current use does.
Bill Wendling034b94b2012-12-19 07:18:57 +0000772 assert(!Attrs.hasAttribute(Attribute::Alignment) &&
Bill Wendling67658342012-10-09 07:45:08 +0000773 "Attempt to exclude alignment!");
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000774#endif
Bill Wendling99faa3b2012-12-07 23:16:57 +0000775 if (AttrList == 0) return AttributeSet();
Bill Wendling77898682012-10-16 06:01:44 +0000776
Bill Wendling034b94b2012-12-19 07:18:57 +0000777 Attribute OldAttrs = getAttributes(Idx);
Bill Wendling702cc912012-10-15 20:35:56 +0000778 AttrBuilder NewAttrs =
779 AttrBuilder(OldAttrs).removeAttributes(Attrs);
780 if (NewAttrs == AttrBuilder(OldAttrs))
Chris Lattner58d74912008-03-12 17:45:29 +0000781 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000782
Devang Patel05988662008-09-25 21:00:45 +0000783 SmallVector<AttributeWithIndex, 8> NewAttrList;
Bill Wendling60507d52013-01-04 20:54:35 +0000784 ArrayRef<AttributeWithIndex> OldAttrList = AttrList->getAttributes();
Chris Lattner58d74912008-03-12 17:45:29 +0000785 unsigned i = 0, e = OldAttrList.size();
Bill Wendling77898682012-10-16 06:01:44 +0000786
Chris Lattner58d74912008-03-12 17:45:29 +0000787 // Copy attributes for arguments before this one.
788 for (; i != e && OldAttrList[i].Index < Idx; ++i)
789 NewAttrList.push_back(OldAttrList[i]);
Bill Wendling77898682012-10-16 06:01:44 +0000790
Chris Lattner58d74912008-03-12 17:45:29 +0000791 // If there are attributes already at this index, merge them in.
792 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
Bill Wendling034b94b2012-12-19 07:18:57 +0000793 Attrs = Attribute::get(C, AttrBuilder(OldAttrList[i].Attrs).
Bill Wendling5886b7b2012-10-14 06:39:53 +0000794 removeAttributes(Attrs));
Chris Lattner58d74912008-03-12 17:45:29 +0000795 ++i;
Bill Wendling7be78482012-10-14 08:54:26 +0000796 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
Devang Patel05988662008-09-25 21:00:45 +0000797 NewAttrList.push_back(AttributeWithIndex::get(Idx, Attrs));
Bill Wendling77898682012-10-16 06:01:44 +0000798
Chris Lattner58d74912008-03-12 17:45:29 +0000799 // Copy attributes for arguments after this one.
Bill Wendling77898682012-10-16 06:01:44 +0000800 NewAttrList.insert(NewAttrList.end(),
Chris Lattner58d74912008-03-12 17:45:29 +0000801 OldAttrList.begin()+i, OldAttrList.end());
Bill Wendling77898682012-10-16 06:01:44 +0000802
Bill Wendling0976e002012-11-20 05:09:20 +0000803 return get(C, NewAttrList);
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000804}
805
Bill Wendling99faa3b2012-12-07 23:16:57 +0000806void AttributeSet::dump() const {
David Greeneef1894e2010-01-05 01:29:58 +0000807 dbgs() << "PAL[ ";
Chris Lattner58d74912008-03-12 17:45:29 +0000808 for (unsigned i = 0; i < getNumSlots(); ++i) {
Bill Wendling85875642013-01-25 21:46:52 +0000809 unsigned Index = getSlotIndex(i);
810 dbgs() << "{ " << Index << " => " << getAsString(Index) << " } ";
Chris Lattner58d74912008-03-12 17:45:29 +0000811 }
Bill Wendling77898682012-10-16 06:01:44 +0000812
David Greeneef1894e2010-01-05 01:29:58 +0000813 dbgs() << "]\n";
Duncan Sandsad9a9e12008-01-06 18:27:01 +0000814}
Bill Wendling8e47daf2013-01-25 23:09:36 +0000815
816//===----------------------------------------------------------------------===//
817// AttributeFuncs Function Defintions
818//===----------------------------------------------------------------------===//
819
820Attribute AttributeFuncs::typeIncompatible(Type *Ty) {
821 AttrBuilder Incompatible;
822
823 if (!Ty->isIntegerTy())
824 // Attribute that only apply to integers.
825 Incompatible.addAttribute(Attribute::SExt)
826 .addAttribute(Attribute::ZExt);
827
828 if (!Ty->isPointerTy())
829 // Attribute that only apply to pointers.
830 Incompatible.addAttribute(Attribute::ByVal)
831 .addAttribute(Attribute::Nest)
832 .addAttribute(Attribute::NoAlias)
833 .addAttribute(Attribute::NoCapture)
834 .addAttribute(Attribute::StructRet);
835
836 return Attribute::get(Ty->getContext(), Incompatible);
837}
838
839/// encodeLLVMAttributesForBitcode - This returns an integer containing an
840/// encoding of all the LLVM attributes found in the given attribute bitset.
841/// Any change to this encoding is a breaking change to bitcode compatibility.
842uint64_t AttributeFuncs::encodeLLVMAttributesForBitcode(AttributeSet Attrs,
843 unsigned Index) {
844 // FIXME: It doesn't make sense to store the alignment information as an
845 // expanded out value, we should store it as a log2 value. However, we can't
846 // just change that here without breaking bitcode compatibility. If this ever
847 // becomes a problem in practice, we should introduce new tag numbers in the
848 // bitcode file and have those tags use a more efficiently encoded alignment
849 // field.
850
851 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
852 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
853 uint64_t EncodedAttrs = Attrs.Raw(Index) & 0xffff;
854 if (Attrs.hasAttribute(Index, Attribute::Alignment))
855 EncodedAttrs |= Attrs.getParamAlignment(Index) << 16;
856 EncodedAttrs |= (Attrs.Raw(Index) & (0xffffULL << 21)) << 11;
857 return EncodedAttrs;
858}
859
860/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
861/// the LLVM attributes that have been decoded from the given integer. This
862/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
863Attribute AttributeFuncs::decodeLLVMAttributesForBitcode(LLVMContext &C,
864 uint64_t EncodedAttrs){
865 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
866 // the bits above 31 down by 11 bits.
867 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
868 assert((!Alignment || isPowerOf2_32(Alignment)) &&
869 "Alignment must be a power of two.");
870
871 AttrBuilder B(EncodedAttrs & 0xffff);
872 if (Alignment)
873 B.addAlignmentAttr(Alignment);
874 B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
875 return Attribute::get(C, B);
876}
877