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 Function;
00037 class LLVMContext;
00038 class Type;
00039 
00040 //===----------------------------------------------------------------------===//
00041 /// \class
00042 /// \brief Functions, function parameters, and return types can have attributes
00043 /// to indicate how they should be treated by optimizations and code
00044 /// generation. This class represents one of those attributes. It's light-weight
00045 /// and should be passed around by-value.
00046 class Attribute {
00047 public:
00048   /// This enumeration lists the attributes that can be associated with
00049   /// parameters, function results, or the function itself.
00050   ///
00051   /// Note: The `uwtable' attribute is about the ABI or the user mandating an
00052   /// entry in the unwind table. The `nounwind' attribute is about an exception
00053   /// passing by the function.
00054   ///
00055   /// In a theoretical system that uses tables for profiling and SjLj for
00056   /// exceptions, they would be fully independent. In a normal system that uses
00057   /// tables for both, the semantics are:
00058   ///
00059   /// nil                = Needs an entry because an exception might pass by.
00060   /// nounwind           = No need for an entry
00061   /// uwtable            = Needs an entry because the ABI says so and because
00062   ///                      an exception might pass by.
00063   /// uwtable + nounwind = Needs an entry because the ABI says so.
00064 
00065   enum AttrKind {
00066     // IR-Level Attributes
00067     None,                  ///< No attributes have been set
00068     #define GET_ATTR_ENUM
00069     #include "llvm/IR/Attributes.inc"
00070     EndAttrKinds           ///< Sentinal value useful for loops
00071   };
00072 
00073 private:
00074   AttributeImpl *pImpl;
00075   Attribute(AttributeImpl *A) : pImpl(A) {}
00076 
00077 public:
00078   Attribute() : pImpl(nullptr) {}
00079 
00080   //===--------------------------------------------------------------------===//
00081   // Attribute Construction
00082   //===--------------------------------------------------------------------===//
00083 
00084   /// \brief Return a uniquified Attribute object.
00085   static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
00086   static Attribute get(LLVMContext &Context, StringRef Kind,
00087                        StringRef Val = StringRef());
00088 
00089   /// \brief Return a uniquified Attribute object that has the specific
00090   /// alignment set.
00091   static Attribute getWithAlignment(LLVMContext &Context, uint64_t Align);
00092   static Attribute getWithStackAlignment(LLVMContext &Context, uint64_t Align);
00093   static Attribute getWithDereferenceableBytes(LLVMContext &Context,
00094                                               uint64_t Bytes);
00095   static Attribute getWithDereferenceableOrNullBytes(LLVMContext &Context,
00096                                                      uint64_t Bytes);
00097 
00098   //===--------------------------------------------------------------------===//
00099   // Attribute Accessors
00100   //===--------------------------------------------------------------------===//
00101 
00102   /// \brief Return true if the attribute is an Attribute::AttrKind type.
00103   bool isEnumAttribute() const;
00104 
00105   /// \brief Return true if the attribute is an integer attribute.
00106   bool isIntAttribute() const;
00107 
00108   /// \brief Return true if the attribute is a string (target-dependent)
00109   /// attribute.
00110   bool isStringAttribute() const;
00111 
00112   /// \brief Return true if the attribute is present.
00113   bool hasAttribute(AttrKind Val) const;
00114 
00115   /// \brief Return true if the target-dependent attribute is present.
00116   bool hasAttribute(StringRef Val) const;
00117 
00118   /// \brief Return the attribute's kind as an enum (Attribute::AttrKind). This
00119   /// requires the attribute to be an enum or integer attribute.
00120   Attribute::AttrKind getKindAsEnum() const;
00121 
00122   /// \brief Return the attribute's value as an integer. This requires that the
00123   /// attribute be an integer attribute.
00124   uint64_t getValueAsInt() const;
00125 
00126   /// \brief Return the attribute's kind as a string. This requires the
00127   /// attribute to be a string attribute.
00128   StringRef getKindAsString() const;
00129 
00130   /// \brief Return the attribute's value as a string. This requires the
00131   /// attribute to be a string attribute.
00132   StringRef getValueAsString() const;
00133 
00134   /// \brief Returns the alignment field of an attribute as a byte alignment
00135   /// value.
00136   unsigned getAlignment() const;
00137 
00138   /// \brief Returns the stack alignment field of an attribute as a byte
00139   /// alignment value.
00140   unsigned getStackAlignment() const;
00141 
00142   /// \brief Returns the number of dereferenceable bytes from the
00143   /// dereferenceable attribute.
00144   uint64_t getDereferenceableBytes() const;
00145 
00146   /// \brief Returns the number of dereferenceable_or_null bytes from the
00147   /// dereferenceable_or_null attribute.
00148   uint64_t getDereferenceableOrNullBytes() const;
00149 
00150   /// \brief The Attribute is converted to a string of equivalent mnemonic. This
00151   /// is, presumably, for writing out the mnemonics for the assembly writer.
00152   std::string getAsString(bool InAttrGrp = false) const;
00153 
00154   /// \brief Equality and non-equality operators.
00155   bool operator==(Attribute A) const { return pImpl == A.pImpl; }
00156   bool operator!=(Attribute A) const { return pImpl != A.pImpl; }
00157 
00158   /// \brief Less-than operator. Useful for sorting the attributes list.
00159   bool operator<(Attribute A) const;
00160 
00161   void Profile(FoldingSetNodeID &ID) const {
00162     ID.AddPointer(pImpl);
00163   }
00164 };
00165 
00166 //===----------------------------------------------------------------------===//
00167 /// \class
00168 /// \brief This class holds the attributes for a function, its return value, and
00169 /// its parameters. You access the attributes for each of them via an index into
00170 /// the AttributeSet object. The function attributes are at index
00171 /// `AttributeSet::FunctionIndex', the return value is at index
00172 /// `AttributeSet::ReturnIndex', and the attributes for the parameters start at
00173 /// index `1'.
00174 class AttributeSet {
00175 public:
00176   enum AttrIndex : unsigned {
00177     ReturnIndex = 0U,
00178     FunctionIndex = ~0U
00179   };
00180 
00181 private:
00182   friend class AttrBuilder;
00183   friend class AttributeSetImpl;
00184   template <typename Ty> friend struct DenseMapInfo;
00185 
00186   /// \brief The attributes that we are managing. This can be null to represent
00187   /// the empty attributes list.
00188   AttributeSetImpl *pImpl;
00189 
00190   /// \brief The attributes for the specified index are returned.
00191   AttributeSetNode *getAttributes(unsigned Index) const;
00192 
00193   /// \brief Create an AttributeSet with the specified parameters in it.
00194   static AttributeSet get(LLVMContext &C,
00195                           ArrayRef<std::pair<unsigned, Attribute> > Attrs);
00196   static AttributeSet get(LLVMContext &C,
00197                           ArrayRef<std::pair<unsigned,
00198                                              AttributeSetNode*> > Attrs);
00199 
00200   static AttributeSet getImpl(LLVMContext &C,
00201                               ArrayRef<std::pair<unsigned,
00202                                                  AttributeSetNode*> > Attrs);
00203 
00204   explicit AttributeSet(AttributeSetImpl *LI) : pImpl(LI) {}
00205 
00206 public:
00207   AttributeSet() : pImpl(nullptr) {}
00208 
00209   //===--------------------------------------------------------------------===//
00210   // AttributeSet Construction and Mutation
00211   //===--------------------------------------------------------------------===//
00212 
00213   /// \brief Return an AttributeSet with the specified parameters in it.
00214   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeSet> Attrs);
00215   static AttributeSet get(LLVMContext &C, unsigned Index,
00216                           ArrayRef<Attribute::AttrKind> Kind);
00217   static AttributeSet get(LLVMContext &C, unsigned Index, const AttrBuilder &B);
00218 
00219   /// \brief Add an attribute to the attribute set at the given index. Because
00220   /// attribute sets are immutable, this returns a new set.
00221   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00222                             Attribute::AttrKind Attr) const;
00223 
00224   /// \brief Add an attribute to the attribute set at the given index. Because
00225   /// attribute sets are immutable, this returns a new set.
00226   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00227                             StringRef Kind) const;
00228   AttributeSet addAttribute(LLVMContext &C, unsigned Index,
00229                             StringRef Kind, StringRef Value) const;
00230 
00231   /// Add an attribute to the attribute set at the given indices. Because
00232   /// attribute sets are immutable, this returns a new set.
00233   AttributeSet addAttribute(LLVMContext &C, ArrayRef<unsigned> Indices,
00234                             Attribute A) const;
00235 
00236   /// \brief Add attributes to the attribute set at the given index. Because
00237   /// attribute sets are immutable, this returns a new set.
00238   AttributeSet addAttributes(LLVMContext &C, unsigned Index,
00239                              AttributeSet Attrs) const;
00240 
00241   /// \brief Remove the specified attribute at the specified index from this
00242   /// attribute list. Because attribute lists are immutable, this returns the
00243   /// new list.
00244   AttributeSet removeAttribute(LLVMContext &C, unsigned Index,
00245                                Attribute::AttrKind Attr) const;
00246 
00247   /// \brief Remove the specified attributes at the specified index from this
00248   /// attribute list. Because attribute lists are immutable, this returns the
00249   /// new list.
00250   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
00251                                 AttributeSet Attrs) const;
00252 
00253   /// \brief Remove the specified attributes at the specified index from this
00254   /// attribute list. Because attribute lists are immutable, this returns the
00255   /// new list.
00256   AttributeSet removeAttributes(LLVMContext &C, unsigned Index,
00257                                 const AttrBuilder &Attrs) const;
00258 
00259   /// \brief Add the dereferenceable attribute to the attribute set at the given
00260   /// index. Because attribute sets are immutable, this returns a new set.
00261   AttributeSet addDereferenceableAttr(LLVMContext &C, unsigned Index,
00262                                       uint64_t Bytes) const;
00263 
00264   /// \brief Add the dereferenceable_or_null attribute to the attribute set at
00265   /// the given index. Because attribute sets are immutable, this returns a new
00266   /// set.
00267   AttributeSet addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
00268                                             uint64_t Bytes) const;
00269 
00270   //===--------------------------------------------------------------------===//
00271   // AttributeSet Accessors
00272   //===--------------------------------------------------------------------===//
00273 
00274   /// \brief Retrieve the LLVM context.
00275   LLVMContext &getContext() const;
00276 
00277   /// \brief The attributes for the specified index are returned.
00278   AttributeSet getParamAttributes(unsigned Index) const;
00279 
00280   /// \brief The attributes for the ret value are returned.
00281   AttributeSet getRetAttributes() const;
00282 
00283   /// \brief The function attributes are returned.
00284   AttributeSet getFnAttributes() const;
00285 
00286   /// \brief Return true if the attribute exists at the given index.
00287   bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00288 
00289   /// \brief Return true if the attribute exists at the given index.
00290   bool hasAttribute(unsigned Index, StringRef Kind) const;
00291 
00292   /// \brief Return true if attribute exists at the given index.
00293   bool hasAttributes(unsigned Index) const;
00294 
00295   /// \brief Return true if the specified attribute is set for at least one
00296   /// parameter or for the return value.
00297   bool hasAttrSomewhere(Attribute::AttrKind Attr) const;
00298 
00299   /// \brief Return the attribute object that exists at the given index.
00300   Attribute getAttribute(unsigned Index, Attribute::AttrKind Kind) const;
00301 
00302   /// \brief Return the attribute object that exists at the given index.
00303   Attribute getAttribute(unsigned Index, StringRef Kind) const;
00304 
00305   /// \brief Return the alignment for the specified function parameter.
00306   unsigned getParamAlignment(unsigned Index) const;
00307 
00308   /// \brief Get the stack alignment.
00309   unsigned getStackAlignment(unsigned Index) const;
00310 
00311   /// \brief Get the number of dereferenceable bytes (or zero if unknown).
00312   uint64_t getDereferenceableBytes(unsigned Index) const;
00313 
00314   /// \brief Get the number of dereferenceable_or_null bytes (or zero if
00315   /// unknown).
00316   uint64_t getDereferenceableOrNullBytes(unsigned Index) const;
00317 
00318   /// \brief Return the attributes at the index as a string.
00319   std::string getAsString(unsigned Index, bool InAttrGrp = false) const;
00320 
00321   typedef ArrayRef<Attribute>::iterator iterator;
00322 
00323   iterator begin(unsigned Slot) const;
00324   iterator end(unsigned Slot) const;
00325 
00326   /// operator==/!= - Provide equality predicates.
00327   bool operator==(const AttributeSet &RHS) const {
00328     return pImpl == RHS.pImpl;
00329   }
00330   bool operator!=(const AttributeSet &RHS) const {
00331     return pImpl != RHS.pImpl;
00332   }
00333 
00334   //===--------------------------------------------------------------------===//
00335   // AttributeSet Introspection
00336   //===--------------------------------------------------------------------===//
00337 
00338   // FIXME: Remove this.
00339   uint64_t Raw(unsigned Index) const;
00340 
00341   /// \brief Return a raw pointer that uniquely identifies this attribute list.
00342   void *getRawPointer() const {
00343     return pImpl;
00344   }
00345 
00346   /// \brief Return true if there are no attributes.
00347   bool isEmpty() const {
00348     return getNumSlots() == 0;
00349   }
00350 
00351   /// \brief Return the number of slots used in this attribute list.  This is
00352   /// the number of arguments that have an attribute set on them (including the
00353   /// function itself).
00354   unsigned getNumSlots() const;
00355 
00356   /// \brief Return the index for the given slot.
00357   unsigned getSlotIndex(unsigned Slot) const;
00358 
00359   /// \brief Return the attributes at the given slot.
00360   AttributeSet getSlotAttributes(unsigned Slot) const;
00361 
00362   void dump() const;
00363 };
00364 
00365 //===----------------------------------------------------------------------===//
00366 /// \class
00367 /// \brief Provide DenseMapInfo for AttributeSet.
00368 template<> struct DenseMapInfo<AttributeSet> {
00369   static inline AttributeSet getEmptyKey() {
00370     uintptr_t Val = static_cast<uintptr_t>(-1);
00371     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00372     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00373   }
00374   static inline AttributeSet getTombstoneKey() {
00375     uintptr_t Val = static_cast<uintptr_t>(-2);
00376     Val <<= PointerLikeTypeTraits<void*>::NumLowBitsAvailable;
00377     return AttributeSet(reinterpret_cast<AttributeSetImpl*>(Val));
00378   }
00379   static unsigned getHashValue(AttributeSet AS) {
00380     return (unsigned((uintptr_t)AS.pImpl) >> 4) ^
00381            (unsigned((uintptr_t)AS.pImpl) >> 9);
00382   }
00383   static bool isEqual(AttributeSet LHS, AttributeSet RHS) { return LHS == RHS; }
00384 };
00385 
00386 //===----------------------------------------------------------------------===//
00387 /// \class
00388 /// \brief This class is used in conjunction with the Attribute::get method to
00389 /// create an Attribute object. The object itself is uniquified. The Builder's
00390 /// value, however, is not. So this can be used as a quick way to test for
00391 /// equality, presence of attributes, etc.
00392 class AttrBuilder {
00393   std::bitset<Attribute::EndAttrKinds> Attrs;
00394   std::map<std::string, std::string> TargetDepAttrs;
00395   uint64_t Alignment;
00396   uint64_t StackAlignment;
00397   uint64_t DerefBytes;
00398   uint64_t DerefOrNullBytes;
00399 
00400 public:
00401   AttrBuilder()
00402       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
00403         DerefOrNullBytes(0) {}
00404   explicit AttrBuilder(uint64_t Val)
00405       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
00406         DerefOrNullBytes(0) {
00407     addRawValue(Val);
00408   }
00409   AttrBuilder(const Attribute &A)
00410       : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
00411         DerefOrNullBytes(0) {
00412     addAttribute(A);
00413   }
00414   AttrBuilder(AttributeSet AS, unsigned Idx);
00415 
00416   void clear();
00417 
00418   /// \brief Add an attribute to the builder.
00419   AttrBuilder &addAttribute(Attribute::AttrKind Val);
00420 
00421   /// \brief Add the Attribute object to the builder.
00422   AttrBuilder &addAttribute(Attribute A);
00423 
00424   /// \brief Add the target-dependent attribute to the builder.
00425   AttrBuilder &addAttribute(StringRef A, StringRef V = StringRef());
00426 
00427   /// \brief Remove an attribute from the builder.
00428   AttrBuilder &removeAttribute(Attribute::AttrKind Val);
00429 
00430   /// \brief Remove the attributes from the builder.
00431   AttrBuilder &removeAttributes(AttributeSet A, uint64_t Index);
00432 
00433   /// \brief Remove the target-dependent attribute to the builder.
00434   AttrBuilder &removeAttribute(StringRef A);
00435 
00436   /// \brief Add the attributes from the builder.
00437   AttrBuilder &merge(const AttrBuilder &B);
00438 
00439   /// \brief Remove the attributes from the builder.
00440   AttrBuilder &remove(const AttrBuilder &B);
00441 
00442   /// \brief Return true if the builder has any attribute that's in the
00443   /// specified builder.
00444   bool overlaps(const AttrBuilder &B) const;
00445 
00446   /// \brief Return true if the builder has the specified attribute.
00447   bool contains(Attribute::AttrKind A) const {
00448     assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
00449     return Attrs[A];
00450   }
00451 
00452   /// \brief Return true if the builder has the specified target-dependent
00453   /// attribute.
00454   bool contains(StringRef A) const;
00455 
00456   /// \brief Return true if the builder has IR-level attributes.
00457   bool hasAttributes() const;
00458 
00459   /// \brief Return true if the builder has any attribute that's in the
00460   /// specified attribute.
00461   bool hasAttributes(AttributeSet A, uint64_t Index) const;
00462 
00463   /// \brief Return true if the builder has an alignment attribute.
00464   bool hasAlignmentAttr() const;
00465 
00466   /// \brief Retrieve the alignment attribute, if it exists.
00467   uint64_t getAlignment() const { return Alignment; }
00468 
00469   /// \brief Retrieve the stack alignment attribute, if it exists.
00470   uint64_t getStackAlignment() const { return StackAlignment; }
00471 
00472   /// \brief Retrieve the number of dereferenceable bytes, if the
00473   /// dereferenceable attribute exists (zero is returned otherwise).
00474   uint64_t getDereferenceableBytes() const { return DerefBytes; }
00475 
00476   /// \brief Retrieve the number of dereferenceable_or_null bytes, if the
00477   /// dereferenceable_or_null attribute exists (zero is returned otherwise).
00478   uint64_t getDereferenceableOrNullBytes() const { return DerefOrNullBytes; }
00479 
00480   /// \brief This turns an int alignment (which must be a power of 2) into the
00481   /// form used internally in Attribute.
00482   AttrBuilder &addAlignmentAttr(unsigned Align);
00483 
00484   /// \brief This turns an int stack alignment (which must be a power of 2) into
00485   /// the form used internally in Attribute.
00486   AttrBuilder &addStackAlignmentAttr(unsigned Align);
00487 
00488   /// \brief This turns the number of dereferenceable bytes into the form used
00489   /// internally in Attribute.
00490   AttrBuilder &addDereferenceableAttr(uint64_t Bytes);
00491 
00492   /// \brief This turns the number of dereferenceable_or_null bytes into the
00493   /// form used internally in Attribute.
00494   AttrBuilder &addDereferenceableOrNullAttr(uint64_t Bytes);
00495 
00496   /// \brief Return true if the builder contains no target-independent
00497   /// attributes.
00498   bool empty() const { return Attrs.none(); }
00499 
00500   // Iterators for target-dependent attributes.
00501   typedef std::pair<std::string, std::string>                td_type;
00502   typedef std::map<std::string, std::string>::iterator       td_iterator;
00503   typedef std::map<std::string, std::string>::const_iterator td_const_iterator;
00504   typedef llvm::iterator_range<td_iterator>                  td_range;
00505   typedef llvm::iterator_range<td_const_iterator>            td_const_range;
00506 
00507   td_iterator td_begin()             { return TargetDepAttrs.begin(); }
00508   td_iterator td_end()               { return TargetDepAttrs.end(); }
00509 
00510   td_const_iterator td_begin() const { return TargetDepAttrs.begin(); }
00511   td_const_iterator td_end() const   { return TargetDepAttrs.end(); }
00512 
00513   td_range td_attrs() { return td_range(td_begin(), td_end()); }
00514   td_const_range td_attrs() const {
00515     return td_const_range(td_begin(), td_end());
00516   }
00517 
00518   bool td_empty() const              { return TargetDepAttrs.empty(); }
00519 
00520   bool operator==(const AttrBuilder &B);
00521   bool operator!=(const AttrBuilder &B) {
00522     return !(*this == B);
00523   }
00524 
00525   // FIXME: Remove this in 4.0.
00526 
00527   /// \brief Add the raw value to the internal representation.
00528   AttrBuilder &addRawValue(uint64_t Val);
00529 };
00530 
00531 namespace AttributeFuncs {
00532 
00533 /// \brief Which attributes cannot be applied to a type.
00534 AttrBuilder typeIncompatible(Type *Ty);
00535 
00536 /// \returns Return true if the two functions have compatible target-independent
00537 /// attributes for inlining purposes.
00538 bool areInlineCompatible(const Function &Caller, const Function &Callee);
00539 
00540 /// \brief Merge caller's and callee's attributes.
00541 void mergeAttributesForInlining(Function &Caller, const Function &Callee);
00542 
00543 } // end AttributeFuncs namespace
00544 
00545 } // end llvm namespace
00546 
00547 #endif