LLVM  mainline
Attributes.h
Go to the documentation of this file.
00001 //===-- llvm/Attributes.h - Container for Attributes ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief This file contains the simple types necessary to represent the
00012 /// attributes associated with functions and their calls.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_IR_ATTRIBUTES_H
00017 #define LLVM_IR_ATTRIBUTES_H
00018 
00019 #include "llvm/ADT/ArrayRef.h"
00020 #include "llvm/ADT/FoldingSet.h"
00021 #include "llvm/Support/Compiler.h"
00022 #include "llvm/Support/PointerLikeTypeTraits.h"
00023 #include <bitset>
00024 #include <cassert>
00025 #include <map>
00026 #include <string>
00027 
00028 namespace llvm {
00029 
00030 class AttrBuilder;
00031 class AttributeImpl;
00032 class AttributeSetImpl;
00033 class AttributeSetNode;
00034 class Constant;
00035 template<typename T> struct DenseMapInfo;
00036 class LLVMContext;
00037 class Type;
00038 
00039 //===----------------------------------------------------------------------===//
00040 /// \class
00041 /// \brief Functions, function parameters, and return types can have attributes
00042 /// to indicate how they should be treated by optimizations and code
00043 /// generation. This class represents one of those attributes. It's light-weight
00044 /// and should be passed around by-value.
00045 class Attribute {
00046 public:
00047   /// This enumeration lists the attributes that can be associated with
00048   /// parameters, function results, or the function itself.
00049   ///
00050   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
00051   /// entry in the unwind table. The `nounwind' attribute is about an exception
00052   /// passing by the function.
00053   ///
00054   /// In a theoretical system that uses tables for profiling and SjLj for
00055   /// exceptions, they would be fully independent. In a normal system that uses
00056   /// tables for both, the semantics are:
00057   ///
00058   /// nil                = Needs an entry because an exception might pass by.
00059   /// nounwind           = No need for an entry
00060   /// uwtable            = Needs an entry because the ABI says so and because
00061   ///                      an exception might pass by.
00062   /// uwtable + nounwind = Needs an entry because the ABI says so.
00063 
00064   enum AttrKind {
00065     // IR-Level Attributes
00066     None,                  ///< No attributes have been set
00067     Alignment,             ///< Alignment of parameter (5 bits)
00068                            ///< stored as log2 of alignment with +1 bias
00069                            ///< 0 means unaligned (different from align(1))
00070     AlwaysInline,          ///< inline=always
00071     Builtin,               ///< Callee is recognized as a builtin, despite
00072                            ///< nobuiltin attribute on its declaration.
00073     ByVal,                 ///< Pass structure by value
00074     InAlloca,              ///< Pass structure in an alloca
00075     Cold,                  ///< Marks function as being in a cold path.
00076     InlineHint,            ///< Source said inlining was desirable
00077     InReg,                 ///< Force argument to be passed in register
00078     JumpTable,             ///< Build jump-instruction tables and replace refs.
00079     MinSize,               ///< Function must be optimized for size first
00080     Naked,                 ///< Naked function
00081     Nest,                  ///< Nested function static chain
00082     NoAlias,               ///< Considered to not alias after call
00083     NoBuiltin,             ///< Callee isn't recognized as a builtin
00084     NoCapture,             ///< Function creates no aliases of pointer
00085     NoDuplicate,           ///< Call cannot be duplicated
00086     NoImplicitFloat,       ///< Disable implicit floating point insts
00087     NoInline,              ///< inline=never
00088     NonLazyBind,           ///< Function is called early and/or
00089                            ///< often, so lazy binding isn't worthwhile
00090     NonNull,               ///< Pointer is known to be not null
00091     Dereferenceable,       ///< Pointer is known to be dereferenceable
00092     NoRedZone,             ///< Disable redzone
00093     NoReturn,              ///< Mark the function as not returning
00094     NoUnwind,              ///< Function doesn't unwind stack
00095     OptimizeForSize,       ///< opt_size
00096     OptimizeNone,          ///< Function must not be optimized.
00097     ReadNone,              ///< Function does not access memory
00098     ReadOnly,              ///< Function only reads from memory
00099     Returned,              ///< Return value is always equal to this argument
00100     ReturnsTwice,          ///< Function can return twice
00101     SExt,                  ///< Sign extended before/after call
00102     StackAlignment,        ///< Alignment of stack for function (3 bits)
00103                            ///< stored as log2 of alignment with +1 bias 0
00104                            ///< means unaligned (different from
00105                            ///< alignstack=(1))
00106     StackProtect,          ///< Stack protection.
00107     StackProtectReq,       ///< Stack protection required.
00108     StackProtectStrong,    ///< Strong Stack protection.
00109     StructRet,             ///< Hidden pointer to structure to return
00110     SanitizeAddress,       ///< AddressSanitizer is on.
00111     SanitizeThread,        ///< ThreadSanitizer is on.
00112     SanitizeMemory,        ///< MemorySanitizer is on.
00113     UWTable,               ///< Function must be in a unwind table
00114     ZExt,                  ///< Zero extended before/after call
00115 
00116     EndAttrKinds           ///< Sentinal value useful for loops
00117   };
00118 private:
00119   AttributeImpl *pImpl;
00120   Attribute(AttributeImpl *A) : pImpl(A) {}
00121 public:
00122   Attribute() : pImpl(nullptr) {}
00123 
00124   //===--------------------------------------------------------------------===//
00125   // Attribute Construction
00126   //===--------------------------------------------------------------------===//
00127 
00128   /// \brief Return a uniquified Attribute object.
00129   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
00130   static Attribute get(LLVMContext &Context, StringRef Kind,
00131                        StringRef Val = StringRef());
00132 
00133   /// \brief Return a uniquified Attribute object that has the specific
00134   /// alignment set.
00135   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
00136   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
00137   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
00138                                               uint64_t Bytes);
00139 
00140   //===--------------------------------------------------------------------===//
00141   // Attribute Accessors
00142   //===--------------------------------------------------------------------===//
00143 
00144   /// \brief Return true if the attribute is an Attribute::AttrKind type.
00145   bool isEnumAttribute() const;
00146 
00147   /// \brief Return true if the attribute is an integer attribute.
00148   bool isIntAttribute() const;
00149 
00150   /// \brief Return true if the attribute is a string (target-dependent)
00151   /// attribute.
00152   bool isStringAttribute() const;
00153 
00154   /// \brief Return true if the attribute is present.
00155   bool hasAttribute(AttrKind Val) const;
00156 
00157   /// \brief Return true if the target-dependent attribute is present.
00158   bool hasAttribute(StringRef Val) const;
00159 
00160   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
00161   /// requires the attribute to be an enum or alignment attribute.
00162   Attribute::AttrKind getKindAsEnum() const;
00163 
00164   /// \brief Return the attribute's value as an integer. This requires that the
00165   /// attribute be an alignment attribute.
00166   uint64_t getValueAsInt() const;
00167 
00168   /// \brief Return the attribute's kind as a string. This requires the
00169   /// attribute to be a string attribute.
00170   StringRef getKindAsString() const;
00171 
00172   /// \brief Return the attribute's value as a string. This requires the
00173   /// attribute to be a string attribute.
00174   StringRef getValueAsString() const;
00175 
00176   /// \brief Returns the alignment field of an attribute as a byte alignment
00177   /// value.
00178   unsigned getAlignment() const;
00179 
00180   /// \brief Returns the stack alignment field of an attribute as a byte
00181   /// alignment value.
00182   unsigned getStackAlignment() const;
00183 
00184   /// \brief Returns the number of dereferenceable bytes from the
00185   /// dereferenceable attribute (or zero if unknown).
00186   uint64_t getDereferenceableBytes() const;
00187 
00188   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
00189   /// is, presumably, for writing out the mnemonics for the assembly writer.
00190   std::string getAsString(bool InAttrGrp = false) const;
00191 
00192   /// \brief Equality and non-equality operators.
00193   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
00194   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
00195 
00196   /// \brief Less-than operator. Useful for sorting the attributes list.
00197   bool operator<(Attribute A) const;
00198 
00199   void Profile(FoldingSetNodeID &ID) const {
00200     ID.AddPointer(pImpl);
00201   }
00202 };
00203 
00204 //===----------------------------------------------------------------------===//
00205 /// \class
00206 /// \brief This class holds the attributes for a function, its return value, and
00207 /// its parameters. You access the attributes for each of them via an index into
00208 /// the AttributeSet object. The function attributes are at index
00209 /// `AttributeSet::FunctionIndex', the return value is at index
00210 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
00211 /// index `1'.
00212 class AttributeSet {
00213 public:
00214   enum AttrIndex : unsigned {
00215     ReturnIndex = 0U,
00216     FunctionIndex = ~0U
00217   };
00218 private:
00219   friend class AttrBuilder;
00220   friend class AttributeSetImpl;
00221   template <typename Ty> friend struct DenseMapInfo;
00222 
00223   /// \brief The attributes that we are managing. This can be null to represent
00224   /// the empty attributes list.
00225   AttributeSetImpl *pImpl;
00226 
00227   /// \brief The attributes for the specified index are returned.
00228   AttributeSetNode *getAttributes(unsigned Index) const;
00229 
00230   /// \brief Create an AttributeSet with the specified parameters in it.
00231   static AttributeSet get(LLVMContext &C,
00232                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
00233   static AttributeSet get(LLVMContext &C,
00234                           ArrayRef<std::pair<unsigned,
00235                                              AttributeSetNode*> > Attrs);
00236 
00237   static AttributeSet getImpl(LLVMContext &C,
00238                               ArrayRef<std::pair<unsigned,
00239                                                  AttributeSetNode*> > Attrs);
00240 
00241 
00242   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
00243 public:
00244   AttributeSet() : pImpl(nullptr) {}
00245 
00246   //===--------------------------------------------------------------------===//
00247   // AttributeSet Construction and Mutation
00248   //===--------------------------------------------------------------------===//
00249 
00250   /// \brief Return an AttributeSet with the specified parameters in it.
00251   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
00252   static AttributeSet get(LLVMContext &C, unsigned Index,
00253                           ArrayRef<Attribute::AttrKind> Kind);
00254   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
00255 
00256   /// \brief Add an attribute to the attribute set at the given index. Since
00257   /// attribute sets are immutable, this returns a new set.
00258   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00259                             Attribute::AttrKind Attr) const;
00260 
00261   /// \brief Add an attribute to the attribute set at the given index. Since
00262   /// attribute sets are immutable, this returns a new set.
00263   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00264                             StringRef Kind) const;
00265   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00266                             StringRef Kind, StringRef Value) const;
00267 
00268   /// \brief Add attributes to the attribute set at the given index. Since
00269   /// attribute sets are immutable, this returns a new set.
00270   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
00271                              AttributeSet Attrs) const;
00272 
00273   /// \brief Remove the specified attribute at the specified index from this
00274   /// attribute list. Since attribute lists are immutable, this returns the new
00275   /// list.
00276   AttributeSet removeAttribute(LLVMContext &C, unsigned Index, 
00277                                Attribute::AttrKind Attr) const;
00278 
00279   /// \brief Remove the specified attributes at the specified index from this
00280   /// attribute list. Since attribute lists are immutable, this returns the new
00281   /// list.
00282   AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 
00283                                 AttributeSet Attrs) const;
00284 
00285   /// \brief Add the dereferenceable attribute to the attribute set at the given
00286   /// index. Since attribute sets are immutable, this returns a new set.
00287   AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
00288                                       uint64_t Bytes) const;
00289 
00290   //===--------------------------------------------------------------------===//
00291   // AttributeSet Accessors
00292   //===--------------------------------------------------------------------===//
00293 
00294   /// \brief Retrieve the LLVM context.
00295   LLVMContext &getContext() const;
00296 
00297   /// \brief The attributes for the specified index are returned.
00298   AttributeSet getParamAttributes(unsigned Index) const;
00299 
00300   /// \brief The attributes for the ret value are returned.
00301   AttributeSet getRetAttributes() const;
00302 
00303   /// \brief The function attributes are returned.
00304   AttributeSet getFnAttributes() const;
00305 
00306   /// \brief Return true if the attribute exists at the given index.
00307   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00308 
00309   /// \brief Return true if the attribute exists at the given index.
00310   bool hasAttribute(unsigned Index, StringRef Kind) const;
00311 
00312   /// \brief Return true if attribute exists at the given index.
00313   bool hasAttributes(unsigned Index) const;
00314 
00315   /// \brief Return true if the specified attribute is set for at least one
00316   /// parameter or for the return value.
00317   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
00318 
00319   /// \brief Return the attribute object that exists at the given index.
00320   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00321 
00322   /// \brief Return the attribute object that exists at the given index.
00323   Attribute getAttribute(unsigned Index, StringRef Kind) const;
00324 
00325   /// \brief Return the alignment for the specified function parameter.
00326   unsigned getParamAlignment(unsigned Index) const;
00327 
00328   /// \brief Get the stack alignment.
00329   unsigned getStackAlignment(unsigned Index) const;
00330 
00331   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
00332   uint64_t getDereferenceableBytes(unsigned Index) const;
00333 
00334   /// \brief Return the attributes at the index as a string.
00335   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
00336 
00337   typedef ArrayRef<Attribute>::iterator iterator;
00338 
00339   iterator begin(unsigned Slot) const;
00340   iterator end(unsigned Slot) const;
00341 
00342   /// operator==/!= - Provide equality predicates.
00343   bool operator==(const AttributeSet &RHS) const {
00344     return pImpl == RHS.pImpl;
00345   }
00346   bool operator!=(const AttributeSet &RHS) const {
00347     return pImpl != RHS.pImpl;
00348   }
00349 
00350   //===--------------------------------------------------------------------===//
00351   // AttributeSet Introspection
00352   //===--------------------------------------------------------------------===//
00353 
00354   // FIXME: Remove this.
00355   uint64_t Raw(unsigned Index) const;
00356 
00357   /// \brief Return a raw pointer that uniquely identifies this attribute list.
00358   void *getRawPointer() const {
00359     return pImpl;
00360   }
00361 
00362   /// \brief Return true if there are no attributes.
00363   bool isEmpty() const {
00364     return getNumSlots() == 0;
00365   }
00366 
00367   /// \brief Return the number of slots used in this attribute list.  This is
00368   /// the number of arguments that have an attribute set on them (including the
00369   /// function itself).
00370   unsigned getNumSlots() const;
00371 
00372   /// \brief Return the index for the given slot.
00373   unsigned getSlotIndex(unsigned Slot) const;
00374 
00375   /// \brief Return the attributes at the given slot.
00376   AttributeSet getSlotAttributes(unsigned Slot) const;
00377 
00378   void dump() const;
00379 };
00380 
00381 //===----------------------------------------------------------------------===//
00382 /// \class
00383 /// \brief Provide DenseMapInfo for AttributeSet.
00384 template<> struct DenseMapInfo<AttributeSet> {
00385   static inline AttributeSet getEmptyKey() {
00386     uintptr_t Val = static_cast<uintptr_t>(-1);
00387     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00388     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00389   }
00390   static inline AttributeSet getTombstoneKey() {
00391     uintptr_t Val = static_cast<uintptr_t>(-2);
00392     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00393     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00394   }
00395   static unsigned getHashValue(AttributeSet AS) {
00396     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
00397            (unsigned((uintptr_t)AS.pImpl) >> 9);
00398   }
00399   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
00400 };
00401 
00402 //===----------------------------------------------------------------------===//
00403 /// \class
00404 /// \brief This class is used in conjunction with the Attribute::get method to
00405 /// create an Attribute object. The object itself is uniquified. The Builder's
00406 /// value, however, is not. So this can be used as a quick way to test for
00407 /// equality, presence of attributes, etc.
00408 class AttrBuilder {
00409   std::bitset<Attribute::EndAttrKinds> Attrs;
00410   std::map<std::string, std::string> TargetDepAttrs;
00411   uint64_t Alignment;
00412   uint64_t StackAlignment;
00413   uint64_t DerefBytes;
00414 public:
00415   AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {}
00416   explicit AttrBuilder(uint64_t Val)
00417     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
00418     addRawValue(Val);
00419   }
00420   AttrBuilder(const Attribute &A)
00421     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
00422     addAttribute(A);
00423   }
00424   AttrBuilder(AttributeSet AS, unsigned Idx);
00425 
00426   void clear();
00427 
00428   /// \brief Add an attribute to the builder.
00429   AttrBuilder &addAttribute(Attribute::AttrKind Val);
00430 
00431   /// \brief Add the Attribute object to the builder.
00432   AttrBuilder &addAttribute(Attribute A);
00433 
00434   /// \brief Add the target-dependent attribute to the builder.
00435   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
00436 
00437   /// \brief Remove an attribute from the builder.
00438   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
00439 
00440   /// \brief Remove the attributes from the builder.
00441   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
00442 
00443   /// \brief Remove the target-dependent attribute to the builder.
00444   AttrBuilder &removeAttribute(StringRef A);
00445 
00446   /// \brief Add the attributes from the builder.
00447   AttrBuilder &merge(const AttrBuilder &B);
00448 
00449   /// \brief Return true if the builder has the specified attribute.
00450   bool contains(Attribute::AttrKind A) const {
00451     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
00452     return Attrs[A];
00453   }
00454 
00455   /// \brief Return true if the builder has the specified target-dependent
00456   /// attribute.
00457   bool contains(StringRef A) const;
00458 
00459   /// \brief Return true if the builder has IR-level attributes.
00460   bool hasAttributes() const;
00461 
00462   /// \brief Return true if the builder has any attribute that's in the
00463   /// specified attribute.
00464   bool hasAttributes(AttributeSet A, uint64_t Index) const;
00465 
00466   /// \brief Return true if the builder has an alignment attribute.
00467   bool hasAlignmentAttr() const;
00468 
00469   /// \brief Retrieve the alignment attribute, if it exists.
00470   uint64_t getAlignment() const { return Alignment; }
00471 
00472   /// \brief Retrieve the stack alignment attribute, if it exists.
00473   uint64_t getStackAlignment() const { return StackAlignment; }
00474 
00475   /// \brief Retrieve the number of dereferenceable bytes, if the dereferenceable
00476   /// attribute exists (zero is returned otherwise).
00477   uint64_t getDereferenceableBytes() const { return DerefBytes; }
00478 
00479   /// \brief This turns an int alignment (which must be a power of 2) into the
00480   /// form used internally in Attribute.
00481   AttrBuilder &addAlignmentAttr(unsigned Align);
00482 
00483   /// \brief This turns an int stack alignment (which must be a power of 2) into
00484   /// the form used internally in Attribute.
00485   AttrBuilder &addStackAlignmentAttr(unsigned Align);
00486 
00487   /// \brief This turns the number of dereferenceable bytes into the form used
00488   /// internally in Attribute.
00489   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
00490 
00491   /// \brief Return true if the builder contains no target-independent
00492   /// attributes.
00493   bool empty() const { return Attrs.none(); }
00494 
00495   // Iterators for target-dependent attributes.
00496   typedef std::pair<std::string, std::string>                td_type;
00497   typedef std::map<std::string, std::string>::iterator       td_iterator;
00498   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
00499   typedef llvm::iterator_range<td_iterator>                  td_range;
00500   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
00501 
00502   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
00503   td_iterator td_end()               { return TargetDepAttrs.end(); }
00504 
00505   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
00506   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
00507 
00508   td_range td_attrs() { return td_range(td_begin(), td_end()); }
00509   td_const_range td_attrs() const {
00510     return td_const_range(td_begin(), td_end());
00511   }
00512 
00513   bool td_empty() const              { return TargetDepAttrs.empty(); }
00514 
00515   bool operator==(const AttrBuilder &B);
00516   bool operator!=(const AttrBuilder &B) {
00517     return !(*this == B);
00518   }
00519 
00520   // FIXME: Remove this in 4.0.
00521 
00522   /// \brief Add the raw value to the internal representation.
00523   AttrBuilder &addRawValue(uint64_t Val);
00524 };
00525 
00526 namespace AttributeFuncs {
00527 
00528 /// \brief Which attributes cannot be applied to a type.
00529 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
00530 
00531 } // end AttributeFuncs namespace
00532 
00533 } // end llvm namespace
00534 
00535 #endif