Attempt to unbreak LLVM build, David G. please check.
[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 MetadataContextImpl;
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 MetadataContextImpl;
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 protected:
94   /// printCustom - Value subclasses can override this to implement custom
95   /// printing behavior.
96   virtual void printCustom(raw_ostream &O) const;
97
98 public:
99   Value(const Type *Ty, unsigned scid);
100   virtual ~Value();
101
102   /// dump - Support for debugging, callable in GDB: V->dump()
103   //
104   void dump() const;
105
106   /// print - Implement operator<< on Value.
107   ///
108   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
109
110   /// All values are typed, get the type of this value.
111   ///
112   inline const Type *getType() const { return VTy; }
113
114   /// All values hold a context through their type.
115   LLVMContext &getContext() const;
116
117   // All values can potentially be named...
118   inline bool hasName() const { return Name != 0; }
119   ValueName *getValueName() const { return Name; }
120   
121   /// getName() - Return a constant reference to the value's name. This is cheap
122   /// and guaranteed to return the same reference as long as the value is not
123   /// modified.
124   ///
125   /// This is currently guaranteed to return a StringRef for which data() points
126   /// to a valid null terminated string. The use of StringRef.data() is 
127   /// deprecated here, however, and clients should not rely on it. If such 
128   /// behavior is needed, clients should use expensive getNameStr(), or switch 
129   /// to an interface that does not depend on null termination.
130   StringRef getName() const;
131
132   /// getNameStr() - Return the name of the specified value, *constructing a
133   /// string* to hold it.  This is guaranteed to construct a string and is very
134   /// expensive, clients should use getName() unless necessary.
135   std::string getNameStr() const;
136
137   /// setName() - Change the name of the value, choosing a new unique name if
138   /// the provided name is taken.
139   ///
140   /// \arg Name - The new name; or "" if the value's name should be removed.
141   void setName(const Twine &Name);
142
143   
144   /// takeName - transfer the name from V to this value, setting V's name to
145   /// empty.  It is an error to call V->takeName(V). 
146   void takeName(Value *V);
147
148   /// replaceAllUsesWith - Go through the uses list for this definition and make
149   /// each use point to "V" instead of "this".  After this completes, 'this's
150   /// use list is guaranteed to be empty.
151   ///
152   void replaceAllUsesWith(Value *V);
153
154   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
155   // Only use when in type resolution situations!
156   void uncheckedReplaceAllUsesWith(Value *V);
157
158   //----------------------------------------------------------------------
159   // Methods for handling the chain of uses of this Value.
160   //
161   typedef value_use_iterator<User>       use_iterator;
162   typedef value_use_iterator<const User> use_const_iterator;
163
164   bool               use_empty() const { return UseList == 0; }
165   use_iterator       use_begin()       { return use_iterator(UseList); }
166   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
167   use_iterator       use_end()         { return use_iterator(0);   }
168   use_const_iterator use_end()   const { return use_const_iterator(0);   }
169   User              *use_back()        { return *use_begin(); }
170   const User        *use_back()  const { return *use_begin(); }
171
172   /// hasOneUse - Return true if there is exactly one user of this value.  This
173   /// is specialized because it is a common request and does not require
174   /// traversing the whole use list.
175   ///
176   bool hasOneUse() const {
177     use_const_iterator I = use_begin(), E = use_end();
178     if (I == E) return false;
179     return ++I == E;
180   }
181
182   /// hasNUses - Return true if this Value has exactly N users.
183   ///
184   bool hasNUses(unsigned N) const;
185
186   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
187   /// logically equivalent to getNumUses() >= N.
188   ///
189   bool hasNUsesOrMore(unsigned N) const;
190
191   bool isUsedInBasicBlock(const BasicBlock *BB) const;
192
193   /// getNumUses - This method computes the number of uses of this Value.  This
194   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
195   /// to check for specific values.
196   unsigned getNumUses() const;
197
198   /// addUse - This method should only be used by the Use class.
199   ///
200   void addUse(Use &U) { U.addToList(&UseList); }
201
202   /// An enumeration for keeping track of the concrete subclass of Value that
203   /// is actually instantiated. Values of this enumeration are kept in the 
204   /// Value classes SubclassID field. They are used for concrete type
205   /// identification.
206   enum ValueTy {
207     ArgumentVal,              // This is an instance of Argument
208     BasicBlockVal,            // This is an instance of BasicBlock
209     FunctionVal,              // This is an instance of Function
210     GlobalAliasVal,           // This is an instance of GlobalAlias
211     GlobalVariableVal,        // This is an instance of GlobalVariable
212     UndefValueVal,            // This is an instance of UndefValue
213     BlockAddressVal,          // This is an instance of BlockAddress
214     ConstantExprVal,          // This is an instance of ConstantExpr
215     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
216     ConstantIntVal,           // This is an instance of ConstantInt
217     ConstantFPVal,            // This is an instance of ConstantFP
218     ConstantArrayVal,         // This is an instance of ConstantArray
219     ConstantStructVal,        // This is an instance of ConstantStruct
220     ConstantVectorVal,        // This is an instance of ConstantVector
221     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
222     MDNodeVal,                // This is an instance of MDNode
223     MDStringVal,              // This is an instance of MDString
224     NamedMDNodeVal,           // This is an instance of NamedMDNode
225     InlineAsmVal,             // This is an instance of InlineAsm
226     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
227     FixedStackPseudoSourceValueVal, // This is an instance of PseudoSourceValue
228     InstructionVal,           // This is an instance of Instruction
229     
230     // Markers:
231     ConstantFirstVal = FunctionVal,
232     ConstantLastVal  = ConstantPointerNullVal
233   };
234
235   /// getValueID - Return an ID for the concrete type of this object.  This is
236   /// used to implement the classof checks.  This should not be used for any
237   /// other purpose, as the values may change as LLVM evolves.  Also, note that
238   /// for instructions, the Instruction's opcode is added to InstructionVal. So
239   /// this means three things:
240   /// # there is no value with code InstructionVal (no opcode==0).
241   /// # there are more possible values for the value type than in ValueTy enum.
242   /// # the InstructionVal enumerator must be the highest valued enumerator in
243   ///   the ValueTy enum.
244   unsigned getValueID() const {
245     return SubclassID;
246   }
247
248   /// getRawSubclassOptionalData - Return the raw optional flags value
249   /// contained in this value. This should only be used when testing two
250   /// Values for equivalence.
251   unsigned getRawSubclassOptionalData() const {
252     return SubclassOptionalData;
253   }
254
255   /// hasSameSubclassOptionalData - Test whether the optional flags contained
256   /// in this value are equal to the optional flags in the given value.
257   bool hasSameSubclassOptionalData(const Value *V) const {
258     return SubclassOptionalData == V->SubclassOptionalData;
259   }
260
261   /// intersectOptionalDataWith - Clear any optional flags in this value
262   /// that are not also set in the given value.
263   void intersectOptionalDataWith(const Value *V) {
264     SubclassOptionalData &= V->SubclassOptionalData;
265   }
266
267   // Methods for support type inquiry through isa, cast, and dyn_cast:
268   static inline bool classof(const Value *) {
269     return true; // Values are always values.
270   }
271
272   /// getRawType - This should only be used to implement the vmcore library.
273   ///
274   const Type *getRawType() const { return VTy.getRawType(); }
275
276   /// stripPointerCasts - This method strips off any unneeded pointer
277   /// casts from the specified value, returning the original uncasted value.
278   /// Note that the returned value has pointer type if the specified value does.
279   Value *stripPointerCasts();
280   const Value *stripPointerCasts() const {
281     return const_cast<Value*>(this)->stripPointerCasts();
282   }
283
284   /// getUnderlyingObject - This method strips off any GEP address adjustments
285   /// and pointer casts from the specified value, returning the original object
286   /// being addressed.  Note that the returned value has pointer type if the
287   /// specified value does.
288   Value *getUnderlyingObject();
289   const Value *getUnderlyingObject() const {
290     return const_cast<Value*>(this)->getUnderlyingObject();
291   }
292   
293   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
294   /// return the value in the PHI node corresponding to PredBB.  If not, return
295   /// ourself.  This is useful if you want to know the value something has in a
296   /// predecessor block.
297   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
298
299   const Value *DoPHITranslation(const BasicBlock *CurBB,
300                                 const BasicBlock *PredBB) const{
301     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
302   }
303
304   /// hasMetadata - Return true if metadata is attached with this value.
305   bool hasMetadata() const { return HasMetadata; }
306 };
307
308 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
309   V.print(OS);
310   return OS;
311 }
312   
313 void Use::set(Value *V) {
314   if (Val) removeFromList();
315   Val = V;
316   if (V) V->addUse(*this);
317 }
318
319
320 // isa - Provide some specializations of isa so that we don't have to include
321 // the subtype header files to test to see if the value is a subclass...
322 //
323 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
324   return Val.getValueID() >= Value::ConstantFirstVal &&
325          Val.getValueID() <= Value::ConstantLastVal;
326 }
327 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
328   return Val.getValueID() == Value::ArgumentVal;
329 }
330 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
331   return Val.getValueID() == Value::InlineAsmVal;
332 }
333 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
334   return Val.getValueID() >= Value::InstructionVal;
335 }
336 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
337   return Val.getValueID() == Value::BasicBlockVal;
338 }
339 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
340   return Val.getValueID() == Value::FunctionVal;
341 }
342 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
343   return Val.getValueID() == Value::GlobalVariableVal;
344 }
345 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
346   return Val.getValueID() == Value::GlobalAliasVal;
347 }
348 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
349   return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
350          isa<GlobalAlias>(Val);
351 }
352   
353   
354 // Value* is only 4-byte aligned.
355 template<>
356 class PointerLikeTypeTraits<Value*> {
357   typedef Value* PT;
358 public:
359   static inline void *getAsVoidPointer(PT P) { return P; }
360   static inline PT getFromVoidPointer(void *P) {
361     return static_cast<PT>(P);
362   }
363   enum { NumLowBitsAvailable = 2 };
364 };
365
366 } // End llvm namespace
367
368 #endif