blob: 629679c6831d039aeca09a138145bb1f95f240fd [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 Wendling94328f42013-02-15 05:25:26 +0000335 if (isEnumAttribute()) {
336 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
337 if (AI.isAlignAttribute()) return true;
338 if (AI.isStringAttribute()) return true;
339 }
Bill Wendling7beee282013-02-01 01:04:27 +0000340
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000341 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000342 if (AI.isEnumAttribute()) return false;
343 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
344 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000345 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000346
Bill Wendling94328f42013-02-15 05:25:26 +0000347 if (AI.isEnumAttribute()) return false;
348 if (AI.isAlignAttribute()) return false;
349 if (getKindAsString() == AI.getKindAsString())
350 return getValueAsString() < AI.getValueAsString();
351 return getKindAsString() < AI.getKindAsString();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000352}
353
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000354uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
355 // FIXME: Remove this.
356 switch (Val) {
357 case Attribute::EndAttrKinds:
358 case Attribute::AttrKindEmptyKey:
359 case Attribute::AttrKindTombstoneKey:
360 llvm_unreachable("Synthetic enumerators which should never get here");
361
362 case Attribute::None: return 0;
363 case Attribute::ZExt: return 1 << 0;
364 case Attribute::SExt: return 1 << 1;
365 case Attribute::NoReturn: return 1 << 2;
366 case Attribute::InReg: return 1 << 3;
367 case Attribute::StructRet: return 1 << 4;
368 case Attribute::NoUnwind: return 1 << 5;
369 case Attribute::NoAlias: return 1 << 6;
370 case Attribute::ByVal: return 1 << 7;
371 case Attribute::Nest: return 1 << 8;
372 case Attribute::ReadNone: return 1 << 9;
373 case Attribute::ReadOnly: return 1 << 10;
374 case Attribute::NoInline: return 1 << 11;
375 case Attribute::AlwaysInline: return 1 << 12;
376 case Attribute::OptimizeForSize: return 1 << 13;
377 case Attribute::StackProtect: return 1 << 14;
378 case Attribute::StackProtectReq: return 1 << 15;
379 case Attribute::Alignment: return 31 << 16;
380 case Attribute::NoCapture: return 1 << 21;
381 case Attribute::NoRedZone: return 1 << 22;
382 case Attribute::NoImplicitFloat: return 1 << 23;
383 case Attribute::Naked: return 1 << 24;
384 case Attribute::InlineHint: return 1 << 25;
385 case Attribute::StackAlignment: return 7 << 26;
386 case Attribute::ReturnsTwice: return 1 << 29;
387 case Attribute::UWTable: return 1 << 30;
388 case Attribute::NonLazyBind: return 1U << 31;
389 case Attribute::AddressSafety: return 1ULL << 32;
390 case Attribute::MinSize: return 1ULL << 33;
391 case Attribute::NoDuplicate: return 1ULL << 34;
392 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryanyab39afa2013-02-11 08:13:54 +0000393 case Attribute::ThreadSafety: return 1ULL << 36;
394 case Attribute::UninitializedChecks: return 1ULL << 37;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000395 }
396 llvm_unreachable("Unsupported attribute type");
397}
398
399//===----------------------------------------------------------------------===//
400// AttributeSetNode Definition
401//===----------------------------------------------------------------------===//
402
403AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
404 ArrayRef<Attribute> Attrs) {
405 if (Attrs.empty())
406 return 0;
407
408 // Otherwise, build a key to look up the existing attributes.
409 LLVMContextImpl *pImpl = C.pImpl;
410 FoldingSetNodeID ID;
411
412 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000413 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000414
415 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
416 E = SortedAttrs.end(); I != E; ++I)
417 I->Profile(ID);
418
419 void *InsertPoint;
420 AttributeSetNode *PA =
421 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
422
423 // If we didn't find any existing attributes of the same shape then create a
424 // new one and insert it.
425 if (!PA) {
426 PA = new AttributeSetNode(SortedAttrs);
427 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
428 }
429
430 // Return the AttributesListNode that we found or created.
431 return PA;
432}
433
Bill Wendling606c8e32013-01-29 03:20:31 +0000434bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
435 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
436 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000437 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000438 return true;
439 return false;
440}
441
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000442bool AttributeSetNode::hasAttribute(StringRef Kind) const {
443 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
444 E = AttrList.end(); I != E; ++I)
445 if (I->hasAttribute(Kind))
446 return true;
447 return false;
448}
449
450Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
451 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
452 E = AttrList.end(); I != E; ++I)
453 if (I->hasAttribute(Kind))
454 return *I;
455 return Attribute();
456}
457
458Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
459 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
460 E = AttrList.end(); I != E; ++I)
461 if (I->hasAttribute(Kind))
462 return *I;
463 return Attribute();
464}
465
Bill Wendling606c8e32013-01-29 03:20:31 +0000466unsigned AttributeSetNode::getAlignment() const {
467 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
468 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000469 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000470 return I->getAlignment();
471 return 0;
472}
473
474unsigned AttributeSetNode::getStackAlignment() const {
475 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
476 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000477 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000478 return I->getStackAlignment();
479 return 0;
480}
481
Bill Wendlingb29ce262013-02-11 08:43:33 +0000482std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000483 std::string Str = "";
484 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000485 E = AttrList.end(); I != E; ) {
Bill Wendlingb29ce262013-02-11 08:43:33 +0000486 Str += I->getAsString(InAttrGrp);
Bill Wendling7beee282013-02-01 01:04:27 +0000487 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000488 }
489 return Str;
490}
491
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000492//===----------------------------------------------------------------------===//
493// AttributeSetImpl Definition
494//===----------------------------------------------------------------------===//
495
496uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
497 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
498 if (getSlotIndex(I) != Index) continue;
499 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000500 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000501
502 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000503 IE = ASN->end(); II != IE; ++II) {
504 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000505
506 // This cannot handle string attributes.
507 if (Attr.isStringAttribute()) continue;
508
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000509 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000510
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000511 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000512 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000513 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000514 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
515 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000516 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000517 }
518
519 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000520 }
521
522 return 0;
523}
524
525//===----------------------------------------------------------------------===//
526// AttributeSet Construction and Mutation Methods
527//===----------------------------------------------------------------------===//
528
Bill Wendling8232ece2013-01-29 01:43:29 +0000529AttributeSet
530AttributeSet::getImpl(LLVMContext &C,
531 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000532 LLVMContextImpl *pImpl = C.pImpl;
533 FoldingSetNodeID ID;
534 AttributeSetImpl::Profile(ID, Attrs);
535
536 void *InsertPoint;
537 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
538
539 // If we didn't find any existing attributes of the same shape then
540 // create a new one and insert it.
541 if (!PA) {
542 PA = new AttributeSetImpl(C, Attrs);
543 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
544 }
545
546 // Return the AttributesList that we found or created.
547 return AttributeSet(PA);
548}
549
550AttributeSet AttributeSet::get(LLVMContext &C,
551 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
552 // If there are no attributes then return a null AttributesList pointer.
553 if (Attrs.empty())
554 return AttributeSet();
555
556#ifndef NDEBUG
557 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
558 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
559 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000560 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000561 "Pointless attribute!");
562 }
563#endif
564
565 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
566 // list.
567 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
568 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
569 E = Attrs.end(); I != E; ) {
570 unsigned Index = I->first;
571 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000572 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000573 AttrVec.push_back(I->second);
574 ++I;
575 }
576
577 AttrPairVec.push_back(std::make_pair(Index,
578 AttributeSetNode::get(C, AttrVec)));
579 }
580
581 return getImpl(C, AttrPairVec);
582}
583
584AttributeSet AttributeSet::get(LLVMContext &C,
585 ArrayRef<std::pair<unsigned,
586 AttributeSetNode*> > Attrs) {
587 // If there are no attributes then return a null AttributesList pointer.
588 if (Attrs.empty())
589 return AttributeSet();
590
591 return getImpl(C, Attrs);
592}
593
594AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
595 if (!B.hasAttributes())
596 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000597
Bill Wendling64754f42013-02-05 23:48:36 +0000598 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000599 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
600 for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
601 Attribute::AttrKind Kind = *I;
602 if (Kind == Attribute::Alignment)
603 Attrs.push_back(std::make_pair(Idx, Attribute::
604 getWithAlignment(C, B.getAlignment())));
605 else if (Kind == Attribute::StackAlignment)
606 Attrs.push_back(std::make_pair(Idx, Attribute::
607 getWithStackAlignment(C, B.getStackAlignment())));
608 else
609 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
610 }
611
Bill Wendling64754f42013-02-05 23:48:36 +0000612 // Add target-dependent (string) attributes.
613 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
614 I != E; ++I)
615 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
616
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000617 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000618}
619
620AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
621 ArrayRef<Attribute::AttrKind> Kind) {
622 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
623 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
624 E = Kind.end(); I != E; ++I)
625 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
626 return get(C, Attrs);
627}
628
629AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
630 if (Attrs.empty()) return AttributeSet();
631
632 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
633 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
634 AttributeSet AS = Attrs[I];
635 if (!AS.pImpl) continue;
636 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
637 }
638
639 return getImpl(C, AttrNodeVec);
640}
641
642AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
643 Attribute::AttrKind Attr) const {
644 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
645}
646
647AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
648 AttributeSet Attrs) const {
649 if (!pImpl) return Attrs;
650 if (!Attrs.pImpl) return *this;
651
652#ifndef NDEBUG
653 // FIXME it is not obvious how this should work for alignment. For now, say
654 // we can't change a known alignment.
655 unsigned OldAlign = getParamAlignment(Idx);
656 unsigned NewAlign = Attrs.getParamAlignment(Idx);
657 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
658 "Attempt to change alignment!");
659#endif
660
661 // Add the attribute slots before the one we're trying to add.
662 SmallVector<AttributeSet, 4> AttrSet;
663 uint64_t NumAttrs = pImpl->getNumAttributes();
664 AttributeSet AS;
665 uint64_t LastIndex = 0;
666 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
667 if (getSlotIndex(I) >= Idx) {
668 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
669 break;
670 }
671 LastIndex = I + 1;
672 AttrSet.push_back(getSlotAttributes(I));
673 }
674
675 // Now add the attribute into the correct slot. There may already be an
676 // AttributeSet there.
677 AttrBuilder B(AS, Idx);
678
679 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
680 if (Attrs.getSlotIndex(I) == Idx) {
681 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
682 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000683 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000684 break;
685 }
686
687 AttrSet.push_back(AttributeSet::get(C, Idx, B));
688
689 // Add the remaining attribute slots.
690 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
691 AttrSet.push_back(getSlotAttributes(I));
692
693 return get(C, AttrSet);
694}
695
696AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
697 Attribute::AttrKind Attr) const {
698 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
699}
700
701AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
702 AttributeSet Attrs) const {
703 if (!pImpl) return AttributeSet();
704 if (!Attrs.pImpl) return *this;
705
706#ifndef NDEBUG
707 // FIXME it is not obvious how this should work for alignment.
708 // For now, say we can't pass in alignment, which no current use does.
709 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
710 "Attempt to change alignment!");
711#endif
712
713 // Add the attribute slots before the one we're trying to add.
714 SmallVector<AttributeSet, 4> AttrSet;
715 uint64_t NumAttrs = pImpl->getNumAttributes();
716 AttributeSet AS;
717 uint64_t LastIndex = 0;
718 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
719 if (getSlotIndex(I) >= Idx) {
720 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
721 break;
722 }
723 LastIndex = I + 1;
724 AttrSet.push_back(getSlotAttributes(I));
725 }
726
Bill Wendlinge7436542013-01-30 23:07:40 +0000727 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000728 // AttributeSet there.
729 AttrBuilder B(AS, Idx);
730
731 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
732 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000733 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000734 break;
735 }
736
737 AttrSet.push_back(AttributeSet::get(C, Idx, B));
738
739 // Add the remaining attribute slots.
740 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
741 AttrSet.push_back(getSlotAttributes(I));
742
743 return get(C, AttrSet);
744}
745
746//===----------------------------------------------------------------------===//
747// AttributeSet Accessor Methods
748//===----------------------------------------------------------------------===//
749
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000750LLVMContext &AttributeSet::getContext() const {
751 return pImpl->getContext();
752}
753
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000754AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
755 return pImpl && hasAttributes(Idx) ?
756 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000757 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000758 std::make_pair(Idx, getAttributes(Idx)))) :
759 AttributeSet();
760}
761
762AttributeSet AttributeSet::getRetAttributes() const {
763 return pImpl && hasAttributes(ReturnIndex) ?
764 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000765 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000766 std::make_pair(ReturnIndex,
767 getAttributes(ReturnIndex)))) :
768 AttributeSet();
769}
770
771AttributeSet AttributeSet::getFnAttributes() const {
772 return pImpl && hasAttributes(FunctionIndex) ?
773 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000774 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000775 std::make_pair(FunctionIndex,
776 getAttributes(FunctionIndex)))) :
777 AttributeSet();
778}
779
780bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000781 AttributeSetNode *ASN = getAttributes(Index);
782 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000783}
784
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000785bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
786 AttributeSetNode *ASN = getAttributes(Index);
787 return ASN ? ASN->hasAttribute(Kind) : false;
788}
789
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000790bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000791 AttributeSetNode *ASN = getAttributes(Index);
792 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000793}
794
795/// \brief Return true if the specified attribute is set for at least one
796/// parameter or for the return value.
797bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
798 if (pImpl == 0) return false;
799
800 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
801 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
802 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000803 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000804 return true;
805
806 return false;
807}
808
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000809Attribute AttributeSet::getAttribute(unsigned Index,
810 Attribute::AttrKind Kind) const {
811 AttributeSetNode *ASN = getAttributes(Index);
812 return ASN ? ASN->getAttribute(Kind) : Attribute();
813}
814
815Attribute AttributeSet::getAttribute(unsigned Index,
816 StringRef Kind) const {
817 AttributeSetNode *ASN = getAttributes(Index);
818 return ASN ? ASN->getAttribute(Kind) : Attribute();
819}
820
Bill Wendling606c8e32013-01-29 03:20:31 +0000821unsigned AttributeSet::getParamAlignment(unsigned Index) const {
822 AttributeSetNode *ASN = getAttributes(Index);
823 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000824}
825
826unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000827 AttributeSetNode *ASN = getAttributes(Index);
828 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000829}
830
Bill Wendlingb29ce262013-02-11 08:43:33 +0000831std::string AttributeSet::getAsString(unsigned Index,
832 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000833 AttributeSetNode *ASN = getAttributes(Index);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000834 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000835}
836
837/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000838AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
839 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000840
Bill Wendling606c8e32013-01-29 03:20:31 +0000841 // Loop through to find the attribute node we want.
842 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
843 if (pImpl->getSlotIndex(I) == Idx)
844 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000845
Bill Wendling606c8e32013-01-29 03:20:31 +0000846 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000847}
848
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000849AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000850 if (!pImpl)
851 return ArrayRef<Attribute>().begin();
852 return pImpl->begin(Idx);
853}
854
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000855AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000856 if (!pImpl)
857 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000858 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000859}
860
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000861//===----------------------------------------------------------------------===//
862// AttributeSet Introspection Methods
863//===----------------------------------------------------------------------===//
864
865/// \brief Return the number of slots used in this attribute list. This is the
866/// number of arguments that have an attribute set on them (including the
867/// function itself).
868unsigned AttributeSet::getNumSlots() const {
869 return pImpl ? pImpl->getNumAttributes() : 0;
870}
871
872uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
873 assert(pImpl && Slot < pImpl->getNumAttributes() &&
874 "Slot # out of range!");
875 return pImpl->getSlotIndex(Slot);
876}
877
878AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
879 assert(pImpl && Slot < pImpl->getNumAttributes() &&
880 "Slot # out of range!");
881 return pImpl->getSlotAttributes(Slot);
882}
883
884uint64_t AttributeSet::Raw(unsigned Index) const {
885 // FIXME: Remove this.
886 return pImpl ? pImpl->Raw(Index) : 0;
887}
888
889void AttributeSet::dump() const {
890 dbgs() << "PAL[\n";
891
892 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
893 uint64_t Index = getSlotIndex(i);
894 dbgs() << " { ";
895 if (Index == ~0U)
896 dbgs() << "~0U";
897 else
898 dbgs() << Index;
899 dbgs() << " => " << getAsString(Index) << " }\n";
900 }
901
902 dbgs() << "]\n";
903}
904
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000905//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000906// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000907//===----------------------------------------------------------------------===//
908
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000909AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
910 : Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000911 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000912 if (!pImpl) return;
913
Bill Wendling73bc4522013-01-28 00:21:34 +0000914 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
915 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000916
Bill Wendling383da6b2013-01-30 21:22:59 +0000917 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000918 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000919 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000920
921 break;
922 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000923}
924
Bill Wendling03198882013-01-04 23:27:34 +0000925void AttrBuilder::clear() {
926 Attrs.clear();
927 Alignment = StackAlignment = 0;
928}
929
930AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Bill Wendling169d5272013-01-31 23:16:25 +0000931 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
932 "Adding alignment attribute without adding alignment value!");
Bill Wendling03198882013-01-04 23:27:34 +0000933 Attrs.insert(Val);
Bill Wendling3a106e62012-10-09 19:01:18 +0000934 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000935}
936
Bill Wendling39da0782013-01-31 23:38:01 +0000937AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000938 if (Attr.isStringAttribute()) {
939 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
940 return *this;
941 }
942
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000943 Attribute::AttrKind Kind = Attr.getKindAsEnum();
944 Attrs.insert(Kind);
Bill Wendling49f60602013-01-28 05:23:28 +0000945
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000946 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000947 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000948 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000949 StackAlignment = Attr.getStackAlignment();
950 return *this;
951}
952
Bill Wendlingea59f892013-02-05 08:09:32 +0000953AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
954 TargetDepAttrs[A] = V;
955 return *this;
956}
957
Bill Wendling39da0782013-01-31 23:38:01 +0000958AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
959 Attrs.erase(Val);
960
961 if (Val == Attribute::Alignment)
962 Alignment = 0;
963 else if (Val == Attribute::StackAlignment)
964 StackAlignment = 0;
965
966 return *this;
967}
968
Bill Wendlinge7436542013-01-30 23:07:40 +0000969AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000970 unsigned Idx = ~0U;
971 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
972 if (A.getSlotIndex(I) == Index) {
973 Idx = I;
974 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000975 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000976
977 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
978
979 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +0000980 Attribute Attr = *I;
981 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
982 Attribute::AttrKind Kind = I->getKindAsEnum();
983 Attrs.erase(Kind);
Bill Wendling30d2c762013-02-01 00:13:50 +0000984
Bill Wendling74fe8252013-02-12 07:56:49 +0000985 if (Kind == Attribute::Alignment)
986 Alignment = 0;
987 else if (Kind == Attribute::StackAlignment)
988 StackAlignment = 0;
989 } else {
990 assert(Attr.isStringAttribute() && "Invalid attribute type!");
991 std::map<std::string, std::string>::iterator
992 Iter = TargetDepAttrs.find(Attr.getKindAsString());
993 if (Iter != TargetDepAttrs.end())
994 TargetDepAttrs.erase(Iter);
995 }
Bill Wendling49f60602013-01-28 05:23:28 +0000996 }
997
998 return *this;
999}
1000
Bill Wendlingea59f892013-02-05 08:09:32 +00001001AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1002 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1003 if (I != TargetDepAttrs.end())
1004 TargetDepAttrs.erase(I);
1005 return *this;
1006}
1007
Bill Wendling702cc912012-10-15 20:35:56 +00001008AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001009 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001010
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001011 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1012 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001013
1014 Attrs.insert(Attribute::Alignment);
1015 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001016 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001017}
1018
Bill Wendling03198882013-01-04 23:27:34 +00001019AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1020 // Default alignment, allow the target to define how to align it.
1021 if (Align == 0) return *this;
1022
1023 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1024 assert(Align <= 0x100 && "Alignment too large.");
1025
1026 Attrs.insert(Attribute::StackAlignment);
1027 StackAlignment = Align;
1028 return *this;
1029}
1030
Bill Wendling85df6b42013-02-06 01:16:00 +00001031AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1032 // FIXME: What if both have alignments, but they don't match?!
1033 if (!Alignment)
1034 Alignment = B.Alignment;
1035
1036 if (!StackAlignment)
1037 StackAlignment = B.StackAlignment;
1038
1039 Attrs.insert(B.Attrs.begin(), B.Attrs.end());
1040
1041 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1042 E = B.TargetDepAttrs.end(); I != E; ++I)
1043 TargetDepAttrs[I->first] = I->second;
1044
1045 return *this;
1046}
1047
Bill Wendling22bd6412013-01-03 01:54:39 +00001048bool AttrBuilder::contains(Attribute::AttrKind A) const {
Bill Wendling03198882013-01-04 23:27:34 +00001049 return Attrs.count(A);
Bill Wendling7d2f2492012-10-10 07:36:45 +00001050}
1051
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001052bool AttrBuilder::contains(StringRef A) const {
1053 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1054}
1055
Bill Wendling702cc912012-10-15 20:35:56 +00001056bool AttrBuilder::hasAttributes() const {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001057 return !Attrs.empty() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001058}
Bill Wendling60507d52013-01-04 20:54:35 +00001059
Bill Wendlinge7436542013-01-30 23:07:40 +00001060bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001061 unsigned Idx = ~0U;
1062 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1063 if (A.getSlotIndex(I) == Index) {
1064 Idx = I;
1065 break;
1066 }
1067
1068 assert(Idx != ~0U && "Couldn't find the index!");
1069
1070 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling74fe8252013-02-12 07:56:49 +00001071 I != E; ++I) {
1072 Attribute Attr = *I;
1073 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1074 if (Attrs.count(I->getKindAsEnum()))
1075 return true;
1076 } else {
1077 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1078 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1079 }
1080 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001081
1082 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001083}
Bill Wendling60507d52013-01-04 20:54:35 +00001084
Bill Wendling702cc912012-10-15 20:35:56 +00001085bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001086 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001087}
1088
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001089bool AttrBuilder::operator==(const AttrBuilder &B) {
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001090 for (DenseSet<Attribute::AttrKind>::iterator I = Attrs.begin(),
1091 E = Attrs.end(); I != E; ++I)
1092 if (!B.Attrs.count(*I))
1093 return false;
1094
1095 for (td_const_iterator I = TargetDepAttrs.begin(),
1096 E = TargetDepAttrs.end(); I != E; ++I)
1097 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1098 return false;
1099
1100 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001101}
1102
1103AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001104 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001105 if (!Val) return *this;
1106
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001107 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1108 I = Attribute::AttrKind(I + 1)) {
1109 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1110 Attrs.insert(I);
1111
1112 if (I == Attribute::Alignment)
1113 Alignment = 1ULL << ((A >> 16) - 1);
1114 else if (I == Attribute::StackAlignment)
1115 StackAlignment = 1ULL << ((A >> 26)-1);
1116 }
1117 }
1118
1119 return *this;
1120}
1121
Bill Wendling8e47daf2013-01-25 23:09:36 +00001122//===----------------------------------------------------------------------===//
1123// AttributeFuncs Function Defintions
1124//===----------------------------------------------------------------------===//
1125
Bill Wendling7beee282013-02-01 01:04:27 +00001126/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001127AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001128 AttrBuilder Incompatible;
1129
1130 if (!Ty->isIntegerTy())
1131 // Attribute that only apply to integers.
1132 Incompatible.addAttribute(Attribute::SExt)
1133 .addAttribute(Attribute::ZExt);
1134
1135 if (!Ty->isPointerTy())
1136 // Attribute that only apply to pointers.
1137 Incompatible.addAttribute(Attribute::ByVal)
1138 .addAttribute(Attribute::Nest)
1139 .addAttribute(Attribute::NoAlias)
1140 .addAttribute(Attribute::NoCapture)
1141 .addAttribute(Attribute::StructRet);
1142
Bill Wendlinge7436542013-01-30 23:07:40 +00001143 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001144}