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