1 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the declarations for metadata subclasses.
12 /// They represent the different flavors of metadata that live in LLVM.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_METADATA_H
17 #define LLVM_METADATA_H
19 #include "llvm/Value.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/Support/ValueHandle.h"
31 class MetadataContextImpl;
33 //===----------------------------------------------------------------------===//
34 // MetadataBase - A base class for MDNode, MDString and NamedMDNode.
35 class MetadataBase : public Value {
37 MetadataBase(const Type *Ty, unsigned scid)
42 /// Methods for support type inquiry through isa, cast, and dyn_cast:
43 static inline bool classof(const MetadataBase *) { return true; }
44 static bool classof(const Value *V) {
45 return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
46 || V->getValueID() == NamedMDNodeVal;
50 //===----------------------------------------------------------------------===//
51 /// MDString - a single uniqued string.
52 /// These are used to efficiently contain a byte sequence for metadata.
53 /// MDString is always unnamd.
54 class MDString : public MetadataBase {
55 MDString(const MDString &); // DO NOT IMPLEMENT
59 explicit MDString(LLVMContext &C, StringRef S)
60 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
63 static MDString *get(LLVMContext &Context, StringRef Str);
64 static MDString *get(LLVMContext &Context, const char *Str);
66 StringRef getString() const { return Str; }
68 unsigned getLength() const { return (unsigned)Str.size(); }
70 typedef StringRef::iterator iterator;
72 /// begin() - Pointer to the first byte of the string.
74 iterator begin() const { return Str.begin(); }
76 /// end() - Pointer to one byte past the end of the string.
78 iterator end() const { return Str.end(); }
80 /// Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const MDString *) { return true; }
82 static bool classof(const Value *V) {
83 return V->getValueID() == MDStringVal;
90 //===----------------------------------------------------------------------===//
91 /// MDNode - a tuple of other values.
92 /// These contain a list of the values that represent the metadata.
93 /// MDNode is always unnamed.
94 class MDNode : public MetadataBase, public FoldingSetNode {
95 MDNode(const MDNode &); // DO NOT IMPLEMENT
97 friend class MDNodeElement;
99 static const unsigned short FunctionLocalBit = 1;
101 // Replace each instance of F from the element list of this node with T.
102 void replaceElement(Value *F, Value *T);
108 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
109 bool isFunctionLocal);
111 // Constructors and destructors.
112 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
113 bool isFunctionLocal = false);
115 /// ~MDNode - Destroy MDNode.
118 /// getElement - Return specified element.
119 Value *getElement(unsigned i) const;
121 /// getNumElements - Return number of MDNode elements.
122 unsigned getNumElements() const { return NodeSize; }
124 /// isFunctionLocal - Return whether MDNode is local to a function.
125 /// Note: MDNodes are designated as function-local when created, and keep
126 /// that designation even if their operands are modified to no longer
127 /// refer to function-local IR.
128 bool isFunctionLocal() const { return SubclassData & FunctionLocalBit; }
130 /// getLocalFunction - Return false if MDNode's recursive function-localness
131 /// is invalid (local to more than one function). Return true otherwise.
132 /// If MDNode has one function to which it is local, set LocalFunction to that
134 bool getLocalFunction(Function *LocalFunction,
135 SmallPtrSet<MDNode *, 32> *VisitedMDNodes = NULL);
137 /// Profile - calculate a unique identifier for this MDNode to collapse
139 void Profile(FoldingSetNodeID &ID) const;
141 /// Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const MDNode *) { return true; }
143 static bool classof(const Value *V) {
144 return V->getValueID() == MDNodeVal;
148 //===----------------------------------------------------------------------===//
149 /// NamedMDNode - a tuple of other metadata.
150 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
151 template<typename ValueSubClass, typename ItemParentClass>
152 class SymbolTableListTraits;
154 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
155 friend class SymbolTableListTraits<NamedMDNode, Module>;
156 friend class LLVMContextImpl;
158 NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
161 void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
163 void setParent(Module *M) { Parent = M; }
165 explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
166 unsigned NumVals, Module *M = 0);
168 static NamedMDNode *Create(LLVMContext &C, const Twine &N,
169 MetadataBase *const *MDs,
170 unsigned NumMDs, Module *M = 0) {
171 return new NamedMDNode(C, N, MDs, NumMDs, M);
174 static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
176 /// eraseFromParent - Drop all references and remove the node from parent
178 void eraseFromParent();
180 /// dropAllReferences - Remove all uses and clear node vector.
181 void dropAllReferences();
183 /// ~NamedMDNode - Destroy NamedMDNode.
186 /// getParent - Get the module that holds this named metadata collection.
187 inline Module *getParent() { return Parent; }
188 inline const Module *getParent() const { return Parent; }
190 /// getElement - Return specified element.
191 MetadataBase *getElement(unsigned i) const;
193 /// getNumElements - Return number of NamedMDNode elements.
194 unsigned getNumElements() const;
196 /// addElement - Add metadata element.
197 void addElement(MetadataBase *M);
199 /// Methods for support type inquiry through isa, cast, and dyn_cast:
200 static inline bool classof(const NamedMDNode *) { return true; }
201 static bool classof(const Value *V) {
202 return V->getValueID() == NamedMDNodeVal;
206 //===----------------------------------------------------------------------===//
207 /// MetadataContext -
208 /// MetadataContext handles uniquing and assignment of IDs for custom metadata
209 /// types. Custom metadata handler names do not contain spaces. And the name
210 /// must start with an alphabet. The regular expression used to check name
211 /// is [a-zA-Z$._][a-zA-Z$._0-9]*
212 class MetadataContext {
214 MetadataContext(MetadataContext&);
215 void operator=(MetadataContext&);
217 MetadataContextImpl *const pImpl;
222 /// registerMDKind - Register a new metadata kind and return its ID.
223 /// A metadata kind can be registered only once.
224 unsigned registerMDKind(StringRef Name);
226 /// getMDKind - Return metadata kind. If the requested metadata kind
227 /// is not registered then return 0.
228 unsigned getMDKind(StringRef Name) const;
230 /// isValidName - Return true if Name is a valid custom metadata handler name.
231 static bool isValidName(StringRef Name);
233 /// getMD - Get the metadata of given kind attached to an Instruction.
234 /// If the metadata is not found then return 0.
235 MDNode *getMD(unsigned Kind, const Instruction *Inst);
237 /// getMDs - Get the metadata attached to an Instruction.
238 void getMDs(const Instruction *Inst,
239 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
241 /// addMD - Attach the metadata of given kind to an Instruction.
242 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
244 /// removeMD - Remove metadata of given kind attached with an instuction.
245 void removeMD(unsigned Kind, Instruction *Inst);
247 /// removeAllMetadata - Remove all metadata attached with an instruction.
248 void removeAllMetadata(Instruction *Inst);
250 /// copyMD - If metadata is attached with Instruction In1 then attach
251 /// the same metadata to In2.
252 void copyMD(Instruction *In1, Instruction *In2);
254 /// getHandlerNames - Populate client supplied smallvector using custom
255 /// metadata name and ID.
256 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
258 /// ValueIsDeleted - This handler is used to update metadata store
259 /// when a value is deleted.
260 void ValueIsDeleted(const Value *) {}
261 void ValueIsDeleted(Instruction *Inst);
262 void ValueIsRAUWd(Value *V1, Value *V2);
264 /// ValueIsCloned - This handler is used to update metadata store
265 /// when In1 is cloned to create In2.
266 void ValueIsCloned(const Instruction *In1, Instruction *In2);
269 } // end llvm namespace