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