Merging the linear scan register allocator in trunk. It currently passes most tests...
[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 "Support/Annotation.h"
23 #include "Support/Casting.h"
24 #include <iostream>
25
26 namespace llvm {
27
28 class Type;
29 class Constant;
30 class Argument;
31 class Instruction;
32 class BasicBlock;
33 class GlobalValue;
34 class Function;
35 class GlobalVariable;
36 class SymbolTable;
37
38 //===----------------------------------------------------------------------===//
39 //                                 Value Class
40 //===----------------------------------------------------------------------===//
41
42 /// Value - The base class of all values computed by a program that may be used
43 /// as operands to other values.
44 ///
45 struct Value : public Annotable {         // Values are annotable
46   enum ValueTy {
47     TypeVal,                // This is an instance of Type
48     ConstantVal,            // This is an instance of Constant
49     ArgumentVal,            // This is an instance of Argument
50     InstructionVal,         // This is an instance of Instruction
51     BasicBlockVal,          // This is an instance of BasicBlock
52     FunctionVal,            // This is an instance of Function
53     GlobalVariableVal,      // This is an instance of GlobalVariable
54   };
55
56 private:
57   iplist<Use> Uses;
58   std::string Name;
59   PATypeHolder Ty;
60   ValueTy VTy;
61
62   void operator=(const Value &);     // Do not implement
63   Value(const Value &);              // Do not implement
64 public:
65   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
66   virtual ~Value();
67   
68   /// dump - Support for debugging, callable in GDB: V->dump()
69   //
70   virtual void dump() const;
71
72   /// print - Implement operator<< on Value...
73   ///
74   virtual void print(std::ostream &O) const = 0;
75   
76   /// All values are typed, get the type of this value.
77   ///
78   inline const Type *getType() const { return Ty; }
79   
80   // All values can potentially be named...
81   inline bool               hasName() const { return Name != ""; }
82   inline const std::string &getName() const { return Name; }
83
84   virtual void setName(const std::string &name, SymbolTable * = 0) {
85     Name = name;
86   }
87   
88   /// getValueType - Return the immediate subclass of this Value.
89   ///
90   inline ValueTy getValueType() const { return VTy; }
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 UseListIteratorWrapper      use_iterator;
106   typedef UseListConstIteratorWrapper use_const_iterator;
107
108   unsigned           use_size()  const { return Uses.size();  }
109   bool               use_empty() const { return Uses.empty(); }
110   use_iterator       use_begin()       { return Uses.begin(); }
111   use_const_iterator use_begin() const { return Uses.begin(); }
112   use_iterator       use_end()         { return Uses.end();   }
113   use_const_iterator use_end()   const { return Uses.end();   }
114   User             *use_back()         { return Uses.back().getUser(); }
115   const User       *use_back()  const  { return Uses.back().getUser(); }
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     iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
123     if (I == E) return false;
124     return ++I == E;
125   }
126
127   /// addUse/killUse - These two methods should only be used by the Use class.
128   ///
129   void addUse(Use &U)  { Uses.push_back(&U); }
130   void killUse(Use &U) { Uses.remove(&U); }
131 };
132
133 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
134   if (V == 0)
135     OS << "<null> value!\n";
136   else
137     V->print(OS);
138   return OS;
139 }
140
141 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
142   V.print(OS);
143   return OS;
144 }
145
146
147 inline User *UseListIteratorWrapper::operator*() const {
148   return Super::operator*().getUser();
149 }
150
151 inline const User *UseListConstIteratorWrapper::operator*() const {
152   return Super::operator*().getUser();
153 }
154
155
156 Use::Use(Value *v, User *user) : Val(v), U(user) {
157   if (Val) Val->addUse(*this);
158 }
159
160 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
161   if (Val) Val->addUse(*this);
162 }
163
164 Use::~Use() {
165   if (Val) Val->killUse(*this);
166 }
167
168 void Use::set(Value *V) { 
169   if (Val) Val->killUse(*this);
170   Val = V;
171   if (V) V->addUse(*this);
172 }
173
174
175 // isa - Provide some specializations of isa so that we don't have to include
176 // the subtype header files to test to see if the value is a subclass...
177 //
178 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
179   return Val.getValueType() == Value::TypeVal;
180 }
181 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
182   return Val.getValueType() == Value::ConstantVal; 
183 }
184 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
185   return Val.getValueType() == Value::ArgumentVal;
186 }
187 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
188   return Val.getValueType() == Value::InstructionVal;
189 }
190 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
191   return Val.getValueType() == Value::BasicBlockVal;
192 }
193 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
194   return Val.getValueType() == Value::FunctionVal;
195 }
196 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
197   return Val.getValueType() == Value::GlobalVariableVal;
198 }
199 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
200   return isa<GlobalVariable>(Val) || isa<Function>(Val);
201 }
202
203 } // End llvm namespace
204
205 #endif