For PR1195:
[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 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 update's 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   std::string getName() const;
91   ValueName *getValueName() const { return Name; }
92
93   void setName(const std::string &name);
94   void setName(const char *Name, unsigned NameLen);
95   void setName(const char *Name);  // Takes a null-terminated string.
96
97   
98   /// takeName - transfer the name from V to this value, setting V's name to
99   /// empty.  It is an error to call V->takeName(V). 
100   void takeName(Value *V);
101
102   /// replaceAllUsesWith - Go through the uses list for this definition and make
103   /// each use point to "V" instead of "this".  After this completes, 'this's
104   /// use list is guaranteed to be empty.
105   ///
106   void replaceAllUsesWith(Value *V);
107
108   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
109   // Only use when in type resolution situations!
110   void uncheckedReplaceAllUsesWith(Value *V);
111
112   //----------------------------------------------------------------------
113   // Methods for handling the vector of uses of this Value.
114   //
115   typedef value_use_iterator<User>       use_iterator;
116   typedef value_use_iterator<const User> use_const_iterator;
117
118   bool               use_empty() const { return UseList == 0; }
119   use_iterator       use_begin()       { return use_iterator(UseList); }
120   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
121   use_iterator       use_end()         { return use_iterator(0);   }
122   use_const_iterator use_end()   const { return use_const_iterator(0);   }
123   User              *use_back()        { return *use_begin(); }
124   const User        *use_back() const  { return *use_begin(); }
125
126   /// hasOneUse - Return true if there is exactly one user of this value.  This
127   /// is specialized because it is a common request and does not require
128   /// traversing the whole use list.
129   ///
130   bool hasOneUse() const {
131     use_const_iterator I = use_begin(), E = use_end();
132     if (I == E) return false;
133     return ++I == E;
134   }
135
136   /// hasNUses - Return true if this Value has exactly N users.
137   ///
138   bool hasNUses(unsigned N) const;
139
140   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
141   /// logically equivalent to getNumUses() >= N.
142   ///
143   bool hasNUsesOrMore(unsigned N) const;
144
145   /// getNumUses - This method computes the number of uses of this Value.  This
146   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
147   /// to check for specific values.
148   unsigned getNumUses() const;
149
150   /// addUse/killUse - These two methods should only be used by the Use class.
151   ///
152   void addUse(Use &U) { U.addToList(&UseList); }
153
154   /// An enumeration for keeping track of the concrete subclass of Value that
155   /// is actually instantiated. Values of this enumeration are kept in the 
156   /// Value classes SubclassID field. They are used for concrete type
157   /// identification.
158   enum ValueTy {
159     ArgumentVal,              // This is an instance of Argument
160     BasicBlockVal,            // This is an instance of BasicBlock
161     FunctionVal,              // This is an instance of Function
162     GlobalVariableVal,        // This is an instance of GlobalVariable
163     UndefValueVal,            // This is an instance of UndefValue
164     ConstantExprVal,          // This is an instance of ConstantExpr
165     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
166     ConstantIntVal,           // This is an instance of ConstantInt
167     ConstantFPVal,            // This is an instance of ConstantFP
168     ConstantArrayVal,         // This is an instance of ConstantArray
169     ConstantStructVal,        // This is an instance of ConstantStruct
170     ConstantVectorVal,        // This is an instance of ConstantPacked
171     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
172     InlineAsmVal,             // This is an instance of InlineAsm
173     InstructionVal,           // This is an instance of Instruction
174     
175     // Markers:
176     ConstantFirstVal = FunctionVal,
177     ConstantLastVal  = ConstantPointerNullVal
178   };
179
180   /// getValueType - Return an ID for the concrete type of this object.  This is
181   /// used to implement the classof checks.  This should not be used for any
182   /// other purpose, as the values may change as LLVM evolves.  Also, note that
183   /// starting with the InstructionVal value, the value stored is actually the
184   /// Instruction opcode, so there are more than just these values possible here
185   /// (and Instruction must be last).
186   ///
187   unsigned getValueType() const {
188     return SubclassID;
189   }
190
191   // Methods for support type inquiry through isa, cast, and dyn_cast:
192   static inline bool classof(const Value *) {
193     return true; // Values are always values.
194   }
195
196   /// getRawType - This should only be used to implement the vmcore library.
197   ///
198   const Type *getRawType() const { return Ty.getRawType(); }
199 };
200
201 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
202   V.print(OS);
203   return OS;
204 }
205
206 void Use::init(Value *v, User *user) {
207   Val = v;
208   U = user;
209   if (Val) Val->addUse(*this);
210 }
211
212 Use::~Use() {
213   if (Val) removeFromList();
214 }
215
216 void Use::set(Value *V) {
217   if (Val) removeFromList();
218   Val = V;
219   if (V) V->addUse(*this);
220 }
221
222
223 // isa - Provide some specializations of isa so that we don't have to include
224 // the subtype header files to test to see if the value is a subclass...
225 //
226 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
227   return Val.getValueType() >= Value::ConstantFirstVal &&
228          Val.getValueType() <= Value::ConstantLastVal;
229 }
230 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
231   return Val.getValueType() == Value::ArgumentVal;
232 }
233 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
234   return Val.getValueType() == Value::InlineAsmVal;
235 }
236 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
237   return Val.getValueType() >= Value::InstructionVal;
238 }
239 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
240   return Val.getValueType() == Value::BasicBlockVal;
241 }
242 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
243   return Val.getValueType() == Value::FunctionVal;
244 }
245 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
246   return Val.getValueType() == Value::GlobalVariableVal;
247 }
248 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
249   return isa<GlobalVariable>(Val) || isa<Function>(Val);
250 }
251
252 } // End llvm namespace
253
254 #endif