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 28 namespace llvm { 29 30 class AttrBuilder; 31 class AttributeImpl; 32 class AttributeSetImpl; 33 class AttributeSetNode; 34 class Constant; 35 template<typename T> struct DenseMapInfo; 36 class LLVMContext; 37 class 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. 45 class Attribute { 46 public: 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 InAlloca, ///< Pass structure in an alloca 75 Cold, ///< Marks function as being in a cold path. 76 Convergent, ///< Can only be moved to control-equivalent blocks 77 InlineHint, ///< Source said inlining was desirable 78 InReg, ///< Force argument to be passed in register 79 JumpTable, ///< Build jump-instruction tables and replace refs. 80 MinSize, ///< Function must be optimized for size first 81 Naked, ///< Naked function 82 Nest, ///< Nested function static chain 83 NoAlias, ///< Considered to not alias after call 84 NoBuiltin, ///< Callee isn't recognized as a builtin 85 NoCapture, ///< Function creates no aliases of pointer 86 NoDuplicate, ///< Call cannot be duplicated 87 NoImplicitFloat, ///< Disable implicit floating point insts 88 NoInline, ///< inline=never 89 NonLazyBind, ///< Function is called early and/or 90 ///< often, so lazy binding isn't worthwhile 91 NonNull, ///< Pointer is known to be not null 92 Dereferenceable, ///< Pointer is known to be dereferenceable 93 DereferenceableOrNull, ///< Pointer is either null or dereferenceable 94 NoRedZone, ///< Disable redzone 95 NoReturn, ///< Mark the function as not returning 96 NoUnwind, ///< Function doesn't unwind stack 97 OptimizeForSize, ///< opt_size 98 OptimizeNone, ///< Function must not be optimized. 99 ReadNone, ///< Function does not access memory 100 ReadOnly, ///< Function only reads from memory 101 ArgMemOnly, ///< Funciton can access memory only using pointers 102 ///< based on its arguments. 103 Returned, ///< Return value is always equal to this argument 104 ReturnsTwice, ///< Function can return twice 105 SExt, ///< Sign extended before/after call 106 StackAlignment, ///< Alignment of stack for function (3 bits) 107 ///< stored as log2 of alignment with +1 bias 0 108 ///< means unaligned (different from 109 ///< alignstack=(1)) 110 StackProtect, ///< Stack protection. 111 StackProtectReq, ///< Stack protection required. 112 StackProtectStrong, ///< Strong Stack protection. 113 SafeStack, ///< Safe Stack protection. 114 StructRet, ///< Hidden pointer to structure to return 115 SanitizeAddress, ///< AddressSanitizer is on. 116 SanitizeThread, ///< ThreadSanitizer is on. 117 SanitizeMemory, ///< MemorySanitizer is on. 118 UWTable, ///< Function must be in a unwind table 119 ZExt, ///< Zero extended before/after call 120 121 EndAttrKinds ///< Sentinal value useful for loops 122 }; 123 private: 124 AttributeImpl *pImpl; Attribute(AttributeImpl * A)125 Attribute(AttributeImpl *A) : pImpl(A) {} 126 public: Attribute()127 Attribute() : pImpl(nullptr) {} 128 129 //===--------------------------------------------------------------------===// 130 // Attribute Construction 131 //===--------------------------------------------------------------------===// 132 133 /// \brief Return a uniquified Attribute object. 134 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0); 135 static Attribute get(LLVMContext &Context, StringRef Kind, 136 StringRef Val = StringRef()); 137 138 /// \brief Return a uniquified Attribute object that has the specific 139 /// alignment set. 140 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align); 141 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align); 142 static Attribute getWithDereferenceableBytes(LLVMContext &Context, 143 uint64_t Bytes); 144 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, 145 uint64_t Bytes); 146 147 //===--------------------------------------------------------------------===// 148 // Attribute Accessors 149 //===--------------------------------------------------------------------===// 150 151 /// \brief Return true if the attribute is an Attribute::AttrKind type. 152 bool isEnumAttribute() const; 153 154 /// \brief Return true if the attribute is an integer attribute. 155 bool isIntAttribute() const; 156 157 /// \brief Return true if the attribute is a string (target-dependent) 158 /// attribute. 159 bool isStringAttribute() const; 160 161 /// \brief Return true if the attribute is present. 162 bool hasAttribute(AttrKind Val) const; 163 164 /// \brief Return true if the target-dependent attribute is present. 165 bool hasAttribute(StringRef Val) const; 166 167 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This 168 /// requires the attribute to be an enum or alignment attribute. 169 Attribute::AttrKind getKindAsEnum() const; 170 171 /// \brief Return the attribute's value as an integer. This requires that the 172 /// attribute be an alignment attribute. 173 uint64_t getValueAsInt() const; 174 175 /// \brief Return the attribute's kind as a string. This requires the 176 /// attribute to be a string attribute. 177 StringRef getKindAsString() const; 178 179 /// \brief Return the attribute's value as a string. This requires the 180 /// attribute to be a string attribute. 181 StringRef getValueAsString() const; 182 183 /// \brief Returns the alignment field of an attribute as a byte alignment 184 /// value. 185 unsigned getAlignment() const; 186 187 /// \brief Returns the stack alignment field of an attribute as a byte 188 /// alignment value. 189 unsigned getStackAlignment() const; 190 191 /// \brief Returns the number of dereferenceable bytes from the 192 /// dereferenceable attribute (or zero if unknown). 193 uint64_t getDereferenceableBytes() const; 194 195 /// \brief Returns the number of dereferenceable_or_null bytes from the 196 /// dereferenceable_or_null attribute (or zero if unknown). 197 uint64_t getDereferenceableOrNullBytes() const; 198 199 /// \brief The Attribute is converted to a string of equivalent mnemonic. This 200 /// is, presumably, for writing out the mnemonics for the assembly writer. 201 std::string getAsString(bool InAttrGrp = false) const; 202 203 /// \brief Equality and non-equality operators. 204 bool operator==(Attribute A) const { return pImpl == A.pImpl; } 205 bool operator!=(Attribute A) const { return pImpl != A.pImpl; } 206 207 /// \brief Less-than operator. Useful for sorting the attributes list. 208 bool operator<(Attribute A) const; 209 Profile(FoldingSetNodeID & ID)210 void Profile(FoldingSetNodeID &ID) const { 211 ID.AddPointer(pImpl); 212 } 213 }; 214 215 //===----------------------------------------------------------------------===// 216 /// \class 217 /// \brief This class holds the attributes for a function, its return value, and 218 /// its parameters. You access the attributes for each of them via an index into 219 /// the AttributeSet object. The function attributes are at index 220 /// `AttributeSet::FunctionIndex', the return value is at index 221 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at 222 /// index `1'. 223 class AttributeSet { 224 public: 225 enum AttrIndex : unsigned { 226 ReturnIndex = 0U, 227 FunctionIndex = ~0U 228 }; 229 private: 230 friend class AttrBuilder; 231 friend class AttributeSetImpl; 232 template <typename Ty> friend struct DenseMapInfo; 233 234 /// \brief The attributes that we are managing. This can be null to represent 235 /// the empty attributes list. 236 AttributeSetImpl *pImpl; 237 238 /// \brief The attributes for the specified index are returned. 239 AttributeSetNode *getAttributes(unsigned Index) const; 240 241 /// \brief Create an AttributeSet with the specified parameters in it. 242 static AttributeSet get(LLVMContext &C, 243 ArrayRef<std::pair<unsigned, Attribute> > Attrs); 244 static AttributeSet get(LLVMContext &C, 245 ArrayRef<std::pair<unsigned, 246 AttributeSetNode*> > Attrs); 247 248 static AttributeSet getImpl(LLVMContext &C, 249 ArrayRef<std::pair<unsigned, 250 AttributeSetNode*> > Attrs); 251 252 AttributeSet(AttributeSetImpl * LI)253 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {} 254 public: AttributeSet()255 AttributeSet() : pImpl(nullptr) {} 256 257 //===--------------------------------------------------------------------===// 258 // AttributeSet Construction and Mutation 259 //===--------------------------------------------------------------------===// 260 261 /// \brief Return an AttributeSet with the specified parameters in it. 262 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs); 263 static AttributeSet get(LLVMContext &C, unsigned Index, 264 ArrayRef<Attribute::AttrKind> Kind); 265 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B); 266 267 /// \brief Add an attribute to the attribute set at the given index. Because 268 /// attribute sets are immutable, this returns a new set. 269 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 270 Attribute::AttrKind Attr) const; 271 272 /// \brief Add an attribute to the attribute set at the given index. Because 273 /// attribute sets are immutable, this returns a new set. 274 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 275 StringRef Kind) const; 276 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 277 StringRef Kind, StringRef Value) const; 278 279 /// \brief Add attributes to the attribute set at the given index. Because 280 /// attribute sets are immutable, this returns a new set. 281 AttributeSet addAttributes(LLVMContext &C, unsigned Index, 282 AttributeSet Attrs) const; 283 284 /// \brief Remove the specified attribute at the specified index from this 285 /// attribute list. Because attribute lists are immutable, this returns the 286 /// new list. 287 AttributeSet removeAttribute(LLVMContext &C, unsigned Index, 288 Attribute::AttrKind Attr) const; 289 290 /// \brief Remove the specified attributes at the specified index from this 291 /// attribute list. Because attribute lists are immutable, this returns the 292 /// new list. 293 AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 294 AttributeSet Attrs) const; 295 296 /// \brief Remove the specified attributes at the specified index from this 297 /// attribute list. Because attribute lists are immutable, this returns the 298 /// new list. 299 AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 300 const AttrBuilder &Attrs) const; 301 302 /// \brief Add the dereferenceable attribute to the attribute set at the given 303 /// index. Because attribute sets are immutable, this returns a new set. 304 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index, 305 uint64_t Bytes) const; 306 307 /// \brief Add the dereferenceable_or_null attribute to the attribute set at 308 /// the given index. Because attribute sets are immutable, this returns a new 309 /// set. 310 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 311 uint64_t Bytes) const; 312 313 //===--------------------------------------------------------------------===// 314 // AttributeSet Accessors 315 //===--------------------------------------------------------------------===// 316 317 /// \brief Retrieve the LLVM context. 318 LLVMContext &getContext() const; 319 320 /// \brief The attributes for the specified index are returned. 321 AttributeSet getParamAttributes(unsigned Index) const; 322 323 /// \brief The attributes for the ret value are returned. 324 AttributeSet getRetAttributes() const; 325 326 /// \brief The function attributes are returned. 327 AttributeSet getFnAttributes() const; 328 329 /// \brief Return true if the attribute exists at the given index. 330 bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const; 331 332 /// \brief Return true if the attribute exists at the given index. 333 bool hasAttribute(unsigned Index, StringRef Kind) const; 334 335 /// \brief Return true if attribute exists at the given index. 336 bool hasAttributes(unsigned Index) const; 337 338 /// \brief Return true if the specified attribute is set for at least one 339 /// parameter or for the return value. 340 bool hasAttrSomewhere(Attribute::AttrKind Attr) const; 341 342 /// \brief Return the attribute object that exists at the given index. 343 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const; 344 345 /// \brief Return the attribute object that exists at the given index. 346 Attribute getAttribute(unsigned Index, StringRef Kind) const; 347 348 /// \brief Return the alignment for the specified function parameter. 349 unsigned getParamAlignment(unsigned Index) const; 350 351 /// \brief Get the stack alignment. 352 unsigned getStackAlignment(unsigned Index) const; 353 354 /// \brief Get the number of dereferenceable bytes (or zero if unknown). 355 uint64_t getDereferenceableBytes(unsigned Index) const; 356 357 /// \brief Get the number of dereferenceable_or_null bytes (or zero if 358 /// unknown). 359 uint64_t getDereferenceableOrNullBytes(unsigned Index) const; 360 361 /// \brief Return the attributes at the index as a string. 362 std::string getAsString(unsigned Index, bool InAttrGrp = false) const; 363 364 typedef ArrayRef<Attribute>::iterator iterator; 365 366 iterator begin(unsigned Slot) const; 367 iterator end(unsigned Slot) const; 368 369 /// operator==/!= - Provide equality predicates. 370 bool operator==(const AttributeSet &RHS) const { 371 return pImpl == RHS.pImpl; 372 } 373 bool operator!=(const AttributeSet &RHS) const { 374 return pImpl != RHS.pImpl; 375 } 376 377 //===--------------------------------------------------------------------===// 378 // AttributeSet Introspection 379 //===--------------------------------------------------------------------===// 380 381 // FIXME: Remove this. 382 uint64_t Raw(unsigned Index) const; 383 384 /// \brief Return a raw pointer that uniquely identifies this attribute list. getRawPointer()385 void *getRawPointer() const { 386 return pImpl; 387 } 388 389 /// \brief Return true if there are no attributes. isEmpty()390 bool isEmpty() const { 391 return getNumSlots() == 0; 392 } 393 394 /// \brief Return the number of slots used in this attribute list. This is 395 /// the number of arguments that have an attribute set on them (including the 396 /// function itself). 397 unsigned getNumSlots() const; 398 399 /// \brief Return the index for the given slot. 400 unsigned getSlotIndex(unsigned Slot) const; 401 402 /// \brief Return the attributes at the given slot. 403 AttributeSet getSlotAttributes(unsigned Slot) const; 404 405 void dump() const; 406 }; 407 408 //===----------------------------------------------------------------------===// 409 /// \class 410 /// \brief Provide DenseMapInfo for AttributeSet. 411 template<> struct DenseMapInfo<AttributeSet> { 412 static inline AttributeSet getEmptyKey() { 413 uintptr_t Val = static_cast<uintptr_t>(-1); 414 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 415 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val)); 416 } 417 static inline AttributeSet getTombstoneKey() { 418 uintptr_t Val = static_cast<uintptr_t>(-2); 419 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 420 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val)); 421 } 422 static unsigned getHashValue(AttributeSet AS) { 423 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^ 424 (unsigned((uintptr_t)AS.pImpl) >> 9); 425 } 426 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; } 427 }; 428 429 //===----------------------------------------------------------------------===// 430 /// \class 431 /// \brief This class is used in conjunction with the Attribute::get method to 432 /// create an Attribute object. The object itself is uniquified. The Builder's 433 /// value, however, is not. So this can be used as a quick way to test for 434 /// equality, presence of attributes, etc. 435 class AttrBuilder { 436 std::bitset<Attribute::EndAttrKinds> Attrs; 437 std::map<std::string, std::string> TargetDepAttrs; 438 uint64_t Alignment; 439 uint64_t StackAlignment; 440 uint64_t DerefBytes; 441 uint64_t DerefOrNullBytes; 442 public: 443 AttrBuilder() 444 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 445 DerefOrNullBytes(0) {} 446 explicit AttrBuilder(uint64_t Val) 447 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 448 DerefOrNullBytes(0) { 449 addRawValue(Val); 450 } 451 AttrBuilder(const Attribute &A) 452 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 453 DerefOrNullBytes(0) { 454 addAttribute(A); 455 } 456 AttrBuilder(AttributeSet AS, unsigned Idx); 457 458 void clear(); 459 460 /// \brief Add an attribute to the builder. 461 AttrBuilder &addAttribute(Attribute::AttrKind Val); 462 463 /// \brief Add the Attribute object to the builder. 464 AttrBuilder &addAttribute(Attribute A); 465 466 /// \brief Add the target-dependent attribute to the builder. 467 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef()); 468 469 /// \brief Remove an attribute from the builder. 470 AttrBuilder &removeAttribute(Attribute::AttrKind Val); 471 472 /// \brief Remove the attributes from the builder. 473 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index); 474 475 /// \brief Remove the target-dependent attribute to the builder. 476 AttrBuilder &removeAttribute(StringRef A); 477 478 /// \brief Add the attributes from the builder. 479 AttrBuilder &merge(const AttrBuilder &B); 480 481 /// \brief Remove the attributes from the builder. 482 AttrBuilder &remove(const AttrBuilder &B); 483 484 /// \brief Return true if the builder has any attribute that's in the 485 /// specified builder. 486 bool overlaps(const AttrBuilder &B) const; 487 488 /// \brief Return true if the builder has the specified attribute. 489 bool contains(Attribute::AttrKind A) const { 490 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!"); 491 return Attrs[A]; 492 } 493 494 /// \brief Return true if the builder has the specified target-dependent 495 /// attribute. 496 bool contains(StringRef A) const; 497 498 /// \brief Return true if the builder has IR-level attributes. 499 bool hasAttributes() const; 500 501 /// \brief Return true if the builder has any attribute that's in the 502 /// specified attribute. 503 bool hasAttributes(AttributeSet A, uint64_t Index) const; 504 505 /// \brief Return true if the builder has an alignment attribute. 506 bool hasAlignmentAttr() const; 507 508 /// \brief Retrieve the alignment attribute, if it exists. 509 uint64_t getAlignment() const { return Alignment; } 510 511 /// \brief Retrieve the stack alignment attribute, if it exists. 512 uint64_t getStackAlignment() const { return StackAlignment; } 513 514 /// \brief Retrieve the number of dereferenceable bytes, if the dereferenceable 515 /// attribute exists (zero is returned otherwise). 516 uint64_t getDereferenceableBytes() const { return DerefBytes; } 517 518 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the 519 /// dereferenceable_or_null attribute exists (zero is returned otherwise). 520 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; } 521 522 /// \brief This turns an int alignment (which must be a power of 2) into the 523 /// form used internally in Attribute. 524 AttrBuilder &addAlignmentAttr(unsigned Align); 525 526 /// \brief This turns an int stack alignment (which must be a power of 2) into 527 /// the form used internally in Attribute. 528 AttrBuilder &addStackAlignmentAttr(unsigned Align); 529 530 /// \brief This turns the number of dereferenceable bytes into the form used 531 /// internally in Attribute. 532 AttrBuilder &addDereferenceableAttr(uint64_t Bytes); 533 534 /// \brief This turns the number of dereferenceable_or_null bytes into the 535 /// form used internally in Attribute. 536 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes); 537 538 /// \brief Return true if the builder contains no target-independent 539 /// attributes. 540 bool empty() const { return Attrs.none(); } 541 542 // Iterators for target-dependent attributes. 543 typedef std::pair<std::string, std::string> td_type; 544 typedef std::map<std::string, std::string>::iterator td_iterator; 545 typedef std::map<std::string, std::string>::const_iterator td_const_iterator; 546 typedef llvm::iterator_range<td_iterator> td_range; 547 typedef llvm::iterator_range<td_const_iterator> td_const_range; 548 549 td_iterator td_begin() { return TargetDepAttrs.begin(); } 550 td_iterator td_end() { return TargetDepAttrs.end(); } 551 552 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); } 553 td_const_iterator td_end() const { return TargetDepAttrs.end(); } 554 555 td_range td_attrs() { return td_range(td_begin(), td_end()); } 556 td_const_range td_attrs() const { 557 return td_const_range(td_begin(), td_end()); 558 } 559 560 bool td_empty() const { return TargetDepAttrs.empty(); } 561 562 bool operator==(const AttrBuilder &B); 563 bool operator!=(const AttrBuilder &B) { 564 return !(*this == B); 565 } 566 567 // FIXME: Remove this in 4.0. 568 569 /// \brief Add the raw value to the internal representation. 570 AttrBuilder &addRawValue(uint64_t Val); 571 }; 572 573 namespace AttributeFuncs { 574 575 /// \brief Which attributes cannot be applied to a type. 576 AttrBuilder typeIncompatible(const Type *Ty); 577 578 } // end AttributeFuncs namespace 579 580 } // end llvm namespace 581 582 #endif 583