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 Function; 37class LLVMContext; 38class Type; 39 40//===----------------------------------------------------------------------===// 41/// \class 42/// \brief Functions, function parameters, and return types can have attributes 43/// to indicate how they should be treated by optimizations and code 44/// generation. This class represents one of those attributes. It's light-weight 45/// and should be passed around by-value. 46class Attribute { 47public: 48 /// This enumeration lists the attributes that can be associated with 49 /// parameters, function results, or the function itself. 50 /// 51 /// Note: The `uwtable' attribute is about the ABI or the user mandating an 52 /// entry in the unwind table. The `nounwind' attribute is about an exception 53 /// passing by the function. 54 /// 55 /// In a theoretical system that uses tables for profiling and SjLj for 56 /// exceptions, they would be fully independent. In a normal system that uses 57 /// tables for both, the semantics are: 58 /// 59 /// nil = Needs an entry because an exception might pass by. 60 /// nounwind = No need for an entry 61 /// uwtable = Needs an entry because the ABI says so and because 62 /// an exception might pass by. 63 /// uwtable + nounwind = Needs an entry because the ABI says so. 64 65 enum AttrKind { 66 // IR-Level Attributes 67 None, ///< No attributes have been set 68 #define GET_ATTR_ENUM 69 #include "llvm/IR/Attributes.inc" 70 EndAttrKinds ///< Sentinal value useful for loops 71 }; 72 73private: 74 AttributeImpl *pImpl; 75 Attribute(AttributeImpl *A) : pImpl(A) {} 76 77public: 78 Attribute() : pImpl(nullptr) {} 79 80 //===--------------------------------------------------------------------===// 81 // Attribute Construction 82 //===--------------------------------------------------------------------===// 83 84 /// \brief Return a uniquified Attribute object. 85 static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0); 86 static Attribute get(LLVMContext &Context, StringRef Kind, 87 StringRef Val = StringRef()); 88 89 /// \brief Return a uniquified Attribute object that has the specific 90 /// alignment set. 91 static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align); 92 static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align); 93 static Attribute getWithDereferenceableBytes(LLVMContext &Context, 94 uint64_t Bytes); 95 static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context, 96 uint64_t Bytes); 97 98 //===--------------------------------------------------------------------===// 99 // Attribute Accessors 100 //===--------------------------------------------------------------------===// 101 102 /// \brief Return true if the attribute is an Attribute::AttrKind type. 103 bool isEnumAttribute() const; 104 105 /// \brief Return true if the attribute is an integer attribute. 106 bool isIntAttribute() const; 107 108 /// \brief Return true if the attribute is a string (target-dependent) 109 /// attribute. 110 bool isStringAttribute() const; 111 112 /// \brief Return true if the attribute is present. 113 bool hasAttribute(AttrKind Val) const; 114 115 /// \brief Return true if the target-dependent attribute is present. 116 bool hasAttribute(StringRef Val) const; 117 118 /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This 119 /// requires the attribute to be an enum or alignment attribute. 120 Attribute::AttrKind getKindAsEnum() const; 121 122 /// \brief Return the attribute's value as an integer. This requires that the 123 /// attribute be an alignment attribute. 124 uint64_t getValueAsInt() const; 125 126 /// \brief Return the attribute's kind as a string. This requires the 127 /// attribute to be a string attribute. 128 StringRef getKindAsString() const; 129 130 /// \brief Return the attribute's value as a string. This requires the 131 /// attribute to be a string attribute. 132 StringRef getValueAsString() const; 133 134 /// \brief Returns the alignment field of an attribute as a byte alignment 135 /// value. 136 unsigned getAlignment() const; 137 138 /// \brief Returns the stack alignment field of an attribute as a byte 139 /// alignment value. 140 unsigned getStackAlignment() const; 141 142 /// \brief Returns the number of dereferenceable bytes from the 143 /// dereferenceable attribute. 144 uint64_t getDereferenceableBytes() const; 145 146 /// \brief Returns the number of dereferenceable_or_null bytes from the 147 /// dereferenceable_or_null attribute. 148 uint64_t getDereferenceableOrNullBytes() const; 149 150 /// \brief The Attribute is converted to a string of equivalent mnemonic. This 151 /// is, presumably, for writing out the mnemonics for the assembly writer. 152 std::string getAsString(bool InAttrGrp = false) const; 153 154 /// \brief Equality and non-equality operators. 155 bool operator==(Attribute A) const { return pImpl == A.pImpl; } 156 bool operator!=(Attribute A) const { return pImpl != A.pImpl; } 157 158 /// \brief Less-than operator. Useful for sorting the attributes list. 159 bool operator<(Attribute A) const; 160 161 void Profile(FoldingSetNodeID &ID) const { 162 ID.AddPointer(pImpl); 163 } 164}; 165 166//===----------------------------------------------------------------------===// 167/// \class 168/// \brief This class holds the attributes for a function, its return value, and 169/// its parameters. You access the attributes for each of them via an index into 170/// the AttributeSet object. The function attributes are at index 171/// `AttributeSet::FunctionIndex', the return value is at index 172/// `AttributeSet::ReturnIndex', and the attributes for the parameters start at 173/// index `1'. 174class AttributeSet { 175public: 176 enum AttrIndex : unsigned { 177 ReturnIndex = 0U, 178 FunctionIndex = ~0U 179 }; 180 181private: 182 friend class AttrBuilder; 183 friend class AttributeSetImpl; 184 template <typename Ty> friend struct DenseMapInfo; 185 186 /// \brief The attributes that we are managing. This can be null to represent 187 /// the empty attributes list. 188 AttributeSetImpl *pImpl; 189 190 /// \brief The attributes for the specified index are returned. 191 AttributeSetNode *getAttributes(unsigned Index) const; 192 193 /// \brief Create an AttributeSet with the specified parameters in it. 194 static AttributeSet get(LLVMContext &C, 195 ArrayRef<std::pair<unsigned, Attribute> > Attrs); 196 static AttributeSet get(LLVMContext &C, 197 ArrayRef<std::pair<unsigned, 198 AttributeSetNode*> > Attrs); 199 200 static AttributeSet getImpl(LLVMContext &C, 201 ArrayRef<std::pair<unsigned, 202 AttributeSetNode*> > Attrs); 203 204 explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {} 205 206public: 207 AttributeSet() : pImpl(nullptr) {} 208 209 //===--------------------------------------------------------------------===// 210 // AttributeSet Construction and Mutation 211 //===--------------------------------------------------------------------===// 212 213 /// \brief Return an AttributeSet with the specified parameters in it. 214 static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs); 215 static AttributeSet get(LLVMContext &C, unsigned Index, 216 ArrayRef<Attribute::AttrKind> Kind); 217 static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B); 218 219 /// \brief Add an attribute to the attribute set at the given index. Because 220 /// attribute sets are immutable, this returns a new set. 221 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 222 Attribute::AttrKind Attr) const; 223 224 /// \brief Add an attribute to the attribute set at the given index. Because 225 /// attribute sets are immutable, this returns a new set. 226 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 227 StringRef Kind) const; 228 AttributeSet addAttribute(LLVMContext &C, unsigned Index, 229 StringRef Kind, StringRef Value) const; 230 231 /// Add an attribute to the attribute set at the given indices. Because 232 /// attribute sets are immutable, this returns a new set. 233 AttributeSet addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices, 234 Attribute A) const; 235 236 /// \brief Add attributes to the attribute set at the given index. Because 237 /// attribute sets are immutable, this returns a new set. 238 AttributeSet addAttributes(LLVMContext &C, unsigned Index, 239 AttributeSet Attrs) const; 240 241 /// \brief Remove the specified attribute at the specified index from this 242 /// attribute list. Because attribute lists are immutable, this returns the 243 /// new list. 244 AttributeSet removeAttribute(LLVMContext &C, unsigned Index, 245 Attribute::AttrKind Attr) const; 246 247 /// \brief Remove the specified attributes at the specified index from this 248 /// attribute list. Because attribute lists are immutable, this returns the 249 /// new list. 250 AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 251 AttributeSet Attrs) const; 252 253 /// \brief Remove the specified attributes at the specified index from this 254 /// attribute list. Because attribute lists are immutable, this returns the 255 /// new list. 256 AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 257 const AttrBuilder &Attrs) const; 258 259 /// \brief Add the dereferenceable attribute to the attribute set at the given 260 /// index. Because attribute sets are immutable, this returns a new set. 261 AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index, 262 uint64_t Bytes) const; 263 264 /// \brief Add the dereferenceable_or_null attribute to the attribute set at 265 /// the given index. Because attribute sets are immutable, this returns a new 266 /// set. 267 AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index, 268 uint64_t Bytes) const; 269 270 //===--------------------------------------------------------------------===// 271 // AttributeSet Accessors 272 //===--------------------------------------------------------------------===// 273 274 /// \brief Retrieve the LLVM context. 275 LLVMContext &getContext() const; 276 277 /// \brief The attributes for the specified index are returned. 278 AttributeSet getParamAttributes(unsigned Index) const; 279 280 /// \brief The attributes for the ret value are returned. 281 AttributeSet getRetAttributes() const; 282 283 /// \brief The function attributes are returned. 284 AttributeSet getFnAttributes() 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 the attribute exists at the given index. 290 bool hasAttribute(unsigned Index, StringRef Kind) const; 291 292 /// \brief Return true if attribute exists at the given index. 293 bool hasAttributes(unsigned Index) const; 294 295 /// \brief Return true if the specified attribute is set for at least one 296 /// parameter or for the return value. 297 bool hasAttrSomewhere(Attribute::AttrKind Attr) const; 298 299 /// \brief Return the attribute object that exists at the given index. 300 Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const; 301 302 /// \brief Return the attribute object that exists at the given index. 303 Attribute getAttribute(unsigned Index, StringRef Kind) const; 304 305 /// \brief Return the alignment for the specified function parameter. 306 unsigned getParamAlignment(unsigned Index) const; 307 308 /// \brief Get the stack alignment. 309 unsigned getStackAlignment(unsigned Index) const; 310 311 /// \brief Get the number of dereferenceable bytes (or zero if unknown). 312 uint64_t getDereferenceableBytes(unsigned Index) const; 313 314 /// \brief Get the number of dereferenceable_or_null bytes (or zero if 315 /// unknown). 316 uint64_t getDereferenceableOrNullBytes(unsigned Index) const; 317 318 /// \brief Return the attributes at the index as a string. 319 std::string getAsString(unsigned Index, bool InAttrGrp = false) const; 320 321 typedef ArrayRef<Attribute>::iterator iterator; 322 323 iterator begin(unsigned Slot) const; 324 iterator end(unsigned Slot) const; 325 326 /// operator==/!= - Provide equality predicates. 327 bool operator==(const AttributeSet &RHS) const { 328 return pImpl == RHS.pImpl; 329 } 330 bool operator!=(const AttributeSet &RHS) const { 331 return pImpl != RHS.pImpl; 332 } 333 334 //===--------------------------------------------------------------------===// 335 // AttributeSet Introspection 336 //===--------------------------------------------------------------------===// 337 338 // FIXME: Remove this. 339 uint64_t Raw(unsigned Index) const; 340 341 /// \brief Return a raw pointer that uniquely identifies this attribute list. 342 void *getRawPointer() const { 343 return pImpl; 344 } 345 346 /// \brief Return true if there are no attributes. 347 bool isEmpty() const { 348 return getNumSlots() == 0; 349 } 350 351 /// \brief Return the number of slots used in this attribute list. This is 352 /// the number of arguments that have an attribute set on them (including the 353 /// function itself). 354 unsigned getNumSlots() const; 355 356 /// \brief Return the index for the given slot. 357 unsigned getSlotIndex(unsigned Slot) const; 358 359 /// \brief Return the attributes at the given slot. 360 AttributeSet getSlotAttributes(unsigned Slot) const; 361 362 void dump() const; 363}; 364 365//===----------------------------------------------------------------------===// 366/// \class 367/// \brief Provide DenseMapInfo for AttributeSet. 368template<> struct DenseMapInfo<AttributeSet> { 369 static inline AttributeSet getEmptyKey() { 370 uintptr_t Val = static_cast<uintptr_t>(-1); 371 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 372 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val)); 373 } 374 static inline AttributeSet getTombstoneKey() { 375 uintptr_t Val = static_cast<uintptr_t>(-2); 376 Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable; 377 return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val)); 378 } 379 static unsigned getHashValue(AttributeSet AS) { 380 return (unsigned((uintptr_t)AS.pImpl) >> 4) ^ 381 (unsigned((uintptr_t)AS.pImpl) >> 9); 382 } 383 static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; } 384}; 385 386//===----------------------------------------------------------------------===// 387/// \class 388/// \brief This class is used in conjunction with the Attribute::get method to 389/// create an Attribute object. The object itself is uniquified. The Builder's 390/// value, however, is not. So this can be used as a quick way to test for 391/// equality, presence of attributes, etc. 392class AttrBuilder { 393 std::bitset<Attribute::EndAttrKinds> Attrs; 394 std::map<std::string, std::string> TargetDepAttrs; 395 uint64_t Alignment; 396 uint64_t StackAlignment; 397 uint64_t DerefBytes; 398 uint64_t DerefOrNullBytes; 399 400public: 401 AttrBuilder() 402 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 403 DerefOrNullBytes(0) {} 404 explicit AttrBuilder(uint64_t Val) 405 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 406 DerefOrNullBytes(0) { 407 addRawValue(Val); 408 } 409 AttrBuilder(const Attribute &A) 410 : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0), 411 DerefOrNullBytes(0) { 412 addAttribute(A); 413 } 414 AttrBuilder(AttributeSet AS, unsigned Idx); 415 416 void clear(); 417 418 /// \brief Add an attribute to the builder. 419 AttrBuilder &addAttribute(Attribute::AttrKind Val); 420 421 /// \brief Add the Attribute object to the builder. 422 AttrBuilder &addAttribute(Attribute A); 423 424 /// \brief Add the target-dependent attribute to the builder. 425 AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef()); 426 427 /// \brief Remove an attribute from the builder. 428 AttrBuilder &removeAttribute(Attribute::AttrKind Val); 429 430 /// \brief Remove the attributes from the builder. 431 AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index); 432 433 /// \brief Remove the target-dependent attribute to the builder. 434 AttrBuilder &removeAttribute(StringRef A); 435 436 /// \brief Add the attributes from the builder. 437 AttrBuilder &merge(const AttrBuilder &B); 438 439 /// \brief Remove the attributes from the builder. 440 AttrBuilder &remove(const AttrBuilder &B); 441 442 /// \brief Return true if the builder has any attribute that's in the 443 /// specified builder. 444 bool overlaps(const AttrBuilder &B) const; 445 446 /// \brief Return true if the builder has the specified attribute. 447 bool contains(Attribute::AttrKind A) const { 448 assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!"); 449 return Attrs[A]; 450 } 451 452 /// \brief Return true if the builder has the specified target-dependent 453 /// attribute. 454 bool contains(StringRef A) const; 455 456 /// \brief Return true if the builder has IR-level attributes. 457 bool hasAttributes() const; 458 459 /// \brief Return true if the builder has any attribute that's in the 460 /// specified attribute. 461 bool hasAttributes(AttributeSet A, uint64_t Index) const; 462 463 /// \brief Return true if the builder has an alignment attribute. 464 bool hasAlignmentAttr() const; 465 466 /// \brief Retrieve the alignment attribute, if it exists. 467 uint64_t getAlignment() const { return Alignment; } 468 469 /// \brief Retrieve the stack alignment attribute, if it exists. 470 uint64_t getStackAlignment() const { return StackAlignment; } 471 472 /// \brief Retrieve the number of dereferenceable bytes, if the 473 /// dereferenceable attribute exists (zero is returned otherwise). 474 uint64_t getDereferenceableBytes() const { return DerefBytes; } 475 476 /// \brief Retrieve the number of dereferenceable_or_null bytes, if the 477 /// dereferenceable_or_null attribute exists (zero is returned otherwise). 478 uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; } 479 480 /// \brief This turns an int alignment (which must be a power of 2) into the 481 /// form used internally in Attribute. 482 AttrBuilder &addAlignmentAttr(unsigned Align); 483 484 /// \brief This turns an int stack alignment (which must be a power of 2) into 485 /// the form used internally in Attribute. 486 AttrBuilder &addStackAlignmentAttr(unsigned Align); 487 488 /// \brief This turns the number of dereferenceable bytes into the form used 489 /// internally in Attribute. 490 AttrBuilder &addDereferenceableAttr(uint64_t Bytes); 491 492 /// \brief This turns the number of dereferenceable_or_null bytes into the 493 /// form used internally in Attribute. 494 AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes); 495 496 /// \brief Return true if the builder contains no target-independent 497 /// attributes. 498 bool empty() const { return Attrs.none(); } 499 500 // Iterators for target-dependent attributes. 501 typedef std::pair<std::string, std::string> td_type; 502 typedef std::map<std::string, std::string>::iterator td_iterator; 503 typedef std::map<std::string, std::string>::const_iterator td_const_iterator; 504 typedef llvm::iterator_range<td_iterator> td_range; 505 typedef llvm::iterator_range<td_const_iterator> td_const_range; 506 507 td_iterator td_begin() { return TargetDepAttrs.begin(); } 508 td_iterator td_end() { return TargetDepAttrs.end(); } 509 510 td_const_iterator td_begin() const { return TargetDepAttrs.begin(); } 511 td_const_iterator td_end() const { return TargetDepAttrs.end(); } 512 513 td_range td_attrs() { return td_range(td_begin(), td_end()); } 514 td_const_range td_attrs() const { 515 return td_const_range(td_begin(), td_end()); 516 } 517 518 bool td_empty() const { return TargetDepAttrs.empty(); } 519 520 bool operator==(const AttrBuilder &B); 521 bool operator!=(const AttrBuilder &B) { 522 return !(*this == B); 523 } 524 525 // FIXME: Remove this in 4.0. 526 527 /// \brief Add the raw value to the internal representation. 528 AttrBuilder &addRawValue(uint64_t Val); 529}; 530 531namespace AttributeFuncs { 532 533/// \brief Which attributes cannot be applied to a type. 534AttrBuilder typeIncompatible(Type *Ty); 535 536/// \returns Return true if the two functions have compatible target-independent 537/// attributes for inlining purposes. 538bool areInlineCompatible(const Function &Caller, const Function &Callee); 539 540/// \brief Merge caller's and callee's attributes. 541void mergeAttributesForInlining(Function &Caller, const Function &Callee); 542 543} // end AttributeFuncs namespace 544 545} // end llvm namespace 546 547#endif 548