86484bacb9232bc4ec83d1e4f7f09a3070a78175
[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/Support/Casting.h"
20 #include <iosfwd>
21 #include <string>
22
23 namespace llvm {
24
25 class Constant;
26 class Argument;
27 class Instruction;
28 class BasicBlock;
29 class GlobalValue;
30 class Function;
31 class GlobalVariable;
32 class GlobalAlias;
33 class InlineAsm;
34 class ValueSymbolTable;
35 class TypeSymbolTable;
36 template<typename ValueTy> class StringMapEntry;
37 template <typename ValueTy = Value>
38 class AssertingVH;
39 typedef StringMapEntry<AssertingVH<> > ValueName;
40 class raw_ostream;
41 class AssemblyAnnotationWriter;
42 class ValueHandleBase;
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. All types can have
52 /// a name and they should 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   /// SubclassData - This member is defined by this class, but is not used for
66   /// anything.  Subclasses can use it to hold whatever state they find useful.
67   /// This field is initialized to zero by the ctor.
68   unsigned short SubclassData;
69 private:
70   PATypeHolder VTy;
71   Use *UseList;
72
73   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
74   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
75   friend class ValueHandleBase;
76   ValueName *Name;
77
78   void operator=(const Value &);     // Do not implement
79   Value(const Value &);              // Do not implement
80
81 public:
82   Value(const Type *Ty, unsigned scid);
83   virtual ~Value();
84
85   /// dump - Support for debugging, callable in GDB: V->dump()
86   //
87   virtual void dump() const;
88
89   /// print - Implement operator<< on Value.
90   ///
91   void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
92   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
93
94   /// All values are typed, get the type of this value.
95   ///
96   inline const Type *getType() const { return VTy; }
97
98   // All values can potentially be named...
99   inline bool hasName() const { return Name != 0; }
100   ValueName *getValueName() const { return Name; }
101
102   /// getNameStart - Return a pointer to a null terminated string for this name.
103   /// Note that names can have null characters within the string as well as at
104   /// their end.  This always returns a non-null pointer.
105   const char *getNameStart() const;
106   /// getNameEnd - Return a pointer to the end of the name.
107   const char *getNameEnd() const { return getNameStart() + getNameLen(); }
108   
109   /// isName - Return true if this value has the name specified by the provided
110   /// nul terminated string.
111   bool isName(const char *N) const;
112   
113   /// getNameLen - Return the length of the string, correctly handling nul
114   /// characters embedded into them.
115   unsigned getNameLen() const;
116
117   /// getName()/getNameStr() - Return the name of the specified value, 
118   /// *constructing a string* to hold it.  Because these are guaranteed to
119   /// construct a string, they are very expensive and should be avoided.
120   std::string getName() const { return getNameStr(); }
121   std::string getNameStr() const;
122
123
124   void setName(const std::string &name);
125   void setName(const char *Name, unsigned NameLen);
126   void setName(const char *Name);  // Takes a null-terminated string.
127
128   
129   /// takeName - transfer the name from V to this value, setting V's name to
130   /// empty.  It is an error to call V->takeName(V). 
131   void takeName(Value *V);
132
133   /// replaceAllUsesWith - Go through the uses list for this definition and make
134   /// each use point to "V" instead of "this".  After this completes, 'this's
135   /// use list is guaranteed to be empty.
136   ///
137   void replaceAllUsesWith(Value *V);
138
139   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
140   // Only use when in type resolution situations!
141   void uncheckedReplaceAllUsesWith(Value *V);
142
143   //----------------------------------------------------------------------
144   // Methods for handling the chain of uses of this Value.
145   //
146   typedef value_use_iterator<User>       use_iterator;
147   typedef value_use_iterator<const User> use_const_iterator;
148
149   bool               use_empty() const { return UseList == 0; }
150   use_iterator       use_begin()       { return use_iterator(UseList); }
151   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
152   use_iterator       use_end()         { return use_iterator(0);   }
153   use_const_iterator use_end()   const { return use_const_iterator(0);   }
154   User              *use_back()        { return *use_begin(); }
155   const User        *use_back()  const { return *use_begin(); }
156
157   /// hasOneUse - Return true if there is exactly one user of this value.  This
158   /// is specialized because it is a common request and does not require
159   /// traversing the whole use list.
160   ///
161   bool hasOneUse() const {
162     use_const_iterator I = use_begin(), E = use_end();
163     if (I == E) return false;
164     return ++I == E;
165   }
166
167   /// hasNUses - Return true if this Value has exactly N users.
168   ///
169   bool hasNUses(unsigned N) const;
170
171   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
172   /// logically equivalent to getNumUses() >= N.
173   ///
174   bool hasNUsesOrMore(unsigned N) const;
175
176   bool isUsedInBasicBlock(const BasicBlock *BB) const;
177
178   /// getNumUses - This method computes the number of uses of this Value.  This
179   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
180   /// to check for specific values.
181   unsigned getNumUses() const;
182
183   /// addUse - This method should only be used by the Use class.
184   ///
185   void addUse(Use &U) { U.addToList(&UseList); }
186
187   /// An enumeration for keeping track of the concrete subclass of Value that
188   /// is actually instantiated. Values of this enumeration are kept in the 
189   /// Value classes SubclassID field. They are used for concrete type
190   /// identification.
191   enum ValueTy {
192     ArgumentVal,              // This is an instance of Argument
193     BasicBlockVal,            // This is an instance of BasicBlock
194     FunctionVal,              // This is an instance of Function
195     GlobalAliasVal,           // This is an instance of GlobalAlias
196     GlobalVariableVal,        // This is an instance of GlobalVariable
197     UndefValueVal,            // This is an instance of UndefValue
198     ConstantExprVal,          // This is an instance of ConstantExpr
199     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
200     ConstantIntVal,           // This is an instance of ConstantInt
201     ConstantFPVal,            // This is an instance of ConstantFP
202     ConstantArrayVal,         // This is an instance of ConstantArray
203     ConstantStructVal,        // This is an instance of ConstantStruct
204     ConstantVectorVal,        // This is an instance of ConstantVector
205     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
206     InlineAsmVal,             // This is an instance of InlineAsm
207     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
208     InstructionVal,           // This is an instance of Instruction
209     
210     // Markers:
211     ConstantFirstVal = FunctionVal,
212     ConstantLastVal  = ConstantPointerNullVal
213   };
214
215   /// getValueID - Return an ID for the concrete type of this object.  This is
216   /// used to implement the classof checks.  This should not be used for any
217   /// other purpose, as the values may change as LLVM evolves.  Also, note that
218   /// for instructions, the Instruction's opcode is added to InstructionVal. So
219   /// this means three things:
220   /// # there is no value with code InstructionVal (no opcode==0).
221   /// # there are more possible values for the value type than in ValueTy enum.
222   /// # the InstructionVal enumerator must be the highest valued enumerator in
223   ///   the ValueTy enum.
224   unsigned getValueID() const {
225     return SubclassID;
226   }
227
228   // Methods for support type inquiry through isa, cast, and dyn_cast:
229   static inline bool classof(const Value *) {
230     return true; // Values are always values.
231   }
232
233   /// getRawType - This should only be used to implement the vmcore library.
234   ///
235   const Type *getRawType() const { return VTy.getRawType(); }
236
237   /// stripPointerCasts - This method strips off any unneeded pointer
238   /// casts from the specified value, returning the original uncasted value.
239   /// Note that the returned value has pointer type if the specified value does.
240   Value *stripPointerCasts();
241   const Value *stripPointerCasts() const {
242     return const_cast<Value*>(this)->stripPointerCasts();
243   }
244
245   /// getUnderlyingObject - This method strips off any GEP address adjustments
246   /// and pointer casts from the specified value, returning the original object
247   /// being addressed.  Note that the returned value has pointer type if the
248   /// specified value does.
249   Value *getUnderlyingObject();
250   const Value *getUnderlyingObject() const {
251     return const_cast<Value*>(this)->getUnderlyingObject();
252   }
253   
254   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
255   /// return the value in the PHI node corresponding to PredBB.  If not, return
256   /// ourself.  This is useful if you want to know the value something has in a
257   /// predecessor block.
258   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
259
260   const Value *DoPHITranslation(const BasicBlock *CurBB,
261                                 const BasicBlock *PredBB) const{
262     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
263   }
264 };
265
266 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
267   V.print(OS);
268   return OS;
269 }
270 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
271   V.print(OS);
272   return OS;
273 }
274   
275 void Use::set(Value *V) {
276   if (Val) removeFromList();
277   Val = V;
278   if (V) V->addUse(*this);
279 }
280
281
282 // isa - Provide some specializations of isa so that we don't have to include
283 // the subtype header files to test to see if the value is a subclass...
284 //
285 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
286   return Val.getValueID() >= Value::ConstantFirstVal &&
287          Val.getValueID() <= Value::ConstantLastVal;
288 }
289 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
290   return Val.getValueID() == Value::ArgumentVal;
291 }
292 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
293   return Val.getValueID() == Value::InlineAsmVal;
294 }
295 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
296   return Val.getValueID() >= Value::InstructionVal;
297 }
298 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
299   return Val.getValueID() == Value::BasicBlockVal;
300 }
301 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
302   return Val.getValueID() == Value::FunctionVal;
303 }
304 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
305   return Val.getValueID() == Value::GlobalVariableVal;
306 }
307 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
308   return Val.getValueID() == Value::GlobalAliasVal;
309 }
310 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
311   return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
312          isa<GlobalAlias>(Val);
313 }
314   
315   
316 // Value* is only 4-byte aligned.
317 template<>
318 class PointerLikeTypeTraits<Value*> {
319   typedef Value* PT;
320 public:
321   static inline void *getAsVoidPointer(PT P) { return P; }
322   static inline PT getFromVoidPointer(void *P) {
323     return static_cast<PT>(P);
324   }
325   enum { NumLowBitsAvailable = 2 };
326 };
327
328 } // End llvm namespace
329
330 #endif