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     MinSize,               ///< Function must be optimized for size first
00079     Naked,                 ///< Naked function
00080     Nest,                  ///< Nested function static chain
00081     NoAlias,               ///< Considered to not alias after call
00082     NoBuiltin,             ///< Callee isn't recognized as a builtin
00083     NoCapture,             ///< Function creates no aliases of pointer
00084     NoDuplicate,           ///< Call cannot be duplicated
00085     NoImplicitFloat,       ///< Disable implicit floating point insts
00086     NoInline,              ///< inline=never
00087     NonLazyBind,           ///< Function is called early and/or
00088                            ///< often, so lazy binding isn't worthwhile
00089     NoRedZone,             ///< Disable redzone
00090     NoReturn,              ///< Mark the function as not returning
00091     NoUnwind,              ///< Function doesn't unwind stack
00092     OptimizeForSize,       ///< opt_size
00093     OptimizeNone,          ///< Function must not be optimized.
00094     ReadNone,              ///< Function does not access memory
00095     ReadOnly,              ///< Function only reads from memory
00096     Returned,              ///< Return value is always equal to this argument
00097     ReturnsTwice,          ///< Function can return twice
00098     SExt,                  ///< Sign extended before/after call
00099     StackAlignment,        ///< Alignment of stack for function (3 bits)
00100                            ///< stored as log2 of alignment with +1 bias 0
00101                            ///< means unaligned (different from
00102                            ///< alignstack=(1))
00103     StackProtect,          ///< Stack protection.
00104     StackProtectReq,       ///< Stack protection required.
00105     StackProtectStrong,    ///< Strong Stack protection.
00106     StructRet,             ///< Hidden pointer to structure to return
00107     SanitizeAddress,       ///< AddressSanitizer is on.
00108     SanitizeThread,        ///< ThreadSanitizer is on.
00109     SanitizeMemory,        ///< MemorySanitizer is on.
00110     UWTable,               ///< Function must be in a unwind table
00111     ZExt,                  ///< Zero extended before/after call
00112 
00113     EndAttrKinds           ///< Sentinal value useful for loops
00114   };
00115 private:
00116   AttributeImpl *pImpl;
00117   Attribute(AttributeImpl *A) : pImpl(A) {}
00118 public:
00119   Attribute() : pImpl(nullptr) {}
00120 
00121   //===--------------------------------------------------------------------===//
00122   // Attribute Construction
00123   //===--------------------------------------------------------------------===//
00124 
00125   /// \brief Return a uniquified Attribute object.
00126   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
00127   static Attribute get(LLVMContext &Context, StringRef Kind,
00128                        StringRef Val = StringRef());
00129 
00130   /// \brief Return a uniquified Attribute object that has the specific
00131   /// alignment set.
00132   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
00133   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
00134 
00135   //===--------------------------------------------------------------------===//
00136   // Attribute Accessors
00137   //===--------------------------------------------------------------------===//
00138 
00139   /// \brief Return true if the attribute is an Attribute::AttrKind type.
00140   bool isEnumAttribute() const;
00141 
00142   /// \brief Return true if the attribute is an alignment attribute.
00143   bool isAlignAttribute() const;
00144 
00145   /// \brief Return true if the attribute is a string (target-dependent)
00146   /// attribute.
00147   bool isStringAttribute() const;
00148 
00149   /// \brief Return true if the attribute is present.
00150   bool hasAttribute(AttrKind Val) const;
00151 
00152   /// \brief Return true if the target-dependent attribute is present.
00153   bool hasAttribute(StringRef Val) const;
00154 
00155   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
00156   /// requires the attribute to be an enum or alignment attribute.
00157   Attribute::AttrKind getKindAsEnum() const;
00158 
00159   /// \brief Return the attribute's value as an integer. This requires that the
00160   /// attribute be an alignment attribute.
00161   uint64_t getValueAsInt() const;
00162 
00163   /// \brief Return the attribute's kind as a string. This requires the
00164   /// attribute to be a string attribute.
00165   StringRef getKindAsString() const;
00166 
00167   /// \brief Return the attribute's value as a string. This requires the
00168   /// attribute to be a string attribute.
00169   StringRef getValueAsString() const;
00170 
00171   /// \brief Returns the alignment field of an attribute as a byte alignment
00172   /// value.
00173   unsigned getAlignment() const;
00174 
00175   /// \brief Returns the stack alignment field of an attribute as a byte
00176   /// alignment value.
00177   unsigned getStackAlignment() const;
00178 
00179   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
00180   /// is, presumably, for writing out the mnemonics for the assembly writer.
00181   std::string getAsString(bool InAttrGrp = false) const;
00182 
00183   /// \brief Equality and non-equality operators.
00184   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
00185   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
00186 
00187   /// \brief Less-than operator. Useful for sorting the attributes list.
00188   bool operator<(Attribute A) const;
00189 
00190   void Profile(FoldingSetNodeID &ID) const {
00191     ID.AddPointer(pImpl);
00192   }
00193 };
00194 
00195 //===----------------------------------------------------------------------===//
00196 /// \class
00197 /// \brief This class holds the attributes for a function, its return value, and
00198 /// its parameters. You access the attributes for each of them via an index into
00199 /// the AttributeSet object. The function attributes are at index
00200 /// `AttributeSet::FunctionIndex', the return value is at index
00201 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
00202 /// index `1'.
00203 class AttributeSet {
00204 public:
00205   enum AttrIndex : unsigned {
00206     ReturnIndex = 0U,
00207     FunctionIndex = ~0U
00208   };
00209 private:
00210   friend class AttrBuilder;
00211   friend class AttributeSetImpl;
00212   template <typename Ty> friend struct DenseMapInfo;
00213 
00214   /// \brief The attributes that we are managing. This can be null to represent
00215   /// the empty attributes list.
00216   AttributeSetImpl *pImpl;
00217 
00218   /// \brief The attributes for the specified index are returned.
00219   AttributeSetNode *getAttributes(unsigned Index) const;
00220 
00221   /// \brief Create an AttributeSet with the specified parameters in it.
00222   static AttributeSet get(LLVMContext &C,
00223                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
00224   static AttributeSet get(LLVMContext &C,
00225                           ArrayRef<std::pair<unsigned,
00226                                              AttributeSetNode*> > Attrs);
00227 
00228   static AttributeSet getImpl(LLVMContext &C,
00229                               ArrayRef<std::pair<unsigned,
00230                                                  AttributeSetNode*> > Attrs);
00231 
00232 
00233   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
00234 public:
00235   AttributeSet() : pImpl(nullptr) {}
00236 
00237   //===--------------------------------------------------------------------===//
00238   // AttributeSet Construction and Mutation
00239   //===--------------------------------------------------------------------===//
00240 
00241   /// \brief Return an AttributeSet with the specified parameters in it.
00242   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
00243   static AttributeSet get(LLVMContext &C, unsigned Index,
00244                           ArrayRef<Attribute::AttrKind> Kind);
00245   static AttributeSet get(LLVMContext &C, unsigned Index, AttrBuilder &B);
00246 
00247   /// \brief Add an attribute to the attribute set at the given index. Since
00248   /// attribute sets are immutable, this returns a new set.
00249   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00250                             Attribute::AttrKind Attr) const;
00251 
00252   /// \brief Add an attribute to the attribute set at the given index. Since
00253   /// attribute sets are immutable, this returns a new set.
00254   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00255                             StringRef Kind) const;
00256   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00257                             StringRef Kind, StringRef Value) const;
00258 
00259   /// \brief Add attributes to the attribute set at the given index. Since
00260   /// attribute sets are immutable, this returns a new set.
00261   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
00262                              AttributeSet Attrs) const;
00263 
00264   /// \brief Remove the specified attribute at the specified index from this
00265   /// attribute list. Since attribute lists are immutable, this returns the new
00266   /// list.
00267   AttributeSet removeAttribute(LLVMContext &C, unsigned Index, 
00268                                Attribute::AttrKind Attr) const;
00269 
00270   /// \brief Remove the specified attributes at the specified index from this
00271   /// attribute list. Since attribute lists are immutable, this returns the new
00272   /// list.
00273   AttributeSet removeAttributes(LLVMContext &C, unsigned Index, 
00274                                 AttributeSet Attrs) const;
00275 
00276   //===--------------------------------------------------------------------===//
00277   // AttributeSet Accessors
00278   //===--------------------------------------------------------------------===//
00279 
00280   /// \brief Retrieve the LLVM context.
00281   LLVMContext &getContext() const;
00282 
00283   /// \brief The attributes for the specified index are returned.
00284   AttributeSet getParamAttributes(unsigned Index) const;
00285 
00286   /// \brief The attributes for the ret value are returned.
00287   AttributeSet getRetAttributes() const;
00288 
00289   /// \brief The function attributes are returned.
00290   AttributeSet getFnAttributes() const;
00291 
00292   /// \brief Return true if the attribute exists at the given index.
00293   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00294 
00295   /// \brief Return true if the attribute exists at the given index.
00296   bool hasAttribute(unsigned Index, StringRef Kind) const;
00297 
00298   /// \brief Return true if attribute exists at the given index.
00299   bool hasAttributes(unsigned Index) const;
00300 
00301   /// \brief Return true if the specified attribute is set for at least one
00302   /// parameter or for the return value.
00303   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
00304 
00305   /// \brief Return the attribute object that exists at the given index.
00306   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00307 
00308   /// \brief Return the attribute object that exists at the given index.
00309   Attribute getAttribute(unsigned Index, StringRef Kind) const;
00310 
00311   /// \brief Return the alignment for the specified function parameter.
00312   unsigned getParamAlignment(unsigned Index) const;
00313 
00314   /// \brief Get the stack alignment.
00315   unsigned getStackAlignment(unsigned Index) const;
00316 
00317   /// \brief Return the attributes at the index as a string.
00318   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
00319 
00320   typedef ArrayRef<Attribute>::iterator iterator;
00321 
00322   iterator begin(unsigned Slot) const;
00323   iterator end(unsigned Slot) const;
00324 
00325   /// operator==/!= - Provide equality predicates.
00326   bool operator==(const AttributeSet &RHS) const {
00327     return pImpl == RHS.pImpl;
00328   }
00329   bool operator!=(const AttributeSet &RHS) const {
00330     return pImpl != RHS.pImpl;
00331   }
00332 
00333   //===--------------------------------------------------------------------===//
00334   // AttributeSet Introspection
00335   //===--------------------------------------------------------------------===//
00336 
00337   // FIXME: Remove this.
00338   uint64_t Raw(unsigned Index) const;
00339 
00340   /// \brief Return a raw pointer that uniquely identifies this attribute list.
00341   void *getRawPointer() const {
00342     return pImpl;
00343   }
00344 
00345   /// \brief Return true if there are no attributes.
00346   bool isEmpty() const {
00347     return getNumSlots() == 0;
00348   }
00349 
00350   /// \brief Return the number of slots used in this attribute list.  This is
00351   /// the number of arguments that have an attribute set on them (including the
00352   /// function itself).
00353   unsigned getNumSlots() const;
00354 
00355   /// \brief Return the index for the given slot.
00356   unsigned getSlotIndex(unsigned Slot) const;
00357 
00358   /// \brief Return the attributes at the given slot.
00359   AttributeSet getSlotAttributes(unsigned Slot) const;
00360 
00361   void dump() const;
00362 };
00363 
00364 //===----------------------------------------------------------------------===//
00365 /// \class
00366 /// \brief Provide DenseMapInfo for AttributeSet.
00367 template<> struct DenseMapInfo<AttributeSet> {
00368   static inline AttributeSet getEmptyKey() {
00369     uintptr_t Val = static_cast<uintptr_t>(-1);
00370     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00371     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00372   }
00373   static inline AttributeSet getTombstoneKey() {
00374     uintptr_t Val = static_cast<uintptr_t>(-2);
00375     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00376     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00377   }
00378   static unsigned getHashValue(AttributeSet AS) {
00379     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
00380            (unsigned((uintptr_t)AS.pImpl) >> 9);
00381   }
00382   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
00383 };
00384 
00385 //===----------------------------------------------------------------------===//
00386 /// \class
00387 /// \brief This class is used in conjunction with the Attribute::get method to
00388 /// create an Attribute object. The object itself is uniquified. The Builder's
00389 /// value, however, is not. So this can be used as a quick way to test for
00390 /// equality, presence of attributes, etc.
00391 class AttrBuilder {
00392   std::bitset<Attribute::EndAttrKinds> Attrs;
00393   std::map<std::string, std::string> TargetDepAttrs;
00394   uint64_t Alignment;
00395   uint64_t StackAlignment;
00396 public:
00397   AttrBuilder() : Attrs(0), Alignment(0), StackAlignment(0) {}
00398   explicit AttrBuilder(uint64_t Val)
00399     : Attrs(0), Alignment(0), StackAlignment(0) {
00400     addRawValue(Val);
00401   }
00402   AttrBuilder(const Attribute &A) : Attrs(0), Alignment(0), StackAlignment(0) {
00403     addAttribute(A);
00404   }
00405   AttrBuilder(AttributeSet AS, unsigned Idx);
00406 
00407   void clear();
00408 
00409   /// \brief Add an attribute to the builder.
00410   AttrBuilder &addAttribute(Attribute::AttrKind Val);
00411 
00412   /// \brief Add the Attribute object to the builder.
00413   AttrBuilder &addAttribute(Attribute A);
00414 
00415   /// \brief Add the target-dependent attribute to the builder.
00416   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
00417 
00418   /// \brief Remove an attribute from the builder.
00419   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
00420 
00421   /// \brief Remove the attributes from the builder.
00422   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
00423 
00424   /// \brief Remove the target-dependent attribute to the builder.
00425   AttrBuilder &removeAttribute(StringRef A);
00426 
00427   /// \brief Add the attributes from the builder.
00428   AttrBuilder &merge(const AttrBuilder &B);
00429 
00430   /// \brief Return true if the builder has the specified attribute.
00431   bool contains(Attribute::AttrKind A) const {
00432     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
00433     return Attrs[A];
00434   }
00435 
00436   /// \brief Return true if the builder has the specified target-dependent
00437   /// attribute.
00438   bool contains(StringRef A) const;
00439 
00440   /// \brief Return true if the builder has IR-level attributes.
00441   bool hasAttributes() const;
00442 
00443   /// \brief Return true if the builder has any attribute that's in the
00444   /// specified attribute.
00445   bool hasAttributes(AttributeSet A, uint64_t Index) const;
00446 
00447   /// \brief Return true if the builder has an alignment attribute.
00448   bool hasAlignmentAttr() const;
00449 
00450   /// \brief Retrieve the alignment attribute, if it exists.
00451   uint64_t getAlignment() const { return Alignment; }
00452 
00453   /// \brief Retrieve the stack alignment attribute, if it exists.
00454   uint64_t getStackAlignment() const { return StackAlignment; }
00455 
00456   /// \brief This turns an int alignment (which must be a power of 2) into the
00457   /// form used internally in Attribute.
00458   AttrBuilder &addAlignmentAttr(unsigned Align);
00459 
00460   /// \brief This turns an int stack alignment (which must be a power of 2) into
00461   /// the form used internally in Attribute.
00462   AttrBuilder &addStackAlignmentAttr(unsigned Align);
00463 
00464   /// \brief Return true if the builder contains no target-independent
00465   /// attributes.
00466   bool empty() const { return Attrs.none(); }
00467 
00468   // Iterators for target-dependent attributes.
00469   typedef std::pair<std::string, std::string>                td_type;
00470   typedef std::map<std::string, std::string>::iterator       td_iterator;
00471   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
00472 
00473   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
00474   td_iterator td_end()               { return TargetDepAttrs.end(); }
00475 
00476   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
00477   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
00478 
00479   bool td_empty() const              { return TargetDepAttrs.empty(); }
00480 
00481   bool operator==(const AttrBuilder &B);
00482   bool operator!=(const AttrBuilder &B) {
00483     return !(*this == B);
00484   }
00485 
00486   // FIXME: Remove this in 4.0.
00487 
00488   /// \brief Add the raw value to the internal representation.
00489   AttrBuilder &addRawValue(uint64_t Val);
00490 };
00491 
00492 namespace AttributeFuncs {
00493 
00494 /// \brief Which attributes cannot be applied to a type.
00495 AttributeSet typeIncompatible(Type *Ty, uint64_t Index);
00496 
00497 } // end AttributeFuncs namespace
00498 
00499 } // end llvm namespace
00500 
00501 #endif