blob: 67ab4eaa699e866396e6cb0e2d189a7ccb274696 [file] [log] [blame]
Bill Wendling87e10df2013-01-28 21:55:20 +00001//===-- Attributes.cpp - Implement AttributesList -------------------------===//
Chris Lattner50ee9dd2008-01-02 23:42:30 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Bill Wendling87e10df2013-01-28 21:55:20 +000010// \file
11// \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
Bill Wendling18e72112012-12-19 22:42:22 +000012// AttributeSetImpl, and AttributeSet classes.
Chris Lattner50ee9dd2008-01-02 23:42:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
Bill Wendlingf6670722012-12-20 01:36:59 +000017#include "AttributeImpl.h"
Bill Wendling2c79ecb2012-09-26 21:07:29 +000018#include "LLVMContextImpl.h"
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 Wendling817abdd2013-01-29 00:48:16 +000030// Attribute Construction Methods
Chris Lattner58d74912008-03-12 17:45:29 +000031//===----------------------------------------------------------------------===//
Chris Lattnerfabfde32008-01-03 00:10:22 +000032
Bill Wendling8c74ecf2013-02-05 22:37:24 +000033Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
34 uint64_t Val) {
Bill Wendling8e635db2012-10-08 21:47:17 +000035 LLVMContextImpl *pImpl = Context.pImpl;
36 FoldingSetNodeID ID;
Bill Wendling8c74ecf2013-02-05 22:37:24 +000037 ID.AddInteger(Kind);
38 if (Val) ID.AddInteger(Val);
39
40 void *InsertPoint;
41 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
42
43 if (!PA) {
44 // If we didn't find any existing attributes of the same shape then create a
45 // new one and insert it.
46 PA = !Val ?
47 new AttributeImpl(Context, Kind) :
48 new AttributeImpl(Context, Kind, Val);
49 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
50 }
51
52 // Return the Attribute that we found or created.
53 return Attribute(PA);
54}
55
56Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
57 LLVMContextImpl *pImpl = Context.pImpl;
58 FoldingSetNodeID ID;
59 ID.AddString(Kind);
60 if (!Val.empty()) ID.AddString(Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000061
62 void *InsertPoint;
Bill Wendlingf6670722012-12-20 01:36:59 +000063 AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
Bill Wendling8e635db2012-10-08 21:47:17 +000064
65 if (!PA) {
66 // If we didn't find any existing attributes of the same shape then create a
67 // new one and insert it.
Bill Wendlingbdcbccc2013-02-02 00:42:06 +000068 PA = new AttributeImpl(Context, Kind, Val);
Bill Wendling8e635db2012-10-08 21:47:17 +000069 pImpl->AttrsSet.InsertNode(PA, InsertPoint);
70 }
71
Bill Wendlingea59f892013-02-05 08:09:32 +000072 // Return the Attribute that we found or created.
Bill Wendling034b94b2012-12-19 07:18:57 +000073 return Attribute(PA);
Bill Wendling8e635db2012-10-08 21:47:17 +000074}
75
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000076Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000077 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
78 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000079 return get(Context, Alignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000080}
81
82Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
83 uint64_t Align) {
Bill Wendling169d5272013-01-31 23:16:25 +000084 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
85 assert(Align <= 0x100 && "Alignment too large.");
Bill Wendling8c74ecf2013-02-05 22:37:24 +000086 return get(Context, StackAlignment, Align);
Bill Wendlingc08a5ef2013-01-27 22:43:04 +000087}
88
Bill Wendling817abdd2013-01-29 00:48:16 +000089//===----------------------------------------------------------------------===//
90// Attribute Accessor Methods
91//===----------------------------------------------------------------------===//
92
Bill Wendling8c74ecf2013-02-05 22:37:24 +000093bool Attribute::isEnumAttribute() const {
94 return pImpl && pImpl->isEnumAttribute();
95}
96
97bool Attribute::isAlignAttribute() const {
98 return pImpl && pImpl->isAlignAttribute();
99}
100
101bool Attribute::isStringAttribute() const {
102 return pImpl && pImpl->isStringAttribute();
103}
104
105Attribute::AttrKind Attribute::getKindAsEnum() const {
106 assert((isEnumAttribute() || isAlignAttribute()) &&
107 "Invalid attribute type to get the kind as an enum!");
108 return pImpl ? pImpl->getKindAsEnum() : None;
109}
110
111uint64_t Attribute::getValueAsInt() const {
112 assert(isAlignAttribute() &&
113 "Expected the attribute to be an alignment attribute!");
114 return pImpl ? pImpl->getValueAsInt() : 0;
115}
116
117StringRef Attribute::getKindAsString() const {
118 assert(isStringAttribute() &&
119 "Invalid attribute type to get the kind as a string!");
120 return pImpl ? pImpl->getKindAsString() : StringRef();
121}
122
123StringRef Attribute::getValueAsString() const {
124 assert(isStringAttribute() &&
125 "Invalid attribute type to get the value as a string!");
126 return pImpl ? pImpl->getValueAsString() : StringRef();
127}
128
Bill Wendling64754f42013-02-05 23:48:36 +0000129bool Attribute::hasAttribute(AttrKind Kind) const {
130 return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
131}
132
133bool Attribute::hasAttribute(StringRef Kind) const {
134 if (!isStringAttribute()) return false;
135 return pImpl && pImpl->hasAttribute(Kind);
Bill Wendling6dc37812013-01-29 20:45:34 +0000136}
137
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000138/// This returns the alignment field of an attribute as a byte alignment value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000139unsigned Attribute::getAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000140 assert(hasAttribute(Attribute::Alignment) &&
141 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000142 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000143}
144
145/// This returns the stack alignment field of an attribute as a byte alignment
146/// value.
Bill Wendling034b94b2012-12-19 07:18:57 +0000147unsigned Attribute::getStackAlignment() const {
Bill Wendling7beee282013-02-01 01:04:27 +0000148 assert(hasAttribute(Attribute::StackAlignment) &&
149 "Trying to get alignment from non-alignment attribute!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000150 return pImpl->getValueAsInt();
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000151}
152
Bill Wendling034b94b2012-12-19 07:18:57 +0000153std::string Attribute::getAsString() const {
Bill Wendling14292a62013-01-31 20:59:05 +0000154 if (!pImpl) return "";
155
156 if (hasAttribute(Attribute::AddressSafety))
157 return "address_safety";
158 if (hasAttribute(Attribute::AlwaysInline))
159 return "alwaysinline";
160 if (hasAttribute(Attribute::ByVal))
161 return "byval";
162 if (hasAttribute(Attribute::InlineHint))
163 return "inlinehint";
Bill Wendling034b94b2012-12-19 07:18:57 +0000164 if (hasAttribute(Attribute::InReg))
Bill Wendling606c8e32013-01-29 03:20:31 +0000165 return "inreg";
Bill Wendling14292a62013-01-31 20:59:05 +0000166 if (hasAttribute(Attribute::MinSize))
167 return "minsize";
168 if (hasAttribute(Attribute::Naked))
169 return "naked";
170 if (hasAttribute(Attribute::Nest))
171 return "nest";
Bill Wendling034b94b2012-12-19 07:18:57 +0000172 if (hasAttribute(Attribute::NoAlias))
Bill Wendling606c8e32013-01-29 03:20:31 +0000173 return "noalias";
Bill Wendling034b94b2012-12-19 07:18:57 +0000174 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000175 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000176 if (hasAttribute(Attribute::NoDuplicate))
177 return "noduplicate";
178 if (hasAttribute(Attribute::NoImplicitFloat))
179 return "noimplicitfloat";
180 if (hasAttribute(Attribute::NoInline))
181 return "noinline";
182 if (hasAttribute(Attribute::NonLazyBind))
183 return "nonlazybind";
184 if (hasAttribute(Attribute::NoRedZone))
185 return "noredzone";
186 if (hasAttribute(Attribute::NoReturn))
187 return "noreturn";
188 if (hasAttribute(Attribute::NoUnwind))
189 return "nounwind";
190 if (hasAttribute(Attribute::OptimizeForSize))
191 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000192 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000193 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000195 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000196 if (hasAttribute(Attribute::ReturnsTwice))
197 return "returns_twice";
198 if (hasAttribute(Attribute::SExt))
199 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000200 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000201 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000203 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000204 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000205 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000206 if (hasAttribute(Attribute::StructRet))
207 return "sret";
208 if (hasAttribute(Attribute::UWTable))
209 return "uwtable";
210 if (hasAttribute(Attribute::ZExt))
211 return "zeroext";
212
213 // FIXME: These should be output like this:
214 //
215 // align=4
216 // alignstack=8
217 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000218 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000219 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000220 Result += "align ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000221 Result += utostr(getValueAsInt());
222 return Result;
223 }
224 if (hasAttribute(Attribute::StackAlignment)) {
225 std::string Result;
226 Result += "alignstack(";
227 Result += utostr(getValueAsInt());
228 Result += ")";
Bill Wendling606c8e32013-01-29 03:20:31 +0000229 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000230 }
Bill Wendling14292a62013-01-31 20:59:05 +0000231
232 // Convert target-dependent attributes to strings of the form:
233 //
234 // "kind"
235 // "kind" = "value"
Bill Wendling5a4041e2013-02-01 22:32:30 +0000236 // "kind" = ( "value1" "value2" "value3" )
Bill Wendling14292a62013-01-31 20:59:05 +0000237 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000238 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000239 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000240 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000241
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000242 StringRef Val = pImpl->getValueAsString();
243 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000244
Bill Wendling14292a62013-01-31 20:59:05 +0000245 Result += " = ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000246 Result += '\"' + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000247 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000248 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000249
250 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000251}
252
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000253bool Attribute::operator<(Attribute A) const {
254 if (!pImpl && !A.pImpl) return false;
255 if (!pImpl) return true;
256 if (!A.pImpl) return false;
257 return *pImpl < *A.pImpl;
258}
259
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000260//===----------------------------------------------------------------------===//
261// AttributeImpl Definition
262//===----------------------------------------------------------------------===//
263
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000264AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
265 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
266
267AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
268 unsigned Align)
269 : Context(C) {
270 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
271 "Wrong kind for alignment attribute!");
272 Entry = new AlignAttributeEntry(Kind, Align);
273}
274
275AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
276 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
277
278AttributeImpl::~AttributeImpl() {
279 delete Entry;
280}
281
282bool AttributeImpl::isEnumAttribute() const {
283 return isa<EnumAttributeEntry>(Entry);
284}
285
286bool AttributeImpl::isAlignAttribute() const {
287 return isa<AlignAttributeEntry>(Entry);
288}
289
290bool AttributeImpl::isStringAttribute() const {
291 return isa<StringAttributeEntry>(Entry);
292}
293
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000294bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000295 if (isStringAttribute()) return false;
296 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000297}
298
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000299bool AttributeImpl::hasAttribute(StringRef Kind) const {
300 if (!isStringAttribute()) return false;
301 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000302}
303
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000304Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
305 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
306 return E->getEnumKind();
307 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000308}
309
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000310uint64_t AttributeImpl::getValueAsInt() const {
311 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312}
313
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000314StringRef AttributeImpl::getKindAsString() const {
315 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000316}
317
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000318StringRef AttributeImpl::getValueAsString() const {
319 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000320}
321
322bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000323 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
324 // relative to their enum value) and then strings.
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000325 if (isEnumAttribute())
326 if (AI.isAlignAttribute() || AI.isEnumAttribute())
327 return getKindAsEnum() < AI.getKindAsEnum();
Bill Wendling7beee282013-02-01 01:04:27 +0000328
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000329 if (isAlignAttribute()) {
330 if (!AI.isStringAttribute() && getKindAsEnum() < AI.getKindAsEnum())
331 return true;
332 if (AI.isAlignAttribute())
333 return getValueAsInt() < AI.getValueAsInt();
334 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000335
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000336 if (isStringAttribute()) {
337 if (!AI.isStringAttribute()) return false;
338 if (getKindAsString() < AI.getKindAsString()) return true;
339 if (getKindAsString() == AI.getKindAsString())
340 return getValueAsString() < AI.getValueAsString();
341 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000342
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000343 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000344}
345
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000346uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
347 // FIXME: Remove this.
348 switch (Val) {
349 case Attribute::EndAttrKinds:
350 case Attribute::AttrKindEmptyKey:
351 case Attribute::AttrKindTombstoneKey:
352 llvm_unreachable("Synthetic enumerators which should never get here");
353
354 case Attribute::None: return 0;
355 case Attribute::ZExt: return 1 << 0;
356 case Attribute::SExt: return 1 << 1;
357 case Attribute::NoReturn: return 1 << 2;
358 case Attribute::InReg: return 1 << 3;
359 case Attribute::StructRet: return 1 << 4;
360 case Attribute::NoUnwind: return 1 << 5;
361 case Attribute::NoAlias: return 1 << 6;
362 case Attribute::ByVal: return 1 << 7;
363 case Attribute::Nest: return 1 << 8;
364 case Attribute::ReadNone: return 1 << 9;
365 case Attribute::ReadOnly: return 1 << 10;
366 case Attribute::NoInline: return 1 << 11;
367 case Attribute::AlwaysInline: return 1 << 12;
368 case Attribute::OptimizeForSize: return 1 << 13;
369 case Attribute::StackProtect: return 1 << 14;
370 case Attribute::StackProtectReq: return 1 << 15;
371 case Attribute::Alignment: return 31 << 16;
372 case Attribute::NoCapture: return 1 << 21;
373 case Attribute::NoRedZone: return 1 << 22;
374 case Attribute::NoImplicitFloat: return 1 << 23;
375 case Attribute::Naked: return 1 << 24;
376 case Attribute::InlineHint: return 1 << 25;
377 case Attribute::StackAlignment: return 7 << 26;
378 case Attribute::ReturnsTwice: return 1 << 29;
379 case Attribute::UWTable: return 1 << 30;
380 case Attribute::NonLazyBind: return 1U << 31;
381 case Attribute::AddressSafety: return 1ULL << 32;
382 case Attribute::MinSize: return 1ULL << 33;
383 case Attribute::NoDuplicate: return 1ULL << 34;
384 case Attribute::StackProtectStrong: return 1ULL << 35;
385 }
386 llvm_unreachable("Unsupported attribute type");
387}
388
389//===----------------------------------------------------------------------===//
390// AttributeSetNode Definition
391//===----------------------------------------------------------------------===//
392
393AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
394 ArrayRef<Attribute> Attrs) {
395 if (Attrs.empty())
396 return 0;
397
398 // Otherwise, build a key to look up the existing attributes.
399 LLVMContextImpl *pImpl = C.pImpl;
400 FoldingSetNodeID ID;
401
402 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
403 std::sort(SortedAttrs.begin(), SortedAttrs.end());
404
405 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
406 E = SortedAttrs.end(); I != E; ++I)
407 I->Profile(ID);
408
409 void *InsertPoint;
410 AttributeSetNode *PA =
411 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
412
413 // If we didn't find any existing attributes of the same shape then create a
414 // new one and insert it.
415 if (!PA) {
416 PA = new AttributeSetNode(SortedAttrs);
417 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
418 }
419
420 // Return the AttributesListNode that we found or created.
421 return PA;
422}
423
Bill Wendling606c8e32013-01-29 03:20:31 +0000424bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
425 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
426 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000427 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000428 return true;
429 return false;
430}
431
432unsigned AttributeSetNode::getAlignment() const {
433 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
434 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000435 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000436 return I->getAlignment();
437 return 0;
438}
439
440unsigned AttributeSetNode::getStackAlignment() const {
441 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
442 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000443 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000444 return I->getStackAlignment();
445 return 0;
446}
447
448std::string AttributeSetNode::getAsString() const {
449 std::string Str = "";
450 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000451 E = AttrList.end(); I != E; ) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000452 Str += I->getAsString();
Bill Wendling7beee282013-02-01 01:04:27 +0000453 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000454 }
455 return Str;
456}
457
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000458//===----------------------------------------------------------------------===//
459// AttributeSetImpl Definition
460//===----------------------------------------------------------------------===//
461
462uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
463 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
464 if (getSlotIndex(I) != Index) continue;
465 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000466 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000467
468 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000469 IE = ASN->end(); II != IE; ++II) {
470 Attribute Attr = *II;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000471 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000472
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000473 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000474 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000475 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000476 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
477 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000478 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000479 }
480
481 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000482 }
483
484 return 0;
485}
486
487//===----------------------------------------------------------------------===//
488// AttributeSet Construction and Mutation Methods
489//===----------------------------------------------------------------------===//
490
Bill Wendling8232ece2013-01-29 01:43:29 +0000491AttributeSet
492AttributeSet::getImpl(LLVMContext &C,
493 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000494 LLVMContextImpl *pImpl = C.pImpl;
495 FoldingSetNodeID ID;
496 AttributeSetImpl::Profile(ID, Attrs);
497
498 void *InsertPoint;
499 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
500
501 // If we didn't find any existing attributes of the same shape then
502 // create a new one and insert it.
503 if (!PA) {
504 PA = new AttributeSetImpl(C, Attrs);
505 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
506 }
507
508 // Return the AttributesList that we found or created.
509 return AttributeSet(PA);
510}
511
512AttributeSet AttributeSet::get(LLVMContext &C,
513 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
514 // If there are no attributes then return a null AttributesList pointer.
515 if (Attrs.empty())
516 return AttributeSet();
517
518#ifndef NDEBUG
519 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
520 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
521 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000522 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000523 "Pointless attribute!");
524 }
525#endif
526
527 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
528 // list.
529 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
530 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
531 E = Attrs.end(); I != E; ) {
532 unsigned Index = I->first;
533 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000534 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000535 AttrVec.push_back(I->second);
536 ++I;
537 }
538
539 AttrPairVec.push_back(std::make_pair(Index,
540 AttributeSetNode::get(C, AttrVec)));
541 }
542
543 return getImpl(C, AttrPairVec);
544}
545
546AttributeSet AttributeSet::get(LLVMContext &C,
547 ArrayRef<std::pair<unsigned,
548 AttributeSetNode*> > Attrs) {
549 // If there are no attributes then return a null AttributesList pointer.
550 if (Attrs.empty())
551 return AttributeSet();
552
553 return getImpl(C, Attrs);
554}
555
556AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
557 if (!B.hasAttributes())
558 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000559
Bill Wendling64754f42013-02-05 23:48:36 +0000560 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000561 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
562 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
563 Attribute::AttrKind Kind = *I;
564 if (Kind == Attribute::Alignment)
565 Attrs.push_back(std::make_pair(Idx, Attribute::
566 getWithAlignment(C, B.getAlignment())));
567 else if (Kind == Attribute::StackAlignment)
568 Attrs.push_back(std::make_pair(Idx, Attribute::
569 getWithStackAlignment(C, B.getStackAlignment())));
570 else
571 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
572 }
573
Bill Wendling64754f42013-02-05 23:48:36 +0000574 // Add target-dependent (string) attributes.
575 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
576 I != E; ++I)
577 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
578
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000579 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000580}
581
582AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
583 ArrayRef<Attribute::AttrKind> Kind) {
584 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
585 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
586 E = Kind.end(); I != E; ++I)
587 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
588 return get(C, Attrs);
589}
590
591AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
592 if (Attrs.empty()) return AttributeSet();
593
594 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
595 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
596 AttributeSet AS = Attrs[I];
597 if (!AS.pImpl) continue;
598 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
599 }
600
601 return getImpl(C, AttrNodeVec);
602}
603
604AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
605 Attribute::AttrKind Attr) const {
606 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
607}
608
609AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
610 AttributeSet Attrs) const {
611 if (!pImpl) return Attrs;
612 if (!Attrs.pImpl) return *this;
613
614#ifndef NDEBUG
615 // FIXME it is not obvious how this should work for alignment. For now, say
616 // we can't change a known alignment.
617 unsigned OldAlign = getParamAlignment(Idx);
618 unsigned NewAlign = Attrs.getParamAlignment(Idx);
619 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
620 "Attempt to change alignment!");
621#endif
622
623 // Add the attribute slots before the one we're trying to add.
624 SmallVector<AttributeSet, 4> AttrSet;
625 uint64_t NumAttrs = pImpl->getNumAttributes();
626 AttributeSet AS;
627 uint64_t LastIndex = 0;
628 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
629 if (getSlotIndex(I) >= Idx) {
630 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
631 break;
632 }
633 LastIndex = I + 1;
634 AttrSet.push_back(getSlotAttributes(I));
635 }
636
637 // Now add the attribute into the correct slot. There may already be an
638 // AttributeSet there.
639 AttrBuilder B(AS, Idx);
640
641 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
642 if (Attrs.getSlotIndex(I) == Idx) {
643 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
644 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000645 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000646 break;
647 }
648
649 AttrSet.push_back(AttributeSet::get(C, Idx, B));
650
651 // Add the remaining attribute slots.
652 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
653 AttrSet.push_back(getSlotAttributes(I));
654
655 return get(C, AttrSet);
656}
657
658AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
659 Attribute::AttrKind Attr) const {
660 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
661}
662
663AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
664 AttributeSet Attrs) const {
665 if (!pImpl) return AttributeSet();
666 if (!Attrs.pImpl) return *this;
667
668#ifndef NDEBUG
669 // FIXME it is not obvious how this should work for alignment.
670 // For now, say we can't pass in alignment, which no current use does.
671 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
672 "Attempt to change alignment!");
673#endif
674
675 // Add the attribute slots before the one we're trying to add.
676 SmallVector<AttributeSet, 4> AttrSet;
677 uint64_t NumAttrs = pImpl->getNumAttributes();
678 AttributeSet AS;
679 uint64_t LastIndex = 0;
680 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
681 if (getSlotIndex(I) >= Idx) {
682 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
683 break;
684 }
685 LastIndex = I + 1;
686 AttrSet.push_back(getSlotAttributes(I));
687 }
688
Bill Wendlinge7436542013-01-30 23:07:40 +0000689 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000690 // AttributeSet there.
691 AttrBuilder B(AS, Idx);
692
693 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
694 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000695 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000696 break;
697 }
698
699 AttrSet.push_back(AttributeSet::get(C, Idx, B));
700
701 // Add the remaining attribute slots.
702 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
703 AttrSet.push_back(getSlotAttributes(I));
704
705 return get(C, AttrSet);
706}
707
708//===----------------------------------------------------------------------===//
709// AttributeSet Accessor Methods
710//===----------------------------------------------------------------------===//
711
712AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
713 return pImpl && hasAttributes(Idx) ?
714 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000715 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000716 std::make_pair(Idx, getAttributes(Idx)))) :
717 AttributeSet();
718}
719
720AttributeSet AttributeSet::getRetAttributes() const {
721 return pImpl && hasAttributes(ReturnIndex) ?
722 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000723 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000724 std::make_pair(ReturnIndex,
725 getAttributes(ReturnIndex)))) :
726 AttributeSet();
727}
728
729AttributeSet AttributeSet::getFnAttributes() const {
730 return pImpl && hasAttributes(FunctionIndex) ?
731 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000732 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000733 std::make_pair(FunctionIndex,
734 getAttributes(FunctionIndex)))) :
735 AttributeSet();
736}
737
738bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000739 AttributeSetNode *ASN = getAttributes(Index);
740 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000741}
742
743bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000744 AttributeSetNode *ASN = getAttributes(Index);
745 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000746}
747
748/// \brief Return true if the specified attribute is set for at least one
749/// parameter or for the return value.
750bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
751 if (pImpl == 0) return false;
752
753 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
754 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
755 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000756 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000757 return true;
758
759 return false;
760}
761
Bill Wendling606c8e32013-01-29 03:20:31 +0000762unsigned AttributeSet::getParamAlignment(unsigned Index) const {
763 AttributeSetNode *ASN = getAttributes(Index);
764 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000765}
766
767unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000768 AttributeSetNode *ASN = getAttributes(Index);
769 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000770}
771
772std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000773 AttributeSetNode *ASN = getAttributes(Index);
774 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000775}
776
777/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000778AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
779 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000780
Bill Wendling606c8e32013-01-29 03:20:31 +0000781 // Loop through to find the attribute node we want.
782 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
783 if (pImpl->getSlotIndex(I) == Idx)
784 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000785
Bill Wendling606c8e32013-01-29 03:20:31 +0000786 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000787}
788
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000789AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000790 if (!pImpl)
791 return ArrayRef<Attribute>().begin();
792 return pImpl->begin(Idx);
793}
794
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000795AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000796 if (!pImpl)
797 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000798 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000799}
800
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000801//===----------------------------------------------------------------------===//
802// AttributeSet Introspection Methods
803//===----------------------------------------------------------------------===//
804
805/// \brief Return the number of slots used in this attribute list. This is the
806/// number of arguments that have an attribute set on them (including the
807/// function itself).
808unsigned AttributeSet::getNumSlots() const {
809 return pImpl ? pImpl->getNumAttributes() : 0;
810}
811
812uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
813 assert(pImpl && Slot < pImpl->getNumAttributes() &&
814 "Slot # out of range!");
815 return pImpl->getSlotIndex(Slot);
816}
817
818AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
819 assert(pImpl && Slot < pImpl->getNumAttributes() &&
820 "Slot # out of range!");
821 return pImpl->getSlotAttributes(Slot);
822}
823
824uint64_t AttributeSet::Raw(unsigned Index) const {
825 // FIXME: Remove this.
826 return pImpl ? pImpl->Raw(Index) : 0;
827}
828
829void AttributeSet::dump() const {
830 dbgs() << "PAL[\n";
831
832 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
833 uint64_t Index = getSlotIndex(i);
834 dbgs() << " { ";
835 if (Index == ~0U)
836 dbgs() << "~0U";
837 else
838 dbgs() << Index;
839 dbgs() << " => " << getAsString(Index) << " }\n";
840 }
841
842 dbgs() << "]\n";
843}
844
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000845//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000846// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000847//===----------------------------------------------------------------------===//
848
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000849AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
850 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000851 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000852 if (!pImpl) return;
853
Bill Wendling73bc4522013-01-28 00:21:34 +0000854 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
855 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000856
Bill Wendling383da6b2013-01-30 21:22:59 +0000857 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000858 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000859 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000860
861 break;
862 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000863}
864
Bill Wendling03198882013-01-04 23:27:34 +0000865void AttrBuilder::clear() {
866 Attrs.clear();
867 Alignment = StackAlignment = 0;
868}
869
870AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000871 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
872 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000873 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000874 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000875}
876
Bill Wendling39da0782013-01-31 23:38:01 +0000877AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000878 // FIXME: Handle string attributes.
879 Attribute::AttrKind Kind = Attr.getKindAsEnum();
880 Attrs.insert(Kind);
Bill Wendling49f60602013-01-28 05:23:28 +0000881
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000882 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000883 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000884 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000885 StackAlignment = Attr.getStackAlignment();
886 return *this;
887}
888
Bill Wendlingea59f892013-02-05 08:09:32 +0000889AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
890 TargetDepAttrs[A] = V;
891 return *this;
892}
893
Bill Wendling39da0782013-01-31 23:38:01 +0000894AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
895 Attrs.erase(Val);
896
897 if (Val == Attribute::Alignment)
898 Alignment = 0;
899 else if (Val == Attribute::StackAlignment)
900 StackAlignment = 0;
901
902 return *this;
903}
904
Bill Wendlinge7436542013-01-30 23:07:40 +0000905AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000906 unsigned Idx = ~0U;
907 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
908 if (A.getSlotIndex(I) == Index) {
909 Idx = I;
910 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000911 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000912
913 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
914
915 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000916 // FIXME: Support string attributes.
917 Attribute::AttrKind Kind = I->getKindAsEnum();
Bill Wendling30d2c762013-02-01 00:13:50 +0000918 Attrs.erase(Kind);
919
920 if (Kind == Attribute::Alignment)
921 Alignment = 0;
922 else if (Kind == Attribute::StackAlignment)
923 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000924 }
925
926 return *this;
927}
928
Bill Wendlingea59f892013-02-05 08:09:32 +0000929AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
930 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
931 if (I != TargetDepAttrs.end())
932 TargetDepAttrs.erase(I);
933 return *this;
934}
935
Bill Wendling702cc912012-10-15 20:35:56 +0000936AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000937 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000938
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000939 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
940 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000941
942 Attrs.insert(Attribute::Alignment);
943 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000944 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000945}
946
Bill Wendling03198882013-01-04 23:27:34 +0000947AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
948 // Default alignment, allow the target to define how to align it.
949 if (Align == 0) return *this;
950
951 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
952 assert(Align <= 0x100 && "Alignment too large.");
953
954 Attrs.insert(Attribute::StackAlignment);
955 StackAlignment = Align;
956 return *this;
957}
958
Bill Wendling85df6b42013-02-06 01:16:00 +0000959AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
960 // FIXME: What if both have alignments, but they don't match?!
961 if (!Alignment)
962 Alignment = B.Alignment;
963
964 if (!StackAlignment)
965 StackAlignment = B.StackAlignment;
966
967 Attrs.insert(B.Attrs.begin(), B.Attrs.end());
968
969 for (td_const_iterator I = B.TargetDepAttrs.begin(),
970 E = B.TargetDepAttrs.end(); I != E; ++I)
971 TargetDepAttrs[I->first] = I->second;
972
973 return *this;
974}
975
Bill Wendling22bd6412013-01-03 01:54:39 +0000976bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000977 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000978}
979
Bill Wendling702cc912012-10-15 20:35:56 +0000980bool AttrBuilder::hasAttributes() const {
Bill Wendling03198882013-01-04 23:27:34 +0000981 return !Attrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +0000982}
Bill Wendling60507d52013-01-04 20:54:35 +0000983
Bill Wendlinge7436542013-01-30 23:07:40 +0000984bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +0000985 unsigned Idx = ~0U;
986 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
987 if (A.getSlotIndex(I) == Index) {
988 Idx = I;
989 break;
990 }
991
992 assert(Idx != ~0U && "Couldn't find the index!");
993
994 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000995 I != E; ++I)
996 // FIXME: Support string attributes.
997 if (Attrs.count(I->getKindAsEnum()))
Bill Wendlingbdcbccc2013-02-02 00:42:06 +0000998 return true;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +0000999
1000 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001001}
Bill Wendling60507d52013-01-04 20:54:35 +00001002
Bill Wendling702cc912012-10-15 20:35:56 +00001003bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001004 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001005}
1006
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001007bool AttrBuilder::operator==(const AttrBuilder &B) {
1008 SmallVector<Attribute::AttrKind, 8> This(Attrs.begin(), Attrs.end());
1009 SmallVector<Attribute::AttrKind, 8> That(B.Attrs.begin(), B.Attrs.end());
1010 return This == That;
1011}
1012
1013AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001014 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001015 if (!Val) return *this;
1016
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001017 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1018 I = Attribute::AttrKind(I + 1)) {
1019 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1020 Attrs.insert(I);
1021
1022 if (I == Attribute::Alignment)
1023 Alignment = 1ULL << ((A >> 16) - 1);
1024 else if (I == Attribute::StackAlignment)
1025 StackAlignment = 1ULL << ((A >> 26)-1);
1026 }
1027 }
1028
1029 return *this;
1030}
1031
Bill Wendling8e47daf2013-01-25 23:09:36 +00001032//===----------------------------------------------------------------------===//
1033// AttributeFuncs Function Defintions
1034//===----------------------------------------------------------------------===//
1035
Bill Wendling7beee282013-02-01 01:04:27 +00001036/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001037AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001038 AttrBuilder Incompatible;
1039
1040 if (!Ty->isIntegerTy())
1041 // Attribute that only apply to integers.
1042 Incompatible.addAttribute(Attribute::SExt)
1043 .addAttribute(Attribute::ZExt);
1044
1045 if (!Ty->isPointerTy())
1046 // Attribute that only apply to pointers.
1047 Incompatible.addAttribute(Attribute::ByVal)
1048 .addAttribute(Attribute::Nest)
1049 .addAttribute(Attribute::NoAlias)
1050 .addAttribute(Attribute::NoCapture)
1051 .addAttribute(Attribute::StructRet);
1052
Bill Wendlinge7436542013-01-30 23:07:40 +00001053 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001054}