add a method for comparing to see if a value has a specified name.
[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 // This file also defines the Use<> template for users of value.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_VALUE_H
16 #define LLVM_VALUE_H
17
18 #include "llvm/AbstractTypeUser.h"
19 #include "llvm/Use.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Streams.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 typedef StringMapEntry<Value*> ValueName;
39
40 //===----------------------------------------------------------------------===//
41 //                                 Value Class
42 //===----------------------------------------------------------------------===//
43
44 /// This is a very important LLVM class. It is the base class of all values 
45 /// computed by a program that may be used as operands to other values. Value is
46 /// the super class of other important classes such as Instruction and Function.
47 /// All Values have a Type. Type is not a subclass of Value. All types can have
48 /// a name and they should belong to some Module. Setting the name on the Value
49 /// automatically updates the module's symbol table.
50 ///
51 /// Every value has a "use list" that keeps track of which other Values are
52 /// using this Value.
53 /// @brief LLVM Value Representation
54 class Value {
55   const unsigned short SubclassID;   // Subclass identifier (for isa/dyn_cast)
56 protected:
57   /// SubclassData - This member is defined by this class, but is not used for
58   /// anything.  Subclasses can use it to hold whatever state they find useful.
59   /// This field is initialized to zero by the ctor.
60   unsigned short SubclassData;
61 private:
62   PATypeHolder Ty;
63   Use *UseList;
64
65   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
66   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
67   ValueName *Name;
68
69   void operator=(const Value &);     // Do not implement
70   Value(const Value &);              // Do not implement
71
72 public:
73   Value(const Type *Ty, unsigned scid);
74   virtual ~Value();
75
76   /// dump - Support for debugging, callable in GDB: V->dump()
77   //
78   virtual void dump() const;
79
80   /// print - Implement operator<< on Value...
81   ///
82   virtual void print(std::ostream &O) const = 0;
83   void print(std::ostream *O) const { if (O) print(*O); }
84
85   /// All values are typed, get the type of this value.
86   ///
87   inline const Type *getType() const { return Ty; }
88
89   // All values can potentially be named...
90   inline bool hasName() const { return Name != 0; }
91   ValueName *getValueName() const { return Name; }
92
93   /// getNameStart - Return a pointer to a null terminated string for this name.
94   /// Note that names can have null characters within the string as well as at
95   /// their end.  This always returns a non-null pointer.
96   const char *getNameStart() const;
97   
98   /// isName - Return true if this value has the name specified by the provided
99   /// nul terminated string.
100   bool isName(const char *N) const;
101   
102   /// getNameLen - Return the length of the string, correctly handling nul
103   /// characters embedded into them.
104   unsigned getNameLen() const;
105
106   /// getName()/getNameStr() - Return the name of the specified value, 
107   /// *constructing a string* to hold it.  Because these are guaranteed to
108   /// construct a string, they are very expensive and should be avoided.
109   std::string getName() const { return getNameStr(); }
110   std::string getNameStr() const;
111
112
113   void setName(const std::string &name);
114   void setName(const char *Name, unsigned NameLen);
115   void setName(const char *Name);  // Takes a null-terminated string.
116
117   
118   /// takeName - transfer the name from V to this value, setting V's name to
119   /// empty.  It is an error to call V->takeName(V). 
120   void takeName(Value *V);
121
122   /// replaceAllUsesWith - Go through the uses list for this definition and make
123   /// each use point to "V" instead of "this".  After this completes, 'this's
124   /// use list is guaranteed to be empty.
125   ///
126   void replaceAllUsesWith(Value *V);
127
128   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
129   // Only use when in type resolution situations!
130   void uncheckedReplaceAllUsesWith(Value *V);
131
132   //----------------------------------------------------------------------
133   // Methods for handling the vector of uses of this Value.
134   //
135   typedef value_use_iterator<User>       use_iterator;
136   typedef value_use_iterator<const User> use_const_iterator;
137
138   bool               use_empty() const { return UseList == 0; }
139   use_iterator       use_begin()       { return use_iterator(UseList); }
140   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
141   use_iterator       use_end()         { return use_iterator(0);   }
142   use_const_iterator use_end()   const { return use_const_iterator(0);   }
143   User              *use_back()        { return *use_begin(); }
144   const User        *use_back() const  { return *use_begin(); }
145
146   /// hasOneUse - Return true if there is exactly one user of this value.  This
147   /// is specialized because it is a common request and does not require
148   /// traversing the whole use list.
149   ///
150   bool hasOneUse() const {
151     use_const_iterator I = use_begin(), E = use_end();
152     if (I == E) return false;
153     return ++I == E;
154   }
155
156   /// hasNUses - Return true if this Value has exactly N users.
157   ///
158   bool hasNUses(unsigned N) const;
159
160   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
161   /// logically equivalent to getNumUses() >= N.
162   ///
163   bool hasNUsesOrMore(unsigned N) const;
164
165   /// getNumUses - This method computes the number of uses of this Value.  This
166   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
167   /// to check for specific values.
168   unsigned getNumUses() const;
169
170   /// addUse - This method should only be used by the Use class.
171   ///
172   void addUse(Use &U) { U.addToList(&UseList); }
173
174   /// An enumeration for keeping track of the concrete subclass of Value that
175   /// is actually instantiated. Values of this enumeration are kept in the 
176   /// Value classes SubclassID field. They are used for concrete type
177   /// identification.
178   enum ValueTy {
179     ArgumentVal,              // This is an instance of Argument
180     BasicBlockVal,            // This is an instance of BasicBlock
181     FunctionVal,              // This is an instance of Function
182     GlobalAliasVal,           // This is an instance of GlobalAlias
183     GlobalVariableVal,        // This is an instance of GlobalVariable
184     UndefValueVal,            // This is an instance of UndefValue
185     ConstantExprVal,          // This is an instance of ConstantExpr
186     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
187     ConstantIntVal,           // This is an instance of ConstantInt
188     ConstantFPVal,            // This is an instance of ConstantFP
189     ConstantArrayVal,         // This is an instance of ConstantArray
190     ConstantStructVal,        // This is an instance of ConstantStruct
191     ConstantVectorVal,        // This is an instance of ConstantVector
192     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
193     InlineAsmVal,             // This is an instance of InlineAsm
194     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
195     InstructionVal,           // This is an instance of Instruction
196     
197     // Markers:
198     ConstantFirstVal = FunctionVal,
199     ConstantLastVal  = ConstantPointerNullVal
200   };
201
202   /// getValueID - Return an ID for the concrete type of this object.  This is
203   /// used to implement the classof checks.  This should not be used for any
204   /// other purpose, as the values may change as LLVM evolves.  Also, note that
205   /// for instructions, the Instruction's opcode is added to InstructionVal. So
206   /// this means three things:
207   /// # there is no value with code InstructionVal (no opcode==0).
208   /// # there are more possible values for the value type than in ValueTy enum.
209   /// # the InstructionVal enumerator must be the highest valued enumerator in
210   ///   the ValueTy enum.
211   unsigned getValueID() const {
212     return SubclassID;
213   }
214
215   // Methods for support type inquiry through isa, cast, and dyn_cast:
216   static inline bool classof(const Value *) {
217     return true; // Values are always values.
218   }
219
220   /// getRawType - This should only be used to implement the vmcore library.
221   ///
222   const Type *getRawType() const { return Ty.getRawType(); }
223 };
224
225 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
226   V.print(OS);
227   return OS;
228 }
229
230 void Use::init(Value *v, User *user) {
231   Val = v;
232   U = user;
233   if (Val) Val->addUse(*this);
234 }
235
236 void Use::set(Value *V) {
237   if (Val) removeFromList();
238   Val = V;
239   if (V) V->addUse(*this);
240 }
241
242
243 // isa - Provide some specializations of isa so that we don't have to include
244 // the subtype header files to test to see if the value is a subclass...
245 //
246 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
247   return Val.getValueID() >= Value::ConstantFirstVal &&
248          Val.getValueID() <= Value::ConstantLastVal;
249 }
250 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
251   return Val.getValueID() == Value::ArgumentVal;
252 }
253 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
254   return Val.getValueID() == Value::InlineAsmVal;
255 }
256 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
257   return Val.getValueID() >= Value::InstructionVal;
258 }
259 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
260   return Val.getValueID() == Value::BasicBlockVal;
261 }
262 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
263   return Val.getValueID() == Value::FunctionVal;
264 }
265 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
266   return Val.getValueID() == Value::GlobalVariableVal;
267 }
268 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
269   return Val.getValueID() == Value::GlobalAliasVal;
270 }
271 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
272   return isa<GlobalVariable>(Val) || isa<Function>(Val) || isa<GlobalAlias>(Val);
273 }
274
275 } // End llvm namespace
276
277 #endif