Added a temporary hack to get the llvm-streams to work for future checkins.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 InlineAsm;
34 class SymbolTable;
35
36 //===----------------------------------------------------------------------===//
37 //                                 Value Class
38 //===----------------------------------------------------------------------===//
39
40 /// This is a very important LLVM class. It is the base class of all values 
41 /// computed by a program that may be used as operands to other values. Value is
42 /// the super class of other important classes such as Instruction and Function.
43 /// All Values have a Type. Type is not a subclass of Value. All types can have
44 /// a name and they should belong to some Module. Setting the name on the Value
45 /// automatically update's the module's symbol table.
46 ///
47 /// Every value has a "use list" that keeps track of which other Values are
48 /// using this Value.
49 /// @brief LLVM Value Representation
50 class Value {
51   unsigned short SubclassID;         // Subclass identifier (for isa/dyn_cast)
52 protected:
53   /// SubclassData - This member is defined by this class, but is not used for
54   /// anything.  Subclasses can use it to hold whatever state they find useful.
55   /// This field is initialized to zero by the ctor.
56   unsigned short SubclassData;
57 private:
58   PATypeHolder Ty;
59   Use *UseList;
60
61   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
62   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
63   std::string Name;
64
65   void operator=(const Value &);     // Do not implement
66   Value(const Value &);              // Do not implement
67
68 public:
69   Value(const Type *Ty, unsigned scid, const std::string &name = "");
70   virtual ~Value();
71
72   /// dump - Support for debugging, callable in GDB: V->dump()
73   //
74   virtual void dump() const;
75
76   /// print - Implement operator<< on Value...
77   ///
78   void print(llvm_ostream &O) const {
79     if (O.stream()) print(*O.stream());
80   }
81   virtual void print(std::ostream &O) const = 0;
82
83   /// All values are typed, get the type of this value.
84   ///
85   inline const Type *getType() const { return Ty; }
86
87   // All values can potentially be named...
88   inline bool               hasName() const { return !Name.empty(); }
89   inline const std::string &getName() const { return Name; }
90
91   void setName(const std::string &name);
92
93   /// replaceAllUsesWith - Go through the uses list for this definition and make
94   /// each use point to "V" instead of "this".  After this completes, 'this's
95   /// use list is guaranteed to be empty.
96   ///
97   void replaceAllUsesWith(Value *V);
98
99   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
100   // Only use when in type resolution situations!
101   void uncheckedReplaceAllUsesWith(Value *V);
102
103   //----------------------------------------------------------------------
104   // Methods for handling the vector of uses of this Value.
105   //
106   typedef value_use_iterator<User>       use_iterator;
107   typedef value_use_iterator<const User> use_const_iterator;
108
109   bool               use_empty() const { return UseList == 0; }
110   use_iterator       use_begin()       { return use_iterator(UseList); }
111   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
112   use_iterator       use_end()         { return use_iterator(0);   }
113   use_const_iterator use_end()   const { return use_const_iterator(0);   }
114   User              *use_back()        { return *use_begin(); }
115   const User        *use_back() const  { return *use_begin(); }
116
117   /// hasOneUse - Return true if there is exactly one user of this value.  This
118   /// is specialized because it is a common request and does not require
119   /// traversing the whole use list.
120   ///
121   bool hasOneUse() const {
122     use_const_iterator I = use_begin(), E = use_end();
123     if (I == E) return false;
124     return ++I == E;
125   }
126
127   /// hasNUses - Return true if this Value has exactly N users.
128   ///
129   bool hasNUses(unsigned N) const;
130
131   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
132   /// logically equivalent to getNumUses() >= N.
133   ///
134   bool hasNUsesOrMore(unsigned N) const;
135
136   /// getNumUses - This method computes the number of uses of this Value.  This
137   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
138   /// to check for specific values.
139   unsigned getNumUses() const;
140
141   /// addUse/killUse - These two methods should only be used by the Use class.
142   ///
143   void addUse(Use &U) { U.addToList(&UseList); }
144
145   /// An enumeration for keeping track of the concrete subclass of Value that
146   /// is actually instantiated. Values of this enumeration are kept in the 
147   /// Value classes SubclassID field. They are used for concrete type
148   /// identification.
149   enum ValueTy {
150     ArgumentVal,              // This is an instance of Argument
151     BasicBlockVal,            // This is an instance of BasicBlock
152     FunctionVal,              // This is an instance of Function
153     GlobalVariableVal,        // This is an instance of GlobalVariable
154     UndefValueVal,            // This is an instance of UndefValue
155     ConstantExprVal,          // This is an instance of ConstantExpr
156     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
157     ConstantBoolVal,          // This is an instance of ConstantBool
158     ConstantIntVal,           // This is an instance of ConstantInt
159     ConstantFPVal,            // This is an instance of ConstantFP
160     ConstantArrayVal,         // This is an instance of ConstantArray
161     ConstantStructVal,        // This is an instance of ConstantStruct
162     ConstantPackedVal,        // This is an instance of ConstantPacked
163     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
164     InlineAsmVal,             // This is an instance of InlineAsm
165     InstructionVal,           // This is an instance of Instruction
166     
167     // Markers:
168     ConstantFirstVal = FunctionVal,
169     ConstantLastVal  = ConstantPointerNullVal
170   };
171
172   /// getValueType - Return an ID for the concrete type of this object.  This is
173   /// used to implement the classof checks.  This should not be used for any
174   /// other purpose, as the values may change as LLVM evolves.  Also, note that
175   /// starting with the InstructionVal value, the value stored is actually the
176   /// Instruction opcode, so there are more than just these values possible here
177   /// (and Instruction must be last).
178   ///
179   unsigned getValueType() const {
180     return SubclassID;
181   }
182
183   // Methods for support type inquiry through isa, cast, and dyn_cast:
184   static inline bool classof(const Value *) {
185     return true; // Values are always values.
186   }
187
188   /// getRawType - This should only be used to implement the vmcore library.
189   ///
190   const Type *getRawType() const { return Ty.getRawType(); }
191
192 private:
193   /// FIXME: this is a gross hack, needed by another gross hack.  Eliminate!
194   void setValueType(unsigned short VT) { SubclassID = VT; }
195   friend class Instruction;
196 };
197
198 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
199   V.print(OS);
200   return OS;
201 }
202
203 void Use::init(Value *v, User *user) {
204   Val = v;
205   U = user;
206   if (Val) Val->addUse(*this);
207 }
208
209 Use::~Use() {
210   if (Val) removeFromList();
211 }
212
213 void Use::set(Value *V) {
214   if (Val) removeFromList();
215   Val = V;
216   if (V) V->addUse(*this);
217 }
218
219
220 // isa - Provide some specializations of isa so that we don't have to include
221 // the subtype header files to test to see if the value is a subclass...
222 //
223 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
224   return Val.getValueType() >= Value::ConstantFirstVal &&
225          Val.getValueType() <= Value::ConstantLastVal;
226 }
227 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
228   return Val.getValueType() == Value::ArgumentVal;
229 }
230 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
231   return Val.getValueType() == Value::InlineAsmVal;
232 }
233 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
234   return Val.getValueType() >= Value::InstructionVal;
235 }
236 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
237   return Val.getValueType() == Value::BasicBlockVal;
238 }
239 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
240   return Val.getValueType() == Value::FunctionVal;
241 }
242 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
243   return Val.getValueType() == Value::GlobalVariableVal;
244 }
245 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
246   return isa<GlobalVariable>(Val) || isa<Function>(Val);
247 }
248
249 } // End llvm namespace
250
251 #endif