Fix case for include of Compiler.h.
[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/Use.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/Compiler.h"
20
21 namespace llvm {
22
23 class Constant;
24 class Argument;
25 class Instruction;
26 class BasicBlock;
27 class GlobalValue;
28 class Function;
29 class GlobalVariable;
30 class GlobalAlias;
31 class InlineAsm;
32 class ValueSymbolTable;
33 template<typename ValueTy> class StringMapEntry;
34 typedef StringMapEntry<Value*> ValueName;
35 class raw_ostream;
36 class AssemblyAnnotationWriter;
37 class ValueHandleBase;
38 class LLVMContext;
39 class Twine;
40 class MDNode;
41 class Type;
42 class StringRef;
43
44 //===----------------------------------------------------------------------===//
45 //                                 Value Class
46 //===----------------------------------------------------------------------===//
47
48 /// This is a very important LLVM class. It is the base class of all values 
49 /// computed by a program that may be used as operands to other values. Value is
50 /// the super class of other important classes such as Instruction and Function.
51 /// All Values have a Type. Type is not a subclass of Value. Some values can
52 /// have a name and they belong to some Module.  Setting the name on the Value
53 /// automatically updates the module's symbol table.
54 ///
55 /// Every value has a "use list" that keeps track of which other Values are
56 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
57 /// objects that watch it and listen to RAUW and Destroy events.  See
58 /// llvm/Support/ValueHandle.h for details.
59 ///
60 /// @brief LLVM Value Representation
61 class Value {
62   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
63   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
64 protected:
65   /// SubclassOptionalData - This member is similar to SubclassData, however it
66   /// is for holding information which may be used to aid optimization, but
67   /// which may be cleared to zero without affecting conservative
68   /// interpretation.
69   unsigned char SubclassOptionalData : 7;
70
71 private:
72   /// SubclassData - This member is defined by this class, but is not used for
73   /// anything.  Subclasses can use it to hold whatever state they find useful.
74   /// This field is initialized to zero by the ctor.
75   unsigned short SubclassData;
76
77   Type *VTy;
78   Use *UseList;
79
80   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
81   friend class ValueHandleBase;
82   ValueName *Name;
83
84   void operator=(const Value &) LLVM_DELETED_FUNCTION;
85   Value(const Value &) LLVM_DELETED_FUNCTION;
86
87 protected:
88   /// printCustom - Value subclasses can override this to implement custom
89   /// printing behavior.
90   virtual void printCustom(raw_ostream &O) const;
91
92   Value(Type *Ty, unsigned scid);
93 public:
94   virtual ~Value();
95
96   /// dump - Support for debugging, callable in GDB: V->dump()
97   //
98   void dump() const;
99
100   /// print - Implement operator<< on Value.
101   ///
102   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
103
104   /// All values are typed, get the type of this value.
105   ///
106   Type *getType() const { return VTy; }
107
108   /// All values hold a context through their type.
109   LLVMContext &getContext() const;
110
111   // All values can potentially be named.
112   bool hasName() const { return Name != 0 && SubclassID != MDStringVal; }
113   ValueName *getValueName() const { return Name; }
114   void setValueName(ValueName *VN) { Name = VN; }
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   StringRef getName() const;
120
121   /// setName() - Change the name of the value, choosing a new unique name if
122   /// the provided name is taken.
123   ///
124   /// \param Name The new name; or "" if the value's name should be removed.
125   void setName(const Twine &Name);
126
127   
128   /// takeName - transfer the name from V to this value, setting V's name to
129   /// empty.  It is an error to call V->takeName(V). 
130   void takeName(Value *V);
131
132   /// replaceAllUsesWith - Go through the uses list for this definition and make
133   /// each use point to "V" instead of "this".  After this completes, 'this's
134   /// use list is guaranteed to be empty.
135   ///
136   void replaceAllUsesWith(Value *V);
137
138   //----------------------------------------------------------------------
139   // Methods for handling the chain of uses of this Value.
140   //
141   typedef value_use_iterator<User>       use_iterator;
142   typedef value_use_iterator<const User> const_use_iterator;
143
144   bool               use_empty() const { return UseList == 0; }
145   use_iterator       use_begin()       { return use_iterator(UseList); }
146   const_use_iterator use_begin() const { return const_use_iterator(UseList); }
147   use_iterator       use_end()         { return use_iterator(0);   }
148   const_use_iterator use_end()   const { return const_use_iterator(0);   }
149   User              *use_back()        { return *use_begin(); }
150   const User        *use_back()  const { return *use_begin(); }
151
152   /// hasOneUse - Return true if there is exactly one user of this value.  This
153   /// is specialized because it is a common request and does not require
154   /// traversing the whole use list.
155   ///
156   bool hasOneUse() const {
157     const_use_iterator I = use_begin(), E = use_end();
158     if (I == E) return false;
159     return ++I == E;
160   }
161
162   /// hasNUses - Return true if this Value has exactly N users.
163   ///
164   bool hasNUses(unsigned N) const;
165
166   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
167   /// logically equivalent to getNumUses() >= N.
168   ///
169   bool hasNUsesOrMore(unsigned N) const;
170
171   bool isUsedInBasicBlock(const BasicBlock *BB) const;
172
173   /// getNumUses - This method computes the number of uses of this Value.  This
174   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasNUsesOrMore
175   /// to check for specific values.
176   unsigned getNumUses() const;
177
178   /// addUse - This method should only be used by the Use class.
179   ///
180   void addUse(Use &U) { U.addToList(&UseList); }
181
182   /// An enumeration for keeping track of the concrete subclass of Value that
183   /// is actually instantiated. Values of this enumeration are kept in the 
184   /// Value classes SubclassID field. They are used for concrete type
185   /// identification.
186   enum ValueTy {
187     ArgumentVal,              // This is an instance of Argument
188     BasicBlockVal,            // This is an instance of BasicBlock
189     FunctionVal,              // This is an instance of Function
190     GlobalAliasVal,           // This is an instance of GlobalAlias
191     GlobalVariableVal,        // This is an instance of GlobalVariable
192     UndefValueVal,            // This is an instance of UndefValue
193     BlockAddressVal,          // This is an instance of BlockAddress
194     ConstantExprVal,          // This is an instance of ConstantExpr
195     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
196     ConstantDataArrayVal,     // This is an instance of ConstantDataArray
197     ConstantDataVectorVal,    // This is an instance of ConstantDataVector
198     ConstantIntVal,           // This is an instance of ConstantInt
199     ConstantFPVal,            // This is an instance of ConstantFP
200     ConstantArrayVal,         // This is an instance of ConstantArray
201     ConstantStructVal,        // This is an instance of ConstantStruct
202     ConstantVectorVal,        // This is an instance of ConstantVector
203     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
204     MDNodeVal,                // This is an instance of MDNode
205     MDStringVal,              // This is an instance of MDString
206     InlineAsmVal,             // This is an instance of InlineAsm
207     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
208     FixedStackPseudoSourceValueVal, // This is an instance of 
209                                     // FixedStackPseudoSourceValue
210     InstructionVal,           // This is an instance of Instruction
211     // Enum values starting at InstructionVal are used for Instructions;
212     // don't add new values here!
213
214     // Markers:
215     ConstantFirstVal = FunctionVal,
216     ConstantLastVal  = ConstantPointerNullVal
217   };
218
219   /// getValueID - Return an ID for the concrete type of this object.  This is
220   /// used to implement the classof checks.  This should not be used for any
221   /// other purpose, as the values may change as LLVM evolves.  Also, note that
222   /// for instructions, the Instruction's opcode is added to InstructionVal. So
223   /// this means three things:
224   /// # there is no value with code InstructionVal (no opcode==0).
225   /// # there are more possible values for the value type than in ValueTy enum.
226   /// # the InstructionVal enumerator must be the highest valued enumerator in
227   ///   the ValueTy enum.
228   unsigned getValueID() const {
229     return SubclassID;
230   }
231
232   /// getRawSubclassOptionalData - Return the raw optional flags value
233   /// contained in this value. This should only be used when testing two
234   /// Values for equivalence.
235   unsigned getRawSubclassOptionalData() const {
236     return SubclassOptionalData;
237   }
238
239   /// clearSubclassOptionalData - Clear the optional flags contained in
240   /// this value.
241   void clearSubclassOptionalData() {
242     SubclassOptionalData = 0;
243   }
244
245   /// hasSameSubclassOptionalData - Test whether the optional flags contained
246   /// in this value are equal to the optional flags in the given value.
247   bool hasSameSubclassOptionalData(const Value *V) const {
248     return SubclassOptionalData == V->SubclassOptionalData;
249   }
250
251   /// intersectOptionalDataWith - Clear any optional flags in this value
252   /// that are not also set in the given value.
253   void intersectOptionalDataWith(const Value *V) {
254     SubclassOptionalData &= V->SubclassOptionalData;
255   }
256
257   /// hasValueHandle - Return true if there is a value handle associated with
258   /// this value.
259   bool hasValueHandle() const { return HasValueHandle; }
260
261   /// stripPointerCasts - This method strips off any unneeded pointer casts and
262   /// all-zero GEPs from the specified value, returning the original uncasted
263   /// value. If this is called on a non-pointer value, it returns 'this'.
264   Value *stripPointerCasts();
265   const Value *stripPointerCasts() const {
266     return const_cast<Value*>(this)->stripPointerCasts();
267   }
268
269   /// stripInBoundsConstantOffsets - This method strips off unneeded pointer casts and
270   /// all-constant GEPs from the specified value, returning the original
271   /// pointer value. If this is called on a non-pointer value, it returns
272   /// 'this'.
273   Value *stripInBoundsConstantOffsets();
274   const Value *stripInBoundsConstantOffsets() const {
275     return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
276   }
277
278   /// stripInBoundsOffsets - This method strips off unneeded pointer casts and
279   /// any in-bounds Offsets from the specified value, returning the original
280   /// pointer value. If this is called on a non-pointer value, it returns
281   /// 'this'.
282   Value *stripInBoundsOffsets();
283   const Value *stripInBoundsOffsets() const {
284     return const_cast<Value*>(this)->stripInBoundsOffsets();
285   }
286
287   /// isDereferenceablePointer - Test if this value is always a pointer to
288   /// allocated and suitably aligned memory for a simple load or store.
289   bool isDereferenceablePointer() const;
290   
291   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
292   /// return the value in the PHI node corresponding to PredBB.  If not, return
293   /// ourself.  This is useful if you want to know the value something has in a
294   /// predecessor block.
295   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
296
297   const Value *DoPHITranslation(const BasicBlock *CurBB,
298                                 const BasicBlock *PredBB) const{
299     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
300   }
301   
302   /// MaximumAlignment - This is the greatest alignment value supported by
303   /// load, store, and alloca instructions, and global values.
304   static const unsigned MaximumAlignment = 1u << 29;
305   
306   /// mutateType - Mutate the type of this Value to be of the specified type.
307   /// Note that this is an extremely dangerous operation which can create
308   /// completely invalid IR very easily.  It is strongly recommended that you
309   /// recreate IR objects with the right types instead of mutating them in
310   /// place.
311   void mutateType(Type *Ty) {
312     VTy = Ty;
313   }
314   
315 protected:
316   unsigned short getSubclassDataFromValue() const { return SubclassData; }
317   void setValueSubclassData(unsigned short D) { SubclassData = D; }
318 };
319
320 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
321   V.print(OS);
322   return OS;
323 }
324   
325 void Use::set(Value *V) {
326   if (Val) removeFromList();
327   Val = V;
328   if (V) V->addUse(*this);
329 }
330
331
332 // isa - Provide some specializations of isa so that we don't have to include
333 // the subtype header files to test to see if the value is a subclass...
334 //
335 template <> struct isa_impl<Constant, Value> {
336   static inline bool doit(const Value &Val) {
337     return Val.getValueID() >= Value::ConstantFirstVal &&
338       Val.getValueID() <= Value::ConstantLastVal;
339   }
340 };
341
342 template <> struct isa_impl<Argument, Value> {
343   static inline bool doit (const Value &Val) {
344     return Val.getValueID() == Value::ArgumentVal;
345   }
346 };
347
348 template <> struct isa_impl<InlineAsm, Value> { 
349   static inline bool doit(const Value &Val) {
350     return Val.getValueID() == Value::InlineAsmVal;
351   }
352 };
353
354 template <> struct isa_impl<Instruction, Value> { 
355   static inline bool doit(const Value &Val) {
356     return Val.getValueID() >= Value::InstructionVal;
357   }
358 };
359
360 template <> struct isa_impl<BasicBlock, Value> { 
361   static inline bool doit(const Value &Val) {
362     return Val.getValueID() == Value::BasicBlockVal;
363   }
364 };
365
366 template <> struct isa_impl<Function, Value> { 
367   static inline bool doit(const Value &Val) {
368     return Val.getValueID() == Value::FunctionVal;
369   }
370 };
371
372 template <> struct isa_impl<GlobalVariable, Value> { 
373   static inline bool doit(const Value &Val) {
374     return Val.getValueID() == Value::GlobalVariableVal;
375   }
376 };
377
378 template <> struct isa_impl<GlobalAlias, Value> { 
379   static inline bool doit(const Value &Val) {
380     return Val.getValueID() == Value::GlobalAliasVal;
381   }
382 };
383
384 template <> struct isa_impl<GlobalValue, Value> { 
385   static inline bool doit(const Value &Val) {
386     return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
387       isa<GlobalAlias>(Val);
388   }
389 };
390
391 template <> struct isa_impl<MDNode, Value> { 
392   static inline bool doit(const Value &Val) {
393     return Val.getValueID() == Value::MDNodeVal;
394   }
395 };
396   
397 // Value* is only 4-byte aligned.
398 template<>
399 class PointerLikeTypeTraits<Value*> {
400   typedef Value* PT;
401 public:
402   static inline void *getAsVoidPointer(PT P) { return P; }
403   static inline PT getFromVoidPointer(void *P) {
404     return static_cast<PT>(P);
405   }
406   enum { NumLowBitsAvailable = 2 };
407 };
408
409 } // End llvm namespace
410
411 #endif