1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// @file 11 /// This file contains the declarations for metadata subclasses. 12 /// They represent the different flavors of metadata that live in LLVM. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_IR_METADATA_H 17 #define LLVM_IR_METADATA_H 18 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/FoldingSet.h" 21 #include "llvm/ADT/ilist_node.h" 22 #include "llvm/IR/Value.h" 23 24 namespace llvm { 25 class LLVMContext; 26 class Module; 27 template<typename ValueSubClass, typename ItemParentClass> 28 class SymbolTableListTraits; 29 30 31 //===----------------------------------------------------------------------===// 32 /// MDString - a single uniqued string. 33 /// These are used to efficiently contain a byte sequence for metadata. 34 /// MDString is always unnamed. 35 class MDString : public Value { 36 virtual void anchor(); 37 MDString(const MDString &) LLVM_DELETED_FUNCTION; 38 39 explicit MDString(LLVMContext &C); 40 public: 41 static MDString *get(LLVMContext &Context, StringRef Str); 42 static MDString *get(LLVMContext &Context, const char *Str) { 43 return get(Context, Str ? StringRef(Str) : StringRef()); 44 } 45 46 StringRef getString() const { return getName(); } 47 48 unsigned getLength() const { return (unsigned)getName().size(); } 49 50 typedef StringRef::iterator iterator; 51 52 /// begin() - Pointer to the first byte of the string. 53 iterator begin() const { return getName().begin(); } 54 55 /// end() - Pointer to one byte past the end of the string. 56 iterator end() const { return getName().end(); } 57 58 /// Methods for support type inquiry through isa, cast, and dyn_cast: 59 static bool classof(const Value *V) { 60 return V->getValueID() == MDStringVal; 61 } 62 }; 63 64 65 class MDNodeOperand; 66 67 //===----------------------------------------------------------------------===// 68 /// MDNode - a tuple of other values. 69 class MDNode : public Value, public FoldingSetNode { 70 MDNode(const MDNode &) LLVM_DELETED_FUNCTION; 71 void operator=(const MDNode &) LLVM_DELETED_FUNCTION; 72 friend class MDNodeOperand; 73 friend class LLVMContextImpl; 74 friend struct FoldingSetTrait<MDNode>; 75 76 /// Hash - If the MDNode is uniqued cache the hash to speed up lookup. 77 unsigned Hash; 78 79 /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the 80 /// end of this MDNode. 81 unsigned NumOperands; 82 83 // Subclass data enums. 84 enum { 85 /// FunctionLocalBit - This bit is set if this MDNode is function local. 86 /// This is true when it (potentially transitively) contains a reference to 87 /// something in a function, like an argument, basicblock, or instruction. 88 FunctionLocalBit = 1 << 0, 89 90 /// NotUniquedBit - This is set on MDNodes that are not uniqued because they 91 /// have a null operand. 92 NotUniquedBit = 1 << 1, 93 94 /// DestroyFlag - This bit is set by destroy() so the destructor can assert 95 /// that the node isn't being destroyed with a plain 'delete'. 96 DestroyFlag = 1 << 2 97 }; 98 99 // FunctionLocal enums. 100 enum FunctionLocalness { 101 FL_Unknown = -1, 102 FL_No = 0, 103 FL_Yes = 1 104 }; 105 106 /// replaceOperand - Replace each instance of F from the operand list of this 107 /// node with T. 108 void replaceOperand(MDNodeOperand *Op, Value *NewVal); 109 ~MDNode(); 110 111 MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal); 112 113 static MDNode *getMDNode(LLVMContext &C, ArrayRef<Value*> Vals, 114 FunctionLocalness FL, bool Insert = true); 115 public: 116 // Constructors and destructors. 117 static MDNode *get(LLVMContext &Context, ArrayRef<Value*> Vals); 118 // getWhenValsUnresolved - Construct MDNode determining function-localness 119 // from isFunctionLocal argument, not by analyzing Vals. 120 static MDNode *getWhenValsUnresolved(LLVMContext &Context, 121 ArrayRef<Value*> Vals, 122 bool isFunctionLocal); 123 124 static MDNode *getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals); 125 126 /// getTemporary - Return a temporary MDNode, for use in constructing 127 /// cyclic MDNode structures. A temporary MDNode is not uniqued, 128 /// may be RAUW'd, and must be manually deleted with deleteTemporary. 129 static MDNode *getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals); 130 131 /// deleteTemporary - Deallocate a node created by getTemporary. The 132 /// node must not have any users. 133 static void deleteTemporary(MDNode *N); 134 135 /// replaceOperandWith - Replace a specific operand. 136 void replaceOperandWith(unsigned i, Value *NewVal); 137 138 /// getOperand - Return specified operand. 139 Value *getOperand(unsigned i) const LLVM_READONLY; 140 141 /// getNumOperands - Return number of MDNode operands. 142 unsigned getNumOperands() const { return NumOperands; } 143 144 /// isFunctionLocal - Return whether MDNode is local to a function. 145 bool isFunctionLocal() const { 146 return (getSubclassDataFromValue() & FunctionLocalBit) != 0; 147 } 148 149 // getFunction - If this metadata is function-local and recursively has a 150 // function-local operand, return the first such operand's parent function. 151 // Otherwise, return null. getFunction() should not be used for performance- 152 // critical code because it recursively visits all the MDNode's operands. 153 const Function *getFunction() const; 154 155 /// Profile - calculate a unique identifier for this MDNode to collapse 156 /// duplicates 157 void Profile(FoldingSetNodeID &ID) const; 158 159 /// Methods for support type inquiry through isa, cast, and dyn_cast: 160 static bool classof(const Value *V) { 161 return V->getValueID() == MDNodeVal; 162 } 163 164 /// Methods for metadata merging. 165 static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B); 166 static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B); 167 static MDNode *getMostGenericRange(MDNode *A, MDNode *B); 168 private: 169 // destroy - Delete this node. Only when there are no uses. 170 void destroy(); 171 172 bool isNotUniqued() const { 173 return (getSubclassDataFromValue() & NotUniquedBit) != 0; 174 } 175 void setIsNotUniqued(); 176 177 // Shadow Value::setValueSubclassData with a private forwarding method so that 178 // any future subclasses cannot accidentally use it. 179 void setValueSubclassData(unsigned short D) { 180 Value::setValueSubclassData(D); 181 } 182 }; 183 184 //===----------------------------------------------------------------------===// 185 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't 186 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain 187 /// lists of MDNodes. 188 class NamedMDNode : public ilist_node<NamedMDNode> { 189 friend class SymbolTableListTraits<NamedMDNode, Module>; 190 friend struct ilist_traits<NamedMDNode>; 191 friend class LLVMContextImpl; 192 friend class Module; 193 NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION; 194 195 std::string Name; 196 Module *Parent; 197 void *Operands; // SmallVector<TrackingVH<MDNode>, 4> 198 199 void setParent(Module *M) { Parent = M; } 200 201 explicit NamedMDNode(const Twine &N); 202 203 public: 204 /// eraseFromParent - Drop all references and remove the node from parent 205 /// module. 206 void eraseFromParent(); 207 208 /// dropAllReferences - Remove all uses and clear node vector. 209 void dropAllReferences(); 210 211 /// ~NamedMDNode - Destroy NamedMDNode. 212 ~NamedMDNode(); 213 214 /// getParent - Get the module that holds this named metadata collection. 215 inline Module *getParent() { return Parent; } 216 inline const Module *getParent() const { return Parent; } 217 218 /// getOperand - Return specified operand. 219 MDNode *getOperand(unsigned i) const; 220 221 /// getNumOperands - Return the number of NamedMDNode operands. 222 unsigned getNumOperands() const; 223 224 /// addOperand - Add metadata operand. 225 void addOperand(MDNode *M); 226 227 /// getName - Return a constant reference to this named metadata's name. 228 StringRef getName() const; 229 230 /// print - Implement operator<< on NamedMDNode. 231 void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = 0) const; 232 233 /// dump() - Allow printing of NamedMDNodes from the debugger. 234 void dump() const; 235 }; 236 237 } // end llvm namespace 238 239 #endif 240