1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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...
13 // This file also defines the Use<> template for users of value.
15 //===----------------------------------------------------------------------===//
20 #include "llvm/AbstractTypeUser.h"
22 #include "Support/Casting.h"
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
41 /// Value - The base class of all values computed by a program that may be used
42 /// as operands to other values.
46 TypeVal, // This is an instance of Type
47 ConstantVal, // This is an instance of Constant
48 ArgumentVal, // This is an instance of Argument
49 InstructionVal, // This is an instance of Instruction
50 BasicBlockVal, // This is an instance of BasicBlock
51 FunctionVal, // This is an instance of Function
52 GlobalVariableVal, // This is an instance of GlobalVariable
61 void operator=(const Value &); // Do not implement
62 Value(const Value &); // Do not implement
64 Value(const Type *Ty, ValueTy vty, const std::string &name = "");
67 /// dump - Support for debugging, callable in GDB: V->dump()
69 virtual void dump() const;
71 /// print - Implement operator<< on Value...
73 virtual void print(std::ostream &O) const = 0;
75 /// All values are typed, get the type of this value.
77 inline const Type *getType() const { return Ty; }
79 // All values can potentially be named...
80 inline bool hasName() const { return !Name.empty(); }
81 inline const std::string &getName() const { return Name; }
83 virtual void setName(const std::string &name, SymbolTable * = 0) {
87 /// getValueType - Return the immediate subclass of this Value.
89 inline ValueTy getValueType() const { return VTy; }
91 /// replaceAllUsesWith - Go through the uses list for this definition and make
92 /// each use point to "V" instead of "this". After this completes, 'this's
93 /// use list is guaranteed to be empty.
95 void replaceAllUsesWith(Value *V);
97 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
98 // Only use when in type resolution situations!
99 void uncheckedReplaceAllUsesWith(Value *V);
101 //----------------------------------------------------------------------
102 // Methods for handling the vector of uses of this Value.
104 typedef UseListIteratorWrapper use_iterator;
105 typedef UseListConstIteratorWrapper use_const_iterator;
107 unsigned use_size() const { return Uses.size(); }
108 bool use_empty() const { return Uses.empty(); }
109 use_iterator use_begin() { return Uses.begin(); }
110 use_const_iterator use_begin() const { return Uses.begin(); }
111 use_iterator use_end() { return Uses.end(); }
112 use_const_iterator use_end() const { return Uses.end(); }
113 User *use_back() { return Uses.back().getUser(); }
114 const User *use_back() const { return Uses.back().getUser(); }
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.
120 bool hasOneUse() const {
121 iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
122 if (I == E) return false;
126 /// addUse/killUse - These two methods should only be used by the Use class.
128 void addUse(Use &U) { Uses.push_back(&U); }
129 void killUse(Use &U) { Uses.remove(&U); }
132 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
134 OS << "<null> value!\n";
140 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
146 inline User *UseListIteratorWrapper::operator*() const {
147 return Super::operator*().getUser();
150 inline const User *UseListConstIteratorWrapper::operator*() const {
151 return Super::operator*().getUser();
155 Use::Use(Value *v, User *user) : Val(v), U(user) {
156 if (Val) Val->addUse(*this);
159 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
160 if (Val) Val->addUse(*this);
164 if (Val) Val->killUse(*this);
167 void Use::set(Value *V) {
168 if (Val) Val->killUse(*this);
170 if (V) V->addUse(*this);
174 // isa - Provide some specializations of isa so that we don't have to include
175 // the subtype header files to test to see if the value is a subclass...
177 template <> inline bool isa_impl<Type, Value>(const Value &Val) {
178 return Val.getValueType() == Value::TypeVal;
180 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
181 return Val.getValueType() == Value::ConstantVal;
183 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
184 return Val.getValueType() == Value::ArgumentVal;
186 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
187 return Val.getValueType() == Value::InstructionVal;
189 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
190 return Val.getValueType() == Value::BasicBlockVal;
192 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
193 return Val.getValueType() == Value::FunctionVal;
195 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
196 return Val.getValueType() == Value::GlobalVariableVal;
198 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
199 return isa<GlobalVariable>(Val) || isa<Function>(Val);
202 } // End llvm namespace