1 //===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the simple types necessary to represent the
12 /// attributes associated with functions and their calls.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_ATTRIBUTES_H
17 #define LLVM_IR_ATTRIBUTES_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/FoldingSet.h"
30 class AttributeSetImpl;
31 class AttributeSetNode;
36 //===----------------------------------------------------------------------===//
38 /// \brief Functions, function parameters, and return types can have attributes
39 /// to indicate how they should be treated by optimizations and code
40 /// generation. This class represents one of those attributes. It's light-weight
41 /// and should be passed around by-value.
44 /// This enumeration lists the attributes that can be associated with
45 /// parameters, function results, or the function itself.
47 /// Note: The `uwtable' attribute is about the ABI or the user mandating an
48 /// entry in the unwind table. The `nounwind' attribute is about an exception
49 /// passing by the function.
51 /// In a theoretical system that uses tables for profiling and SjLj for
52 /// exceptions, they would be fully independent. In a normal system that uses
53 /// tables for both, the semantics are:
55 /// nil = Needs an entry because an exception might pass by.
56 /// nounwind = No need for an entry
57 /// uwtable = Needs an entry because the ABI says so and because
58 /// an exception might pass by.
59 /// uwtable + nounwind = Needs an entry because the ABI says so.
62 // IR-Level Attributes
63 None, ///< No attributes have been set
64 AddressSafety, ///< Address safety checking is on.
65 Alignment, ///< Alignment of parameter (5 bits)
66 ///< stored as log2 of alignment with +1 bias
67 ///< 0 means unaligned (different from align(1))
68 AlwaysInline, ///< inline=always
69 ByVal, ///< Pass structure by value
70 InlineHint, ///< Source said inlining was desirable
71 InReg, ///< Force argument to be passed in register
72 MinSize, ///< Function must be optimized for size first
73 Naked, ///< Naked function
74 Nest, ///< Nested function static chain
75 NoAlias, ///< Considered to not alias after call
76 NoCapture, ///< Function creates no aliases of pointer
77 NoDuplicate, ///< Call cannot be duplicated
78 NoImplicitFloat, ///< Disable implicit floating point insts
79 NoInline, ///< inline=never
80 NonLazyBind, ///< Function is called early and/or
81 ///< often, so lazy binding isn't worthwhile
82 NoRedZone, ///< Disable redzone
83 NoReturn, ///< Mark the function as not returning
84 NoUnwind, ///< Function doesn't unwind stack
85 OptimizeForSize, ///< opt_size
86 ReadNone, ///< Function does not access memory
87 ReadOnly, ///< Function only reads from memory
88 ReturnsTwice, ///< Function can return twice
89 SExt, ///< Sign extended before/after call
90 StackAlignment, ///< Alignment of stack for function (3 bits)
91 ///< stored as log2 of alignment with +1 bias 0
92 ///< means unaligned (different from
94 StackProtect, ///< Stack protection.
95 StackProtectReq, ///< Stack protection required.
96 StackProtectStrong, ///< Strong Stack protection.
97 StructRet, ///< Hidden pointer to structure to return
98 UWTable, ///< Function must be in a unwind table
99 ZExt, ///< Zero extended before/after call
101 EndAttrKinds, ///< Sentinal value useful for loops
103 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
104 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
107 AttributeImpl *pImpl;
108 Attribute(AttributeImpl *A) : pImpl(A) {}
110 Attribute() : pImpl(0) {}
112 //===--------------------------------------------------------------------===//
113 // Attribute Construction
114 //===--------------------------------------------------------------------===//
116 /// \brief Return a uniquified Attribute object.
117 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
118 static Attribute get(LLVMContext &Context, StringRef Kind,
119 StringRef Val = StringRef());
121 /// \brief Return a uniquified Attribute object that has the specific
123 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
124 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
126 //===--------------------------------------------------------------------===//
127 // Attribute Accessors
128 //===--------------------------------------------------------------------===//
130 /// \brief Return true if the attribute is an Attribute::AttrKind type.
131 bool isEnumAttribute() const;
133 /// \brief Return true if the attribute is an alignment attribute.
134 bool isAlignAttribute() const;
136 /// \brief Return true if the attribute is a string (target-dependent)
138 bool isStringAttribute() const;
140 /// \brief Return true if the attribute is present.
141 bool hasAttribute(AttrKind Val) const;
143 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
144 /// requires the attribute to be an enum or alignment attribute.
145 Attribute::AttrKind getKindAsEnum() const;
147 /// \brief Return the attribute's value as an integer. This requires that the
148 /// attribute be an alignment attribute.
149 uint64_t getValueAsInt() const;
151 /// \brief Return the attribute's kind as a string. This requires the
152 /// attribute to be a string attribute.
153 StringRef getKindAsString() const;
155 /// \brief Return the attribute's value as a string. This requires the
156 /// attribute to be a string attribute.
157 StringRef getValueAsString() const;
159 /// \brief Returns the alignment field of an attribute as a byte alignment
161 unsigned getAlignment() const;
163 /// \brief Returns the stack alignment field of an attribute as a byte
165 unsigned getStackAlignment() const;
167 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
168 /// is, presumably, for writing out the mnemonics for the assembly writer.
169 std::string getAsString() const;
171 /// \brief Equality and non-equality operators.
172 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
173 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
175 /// \brief Less-than operator. Useful for sorting the attributes list.
176 bool operator<(Attribute A) const;
178 void Profile(FoldingSetNodeID &ID) const {
179 ID.AddPointer(pImpl);
183 //===----------------------------------------------------------------------===//
185 /// \brief This class manages the ref count for the opaque AttributeSetImpl
186 /// object and provides accessors for it.
194 friend class AttrBuilder;
195 friend class AttributeSetImpl;
197 /// \brief The attributes that we are managing. This can be null to represent
198 /// the empty attributes list.
199 AttributeSetImpl *pImpl;
201 /// \brief The attributes for the specified index are returned.
202 AttributeSetNode *getAttributes(unsigned Idx) const;
204 /// \brief Create an AttributeSet with the specified parameters in it.
205 static AttributeSet get(LLVMContext &C,
206 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
207 static AttributeSet get(LLVMContext &C,
208 ArrayRef<std::pair<unsigned,
209 AttributeSetNode*> > Attrs);
211 static AttributeSet getImpl(LLVMContext &C,
212 ArrayRef<std::pair<unsigned,
213 AttributeSetNode*> > Attrs);
216 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
218 AttributeSet() : pImpl(0) {}
219 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
220 const AttributeSet &operator=(const AttributeSet &RHS) {
225 //===--------------------------------------------------------------------===//
226 // AttributeSet Construction and Mutation
227 //===--------------------------------------------------------------------===//
229 /// \brief Return an AttributeSet with the specified parameters in it.
230 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
231 static AttributeSet get(LLVMContext &C, unsigned Idx,
232 ArrayRef<Attribute::AttrKind> Kind);
233 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
235 /// \brief Add an attribute to the attribute set at the given index. Since
236 /// attribute sets are immutable, this returns a new set.
237 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
238 Attribute::AttrKind Attr) const;
240 /// \brief Add attributes to the attribute set at the given index. Since
241 /// attribute sets are immutable, this returns a new set.
242 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
243 AttributeSet Attrs) const;
245 /// \brief Remove the specified attribute at the specified index from this
246 /// attribute list. Since attribute lists are immutable, this returns the new
248 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
249 Attribute::AttrKind Attr) const;
251 /// \brief Remove the specified attributes at the specified index from this
252 /// attribute list. Since attribute lists are immutable, this returns the new
254 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
255 AttributeSet Attrs) const;
257 //===--------------------------------------------------------------------===//
258 // AttributeSet Accessors
259 //===--------------------------------------------------------------------===//
261 /// \brief The attributes for the specified index are returned.
262 AttributeSet getParamAttributes(unsigned Idx) const;
264 /// \brief The attributes for the ret value are returned.
265 AttributeSet getRetAttributes() const;
267 /// \brief The function attributes are returned.
268 AttributeSet getFnAttributes() const;
270 /// \brief Return true if the attribute exists at the given index.
271 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
273 /// \brief Return true if attribute exists at the given index.
274 bool hasAttributes(unsigned Index) const;
276 /// \brief Return true if the specified attribute is set for at least one
277 /// parameter or for the return value.
278 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
280 /// \brief Return the alignment for the specified function parameter.
281 unsigned getParamAlignment(unsigned Idx) const;
283 /// \brief Get the stack alignment.
284 unsigned getStackAlignment(unsigned Index) const;
286 /// \brief Return the attributes at the index as a string.
287 std::string getAsString(unsigned Index) const;
289 typedef ArrayRef<Attribute>::iterator iterator;
291 iterator begin(unsigned Idx) const;
292 iterator end(unsigned Idx) const;
294 /// operator==/!= - Provide equality predicates.
295 bool operator==(const AttributeSet &RHS) const {
296 return pImpl == RHS.pImpl;
298 bool operator!=(const AttributeSet &RHS) const {
299 return pImpl != RHS.pImpl;
302 //===--------------------------------------------------------------------===//
303 // AttributeSet Introspection
304 //===--------------------------------------------------------------------===//
306 // FIXME: Remove this.
307 uint64_t Raw(unsigned Index) const;
309 /// \brief Return a raw pointer that uniquely identifies this attribute list.
310 void *getRawPointer() const {
314 /// \brief Return true if there are no attributes.
315 bool isEmpty() const {
316 return getNumSlots() == 0;
319 /// \brief Return the number of slots used in this attribute list. This is
320 /// the number of arguments that have an attribute set on them (including the
321 /// function itself).
322 unsigned getNumSlots() const;
324 /// \brief Return the index for the given slot.
325 uint64_t getSlotIndex(unsigned Slot) const;
327 /// \brief Return the attributes at the given slot.
328 AttributeSet getSlotAttributes(unsigned Slot) const;
333 //===----------------------------------------------------------------------===//
335 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
337 template<> struct DenseMapInfo<Attribute::AttrKind> {
338 static inline Attribute::AttrKind getEmptyKey() {
339 return Attribute::AttrKindEmptyKey;
341 static inline Attribute::AttrKind getTombstoneKey() {
342 return Attribute::AttrKindTombstoneKey;
344 static unsigned getHashValue(const Attribute::AttrKind &Val) {
347 static bool isEqual(const Attribute::AttrKind &LHS,
348 const Attribute::AttrKind &RHS) {
353 //===----------------------------------------------------------------------===//
355 /// \brief This class is used in conjunction with the Attribute::get method to
356 /// create an Attribute object. The object itself is uniquified. The Builder's
357 /// value, however, is not. So this can be used as a quick way to test for
358 /// equality, presence of attributes, etc.
360 DenseSet<Attribute::AttrKind> Attrs;
361 std::map<std::string, std::string> TargetDepAttrs;
363 uint64_t StackAlignment;
365 AttrBuilder() : Alignment(0), StackAlignment(0) {}
366 explicit AttrBuilder(uint64_t Val) : Alignment(0), StackAlignment(0) {
369 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
372 AttrBuilder(AttributeSet AS, unsigned Idx);
376 /// \brief Add an attribute to the builder.
377 AttrBuilder &addAttribute(Attribute::AttrKind Val);
379 /// \brief Add the Attribute object to the builder.
380 AttrBuilder &addAttribute(Attribute A);
382 /// \brief Add the target-dependent attribute to the builder.
383 AttrBuilder &addAttribute(StringRef A, StringRef V);
385 /// \brief Remove an attribute from the builder.
386 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
388 /// \brief Remove the attributes from the builder.
389 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
391 /// \brief Remove the target-dependent attribute to the builder.
392 AttrBuilder &removeAttribute(StringRef A);
394 /// \brief Return true if the builder has the specified attribute.
395 bool contains(Attribute::AttrKind A) const;
397 /// \brief Return true if the builder has IR-level attributes.
398 bool hasAttributes() const;
400 /// \brief Return true if the builder has any attribute that's in the
401 /// specified attribute.
402 bool hasAttributes(AttributeSet A, uint64_t Index) const;
404 /// \brief Return true if the builder has an alignment attribute.
405 bool hasAlignmentAttr() const;
407 /// \brief Retrieve the alignment attribute, if it exists.
408 uint64_t getAlignment() const { return Alignment; }
410 /// \brief Retrieve the stack alignment attribute, if it exists.
411 uint64_t getStackAlignment() const { return StackAlignment; }
413 /// \brief This turns an int alignment (which must be a power of 2) into the
414 /// form used internally in Attribute.
415 AttrBuilder &addAlignmentAttr(unsigned Align);
417 /// \brief This turns an int stack alignment (which must be a power of 2) into
418 /// the form used internally in Attribute.
419 AttrBuilder &addStackAlignmentAttr(unsigned Align);
421 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
422 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
424 iterator begin() { return Attrs.begin(); }
425 iterator end() { return Attrs.end(); }
427 const_iterator begin() const { return Attrs.begin(); }
428 const_iterator end() const { return Attrs.end(); }
430 /// \brief Remove attributes that are used on functions only.
431 void removeFunctionOnlyAttrs() {
432 removeAttribute(Attribute::NoReturn)
433 .removeAttribute(Attribute::NoUnwind)
434 .removeAttribute(Attribute::ReadNone)
435 .removeAttribute(Attribute::ReadOnly)
436 .removeAttribute(Attribute::NoInline)
437 .removeAttribute(Attribute::AlwaysInline)
438 .removeAttribute(Attribute::OptimizeForSize)
439 .removeAttribute(Attribute::StackProtect)
440 .removeAttribute(Attribute::StackProtectReq)
441 .removeAttribute(Attribute::StackProtectStrong)
442 .removeAttribute(Attribute::NoRedZone)
443 .removeAttribute(Attribute::NoImplicitFloat)
444 .removeAttribute(Attribute::Naked)
445 .removeAttribute(Attribute::InlineHint)
446 .removeAttribute(Attribute::StackAlignment)
447 .removeAttribute(Attribute::UWTable)
448 .removeAttribute(Attribute::NonLazyBind)
449 .removeAttribute(Attribute::ReturnsTwice)
450 .removeAttribute(Attribute::AddressSafety)
451 .removeAttribute(Attribute::MinSize)
452 .removeAttribute(Attribute::NoDuplicate);
455 bool operator==(const AttrBuilder &B);
456 bool operator!=(const AttrBuilder &B) {
457 return !(*this == B);
460 // FIXME: Remove this in 4.0.
462 /// \brief Add the raw value to the internal representation.
463 AttrBuilder &addRawValue(uint64_t Val);
466 namespace AttributeFuncs {
468 /// \brief Which attributes cannot be applied to a type.
469 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
471 } // end AttributeFuncs namespace
473 } // end llvm namespace