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/AttributesImpl.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/ADT/ArrayRef.h"
31 /// AttrConst - We use this proxy POD type to allow constructing Attributes
32 /// constants using initializer lists. Do not use this class directly.
35 AttrConst operator | (const AttrConst Attrs) const {
36 AttrConst Res = {v | Attrs.v};
39 AttrConst operator ~ () const {
45 /// Function parameters and results can have attributes to indicate how they
46 /// should be treated by optimizations and code generation. This enumeration
47 /// lists the attributes that can be associated with parameters, function
48 /// results or the function itself.
49 /// @brief Function attributes.
51 /// We declare AttrConst objects that will be used throughout the code and also
52 /// raw uint64_t objects with _i suffix to be used below for other constant
53 /// declarations. This is done to avoid static CTORs and at the same time to
54 /// keep type-safety of Attributes.
55 #define DECLARE_LLVM_ATTRIBUTE(name, value) \
56 const uint64_t name##_i = value; \
57 const AttrConst name = {value};
59 DECLARE_LLVM_ATTRIBUTE(None,0) ///< No attributes have been set
60 DECLARE_LLVM_ATTRIBUTE(ZExt,1<<0) ///< Zero extended before/after call
61 DECLARE_LLVM_ATTRIBUTE(SExt,1<<1) ///< Sign extended before/after call
62 DECLARE_LLVM_ATTRIBUTE(NoReturn,1<<2) ///< Mark the function as not returning
63 DECLARE_LLVM_ATTRIBUTE(InReg,1<<3) ///< Force argument to be passed in register
64 DECLARE_LLVM_ATTRIBUTE(StructRet,1<<4) ///< Hidden pointer to structure to return
65 DECLARE_LLVM_ATTRIBUTE(NoUnwind,1<<5) ///< Function doesn't unwind stack
66 DECLARE_LLVM_ATTRIBUTE(NoAlias,1<<6) ///< Considered to not alias after call
67 DECLARE_LLVM_ATTRIBUTE(ByVal,1<<7) ///< Pass structure by value
68 DECLARE_LLVM_ATTRIBUTE(Nest,1<<8) ///< Nested function static chain
69 DECLARE_LLVM_ATTRIBUTE(ReadNone,1<<9) ///< Function does not access memory
70 DECLARE_LLVM_ATTRIBUTE(ReadOnly,1<<10) ///< Function only reads from memory
71 DECLARE_LLVM_ATTRIBUTE(NoInline,1<<11) ///< inline=never
72 DECLARE_LLVM_ATTRIBUTE(AlwaysInline,1<<12) ///< inline=always
73 DECLARE_LLVM_ATTRIBUTE(OptimizeForSize,1<<13) ///< opt_size
74 DECLARE_LLVM_ATTRIBUTE(StackProtect,1<<14) ///< Stack protection.
75 DECLARE_LLVM_ATTRIBUTE(StackProtectReq,1<<15) ///< Stack protection required.
76 DECLARE_LLVM_ATTRIBUTE(Alignment,31<<16) ///< Alignment of parameter (5 bits)
77 // stored as log2 of alignment with +1 bias
78 // 0 means unaligned different from align 1
79 DECLARE_LLVM_ATTRIBUTE(NoCapture,1<<21) ///< Function creates no aliases of pointer
80 DECLARE_LLVM_ATTRIBUTE(NoRedZone,1<<22) /// disable redzone
81 DECLARE_LLVM_ATTRIBUTE(NoImplicitFloat,1<<23) /// disable implicit floating point
83 DECLARE_LLVM_ATTRIBUTE(Naked,1<<24) ///< Naked function
84 DECLARE_LLVM_ATTRIBUTE(InlineHint,1<<25) ///< source said inlining was
86 DECLARE_LLVM_ATTRIBUTE(StackAlignment,7<<26) ///< Alignment of stack for
87 ///function (3 bits) stored as log2
88 ///of alignment with +1 bias
89 ///0 means unaligned (different from
91 DECLARE_LLVM_ATTRIBUTE(ReturnsTwice,1<<29) ///< Function can return twice
92 DECLARE_LLVM_ATTRIBUTE(UWTable,1<<30) ///< Function must be in a unwind
94 DECLARE_LLVM_ATTRIBUTE(NonLazyBind,1U<<31) ///< Function is called early and/or
95 /// often, so lazy binding isn't
97 DECLARE_LLVM_ATTRIBUTE(AddressSafety,1ULL<<32) ///< Address safety checking is on.
99 #undef DECLARE_LLVM_ATTRIBUTE
101 /// Note that uwtable is about the ABI or the user mandating an entry in the
102 /// unwind table. The nounwind attribute is about an exception passing by the
104 /// In a theoretical system that uses tables for profiling and sjlj for
105 /// exceptions, they would be fully independent. In a normal system that
106 /// uses tables for both, the semantics are:
107 /// nil = Needs an entry because an exception might pass by.
108 /// nounwind = No need for an entry
109 /// uwtable = Needs an entry because the ABI says so and because
110 /// an exception might pass by.
111 /// uwtable + nounwind = Needs an entry because the ABI says so.
113 /// @brief Attributes that only apply to function parameters.
114 const AttrConst ParameterOnly = {ByVal_i | Nest_i |
115 StructRet_i | NoCapture_i};
117 /// @brief Attributes that may be applied to the function itself. These cannot
118 /// be used on return values or function parameters.
119 const AttrConst FunctionOnly = {NoReturn_i | NoUnwind_i | ReadNone_i |
120 ReadOnly_i | NoInline_i | AlwaysInline_i | OptimizeForSize_i |
121 StackProtect_i | StackProtectReq_i | NoRedZone_i | NoImplicitFloat_i |
122 Naked_i | InlineHint_i | StackAlignment_i |
123 UWTable_i | NonLazyBind_i | ReturnsTwice_i | AddressSafety_i};
125 /// @brief Parameter attributes that do not apply to vararg call arguments.
126 const AttrConst VarArgsIncompatible = {StructRet_i};
128 /// @brief Attributes that are mutually incompatible.
129 const AttrConst MutuallyIncompatible[5] = {
130 {ByVal_i | Nest_i | StructRet_i},
131 {ByVal_i | Nest_i | InReg_i },
133 {ReadNone_i | ReadOnly_i},
134 {NoInline_i | AlwaysInline_i}
137 } // namespace Attribute
139 /// AttributeImpl - The internal representation of the Attributes class. This is
141 class AttributesImpl;
143 /// Attributes - A bitset of attributes.
145 // Currently, we need less than 64 bits.
146 AttributesImpl Attrs;
148 explicit Attributes(AttributesImpl *A);
150 Attributes() : Attrs(0) {}
151 explicit Attributes(uint64_t Val);
152 /*implicit*/ Attributes(Attribute::AttrConst Val);
155 friend class Attributes;
158 Builder() : Bits(0) {}
159 Builder(const Attributes &A) : Bits(A.Raw()) {}
161 void addAddressSafetyAttr();
162 void addAlwaysInlineAttr();
164 void addInlineHintAttr();
168 void addNoAliasAttr();
169 void addNoCaptureAttr();
170 void addNoImplicitFloatAttr();
171 void addNoInlineAttr();
172 void addNonLazyBindAttr();
173 void addNoRedZoneAttr();
174 void addNoReturnAttr();
175 void addNoUnwindAttr();
176 void addOptimizeForSizeAttr();
177 void addReadNoneAttr();
178 void addReadOnlyAttr();
179 void addReturnsTwiceAttr();
181 void addStackProtectAttr();
182 void addStackProtectReqAttr();
183 void addStructRetAttr();
184 void addUWTableAttr();
187 void addAlignmentAttr(unsigned Align);
188 void addStackAlignmentAttr(unsigned Align);
190 void removeAddressSafetyAttr();
191 void removeAlwaysInlineAttr();
192 void removeByValAttr();
193 void removeInlineHintAttr();
194 void removeInRegAttr();
195 void removeNakedAttr();
196 void removeNestAttr();
197 void removeNoAliasAttr();
198 void removeNoCaptureAttr();
199 void removeNoImplicitFloatAttr();
200 void removeNoInlineAttr();
201 void removeNonLazyBindAttr();
202 void removeNoRedZoneAttr();
203 void removeNoReturnAttr();
204 void removeNoUnwindAttr();
205 void removeOptimizeForSizeAttr();
206 void removeReadNoneAttr();
207 void removeReadOnlyAttr();
208 void removeReturnsTwiceAttr();
209 void removeSExtAttr();
210 void removeStackProtectAttr();
211 void removeStackProtectReqAttr();
212 void removeStructRetAttr();
213 void removeUWTableAttr();
214 void removeZExtAttr();
217 /// get - Return a uniquified Attributes object. This takes the uniquified
218 /// value from the Builder and wraps it in the Attributes class.
219 static Attributes get(Builder &B);
220 static Attributes get(LLVMContext &Context, Builder &B);
222 // Attribute query methods.
223 // FIXME: StackAlignment & Alignment attributes have no predicate methods.
224 bool hasAttributes() const {
225 return Attrs.hasAttributes();
227 bool hasAttributes(const Attributes &A) const;
228 bool hasAddressSafetyAttr() const;
229 bool hasAlignmentAttr() const;
230 bool hasAlwaysInlineAttr() const;
231 bool hasByValAttr() const;
232 bool hasInRegAttr() const;
233 bool hasInlineHintAttr() const;
234 bool hasNakedAttr() const;
235 bool hasNestAttr() const;
236 bool hasNoAliasAttr() const;
237 bool hasNoCaptureAttr() const;
238 bool hasNoImplicitFloatAttr() const;
239 bool hasNoInlineAttr() const;
240 bool hasNonLazyBindAttr() const;
241 bool hasNoRedZoneAttr() const;
242 bool hasNoReturnAttr() const;
243 bool hasNoUnwindAttr() const;
244 bool hasOptimizeForSizeAttr() const;
245 bool hasReadNoneAttr() const;
246 bool hasReadOnlyAttr() const;
247 bool hasReturnsTwiceAttr() const;
248 bool hasSExtAttr() const;
249 bool hasStackAlignmentAttr() const;
250 bool hasStackProtectAttr() const;
251 bool hasStackProtectReqAttr() const;
252 bool hasStructRetAttr() const;
253 bool hasUWTableAttr() const;
254 bool hasZExtAttr() const;
256 /// This returns the alignment field of an attribute as a byte alignment
258 unsigned getAlignment() const;
260 /// This returns the stack alignment field of an attribute as a byte alignment
262 unsigned getStackAlignment() const;
264 bool isEmptyOrSingleton() const;
266 // This is a "safe bool() operator".
267 operator const void *() const { return Attrs.Bits ? this : 0; }
268 bool operator == (const Attributes &A) const {
269 return Attrs.Bits == A.Attrs.Bits;
271 bool operator != (const Attributes &A) const {
272 return Attrs.Bits != A.Attrs.Bits;
275 Attributes operator | (const Attributes &A) const;
276 Attributes operator & (const Attributes &A) const;
277 Attributes operator ^ (const Attributes &A) const;
278 Attributes &operator |= (const Attributes &A);
279 Attributes &operator &= (const Attributes &A);
280 Attributes operator ~ () const;
282 uint64_t Raw() const;
284 /// constructAlignmentFromInt - This turns an int alignment (a power of 2,
285 /// normally) into the form used internally in Attributes.
286 static Attributes constructAlignmentFromInt(unsigned i) {
287 // Default alignment, allow the target to define how to align it.
289 return Attribute::None;
291 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
292 assert(i <= 0x40000000 && "Alignment too large.");
293 return Attributes((Log2_32(i)+1) << 16);
296 /// constructStackAlignmentFromInt - This turns an int stack alignment (which
297 /// must be a power of 2) into the form used internally in Attributes.
298 static Attributes constructStackAlignmentFromInt(unsigned i) {
299 // Default alignment, allow the target to define how to align it.
301 return Attribute::None;
303 assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
304 assert(i <= 0x100 && "Alignment too large.");
305 return Attributes((Log2_32(i)+1) << 26);
308 /// @brief Which attributes cannot be applied to a type.
309 static Attributes typeIncompatible(Type *Ty);
311 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
312 /// encoding of all the LLVM attributes found in the given attribute bitset.
313 /// Any change to this encoding is a breaking change to bitcode compatibility.
314 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
315 // FIXME: It doesn't make sense to store the alignment information as an
316 // expanded out value, we should store it as a log2 value. However, we
317 // can't just change that here without breaking bitcode compatibility. If
318 // this ever becomes a problem in practice, we should introduce new tag
319 // numbers in the bitcode file and have those tags use a more efficiently
320 // encoded alignment field.
322 // Store the alignment in the bitcode as a 16-bit raw value instead of a
323 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
325 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
326 if (Attrs.hasAlignmentAttr())
327 EncodedAttrs |= (1ULL << 16) <<
328 (((Attrs.Raw() & Attribute::Alignment_i) - 1) >> 16);
329 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
333 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
334 /// containing the LLVM attributes that have been decoded from the given
335 /// integer. This function must stay in sync with
336 /// 'encodeLLVMAttributesForBitcode'.
337 static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
338 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
339 // the bits above 31 down by 11 bits.
340 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
341 assert((!Alignment || isPowerOf2_32(Alignment)) &&
342 "Alignment must be a power of two.");
344 Attributes Attrs(EncodedAttrs & 0xffff);
346 Attrs |= Attributes::constructAlignmentFromInt(Alignment);
347 Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
351 /// getAsString - The set of Attributes set in Attributes is converted to a
352 /// string of equivalent mnemonics. This is, presumably, for writing out the
353 /// mnemonics for the assembly writer.
354 /// @brief Convert attribute bits to text
355 std::string getAsString() const;
358 //===----------------------------------------------------------------------===//
359 // AttributeWithIndex
360 //===----------------------------------------------------------------------===//
362 /// AttributeWithIndex - This is just a pair of values to associate a set of
363 /// attributes with an index.
364 struct AttributeWithIndex {
365 Attributes Attrs; ///< The attributes that are set, or'd together.
366 unsigned Index; ///< Index of the parameter for which the attributes apply.
367 ///< Index 0 is used for return value attributes.
368 ///< Index ~0U is used for function attributes.
370 static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
371 AttributeWithIndex P;
378 //===----------------------------------------------------------------------===//
379 // AttrListPtr Smart Pointer
380 //===----------------------------------------------------------------------===//
382 class AttributeListImpl;
384 /// AttrListPtr - This class manages the ref count for the opaque
385 /// AttributeListImpl object and provides accessors for it.
387 /// AttrList - The attributes that we are managing. This can be null
388 /// to represent the empty attributes list.
389 AttributeListImpl *AttrList;
391 AttrListPtr() : AttrList(0) {}
392 AttrListPtr(const AttrListPtr &P);
393 const AttrListPtr &operator=(const AttrListPtr &RHS);
396 //===--------------------------------------------------------------------===//
397 // Attribute List Construction and Mutation
398 //===--------------------------------------------------------------------===//
400 /// get - Return a Attributes list with the specified parameters in it.
401 static AttrListPtr get(ArrayRef<AttributeWithIndex> Attrs);
403 /// addAttr - Add the specified attribute at the specified index to this
404 /// attribute list. Since attribute lists are immutable, this
405 /// returns the new list.
406 AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
408 /// removeAttr - Remove the specified attribute at the specified index from
409 /// this attribute list. Since attribute lists are immutable, this
410 /// returns the new list.
411 AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
413 //===--------------------------------------------------------------------===//
414 // Attribute List Accessors
415 //===--------------------------------------------------------------------===//
416 /// getParamAttributes - The attributes for the specified index are
418 Attributes getParamAttributes(unsigned Idx) const {
419 return getAttributes(Idx);
422 /// getRetAttributes - The attributes for the ret value are
424 Attributes getRetAttributes() const {
425 return getAttributes(0);
428 /// getFnAttributes - The function attributes are returned.
429 Attributes getFnAttributes() const {
430 return getAttributes(~0U);
433 /// paramHasAttr - Return true if the specified parameter index has the
434 /// specified attribute set.
435 bool paramHasAttr(unsigned Idx, Attributes Attr) const {
436 return getAttributes(Idx).hasAttributes(Attr);
439 /// getParamAlignment - Return the alignment for the specified function
441 unsigned getParamAlignment(unsigned Idx) const {
442 return getAttributes(Idx).getAlignment();
445 /// hasAttrSomewhere - Return true if the specified attribute is set for at
446 /// least one parameter or for the return value.
447 bool hasAttrSomewhere(Attributes Attr) const;
449 /// operator==/!= - Provide equality predicates.
450 bool operator==(const AttrListPtr &RHS) const
451 { return AttrList == RHS.AttrList; }
452 bool operator!=(const AttrListPtr &RHS) const
453 { return AttrList != RHS.AttrList; }
457 //===--------------------------------------------------------------------===//
458 // Attribute List Introspection
459 //===--------------------------------------------------------------------===//
461 /// getRawPointer - Return a raw pointer that uniquely identifies this
463 void *getRawPointer() const {
467 // Attributes are stored as a dense set of slots, where there is one
468 // slot for each argument that has an attribute. This allows walking over the
469 // dense set instead of walking the sparse list of attributes.
471 /// isEmpty - Return true if there are no attributes.
473 bool isEmpty() const {
474 return AttrList == 0;
477 /// getNumSlots - Return the number of slots used in this attribute list.
478 /// This is the number of arguments that have an attribute set on them
479 /// (including the function itself).
480 unsigned getNumSlots() const;
482 /// getSlot - Return the AttributeWithIndex at the specified slot. This
483 /// holds a index number plus a set of attributes.
484 const AttributeWithIndex &getSlot(unsigned Slot) const;
487 explicit AttrListPtr(AttributeListImpl *L);
489 /// getAttributes - The attributes for the specified index are
490 /// returned. Attributes for the result are denoted with Idx = 0.
491 Attributes getAttributes(unsigned Idx) const;
495 } // End llvm namespace