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_ATTRIBUTES_H
17 #define LLVM_ATTRIBUTES_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseSet.h"
30 //===----------------------------------------------------------------------===//
32 /// \brief Functions, function parameters, and return types can have attributes
33 /// to indicate how they should be treated by optimizations and code
34 /// generation. This class represents one of those attributes. It's light-weight
35 /// and should be passed around by-value.
38 /// This enumeration lists the attributes that can be associated with
39 /// parameters, function results or the function itself.
41 /// Note: uwtable is about the ABI or the user mandating an entry in the
42 /// unwind table. The nounwind attribute is about an exception passing by the
45 /// In a theoretical system that uses tables for profiling and sjlj for
46 /// exceptions, they would be fully independent. In a normal system that uses
47 /// tables for both, the semantics are:
49 /// nil = Needs an entry because an exception might pass by.
50 /// nounwind = No need for an entry
51 /// uwtable = Needs an entry because the ABI says so and because
52 /// an exception might pass by.
53 /// uwtable + nounwind = Needs an entry because the ABI says so.
56 // IR-Level Attributes
57 None, ///< No attributes have been set
58 AddressSafety, ///< Address safety checking is on.
59 Alignment, ///< Alignment of parameter (5 bits)
60 ///< stored as log2 of alignment with +1 bias
61 ///< 0 means unaligned (different from align(1))
62 AlwaysInline, ///< inline=always
63 ByVal, ///< Pass structure by value
64 InlineHint, ///< Source said inlining was desirable
65 InReg, ///< Force argument to be passed in register
66 MinSize, ///< Function must be optimized for size first
67 Naked, ///< Naked function
68 Nest, ///< Nested function static chain
69 NoAlias, ///< Considered to not alias after call
70 NoCapture, ///< Function creates no aliases of pointer
71 NoDuplicate, ///< Call cannot be duplicated
72 NoImplicitFloat, ///< Disable implicit floating point insts
73 NoInline, ///< inline=never
74 NonLazyBind, ///< Function is called early and/or
75 ///< often, so lazy binding isn't worthwhile
76 NoRedZone, ///< Disable redzone
77 NoReturn, ///< Mark the function as not returning
78 NoUnwind, ///< Function doesn't unwind stack
79 OptimizeForSize, ///< opt_size
80 ReadNone, ///< Function does not access memory
81 ReadOnly, ///< Function only reads from memory
82 ReturnsTwice, ///< Function can return twice
83 SExt, ///< Sign extended before/after call
84 StackAlignment, ///< Alignment of stack for function (3 bits)
85 ///< stored as log2 of alignment with +1 bias 0
86 ///< means unaligned (different from
88 StackProtect, ///< Stack protection.
89 StackProtectReq, ///< Stack protection required.
90 StructRet, ///< Hidden pointer to structure to return
91 UWTable, ///< Function must be in a unwind table
92 ZExt ///< Zero extended before/after call
96 Attribute(AttributeImpl *A) : pImpl(A) {}
98 Attribute() : pImpl(0) {}
100 /// \brief Return a uniquified Attribute object. This takes the uniquified
101 /// value from the Builder and wraps it in the Attribute class.
102 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals);
103 static Attribute get(LLVMContext &Context, AttrBuilder &B);
105 /// \brief Return true if the attribute is present.
106 bool hasAttribute(AttrKind Val) const;
108 /// \brief Return true if attributes exist
109 bool hasAttributes() const;
111 /// \brief Return true if the attributes are a non-null intersection.
112 bool hasAttributes(const Attribute &A) const;
114 /// \brief Returns the alignment field of an attribute as a byte alignment
116 unsigned getAlignment() const;
118 /// \brief Returns the stack alignment field of an attribute as a byte
120 unsigned getStackAlignment() const;
122 bool operator==(AttrKind K) const;
123 bool operator!=(AttrKind K) const;
125 // FIXME: Remove these 'operator' methods.
126 bool operator==(const Attribute &A) const {
127 return pImpl == A.pImpl;
129 bool operator!=(const Attribute &A) const {
130 return pImpl != A.pImpl;
133 uint64_t getBitMask() const;
135 /// \brief Which attributes cannot be applied to a type.
136 static Attribute typeIncompatible(Type *Ty);
138 /// \brief This returns an integer containing an encoding of all the LLVM
139 /// attributes found in the given attribute bitset. Any change to this
140 /// encoding is a breaking change to bitcode compatibility.
141 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs);
143 /// \brief This returns an attribute bitset containing the LLVM attributes
144 /// that have been decoded from the given integer. This function must stay in
145 /// sync with 'encodeLLVMAttributesForBitcode'.
146 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C,
147 uint64_t EncodedAttrs);
149 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
150 /// is, presumably, for writing out the mnemonics for the assembly writer.
151 std::string getAsString() const;
154 //===----------------------------------------------------------------------===//
156 /// \brief Provide DenseMapInfo for Attribute::AttrKind.
157 template<> struct DenseMapInfo<Attribute::AttrKind> {
158 static inline Attribute::AttrKind getEmptyKey() {
159 return Attribute::AttrKind(-1);
161 static inline Attribute::AttrKind getTombstoneKey() {
162 return Attribute::AttrKind(~0UL - 1L);
164 static unsigned getHashValue(const Attribute::AttrKind &Val) {
165 return (unsigned)(Val * 37UL);
167 static bool isEqual(const Attribute::AttrKind &LHS,
168 const Attribute::AttrKind &RHS) {
173 //===----------------------------------------------------------------------===//
175 /// \brief This class is used in conjunction with the Attribute::get method to
176 /// create an Attribute object. The object itself is uniquified. The Builder's
177 /// value, however, is not. So this can be used as a quick way to test for
178 /// equality, presence of attributes, etc.
180 DenseSet<Attribute::AttrKind> AttrSet;
182 uint64_t StackAlignment;
184 uint64_t Bits; // FIXME: Remove after encoding the attr list in the bc file.
186 AttrBuilder() : Alignment(0), StackAlignment(0), Bits(0) {}
187 explicit AttrBuilder(uint64_t B) : Bits(B) {}
188 AttrBuilder(const Attribute &A) : Bits(A.getBitMask()) {}
190 /// \brief Clear out the builder's internals.
193 /// \brief Add an attribute to the builder.
194 AttrBuilder &addAttribute(Attribute::AttrKind Val);
196 /// \brief Remove an attribute from the builder.
197 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
199 /// \brief Add the attributes from A to the builder.
200 AttrBuilder &addAttributes(const Attribute &A);
202 /// \brief Remove the attributes from A from the builder.
203 AttrBuilder &removeAttributes(const Attribute &A);
205 /// \brief Return true if the builder has the specified attribute.
206 bool contains(Attribute::AttrKind A) const;
208 /// \brief Return true if the builder has IR-level attributes.
209 bool hasAttributes() const;
211 /// \brief Return true if the builder has any attribute that's in the
212 /// specified attribute.
213 bool hasAttributes(const Attribute &A) const;
215 /// \brief Return true if the builder has an alignment attribute.
216 bool hasAlignmentAttr() const;
218 /// \brief Retrieve the alignment attribute, if it exists.
219 uint64_t getAlignment() const;
221 /// \brief Retrieve the stack alignment attribute, if it exists.
222 uint64_t getStackAlignment() const;
224 /// \brief This turns an int alignment (which must be a power of 2) into the
225 /// form used internally in Attribute.
226 AttrBuilder &addAlignmentAttr(unsigned Align);
228 /// \brief This turns an int stack alignment (which must be a power of 2) into
229 /// the form used internally in Attribute.
230 AttrBuilder &addStackAlignmentAttr(unsigned Align);
232 /// \brief Add the raw value to the internal representation.
234 /// N.B. This should be used ONLY for decoding LLVM bitcode!
235 AttrBuilder &addRawValue(uint64_t Val);
237 /// \brief Remove attributes that are used on functions only.
238 void removeFunctionOnlyAttrs() {
239 removeAttribute(Attribute::NoReturn)
240 .removeAttribute(Attribute::NoUnwind)
241 .removeAttribute(Attribute::ReadNone)
242 .removeAttribute(Attribute::ReadOnly)
243 .removeAttribute(Attribute::NoInline)
244 .removeAttribute(Attribute::AlwaysInline)
245 .removeAttribute(Attribute::OptimizeForSize)
246 .removeAttribute(Attribute::StackProtect)
247 .removeAttribute(Attribute::StackProtectReq)
248 .removeAttribute(Attribute::NoRedZone)
249 .removeAttribute(Attribute::NoImplicitFloat)
250 .removeAttribute(Attribute::Naked)
251 .removeAttribute(Attribute::InlineHint)
252 .removeAttribute(Attribute::StackAlignment)
253 .removeAttribute(Attribute::UWTable)
254 .removeAttribute(Attribute::NonLazyBind)
255 .removeAttribute(Attribute::ReturnsTwice)
256 .removeAttribute(Attribute::AddressSafety)
257 .removeAttribute(Attribute::MinSize)
258 .removeAttribute(Attribute::NoDuplicate);
261 uint64_t getBitMask() const { return Bits; }
263 bool operator==(const AttrBuilder &B) const {
264 return Bits == B.Bits;
266 bool operator!=(const AttrBuilder &B) const {
267 return Bits != B.Bits;
271 //===----------------------------------------------------------------------===//
273 /// \brief This is just a pair of values to associate a set of attributes with
275 struct AttributeWithIndex {
276 Attribute Attrs; ///< The attributes that are set, or'd together.
277 unsigned Index; ///< Index of the parameter for which the attributes apply.
278 ///< Index 0 is used for return value attributes.
279 ///< Index ~0U is used for function attributes.
281 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
282 ArrayRef<Attribute::AttrKind> Attrs) {
283 return get(Idx, Attribute::get(C, Attrs));
285 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) {
286 AttributeWithIndex P;
293 //===----------------------------------------------------------------------===//
294 // AttributeSet Smart Pointer
295 //===----------------------------------------------------------------------===//
297 class AttributeSetImpl;
299 //===----------------------------------------------------------------------===//
301 /// \brief This class manages the ref count for the opaque AttributeSetImpl
302 /// object and provides accessors for it.
310 /// \brief The attributes that we are managing. This can be null to represent
311 /// the empty attributes list.
312 AttributeSetImpl *AttrList;
314 /// \brief The attributes for the specified index are returned. Attributes
315 /// for the result are denoted with Idx = 0.
316 Attribute getAttributes(unsigned Idx) const;
318 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {}
320 AttributeSet() : AttrList(0) {}
321 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {}
322 const AttributeSet &operator=(const AttributeSet &RHS);
324 //===--------------------------------------------------------------------===//
325 // Attribute List Construction and Mutation
326 //===--------------------------------------------------------------------===//
328 /// \brief Return an AttributeSet with the specified parameters in it.
329 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
331 /// \brief Add the specified attribute at the specified index to this
332 /// attribute list. Since attribute lists are immutable, this returns the new
334 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
336 /// \brief Remove the specified attribute at the specified index from this
337 /// attribute list. Since attribute lists are immutable, this returns the new
339 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const;
341 //===--------------------------------------------------------------------===//
342 // Attribute List Accessors
343 //===--------------------------------------------------------------------===//
345 /// \brief The attributes for the specified index are returned.
346 Attribute getParamAttributes(unsigned Idx) const {
347 return getAttributes(Idx);
350 /// \brief The attributes for the ret value are returned.
351 Attribute getRetAttributes() const {
352 return getAttributes(ReturnIndex);
355 /// \brief The function attributes are returned.
356 Attribute getFnAttributes() const {
357 return getAttributes(FunctionIndex);
360 /// \brief Return the alignment for the specified function parameter.
361 unsigned getParamAlignment(unsigned Idx) const {
362 return getAttributes(Idx).getAlignment();
365 /// \brief Return true if the attribute exists at the given index.
366 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
368 /// \brief Return true if attribute exists at the given index.
369 bool hasAttributes(unsigned Index) const;
371 /// \brief Get the stack alignment.
372 unsigned getStackAlignment(unsigned Index) const;
374 /// \brief Return the attributes at the index as a string.
375 std::string getAsString(unsigned Index) const;
377 uint64_t getBitMask(unsigned Index) const;
379 /// \brief Return true if the specified attribute is set for at least one
380 /// parameter or for the return value.
381 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
383 /// operator==/!= - Provide equality predicates.
384 bool operator==(const AttributeSet &RHS) const {
385 return AttrList == RHS.AttrList;
387 bool operator!=(const AttributeSet &RHS) const {
388 return AttrList != RHS.AttrList;
391 //===--------------------------------------------------------------------===//
392 // Attribute List Introspection
393 //===--------------------------------------------------------------------===//
395 /// \brief Return a raw pointer that uniquely identifies this attribute list.
396 void *getRawPointer() const {
400 // Attributes are stored as a dense set of slots, where there is one slot for
401 // each argument that has an attribute. This allows walking over the dense
402 // set instead of walking the sparse list of attributes.
404 /// \brief Return true if there are no attributes.
405 bool isEmpty() const {
406 return AttrList == 0;
409 /// \brief Return the number of slots used in this attribute list. This is
410 /// the number of arguments that have an attribute set on them (including the
411 /// function itself).
412 unsigned getNumSlots() const;
414 /// \brief Return the AttributeWithIndex at the specified slot. This holds a
415 /// index number plus a set of attributes.
416 const AttributeWithIndex &getSlot(unsigned Slot) const;
421 } // end llvm namespace