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