Add MDNode::getIfExists(), an efficient way to determine if a value is used by metada...
[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
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 perand.
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   // Replace each instance of F from the operand list of this node with T.
107   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
108   ~MDNode();
109
110 protected:
111   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
112                   bool isFunctionLocal);
113   
114   static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
115                            FunctionLocalness FL, bool Insert = true);
116 public:
117   // Constructors and destructors.
118   static MDNode *get(LLVMContext &Context, Value *const *Vals,
119                      unsigned NumVals);
120   // getWhenValsUnresolved - Construct MDNode determining function-localness
121   // from isFunctionLocal argument, not by analyzing Vals.
122   static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
123                                        unsigned NumVals, bool isFunctionLocal);
124                                        
125   static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
126                              unsigned NumVals);
127   
128   /// getOperand - Return specified operand.
129   Value *getOperand(unsigned i) const;
130   
131   /// getNumOperands - Return number of MDNode operands.
132   unsigned getNumOperands() const { return NumOperands; }
133   
134   /// isFunctionLocal - Return whether MDNode is local to a function.
135   /// Note: MDNodes are designated as function-local when created, and keep
136   ///       that designation even if their operands are modified to no longer
137   ///       refer to function-local IR.
138   bool isFunctionLocal() const {
139     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
140   }
141   
142   // getFunction - If this metadata is function-local and recursively has a
143   // function-local operand, return the first such operand's parent function.
144   // Otherwise, return null. getFunction() should not be used for performance-
145   // critical code because it recursively visits all the MDNode's operands.  
146   const Function *getFunction() const;
147
148   // destroy - Delete this node.  Only when there are no uses.
149   void destroy();
150
151   /// Profile - calculate a unique identifier for this MDNode to collapse
152   /// duplicates
153   void Profile(FoldingSetNodeID &ID) const;
154
155   /// Methods for support type inquiry through isa, cast, and dyn_cast:
156   static inline bool classof(const MDNode *) { return true; }
157   static bool classof(const Value *V) {
158     return V->getValueID() == MDNodeVal;
159   }
160 private:
161   bool isNotUniqued() const { 
162     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
163   }
164   void setIsNotUniqued() {
165     setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
166   }
167   
168   // Shadow Value::setValueSubclassData with a private forwarding method so that
169   // any future subclasses cannot accidentally use it.
170   void setValueSubclassData(unsigned short D) {
171     Value::setValueSubclassData(D);
172   }
173 };
174
175 //===----------------------------------------------------------------------===//
176 /// NamedMDNode - a tuple of MDNodes.
177 /// NamedMDNode is always named. All NamedMDNode operand has a type of metadata.
178 class NamedMDNode : public Value, public ilist_node<NamedMDNode> {
179   friend class SymbolTableListTraits<NamedMDNode, Module>;
180   friend struct ilist_traits<NamedMDNode>;
181   friend class LLVMContextImpl;
182   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
183
184   std::string Name;
185   Module *Parent;
186   void *Operands; // SmallVector<WeakVH<MDNode>, 4>
187
188   void setParent(Module *M) { Parent = M; }
189 protected:
190   explicit NamedMDNode(LLVMContext &C, const Twine &N, MDNode*const *Vals, 
191                        unsigned NumVals, Module *M = 0);
192 public:
193   static NamedMDNode *Create(LLVMContext &C, const Twine &N,
194                              MDNode *const *MDs, 
195                              unsigned NumMDs, Module *M = 0) {
196     return new NamedMDNode(C, N, MDs, NumMDs, M);
197   }
198
199   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
200
201   /// eraseFromParent - Drop all references and remove the node from parent
202   /// module.
203   void eraseFromParent();
204
205   /// dropAllReferences - Remove all uses and clear node vector.
206   void dropAllReferences();
207
208   /// ~NamedMDNode - Destroy NamedMDNode.
209   ~NamedMDNode();
210
211   /// getParent - Get the module that holds this named metadata collection.
212   inline Module *getParent() { return Parent; }
213   inline const Module *getParent() const { return Parent; }
214
215   /// getOperand - Return specified operand.
216   MDNode *getOperand(unsigned i) const;
217   
218   /// getNumOperands - Return the number of NamedMDNode operands.
219   unsigned getNumOperands() const;
220
221   /// addOperand - Add metadata operand.
222   void addOperand(MDNode *M);
223
224   /// setName - Set the name of this named metadata.
225   void setName(const Twine &NewName);
226
227   /// getName - Return a constant reference to this named metadata's name.
228   StringRef getName() const;
229
230   /// Methods for support type inquiry through isa, cast, and dyn_cast:
231   static inline bool classof(const NamedMDNode *) { return true; }
232   static bool classof(const Value *V) {
233     return V->getValueID() == NamedMDNodeVal;
234   }
235 };
236
237 } // end llvm namespace
238
239 #endif