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