blob: 2d828914cdca4cb17c5f13d67e93a6d8cb57769c [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
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000156 if (hasAttribute(Attribute::SanitizeAddress))
157 return "sanitize_address";
Bill Wendling14292a62013-01-31 20:59:05 +0000158 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 Wendling143d4642013-02-22 00:12:35 +0000174 if (hasAttribute(Attribute::NoBuiltin))
175 return "nobuiltin";
Bill Wendling034b94b2012-12-19 07:18:57 +0000176 if (hasAttribute(Attribute::NoCapture))
Bill Wendling606c8e32013-01-29 03:20:31 +0000177 return "nocapture";
Bill Wendling14292a62013-01-31 20:59:05 +0000178 if (hasAttribute(Attribute::NoDuplicate))
179 return "noduplicate";
180 if (hasAttribute(Attribute::NoImplicitFloat))
181 return "noimplicitfloat";
182 if (hasAttribute(Attribute::NoInline))
183 return "noinline";
184 if (hasAttribute(Attribute::NonLazyBind))
185 return "nonlazybind";
186 if (hasAttribute(Attribute::NoRedZone))
187 return "noredzone";
188 if (hasAttribute(Attribute::NoReturn))
189 return "noreturn";
190 if (hasAttribute(Attribute::NoUnwind))
191 return "nounwind";
192 if (hasAttribute(Attribute::OptimizeForSize))
193 return "optsize";
Bill Wendling034b94b2012-12-19 07:18:57 +0000194 if (hasAttribute(Attribute::ReadNone))
Bill Wendling606c8e32013-01-29 03:20:31 +0000195 return "readnone";
Bill Wendling034b94b2012-12-19 07:18:57 +0000196 if (hasAttribute(Attribute::ReadOnly))
Bill Wendling606c8e32013-01-29 03:20:31 +0000197 return "readonly";
Bill Wendling14292a62013-01-31 20:59:05 +0000198 if (hasAttribute(Attribute::ReturnsTwice))
199 return "returns_twice";
200 if (hasAttribute(Attribute::SExt))
201 return "signext";
Bill Wendling034b94b2012-12-19 07:18:57 +0000202 if (hasAttribute(Attribute::StackProtect))
Bill Wendling606c8e32013-01-29 03:20:31 +0000203 return "ssp";
Bill Wendling034b94b2012-12-19 07:18:57 +0000204 if (hasAttribute(Attribute::StackProtectReq))
Bill Wendling606c8e32013-01-29 03:20:31 +0000205 return "sspreq";
Bill Wendling114baee2013-01-23 06:41:41 +0000206 if (hasAttribute(Attribute::StackProtectStrong))
Bill Wendling606c8e32013-01-29 03:20:31 +0000207 return "sspstrong";
Bill Wendling14292a62013-01-31 20:59:05 +0000208 if (hasAttribute(Attribute::StructRet))
209 return "sret";
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000210 if (hasAttribute(Attribute::SanitizeThread))
211 return "sanitize_thread";
212 if (hasAttribute(Attribute::SanitizeMemory))
213 return "sanitize_memory";
Bill Wendling14292a62013-01-31 20:59:05 +0000214 if (hasAttribute(Attribute::UWTable))
215 return "uwtable";
216 if (hasAttribute(Attribute::ZExt))
217 return "zeroext";
218
219 // FIXME: These should be output like this:
220 //
221 // align=4
222 // alignstack=8
223 //
Bill Wendling034b94b2012-12-19 07:18:57 +0000224 if (hasAttribute(Attribute::Alignment)) {
Bill Wendling606c8e32013-01-29 03:20:31 +0000225 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000226 Result += "align";
227 Result += (InAttrGrp) ? "=" : " ";
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000228 Result += utostr(getValueAsInt());
229 return Result;
230 }
Bill Wendlingb29ce262013-02-11 08:43:33 +0000231
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000232 if (hasAttribute(Attribute::StackAlignment)) {
233 std::string Result;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000234 Result += "alignstack";
235 if (InAttrGrp) {
236 Result += "=";
237 Result += utostr(getValueAsInt());
238 } else {
239 Result += "(";
240 Result += utostr(getValueAsInt());
241 Result += ")";
242 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000243 return Result;
Dale Johannesen6167c3f2008-02-19 23:51:49 +0000244 }
Bill Wendling14292a62013-01-31 20:59:05 +0000245
246 // Convert target-dependent attributes to strings of the form:
247 //
248 // "kind"
249 // "kind" = "value"
Bill Wendling14292a62013-01-31 20:59:05 +0000250 //
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000251 if (isStringAttribute()) {
Bill Wendling14292a62013-01-31 20:59:05 +0000252 std::string Result;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000253 Result += '\"' + getKindAsString().str() + '"';
Bill Wendling14292a62013-01-31 20:59:05 +0000254
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000255 StringRef Val = pImpl->getValueAsString();
256 if (Val.empty()) return Result;
Bill Wendling5a4041e2013-02-01 22:32:30 +0000257
Bill Wendlingb29ce262013-02-11 08:43:33 +0000258 Result += "=\"" + Val.str() + '"';
Bill Wendling7beee282013-02-01 01:04:27 +0000259 return Result;
Bill Wendling14292a62013-01-31 20:59:05 +0000260 }
Bill Wendling606c8e32013-01-29 03:20:31 +0000261
262 llvm_unreachable("Unknown attribute");
Chris Lattner50ee9dd2008-01-02 23:42:30 +0000263}
264
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000265bool Attribute::operator<(Attribute A) const {
266 if (!pImpl && !A.pImpl) return false;
267 if (!pImpl) return true;
268 if (!A.pImpl) return false;
269 return *pImpl < *A.pImpl;
270}
271
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000272//===----------------------------------------------------------------------===//
273// AttributeImpl Definition
274//===----------------------------------------------------------------------===//
275
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000276AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind)
277 : Context(C), Entry(new EnumAttributeEntry(Kind)) {}
278
279AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind,
280 unsigned Align)
281 : Context(C) {
282 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
283 "Wrong kind for alignment attribute!");
284 Entry = new AlignAttributeEntry(Kind, Align);
285}
286
287AttributeImpl::AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val)
288 : Context(C), Entry(new StringAttributeEntry(Kind, Val)) {}
289
290AttributeImpl::~AttributeImpl() {
291 delete Entry;
292}
293
294bool AttributeImpl::isEnumAttribute() const {
295 return isa<EnumAttributeEntry>(Entry);
296}
297
298bool AttributeImpl::isAlignAttribute() const {
299 return isa<AlignAttributeEntry>(Entry);
300}
301
302bool AttributeImpl::isStringAttribute() const {
303 return isa<StringAttributeEntry>(Entry);
304}
305
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000306bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000307 if (isStringAttribute()) return false;
308 return getKindAsEnum() == A;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000309}
310
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000311bool AttributeImpl::hasAttribute(StringRef Kind) const {
312 if (!isStringAttribute()) return false;
313 return getKindAsString() == Kind;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000314}
315
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000316Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
317 if (EnumAttributeEntry *E = dyn_cast<EnumAttributeEntry>(Entry))
318 return E->getEnumKind();
319 return cast<AlignAttributeEntry>(Entry)->getEnumKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000320}
321
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000322uint64_t AttributeImpl::getValueAsInt() const {
323 return cast<AlignAttributeEntry>(Entry)->getAlignment();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000324}
325
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000326StringRef AttributeImpl::getKindAsString() const {
327 return cast<StringAttributeEntry>(Entry)->getStringKind();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000328}
329
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000330StringRef AttributeImpl::getValueAsString() const {
331 return cast<StringAttributeEntry>(Entry)->getStringValue();
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000332}
333
334bool AttributeImpl::operator<(const AttributeImpl &AI) const {
Bill Wendling7beee282013-02-01 01:04:27 +0000335 // This sorts the attributes with Attribute::AttrKinds coming first (sorted
336 // relative to their enum value) and then strings.
Bill Wendling94328f42013-02-15 05:25:26 +0000337 if (isEnumAttribute()) {
338 if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
339 if (AI.isAlignAttribute()) return true;
340 if (AI.isStringAttribute()) return true;
341 }
Bill Wendling7beee282013-02-01 01:04:27 +0000342
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000343 if (isAlignAttribute()) {
Bill Wendling94328f42013-02-15 05:25:26 +0000344 if (AI.isEnumAttribute()) return false;
345 if (AI.isAlignAttribute()) return getValueAsInt() < AI.getValueAsInt();
346 if (AI.isStringAttribute()) return true;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000347 }
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000348
Bill Wendling94328f42013-02-15 05:25:26 +0000349 if (AI.isEnumAttribute()) return false;
350 if (AI.isAlignAttribute()) return false;
351 if (getKindAsString() == AI.getKindAsString())
352 return getValueAsString() < AI.getValueAsString();
353 return getKindAsString() < AI.getKindAsString();
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:
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000360 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;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000389 case Attribute::SanitizeAddress: return 1ULL << 32;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000390 case Attribute::MinSize: return 1ULL << 33;
391 case Attribute::NoDuplicate: return 1ULL << 34;
392 case Attribute::StackProtectStrong: return 1ULL << 35;
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000393 case Attribute::SanitizeThread: return 1ULL << 36;
394 case Attribute::SanitizeMemory: return 1ULL << 37;
Bill Wendlingd18e0b92013-02-22 00:40:12 +0000395 case Attribute::NoBuiltin: return 1ULL << 38;
Bill Wendlingbd2acfa2013-02-22 00:50:09 +0000396 }
397 llvm_unreachable("Unsupported attribute type");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000398}
399
400//===----------------------------------------------------------------------===//
401// AttributeSetNode Definition
402//===----------------------------------------------------------------------===//
403
404AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
405 ArrayRef<Attribute> Attrs) {
406 if (Attrs.empty())
407 return 0;
408
409 // Otherwise, build a key to look up the existing attributes.
410 LLVMContextImpl *pImpl = C.pImpl;
411 FoldingSetNodeID ID;
412
413 SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
Bill Wendlingf107e6c2013-02-13 09:26:26 +0000414 array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000415
416 for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
417 E = SortedAttrs.end(); I != E; ++I)
418 I->Profile(ID);
419
420 void *InsertPoint;
421 AttributeSetNode *PA =
422 pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
423
424 // If we didn't find any existing attributes of the same shape then create a
425 // new one and insert it.
426 if (!PA) {
427 PA = new AttributeSetNode(SortedAttrs);
428 pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
429 }
430
431 // Return the AttributesListNode that we found or created.
432 return PA;
433}
434
Bill Wendling606c8e32013-01-29 03:20:31 +0000435bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
436 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
437 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000438 if (I->hasAttribute(Kind))
Bill Wendling606c8e32013-01-29 03:20:31 +0000439 return true;
440 return false;
441}
442
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000443bool AttributeSetNode::hasAttribute(StringRef Kind) const {
444 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
445 E = AttrList.end(); I != E; ++I)
446 if (I->hasAttribute(Kind))
447 return true;
448 return false;
449}
450
451Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
452 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
453 E = AttrList.end(); I != E; ++I)
454 if (I->hasAttribute(Kind))
455 return *I;
456 return Attribute();
457}
458
459Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
460 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
461 E = AttrList.end(); I != E; ++I)
462 if (I->hasAttribute(Kind))
463 return *I;
464 return Attribute();
465}
466
Bill Wendling606c8e32013-01-29 03:20:31 +0000467unsigned AttributeSetNode::getAlignment() const {
468 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
469 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000470 if (I->hasAttribute(Attribute::Alignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000471 return I->getAlignment();
472 return 0;
473}
474
475unsigned AttributeSetNode::getStackAlignment() const {
476 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
477 E = AttrList.end(); I != E; ++I)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000478 if (I->hasAttribute(Attribute::StackAlignment))
Bill Wendling606c8e32013-01-29 03:20:31 +0000479 return I->getStackAlignment();
480 return 0;
481}
482
Bill Wendlingb29ce262013-02-11 08:43:33 +0000483std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000484 std::string Str = "";
485 for (SmallVectorImpl<Attribute>::const_iterator I = AttrList.begin(),
Bill Wendling7beee282013-02-01 01:04:27 +0000486 E = AttrList.end(); I != E; ) {
Bill Wendlingb29ce262013-02-11 08:43:33 +0000487 Str += I->getAsString(InAttrGrp);
Bill Wendling7beee282013-02-01 01:04:27 +0000488 if (++I != E) Str += " ";
Bill Wendling606c8e32013-01-29 03:20:31 +0000489 }
490 return Str;
491}
492
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000493//===----------------------------------------------------------------------===//
494// AttributeSetImpl Definition
495//===----------------------------------------------------------------------===//
496
497uint64_t AttributeSetImpl::Raw(uint64_t Index) const {
498 for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
499 if (getSlotIndex(I) != Index) continue;
500 const AttributeSetNode *ASN = AttrNodes[I].second;
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000501 uint64_t Mask = 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000502
503 for (AttributeSetNode::const_iterator II = ASN->begin(),
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000504 IE = ASN->end(); II != IE; ++II) {
505 Attribute Attr = *II;
Bill Wendling21536912013-02-10 23:18:05 +0000506
507 // This cannot handle string attributes.
508 if (Attr.isStringAttribute()) continue;
509
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000510 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000511
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000512 if (Kind == Attribute::Alignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000513 Mask |= (Log2_32(ASN->getAlignment()) + 1) << 16;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000514 else if (Kind == Attribute::StackAlignment)
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000515 Mask |= (Log2_32(ASN->getStackAlignment()) + 1) << 26;
516 else
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000517 Mask |= AttributeImpl::getAttrMask(Kind);
Bill Wendlingfca0ed22013-02-02 00:52:44 +0000518 }
519
520 return Mask;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000521 }
522
523 return 0;
524}
525
526//===----------------------------------------------------------------------===//
527// AttributeSet Construction and Mutation Methods
528//===----------------------------------------------------------------------===//
529
Bill Wendling8232ece2013-01-29 01:43:29 +0000530AttributeSet
531AttributeSet::getImpl(LLVMContext &C,
532 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000533 LLVMContextImpl *pImpl = C.pImpl;
534 FoldingSetNodeID ID;
535 AttributeSetImpl::Profile(ID, Attrs);
536
537 void *InsertPoint;
538 AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
539
540 // If we didn't find any existing attributes of the same shape then
541 // create a new one and insert it.
542 if (!PA) {
543 PA = new AttributeSetImpl(C, Attrs);
544 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
545 }
546
547 // Return the AttributesList that we found or created.
548 return AttributeSet(PA);
549}
550
551AttributeSet AttributeSet::get(LLVMContext &C,
552 ArrayRef<std::pair<unsigned, Attribute> > Attrs){
553 // If there are no attributes then return a null AttributesList pointer.
554 if (Attrs.empty())
555 return AttributeSet();
556
557#ifndef NDEBUG
558 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
559 assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
560 "Misordered Attributes list!");
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000561 assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000562 "Pointless attribute!");
563 }
564#endif
565
566 // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
567 // list.
568 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
569 for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
570 E = Attrs.end(); I != E; ) {
571 unsigned Index = I->first;
572 SmallVector<Attribute, 4> AttrVec;
NAKAMURA Takumi3ba51ce2013-01-29 15:18:16 +0000573 while (I != E && I->first == Index) {
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000574 AttrVec.push_back(I->second);
575 ++I;
576 }
577
578 AttrPairVec.push_back(std::make_pair(Index,
579 AttributeSetNode::get(C, AttrVec)));
580 }
581
582 return getImpl(C, AttrPairVec);
583}
584
585AttributeSet AttributeSet::get(LLVMContext &C,
586 ArrayRef<std::pair<unsigned,
587 AttributeSetNode*> > Attrs) {
588 // If there are no attributes then return a null AttributesList pointer.
589 if (Attrs.empty())
590 return AttributeSet();
591
592 return getImpl(C, Attrs);
593}
594
595AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
596 if (!B.hasAttributes())
597 return AttributeSet();
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000598
Bill Wendling64754f42013-02-05 23:48:36 +0000599 // Add target-independent attributes.
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000600 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000601 for (Attribute::AttrKind Kind = Attribute::None;
Benjamin Kramer7c146122013-02-16 19:22:28 +0000602 Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000603 if (!B.contains(Kind))
604 continue;
605
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000606 if (Kind == Attribute::Alignment)
607 Attrs.push_back(std::make_pair(Idx, Attribute::
608 getWithAlignment(C, B.getAlignment())));
609 else if (Kind == Attribute::StackAlignment)
610 Attrs.push_back(std::make_pair(Idx, Attribute::
611 getWithStackAlignment(C, B.getStackAlignment())));
612 else
613 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, Kind)));
614 }
615
Bill Wendling64754f42013-02-05 23:48:36 +0000616 // Add target-dependent (string) attributes.
617 for (AttrBuilder::td_iterator I = B.td_begin(), E = B.td_end();
618 I != E; ++I)
619 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, I->first,I->second)));
620
Bill Wendling8fbc0c22013-01-29 01:02:03 +0000621 return get(C, Attrs);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000622}
623
624AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx,
625 ArrayRef<Attribute::AttrKind> Kind) {
626 SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
627 for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
628 E = Kind.end(); I != E; ++I)
629 Attrs.push_back(std::make_pair(Idx, Attribute::get(C, *I)));
630 return get(C, Attrs);
631}
632
633AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
634 if (Attrs.empty()) return AttributeSet();
635
636 SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
637 for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
638 AttributeSet AS = Attrs[I];
639 if (!AS.pImpl) continue;
640 AttrNodeVec.append(AS.pImpl->AttrNodes.begin(), AS.pImpl->AttrNodes.end());
641 }
642
643 return getImpl(C, AttrNodeVec);
644}
645
646AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
647 Attribute::AttrKind Attr) const {
Bill Wendling1abc00b2013-02-28 19:17:40 +0000648 if (hasAttribute(Idx, Attr)) return *this;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000649 return addAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
650}
651
Reed Kotler9106f732013-03-13 20:20:08 +0000652AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Idx,
653 StringRef Kind) const {
654 llvm::AttrBuilder B;
655 B.addAttribute(Kind);
656 return addAttributes(C, Idx, AttributeSet::get(C, Idx, B));
657}
658
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000659AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Idx,
660 AttributeSet Attrs) const {
661 if (!pImpl) return Attrs;
662 if (!Attrs.pImpl) return *this;
663
664#ifndef NDEBUG
665 // FIXME it is not obvious how this should work for alignment. For now, say
666 // we can't change a known alignment.
667 unsigned OldAlign = getParamAlignment(Idx);
668 unsigned NewAlign = Attrs.getParamAlignment(Idx);
669 assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
670 "Attempt to change alignment!");
671#endif
672
673 // Add the attribute slots before the one we're trying to add.
674 SmallVector<AttributeSet, 4> AttrSet;
675 uint64_t NumAttrs = pImpl->getNumAttributes();
676 AttributeSet AS;
677 uint64_t LastIndex = 0;
678 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
679 if (getSlotIndex(I) >= Idx) {
680 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
681 break;
682 }
683 LastIndex = I + 1;
684 AttrSet.push_back(getSlotAttributes(I));
685 }
686
687 // Now add the attribute into the correct slot. There may already be an
688 // AttributeSet there.
689 AttrBuilder B(AS, Idx);
690
691 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
692 if (Attrs.getSlotIndex(I) == Idx) {
693 for (AttributeSetImpl::const_iterator II = Attrs.pImpl->begin(I),
694 IE = Attrs.pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000695 B.addAttribute(*II);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000696 break;
697 }
698
699 AttrSet.push_back(AttributeSet::get(C, Idx, B));
700
701 // Add the remaining attribute slots.
702 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
703 AttrSet.push_back(getSlotAttributes(I));
704
705 return get(C, AttrSet);
706}
707
708AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Idx,
709 Attribute::AttrKind Attr) const {
Bill Wendling1abc00b2013-02-28 19:17:40 +0000710 if (!hasAttribute(Idx, Attr)) return *this;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000711 return removeAttributes(C, Idx, AttributeSet::get(C, Idx, Attr));
712}
713
714AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Idx,
715 AttributeSet Attrs) const {
716 if (!pImpl) return AttributeSet();
717 if (!Attrs.pImpl) return *this;
718
719#ifndef NDEBUG
720 // FIXME it is not obvious how this should work for alignment.
721 // For now, say we can't pass in alignment, which no current use does.
722 assert(!Attrs.hasAttribute(Idx, Attribute::Alignment) &&
723 "Attempt to change alignment!");
724#endif
725
726 // Add the attribute slots before the one we're trying to add.
727 SmallVector<AttributeSet, 4> AttrSet;
728 uint64_t NumAttrs = pImpl->getNumAttributes();
729 AttributeSet AS;
730 uint64_t LastIndex = 0;
731 for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
732 if (getSlotIndex(I) >= Idx) {
733 if (getSlotIndex(I) == Idx) AS = getSlotAttributes(LastIndex++);
734 break;
735 }
736 LastIndex = I + 1;
737 AttrSet.push_back(getSlotAttributes(I));
738 }
739
Bill Wendlinge7436542013-01-30 23:07:40 +0000740 // Now remove the attribute from the correct slot. There may already be an
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000741 // AttributeSet there.
742 AttrBuilder B(AS, Idx);
743
744 for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
745 if (Attrs.getSlotIndex(I) == Idx) {
Bill Wendlinge7436542013-01-30 23:07:40 +0000746 B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Idx);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000747 break;
748 }
749
750 AttrSet.push_back(AttributeSet::get(C, Idx, B));
751
752 // Add the remaining attribute slots.
753 for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
754 AttrSet.push_back(getSlotAttributes(I));
755
756 return get(C, AttrSet);
757}
758
759//===----------------------------------------------------------------------===//
760// AttributeSet Accessor Methods
761//===----------------------------------------------------------------------===//
762
Bill Wendling85b3fbe2013-02-10 05:00:40 +0000763LLVMContext &AttributeSet::getContext() const {
764 return pImpl->getContext();
765}
766
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000767AttributeSet AttributeSet::getParamAttributes(unsigned Idx) const {
768 return pImpl && hasAttributes(Idx) ?
769 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000770 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000771 std::make_pair(Idx, getAttributes(Idx)))) :
772 AttributeSet();
773}
774
775AttributeSet AttributeSet::getRetAttributes() const {
776 return pImpl && hasAttributes(ReturnIndex) ?
777 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000778 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000779 std::make_pair(ReturnIndex,
780 getAttributes(ReturnIndex)))) :
781 AttributeSet();
782}
783
784AttributeSet AttributeSet::getFnAttributes() const {
785 return pImpl && hasAttributes(FunctionIndex) ?
786 AttributeSet::get(pImpl->getContext(),
Bill Wendling606c8e32013-01-29 03:20:31 +0000787 ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000788 std::make_pair(FunctionIndex,
789 getAttributes(FunctionIndex)))) :
790 AttributeSet();
791}
792
793bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
Bill Wendling606c8e32013-01-29 03:20:31 +0000794 AttributeSetNode *ASN = getAttributes(Index);
795 return ASN ? ASN->hasAttribute(Kind) : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000796}
797
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000798bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
799 AttributeSetNode *ASN = getAttributes(Index);
800 return ASN ? ASN->hasAttribute(Kind) : false;
801}
802
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000803bool AttributeSet::hasAttributes(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000804 AttributeSetNode *ASN = getAttributes(Index);
805 return ASN ? ASN->hasAttributes() : false;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000806}
807
808/// \brief Return true if the specified attribute is set for at least one
809/// parameter or for the return value.
810bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
811 if (pImpl == 0) return false;
812
813 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
814 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
815 IE = pImpl->end(I); II != IE; ++II)
NAKAMURA Takumieddab152013-01-31 03:47:28 +0000816 if (II->hasAttribute(Attr))
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000817 return true;
818
819 return false;
820}
821
Bill Wendling0e9d5d02013-02-13 08:42:21 +0000822Attribute AttributeSet::getAttribute(unsigned Index,
823 Attribute::AttrKind Kind) const {
824 AttributeSetNode *ASN = getAttributes(Index);
825 return ASN ? ASN->getAttribute(Kind) : Attribute();
826}
827
828Attribute AttributeSet::getAttribute(unsigned Index,
829 StringRef Kind) const {
830 AttributeSetNode *ASN = getAttributes(Index);
831 return ASN ? ASN->getAttribute(Kind) : Attribute();
832}
833
Bill Wendling606c8e32013-01-29 03:20:31 +0000834unsigned AttributeSet::getParamAlignment(unsigned Index) const {
835 AttributeSetNode *ASN = getAttributes(Index);
836 return ASN ? ASN->getAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000837}
838
839unsigned AttributeSet::getStackAlignment(unsigned Index) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000840 AttributeSetNode *ASN = getAttributes(Index);
841 return ASN ? ASN->getStackAlignment() : 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000842}
843
Bill Wendlingb29ce262013-02-11 08:43:33 +0000844std::string AttributeSet::getAsString(unsigned Index,
845 bool InAttrGrp) const {
Bill Wendling606c8e32013-01-29 03:20:31 +0000846 AttributeSetNode *ASN = getAttributes(Index);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000847 return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000848}
849
850/// \brief The attributes for the specified index are returned.
Bill Wendling606c8e32013-01-29 03:20:31 +0000851AttributeSetNode *AttributeSet::getAttributes(unsigned Idx) const {
852 if (!pImpl) return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000853
Bill Wendling606c8e32013-01-29 03:20:31 +0000854 // Loop through to find the attribute node we want.
855 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
856 if (pImpl->getSlotIndex(I) == Idx)
857 return pImpl->getSlotNode(I);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000858
Bill Wendling606c8e32013-01-29 03:20:31 +0000859 return 0;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000860}
861
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000862AttributeSet::iterator AttributeSet::begin(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000863 if (!pImpl)
864 return ArrayRef<Attribute>().begin();
865 return pImpl->begin(Idx);
866}
867
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000868AttributeSet::iterator AttributeSet::end(unsigned Idx) const {
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000869 if (!pImpl)
870 return ArrayRef<Attribute>().end();
Bill Wendling30d2c762013-02-01 00:13:50 +0000871 return pImpl->end(Idx);
Bill Wendling16c4b3c2013-01-31 23:53:05 +0000872}
873
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000874//===----------------------------------------------------------------------===//
875// AttributeSet Introspection Methods
876//===----------------------------------------------------------------------===//
877
878/// \brief Return the number of slots used in this attribute list. This is the
879/// number of arguments that have an attribute set on them (including the
880/// function itself).
881unsigned AttributeSet::getNumSlots() const {
882 return pImpl ? pImpl->getNumAttributes() : 0;
883}
884
885uint64_t AttributeSet::getSlotIndex(unsigned Slot) const {
886 assert(pImpl && Slot < pImpl->getNumAttributes() &&
887 "Slot # out of range!");
888 return pImpl->getSlotIndex(Slot);
889}
890
891AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
892 assert(pImpl && Slot < pImpl->getNumAttributes() &&
893 "Slot # out of range!");
894 return pImpl->getSlotAttributes(Slot);
895}
896
897uint64_t AttributeSet::Raw(unsigned Index) const {
898 // FIXME: Remove this.
899 return pImpl ? pImpl->Raw(Index) : 0;
900}
901
902void AttributeSet::dump() const {
903 dbgs() << "PAL[\n";
904
905 for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
906 uint64_t Index = getSlotIndex(i);
907 dbgs() << " { ";
908 if (Index == ~0U)
909 dbgs() << "~0U";
910 else
911 dbgs() << Index;
912 dbgs() << " => " << getAsString(Index) << " }\n";
913 }
914
915 dbgs() << "]\n";
916}
917
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000918//===----------------------------------------------------------------------===//
Bill Wendling03198882013-01-04 23:27:34 +0000919// AttrBuilder Method Implementations
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000920//===----------------------------------------------------------------------===//
921
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000922AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
Benjamin Kramerc835b8c2013-02-16 19:13:18 +0000923 : Attrs(0), Alignment(0), StackAlignment(0) {
Bill Wendlingec258982013-01-27 21:23:46 +0000924 AttributeSetImpl *pImpl = AS.pImpl;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000925 if (!pImpl) return;
926
Bill Wendling73bc4522013-01-28 00:21:34 +0000927 for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
928 if (pImpl->getSlotIndex(I) != Idx) continue;
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000929
Bill Wendling383da6b2013-01-30 21:22:59 +0000930 for (AttributeSetImpl::const_iterator II = pImpl->begin(I),
Bill Wendling73bc4522013-01-28 00:21:34 +0000931 IE = pImpl->end(I); II != IE; ++II)
Bill Wendling39da0782013-01-31 23:38:01 +0000932 addAttribute(*II);
Bill Wendling73bc4522013-01-28 00:21:34 +0000933
934 break;
935 }
Bill Wendlinga90a99a2013-01-07 08:24:35 +0000936}
937
Bill Wendling03198882013-01-04 23:27:34 +0000938void AttrBuilder::clear() {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000939 Attrs.reset();
Bill Wendling03198882013-01-04 23:27:34 +0000940 Alignment = StackAlignment = 0;
941}
942
943AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000944 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
Bill Wendling169d5272013-01-31 23:16:25 +0000945 assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
946 "Adding alignment attribute without adding alignment value!");
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000947 Attrs[Val] = true;
Bill Wendling3a106e62012-10-09 19:01:18 +0000948 return *this;
Bill Wendlinge66f3d32012-10-05 06:44:41 +0000949}
950
Bill Wendling39da0782013-01-31 23:38:01 +0000951AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
Bill Wendling09ed9102013-02-10 10:13:23 +0000952 if (Attr.isStringAttribute()) {
953 addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
954 return *this;
955 }
956
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000957 Attribute::AttrKind Kind = Attr.getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000958 Attrs[Kind] = true;
Bill Wendling49f60602013-01-28 05:23:28 +0000959
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000960 if (Kind == Attribute::Alignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000961 Alignment = Attr.getAlignment();
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000962 else if (Kind == Attribute::StackAlignment)
Bill Wendling49f60602013-01-28 05:23:28 +0000963 StackAlignment = Attr.getStackAlignment();
964 return *this;
965}
966
Bill Wendlingea59f892013-02-05 08:09:32 +0000967AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
968 TargetDepAttrs[A] = V;
969 return *this;
970}
971
Bill Wendling39da0782013-01-31 23:38:01 +0000972AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000973 assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
974 Attrs[Val] = false;
Bill Wendling39da0782013-01-31 23:38:01 +0000975
976 if (Val == Attribute::Alignment)
977 Alignment = 0;
978 else if (Val == Attribute::StackAlignment)
979 StackAlignment = 0;
980
981 return *this;
982}
983
Bill Wendlinge7436542013-01-30 23:07:40 +0000984AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
Bill Wendling30d2c762013-02-01 00:13:50 +0000985 unsigned Idx = ~0U;
986 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
987 if (A.getSlotIndex(I) == Index) {
988 Idx = I;
989 break;
Bill Wendling49f60602013-01-28 05:23:28 +0000990 }
Bill Wendling30d2c762013-02-01 00:13:50 +0000991
992 assert(Idx != ~0U && "Couldn't find index in AttributeSet!");
993
994 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx); I != E; ++I) {
Bill Wendling74fe8252013-02-12 07:56:49 +0000995 Attribute Attr = *I;
996 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
997 Attribute::AttrKind Kind = I->getKindAsEnum();
Benjamin Kramer3f213e72013-02-18 12:09:51 +0000998 Attrs[Kind] = false;
Bill Wendling30d2c762013-02-01 00:13:50 +0000999
Bill Wendling74fe8252013-02-12 07:56:49 +00001000 if (Kind == Attribute::Alignment)
1001 Alignment = 0;
1002 else if (Kind == Attribute::StackAlignment)
1003 StackAlignment = 0;
1004 } else {
1005 assert(Attr.isStringAttribute() && "Invalid attribute type!");
1006 std::map<std::string, std::string>::iterator
1007 Iter = TargetDepAttrs.find(Attr.getKindAsString());
1008 if (Iter != TargetDepAttrs.end())
1009 TargetDepAttrs.erase(Iter);
1010 }
Bill Wendling49f60602013-01-28 05:23:28 +00001011 }
1012
1013 return *this;
1014}
1015
Bill Wendlingea59f892013-02-05 08:09:32 +00001016AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
1017 std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
1018 if (I != TargetDepAttrs.end())
1019 TargetDepAttrs.erase(I);
1020 return *this;
1021}
1022
Bill Wendling702cc912012-10-15 20:35:56 +00001023AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001024 if (Align == 0) return *this;
Bill Wendling03198882013-01-04 23:27:34 +00001025
Bill Wendlinge66f3d32012-10-05 06:44:41 +00001026 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1027 assert(Align <= 0x40000000 && "Alignment too large.");
Bill Wendling03198882013-01-04 23:27:34 +00001028
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001029 Attrs[Attribute::Alignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001030 Alignment = Align;
Bill Wendlingda3f9d82012-10-14 03:58:29 +00001031 return *this;
Chris Lattner50ee9dd2008-01-02 23:42:30 +00001032}
1033
Bill Wendling03198882013-01-04 23:27:34 +00001034AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
1035 // Default alignment, allow the target to define how to align it.
1036 if (Align == 0) return *this;
1037
1038 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
1039 assert(Align <= 0x100 && "Alignment too large.");
1040
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001041 Attrs[Attribute::StackAlignment] = true;
Bill Wendling03198882013-01-04 23:27:34 +00001042 StackAlignment = Align;
1043 return *this;
1044}
1045
Bill Wendling85df6b42013-02-06 01:16:00 +00001046AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
1047 // FIXME: What if both have alignments, but they don't match?!
1048 if (!Alignment)
1049 Alignment = B.Alignment;
1050
1051 if (!StackAlignment)
1052 StackAlignment = B.StackAlignment;
1053
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001054 Attrs |= B.Attrs;
Bill Wendling85df6b42013-02-06 01:16:00 +00001055
1056 for (td_const_iterator I = B.TargetDepAttrs.begin(),
1057 E = B.TargetDepAttrs.end(); I != E; ++I)
1058 TargetDepAttrs[I->first] = I->second;
1059
1060 return *this;
1061}
1062
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001063bool AttrBuilder::contains(StringRef A) const {
1064 return TargetDepAttrs.find(A) != TargetDepAttrs.end();
1065}
1066
Bill Wendling702cc912012-10-15 20:35:56 +00001067bool AttrBuilder::hasAttributes() const {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001068 return !Attrs.none() || !TargetDepAttrs.empty();
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001069}
Bill Wendling60507d52013-01-04 20:54:35 +00001070
Bill Wendlinge7436542013-01-30 23:07:40 +00001071bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001072 unsigned Idx = ~0U;
1073 for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
1074 if (A.getSlotIndex(I) == Index) {
1075 Idx = I;
1076 break;
1077 }
1078
1079 assert(Idx != ~0U && "Couldn't find the index!");
1080
1081 for (AttributeSet::iterator I = A.begin(Idx), E = A.end(Idx);
Bill Wendling74fe8252013-02-12 07:56:49 +00001082 I != E; ++I) {
1083 Attribute Attr = *I;
1084 if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001085 if (Attrs[I->getKindAsEnum()])
Bill Wendling74fe8252013-02-12 07:56:49 +00001086 return true;
1087 } else {
1088 assert(Attr.isStringAttribute() && "Invalid attribute kind!");
1089 return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
1090 }
1091 }
Bill Wendlingbdcbccc2013-02-02 00:42:06 +00001092
1093 return false;
Bill Wendling8831c062012-10-09 00:01:21 +00001094}
Bill Wendling60507d52013-01-04 20:54:35 +00001095
Bill Wendling702cc912012-10-15 20:35:56 +00001096bool AttrBuilder::hasAlignmentAttr() const {
Bill Wendling03198882013-01-04 23:27:34 +00001097 return Alignment != 0;
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001098}
1099
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001100bool AttrBuilder::operator==(const AttrBuilder &B) {
Benjamin Kramerc835b8c2013-02-16 19:13:18 +00001101 if (Attrs != B.Attrs)
1102 return false;
Bill Wendlingc342d9d2013-02-06 01:33:42 +00001103
1104 for (td_const_iterator I = TargetDepAttrs.begin(),
1105 E = TargetDepAttrs.end(); I != E; ++I)
1106 if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
1107 return false;
1108
1109 return Alignment == B.Alignment && StackAlignment == B.StackAlignment;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001110}
1111
Bill Wendling143d4642013-02-22 00:12:35 +00001112void AttrBuilder::removeFunctionOnlyAttrs() {
1113 removeAttribute(Attribute::NoReturn)
1114 .removeAttribute(Attribute::NoUnwind)
1115 .removeAttribute(Attribute::ReadNone)
1116 .removeAttribute(Attribute::ReadOnly)
1117 .removeAttribute(Attribute::NoInline)
1118 .removeAttribute(Attribute::AlwaysInline)
1119 .removeAttribute(Attribute::OptimizeForSize)
1120 .removeAttribute(Attribute::StackProtect)
1121 .removeAttribute(Attribute::StackProtectReq)
1122 .removeAttribute(Attribute::StackProtectStrong)
1123 .removeAttribute(Attribute::NoRedZone)
1124 .removeAttribute(Attribute::NoImplicitFloat)
1125 .removeAttribute(Attribute::Naked)
1126 .removeAttribute(Attribute::InlineHint)
1127 .removeAttribute(Attribute::StackAlignment)
1128 .removeAttribute(Attribute::UWTable)
1129 .removeAttribute(Attribute::NonLazyBind)
1130 .removeAttribute(Attribute::ReturnsTwice)
Kostya Serebryany8eec41f2013-02-26 06:58:09 +00001131 .removeAttribute(Attribute::SanitizeAddress)
1132 .removeAttribute(Attribute::SanitizeThread)
1133 .removeAttribute(Attribute::SanitizeMemory)
Bill Wendling143d4642013-02-22 00:12:35 +00001134 .removeAttribute(Attribute::MinSize)
1135 .removeAttribute(Attribute::NoDuplicate)
1136 .removeAttribute(Attribute::NoBuiltin);
1137}
1138
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001139AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
Bill Wendlingf9271ea2013-02-04 23:32:23 +00001140 // FIXME: Remove this in 4.0.
Bill Wendling8232ece2013-01-29 01:43:29 +00001141 if (!Val) return *this;
1142
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001143 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1144 I = Attribute::AttrKind(I + 1)) {
1145 if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
Benjamin Kramer3f213e72013-02-18 12:09:51 +00001146 Attrs[I] = true;
Bill Wendlingc22f4aa2013-01-29 00:34:06 +00001147
1148 if (I == Attribute::Alignment)
1149 Alignment = 1ULL << ((A >> 16) - 1);
1150 else if (I == Attribute::StackAlignment)
1151 StackAlignment = 1ULL << ((A >> 26)-1);
1152 }
1153 }
1154
1155 return *this;
1156}
1157
Bill Wendling8e47daf2013-01-25 23:09:36 +00001158//===----------------------------------------------------------------------===//
1159// AttributeFuncs Function Defintions
1160//===----------------------------------------------------------------------===//
1161
Bill Wendling7beee282013-02-01 01:04:27 +00001162/// \brief Which attributes cannot be applied to a type.
Bill Wendlinge7436542013-01-30 23:07:40 +00001163AttributeSet AttributeFuncs::typeIncompatible(Type *Ty, uint64_t Index) {
Bill Wendling8e47daf2013-01-25 23:09:36 +00001164 AttrBuilder Incompatible;
1165
1166 if (!Ty->isIntegerTy())
1167 // Attribute that only apply to integers.
1168 Incompatible.addAttribute(Attribute::SExt)
1169 .addAttribute(Attribute::ZExt);
1170
1171 if (!Ty->isPointerTy())
1172 // Attribute that only apply to pointers.
1173 Incompatible.addAttribute(Attribute::ByVal)
1174 .addAttribute(Attribute::Nest)
1175 .addAttribute(Attribute::NoAlias)
1176 .addAttribute(Attribute::NoCapture)
1177 .addAttribute(Attribute::StructRet);
1178
Bill Wendlinge7436542013-01-30 23:07:40 +00001179 return AttributeSet::get(Ty->getContext(), Index, Incompatible);
Bill Wendling8e47daf2013-01-25 23:09:36 +00001180}