LLVM API Documentation

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   //===--------------------------------------------------------------------===//
00286   // AttributeSet Accessors
00287   //===--------------------------------------------------------------------===//
00288 
00289   /// \brief Retrieve the LLVM context.
00290   LLVMContext &getContext() const;
00291 
00292   /// \brief The attributes for the specified index are returned.
00293   AttributeSet getParamAttributes(unsigned Index) const;
00294 
00295   /// \brief The attributes for the ret value are returned.
00296   AttributeSet getRetAttributes() const;
00297 
00298   /// \brief The function attributes are returned.
00299   AttributeSet getFnAttributes() const;
00300 
00301   /// \brief Return true if the attribute exists at the given index.
00302   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00303 
00304   /// \brief Return true if the attribute exists at the given index.
00305   bool hasAttribute(unsigned Index, StringRef Kind) const;
00306 
00307   /// \brief Return true if attribute exists at the given index.
00308   bool hasAttributes(unsigned Index) const;
00309 
00310   /// \brief Return true if the specified attribute is set for at least one
00311   /// parameter or for the return value.
00312   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
00313 
00314   /// \brief Return the attribute object that exists at the given index.
00315   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00316 
00317   /// \brief Return the attribute object that exists at the given index.
00318   Attribute getAttribute(unsigned Index, StringRef Kind) const;
00319 
00320   /// \brief Return the alignment for the specified function parameter.
00321   unsigned getParamAlignment(unsigned Index) const;
00322 
00323   /// \brief Get the stack alignment.
00324   unsigned getStackAlignment(unsigned Index) const;
00325 
00326   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
00327   uint64_t getDereferenceableBytes(unsigned Index) const;
00328 
00329   /// \brief Return the attributes at the index as a string.
00330   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
00331 
00332   typedef ArrayRef<Attribute>::iterator iterator;
00333 
00334   iterator begin(unsigned Slot) const;
00335   iterator end(unsigned Slot) const;
00336 
00337   /// operator==/!= - Provide equality predicates.
00338   bool operator==(const AttributeSet &RHS) const {
00339     return pImpl == RHS.pImpl;
00340   }
00341   bool operator!=(const AttributeSet &RHS) const {
00342     return pImpl != RHS.pImpl;
00343   }
00344 
00345   //===--------------------------------------------------------------------===//
00346   // AttributeSet Introspection
00347   //===--------------------------------------------------------------------===//
00348 
00349   // FIXME: Remove this.
00350   uint64_t Raw(unsigned Index) const;
00351 
00352   /// \brief Return a raw pointer that uniquely identifies this attribute list.
00353   void *getRawPointer() const {
00354     return pImpl;
00355   }
00356 
00357   /// \brief Return true if there are no attributes.
00358   bool isEmpty() const {
00359     return getNumSlots() == 0;
00360   }
00361 
00362   /// \brief Return the number of slots used in this attribute list.  This is
00363   /// the number of arguments that have an attribute set on them (including the
00364   /// function itself).
00365   unsigned getNumSlots() const;
00366 
00367   /// \brief Return the index for the given slot.
00368   unsigned getSlotIndex(unsigned Slot) const;
00369 
00370   /// \brief Return the attributes at the given slot.
00371   AttributeSet getSlotAttributes(unsigned Slot) const;
00372 
00373   void dump() const;
00374 };
00375 
00376 //===----------------------------------------------------------------------===//
00377 /// \class
00378 /// \brief Provide DenseMapInfo for AttributeSet.
00379 template<> struct DenseMapInfo<AttributeSet> {
00380   static inline AttributeSet getEmptyKey() {
00381     uintptr_t Val = static_cast<uintptr_t>(-1);
00382     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00383     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00384   }
00385   static inline AttributeSet getTombstoneKey() {
00386     uintptr_t Val = static_cast<uintptr_t>(-2);
00387     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00388     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00389   }
00390   static unsigned getHashValue(AttributeSet AS) {
00391     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
00392            (unsigned((uintptr_t)AS.pImpl) >> 9);
00393   }
00394   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
00395 };
00396 
00397 //===----------------------------------------------------------------------===//
00398 /// \class
00399 /// \brief This class is used in conjunction with the Attribute::get method to
00400 /// create an Attribute object. The object itself is uniquified. The Builder's
00401 /// value, however, is not. So this can be used as a quick way to test for
00402 /// equality, presence of attributes, etc.
00403 class AttrBuilder {
00404   std::bitset<Attribute::EndAttrKinds> Attrs;
00405   std::map<std::string, std::string> TargetDepAttrs;
00406   uint64_t Alignment;
00407   uint64_t StackAlignment;
00408   uint64_t DerefBytes;
00409 public:
00410   AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {}
00411   explicit AttrBuilder(uint64_t Val)
00412     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
00413     addRawValue(Val);
00414   }
00415   AttrBuilder(const Attribute &A)
00416     : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0) {
00417     addAttribute(A);
00418   }
00419   AttrBuilder(AttributeSet AS, unsigned Idx);
00420 
00421   void clear();
00422 
00423   /// \brief Add an attribute to the builder.
00424   AttrBuilder &addAttribute(Attribute::AttrKind Val);
00425 
00426   /// \brief Add the Attribute object to the builder.
00427   AttrBuilder &addAttribute(Attribute A);
00428 
00429   /// \brief Add the target-dependent attribute to the builder.
00430   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
00431 
00432   /// \brief Remove an attribute from the builder.
00433   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
00434 
00435   /// \brief Remove the attributes from the builder.
00436   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
00437 
00438   /// \brief Remove the target-dependent attribute to the builder.
00439   AttrBuilder &removeAttribute(StringRef A);
00440 
00441   /// \brief Add the attributes from the builder.
00442   AttrBuilder &merge(const AttrBuilder &B);
00443 
00444   /// \brief Return true if the builder has the specified attribute.
00445   bool contains(Attribute::AttrKind A) const {
00446     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
00447     return Attrs[A];
00448   }
00449 
00450   /// \brief Return true if the builder has the specified target-dependent
00451   /// attribute.
00452   bool contains(StringRef A) const;
00453 
00454   /// \brief Return true if the builder has IR-level attributes.
00455   bool hasAttributes() const;
00456 
00457   /// \brief Return true if the builder has any attribute that's in the
00458   /// specified attribute.
00459   bool hasAttributes(AttributeSet A, uint64_t Index) const;
00460 
00461   /// \brief Return true if the builder has an alignment attribute.
00462   bool hasAlignmentAttr() const;
00463 
00464   /// \brief Retrieve the alignment attribute, if it exists.
00465   uint64_t getAlignment() const { return Alignment; }
00466 
00467   /// \brief Retrieve the stack alignment attribute, if it exists.
00468   uint64_t getStackAlignment() const { return StackAlignment; }
00469 
00470   /// \brief Retrieve the number of dereferenceable bytes, if the dereferenceable
00471   /// attribute exists (zero is returned otherwise).
00472   uint64_t getDereferenceableBytes() const { return DerefBytes; }
00473 
00474   /// \brief This turns an int alignment (which must be a power of 2) into the
00475   /// form used internally in Attribute.
00476   AttrBuilder &addAlignmentAttr(unsigned Align);
00477 
00478   /// \brief This turns an int stack alignment (which must be a power of 2) into
00479   /// the form used internally in Attribute.
00480   AttrBuilder &addStackAlignmentAttr(unsigned Align);
00481 
00482   /// \brief This turns the number of dereferenceable bytes into the form used
00483   /// internally in Attribute.
00484   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
00485 
00486   /// \brief Return true if the builder contains no target-independent
00487   /// attributes.
00488   bool empty() const { return Attrs.none(); }
00489 
00490   // Iterators for target-dependent attributes.
00491   typedef std::pair<std::string, std::string>                td_type;
00492   typedef std::map<std::string, std::string>::iterator       td_iterator;
00493   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
00494   typedef llvm::iterator_range<td_iterator>                  td_range;
00495   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
00496 
00497   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
00498   td_iterator td_end()               { return TargetDepAttrs.end(); }
00499 
00500   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
00501   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
00502 
00503   td_range td_attrs() { return td_range(td_begin(), td_end()); }
00504   td_const_range td_attrs() const {
00505     return td_const_range(td_begin(), td_end());
00506   }
00507 
00508   bool td_empty() const              { return TargetDepAttrs.empty(); }
00509 
00510   bool operator==(const AttrBuilder &B);
00511   bool operator!=(const AttrBuilder &B) {
00512     return !(*this == B);
00513   }
00514 
00515   // FIXME: Remove this in 4.0.
00516 
00517   /// \brief Add the raw value to the internal representation.
00518   AttrBuilder &addRawValue(uint64_t Val);
00519 };
00520 
00521 namespace AttributeFuncs {
00522 
00523 /// \brief Which attributes cannot be applied to a type.
00524 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
00525 
00526 } // end AttributeFuncs namespace
00527 
00528 } // end llvm namespace
00529 
00530 #endif