LLVM API Documentation

Metadata.h
Go to the documentation of this file.
00001 //===-- llvm/Metadata.h - Metadata definitions ------------------*- 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 /// This file contains the declarations for metadata subclasses.
00012 /// They represent the different flavors of metadata that live in LLVM.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_IR_METADATA_H
00017 #define LLVM_IR_METADATA_H
00018 
00019 #include "llvm/ADT/ArrayRef.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/FoldingSet.h"
00022 #include "llvm/ADT/ilist_node.h"
00023 #include "llvm/ADT/iterator_range.h"
00024 #include "llvm/IR/Value.h"
00025 
00026 namespace llvm {
00027 class LLVMContext;
00028 class Module;
00029 template<typename ValueSubClass, typename ItemParentClass>
00030   class SymbolTableListTraits;
00031 
00032 
00033 enum LLVMConstants : uint32_t {
00034   DEBUG_METADATA_VERSION = 2  // Current debug info version number.
00035 };
00036 
00037 //===----------------------------------------------------------------------===//
00038 /// \brief A single uniqued string.
00039 ///
00040 /// These are used to efficiently contain a byte sequence for metadata.
00041 /// MDString is always unnamed.
00042 class MDString : public Value {
00043   virtual void anchor();
00044   MDString(const MDString &) LLVM_DELETED_FUNCTION;
00045 
00046   explicit MDString(LLVMContext &C);
00047 public:
00048   static MDString *get(LLVMContext &Context, StringRef Str);
00049   static MDString *get(LLVMContext &Context, const char *Str) {
00050     return get(Context, Str ? StringRef(Str) : StringRef());
00051   }
00052 
00053   StringRef getString() const { return getName(); }
00054 
00055   unsigned getLength() const { return (unsigned)getName().size(); }
00056 
00057   typedef StringRef::iterator iterator;
00058 
00059   /// \brief Pointer to the first byte of the string.
00060   iterator begin() const { return getName().begin(); }
00061 
00062   /// \brief Pointer to one byte past the end of the string.
00063   iterator end() const { return getName().end(); }
00064 
00065   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
00066   static bool classof(const Value *V) {
00067     return V->getValueID() == MDStringVal;
00068   }
00069 };
00070 
00071 /// \brief A collection of metadata nodes that might be associated with a
00072 /// memory access used by the alias-analysis infrastructure.
00073 struct AAMDNodes {
00074   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
00075                      MDNode *N = nullptr)
00076       : TBAA(T), Scope(S), NoAlias(N) {}
00077 
00078   bool operator==(const AAMDNodes &A) const {
00079     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
00080   }
00081 
00082   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
00083 
00084   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
00085 
00086   /// \brief The tag for type-based alias analysis.
00087   MDNode *TBAA;
00088 
00089   /// \brief The tag for alias scope specification (used with noalias).
00090   MDNode *Scope;
00091 
00092   /// \brief The tag specifying the noalias scope.
00093   MDNode *NoAlias;
00094 };
00095 
00096 // Specialize DenseMapInfo for AAMDNodes.
00097 template<>
00098 struct DenseMapInfo<AAMDNodes> {
00099   static inline AAMDNodes getEmptyKey() {
00100     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
00101   }
00102   static inline AAMDNodes getTombstoneKey() {
00103     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
00104   }
00105   static unsigned getHashValue(const AAMDNodes &Val) {
00106     return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
00107            DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
00108            DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
00109   }
00110   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
00111     return LHS == RHS;
00112   }
00113 };
00114 
00115 class MDNodeOperand;
00116 
00117 //===----------------------------------------------------------------------===//
00118 /// \brief A tuple of other values.
00119 class MDNode : public Value, public FoldingSetNode {
00120   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
00121   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
00122   friend class MDNodeOperand;
00123   friend class LLVMContextImpl;
00124   friend struct FoldingSetTrait<MDNode>;
00125 
00126   /// \brief If the MDNode is uniqued cache the hash to speed up lookup.
00127   unsigned Hash;
00128 
00129   /// \brief Subclass data enums.
00130   enum {
00131     /// FunctionLocalBit - This bit is set if this MDNode is function local.
00132     /// This is true when it (potentially transitively) contains a reference to
00133     /// something in a function, like an argument, basicblock, or instruction.
00134     FunctionLocalBit = 1 << 0,
00135 
00136     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
00137     /// have a null operand.
00138     NotUniquedBit    = 1 << 1,
00139 
00140     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
00141     /// that the node isn't being destroyed with a plain 'delete'.
00142     DestroyFlag      = 1 << 2
00143   };
00144 
00145   /// \brief FunctionLocal enums.
00146   enum FunctionLocalness {
00147     FL_Unknown = -1,
00148     FL_No = 0,
00149     FL_Yes = 1
00150   };
00151 
00152   /// \brief Replace each instance of the given operand with a new value.
00153   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
00154   ~MDNode();
00155 
00156   MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
00157 
00158   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
00159                            FunctionLocalness FL, bool Insert = true);
00160 public:
00161   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
00162   /// \brief Construct MDNode with an explicit function-localness.
00163   ///
00164   /// Don't analyze Vals; trust isFunctionLocal.
00165   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
00166                                        ArrayRef<Value*> Vals,
00167                                        bool isFunctionLocal);
00168 
00169   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
00170 
00171   /// \brief Return a temporary MDNode
00172   ///
00173   /// For use in constructing cyclic MDNode structures. A temporary MDNode is
00174   /// not uniqued, may be RAUW'd, and must be manually deleted with
00175   /// deleteTemporary.
00176   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
00177 
00178   /// \brief Deallocate a node created by getTemporary.
00179   ///
00180   /// The node must not have any users.
00181   static void deleteTemporary(MDNode *N);
00182 
00183   /// \brief Replace a specific operand.
00184   void replaceOperandWith(unsigned i, Value *NewVal);
00185 
00186   /// \brief Return specified operand.
00187   Value *getOperand(unsigned i) const LLVM_READONLY;
00188 
00189   /// \brief Return number of MDNode operands.
00190   unsigned getNumOperands() const { return NumOperands; }
00191 
00192   /// \brief Return whether MDNode is local to a function.
00193   bool isFunctionLocal() const {
00194     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
00195   }
00196 
00197   /// \brief Return the first function-local operand's function.
00198   ///
00199   /// If this metadata is function-local and recursively has a function-local
00200   /// operand, return the first such operand's parent function.  Otherwise,
00201   /// return null. getFunction() should not be used for performance- critical
00202   /// code because it recursively visits all the MDNode's operands.
00203   const Function *getFunction() const;
00204 
00205   /// \brief Calculate a unique identifier for this MDNode.
00206   void Profile(FoldingSetNodeID &ID) const;
00207 
00208   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
00209   static bool classof(const Value *V) {
00210     return V->getValueID() == MDNodeVal;
00211   }
00212 
00213   /// \brief Check whether MDNode is a vtable access.
00214   bool isTBAAVtableAccess() const;
00215 
00216   /// \brief Methods for metadata merging.
00217   static MDNode *concatenate(MDNode *A, MDNode *B);
00218   static MDNode *intersect(MDNode *A, MDNode *B);
00219   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
00220   static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
00221   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
00222   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
00223 private:
00224   /// \brief Delete this node.  Only when there are no uses.
00225   void destroy();
00226 
00227   bool isNotUniqued() const {
00228     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
00229   }
00230   void setIsNotUniqued();
00231 
00232   // Shadow Value::setValueSubclassData with a private forwarding method so that
00233   // any future subclasses cannot accidentally use it.
00234   void setValueSubclassData(unsigned short D) {
00235     Value::setValueSubclassData(D);
00236   }
00237 };
00238 
00239 //===----------------------------------------------------------------------===//
00240 /// \brief A tuple of MDNodes.
00241 ///
00242 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
00243 /// to modules, have names, and contain lists of MDNodes.
00244 class NamedMDNode : public ilist_node<NamedMDNode> {
00245   friend class SymbolTableListTraits<NamedMDNode, Module>;
00246   friend struct ilist_traits<NamedMDNode>;
00247   friend class LLVMContextImpl;
00248   friend class Module;
00249   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
00250 
00251   std::string Name;
00252   Module *Parent;
00253   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
00254 
00255   void setParent(Module *M) { Parent = M; }
00256 
00257   explicit NamedMDNode(const Twine &N);
00258 
00259   template<class T1, class T2>
00260   class op_iterator_impl :
00261       public std::iterator<std::bidirectional_iterator_tag, T2> {
00262     const NamedMDNode *Node;
00263     unsigned Idx;
00264     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
00265 
00266     friend class NamedMDNode;
00267 
00268   public:
00269     op_iterator_impl() : Node(nullptr), Idx(0) { }
00270 
00271     bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
00272     bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
00273     op_iterator_impl &operator++() {
00274       ++Idx;
00275       return *this;
00276     }
00277     op_iterator_impl operator++(int) {
00278       op_iterator_impl tmp(*this);
00279       operator++();
00280       return tmp;
00281     }
00282     op_iterator_impl &operator--() {
00283       --Idx;
00284       return *this;
00285     }
00286     op_iterator_impl operator--(int) {
00287       op_iterator_impl tmp(*this);
00288       operator--();
00289       return tmp;
00290     }
00291 
00292     T1 operator*() const { return Node->getOperand(Idx); }
00293   };
00294 
00295 public:
00296   /// \brief Drop all references and remove the node from parent module.
00297   void eraseFromParent();
00298 
00299   /// \brief Remove all uses and clear node vector.
00300   void dropAllReferences();
00301 
00302   ~NamedMDNode();
00303 
00304   /// \brief Get the module that holds this named metadata collection.
00305   inline Module *getParent() { return Parent; }
00306   inline const Module *getParent() const { return Parent; }
00307 
00308   MDNode *getOperand(unsigned i) const;
00309   unsigned getNumOperands() const;
00310   void addOperand(MDNode *M);
00311   StringRef getName() const;
00312   void print(raw_ostream &ROS) const;
00313   void dump() const;
00314 
00315   // ---------------------------------------------------------------------------
00316   // Operand Iterator interface...
00317   //
00318   typedef op_iterator_impl<MDNode*, MDNode> op_iterator;
00319   op_iterator op_begin() { return op_iterator(this, 0); }
00320   op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
00321 
00322   typedef op_iterator_impl<const MDNode*, MDNode> const_op_iterator;
00323   const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
00324   const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
00325 
00326   inline iterator_range<op_iterator>  operands() {
00327     return iterator_range<op_iterator>(op_begin(), op_end());
00328   }
00329   inline iterator_range<const_op_iterator> operands() const {
00330     return iterator_range<const_op_iterator>(op_begin(), op_end());
00331   }
00332 };
00333 
00334 } // end llvm namespace
00335 
00336 #endif