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