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"
29 class AttributeSetImpl;
30 class AttributeSetNode;
34 //===----------------------------------------------------------------------===//
36 /// \brief Functions, function parameters, and return types can have attributes
37 /// to indicate how they should be treated by optimizations and code
38 /// generation. This class represents one of those attributes. It's light-weight
39 /// and should be passed around by-value.
42 /// This enumeration lists the attributes that can be associated with
43 /// parameters, function results or the function itself.
45 /// Note: uwtable is about the ABI or the user mandating an entry in the
46 /// unwind table. The nounwind attribute is about an exception passing by the
49 /// In a theoretical system that uses tables for profiling and sjlj for
50 /// exceptions, they would be fully independent. In a normal system that uses
51 /// tables for both, the semantics are:
53 /// nil = Needs an entry because an exception might pass by.
54 /// nounwind = No need for an entry
55 /// uwtable = Needs an entry because the ABI says so and because
56 /// an exception might pass by.
57 /// uwtable + nounwind = Needs an entry because the ABI says so.
60 // IR-Level Attributes
61 None, ///< No attributes have been set
62 AddressSafety, ///< Address safety checking is on.
63 Alignment, ///< Alignment of parameter (5 bits)
64 ///< stored as log2 of alignment with +1 bias
65 ///< 0 means unaligned (different from align(1))
66 AlwaysInline, ///< inline=always
67 ByVal, ///< Pass structure by value
68 InlineHint, ///< Source said inlining was desirable
69 InReg, ///< Force argument to be passed in register
70 MinSize, ///< Function must be optimized for size first
71 Naked, ///< Naked function
72 Nest, ///< Nested function static chain
73 NoAlias, ///< Considered to not alias after call
74 NoCapture, ///< Function creates no aliases of pointer
75 NoDuplicate, ///< Call cannot be duplicated
76 NoImplicitFloat, ///< Disable implicit floating point insts
77 NoInline, ///< inline=never
78 NonLazyBind, ///< Function is called early and/or
79 ///< often, so lazy binding isn't worthwhile
80 NoRedZone, ///< Disable redzone
81 NoReturn, ///< Mark the function as not returning
82 NoUnwind, ///< Function doesn't unwind stack
83 OptimizeForSize, ///< opt_size
84 ReadNone, ///< Function does not access memory
85 ReadOnly, ///< Function only reads from memory
86 ReturnsTwice, ///< Function can return twice
87 SExt, ///< Sign extended before/after call
88 StackAlignment, ///< Alignment of stack for function (3 bits)
89 ///< stored as log2 of alignment with +1 bias 0
90 ///< means unaligned (different from
92 StackProtect, ///< Stack protection.
93 StackProtectReq, ///< Stack protection required.
94 StackProtectStrong, ///< Strong Stack protection.
95 StructRet, ///< Hidden pointer to structure to return
96 UWTable, ///< Function must be in a unwind table
97 ZExt, ///< Zero extended before/after call
99 EndAttrKinds, ///< Sentinal value useful for loops
101 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
102 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
105 AttributeImpl *pImpl;
106 Attribute(AttributeImpl *A) : pImpl(A) {}
108 Attribute() : pImpl(0) {}
110 //===--------------------------------------------------------------------===//
111 // Attribute Construction
112 //===--------------------------------------------------------------------===//
114 /// \brief Return a uniquified Attribute object.
115 static Attribute get(LLVMContext &Context, AttrKind Kind);
116 static Attribute get(LLVMContext &Context, AttrBuilder &B);
118 /// \brief Return a uniquified Attribute object that has the specific
120 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
121 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
123 //===--------------------------------------------------------------------===//
124 // Attribute Accessors
125 //===--------------------------------------------------------------------===//
127 /// \brief Return true if the attribute is present.
128 bool hasAttribute(AttrKind Val) const;
130 /// \brief Return true if attributes exist
131 bool hasAttributes() const;
133 /// \brief Returns the alignment field of an attribute as a byte alignment
135 unsigned getAlignment() const;
137 /// \brief Returns the stack alignment field of an attribute as a byte
139 unsigned getStackAlignment() const;
141 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
142 /// is, presumably, for writing out the mnemonics for the assembly writer.
143 std::string getAsString() const;
145 /// \brief Equality and non-equality query methods.
146 bool operator==(AttrKind K) const;
147 bool operator!=(AttrKind K) const;
149 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
150 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
152 /// \brief Less-than operator. Useful for sorting the attributes list.
153 bool operator<(Attribute A) const;
155 void Profile(FoldingSetNodeID &ID) const {
156 ID.AddPointer(pImpl);
159 // FIXME: Remove this.
160 uint64_t Raw() const;
163 //===----------------------------------------------------------------------===//
165 /// \brief This class manages the ref count for the opaque AttributeSetImpl
166 /// object and provides accessors for it.
174 friend class AttrBuilder;
175 friend class AttributeSetImpl;
177 /// \brief The attributes that we are managing. This can be null to represent
178 /// the empty attributes list.
179 AttributeSetImpl *pImpl;
181 /// \brief The attributes for the specified index are returned. Attributes
182 /// for the result are denoted with Idx = 0.
183 AttributeSetNode *getAttributes(unsigned Idx) const;
185 /// \brief Create an AttributeSet with the specified parameters in it.
186 static AttributeSet get(LLVMContext &C,
187 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
188 static AttributeSet get(LLVMContext &C,
189 ArrayRef<std::pair<unsigned,
190 AttributeSetNode*> > Attrs);
192 static AttributeSet getImpl(LLVMContext &C,
193 ArrayRef<std::pair<unsigned,
194 AttributeSetNode*> > Attrs);
197 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
199 AttributeSet() : pImpl(0) {}
200 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
201 const AttributeSet &operator=(const AttributeSet &RHS) {
206 //===--------------------------------------------------------------------===//
207 // AttributeSet Construction and Mutation
208 //===--------------------------------------------------------------------===//
210 /// \brief Return an AttributeSet with the specified parameters in it.
211 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
212 static AttributeSet get(LLVMContext &C, unsigned Idx,
213 ArrayRef<Attribute::AttrKind> Kind);
214 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
216 /// \brief Add an attribute to the attribute set at the given index. Since
217 /// attribute sets are immutable, this returns a new set.
218 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
219 Attribute::AttrKind Attr) const;
221 /// \brief Add attributes to the attribute set at the given index. Since
222 /// attribute sets are immutable, this returns a new set.
223 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
224 AttributeSet Attrs) const;
226 /// \brief Add return attributes to this attribute set. Since attribute sets
227 /// are immutable, this returns a new set.
228 AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const {
229 return addAttributes(C, ReturnIndex, Attrs);
232 /// \brief Add function attributes to this attribute set. Since attribute sets
233 /// are immutable, this returns a new set.
234 AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const {
235 return addAttributes(C, FunctionIndex, Attrs);
238 /// \brief Remove the specified attribute at the specified index from this
239 /// attribute list. Since attribute lists are immutable, this returns the new
241 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
242 Attribute::AttrKind Attr) const;
244 /// \brief Remove the specified attributes at the specified index from this
245 /// attribute list. Since attribute lists are immutable, this returns the new
247 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
248 AttributeSet Attrs) const;
250 //===--------------------------------------------------------------------===//
251 // AttributeSet Accessors
252 //===--------------------------------------------------------------------===//
254 /// \brief The attributes for the specified index are returned.
255 AttributeSet getParamAttributes(unsigned Idx) const;
257 /// \brief The attributes for the ret value are returned.
258 AttributeSet getRetAttributes() const;
260 /// \brief The function attributes are returned.
261 AttributeSet getFnAttributes() const;
263 /// \brief Return true if the attribute exists at the given index.
264 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
266 /// \brief Return true if attribute exists at the given index.
267 bool hasAttributes(unsigned Index) const;
269 /// \brief Return true if the specified attribute is set for at least one
270 /// parameter or for the return value.
271 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
273 /// \brief Return the alignment for the specified function parameter.
274 unsigned getParamAlignment(unsigned Idx) const;
276 /// \brief Get the stack alignment.
277 unsigned getStackAlignment(unsigned Index) const;
279 /// \brief Return the attributes at the index as a string.
280 std::string getAsString(unsigned Index) const;
282 /// operator==/!= - Provide equality predicates.
283 bool operator==(const AttributeSet &RHS) const {
284 return pImpl == RHS.pImpl;
286 bool operator!=(const AttributeSet &RHS) const {
287 return pImpl != RHS.pImpl;
290 //===--------------------------------------------------------------------===//
291 // AttributeSet Introspection
292 //===--------------------------------------------------------------------===//
294 // FIXME: Remove this.
295 uint64_t Raw(unsigned Index) const;
297 /// \brief Return a raw pointer that uniquely identifies this attribute list.
298 void *getRawPointer() const {
302 /// \brief Return true if there are no attributes.
303 bool isEmpty() const {
307 /// \brief Return the number of slots used in this attribute list. This is
308 /// the number of arguments that have an attribute set on them (including the
309 /// function itself).
310 unsigned getNumSlots() const;
312 /// \brief Return the index for the given slot.
313 uint64_t getSlotIndex(unsigned Slot) const;
315 /// \brief Return the attributes at the given slot.
316 AttributeSet getSlotAttributes(unsigned Slot) const;
321 //===----------------------------------------------------------------------===//
323 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
325 template<> struct DenseMapInfo<Attribute::AttrKind> {
326 static inline Attribute::AttrKind getEmptyKey() {
327 return Attribute::AttrKindEmptyKey;
329 static inline Attribute::AttrKind getTombstoneKey() {
330 return Attribute::AttrKindTombstoneKey;
332 static unsigned getHashValue(const Attribute::AttrKind &Val) {
335 static bool isEqual(const Attribute::AttrKind &LHS,
336 const Attribute::AttrKind &RHS) {
341 //===----------------------------------------------------------------------===//
343 /// \brief This class is used in conjunction with the Attribute::get method to
344 /// create an Attribute object. The object itself is uniquified. The Builder's
345 /// value, however, is not. So this can be used as a quick way to test for
346 /// equality, presence of attributes, etc.
348 DenseSet<Attribute::AttrKind> Attrs;
350 uint64_t StackAlignment;
352 AttrBuilder() : Alignment(0), StackAlignment(0) {}
353 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
356 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
359 AttrBuilder(AttributeSet AS, unsigned Idx);
363 /// \brief Add an attribute to the builder.
364 AttrBuilder &addAttribute(Attribute::AttrKind Val);
366 /// \brief Remove an attribute from the builder.
367 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
369 /// \brief Add the attributes to the builder.
370 AttrBuilder &addAttributes(Attribute A);
372 /// \brief Remove the attributes from the builder.
373 AttrBuilder &removeAttributes(Attribute A);
375 /// \brief Add the attributes to the builder.
376 AttrBuilder &addAttributes(AttributeSet A);
378 /// \brief Return true if the builder has the specified attribute.
379 bool contains(Attribute::AttrKind A) const;
381 /// \brief Return true if the builder has IR-level attributes.
382 bool hasAttributes() const;
384 /// \brief Return true if the builder has any attribute that's in the
385 /// specified attribute.
386 bool hasAttributes(const Attribute &A) const;
388 /// \brief Return true if the builder has an alignment attribute.
389 bool hasAlignmentAttr() const;
391 /// \brief Retrieve the alignment attribute, if it exists.
392 uint64_t getAlignment() const { return Alignment; }
394 /// \brief Retrieve the stack alignment attribute, if it exists.
395 uint64_t getStackAlignment() const { return StackAlignment; }
397 /// \brief This turns an int alignment (which must be a power of 2) into the
398 /// form used internally in Attribute.
399 AttrBuilder &addAlignmentAttr(unsigned Align);
401 /// \brief This turns an int stack alignment (which must be a power of 2) into
402 /// the form used internally in Attribute.
403 AttrBuilder &addStackAlignmentAttr(unsigned Align);
405 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
406 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
408 iterator begin() { return Attrs.begin(); }
409 iterator end() { return Attrs.end(); }
411 const_iterator begin() const { return Attrs.begin(); }
412 const_iterator end() const { return Attrs.end(); }
414 /// \brief Remove attributes that are used on functions only.
415 void removeFunctionOnlyAttrs() {
416 removeAttribute(Attribute::NoReturn)
417 .removeAttribute(Attribute::NoUnwind)
418 .removeAttribute(Attribute::ReadNone)
419 .removeAttribute(Attribute::ReadOnly)
420 .removeAttribute(Attribute::NoInline)
421 .removeAttribute(Attribute::AlwaysInline)
422 .removeAttribute(Attribute::OptimizeForSize)
423 .removeAttribute(Attribute::StackProtect)
424 .removeAttribute(Attribute::StackProtectReq)
425 .removeAttribute(Attribute::StackProtectStrong)
426 .removeAttribute(Attribute::NoRedZone)
427 .removeAttribute(Attribute::NoImplicitFloat)
428 .removeAttribute(Attribute::Naked)
429 .removeAttribute(Attribute::InlineHint)
430 .removeAttribute(Attribute::StackAlignment)
431 .removeAttribute(Attribute::UWTable)
432 .removeAttribute(Attribute::NonLazyBind)
433 .removeAttribute(Attribute::ReturnsTwice)
434 .removeAttribute(Attribute::AddressSafety)
435 .removeAttribute(Attribute::MinSize)
436 .removeAttribute(Attribute::NoDuplicate);
439 bool operator==(const AttrBuilder &B);
440 bool operator!=(const AttrBuilder &B) {
441 return !(*this == B);
444 // FIXME: Remove these.
446 /// \brief Add the raw value to the internal representation.
448 /// N.B. This should be used ONLY for decoding LLVM bitcode!
449 AttrBuilder &addRawValue(uint64_t Val);
451 uint64_t Raw() const;
454 namespace AttributeFuncs {
456 /// \brief Which attributes cannot be applied to a type.
457 Attribute typeIncompatible(Type *Ty);
459 /// \brief This returns an integer containing an encoding of all the LLVM
460 /// attributes found in the given attribute bitset. Any change to this encoding
461 /// is a breaking change to bitcode compatibility.
462 uint64_t encodeLLVMAttributesForBitcode(AttributeSet Attrs, unsigned Index);
464 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
465 /// been decoded from the given integer. This function must stay in sync with
466 /// 'encodeLLVMAttributesForBitcode'.
467 /// N.B. This should be used only by the bitcode reader!
468 void decodeLLVMAttributesForBitcode(LLVMContext &C, AttrBuilder &B,
469 uint64_t EncodedAttrs);
471 } // end AttributeFuncs namespace
473 } // end llvm namespace