Optimize MDNode to coallocate the operand list immediately
[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
30 //===----------------------------------------------------------------------===//
31 // MetadataBase  - A base class for MDNode, MDString and NamedMDNode.
32 class MetadataBase : public Value {
33 protected:
34   MetadataBase(const Type *Ty, unsigned scid)
35     : Value(Ty, scid) {}
36
37 public:
38
39   /// Methods for support type inquiry through isa, cast, and dyn_cast:
40   static inline bool classof(const MetadataBase *) { return true; }
41   static bool classof(const Value *V) {
42     return V->getValueID() == MDStringVal || V->getValueID() == MDNodeVal
43       || V->getValueID() == NamedMDNodeVal;
44   }
45 };
46
47 //===----------------------------------------------------------------------===//
48 /// MDString - a single uniqued string.
49 /// These are used to efficiently contain a byte sequence for metadata.
50 /// MDString is always unnamd.
51 class MDString : public MetadataBase {
52   MDString(const MDString &);            // DO NOT IMPLEMENT
53
54   StringRef Str;
55 protected:
56   explicit MDString(LLVMContext &C, StringRef S);
57
58 public:
59   static MDString *get(LLVMContext &Context, StringRef Str);
60   static MDString *get(LLVMContext &Context, const char *Str);
61   
62   StringRef getString() const { return Str; }
63
64   unsigned getLength() const { return (unsigned)Str.size(); }
65
66   typedef StringRef::iterator iterator;
67   
68   /// begin() - Pointer to the first byte of the string.
69   ///
70   iterator begin() const { return Str.begin(); }
71
72   /// end() - Pointer to one byte past the end of the string.
73   ///
74   iterator end() const { return Str.end(); }
75
76   /// Methods for support type inquiry through isa, cast, and dyn_cast:
77   static inline bool classof(const MDString *) { return true; }
78   static bool classof(const Value *V) {
79     return V->getValueID() == MDStringVal;
80   }
81 };
82
83   
84 class MDNodeElement;
85   
86 //===----------------------------------------------------------------------===//
87 /// MDNode - a tuple of other values.
88 class MDNode : public MetadataBase, public FoldingSetNode {
89   MDNode(const MDNode &);                // DO NOT IMPLEMENT
90   void operator=(const MDNode &);        // DO NOT IMPLEMENT
91   friend class MDNodeElement;
92
93   /// NumOperands - This many 'MDNodeElement' items are co-allocated onto the
94   /// end of this MDNode.
95   unsigned NumOperands;
96   
97   // Subclass data enums.
98   enum {
99     /// FunctionLocalBit - This bit is set if this MDNode is function local.
100     /// This is true when it (potentially transitively) contains a reference to
101     /// something in a function, like an argument, basicblock, or instruction.
102     FunctionLocalBit = 1 << 0,
103     
104     /// NotUniquedBit - This is set on MDNodes that are not uniqued because they
105     /// have a null perand.
106     NotUniquedBit    = 1 << 1,
107     
108     /// DestroyFlag - This bit is set by destroy() so the destructor can assert
109     /// that the node isn't being destroyed with a plain 'delete'.
110     DestroyFlag      = 1 << 2
111   };
112   
113   // Replace each instance of F from the element list of this node with T.
114   void replaceElement(MDNodeElement *Op, Value *NewVal);
115   ~MDNode();
116
117 protected:
118   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
119                   bool isFunctionLocal);
120 public:
121   // Constructors and destructors.
122   static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
123                      bool isFunctionLocal = false);
124   
125   /// getElement - Return specified element.
126   Value *getElement(unsigned i) const;
127   
128   /// getNumElements - Return number of MDNode elements.
129   unsigned getNumElements() const { return NumOperands; }
130   
131   /// isFunctionLocal - Return whether MDNode is local to a function.
132   /// Note: MDNodes are designated as function-local when created, and keep
133   ///       that designation even if their operands are modified to no longer
134   ///       refer to function-local IR.
135   bool isFunctionLocal() const {
136     return (getSubclassDataFromValue() & FunctionLocalBit) != 0;
137   }
138
139   // destroy - Delete this node.  Only when there are no uses.
140   void destroy();
141
142   /// Profile - calculate a unique identifier for this MDNode to collapse
143   /// duplicates
144   void Profile(FoldingSetNodeID &ID) const;
145
146   /// Methods for support type inquiry through isa, cast, and dyn_cast:
147   static inline bool classof(const MDNode *) { return true; }
148   static bool classof(const Value *V) {
149     return V->getValueID() == MDNodeVal;
150   }
151 private:
152   bool isNotUniqued() const { 
153     return (getSubclassDataFromValue() & NotUniquedBit) != 0;
154   }
155   void setIsNotUniqued() {
156     setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
157   }
158   
159   // Shadow Value::setValueSubclassData with a private forwarding method so that
160   // any future subclasses cannot accidentally use it.
161   void setValueSubclassData(unsigned short D) {
162     Value::setValueSubclassData(D);
163   }
164 };
165
166 //===----------------------------------------------------------------------===//
167 /// NamedMDNode - a tuple of other metadata. 
168 /// NamedMDNode is always named. All NamedMDNode element has a type of metadata.
169 template<typename ValueSubClass, typename ItemParentClass>
170   class SymbolTableListTraits;
171
172 class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
173   friend class SymbolTableListTraits<NamedMDNode, Module>;
174   friend class LLVMContextImpl;
175
176   NamedMDNode(const NamedMDNode &);      // DO NOT IMPLEMENT
177
178   Module *Parent;
179   void *Operands; // SmallVector<TrackingVH<MetadataBase>, 4>
180
181   void setParent(Module *M) { Parent = M; }
182 protected:
183   explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals, 
184                        unsigned NumVals, Module *M = 0);
185 public:
186   static NamedMDNode *Create(LLVMContext &C, const Twine &N, 
187                              MetadataBase *const *MDs, 
188                              unsigned NumMDs, Module *M = 0) {
189     return new NamedMDNode(C, N, MDs, NumMDs, M);
190   }
191
192   static NamedMDNode *Create(const NamedMDNode *NMD, Module *M = 0);
193
194   /// eraseFromParent - Drop all references and remove the node from parent
195   /// module.
196   void eraseFromParent();
197
198   /// dropAllReferences - Remove all uses and clear node vector.
199   void dropAllReferences();
200
201   /// ~NamedMDNode - Destroy NamedMDNode.
202   ~NamedMDNode();
203
204   /// getParent - Get the module that holds this named metadata collection.
205   inline Module *getParent() { return Parent; }
206   inline const Module *getParent() const { return Parent; }
207
208   /// getElement - Return specified element.
209   MetadataBase *getElement(unsigned i) const;
210   
211   /// getNumElements - Return number of NamedMDNode elements.
212   unsigned getNumElements() const;
213
214   /// addElement - Add metadata element.
215   void addElement(MetadataBase *M);
216   
217   /// Methods for support type inquiry through isa, cast, and dyn_cast:
218   static inline bool classof(const NamedMDNode *) { return true; }
219   static bool classof(const Value *V) {
220     return V->getValueID() == NamedMDNodeVal;
221   }
222 };
223
224 } // end llvm namespace
225
226 #endif