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