blob: 267c1aa893273a34f7e6a3eff40e268434c07f8d [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";
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000208 if (hasAttribute(Attribute::ThreadSafety))
209 return "thread_safety";
210 if (hasAttribute(Attribute::UninitializedChecks))
211 return "uninitialized_checks";
Bill Wendling14292a62013-01-31 20:59:05 +0000212 if (hasAttribute(Attribute::UWTable))
213 return "uwtable";
214 if (hasAttribute(Attribute::ZExt))
215 return "zeroext";
216
217 // FIXME: These should be output like this:
218 //
219 // align=4
220 // alignstack=8
221 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000222 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000223 std::string Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000224 Result += "align ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000225 Result += utostr(getValueAsInt());
226 return Result;
227 }
228 if (hasAttribute(Attribute::StackAlignment)) {
229 std::string Result;
230 Result += "alignstack(";
231 Result += utostr(getValueAsInt());
232 Result += ")";
Bill Wendling606c8e32013-01-29 03:20:31 +0000233 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000234 }
Bill Wendling14292a62013-01-31 20:59:05 +0000235
236 // Convert target-dependent attributes to strings of the form:
237 //
238 // "kind"
239 // "kind" = "value"
Bill Wendling5a4041e2013-02-01 22:32:30 +0000240 // "kind" = ( "value1" "value2" "value3" )
Bill Wendling14292a62013-01-31 20:59:05 +0000241 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000242 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000243 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000244 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000245
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000246 StringRef Val = pImpl->getValueAsString();
247 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000248
Bill Wendling14292a62013-01-31 20:59:05 +0000249 Result += " = ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000250 Result += '\"' + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000251 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000252 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000253
254 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000255}
256
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000257bool Attribute::operator<(Attribute A) const {
258 if (!pImpl && !A.pImpl) return false;
259 if (!pImpl) return true;
260 if (!A.pImpl) return false;
261 return *pImpl < *A.pImpl;
262}
263
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000264//===----------------------------------------------------------------------===//
265// AttributeImpl Definition
266//===----------------------------------------------------------------------===//
267
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000268AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
269 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
270
271AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
272 unsigned Align)
273 : Context(C) {
274 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
275 "Wrong kind for alignment attribute!");
276 Entry = new AlignAttributeEntry(Kind, Align);
277}
278
279AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
280 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
281
282AttributeImpl::~AttributeImpl() {
283 delete Entry;
284}
285
286bool AttributeImpl::isEnumAttribute() const {
287 return isa<EnumAttributeEntry>(Entry);
288}
289
290bool AttributeImpl::isAlignAttribute() const {
291 return isa<AlignAttributeEntry>(Entry);
292}
293
294bool AttributeImpl::isStringAttribute() const {
295 return isa<StringAttributeEntry>(Entry);
296}
297
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000298bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000299 if (isStringAttribute()) return false;
300 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000301}
302
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000303bool AttributeImpl::hasAttribute(StringRef Kind) const {
304 if (!isStringAttribute()) return false;
305 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000306}
307
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000308Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
309 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
310 return E->getEnumKind();
311 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312}
313
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000314uint64_t AttributeImpl::getValueAsInt() const {
315 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000316}
317
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000318StringRef AttributeImpl::getKindAsString() const {
319 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000320}
321
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000322StringRef AttributeImpl::getValueAsString() const {
323 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000324}
325
326bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000327 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
328 // relative to their enum value) and then strings.
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000329 if (isEnumAttribute())
330 if (AI.isAlignAttribute() || AI.isEnumAttribute())
331 return getKindAsEnum() < AI.getKindAsEnum();
Bill Wendling7beee282013-02-01 01:04:27 +0000332
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000333 if (isAlignAttribute()) {
334 if (!AI.isStringAttribute() && getKindAsEnum() < AI.getKindAsEnum())
335 return true;
336 if (AI.isAlignAttribute())
337 return getValueAsInt() < AI.getValueAsInt();
338 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000339
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000340 if (isStringAttribute()) {
341 if (!AI.isStringAttribute()) return false;
342 if (getKindAsString() < AI.getKindAsString()) return true;
343 if (getKindAsString() == AI.getKindAsString())
344 return getValueAsString() < AI.getValueAsString();
345 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000346
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000347 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000348}
349
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000350uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
351 // FIXME: Remove this.
352 switch (Val) {
353 case Attribute::EndAttrKinds:
354 case Attribute::AttrKindEmptyKey:
355 case Attribute::AttrKindTombstoneKey:
356 llvm_unreachable("Synthetic enumerators which should never get here");
357
358 case Attribute::None: return 0;
359 case Attribute::ZExt: return 1 << 0;
360 case Attribute::SExt: return 1 << 1;
361 case Attribute::NoReturn: return 1 << 2;
362 case Attribute::InReg: return 1 << 3;
363 case Attribute::StructRet: return 1 << 4;
364 case Attribute::NoUnwind: return 1 << 5;
365 case Attribute::NoAlias: return 1 << 6;
366 case Attribute::ByVal: return 1 << 7;
367 case Attribute::Nest: return 1 << 8;
368 case Attribute::ReadNone: return 1 << 9;
369 case Attribute::ReadOnly: return 1 << 10;
370 case Attribute::NoInline: return 1 << 11;
371 case Attribute::AlwaysInline: return 1 << 12;
372 case Attribute::OptimizeForSize: return 1 << 13;
373 case Attribute::StackProtect: return 1 << 14;
374 case Attribute::StackProtectReq: return 1 << 15;
375 case Attribute::Alignment: return 31 << 16;
376 case Attribute::NoCapture: return 1 << 21;
377 case Attribute::NoRedZone: return 1 << 22;
378 case Attribute::NoImplicitFloat: return 1 << 23;
379 case Attribute::Naked: return 1 << 24;
380 case Attribute::InlineHint: return 1 << 25;
381 case Attribute::StackAlignment: return 7 << 26;
382 case Attribute::ReturnsTwice: return 1 << 29;
383 case Attribute::UWTable: return 1 << 30;
384 case Attribute::NonLazyBind: return 1U << 31;
385 case Attribute::AddressSafety: return 1ULL << 32;
386 case Attribute::MinSize: return 1ULL << 33;
387 case Attribute::NoDuplicate: return 1ULL << 34;
388 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000389 case Attribute::ThreadSafety: return 1ULL << 36;
390 case Attribute::UninitializedChecks: return 1ULL << 37;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000391 }
392 llvm_unreachable("Unsupported attribute type");
393}
394
395//===----------------------------------------------------------------------===//
396// AttributeSetNode Definition
397//===----------------------------------------------------------------------===//
398
399AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
400 ArrayRef<Attribute> Attrs) {
401 if (Attrs.empty())
402 return 0;
403
404 // Otherwise, build a key to look up the existing attributes.
405 LLVMContextImpl *pImpl = C.pImpl;
406 FoldingSetNodeID ID;
407
408 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
409 std::sort(SortedAttrs.begin(), SortedAttrs.end());
410
411 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
412 E = SortedAttrs.end(); I != E; ++I)
413 I->Profile(ID);
414
415 void *InsertPoint;
416 AttributeSetNode *PA =
417 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
418
419 // If we didn't find any existing attributes of the same shape then create a
420 // new one and insert it.
421 if (!PA) {
422 PA = new AttributeSetNode(SortedAttrs);
423 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
424 }
425
426 // Return the AttributesListNode that we found or created.
427 return PA;
428}
429
Bill Wendling606c8e32013-01-29 03:20:31 +0000430bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
431 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
432 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000433 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000434 return true;
435 return false;
436}
437
438unsigned AttributeSetNode::getAlignment() const {
439 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
440 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000441 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000442 return I->getAlignment();
443 return 0;
444}
445
446unsigned AttributeSetNode::getStackAlignment() const {
447 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
448 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000449 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000450 return I->getStackAlignment();
451 return 0;
452}
453
454std::string AttributeSetNode::getAsString() const {
455 std::string Str = "";
456 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000457 E = AttrList.end(); I != E; ) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000458 Str += I->getAsString();
Bill Wendling7beee282013-02-01 01:04:27 +0000459 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000460 }
461 return Str;
462}
463
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000464//===----------------------------------------------------------------------===//
465// AttributeSetImpl Definition
466//===----------------------------------------------------------------------===//
467
468uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
469 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
470 if (getSlotIndex(I) != Index) continue;
471 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000472 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000473
474 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000475 IE = ASN->end(); II != IE; ++II) {
476 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000477
478 // This cannot handle string attributes.
479 if (Attr.isStringAttribute()) continue;
480
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000481 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000482
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000483 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000484 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000485 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000486 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
487 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000488 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000489 }
490
491 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000492 }
493
494 return 0;
495}
496
497//===----------------------------------------------------------------------===//
498// AttributeSet Construction and Mutation Methods
499//===----------------------------------------------------------------------===//
500
Bill Wendling8232ece2013-01-29 01:43:29 +0000501AttributeSet
502AttributeSet::getImpl(LLVMContext &C,
503 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000504 LLVMContextImpl *pImpl = C.pImpl;
505 FoldingSetNodeID ID;
506 AttributeSetImpl::Profile(ID, Attrs);
507
508 void *InsertPoint;
509 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
510
511 // If we didn't find any existing attributes of the same shape then
512 // create a new one and insert it.
513 if (!PA) {
514 PA = new AttributeSetImpl(C, Attrs);
515 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
516 }
517
518 // Return the AttributesList that we found or created.
519 return AttributeSet(PA);
520}
521
522AttributeSet AttributeSet::get(LLVMContext &C,
523 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
524 // If there are no attributes then return a null AttributesList pointer.
525 if (Attrs.empty())
526 return AttributeSet();
527
528#ifndef NDEBUG
529 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
530 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
531 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000532 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000533 "Pointless attribute!");
534 }
535#endif
536
537 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
538 // list.
539 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
540 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
541 E = Attrs.end(); I != E; ) {
542 unsigned Index = I->first;
543 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000544 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000545 AttrVec.push_back(I->second);
546 ++I;
547 }
548
549 AttrPairVec.push_back(std::make_pair(Index,
550 AttributeSetNode::get(C, AttrVec)));
551 }
552
553 return getImpl(C, AttrPairVec);
554}
555
556AttributeSet AttributeSet::get(LLVMContext &C,
557 ArrayRef<std::pair<unsigned,
558 AttributeSetNode*> > Attrs) {
559 // If there are no attributes then return a null AttributesList pointer.
560 if (Attrs.empty())
561 return AttributeSet();
562
563 return getImpl(C, Attrs);
564}
565
566AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
567 if (!B.hasAttributes())
568 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000569
Bill Wendling64754f42013-02-05 23:48:36 +0000570 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000571 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
572 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
573 Attribute::AttrKind Kind = *I;
574 if (Kind == Attribute::Alignment)
575 Attrs.push_back(std::make_pair(Idx, Attribute::
576 getWithAlignment(C, B.getAlignment())));
577 else if (Kind == Attribute::StackAlignment)
578 Attrs.push_back(std::make_pair(Idx, Attribute::
579 getWithStackAlignment(C, B.getStackAlignment())));
580 else
581 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
582 }
583
Bill Wendling64754f42013-02-05 23:48:36 +0000584 // Add target-dependent (string) attributes.
585 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
586 I != E; ++I)
587 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
588
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000589 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000590}
591
592AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
593 ArrayRef<Attribute::AttrKind> Kind) {
594 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
595 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
596 E = Kind.end(); I != E; ++I)
597 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
598 return get(C, Attrs);
599}
600
601AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
602 if (Attrs.empty()) return AttributeSet();
603
604 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
605 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
606 AttributeSet AS = Attrs[I];
607 if (!AS.pImpl) continue;
608 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
609 }
610
611 return getImpl(C, AttrNodeVec);
612}
613
614AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
615 Attribute::AttrKind Attr) const {
616 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
617}
618
619AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
620 AttributeSet Attrs) const {
621 if (!pImpl) return Attrs;
622 if (!Attrs.pImpl) return *this;
623
624#ifndef NDEBUG
625 // FIXME it is not obvious how this should work for alignment. For now, say
626 // we can't change a known alignment.
627 unsigned OldAlign = getParamAlignment(Idx);
628 unsigned NewAlign = Attrs.getParamAlignment(Idx);
629 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
630 "Attempt to change alignment!");
631#endif
632
633 // Add the attribute slots before the one we're trying to add.
634 SmallVector<AttributeSet, 4> AttrSet;
635 uint64_t NumAttrs = pImpl->getNumAttributes();
636 AttributeSet AS;
637 uint64_t LastIndex = 0;
638 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
639 if (getSlotIndex(I) >= Idx) {
640 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
641 break;
642 }
643 LastIndex = I + 1;
644 AttrSet.push_back(getSlotAttributes(I));
645 }
646
647 // Now add the attribute into the correct slot. There may already be an
648 // AttributeSet there.
649 AttrBuilder B(AS, Idx);
650
651 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
652 if (Attrs.getSlotIndex(I) == Idx) {
653 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
654 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000655 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000656 break;
657 }
658
659 AttrSet.push_back(AttributeSet::get(C, Idx, B));
660
661 // Add the remaining attribute slots.
662 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
663 AttrSet.push_back(getSlotAttributes(I));
664
665 return get(C, AttrSet);
666}
667
668AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
669 Attribute::AttrKind Attr) const {
670 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
671}
672
673AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
674 AttributeSet Attrs) const {
675 if (!pImpl) return AttributeSet();
676 if (!Attrs.pImpl) return *this;
677
678#ifndef NDEBUG
679 // FIXME it is not obvious how this should work for alignment.
680 // For now, say we can't pass in alignment, which no current use does.
681 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
682 "Attempt to change alignment!");
683#endif
684
685 // Add the attribute slots before the one we're trying to add.
686 SmallVector<AttributeSet, 4> AttrSet;
687 uint64_t NumAttrs = pImpl->getNumAttributes();
688 AttributeSet AS;
689 uint64_t LastIndex = 0;
690 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
691 if (getSlotIndex(I) >= Idx) {
692 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
693 break;
694 }
695 LastIndex = I + 1;
696 AttrSet.push_back(getSlotAttributes(I));
697 }
698
Bill Wendlinge7436542013-01-30 23:07:40 +0000699 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000700 // AttributeSet there.
701 AttrBuilder B(AS, Idx);
702
703 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
704 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000705 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706 break;
707 }
708
709 AttrSet.push_back(AttributeSet::get(C, Idx, B));
710
711 // Add the remaining attribute slots.
712 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
713 AttrSet.push_back(getSlotAttributes(I));
714
715 return get(C, AttrSet);
716}
717
718//===----------------------------------------------------------------------===//
719// AttributeSet Accessor Methods
720//===----------------------------------------------------------------------===//
721
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000722LLVMContext &AttributeSet::getContext() const {
723 return pImpl->getContext();
724}
725
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000726AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
727 return pImpl && hasAttributes(Idx) ?
728 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000729 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000730 std::make_pair(Idx, getAttributes(Idx)))) :
731 AttributeSet();
732}
733
734AttributeSet AttributeSet::getRetAttributes() const {
735 return pImpl && hasAttributes(ReturnIndex) ?
736 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000737 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000738 std::make_pair(ReturnIndex,
739 getAttributes(ReturnIndex)))) :
740 AttributeSet();
741}
742
743AttributeSet AttributeSet::getFnAttributes() const {
744 return pImpl && hasAttributes(FunctionIndex) ?
745 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000746 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000747 std::make_pair(FunctionIndex,
748 getAttributes(FunctionIndex)))) :
749 AttributeSet();
750}
751
752bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000753 AttributeSetNode *ASN = getAttributes(Index);
754 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000755}
756
757bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000758 AttributeSetNode *ASN = getAttributes(Index);
759 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000760}
761
762/// \brief Return true if the specified attribute is set for at least one
763/// parameter or for the return value.
764bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
765 if (pImpl == 0) return false;
766
767 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
768 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
769 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000770 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000771 return true;
772
773 return false;
774}
775
Bill Wendling606c8e32013-01-29 03:20:31 +0000776unsigned AttributeSet::getParamAlignment(unsigned Index) const {
777 AttributeSetNode *ASN = getAttributes(Index);
778 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000779}
780
781unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000782 AttributeSetNode *ASN = getAttributes(Index);
783 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000784}
785
786std::string AttributeSet::getAsString(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000787 AttributeSetNode *ASN = getAttributes(Index);
788 return ASN ? ASN->getAsString() : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000789}
790
791/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000792AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
793 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000794
Bill Wendling606c8e32013-01-29 03:20:31 +0000795 // Loop through to find the attribute node we want.
796 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
797 if (pImpl->getSlotIndex(I) == Idx)
798 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000799
Bill Wendling606c8e32013-01-29 03:20:31 +0000800 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000801}
802
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000803AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000804 if (!pImpl)
805 return ArrayRef<Attribute>().begin();
806 return pImpl->begin(Idx);
807}
808
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000809AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000810 if (!pImpl)
811 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000812 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000813}
814
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000815//===----------------------------------------------------------------------===//
816// AttributeSet Introspection Methods
817//===----------------------------------------------------------------------===//
818
819/// \brief Return the number of slots used in this attribute list. This is the
820/// number of arguments that have an attribute set on them (including the
821/// function itself).
822unsigned AttributeSet::getNumSlots() const {
823 return pImpl ? pImpl->getNumAttributes() : 0;
824}
825
826uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
827 assert(pImpl && Slot < pImpl->getNumAttributes() &&
828 "Slot # out of range!");
829 return pImpl->getSlotIndex(Slot);
830}
831
832AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
833 assert(pImpl && Slot < pImpl->getNumAttributes() &&
834 "Slot # out of range!");
835 return pImpl->getSlotAttributes(Slot);
836}
837
838uint64_t AttributeSet::Raw(unsigned Index) const {
839 // FIXME: Remove this.
840 return pImpl ? pImpl->Raw(Index) : 0;
841}
842
843void AttributeSet::dump() const {
844 dbgs() << "PAL[\n";
845
846 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
847 uint64_t Index = getSlotIndex(i);
848 dbgs() << " { ";
849 if (Index == ~0U)
850 dbgs() << "~0U";
851 else
852 dbgs() << Index;
853 dbgs() << " => " << getAsString(Index) << " }\n";
854 }
855
856 dbgs() << "]\n";
857}
858
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000859//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000860// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000861//===----------------------------------------------------------------------===//
862
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000863AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
864 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000865 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000866 if (!pImpl) return;
867
Bill Wendling73bc4522013-01-28 00:21:34 +0000868 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
869 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000870
Bill Wendling383da6b2013-01-30 21:22:59 +0000871 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000872 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000873 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000874
875 break;
876 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000877}
878
Bill Wendling03198882013-01-04 23:27:34 +0000879void AttrBuilder::clear() {
880 Attrs.clear();
881 Alignment = StackAlignment = 0;
882}
883
884AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000885 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
886 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000887 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000888 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000889}
890
Bill Wendling39da0782013-01-31 23:38:01 +0000891AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000892 if (Attr.isStringAttribute()) {
893 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
894 return *this;
895 }
896
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000897 Attribute::AttrKind Kind = Attr.getKindAsEnum();
898 Attrs.insert(Kind);
Bill Wendling49f60602013-01-28 05:23:28 +0000899
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000900 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000901 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000902 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000903 StackAlignment = Attr.getStackAlignment();
904 return *this;
905}
906
Bill Wendlingea59f892013-02-05 08:09:32 +0000907AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
908 TargetDepAttrs[A] = V;
909 return *this;
910}
911
Bill Wendling39da0782013-01-31 23:38:01 +0000912AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
913 Attrs.erase(Val);
914
915 if (Val == Attribute::Alignment)
916 Alignment = 0;
917 else if (Val == Attribute::StackAlignment)
918 StackAlignment = 0;
919
920 return *this;
921}
922
Bill Wendlinge7436542013-01-30 23:07:40 +0000923AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000924 unsigned Idx = ~0U;
925 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
926 if (A.getSlotIndex(I) == Index) {
927 Idx = I;
928 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000929 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000930
931 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
932
933 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000934 // FIXME: Support string attributes.
935 Attribute::AttrKind Kind = I->getKindAsEnum();
Bill Wendling30d2c762013-02-01 00:13:50 +0000936 Attrs.erase(Kind);
937
938 if (Kind == Attribute::Alignment)
939 Alignment = 0;
940 else if (Kind == Attribute::StackAlignment)
941 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000942 }
943
944 return *this;
945}
946
Bill Wendlingea59f892013-02-05 08:09:32 +0000947AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
948 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
949 if (I != TargetDepAttrs.end())
950 TargetDepAttrs.erase(I);
951 return *this;
952}
953
Bill Wendling702cc912012-10-15 20:35:56 +0000954AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000955 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000956
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000957 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
958 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000959
960 Attrs.insert(Attribute::Alignment);
961 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000962 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000963}
964
Bill Wendling03198882013-01-04 23:27:34 +0000965AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
966 // Default alignment, allow the target to define how to align it.
967 if (Align == 0) return *this;
968
969 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
970 assert(Align <= 0x100 && "Alignment too large.");
971
972 Attrs.insert(Attribute::StackAlignment);
973 StackAlignment = Align;
974 return *this;
975}
976
Bill Wendling85df6b42013-02-06 01:16:00 +0000977AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
978 // FIXME: What if both have alignments, but they don't match?!
979 if (!Alignment)
980 Alignment = B.Alignment;
981
982 if (!StackAlignment)
983 StackAlignment = B.StackAlignment;
984
985 Attrs.insert(B.Attrs.begin(), B.Attrs.end());
986
987 for (td_const_iterator I = B.TargetDepAttrs.begin(),
988 E = B.TargetDepAttrs.end(); I != E; ++I)
989 TargetDepAttrs[I->first] = I->second;
990
991 return *this;
992}
993
Bill Wendling22bd6412013-01-03 01:54:39 +0000994bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +0000995 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +0000996}
997
Bill Wendlingc342d9d2013-02-06 01:33:42 +0000998bool AttrBuilder::contains(StringRef A) const {
999 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1000}
1001
Bill Wendling702cc912012-10-15 20:35:56 +00001002bool AttrBuilder::hasAttributes() const {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001003 return !Attrs.empty() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001004}
Bill Wendling60507d52013-01-04 20:54:35 +00001005
Bill Wendlinge7436542013-01-30 23:07:40 +00001006bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001007 unsigned Idx = ~0U;
1008 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1009 if (A.getSlotIndex(I) == Index) {
1010 Idx = I;
1011 break;
1012 }
1013
1014 assert(Idx != ~0U && "Couldn't find the index!");
1015
1016 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling8c74ecf2013-02-05 22:37:24 +00001017 I != E; ++I)
1018 // FIXME: Support string attributes.
1019 if (Attrs.count(I->getKindAsEnum()))
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001020 return true;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001021
1022 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001023}
Bill Wendling60507d52013-01-04 20:54:35 +00001024
Bill Wendling702cc912012-10-15 20:35:56 +00001025bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001026 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001027}
1028
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001029bool AttrBuilder::operator==(const AttrBuilder &B) {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001030 for (DenseSet<Attribute::AttrKind>::iterator I = Attrs.begin(),
1031 E = Attrs.end(); I != E; ++I)
1032 if (!B.Attrs.count(*I))
1033 return false;
1034
1035 for (td_const_iterator I = TargetDepAttrs.begin(),
1036 E = TargetDepAttrs.end(); I != E; ++I)
1037 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1038 return false;
1039
1040 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001041}
1042
1043AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001044 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001045 if (!Val) return *this;
1046
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001047 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1048 I = Attribute::AttrKind(I + 1)) {
1049 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1050 Attrs.insert(I);
1051
1052 if (I == Attribute::Alignment)
1053 Alignment = 1ULL << ((A >> 16) - 1);
1054 else if (I == Attribute::StackAlignment)
1055 StackAlignment = 1ULL << ((A >> 26)-1);
1056 }
1057 }
1058
1059 return *this;
1060}
1061
Bill Wendling8e47daf2013-01-25 23:09:36 +00001062//===----------------------------------------------------------------------===//
1063// AttributeFuncs Function Defintions
1064//===----------------------------------------------------------------------===//
1065
Bill Wendling7beee282013-02-01 01:04:27 +00001066/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001067AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001068 AttrBuilder Incompatible;
1069
1070 if (!Ty->isIntegerTy())
1071 // Attribute that only apply to integers.
1072 Incompatible.addAttribute(Attribute::SExt)
1073 .addAttribute(Attribute::ZExt);
1074
1075 if (!Ty->isPointerTy())
1076 // Attribute that only apply to pointers.
1077 Incompatible.addAttribute(Attribute::ByVal)
1078 .addAttribute(Attribute::Nest)
1079 .addAttribute(Attribute::NoAlias)
1080 .addAttribute(Attribute::NoCapture)
1081 .addAttribute(Attribute::StructRet);
1082
Bill Wendlinge7436542013-01-30 23:07:40 +00001083 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001084}