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