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