Destroy MDNodes gracefully while deleting llvm context.
[oota-llvm.git] / include / llvm / Metadata.h
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_METADATA_H
17 #define LLVM_METADATA_H
18
19 #include "llvm/Value.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/ilist_node.h"
22
23 namespace llvm {
24 class Constant;
25 class Instruction;
26 class LLVMContext;
27 class Module;
28 template <typename T> class SmallVectorImpl;
29 template<typename ValueSubClass, typename ItemParentClass>
30   class SymbolTableListTraits;
31   
32   
33 //===----------------------------------------------------------------------===//
34 /// MDString - a single uniqued string.
35 /// These are used to efficiently contain a byte sequence for metadata.
36 /// MDString is always unnamd.
37 class MDString : public Value {
38   MDString(const MDString &);            // DO NOT IMPLEMENT
39
40   StringRef Str;
41 protected:
42   explicit MDString(LLVMContext &C, StringRef S);
43
44 public:
45   static MDString *get(LLVMContext &Context, StringRef Str);
46   static MDString *get(LLVMContext &Context, const char *Str);
47   
48   StringRef getString() const { return Str; }
49
50   unsigned getLength() const { return (unsigned)Str.size(); }
51
52   typedef StringRef::iterator iterator;
53   
54   /// begin() - Pointer to the first byte of the string.
55   ///
56   iterator begin() const { return Str.begin(); }
57
58   /// end() - Pointer to one byte past the end of the string.
59   ///
60   iterator end() const { return Str.end(); }
61
62   /// Methods for support type inquiry through isa, cast, and dyn_cast:
63   static inline bool classof(const MDString *) { return true; }
64   static bool classof(const Value *V) {
65     return V->getValueID() == MDStringVal;
66   }
67 };
68
69   
70 class MDNodeOperand;
71   
72 //===----------------------------------------------------------------------===//
73 /// MDNode - a tuple of other values.
74 class MDNode : public Value, public FoldingSetNode {
75   MDNode(const MDNode &);                // DO NOT IMPLEMENT
76   void operator=(const MDNode &);        // DO NOT IMPLEMENT
77   friend class MDNodeOperand;
78   friend class LLVMContextImpl;
79
80   /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
81   /// end of this MDNode.
82   unsigned NumOperands;
83   
84   // Subclass data enums.
85   enum {
86     /// FunctionLocalBit - This bit is set if this MDNode is function local.
87     /// This is true when it (potentially transitively) contains a reference to
88     /// something in a function, like an argument, basicblock, or instruction.
89     FunctionLocalBit = 1 << 0,
90     
91     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
92     /// have a null perand.
93     NotUniquedBit    = 1 << 1,
94     
95     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
96     /// that the node isn't being destroyed with a plain 'delete'.
97     DestroyFlag      = 1 << 2
98   };
99   
100   // FunctionLocal enums.
101   enum FunctionLocalness {
102     FL_Unknown = -1,
103     FL_No = 0,
104     FL_Yes = 1
105   };
106   
107   // Replace each instance of F from the operand list of this node with T.
108   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
109   ~MDNode();
110     // replaceAllOperandsWithNull - This is used while destroying llvm context to 
111   // gracefully delete all nodes. This method replaces all operands with null.
112   void replaceAllOperandsWithNull();
113
114 protected:
115   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
116                   bool isFunctionLocal);
117   
118   static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
119                            FunctionLocalness FL, bool Insert = true);
120 public:
121   // Constructors and destructors.
122   static MDNode *get(LLVMContext &Context, Value *const *Vals,
123                      unsigned NumVals);
124   // getWhenValsUnresolved - Construct MDNode determining function-localness
125   // from isFunctionLocal argument, not by analyzing Vals.
126   static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
127                                        unsigned NumVals, bool isFunctionLocal);
128                                        
129   static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
130                              unsigned NumVals);
131   
132   /// getOperand - Return specified operand.
133   Value *getOperand(unsigned i) const;
134   
135   /// getNumOperands - Return number of MDNode operands.
136   unsigned getNumOperands() const { return NumOperands; }
137   
138   /// isFunctionLocal - Return whether MDNode is local to a function.
139   /// Note: MDNodes are designated as function-local when created, and keep
140   ///       that designation even if their operands are modified to no longer
141   ///       refer to function-local IR.
142   bool isFunctionLocal() const {
143     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
144   }
145   
146   // getFunction - If this metadata is function-local and recursively has a
147   // function-local operand, return the first such operand's parent function.
148   // Otherwise, return null. getFunction() should not be used for performance-
149   // critical code because it recursively visits all the MDNode's operands.  
150   const Function *getFunction() const;
151
152   // destroy - Delete this node.  Only when there are no uses.
153   void destroy();
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 inline bool classof(const MDNode *) { return true; }
161   static bool classof(const Value *V) {
162     return V->getValueID() == MDNodeVal;
163   }
164 private:
165   bool isNotUniqued() const { 
166     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
167   }
168   void setIsNotUniqued() {
169     setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
170   }
171   
172   // Shadow Value::setValueSubclassData with a private forwarding method so that
173   // any future subclasses cannot accidentally use it.
174   void setValueSubclassData(unsigned short D) {
175     Value::setValueSubclassData(D);
176   }
177 };
178
179 //===----------------------------------------------------------------------===//
180 /// NamedMDNode - a tuple of MDNodes.
181 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
182 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
183   friend class SymbolTableListTraits<NamedMDNode, Module>;
184   friend struct ilist_traits<NamedMDNode>;
185   friend class LLVMContextImpl;
186   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
187
188   std::string Name;
189   Module *Parent;
190   void *Operands; // SmallVector<WeakVH<MDNode>, 4>
191
192   void setParent(Module *M) { Parent = M; }
193 protected:
194   explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals, 
195                        unsigned NumVals, Module *M = 0);
196 public:
197   static NamedMDNode *Create(LLVMContext &C, const Twine &N,
198                              MDNode *const *MDs, 
199                              unsigned NumMDs, Module *M = 0) {
200     return new NamedMDNode(C, N, MDs, NumMDs, M);
201   }
202
203   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
204
205   /// eraseFromParent - Drop all references and remove the node from parent
206   /// module.
207   void eraseFromParent();
208
209   /// dropAllReferences - Remove all uses and clear node vector.
210   void dropAllReferences();
211
212   /// ~NamedMDNode - Destroy NamedMDNode.
213   ~NamedMDNode();
214
215   /// getParent - Get the module that holds this named metadata collection.
216   inline Module *getParent() { return Parent; }
217   inline const Module *getParent() const { return Parent; }
218
219   /// getOperand - Return specified operand.
220   MDNode *getOperand(unsigned i) const;
221   
222   /// getNumOperands - Return the number of NamedMDNode operands.
223   unsigned getNumOperands() const;
224
225   /// addOperand - Add metadata operand.
226   void addOperand(MDNode *M);
227
228   /// setName - Set the name of this named metadata.
229   void setName(const Twine &NewName);
230
231   /// getName - Return a constant reference to this named metadata's name.
232   StringRef getName() const;
233
234   /// Methods for support type inquiry through isa, cast, and dyn_cast:
235   static inline bool classof(const NamedMDNode *) { return true; }
236   static bool classof(const Value *V) {
237     return V->getValueID() == NamedMDNodeVal;
238   }
239 };
240
241 } // end llvm namespace
242
243 #endif