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