Attributes.h revision 6f78fbbc630d2b86fb752574f5ad74473f57dfb1
1//===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9/// 10/// \file 11/// \brief This file contains the simple types necessary to represent the 12/// attributes associated with functions and their calls. 13/// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_ATTRIBUTES_H 17#define LLVM_ATTRIBUTES_H 18 19#include "llvm/ADT/ArrayRef.h" 20#include "llvm/ADT/DenseSet.h" 21#include "llvm/Support/MathExtras.h" 22#include <cassert> 23#include <string> 24 25namespace llvm { 26 27class AttrBuilder; 28class AttributeImpl; 29class LLVMContext; 30class Type; 31 32//===----------------------------------------------------------------------===// 33/// \class 34/// \brief Functions, function parameters, and return types can have attributes 35/// to indicate how they should be treated by optimizations and code 36/// generation. This class represents one of those attributes. It's light-weight 37/// and should be passed around by-value. 38class Attribute { 39public: 40 /// This enumeration lists the attributes that can be associated with 41 /// parameters, function results or the function itself. 42 /// 43 /// Note: uwtable is about the ABI or the user mandating an entry in the 44 /// unwind table. The nounwind attribute is about an exception passing by the 45 /// function. 46 /// 47 /// In a theoretical system that uses tables for profiling and sjlj for 48 /// exceptions, they would be fully independent. In a normal system that uses 49 /// tables for both, the semantics are: 50 /// 51 /// nil = Needs an entry because an exception might pass by. 52 /// nounwind = No need for an entry 53 /// uwtable = Needs an entry because the ABI says so and because 54 /// an exception might pass by. 55 /// uwtable + nounwind = Needs an entry because the ABI says so. 56 57 enum AttrKind { 58 // IR-Level Attributes 59 None, ///< No attributes have been set 60 AddressSafety, ///< Address safety checking is on. 61 Alignment, ///< Alignment of parameter (5 bits) 62 ///< stored as log2 of alignment with +1 bias 63 ///< 0 means unaligned (different from align(1)) 64 AlwaysInline, ///< inline=always 65 ByVal, ///< Pass structure by value 66 InlineHint, ///< Source said inlining was desirable 67 InReg, ///< Force argument to be passed in register 68 MinSize, ///< Function must be optimized for size first 69 Naked, ///< Naked function 70 Nest, ///< Nested function static chain 71 NoAlias, ///< Considered to not alias after call 72 NoCapture, ///< Function creates no aliases of pointer 73 NoDuplicate, ///< Call cannot be duplicated 74 NoImplicitFloat, ///< Disable implicit floating point insts 75 NoInline, ///< inline=never 76 NonLazyBind, ///< Function is called early and/or 77 ///< often, so lazy binding isn't worthwhile 78 NoRedZone, ///< Disable redzone 79 NoReturn, ///< Mark the function as not returning 80 NoUnwind, ///< Function doesn't unwind stack 81 OptimizeForSize, ///< opt_size 82 ReadNone, ///< Function does not access memory 83 ReadOnly, ///< Function only reads from memory 84 ReturnsTwice, ///< Function can return twice 85 SExt, ///< Sign extended before/after call 86 StackAlignment, ///< Alignment of stack for function (3 bits) 87 ///< stored as log2 of alignment with +1 bias 0 88 ///< means unaligned (different from 89 ///< alignstack=(1)) 90 StackProtect, ///< Stack protection. 91 StackProtectReq, ///< Stack protection required. 92 StructRet, ///< Hidden pointer to structure to return 93 UWTable, ///< Function must be in a unwind table 94 ZExt, ///< Zero extended before/after call 95 96 EndAttrKinds, ///< Sentinal value useful for loops 97 98 AttrKindEmptyKey, ///< Empty key value for DenseMapInfo 99 AttrKindTombstoneKey ///< Tombstone key value for DenseMapInfo 100 }; 101private: 102 AttributeImpl *pImpl; 103 Attribute(AttributeImpl *A) : pImpl(A) {} 104public: 105 Attribute() : pImpl(0) {} 106 107 /// \brief Return a uniquified Attribute object. This takes the uniquified 108 /// value from the Builder and wraps it in the Attribute class. 109 static Attribute get(LLVMContext &Context, ArrayRef<AttrKind> Vals); 110 static Attribute get(LLVMContext &Context, AttrBuilder &B); 111 112 /// \brief Return true if the attribute is present. 113 bool hasAttribute(AttrKind Val) const; 114 115 /// \brief Return true if attributes exist 116 bool hasAttributes() const; 117 118 /// \brief Returns the alignment field of an attribute as a byte alignment 119 /// value. 120 unsigned getAlignment() const; 121 122 /// \brief Set the alignment field of an attribute. 123 void setAlignment(unsigned Align); 124 125 /// \brief Returns the stack alignment field of an attribute as a byte 126 /// alignment value. 127 unsigned getStackAlignment() const; 128 129 /// \brief Set the stack alignment field of an attribute. 130 void setStackAlignment(unsigned Align); 131 132 /// \brief Equality and non-equality query methods. 133 bool operator==(AttrKind K) const; 134 bool operator!=(AttrKind K) const; 135 136 // FIXME: Remove these 'operator' methods. 137 bool operator==(const Attribute &A) const { 138 return pImpl == A.pImpl; 139 } 140 bool operator!=(const Attribute &A) const { 141 return pImpl != A.pImpl; 142 } 143 144 uint64_t getBitMask() const; 145 146 /// \brief Which attributes cannot be applied to a type. 147 static Attribute typeIncompatible(Type *Ty); 148 149 /// \brief This returns an integer containing an encoding of all the LLVM 150 /// attributes found in the given attribute bitset. Any change to this 151 /// encoding is a breaking change to bitcode compatibility. 152 static uint64_t encodeLLVMAttributesForBitcode(Attribute Attrs); 153 154 /// \brief This returns an attribute bitset containing the LLVM attributes 155 /// that have been decoded from the given integer. This function must stay in 156 /// sync with 'encodeLLVMAttributesForBitcode'. 157 static Attribute decodeLLVMAttributesForBitcode(LLVMContext &C, 158 uint64_t EncodedAttrs); 159 160 /// \brief The Attribute is converted to a string of equivalent mnemonic. This 161 /// is, presumably, for writing out the mnemonics for the assembly writer. 162 std::string getAsString() const; 163}; 164 165//===----------------------------------------------------------------------===// 166/// \class 167/// \brief Provide DenseMapInfo for Attribute::AttrKinds. This is used by 168/// AttrBuilder. 169template<> struct DenseMapInfo<Attribute::AttrKind> { 170 static inline Attribute::AttrKind getEmptyKey() { 171 return Attribute::AttrKindEmptyKey; 172 } 173 static inline Attribute::AttrKind getTombstoneKey() { 174 return Attribute::AttrKindTombstoneKey; 175 } 176 static unsigned getHashValue(const Attribute::AttrKind &Val) { 177 return Val * 37U; 178 } 179 static bool isEqual(const Attribute::AttrKind &LHS, 180 const Attribute::AttrKind &RHS) { 181 return LHS == RHS; 182 } 183}; 184 185//===----------------------------------------------------------------------===// 186/// \class 187/// \brief This class is used in conjunction with the Attribute::get method to 188/// create an Attribute object. The object itself is uniquified. The Builder's 189/// value, however, is not. So this can be used as a quick way to test for 190/// equality, presence of attributes, etc. 191class AttrBuilder { 192 DenseSet<Attribute::AttrKind> Attrs; 193 uint64_t Alignment; 194 uint64_t StackAlignment; 195public: 196 AttrBuilder() : Alignment(0), StackAlignment(0) {} 197 explicit AttrBuilder(uint64_t B) : Alignment(0), StackAlignment(0) { 198 addRawValue(B); 199 } 200 AttrBuilder(const Attribute &A) : Alignment(0), StackAlignment(0) { 201 addAttributes(A); 202 } 203 204 void clear(); 205 206 /// \brief Add an attribute to the builder. 207 AttrBuilder &addAttribute(Attribute::AttrKind Val); 208 209 /// \brief Remove an attribute from the builder. 210 AttrBuilder &removeAttribute(Attribute::AttrKind Val); 211 212 /// \brief Add the attributes from A to the builder. 213 AttrBuilder &addAttributes(const Attribute &A); 214 215 /// \brief Remove the attributes from A from the builder. 216 AttrBuilder &removeAttributes(const Attribute &A); 217 218 /// \brief Return true if the builder has the specified attribute. 219 bool contains(Attribute::AttrKind A) const; 220 221 /// \brief Return true if the builder has IR-level attributes. 222 bool hasAttributes() const; 223 224 /// \brief Return true if the builder has any attribute that's in the 225 /// specified attribute. 226 bool hasAttributes(const Attribute &A) const; 227 228 /// \brief Return true if the builder has an alignment attribute. 229 bool hasAlignmentAttr() const; 230 231 /// \brief Retrieve the alignment attribute, if it exists. 232 uint64_t getAlignment() const { return Alignment; } 233 234 /// \brief Retrieve the stack alignment attribute, if it exists. 235 uint64_t getStackAlignment() const { return StackAlignment; } 236 237 /// \brief This turns an int alignment (which must be a power of 2) into the 238 /// form used internally in Attribute. 239 AttrBuilder &addAlignmentAttr(unsigned Align); 240 241 /// \brief This turns an int stack alignment (which must be a power of 2) into 242 /// the form used internally in Attribute. 243 AttrBuilder &addStackAlignmentAttr(unsigned Align); 244 245 typedef DenseSet<Attribute::AttrKind>::iterator iterator; 246 typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator; 247 248 iterator begin() { return Attrs.begin(); } 249 iterator end() { return Attrs.end(); } 250 251 const_iterator begin() const { return Attrs.begin(); } 252 const_iterator end() const { return Attrs.end(); } 253 254 /// \brief Add the raw value to the internal representation. 255 /// 256 /// N.B. This should be used ONLY for decoding LLVM bitcode! 257 AttrBuilder &addRawValue(uint64_t Val); 258 259 /// \brief Remove attributes that are used on functions only. 260 void removeFunctionOnlyAttrs() { 261 removeAttribute(Attribute::NoReturn) 262 .removeAttribute(Attribute::NoUnwind) 263 .removeAttribute(Attribute::ReadNone) 264 .removeAttribute(Attribute::ReadOnly) 265 .removeAttribute(Attribute::NoInline) 266 .removeAttribute(Attribute::AlwaysInline) 267 .removeAttribute(Attribute::OptimizeForSize) 268 .removeAttribute(Attribute::StackProtect) 269 .removeAttribute(Attribute::StackProtectReq) 270 .removeAttribute(Attribute::NoRedZone) 271 .removeAttribute(Attribute::NoImplicitFloat) 272 .removeAttribute(Attribute::Naked) 273 .removeAttribute(Attribute::InlineHint) 274 .removeAttribute(Attribute::StackAlignment) 275 .removeAttribute(Attribute::UWTable) 276 .removeAttribute(Attribute::NonLazyBind) 277 .removeAttribute(Attribute::ReturnsTwice) 278 .removeAttribute(Attribute::AddressSafety) 279 .removeAttribute(Attribute::MinSize) 280 .removeAttribute(Attribute::NoDuplicate); 281 } 282 283 uint64_t getBitMask() const; 284 285 bool operator==(const AttrBuilder &B); 286 bool operator!=(const AttrBuilder &B) { 287 return !(*this == B); 288 } 289}; 290 291//===----------------------------------------------------------------------===// 292/// \class 293/// \brief This is just a pair of values to associate a set of attributes with 294/// an index. 295struct AttributeWithIndex { 296 Attribute Attrs; ///< The attributes that are set, or'd together. 297 unsigned Index; ///< Index of the parameter for which the attributes apply. 298 ///< Index 0 is used for return value attributes. 299 ///< Index ~0U is used for function attributes. 300 301 static AttributeWithIndex get(LLVMContext &C, unsigned Idx, 302 ArrayRef<Attribute::AttrKind> Attrs) { 303 return get(Idx, Attribute::get(C, Attrs)); 304 } 305 static AttributeWithIndex get(unsigned Idx, Attribute Attrs) { 306 AttributeWithIndex P; 307 P.Index = Idx; 308 P.Attrs = Attrs; 309 return P; 310 } 311}; 312 313//===----------------------------------------------------------------------===// 314// AttributeSet Smart Pointer 315//===----------------------------------------------------------------------===// 316 317class AttributeSetImpl; 318 319//===----------------------------------------------------------------------===// 320/// \class 321/// \brief This class manages the ref count for the opaque AttributeSetImpl 322/// object and provides accessors for it. 323class AttributeSet { 324public: 325 enum AttrIndex { 326 ReturnIndex = 0U, 327 FunctionIndex = ~0U 328 }; 329private: 330 /// \brief The attributes that we are managing. This can be null to represent 331 /// the empty attributes list. 332 AttributeSetImpl *AttrList; 333 334 /// \brief The attributes for the specified index are returned. Attributes 335 /// for the result are denoted with Idx = 0. 336 Attribute getAttributes(unsigned Idx) const; 337 338 explicit AttributeSet(AttributeSetImpl *LI) : AttrList(LI) {} 339public: 340 AttributeSet() : AttrList(0) {} 341 AttributeSet(const AttributeSet &P) : AttrList(P.AttrList) {} 342 const AttributeSet &operator=(const AttributeSet &RHS); 343 344 //===--------------------------------------------------------------------===// 345 // Attribute List Construction and Mutation 346 //===--------------------------------------------------------------------===// 347 348 /// \brief Return an AttributeSet with the specified parameters in it. 349 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs); 350 static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B); 351 352 /// \brief Add the specified attribute at the specified index to this 353 /// attribute list. Since attribute lists are immutable, this returns the new 354 /// list. 355 AttributeSet addAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const; 356 357 /// \brief Remove the specified attribute at the specified index from this 358 /// attribute list. Since attribute lists are immutable, this returns the new 359 /// list. 360 AttributeSet removeAttr(LLVMContext &C, unsigned Idx, Attribute Attrs) const; 361 362 //===--------------------------------------------------------------------===// 363 // Attribute List Accessors 364 //===--------------------------------------------------------------------===// 365 366 /// \brief The attributes for the specified index are returned. 367 Attribute getParamAttributes(unsigned Idx) const { 368 return getAttributes(Idx); 369 } 370 371 /// \brief The attributes for the ret value are returned. 372 Attribute getRetAttributes() const { 373 return getAttributes(ReturnIndex); 374 } 375 376 /// \brief The function attributes are returned. 377 Attribute getFnAttributes() const { 378 return getAttributes(FunctionIndex); 379 } 380 381 /// \brief Return the alignment for the specified function parameter. 382 unsigned getParamAlignment(unsigned Idx) const { 383 return getAttributes(Idx).getAlignment(); 384 } 385 386 /// \brief Return true if the attribute exists at the given index. 387 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const; 388 389 /// \brief Return true if attribute exists at the given index. 390 bool hasAttributes(unsigned Index) const; 391 392 /// \brief Get the stack alignment. 393 unsigned getStackAlignment(unsigned Index) const; 394 395 /// \brief Return the attributes at the index as a string. 396 std::string getAsString(unsigned Index) const; 397 398 uint64_t getBitMask(unsigned Index) const; 399 400 /// \brief Return true if the specified attribute is set for at least one 401 /// parameter or for the return value. 402 bool hasAttrSomewhere(Attribute::AttrKind Attr) const; 403 404 /// operator==/!= - Provide equality predicates. 405 bool operator==(const AttributeSet &RHS) const { 406 return AttrList == RHS.AttrList; 407 } 408 bool operator!=(const AttributeSet &RHS) const { 409 return AttrList != RHS.AttrList; 410 } 411 412 //===--------------------------------------------------------------------===// 413 // Attribute List Introspection 414 //===--------------------------------------------------------------------===// 415 416 /// \brief Return a raw pointer that uniquely identifies this attribute list. 417 void *getRawPointer() const { 418 return AttrList; 419 } 420 421 // Attributes are stored as a dense set of slots, where there is one slot for 422 // each argument that has an attribute. This allows walking over the dense 423 // set instead of walking the sparse list of attributes. 424 425 /// \brief Return true if there are no attributes. 426 bool isEmpty() const { 427 return AttrList == 0; 428 } 429 430 /// \brief Return the number of slots used in this attribute list. This is 431 /// the number of arguments that have an attribute set on them (including the 432 /// function itself). 433 unsigned getNumSlots() const; 434 435 /// \brief Return the AttributeWithIndex at the specified slot. This holds a 436 /// index number plus a set of attributes. 437 const AttributeWithIndex &getSlot(unsigned Slot) const; 438 439 void dump() const; 440}; 441 442} // end llvm namespace 443 444#endif 445