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