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