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