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;
35 //===----------------------------------------------------------------------===//
37 /// \brief Functions, function parameters, and return types can have attributes
38 /// to indicate how they should be treated by optimizations and code
39 /// generation. This class represents one of those attributes. It's light-weight
40 /// and should be passed around by-value.
43 /// This enumeration lists the attributes that can be associated with
44 /// parameters, function results or the function itself.
46 /// Note: uwtable is about the ABI or the user mandating an entry in the
47 /// unwind table. The nounwind attribute is about an exception passing by the
50 /// In a theoretical system that uses tables for profiling and sjlj for
51 /// exceptions, they would be fully independent. In a normal system that uses
52 /// tables for both, the semantics are:
54 /// nil = Needs an entry because an exception might pass by.
55 /// nounwind = No need for an entry
56 /// uwtable = Needs an entry because the ABI says so and because
57 /// an exception might pass by.
58 /// uwtable + nounwind = Needs an entry because the ABI says so.
61 // IR-Level Attributes
62 None, ///< No attributes have been set
63 AddressSafety, ///< Address safety checking is on.
64 Alignment, ///< Alignment of parameter (5 bits)
65 ///< stored as log2 of alignment with +1 bias
66 ///< 0 means unaligned (different from align(1))
67 AlwaysInline, ///< inline=always
68 ByVal, ///< Pass structure by value
69 InlineHint, ///< Source said inlining was desirable
70 InReg, ///< Force argument to be passed in register
71 MinSize, ///< Function must be optimized for size first
72 Naked, ///< Naked function
73 Nest, ///< Nested function static chain
74 NoAlias, ///< Considered to not alias after call
75 NoCapture, ///< Function creates no aliases of pointer
76 NoDuplicate, ///< Call cannot be duplicated
77 NoImplicitFloat, ///< Disable implicit floating point insts
78 NoInline, ///< inline=never
79 NonLazyBind, ///< Function is called early and/or
80 ///< often, so lazy binding isn't worthwhile
81 NoRedZone, ///< Disable redzone
82 NoReturn, ///< Mark the function as not returning
83 NoUnwind, ///< Function doesn't unwind stack
84 OptimizeForSize, ///< opt_size
85 ReadNone, ///< Function does not access memory
86 ReadOnly, ///< Function only reads from memory
87 ReturnsTwice, ///< Function can return twice
88 SExt, ///< Sign extended before/after call
89 StackAlignment, ///< Alignment of stack for function (3 bits)
90 ///< stored as log2 of alignment with +1 bias 0
91 ///< means unaligned (different from
93 StackProtect, ///< Stack protection.
94 StackProtectReq, ///< Stack protection required.
95 StackProtectStrong, ///< Strong Stack protection.
96 StructRet, ///< Hidden pointer to structure to return
97 UWTable, ///< Function must be in a unwind table
98 ZExt, ///< Zero extended before/after call
100 EndAttrKinds, ///< Sentinal value useful for loops
102 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo
103 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo
106 AttributeImpl *pImpl;
107 Attribute(AttributeImpl *A) : pImpl(A) {}
109 Attribute() : pImpl(0) {}
111 //===--------------------------------------------------------------------===//
112 // Attribute Construction
113 //===--------------------------------------------------------------------===//
115 /// \brief Return a uniquified Attribute object.
116 static Attribute get(LLVMContext &Context, AttrKind Kind);
117 static Attribute get(LLVMContext &Context, AttrBuilder &B);
119 /// \brief Return a uniquified Attribute object that has the specific
121 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
122 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
124 //===--------------------------------------------------------------------===//
125 // Attribute Accessors
126 //===--------------------------------------------------------------------===//
128 /// \brief Return true if the attribute is present.
129 bool hasAttribute(AttrKind Val) const;
131 /// \brief Return true if attributes exist
132 bool hasAttributes() const;
134 /// \brief Return the kind of this attribute.
135 Constant *getAttributeKind() const;
137 /// \brief Return the value (if present) of the non-target-specific attribute.
138 ArrayRef<Constant*> getAttributeValues() const;
140 /// \brief Returns the alignment field of an attribute as a byte alignment
142 unsigned getAlignment() const;
144 /// \brief Returns the stack alignment field of an attribute as a byte
146 unsigned getStackAlignment() const;
148 /// \brief The Attribute is converted to a string of equivalent mnemonic. This
149 /// is, presumably, for writing out the mnemonics for the assembly writer.
150 std::string getAsString() const;
152 /// \brief Equality and non-equality query methods.
153 bool operator==(AttrKind K) const;
154 bool operator!=(AttrKind K) const;
156 bool operator==(Attribute A) const { return pImpl == A.pImpl; }
157 bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
159 /// \brief Less-than operator. Useful for sorting the attributes list.
160 bool operator<(Attribute A) const;
162 void Profile(FoldingSetNodeID &ID) const {
163 ID.AddPointer(pImpl);
166 // FIXME: Remove this.
167 uint64_t Raw() const;
170 //===----------------------------------------------------------------------===//
172 /// \brief This class manages the ref count for the opaque AttributeSetImpl
173 /// object and provides accessors for it.
181 friend class AttrBuilder;
182 friend class AttributeSetImpl;
184 /// \brief The attributes that we are managing. This can be null to represent
185 /// the empty attributes list.
186 AttributeSetImpl *pImpl;
188 /// \brief The attributes for the specified index are returned. Attributes
189 /// for the result are denoted with Idx = 0.
190 AttributeSetNode *getAttributes(unsigned Idx) const;
192 /// \brief Create an AttributeSet with the specified parameters in it.
193 static AttributeSet get(LLVMContext &C,
194 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
195 static AttributeSet get(LLVMContext &C,
196 ArrayRef<std::pair<unsigned,
197 AttributeSetNode*> > Attrs);
199 static AttributeSet getImpl(LLVMContext &C,
200 ArrayRef<std::pair<unsigned,
201 AttributeSetNode*> > Attrs);
204 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
206 AttributeSet() : pImpl(0) {}
207 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
208 const AttributeSet &operator=(const AttributeSet &RHS) {
213 //===--------------------------------------------------------------------===//
214 // AttributeSet Construction and Mutation
215 //===--------------------------------------------------------------------===//
217 /// \brief Return an AttributeSet with the specified parameters in it.
218 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
219 static AttributeSet get(LLVMContext &C, unsigned Idx,
220 ArrayRef<Attribute::AttrKind> Kind);
221 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
223 /// \brief Add an attribute to the attribute set at the given index. Since
224 /// attribute sets are immutable, this returns a new set.
225 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
226 Attribute::AttrKind Attr) const;
228 /// \brief Add attributes to the attribute set at the given index. Since
229 /// attribute sets are immutable, this returns a new set.
230 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
231 AttributeSet Attrs) const;
233 /// \brief Add return attributes to this attribute set. Since attribute sets
234 /// are immutable, this returns a new set.
235 AttributeSet addRetAttributes(LLVMContext &C, AttributeSet Attrs) const {
236 return addAttributes(C, ReturnIndex, Attrs);
239 /// \brief Add function attributes to this attribute set. Since attribute sets
240 /// are immutable, this returns a new set.
241 AttributeSet addFnAttributes(LLVMContext &C, AttributeSet Attrs) const {
242 return addAttributes(C, FunctionIndex, Attrs);
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 /// operator==/!= - Provide equality predicates.
290 bool operator==(const AttributeSet &RHS) const {
291 return pImpl == RHS.pImpl;
293 bool operator!=(const AttributeSet &RHS) const {
294 return pImpl != RHS.pImpl;
297 //===--------------------------------------------------------------------===//
298 // AttributeSet Introspection
299 //===--------------------------------------------------------------------===//
301 // FIXME: Remove this.
302 uint64_t Raw(unsigned Index) const;
304 /// \brief Return a raw pointer that uniquely identifies this attribute list.
305 void *getRawPointer() const {
309 /// \brief Return true if there are no attributes.
310 bool isEmpty() const {
311 return getNumSlots() == 0;
314 /// \brief Return the number of slots used in this attribute list. This is
315 /// the number of arguments that have an attribute set on them (including the
316 /// function itself).
317 unsigned getNumSlots() const;
319 /// \brief Return the index for the given slot.
320 uint64_t getSlotIndex(unsigned Slot) const;
322 /// \brief Return the attributes at the given slot.
323 AttributeSet getSlotAttributes(unsigned Slot) const;
328 //===----------------------------------------------------------------------===//
330 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
332 template<> struct DenseMapInfo<Attribute::AttrKind> {
333 static inline Attribute::AttrKind getEmptyKey() {
334 return Attribute::AttrKindEmptyKey;
336 static inline Attribute::AttrKind getTombstoneKey() {
337 return Attribute::AttrKindTombstoneKey;
339 static unsigned getHashValue(const Attribute::AttrKind &Val) {
342 static bool isEqual(const Attribute::AttrKind &LHS,
343 const Attribute::AttrKind &RHS) {
348 //===----------------------------------------------------------------------===//
350 /// \brief This class is used in conjunction with the Attribute::get method to
351 /// create an Attribute object. The object itself is uniquified. The Builder's
352 /// value, however, is not. So this can be used as a quick way to test for
353 /// equality, presence of attributes, etc.
355 DenseSet<Attribute::AttrKind> Attrs;
357 uint64_t StackAlignment;
359 AttrBuilder() : Alignment(0), StackAlignment(0) {}
360 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) {
363 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
366 AttrBuilder(AttributeSet AS, unsigned Idx);
370 /// \brief Add an attribute to the builder.
371 AttrBuilder &addAttribute(Attribute::AttrKind Val);
373 /// \brief Remove an attribute from the builder.
374 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
376 /// \brief Add the attributes to the builder.
377 AttrBuilder &addAttributes(Attribute A);
379 /// \brief Remove the attributes from the builder.
380 AttrBuilder &removeAttributes(Attribute A);
382 /// \brief Add the attributes to the builder.
383 AttrBuilder &addAttributes(AttributeSet A);
385 /// \brief Return true if the builder has the specified attribute.
386 bool contains(Attribute::AttrKind A) const;
388 /// \brief Return true if the builder has IR-level attributes.
389 bool hasAttributes() const;
391 /// \brief Return true if the builder has any attribute that's in the
392 /// specified attribute.
393 bool hasAttributes(const Attribute &A) const;
395 /// \brief Return true if the builder has an alignment attribute.
396 bool hasAlignmentAttr() const;
398 /// \brief Retrieve the alignment attribute, if it exists.
399 uint64_t getAlignment() const { return Alignment; }
401 /// \brief Retrieve the stack alignment attribute, if it exists.
402 uint64_t getStackAlignment() const { return StackAlignment; }
404 /// \brief This turns an int alignment (which must be a power of 2) into the
405 /// form used internally in Attribute.
406 AttrBuilder &addAlignmentAttr(unsigned Align);
408 /// \brief This turns an int stack alignment (which must be a power of 2) into
409 /// the form used internally in Attribute.
410 AttrBuilder &addStackAlignmentAttr(unsigned Align);
412 typedef DenseSet<Attribute::AttrKind>::iterator iterator;
413 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
415 iterator begin() { return Attrs.begin(); }
416 iterator end() { return Attrs.end(); }
418 const_iterator begin() const { return Attrs.begin(); }
419 const_iterator end() const { return Attrs.end(); }
421 /// \brief Remove attributes that are used on functions only.
422 void removeFunctionOnlyAttrs() {
423 removeAttribute(Attribute::NoReturn)
424 .removeAttribute(Attribute::NoUnwind)
425 .removeAttribute(Attribute::ReadNone)
426 .removeAttribute(Attribute::ReadOnly)
427 .removeAttribute(Attribute::NoInline)
428 .removeAttribute(Attribute::AlwaysInline)
429 .removeAttribute(Attribute::OptimizeForSize)
430 .removeAttribute(Attribute::StackProtect)
431 .removeAttribute(Attribute::StackProtectReq)
432 .removeAttribute(Attribute::StackProtectStrong)
433 .removeAttribute(Attribute::NoRedZone)
434 .removeAttribute(Attribute::NoImplicitFloat)
435 .removeAttribute(Attribute::Naked)
436 .removeAttribute(Attribute::InlineHint)
437 .removeAttribute(Attribute::StackAlignment)
438 .removeAttribute(Attribute::UWTable)
439 .removeAttribute(Attribute::NonLazyBind)
440 .removeAttribute(Attribute::ReturnsTwice)
441 .removeAttribute(Attribute::AddressSafety)
442 .removeAttribute(Attribute::MinSize)
443 .removeAttribute(Attribute::NoDuplicate);
446 bool operator==(const AttrBuilder &B);
447 bool operator!=(const AttrBuilder &B) {
448 return !(*this == B);
451 // FIXME: Remove these.
453 /// \brief Add the raw value to the internal representation.
455 /// N.B. This should be used ONLY for decoding LLVM bitcode!
456 AttrBuilder &addRawValue(uint64_t Val);
458 uint64_t Raw() const;
461 namespace AttributeFuncs {
463 /// \brief Which attributes cannot be applied to a type.
464 Attribute typeIncompatible(Type *Ty);
466 /// \brief This returns an integer containing an encoding of all the LLVM
467 /// attributes found in the given attribute bitset. Any change to this encoding
468 /// is a breaking change to bitcode compatibility.
469 uint64_t encodeLLVMAttributesForBitcode(AttributeSet Attrs, unsigned Index);
471 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
472 /// been decoded from the given integer. This function must stay in sync with
473 /// 'encodeLLVMAttributesForBitcode'.
474 /// N.B. This should be used only by the bitcode reader!
475 void decodeLLVMAttributesForBitcode(LLVMContext &C, AttrBuilder &B,
476 uint64_t EncodedAttrs);
478 } // end AttributeFuncs namespace
480 } // end llvm namespace