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 //===----------------------------------------------------------------------===//
10 // This file contains the simple types necessary to represent the
11 // attributes associated with functions and their calls.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/ADT/ArrayRef.h"
30 /// AttrConst - We use this proxy POD type to allow constructing Attributes
31 /// constants using initializer lists. Do not use this class directly.
34 AttrConst operator | (const AttrConst Attrs) const {
35 AttrConst Res = {v | Attrs.v};
38 AttrConst operator ~ () const {
44 /// Function parameters and results can have attributes to indicate how they
45 /// should be treated by optimizations and code generation. This enumeration
46 /// lists the attributes that can be associated with parameters, function
47 /// results or the function itself.
48 /// @brief Function attributes.
50 /// We declare AttrConst objects that will be used throughout the code and also
51 /// raw uint64_t objects with _i suffix to be used below for other constant
52 /// declarations. This is done to avoid static CTORs and at the same time to
53 /// keep type-safety of Attributes.
54 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
55 const uint64_t name##_i = value; \
56 const AttrConst name = {value};
58 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
59 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
60 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
61 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
62 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
63 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
64 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
65 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
66 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
67 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
68 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
69 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
70 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
71 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
72 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
73 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
74 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
75 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
76 // stored as log2 of alignment with +1 bias
77 // 0 means unaligned different from align 1
78 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
79 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
80 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
82 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
83 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
85 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
86 ///function (3 bits) stored as log2
87 ///of alignment with +1 bias
88 ///0 means unaligned (different from
90 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
91 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
93 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
94 /// often, so lazy binding isn't
96 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
98 #undef DECLARE_LLVM_ATTRIBUTE
100 /// Note that uwtable is about the ABI or the user mandating an entry in the
101 /// unwind table. The nounwind attribute is about an exception passing by the
103 /// In a theoretical system that uses tables for profiling and sjlj for
104 /// exceptions, they would be fully independent. In a normal system that
105 /// uses tables for both, the semantics are:
106 /// nil = Needs an entry because an exception might pass by.
107 /// nounwind = No need for an entry
108 /// uwtable = Needs an entry because the ABI says so and because
109 /// an exception might pass by.
110 /// uwtable + nounwind = Needs an entry because the ABI says so.
112 /// @brief Attributes that only apply to function parameters.
113 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
114 StructRet_i | NoCapture_i};
116 /// @brief Attributes that may be applied to the function itself. These cannot
117 /// be used on return values or function parameters.
118 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
119 ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
120 StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
121 Naked_i | InlineHint_i | StackAlignment_i |
122 UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
124 /// @brief Parameter attributes that do not apply to vararg call arguments.
125 const AttrConst VarArgsIncompatible = {StructRet_i};
127 /// @brief Attributes that are mutually incompatible.
128 const AttrConst MutuallyIncompatible[5] = {
129 {ByVal_i | Nest_i | StructRet_i},
130 {ByVal_i | Nest_i | InReg_i },
132 {ReadNone_i | ReadOnly_i},
133 {NoInline_i | AlwaysInline_i}
136 } // namespace Attribute
138 /// AttributeImpl - The internal representation of the Attributes class. This is
140 class AttributesImpl;
142 /// Attributes - A bitset of attributes.
144 // Currently, we need less than 64 bits.
147 explicit Attributes(AttributesImpl *A);
149 Attributes() : Bits(0) {}
150 explicit Attributes(uint64_t Val) : Bits(Val) {}
151 /*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) {}
154 friend class Attributes;
157 Builder() : Bits(0) {}
158 Builder(const Attributes &A) : Bits(A.Bits) {}
160 void addAddressSafetyAttr();
161 void addAlwaysInlineAttr();
163 void addInlineHintAttr();
167 void addNoAliasAttr();
168 void addNoCaptureAttr();
169 void addNoImplicitFloatAttr();
170 void addNoInlineAttr();
171 void addNonLazyBindAttr();
172 void addNoRedZoneAttr();
173 void addNoReturnAttr();
174 void addNoUnwindAttr();
175 void addOptimizeForSizeAttr();
176 void addReadNoneAttr();
177 void addReadOnlyAttr();
178 void addReturnsTwiceAttr();
180 void addStackProtectAttr();
181 void addStackProtectReqAttr();
182 void addStructRetAttr();
183 void addUWTableAttr();
186 void addAlignmentAttr(unsigned Align);
187 void addStackAlignmentAttr(unsigned Align);
190 /// get - Return a uniquified Attributes object. This takes the uniquified
191 /// value from the Builder and wraps it in the Attributes class.
192 static Attributes get(LLVMContext &Context, Builder &B);
194 // Attribute query methods.
195 // FIXME: StackAlignment & Alignment attributes have no predicate methods.
196 bool hasAttributes() const {
199 bool hasAttributes(const Attributes &A) const;
200 bool hasAddressSafetyAttr() const;
201 bool hasAlignmentAttr() const;
202 bool hasAlwaysInlineAttr() const;
203 bool hasByValAttr() const;
204 bool hasInRegAttr() const;
205 bool hasInlineHintAttr() const;
206 bool hasNakedAttr() const;
207 bool hasNestAttr() const;
208 bool hasNoAliasAttr() const;
209 bool hasNoCaptureAttr() const;
210 bool hasNoImplicitFloatAttr() const;
211 bool hasNoInlineAttr() const;
212 bool hasNonLazyBindAttr() const;
213 bool hasNoRedZoneAttr() const;
214 bool hasNoReturnAttr() const;
215 bool hasNoUnwindAttr() const;
216 bool hasOptimizeForSizeAttr() const;
217 bool hasReadNoneAttr() const;
218 bool hasReadOnlyAttr() const;
219 bool hasReturnsTwiceAttr() const;
220 bool hasSExtAttr() const;
221 bool hasStackAlignmentAttr() const;
222 bool hasStackProtectAttr() const;
223 bool hasStackProtectReqAttr() const;
224 bool hasStructRetAttr() const;
225 bool hasUWTableAttr() const;
226 bool hasZExtAttr() const;
228 /// This returns the alignment field of an attribute as a byte alignment
230 unsigned getAlignment() const;
232 /// This returns the stack alignment field of an attribute as a byte alignment
234 unsigned getStackAlignment() const;
236 bool isEmptyOrSingleton() const;
238 // This is a "safe bool() operator".
239 operator const void *() const { return Bits ? this : 0; }
240 bool operator == (const Attributes &Attrs) const {
241 return Bits == Attrs.Bits;
243 bool operator != (const Attributes &Attrs) const {
244 return Bits != Attrs.Bits;
247 Attributes operator | (const Attributes &Attrs) const;
248 Attributes operator & (const Attributes &Attrs) const;
249 Attributes operator ^ (const Attributes &Attrs) const;
250 Attributes &operator |= (const Attributes &Attrs);
251 Attributes &operator &= (const Attributes &Attrs);
252 Attributes operator ~ () const;
254 uint64_t Raw() const { return Bits; }
256 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
257 /// normally) into the form used internally in Attributes.
258 static Attributes constructAlignmentFromInt(unsigned i) {
259 // Default alignment, allow the target to define how to align it.
261 return Attribute::None;
263 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
264 assert(i <= 0x40000000 && "Alignment too large.");
265 return Attributes((Log2_32(i)+1) << 16);
268 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
269 /// must be a power of 2) into the form used internally in Attributes.
270 static Attributes constructStackAlignmentFromInt(unsigned i) {
271 // Default alignment, allow the target to define how to align it.
273 return Attribute::None;
275 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
276 assert(i <= 0x100 && "Alignment too large.");
277 return Attributes((Log2_32(i)+1) << 26);
280 /// @brief Which attributes cannot be applied to a type.
281 static Attributes typeIncompatible(Type *Ty);
283 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
284 /// encoding of all the LLVM attributes found in the given attribute bitset.
285 /// Any change to this encoding is a breaking change to bitcode compatibility.
286 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
287 // FIXME: It doesn't make sense to store the alignment information as an
288 // expanded out value, we should store it as a log2 value. However, we
289 // can't just change that here without breaking bitcode compatibility. If
290 // this ever becomes a problem in practice, we should introduce new tag
291 // numbers in the bitcode file and have those tags use a more efficiently
292 // encoded alignment field.
294 // Store the alignment in the bitcode as a 16-bit raw value instead of a
295 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
297 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
298 if (Attrs.hasAlignmentAttr())
299 EncodedAttrs |= (1ULL << 16) <<
300 (((Attrs.Raw() & Attribute::Alignment_i) - 1) >> 16);
301 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
305 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
306 /// containing the LLVM attributes that have been decoded from the given
307 /// integer. This function must stay in sync with
308 /// 'encodeLLVMAttributesForBitcode'.
309 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
310 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
311 // the bits above 31 down by 11 bits.
312 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
313 assert((!Alignment || isPowerOf2_32(Alignment)) &&
314 "Alignment must be a power of two.");
316 Attributes Attrs(EncodedAttrs & 0xffff);
318 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
319 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
323 /// getAsString - The set of Attributes set in Attributes is converted to a
324 /// string of equivalent mnemonics. This is, presumably, for writing out the
325 /// mnemonics for the assembly writer.
326 /// @brief Convert attribute bits to text
327 std::string getAsString() const;
330 //===----------------------------------------------------------------------===//
331 // AttributeWithIndex
332 //===----------------------------------------------------------------------===//
334 /// AttributeWithIndex - This is just a pair of values to associate a set of
335 /// attributes with an index.
336 struct AttributeWithIndex {
337 Attributes Attrs; ///< The attributes that are set, or'd together.
338 unsigned Index; ///< Index of the parameter for which the attributes apply.
339 ///< Index 0 is used for return value attributes.
340 ///< Index ~0U is used for function attributes.
342 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
343 AttributeWithIndex P;
350 //===----------------------------------------------------------------------===//
351 // AttrListPtr Smart Pointer
352 //===----------------------------------------------------------------------===//
354 class AttributeListImpl;
356 /// AttrListPtr - This class manages the ref count for the opaque
357 /// AttributeListImpl object and provides accessors for it.
359 /// AttrList - The attributes that we are managing. This can be null
360 /// to represent the empty attributes list.
361 AttributeListImpl *AttrList;
363 AttrListPtr() : AttrList(0) {}
364 AttrListPtr(const AttrListPtr &P);
365 const AttrListPtr &operator=(const AttrListPtr &RHS);
368 //===--------------------------------------------------------------------===//
369 // Attribute List Construction and Mutation
370 //===--------------------------------------------------------------------===//
372 /// get - Return a Attributes list with the specified parameters in it.
373 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
375 /// addAttr - Add the specified attribute at the specified index to this
376 /// attribute list. Since attribute lists are immutable, this
377 /// returns the new list.
378 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
380 /// removeAttr - Remove the specified attribute at the specified index from
381 /// this attribute list. Since attribute lists are immutable, this
382 /// returns the new list.
383 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
385 //===--------------------------------------------------------------------===//
386 // Attribute List Accessors
387 //===--------------------------------------------------------------------===//
388 /// getParamAttributes - The attributes for the specified index are
390 Attributes getParamAttributes(unsigned Idx) const {
391 return getAttributes(Idx);
394 /// getRetAttributes - The attributes for the ret value are
396 Attributes getRetAttributes() const {
397 return getAttributes(0);
400 /// getFnAttributes - The function attributes are returned.
401 Attributes getFnAttributes() const {
402 return getAttributes(~0U);
405 /// paramHasAttr - Return true if the specified parameter index has the
406 /// specified attribute set.
407 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
408 return getAttributes(Idx).hasAttributes(Attr);
411 /// getParamAlignment - Return the alignment for the specified function
413 unsigned getParamAlignment(unsigned Idx) const {
414 return getAttributes(Idx).getAlignment();
417 /// hasAttrSomewhere - Return true if the specified attribute is set for at
418 /// least one parameter or for the return value.
419 bool hasAttrSomewhere(Attributes Attr) const;
421 /// operator==/!= - Provide equality predicates.
422 bool operator==(const AttrListPtr &RHS) const
423 { return AttrList == RHS.AttrList; }
424 bool operator!=(const AttrListPtr &RHS) const
425 { return AttrList != RHS.AttrList; }
429 //===--------------------------------------------------------------------===//
430 // Attribute List Introspection
431 //===--------------------------------------------------------------------===//
433 /// getRawPointer - Return a raw pointer that uniquely identifies this
435 void *getRawPointer() const {
439 // Attributes are stored as a dense set of slots, where there is one
440 // slot for each argument that has an attribute. This allows walking over the
441 // dense set instead of walking the sparse list of attributes.
443 /// isEmpty - Return true if there are no attributes.
445 bool isEmpty() const {
446 return AttrList == 0;
449 /// getNumSlots - Return the number of slots used in this attribute list.
450 /// This is the number of arguments that have an attribute set on them
451 /// (including the function itself).
452 unsigned getNumSlots() const;
454 /// getSlot - Return the AttributeWithIndex at the specified slot. This
455 /// holds a index number plus a set of attributes.
456 const AttributeWithIndex &getSlot(unsigned Slot) const;
459 explicit AttrListPtr(AttributeListImpl *L);
461 /// getAttributes - The attributes for the specified index are
462 /// returned. Attributes for the result are denoted with Idx = 0.
463 Attributes getAttributes(unsigned Idx) const;
467 } // End llvm namespace