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