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 #include "llvm/Support/ErrorHandling.h"
00026 
00027 namespace llvm {
00028 class LLVMContext;
00029 class Module;
00030 template<typename ValueSubClass, typename ItemParentClass>
00031   class SymbolTableListTraits;
00032 
00033 
00034 enum LLVMConstants : uint32_t {
00035   DEBUG_METADATA_VERSION = 2  // Current debug info version number.
00036 };
00037 
00038 /// \brief Root of the metadata hierarchy.
00039 ///
00040 /// This is a root class for typeless data in the IR.
00041 ///
00042 /// TODO: Detach from the Value hierarchy.
00043 class Metadata : public Value {
00044 protected:
00045   Metadata(LLVMContext &Context, unsigned ID);
00046 
00047 public:
00048   static bool classof(const Value *V) {
00049     return V->getValueID() == GenericMDNodeVal ||
00050            V->getValueID() == MDNodeFwdDeclVal ||
00051            V->getValueID() == MDStringVal;
00052   }
00053 };
00054 
00055 //===----------------------------------------------------------------------===//
00056 /// \brief A single uniqued string.
00057 ///
00058 /// These are used to efficiently contain a byte sequence for metadata.
00059 /// MDString is always unnamed.
00060 class MDString : public Metadata {
00061   friend class StringMapEntry<MDString>;
00062 
00063   virtual void anchor();
00064   MDString(const MDString &) LLVM_DELETED_FUNCTION;
00065 
00066   explicit MDString(LLVMContext &Context)
00067       : Metadata(Context, Value::MDStringVal) {}
00068 
00069   /// \brief Shadow Value::getName() to prevent its use.
00070   StringRef getName() const LLVM_DELETED_FUNCTION;
00071 
00072 public:
00073   static MDString *get(LLVMContext &Context, StringRef Str);
00074   static MDString *get(LLVMContext &Context, const char *Str) {
00075     return get(Context, Str ? StringRef(Str) : StringRef());
00076   }
00077 
00078   StringRef getString() const;
00079 
00080   unsigned getLength() const { return (unsigned)getString().size(); }
00081 
00082   typedef StringRef::iterator iterator;
00083 
00084   /// \brief Pointer to the first byte of the string.
00085   iterator begin() const { return getString().begin(); }
00086 
00087   /// \brief Pointer to one byte past the end of the string.
00088   iterator end() const { return getString().end(); }
00089 
00090   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
00091   static bool classof(const Value *V) {
00092     return V->getValueID() == MDStringVal;
00093   }
00094 };
00095 
00096 /// \brief A collection of metadata nodes that might be associated with a
00097 /// memory access used by the alias-analysis infrastructure.
00098 struct AAMDNodes {
00099   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
00100                      MDNode *N = nullptr)
00101       : TBAA(T), Scope(S), NoAlias(N) {}
00102 
00103   bool operator==(const AAMDNodes &A) const {
00104     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
00105   }
00106 
00107   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
00108 
00109   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
00110 
00111   /// \brief The tag for type-based alias analysis.
00112   MDNode *TBAA;
00113 
00114   /// \brief The tag for alias scope specification (used with noalias).
00115   MDNode *Scope;
00116 
00117   /// \brief The tag specifying the noalias scope.
00118   MDNode *NoAlias;
00119 };
00120 
00121 // Specialize DenseMapInfo for AAMDNodes.
00122 template<>
00123 struct DenseMapInfo<AAMDNodes> {
00124   static inline AAMDNodes getEmptyKey() {
00125     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
00126   }
00127   static inline AAMDNodes getTombstoneKey() {
00128     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
00129   }
00130   static unsigned getHashValue(const AAMDNodes &Val) {
00131     return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
00132            DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
00133            DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
00134   }
00135   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
00136     return LHS == RHS;
00137   }
00138 };
00139 
00140 class MDNodeOperand;
00141 
00142 //===----------------------------------------------------------------------===//
00143 /// \brief Tuple of metadata.
00144 class MDNode : public Metadata {
00145   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
00146   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
00147   friend class MDNodeOperand;
00148   friend class LLVMContextImpl;
00149   void *operator new(size_t) LLVM_DELETED_FUNCTION;
00150 
00151 protected:
00152   void *operator new(size_t Size, unsigned NumOps);
00153 
00154   /// \brief Required by std, but never called.
00155   void operator delete(void *Mem);
00156 
00157   /// \brief Required by std, but never called.
00158   void operator delete(void *, unsigned) {
00159     llvm_unreachable("Constructor throws?");
00160   }
00161 
00162   /// \brief Required by std, but never called.
00163   void operator delete(void *, unsigned, bool) {
00164     llvm_unreachable("Constructor throws?");
00165   }
00166 
00167   /// \brief Subclass data enums.
00168   enum {
00169     /// FunctionLocalBit - This bit is set if this MDNode is function local.
00170     /// This is true when it (potentially transitively) contains a reference to
00171     /// something in a function, like an argument, basicblock, or instruction.
00172     FunctionLocalBit = 1 << 0,
00173 
00174     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
00175     /// have a null operand.
00176     NotUniquedBit    = 1 << 1
00177   };
00178 
00179   /// \brief FunctionLocal enums.
00180   enum FunctionLocalness {
00181     FL_Unknown = -1,
00182     FL_No = 0,
00183     FL_Yes = 1
00184   };
00185 
00186   /// \brief Replace each instance of the given operand with a new value.
00187   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
00188 
00189   MDNode(LLVMContext &C, unsigned ID, ArrayRef<Value *> Vals,
00190          bool isFunctionLocal);
00191   ~MDNode() {}
00192 
00193   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
00194                            FunctionLocalness FL, bool Insert = true);
00195 public:
00196   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
00197   /// \brief Construct MDNode with an explicit function-localness.
00198   ///
00199   /// Don't analyze Vals; trust isFunctionLocal.
00200   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
00201                                        ArrayRef<Value*> Vals,
00202                                        bool isFunctionLocal);
00203 
00204   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
00205 
00206   /// \brief Return a temporary MDNode
00207   ///
00208   /// For use in constructing cyclic MDNode structures. A temporary MDNode is
00209   /// not uniqued, may be RAUW'd, and must be manually deleted with
00210   /// deleteTemporary.
00211   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
00212 
00213   /// \brief Deallocate a node created by getTemporary.
00214   ///
00215   /// The node must not have any users.
00216   static void deleteTemporary(MDNode *N);
00217 
00218   /// \brief Replace a specific operand.
00219   void replaceOperandWith(unsigned i, Value *NewVal);
00220 
00221   /// \brief Return specified operand.
00222   Value *getOperand(unsigned i) const LLVM_READONLY;
00223 
00224   /// \brief Return number of MDNode operands.
00225   unsigned getNumOperands() const { return NumOperands; }
00226 
00227   /// \brief Return whether MDNode is local to a function.
00228   bool isFunctionLocal() const {
00229     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
00230   }
00231 
00232   /// \brief Return the first function-local operand's function.
00233   ///
00234   /// If this metadata is function-local and recursively has a function-local
00235   /// operand, return the first such operand's parent function.  Otherwise,
00236   /// return null. getFunction() should not be used for performance- critical
00237   /// code because it recursively visits all the MDNode's operands.
00238   const Function *getFunction() const;
00239 
00240   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
00241   static bool classof(const Value *V) {
00242     return V->getValueID() == GenericMDNodeVal ||
00243            V->getValueID() == MDNodeFwdDeclVal;
00244   }
00245 
00246   /// \brief Check whether MDNode is a vtable access.
00247   bool isTBAAVtableAccess() const;
00248 
00249   /// \brief Methods for metadata merging.
00250   static MDNode *concatenate(MDNode *A, MDNode *B);
00251   static MDNode *intersect(MDNode *A, MDNode *B);
00252   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
00253   static AAMDNodes getMostGenericAA(const AAMDNodes &A, const AAMDNodes &B);
00254   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
00255   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
00256 
00257 protected:
00258   bool isNotUniqued() const {
00259     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
00260   }
00261   void setIsNotUniqued();
00262 
00263   // Shadow Value::setValueSubclassData with a private forwarding method so that
00264   // any future subclasses cannot accidentally use it.
00265   void setValueSubclassData(unsigned short D) {
00266     Value::setValueSubclassData(D);
00267   }
00268 };
00269 
00270 /// \brief Generic metadata node.
00271 ///
00272 /// Generic metadata nodes, with opt-out support for uniquing.
00273 ///
00274 /// Although nodes are uniqued by default, \a GenericMDNode has no support for
00275 /// RAUW.  If an operand change (due to RAUW or otherwise) causes a uniquing
00276 /// collision, the uniquing bit is dropped.
00277 ///
00278 /// TODO: Make uniquing opt-out (status: mandatory, sometimes dropped).
00279 /// TODO: Drop support for RAUW.
00280 class GenericMDNode : public MDNode {
00281   friend class MDNode;
00282   friend class LLVMContextImpl;
00283 
00284   unsigned Hash;
00285 
00286   GenericMDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
00287       : MDNode(C, GenericMDNodeVal, Vals, isFunctionLocal), Hash(0) {}
00288   ~GenericMDNode();
00289 
00290   void dropAllReferences();
00291 
00292 public:
00293   /// \brief Get the hash, if any.
00294   unsigned getHash() const { return Hash; }
00295 
00296   static bool classof(const Value *V) {
00297     return V->getValueID() == GenericMDNodeVal;
00298   }
00299 };
00300 
00301 /// \brief Forward declaration of metadata.
00302 ///
00303 /// Forward declaration of metadata, in the form of a metadata node.  Unlike \a
00304 /// GenericMDNode, this class has support for RAUW and is suitable for forward
00305 /// references.
00306 class MDNodeFwdDecl : public MDNode {
00307   friend class MDNode;
00308 
00309   MDNodeFwdDecl(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
00310       : MDNode(C, MDNodeFwdDeclVal, Vals, isFunctionLocal) {}
00311   ~MDNodeFwdDecl() {}
00312 
00313 public:
00314   static bool classof(const Value *V) {
00315     return V->getValueID() == MDNodeFwdDeclVal;
00316   }
00317 };
00318 
00319 //===----------------------------------------------------------------------===//
00320 /// \brief A tuple of MDNodes.
00321 ///
00322 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
00323 /// to modules, have names, and contain lists of MDNodes.
00324 ///
00325 /// TODO: Inherit from Metadata.
00326 class NamedMDNode : public ilist_node<NamedMDNode> {
00327   friend class SymbolTableListTraits<NamedMDNode, Module>;
00328   friend struct ilist_traits<NamedMDNode>;
00329   friend class LLVMContextImpl;
00330   friend class Module;
00331   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
00332 
00333   std::string Name;
00334   Module *Parent;
00335   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
00336 
00337   void setParent(Module *M) { Parent = M; }
00338 
00339   explicit NamedMDNode(const Twine &N);
00340 
00341   template<class T1, class T2>
00342   class op_iterator_impl :
00343       public std::iterator<std::bidirectional_iterator_tag, T2> {
00344     const NamedMDNode *Node;
00345     unsigned Idx;
00346     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
00347 
00348     friend class NamedMDNode;
00349 
00350   public:
00351     op_iterator_impl() : Node(nullptr), Idx(0) { }
00352 
00353     bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
00354     bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
00355     op_iterator_impl &operator++() {
00356       ++Idx;
00357       return *this;
00358     }
00359     op_iterator_impl operator++(int) {
00360       op_iterator_impl tmp(*this);
00361       operator++();
00362       return tmp;
00363     }
00364     op_iterator_impl &operator--() {
00365       --Idx;
00366       return *this;
00367     }
00368     op_iterator_impl operator--(int) {
00369       op_iterator_impl tmp(*this);
00370       operator--();
00371       return tmp;
00372     }
00373 
00374     T1 operator*() const { return Node->getOperand(Idx); }
00375   };
00376 
00377 public:
00378   /// \brief Drop all references and remove the node from parent module.
00379   void eraseFromParent();
00380 
00381   /// \brief Remove all uses and clear node vector.
00382   void dropAllReferences();
00383 
00384   ~NamedMDNode();
00385 
00386   /// \brief Get the module that holds this named metadata collection.
00387   inline Module *getParent() { return Parent; }
00388   inline const Module *getParent() const { return Parent; }
00389 
00390   MDNode *getOperand(unsigned i) const;
00391   unsigned getNumOperands() const;
00392   void addOperand(MDNode *M);
00393   StringRef getName() const;
00394   void print(raw_ostream &ROS) const;
00395   void dump() const;
00396 
00397   // ---------------------------------------------------------------------------
00398   // Operand Iterator interface...
00399   //
00400   typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
00401   op_iterator op_begin() { return op_iterator(this, 0); }
00402   op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
00403 
00404   typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
00405   const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
00406   const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
00407 
00408   inline iterator_range<op_iterator>  operands() {
00409     return iterator_range<op_iterator>(op_begin(), op_end());
00410   }
00411   inline iterator_range<const_op_iterator> operands() const {
00412     return iterator_range<const_op_iterator>(op_begin(), op_end());
00413   }
00414 };
00415 
00416 } // end llvm namespace
00417 
00418 #endif