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