Introduce a new temporary MDNode concept. Temporary MDNodes are
[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     return get(Context, Str ? StringRef(Str) : StringRef());
48   }
49
50   StringRef getString() const { return Str; }
51
52   unsigned getLength() const { return (unsigned)Str.size(); }
53
54   typedef StringRef::iterator iterator;
55   
56   /// begin() - Pointer to the first byte of the string.
57   ///
58   iterator begin() const { return Str.begin(); }
59
60   /// end() - Pointer to one byte past the end of the string.
61   ///
62   iterator end() const { return Str.end(); }
63
64   /// Methods for support type inquiry through isa, cast, and dyn_cast:
65   static inline bool classof(const MDString *) { return true; }
66   static bool classof(const Value *V) {
67     return V->getValueID() == MDStringVal;
68   }
69 };
70
71   
72 class MDNodeOperand;
73   
74 //===----------------------------------------------------------------------===//
75 /// MDNode - a tuple of other values.
76 class MDNode : public Value, public FoldingSetNode {
77   MDNode(const MDNode &);                // DO NOT IMPLEMENT
78   void operator=(const MDNode &);        // DO NOT IMPLEMENT
79   friend class MDNodeOperand;
80   friend class LLVMContextImpl;
81
82   /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
83   /// end of this MDNode.
84   unsigned NumOperands;
85   
86   // Subclass data enums.
87   enum {
88     /// FunctionLocalBit - This bit is set if this MDNode is function local.
89     /// This is true when it (potentially transitively) contains a reference to
90     /// something in a function, like an argument, basicblock, or instruction.
91     FunctionLocalBit = 1 << 0,
92     
93     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
94     /// have a null operand.
95     NotUniquedBit    = 1 << 1,
96     
97     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
98     /// that the node isn't being destroyed with a plain 'delete'.
99     DestroyFlag      = 1 << 2
100   };
101   
102   // FunctionLocal enums.
103   enum FunctionLocalness {
104     FL_Unknown = -1,
105     FL_No = 0,
106     FL_Yes = 1
107   };
108   
109   /// replaceOperand - Replace each instance of F from the operand list of this 
110   /// node with T.
111   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
112   ~MDNode();
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   /// getTemporary - Return a temporary MDNode, for use in constructing
133   /// cyclic MDNode structures. A temporary MDNode is not uniqued,
134   /// may be RAUW'd, and must be manually deleted with deleteTemporary.
135   static MDNode *getTemporary(LLVMContext &Context, Value *const *Vals,
136                               unsigned NumVals);
137
138   /// deleteTemporary - Deallocate a node created by getTemporary. The
139   /// node must not have any users.
140   static void deleteTemporary(MDNode *N);
141   
142   /// getOperand - Return specified operand.
143   Value *getOperand(unsigned i) const;
144   
145   /// getNumOperands - Return number of MDNode operands.
146   unsigned getNumOperands() const { return NumOperands; }
147   
148   /// isFunctionLocal - Return whether MDNode is local to a function.
149   /// Note: MDNodes are designated as function-local when created, and keep
150   ///       that designation even if their operands are modified to no longer
151   ///       refer to function-local IR.
152   bool isFunctionLocal() const {
153     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
154   }
155   
156   // getFunction - If this metadata is function-local and recursively has a
157   // function-local operand, return the first such operand's parent function.
158   // Otherwise, return null. getFunction() should not be used for performance-
159   // critical code because it recursively visits all the MDNode's operands.  
160   const Function *getFunction() const;
161
162   /// Profile - calculate a unique identifier for this MDNode to collapse
163   /// duplicates
164   void Profile(FoldingSetNodeID &ID) const;
165
166   /// Methods for support type inquiry through isa, cast, and dyn_cast:
167   static inline bool classof(const MDNode *) { return true; }
168   static bool classof(const Value *V) {
169     return V->getValueID() == MDNodeVal;
170   }
171 private:
172   // destroy - Delete this node.  Only when there are no uses.
173   void destroy();
174
175   bool isNotUniqued() const { 
176     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
177   }
178   void setIsNotUniqued();
179   
180   // Shadow Value::setValueSubclassData with a private forwarding method so that
181   // any future subclasses cannot accidentally use it.
182   void setValueSubclassData(unsigned short D) {
183     Value::setValueSubclassData(D);
184   }
185 };
186
187 //===----------------------------------------------------------------------===//
188 /// NamedMDNode - a tuple of MDNodes. Despite its name, a NamedMDNode isn't
189 /// itself an MDNode. NamedMDNodes belong to modules, have names, and contain
190 /// lists of MDNodes.
191 class NamedMDNode : public ilist_node<NamedMDNode> {
192   friend class SymbolTableListTraits<NamedMDNode, Module>;
193   friend struct ilist_traits<NamedMDNode>;
194   friend class LLVMContextImpl;
195   friend class Module;
196   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
197
198   std::string Name;
199   Module *Parent;
200   void *Operands; // SmallVector<TrackingVH<MDNode>, 4>
201
202   void setParent(Module *M) { Parent = M; }
203
204 protected:
205   explicit NamedMDNode(const Twine &N);
206
207 public:
208   /// eraseFromParent - Drop all references and remove the node from parent
209   /// module.
210   void eraseFromParent();
211
212   /// dropAllReferences - Remove all uses and clear node vector.
213   void dropAllReferences();
214
215   /// ~NamedMDNode - Destroy NamedMDNode.
216   ~NamedMDNode();
217
218   /// getParent - Get the module that holds this named metadata collection.
219   inline Module *getParent() { return Parent; }
220   inline const Module *getParent() const { return Parent; }
221
222   /// getOperand - Return specified operand.
223   MDNode *getOperand(unsigned i) const;
224   
225   /// getNumOperands - Return the number of NamedMDNode operands.
226   unsigned getNumOperands() const;
227
228   /// addOperand - Add metadata operand.
229   void addOperand(MDNode *M);
230
231   /// getName - Return a constant reference to this named metadata's name.
232   StringRef getName() const;
233
234   /// print - Implement operator<< on NamedMDNode.
235   void print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW = 0) const;
236 };
237
238 } // end llvm namespace
239
240 #endif