blob: d338d6538e845b84ef703de3ae0d7eb944aa54ab [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 Wendlingb29ce262013-02-11 08:43:33 +0000153std::string Attribute::getAsString(bool InAttrGrp) 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;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000224 Result += "align";
225 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000226 Result += utostr(getValueAsInt());
227 return Result;
228 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000229
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000230 if (hasAttribute(Attribute::StackAlignment)) {
231 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000232 Result += "alignstack";
233 if (InAttrGrp) {
234 Result += "=";
235 Result += utostr(getValueAsInt());
236 } else {
237 Result += "(";
238 Result += utostr(getValueAsInt());
239 Result += ")";
240 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000241 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000242 }
Bill Wendling14292a62013-01-31 20:59:05 +0000243
244 // Convert target-dependent attributes to strings of the form:
245 //
246 // "kind"
247 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000248 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000249 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000250 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000251 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000252
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000253 StringRef Val = pImpl->getValueAsString();
254 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000255
Bill Wendlingb29ce262013-02-11 08:43:33 +0000256 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000257 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000258 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000259
260 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000261}
262
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000263bool Attribute::operator<(Attribute A) const {
264 if (!pImpl && !A.pImpl) return false;
265 if (!pImpl) return true;
266 if (!A.pImpl) return false;
267 return *pImpl < *A.pImpl;
268}
269
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000270//===----------------------------------------------------------------------===//
271// AttributeImpl Definition
272//===----------------------------------------------------------------------===//
273
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000274AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
275 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
276
277AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
278 unsigned Align)
279 : Context(C) {
280 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
281 "Wrong kind for alignment attribute!");
282 Entry = new AlignAttributeEntry(Kind, Align);
283}
284
285AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
286 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
287
288AttributeImpl::~AttributeImpl() {
289 delete Entry;
290}
291
292bool AttributeImpl::isEnumAttribute() const {
293 return isa<EnumAttributeEntry>(Entry);
294}
295
296bool AttributeImpl::isAlignAttribute() const {
297 return isa<AlignAttributeEntry>(Entry);
298}
299
300bool AttributeImpl::isStringAttribute() const {
301 return isa<StringAttributeEntry>(Entry);
302}
303
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000304bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000305 if (isStringAttribute()) return false;
306 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000307}
308
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000309bool AttributeImpl::hasAttribute(StringRef Kind) const {
310 if (!isStringAttribute()) return false;
311 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000312}
313
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000314Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
315 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
316 return E->getEnumKind();
317 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000318}
319
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000320uint64_t AttributeImpl::getValueAsInt() const {
321 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000322}
323
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000324StringRef AttributeImpl::getKindAsString() const {
325 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000326}
327
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000328StringRef AttributeImpl::getValueAsString() const {
329 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000330}
331
332bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000333 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
334 // relative to their enum value) and then strings.
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000335 if (isEnumAttribute())
336 if (AI.isAlignAttribute() || AI.isEnumAttribute())
337 return getKindAsEnum() < AI.getKindAsEnum();
Bill Wendling7beee282013-02-01 01:04:27 +0000338
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000339 if (isAlignAttribute()) {
340 if (!AI.isStringAttribute() && getKindAsEnum() < AI.getKindAsEnum())
341 return true;
342 if (AI.isAlignAttribute())
343 return getValueAsInt() < AI.getValueAsInt();
344 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000345
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000346 if (isStringAttribute()) {
347 if (!AI.isStringAttribute()) return false;
348 if (getKindAsString() < AI.getKindAsString()) return true;
349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
351 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000352
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000353 return false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000354}
355
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000356uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
357 // FIXME: Remove this.
358 switch (Val) {
359 case Attribute::EndAttrKinds:
360 case Attribute::AttrKindEmptyKey:
361 case Attribute::AttrKindTombstoneKey:
362 llvm_unreachable("Synthetic enumerators which should never get here");
363
364 case Attribute::None: return 0;
365 case Attribute::ZExt: return 1 << 0;
366 case Attribute::SExt: return 1 << 1;
367 case Attribute::NoReturn: return 1 << 2;
368 case Attribute::InReg: return 1 << 3;
369 case Attribute::StructRet: return 1 << 4;
370 case Attribute::NoUnwind: return 1 << 5;
371 case Attribute::NoAlias: return 1 << 6;
372 case Attribute::ByVal: return 1 << 7;
373 case Attribute::Nest: return 1 << 8;
374 case Attribute::ReadNone: return 1 << 9;
375 case Attribute::ReadOnly: return 1 << 10;
376 case Attribute::NoInline: return 1 << 11;
377 case Attribute::AlwaysInline: return 1 << 12;
378 case Attribute::OptimizeForSize: return 1 << 13;
379 case Attribute::StackProtect: return 1 << 14;
380 case Attribute::StackProtectReq: return 1 << 15;
381 case Attribute::Alignment: return 31 << 16;
382 case Attribute::NoCapture: return 1 << 21;
383 case Attribute::NoRedZone: return 1 << 22;
384 case Attribute::NoImplicitFloat: return 1 << 23;
385 case Attribute::Naked: return 1 << 24;
386 case Attribute::InlineHint: return 1 << 25;
387 case Attribute::StackAlignment: return 7 << 26;
388 case Attribute::ReturnsTwice: return 1 << 29;
389 case Attribute::UWTable: return 1 << 30;
390 case Attribute::NonLazyBind: return 1U << 31;
391 case Attribute::AddressSafety: return 1ULL << 32;
392 case Attribute::MinSize: return 1ULL << 33;
393 case Attribute::NoDuplicate: return 1ULL << 34;
394 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000395 case Attribute::ThreadSafety: return 1ULL << 36;
396 case Attribute::UninitializedChecks: return 1ULL << 37;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000397 }
398 llvm_unreachable("Unsupported attribute type");
399}
400
401//===----------------------------------------------------------------------===//
402// AttributeSetNode Definition
403//===----------------------------------------------------------------------===//
404
405AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
406 ArrayRef<Attribute> Attrs) {
407 if (Attrs.empty())
408 return 0;
409
410 // Otherwise, build a key to look up the existing attributes.
411 LLVMContextImpl *pImpl = C.pImpl;
412 FoldingSetNodeID ID;
413
414 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
415 std::sort(SortedAttrs.begin(), SortedAttrs.end());
416
417 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
418 E = SortedAttrs.end(); I != E; ++I)
419 I->Profile(ID);
420
421 void *InsertPoint;
422 AttributeSetNode *PA =
423 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
424
425 // If we didn't find any existing attributes of the same shape then create a
426 // new one and insert it.
427 if (!PA) {
428 PA = new AttributeSetNode(SortedAttrs);
429 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
430 }
431
432 // Return the AttributesListNode that we found or created.
433 return PA;
434}
435
Bill Wendling606c8e32013-01-29 03:20:31 +0000436bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
437 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
438 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000439 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000440 return true;
441 return false;
442}
443
444unsigned AttributeSetNode::getAlignment() const {
445 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
446 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000447 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000448 return I->getAlignment();
449 return 0;
450}
451
452unsigned AttributeSetNode::getStackAlignment() const {
453 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
454 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000455 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000456 return I->getStackAlignment();
457 return 0;
458}
459
Bill Wendlingb29ce262013-02-11 08:43:33 +0000460std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000461 std::string Str = "";
462 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000463 E = AttrList.end(); I != E; ) {
Bill Wendlingb29ce262013-02-11 08:43:33 +0000464 Str += I->getAsString(InAttrGrp);
Bill Wendling7beee282013-02-01 01:04:27 +0000465 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000466 }
467 return Str;
468}
469
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000470//===----------------------------------------------------------------------===//
471// AttributeSetImpl Definition
472//===----------------------------------------------------------------------===//
473
474uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
475 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
476 if (getSlotIndex(I) != Index) continue;
477 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000478 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000479
480 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000481 IE = ASN->end(); II != IE; ++II) {
482 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000483
484 // This cannot handle string attributes.
485 if (Attr.isStringAttribute()) continue;
486
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000487 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000488
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000489 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000490 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000491 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000492 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
493 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000494 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000495 }
496
497 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000498 }
499
500 return 0;
501}
502
503//===----------------------------------------------------------------------===//
504// AttributeSet Construction and Mutation Methods
505//===----------------------------------------------------------------------===//
506
Bill Wendling8232ece2013-01-29 01:43:29 +0000507AttributeSet
508AttributeSet::getImpl(LLVMContext &C,
509 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000510 LLVMContextImpl *pImpl = C.pImpl;
511 FoldingSetNodeID ID;
512 AttributeSetImpl::Profile(ID, Attrs);
513
514 void *InsertPoint;
515 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
516
517 // If we didn't find any existing attributes of the same shape then
518 // create a new one and insert it.
519 if (!PA) {
520 PA = new AttributeSetImpl(C, Attrs);
521 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
522 }
523
524 // Return the AttributesList that we found or created.
525 return AttributeSet(PA);
526}
527
528AttributeSet AttributeSet::get(LLVMContext &C,
529 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
530 // If there are no attributes then return a null AttributesList pointer.
531 if (Attrs.empty())
532 return AttributeSet();
533
534#ifndef NDEBUG
535 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
536 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
537 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000538 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000539 "Pointless attribute!");
540 }
541#endif
542
543 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
544 // list.
545 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
546 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
547 E = Attrs.end(); I != E; ) {
548 unsigned Index = I->first;
549 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000550 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000551 AttrVec.push_back(I->second);
552 ++I;
553 }
554
555 AttrPairVec.push_back(std::make_pair(Index,
556 AttributeSetNode::get(C, AttrVec)));
557 }
558
559 return getImpl(C, AttrPairVec);
560}
561
562AttributeSet AttributeSet::get(LLVMContext &C,
563 ArrayRef<std::pair<unsigned,
564 AttributeSetNode*> > Attrs) {
565 // If there are no attributes then return a null AttributesList pointer.
566 if (Attrs.empty())
567 return AttributeSet();
568
569 return getImpl(C, Attrs);
570}
571
572AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
573 if (!B.hasAttributes())
574 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000575
Bill Wendling64754f42013-02-05 23:48:36 +0000576 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000577 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
578 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
579 Attribute::AttrKind Kind = *I;
580 if (Kind == Attribute::Alignment)
581 Attrs.push_back(std::make_pair(Idx, Attribute::
582 getWithAlignment(C, B.getAlignment())));
583 else if (Kind == Attribute::StackAlignment)
584 Attrs.push_back(std::make_pair(Idx, Attribute::
585 getWithStackAlignment(C, B.getStackAlignment())));
586 else
587 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
588 }
589
Bill Wendling64754f42013-02-05 23:48:36 +0000590 // Add target-dependent (string) attributes.
591 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
592 I != E; ++I)
593 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
594
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000595 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000596}
597
598AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
599 ArrayRef<Attribute::AttrKind> Kind) {
600 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
601 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
602 E = Kind.end(); I != E; ++I)
603 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
604 return get(C, Attrs);
605}
606
607AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
608 if (Attrs.empty()) return AttributeSet();
609
610 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
611 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
612 AttributeSet AS = Attrs[I];
613 if (!AS.pImpl) continue;
614 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
615 }
616
617 return getImpl(C, AttrNodeVec);
618}
619
620AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
621 Attribute::AttrKind Attr) const {
622 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
623}
624
625AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
626 AttributeSet Attrs) const {
627 if (!pImpl) return Attrs;
628 if (!Attrs.pImpl) return *this;
629
630#ifndef NDEBUG
631 // FIXME it is not obvious how this should work for alignment. For now, say
632 // we can't change a known alignment.
633 unsigned OldAlign = getParamAlignment(Idx);
634 unsigned NewAlign = Attrs.getParamAlignment(Idx);
635 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
636 "Attempt to change alignment!");
637#endif
638
639 // Add the attribute slots before the one we're trying to add.
640 SmallVector<AttributeSet, 4> AttrSet;
641 uint64_t NumAttrs = pImpl->getNumAttributes();
642 AttributeSet AS;
643 uint64_t LastIndex = 0;
644 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
645 if (getSlotIndex(I) >= Idx) {
646 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
647 break;
648 }
649 LastIndex = I + 1;
650 AttrSet.push_back(getSlotAttributes(I));
651 }
652
653 // Now add the attribute into the correct slot. There may already be an
654 // AttributeSet there.
655 AttrBuilder B(AS, Idx);
656
657 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
658 if (Attrs.getSlotIndex(I) == Idx) {
659 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
660 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000661 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000662 break;
663 }
664
665 AttrSet.push_back(AttributeSet::get(C, Idx, B));
666
667 // Add the remaining attribute slots.
668 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
669 AttrSet.push_back(getSlotAttributes(I));
670
671 return get(C, AttrSet);
672}
673
674AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
675 Attribute::AttrKind Attr) const {
676 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
677}
678
679AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
680 AttributeSet Attrs) const {
681 if (!pImpl) return AttributeSet();
682 if (!Attrs.pImpl) return *this;
683
684#ifndef NDEBUG
685 // FIXME it is not obvious how this should work for alignment.
686 // For now, say we can't pass in alignment, which no current use does.
687 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
688 "Attempt to change alignment!");
689#endif
690
691 // Add the attribute slots before the one we're trying to add.
692 SmallVector<AttributeSet, 4> AttrSet;
693 uint64_t NumAttrs = pImpl->getNumAttributes();
694 AttributeSet AS;
695 uint64_t LastIndex = 0;
696 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
697 if (getSlotIndex(I) >= Idx) {
698 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
699 break;
700 }
701 LastIndex = I + 1;
702 AttrSet.push_back(getSlotAttributes(I));
703 }
704
Bill Wendlinge7436542013-01-30 23:07:40 +0000705 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000706 // AttributeSet there.
707 AttrBuilder B(AS, Idx);
708
709 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
710 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000711 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000712 break;
713 }
714
715 AttrSet.push_back(AttributeSet::get(C, Idx, B));
716
717 // Add the remaining attribute slots.
718 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
719 AttrSet.push_back(getSlotAttributes(I));
720
721 return get(C, AttrSet);
722}
723
724//===----------------------------------------------------------------------===//
725// AttributeSet Accessor Methods
726//===----------------------------------------------------------------------===//
727
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000728LLVMContext &AttributeSet::getContext() const {
729 return pImpl->getContext();
730}
731
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000732AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
733 return pImpl && hasAttributes(Idx) ?
734 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000735 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000736 std::make_pair(Idx, getAttributes(Idx)))) :
737 AttributeSet();
738}
739
740AttributeSet AttributeSet::getRetAttributes() const {
741 return pImpl && hasAttributes(ReturnIndex) ?
742 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000743 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000744 std::make_pair(ReturnIndex,
745 getAttributes(ReturnIndex)))) :
746 AttributeSet();
747}
748
749AttributeSet AttributeSet::getFnAttributes() const {
750 return pImpl && hasAttributes(FunctionIndex) ?
751 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000752 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000753 std::make_pair(FunctionIndex,
754 getAttributes(FunctionIndex)))) :
755 AttributeSet();
756}
757
758bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000759 AttributeSetNode *ASN = getAttributes(Index);
760 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000761}
762
763bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000764 AttributeSetNode *ASN = getAttributes(Index);
765 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000766}
767
768/// \brief Return true if the specified attribute is set for at least one
769/// parameter or for the return value.
770bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
771 if (pImpl == 0) return false;
772
773 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
774 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
775 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000776 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000777 return true;
778
779 return false;
780}
781
Bill Wendling606c8e32013-01-29 03:20:31 +0000782unsigned AttributeSet::getParamAlignment(unsigned Index) const {
783 AttributeSetNode *ASN = getAttributes(Index);
784 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000785}
786
787unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000788 AttributeSetNode *ASN = getAttributes(Index);
789 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000790}
791
Bill Wendlingb29ce262013-02-11 08:43:33 +0000792std::string AttributeSet::getAsString(unsigned Index,
793 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000794 AttributeSetNode *ASN = getAttributes(Index);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000795 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000796}
797
798/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000799AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
800 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000801
Bill Wendling606c8e32013-01-29 03:20:31 +0000802 // Loop through to find the attribute node we want.
803 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
804 if (pImpl->getSlotIndex(I) == Idx)
805 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000806
Bill Wendling606c8e32013-01-29 03:20:31 +0000807 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000808}
809
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000810AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000811 if (!pImpl)
812 return ArrayRef<Attribute>().begin();
813 return pImpl->begin(Idx);
814}
815
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000816AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000817 if (!pImpl)
818 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000819 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000820}
821
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000822//===----------------------------------------------------------------------===//
823// AttributeSet Introspection Methods
824//===----------------------------------------------------------------------===//
825
826/// \brief Return the number of slots used in this attribute list. This is the
827/// number of arguments that have an attribute set on them (including the
828/// function itself).
829unsigned AttributeSet::getNumSlots() const {
830 return pImpl ? pImpl->getNumAttributes() : 0;
831}
832
833uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
834 assert(pImpl && Slot < pImpl->getNumAttributes() &&
835 "Slot # out of range!");
836 return pImpl->getSlotIndex(Slot);
837}
838
839AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
840 assert(pImpl && Slot < pImpl->getNumAttributes() &&
841 "Slot # out of range!");
842 return pImpl->getSlotAttributes(Slot);
843}
844
845uint64_t AttributeSet::Raw(unsigned Index) const {
846 // FIXME: Remove this.
847 return pImpl ? pImpl->Raw(Index) : 0;
848}
849
850void AttributeSet::dump() const {
851 dbgs() << "PAL[\n";
852
853 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
854 uint64_t Index = getSlotIndex(i);
855 dbgs() << " { ";
856 if (Index == ~0U)
857 dbgs() << "~0U";
858 else
859 dbgs() << Index;
860 dbgs() << " => " << getAsString(Index) << " }\n";
861 }
862
863 dbgs() << "]\n";
864}
865
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000866//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000867// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000868//===----------------------------------------------------------------------===//
869
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000870AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
871 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000872 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000873 if (!pImpl) return;
874
Bill Wendling73bc4522013-01-28 00:21:34 +0000875 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
876 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000877
Bill Wendling383da6b2013-01-30 21:22:59 +0000878 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000879 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000880 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000881
882 break;
883 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000884}
885
Bill Wendling03198882013-01-04 23:27:34 +0000886void AttrBuilder::clear() {
887 Attrs.clear();
888 Alignment = StackAlignment = 0;
889}
890
891AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000892 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
893 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000894 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000895 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000896}
897
Bill Wendling39da0782013-01-31 23:38:01 +0000898AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000899 if (Attr.isStringAttribute()) {
900 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
901 return *this;
902 }
903
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000904 Attribute::AttrKind Kind = Attr.getKindAsEnum();
905 Attrs.insert(Kind);
Bill Wendling49f60602013-01-28 05:23:28 +0000906
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000907 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000908 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000909 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000910 StackAlignment = Attr.getStackAlignment();
911 return *this;
912}
913
Bill Wendlingea59f892013-02-05 08:09:32 +0000914AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
915 TargetDepAttrs[A] = V;
916 return *this;
917}
918
Bill Wendling39da0782013-01-31 23:38:01 +0000919AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
920 Attrs.erase(Val);
921
922 if (Val == Attribute::Alignment)
923 Alignment = 0;
924 else if (Val == Attribute::StackAlignment)
925 StackAlignment = 0;
926
927 return *this;
928}
929
Bill Wendlinge7436542013-01-30 23:07:40 +0000930AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000931 unsigned Idx = ~0U;
932 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
933 if (A.getSlotIndex(I) == Index) {
934 Idx = I;
935 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000936 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000937
938 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
939
940 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000941 // FIXME: Support string attributes.
942 Attribute::AttrKind Kind = I->getKindAsEnum();
Bill Wendling30d2c762013-02-01 00:13:50 +0000943 Attrs.erase(Kind);
944
945 if (Kind == Attribute::Alignment)
946 Alignment = 0;
947 else if (Kind == Attribute::StackAlignment)
948 StackAlignment = 0;
Bill Wendling49f60602013-01-28 05:23:28 +0000949 }
950
951 return *this;
952}
953
Bill Wendlingea59f892013-02-05 08:09:32 +0000954AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
955 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
956 if (I != TargetDepAttrs.end())
957 TargetDepAttrs.erase(I);
958 return *this;
959}
960
Bill Wendling702cc912012-10-15 20:35:56 +0000961AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000962 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +0000963
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000964 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
965 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +0000966
967 Attrs.insert(Attribute::Alignment);
968 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +0000969 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000970}
971
Bill Wendling03198882013-01-04 23:27:34 +0000972AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
973 // Default alignment, allow the target to define how to align it.
974 if (Align == 0) return *this;
975
976 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
977 assert(Align <= 0x100 && "Alignment too large.");
978
979 Attrs.insert(Attribute::StackAlignment);
980 StackAlignment = Align;
981 return *this;
982}
983
Bill Wendling85df6b42013-02-06 01:16:00 +0000984AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
985 // FIXME: What if both have alignments, but they don't match?!
986 if (!Alignment)
987 Alignment = B.Alignment;
988
989 if (!StackAlignment)
990 StackAlignment = B.StackAlignment;
991
992 Attrs.insert(B.Attrs.begin(), B.Attrs.end());
993
994 for (td_const_iterator I = B.TargetDepAttrs.begin(),
995 E = B.TargetDepAttrs.end(); I != E; ++I)
996 TargetDepAttrs[I->first] = I->second;
997
998 return *this;
999}
1000
Bill Wendling22bd6412013-01-03 01:54:39 +00001001bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +00001002 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +00001003}
1004
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001005bool AttrBuilder::contains(StringRef A) const {
1006 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1007}
1008
Bill Wendling702cc912012-10-15 20:35:56 +00001009bool AttrBuilder::hasAttributes() const {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001010 return !Attrs.empty() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001011}
Bill Wendling60507d52013-01-04 20:54:35 +00001012
Bill Wendlinge7436542013-01-30 23:07:40 +00001013bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001014 unsigned Idx = ~0U;
1015 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1016 if (A.getSlotIndex(I) == Index) {
1017 Idx = I;
1018 break;
1019 }
1020
1021 assert(Idx != ~0U && "Couldn't find the index!");
1022
1023 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling8c74ecf2013-02-05 22:37:24 +00001024 I != E; ++I)
1025 // FIXME: Support string attributes.
1026 if (Attrs.count(I->getKindAsEnum()))
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001027 return true;
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001028
1029 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001030}
Bill Wendling60507d52013-01-04 20:54:35 +00001031
Bill Wendling702cc912012-10-15 20:35:56 +00001032bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001033 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001034}
1035
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001036bool AttrBuilder::operator==(const AttrBuilder &B) {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001037 for (DenseSet<Attribute::AttrKind>::iterator I = Attrs.begin(),
1038 E = Attrs.end(); I != E; ++I)
1039 if (!B.Attrs.count(*I))
1040 return false;
1041
1042 for (td_const_iterator I = TargetDepAttrs.begin(),
1043 E = TargetDepAttrs.end(); I != E; ++I)
1044 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1045 return false;
1046
1047 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001048}
1049
1050AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001051 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001052 if (!Val) return *this;
1053
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001054 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1055 I = Attribute::AttrKind(I + 1)) {
1056 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1057 Attrs.insert(I);
1058
1059 if (I == Attribute::Alignment)
1060 Alignment = 1ULL << ((A >> 16) - 1);
1061 else if (I == Attribute::StackAlignment)
1062 StackAlignment = 1ULL << ((A >> 26)-1);
1063 }
1064 }
1065
1066 return *this;
1067}
1068
Bill Wendling8e47daf2013-01-25 23:09:36 +00001069//===----------------------------------------------------------------------===//
1070// AttributeFuncs Function Defintions
1071//===----------------------------------------------------------------------===//
1072
Bill Wendling7beee282013-02-01 01:04:27 +00001073/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001074AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001075 AttrBuilder Incompatible;
1076
1077 if (!Ty->isIntegerTy())
1078 // Attribute that only apply to integers.
1079 Incompatible.addAttribute(Attribute::SExt)
1080 .addAttribute(Attribute::ZExt);
1081
1082 if (!Ty->isPointerTy())
1083 // Attribute that only apply to pointers.
1084 Incompatible.addAttribute(Attribute::ByVal)
1085 .addAttribute(Attribute::Nest)
1086 .addAttribute(Attribute::NoAlias)
1087 .addAttribute(Attribute::NoCapture)
1088 .addAttribute(Attribute::StructRet);
1089
Bill Wendlinge7436542013-01-30 23:07:40 +00001090 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001091}