Add llvm::Metadata to manage metadata used in a context.
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class ------------*- 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 // This file declares the Value class. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_VALUE_H
15 #define LLVM_VALUE_H
16
17 #include "llvm/AbstractTypeUser.h"
18 #include "llvm/Use.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Casting.h"
22 #include <string>
23
24 namespace llvm {
25
26 class Constant;
27 class Argument;
28 class Instruction;
29 class BasicBlock;
30 class GlobalValue;
31 class Function;
32 class GlobalVariable;
33 class GlobalAlias;
34 class InlineAsm;
35 class ValueSymbolTable;
36 class TypeSymbolTable;
37 template<typename ValueTy> class StringMapEntry;
38 template <typename ValueTy = Value>
39 class AssertingVH;
40 typedef StringMapEntry<Value*> ValueName;
41 class raw_ostream;
42 class AssemblyAnnotationWriter;
43 class ValueHandleBase;
44 class LLVMContext;
45 class Metadata;
46
47 //===----------------------------------------------------------------------===//
48 //                                 Value Class
49 //===----------------------------------------------------------------------===//
50
51 /// This is a very important LLVM class. It is the base class of all values 
52 /// computed by a program that may be used as operands to other values. Value is
53 /// the super class of other important classes such as Instruction and Function.
54 /// All Values have a Type. Type is not a subclass of Value. All types can have
55 /// a name and they should belong to some Module. Setting the name on the Value
56 /// automatically updates the module's symbol table.
57 ///
58 /// Every value has a "use list" that keeps track of which other Values are
59 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
60 /// objects that watch it and listen to RAUW and Destroy events see
61 /// llvm/Support/ValueHandle.h for details.
62 ///
63 /// @brief LLVM Value Representation
64 class Value {
65   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
66   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
67   unsigned char HasMetadata : 1;    // Has a metadata attached to this ?
68 protected:
69   /// SubclassOptionalData - This member is similar to SubclassData, however it
70   /// is for holding information which may be used to aid optimization, but
71   /// which may be cleared to zero without affecting conservative
72   /// interpretation.
73   unsigned char SubclassOptionalData : 7;
74
75   /// SubclassData - This member is defined by this class, but is not used for
76   /// anything.  Subclasses can use it to hold whatever state they find useful.
77   /// This field is initialized to zero by the ctor.
78   unsigned short SubclassData;
79 private:
80   PATypeHolder VTy;
81   Use *UseList;
82
83   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
84   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
85   friend class ValueHandleBase;
86   friend class Metadata;
87   friend class AbstractTypeUser;
88   ValueName *Name;
89
90   void operator=(const Value &);     // Do not implement
91   Value(const Value &);              // Do not implement
92
93 public:
94   Value(const Type *Ty, unsigned scid);
95   virtual ~Value();
96
97   /// dump - Support for debugging, callable in GDB: V->dump()
98   //
99   virtual void dump() const;
100
101   /// print - Implement operator<< on Value.
102   ///
103   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
104
105   /// All values are typed, get the type of this value.
106   ///
107   inline const Type *getType() const { return VTy; }
108
109   /// All values hold a context through their type.
110   LLVMContext &getContext() const;
111
112   // All values can potentially be named...
113   inline bool hasName() const { return Name != 0; }
114   ValueName *getValueName() const { return Name; }
115   
116   /// getName() - Return a constant reference to the value's name. This is cheap
117   /// and guaranteed to return the same reference as long as the value is not
118   /// modified.
119   ///
120   /// This is currently guaranteed to return a StringRef for which data() points
121   /// to a valid null terminated string. The use of StringRef.data() is 
122   /// deprecated here, however, and clients should not rely on it. If such 
123   /// behavior is needed, clients should use expensive getNameStr(), or switch 
124   /// to an interface that does not depend on null termination.
125   StringRef getName() const;
126
127   /// getNameStr() - Return the name of the specified value, *constructing a
128   /// string* to hold it.  This is guaranteed to construct a string and is very
129   /// expensive, clients should use getName() unless necessary.
130   std::string getNameStr() const;
131
132   /// setName() - Change the name of the value, choosing a new unique name if
133   /// the provided name is taken.
134   ///
135   /// \arg Name - The new name; or "" if the value's name should be removed.
136   void setName(const Twine &Name);
137
138   
139   /// takeName - transfer the name from V to this value, setting V's name to
140   /// empty.  It is an error to call V->takeName(V). 
141   void takeName(Value *V);
142
143   /// replaceAllUsesWith - Go through the uses list for this definition and make
144   /// each use point to "V" instead of "this".  After this completes, 'this's
145   /// use list is guaranteed to be empty.
146   ///
147   void replaceAllUsesWith(Value *V);
148
149   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
150   // Only use when in type resolution situations!
151   void uncheckedReplaceAllUsesWith(Value *V);
152
153   //----------------------------------------------------------------------
154   // Methods for handling the chain of uses of this Value.
155   //
156   typedef value_use_iterator<User>       use_iterator;
157   typedef value_use_iterator<const User> use_const_iterator;
158
159   bool               use_empty() const { return UseList == 0; }
160   use_iterator       use_begin()       { return use_iterator(UseList); }
161   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
162   use_iterator       use_end()         { return use_iterator(0);   }
163   use_const_iterator use_end()   const { return use_const_iterator(0);   }
164   User              *use_back()        { return *use_begin(); }
165   const User        *use_back()  const { return *use_begin(); }
166
167   /// hasOneUse - Return true if there is exactly one user of this value.  This
168   /// is specialized because it is a common request and does not require
169   /// traversing the whole use list.
170   ///
171   bool hasOneUse() const {
172     use_const_iterator I = use_begin(), E = use_end();
173     if (I == E) return false;
174     return ++I == E;
175   }
176
177   /// hasNUses - Return true if this Value has exactly N users.
178   ///
179   bool hasNUses(unsigned N) const;
180
181   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
182   /// logically equivalent to getNumUses() >= N.
183   ///
184   bool hasNUsesOrMore(unsigned N) const;
185
186   bool isUsedInBasicBlock(const BasicBlock *BB) const;
187
188   /// getNumUses - This method computes the number of uses of this Value.  This
189   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
190   /// to check for specific values.
191   unsigned getNumUses() const;
192
193   /// addUse - This method should only be used by the Use class.
194   ///
195   void addUse(Use &U) { U.addToList(&UseList); }
196
197   /// An enumeration for keeping track of the concrete subclass of Value that
198   /// is actually instantiated. Values of this enumeration are kept in the 
199   /// Value classes SubclassID field. They are used for concrete type
200   /// identification.
201   enum ValueTy {
202     ArgumentVal,              // This is an instance of Argument
203     BasicBlockVal,            // This is an instance of BasicBlock
204     FunctionVal,              // This is an instance of Function
205     GlobalAliasVal,           // This is an instance of GlobalAlias
206     GlobalVariableVal,        // This is an instance of GlobalVariable
207     UndefValueVal,            // This is an instance of UndefValue
208     ConstantExprVal,          // This is an instance of ConstantExpr
209     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
210     ConstantIntVal,           // This is an instance of ConstantInt
211     ConstantFPVal,            // This is an instance of ConstantFP
212     ConstantArrayVal,         // This is an instance of ConstantArray
213     ConstantStructVal,        // This is an instance of ConstantStruct
214     ConstantVectorVal,        // This is an instance of ConstantVector
215     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
216     MDNodeVal,                // This is an instance of MDNode
217     MDStringVal,              // This is an instance of MDString
218     NamedMDNodeVal,           // This is an instance of NamedMDNode
219     InlineAsmVal,             // This is an instance of InlineAsm
220     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
221     InstructionVal,           // This is an instance of Instruction
222     
223     // Markers:
224     ConstantFirstVal = FunctionVal,
225     ConstantLastVal  = ConstantPointerNullVal
226   };
227
228   /// getValueID - Return an ID for the concrete type of this object.  This is
229   /// used to implement the classof checks.  This should not be used for any
230   /// other purpose, as the values may change as LLVM evolves.  Also, note that
231   /// for instructions, the Instruction's opcode is added to InstructionVal. So
232   /// this means three things:
233   /// # there is no value with code InstructionVal (no opcode==0).
234   /// # there are more possible values for the value type than in ValueTy enum.
235   /// # the InstructionVal enumerator must be the highest valued enumerator in
236   ///   the ValueTy enum.
237   unsigned getValueID() const {
238     return SubclassID;
239   }
240
241   /// getRawSubclassOptionalData - Return the raw optional flags value
242   /// contained in this value. This should only be used when testing two
243   /// Values for equivalence.
244   unsigned getRawSubclassOptionalData() const {
245     return SubclassOptionalData;
246   }
247
248   /// hasSameSubclassOptionalData - Test whether the optional flags contained
249   /// in this value are equal to the optional flags in the given value.
250   bool hasSameSubclassOptionalData(const Value *V) const {
251     return SubclassOptionalData == V->SubclassOptionalData;
252   }
253
254   /// intersectOptionalDataWith - Clear any optional flags in this value
255   /// that are not also set in the given value.
256   void intersectOptionalDataWith(const Value *V) {
257     SubclassOptionalData &= V->SubclassOptionalData;
258   }
259
260   // Methods for support type inquiry through isa, cast, and dyn_cast:
261   static inline bool classof(const Value *) {
262     return true; // Values are always values.
263   }
264
265   /// getRawType - This should only be used to implement the vmcore library.
266   ///
267   const Type *getRawType() const { return VTy.getRawType(); }
268
269   /// stripPointerCasts - This method strips off any unneeded pointer
270   /// casts from the specified value, returning the original uncasted value.
271   /// Note that the returned value has pointer type if the specified value does.
272   Value *stripPointerCasts();
273   const Value *stripPointerCasts() const {
274     return const_cast<Value*>(this)->stripPointerCasts();
275   }
276
277   /// getUnderlyingObject - This method strips off any GEP address adjustments
278   /// and pointer casts from the specified value, returning the original object
279   /// being addressed.  Note that the returned value has pointer type if the
280   /// specified value does.
281   Value *getUnderlyingObject();
282   const Value *getUnderlyingObject() const {
283     return const_cast<Value*>(this)->getUnderlyingObject();
284   }
285   
286   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
287   /// return the value in the PHI node corresponding to PredBB.  If not, return
288   /// ourself.  This is useful if you want to know the value something has in a
289   /// predecessor block.
290   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
291
292   const Value *DoPHITranslation(const BasicBlock *CurBB,
293                                 const BasicBlock *PredBB) const{
294     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
295   }
296 };
297
298 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
299   V.print(OS);
300   return OS;
301 }
302   
303 void Use::set(Value *V) {
304   if (Val) removeFromList();
305   Val = V;
306   if (V) V->addUse(*this);
307 }
308
309
310 // isa - Provide some specializations of isa so that we don't have to include
311 // the subtype header files to test to see if the value is a subclass...
312 //
313 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
314   return Val.getValueID() >= Value::ConstantFirstVal &&
315          Val.getValueID() <= Value::ConstantLastVal;
316 }
317 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
318   return Val.getValueID() == Value::ArgumentVal;
319 }
320 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
321   return Val.getValueID() == Value::InlineAsmVal;
322 }
323 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
324   return Val.getValueID() >= Value::InstructionVal;
325 }
326 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
327   return Val.getValueID() == Value::BasicBlockVal;
328 }
329 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
330   return Val.getValueID() == Value::FunctionVal;
331 }
332 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
333   return Val.getValueID() == Value::GlobalVariableVal;
334 }
335 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
336   return Val.getValueID() == Value::GlobalAliasVal;
337 }
338 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
339   return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
340          isa<GlobalAlias>(Val);
341 }
342   
343   
344 // Value* is only 4-byte aligned.
345 template<>
346 class PointerLikeTypeTraits<Value*> {
347   typedef Value* PT;
348 public:
349   static inline void *getAsVoidPointer(PT P) { return P; }
350   static inline PT getFromVoidPointer(void *P) {
351     return static_cast<PT>(P);
352   }
353   enum { NumLowBitsAvailable = 2 };
354 };
355
356 } // End llvm namespace
357
358 #endif