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/FoldingSet.h"
00021 #include "llvm/ADT/ilist_node.h"
00022 #include "llvm/IR/Value.h"
00023 
00024 namespace llvm {
00025 class Constant;
00026 class Instruction;
00027 class LLVMContext;
00028 class Module;
00029 template <typename T> class SmallVectorImpl;
00030 template<typename ValueSubClass, typename ItemParentClass>
00031   class SymbolTableListTraits;
00032 
00033 
00034 //===----------------------------------------------------------------------===//
00035 /// MDString - a single uniqued string.
00036 /// These are used to efficiently contain a byte sequence for metadata.
00037 /// MDString is always unnamed.
00038 class MDString : public Value {
00039   virtual void anchor();
00040   MDString(const MDString &) LLVM_DELETED_FUNCTION;
00041 
00042   explicit MDString(LLVMContext &C);
00043 public:
00044   static MDString *get(LLVMContext &Context, StringRef Str);
00045   static MDString *get(LLVMContext &Context, const char *Str) {
00046     return get(Context, Str ? StringRef(Str) : StringRef());
00047   }
00048 
00049   StringRef getString() const { return getName(); }
00050 
00051   unsigned getLength() const { return (unsigned)getName().size(); }
00052 
00053   typedef StringRef::iterator iterator;
00054 
00055   /// begin() - Pointer to the first byte of the string.
00056   iterator begin() const { return getName().begin(); }
00057 
00058   /// end() - Pointer to one byte past the end of the string.
00059   iterator end() const { return getName().end(); }
00060 
00061   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00062   static bool classof(const Value *V) {
00063     return V->getValueID() == MDStringVal;
00064   }
00065 };
00066 
00067 
00068 class MDNodeOperand;
00069 
00070 //===----------------------------------------------------------------------===//
00071 /// MDNode - a tuple of other values.
00072 class MDNode : public Value, public FoldingSetNode {
00073   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
00074   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
00075   friend class MDNodeOperand;
00076   friend class LLVMContextImpl;
00077   friend struct FoldingSetTrait<MDNode>;
00078 
00079   /// Hash - If the MDNode is uniqued cache the hash to speed up lookup.
00080   unsigned Hash;
00081 
00082   /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
00083   /// end of this MDNode.
00084   unsigned NumOperands;
00085 
00086   // Subclass data enums.
00087   enum {
00088     /// FunctionLocalBit - This bit is set if this MDNode is function local.
00089     /// This is true when it (potentially transitively) contains a reference to
00090     /// something in a function, like an argument, basicblock, or instruction.
00091     FunctionLocalBit = 1 << 0,
00092 
00093     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
00094     /// have a null operand.
00095     NotUniquedBit    = 1 << 1,
00096 
00097     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
00098     /// that the node isn't being destroyed with a plain 'delete'.
00099     DestroyFlag      = 1 << 2
00100   };
00101 
00102   // FunctionLocal enums.
00103   enum FunctionLocalness {
00104     FL_Unknown = -1,
00105     FL_No = 0,
00106     FL_Yes = 1
00107   };
00108 
00109   /// replaceOperand - Replace each instance of F from the operand list of this
00110   /// node with T.
00111   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
00112   ~MDNode();
00113 
00114   MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal);
00115 
00116   static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals,
00117                            FunctionLocalness FL, bool Insert = true);
00118 public:
00119   // Constructors and destructors.
00120   static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals);
00121   // getWhenValsUnresolved - Construct MDNode determining function-localness
00122   // from isFunctionLocal argument, not by analyzing Vals.
00123   static MDNode *getWhenValsUnresolved(LLVMContext &Context,
00124                                        ArrayRef<Value*> Vals,
00125                                        bool isFunctionLocal);
00126 
00127   static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals);
00128 
00129   /// getTemporary - Return a temporary MDNode, for use in constructing
00130   /// cyclic MDNode structures. A temporary MDNode is not uniqued,
00131   /// may be RAUW'd, and must be manually deleted with deleteTemporary.
00132   static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals);
00133 
00134   /// deleteTemporary - Deallocate a node created by getTemporary. The
00135   /// node must not have any users.
00136   static void deleteTemporary(MDNode *N);
00137 
00138   /// replaceOperandWith - Replace a specific operand.
00139   void replaceOperandWith(unsigned i, Value *NewVal);
00140 
00141   /// getOperand - Return specified operand.
00142   Value *getOperand(unsigned i) const;
00143 
00144   /// getNumOperands - Return number of MDNode operands.
00145   unsigned getNumOperands() const { return NumOperands; }
00146 
00147   /// isFunctionLocal - Return whether MDNode is local to a function.
00148   bool isFunctionLocal() const {
00149     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
00150   }
00151 
00152   // getFunction - If this metadata is function-local and recursively has a
00153   // function-local operand, return the first such operand's parent function.
00154   // Otherwise, return null. getFunction() should not be used for performance-
00155   // critical code because it recursively visits all the MDNode's operands.
00156   const Function *getFunction() const;
00157 
00158   /// Profile - calculate a unique identifier for this MDNode to collapse
00159   /// duplicates
00160   void Profile(FoldingSetNodeID &ID) const;
00161 
00162   /// Methods for support type inquiry through isa, cast, and dyn_cast:
00163   static bool classof(const Value *V) {
00164     return V->getValueID() == MDNodeVal;
00165   }
00166 
00167   /// Methods for metadata merging.
00168   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
00169   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
00170   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
00171 private:
00172   // destroy - Delete this node.  Only when there are no uses.
00173   void destroy();
00174 
00175   bool isNotUniqued() const {
00176     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
00177   }
00178   void setIsNotUniqued();
00179 
00180   // Shadow Value::setValueSubclassData with a private forwarding method so that
00181   // any future subclasses cannot accidentally use it.
00182   void setValueSubclassData(unsigned short D) {
00183     Value::setValueSubclassData(D);
00184   }
00185 };
00186 
00187 //===----------------------------------------------------------------------===//
00188 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
00189 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
00190 /// lists of MDNodes.
00191 class NamedMDNode : public ilist_node<NamedMDNode> {
00192   friend class SymbolTableListTraits<NamedMDNode, Module>;
00193   friend struct ilist_traits<NamedMDNode>;
00194   friend class LLVMContextImpl;
00195   friend class Module;
00196   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
00197 
00198   std::string Name;
00199   Module *Parent;
00200   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
00201 
00202   void setParent(Module *M) { Parent = M; }
00203 
00204   explicit NamedMDNode(const Twine &N);
00205 
00206 public:
00207   /// eraseFromParent - Drop all references and remove the node from parent
00208   /// module.
00209   void eraseFromParent();
00210 
00211   /// dropAllReferences - Remove all uses and clear node vector.
00212   void dropAllReferences();
00213 
00214   /// ~NamedMDNode - Destroy NamedMDNode.
00215   ~NamedMDNode();
00216 
00217   /// getParent - Get the module that holds this named metadata collection.
00218   inline Module *getParent() { return Parent; }
00219   inline const Module *getParent() const { return Parent; }
00220 
00221   /// getOperand - Return specified operand.
00222   MDNode *getOperand(unsigned i) const;
00223 
00224   /// getNumOperands - Return the number of NamedMDNode operands.
00225   unsigned getNumOperands() const;
00226 
00227   /// addOperand - Add metadata operand.
00228   void addOperand(MDNode *M);
00229 
00230   /// getName - Return a constant reference to this named metadata's name.
00231   StringRef getName() const;
00232 
00233   /// print - Implement operator<< on NamedMDNode.
00234   void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = 0) const;
00235 
00236   /// dump() - Allow printing of NamedMDNodes from the debugger.
00237   void dump() const;
00238 };
00239 
00240 } // end llvm namespace
00241 
00242 #endif