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