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, Constant *Val = 0);
118 static Attribute get(LLVMContext &Context, Constant *Kind, Constant *Val = 0);
120 /// \brief Return a uniquified Attribute object that has the specific
122 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
123 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
125 //===--------------------------------------------------------------------===//
126 // Attribute Accessors
127 //===--------------------------------------------------------------------===//
129 /// \brief Return true if the attribute is present.
130 bool hasAttribute(AttrKind Val) const;
132 /// \brief Return the kind of this attribute: enum or string.
133 Constant *getAttributeKind() const;
135 /// \brief Return the values (if present) of the attribute. This may be a
136 /// ConstantVector to represent a list of values associated with the
138 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);
167 //===----------------------------------------------------------------------===//
169 /// \brief This class manages the ref count for the opaque AttributeSetImpl
170 /// object and provides accessors for it.
178 friend class AttrBuilder;
179 friend class AttributeSetImpl;
181 /// \brief The attributes that we are managing. This can be null to represent
182 /// the empty attributes list.
183 AttributeSetImpl *pImpl;
185 /// \brief The attributes for the specified index are returned.
186 AttributeSetNode *getAttributes(unsigned Idx) const;
188 /// \brief Create an AttributeSet with the specified parameters in it.
189 static AttributeSet get(LLVMContext &C,
190 ArrayRef<std::pair<unsigned, Attribute> > Attrs);
191 static AttributeSet get(LLVMContext &C,
192 ArrayRef<std::pair<unsigned,
193 AttributeSetNode*> > Attrs);
195 static AttributeSet getImpl(LLVMContext &C,
196 ArrayRef<std::pair<unsigned,
197 AttributeSetNode*> > Attrs);
200 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
202 AttributeSet() : pImpl(0) {}
203 AttributeSet(const AttributeSet &P) : pImpl(P.pImpl) {}
204 const AttributeSet &operator=(const AttributeSet &RHS) {
209 //===--------------------------------------------------------------------===//
210 // AttributeSet Construction and Mutation
211 //===--------------------------------------------------------------------===//
213 /// \brief Return an AttributeSet with the specified parameters in it.
214 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
215 static AttributeSet get(LLVMContext &C, unsigned Idx,
216 ArrayRef<Attribute::AttrKind> Kind);
217 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
219 /// \brief Add an attribute to the attribute set at the given index. Since
220 /// attribute sets are immutable, this returns a new set.
221 AttributeSet addAttribute(LLVMContext &C, unsigned Idx,
222 Attribute::AttrKind Attr) const;
224 /// \brief Add attributes to the attribute set at the given index. Since
225 /// attribute sets are immutable, this returns a new set.
226 AttributeSet addAttributes(LLVMContext &C, unsigned Idx,
227 AttributeSet Attrs) const;
229 /// \brief Remove the specified attribute at the specified index from this
230 /// attribute list. Since attribute lists are immutable, this returns the new
232 AttributeSet removeAttribute(LLVMContext &C, unsigned Idx,
233 Attribute::AttrKind Attr) const;
235 /// \brief Remove the specified attributes at the specified index from this
236 /// attribute list. Since attribute lists are immutable, this returns the new
238 AttributeSet removeAttributes(LLVMContext &C, unsigned Idx,
239 AttributeSet Attrs) const;
241 //===--------------------------------------------------------------------===//
242 // AttributeSet Accessors
243 //===--------------------------------------------------------------------===//
245 /// \brief The attributes for the specified index are returned.
246 AttributeSet getParamAttributes(unsigned Idx) const;
248 /// \brief The attributes for the ret value are returned.
249 AttributeSet getRetAttributes() const;
251 /// \brief The function attributes are returned.
252 AttributeSet getFnAttributes() const;
254 /// \brief Return true if the attribute exists at the given index.
255 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
257 /// \brief Return true if attribute exists at the given index.
258 bool hasAttributes(unsigned Index) const;
260 /// \brief Return true if the specified attribute is set for at least one
261 /// parameter or for the return value.
262 bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
264 /// \brief Return the alignment for the specified function parameter.
265 unsigned getParamAlignment(unsigned Idx) const;
267 /// \brief Get the stack alignment.
268 unsigned getStackAlignment(unsigned Index) const;
270 /// \brief Return the attributes at the index as a string.
271 std::string getAsString(unsigned Index) const;
273 typedef ArrayRef<Attribute>::iterator iterator;
275 iterator begin(unsigned Idx) const;
276 iterator end(unsigned Idx) const;
278 /// operator==/!= - Provide equality predicates.
279 bool operator==(const AttributeSet &RHS) const {
280 return pImpl == RHS.pImpl;
282 bool operator!=(const AttributeSet &RHS) const {
283 return pImpl != RHS.pImpl;
286 //===--------------------------------------------------------------------===//
287 // AttributeSet Introspection
288 //===--------------------------------------------------------------------===//
290 // FIXME: Remove this.
291 uint64_t Raw(unsigned Index) const;
293 /// \brief Return a raw pointer that uniquely identifies this attribute list.
294 void *getRawPointer() const {
298 /// \brief Return true if there are no attributes.
299 bool isEmpty() const {
300 return getNumSlots() == 0;
303 /// \brief Return the number of slots used in this attribute list. This is
304 /// the number of arguments that have an attribute set on them (including the
305 /// function itself).
306 unsigned getNumSlots() const;
308 /// \brief Return the index for the given slot.
309 uint64_t getSlotIndex(unsigned Slot) const;
311 /// \brief Return the attributes at the given slot.
312 AttributeSet getSlotAttributes(unsigned Slot) const;
317 //===----------------------------------------------------------------------===//
319 /// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by
321 template<> struct DenseMapInfo<Attribute::AttrKind> {
322 static inline Attribute::AttrKind getEmptyKey() {
323 return Attribute::AttrKindEmptyKey;
325 static inline Attribute::AttrKind getTombstoneKey() {
326 return Attribute::AttrKindTombstoneKey;
328 static unsigned getHashValue(const Attribute::AttrKind &Val) {
331 static bool isEqual(const Attribute::AttrKind &LHS,
332 const Attribute::AttrKind &RHS) {
337 //===----------------------------------------------------------------------===//
339 /// \brief This class is used in conjunction with the Attribute::get method to
340 /// create an Attribute object. The object itself is uniquified. The Builder's
341 /// value, however, is not. So this can be used as a quick way to test for
342 /// equality, presence of attributes, etc.
344 DenseSet<Attribute::AttrKind> Attrs;
345 std::map<std::string, std::string> TargetDepAttrs;
347 uint64_t StackAlignment;
349 AttrBuilder() : Alignment(0), StackAlignment(0) {}
350 explicit AttrBuilder(uint64_t Val) : Alignment(0), StackAlignment(0) {
353 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) {
356 AttrBuilder(AttributeSet AS, unsigned Idx);
360 /// \brief Add an attribute to the builder.
361 AttrBuilder &addAttribute(Attribute::AttrKind Val);
363 /// \brief Add the Attribute object to the builder.
364 AttrBuilder &addAttribute(Attribute A);
366 /// \brief Add the target-dependent attribute to the builder.
367 AttrBuilder &addAttribute(StringRef A, StringRef V);
369 /// \brief Remove an attribute from the builder.
370 AttrBuilder &removeAttribute(Attribute::AttrKind Val);
372 /// \brief Remove the attributes from the builder.
373 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
375 /// \brief Remove the target-dependent attribute to the builder.
376 AttrBuilder &removeAttribute(StringRef 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(AttributeSet A, uint64_t Index) 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 this in 4.0.
446 /// \brief Add the raw value to the internal representation.
447 AttrBuilder &addRawValue(uint64_t Val);
450 namespace AttributeFuncs {
452 /// \brief Which attributes cannot be applied to a type.
453 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
455 } // end AttributeFuncs namespace
457 } // end llvm namespace