LLVM API Documentation
00001 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the very important Value class. This is subclassed by a 00011 // bunch of other important classes, like Instruction, Function, Type, etc... 00012 // 00013 // This file also defines the Use<> template for users of value. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #ifndef LLVM_VALUE_H 00018 #define LLVM_VALUE_H 00019 00020 #include "llvm/AbstractTypeUser.h" 00021 #include "llvm/Use.h" 00022 #include "llvm/Support/Casting.h" 00023 #include <string> 00024 00025 namespace llvm { 00026 00027 class Constant; 00028 class Argument; 00029 class Instruction; 00030 class BasicBlock; 00031 class GlobalValue; 00032 class Function; 00033 class GlobalVariable; 00034 class SymbolTable; 00035 00036 //===----------------------------------------------------------------------===// 00037 // Value Class 00038 //===----------------------------------------------------------------------===// 00039 00040 /// Value - The base class of all values computed by a program that may be used 00041 /// as operands to other values. 00042 /// 00043 class Value { 00044 private: 00045 unsigned SubclassID; // Subclass identifier (for isa/dyn_cast) 00046 PATypeHolder Ty; 00047 iplist<Use> Uses; 00048 std::string Name; 00049 00050 void operator=(const Value &); // Do not implement 00051 Value(const Value &); // Do not implement 00052 00053 public: 00054 Value(const Type *Ty, unsigned scid, const std::string &name = ""); 00055 virtual ~Value(); 00056 00057 /// dump - Support for debugging, callable in GDB: V->dump() 00058 // 00059 virtual void dump() const; 00060 00061 /// print - Implement operator<< on Value... 00062 /// 00063 virtual void print(std::ostream &O) const = 0; 00064 00065 /// All values are typed, get the type of this value. 00066 /// 00067 inline const Type *getType() const { return Ty; } 00068 00069 // All values can potentially be named... 00070 inline bool hasName() const { return !Name.empty(); } 00071 inline const std::string &getName() const { return Name; } 00072 00073 virtual void setName(const std::string &name, SymbolTable * = 0) { 00074 Name = name; 00075 } 00076 00077 /// replaceAllUsesWith - Go through the uses list for this definition and make 00078 /// each use point to "V" instead of "this". After this completes, 'this's 00079 /// use list is guaranteed to be empty. 00080 /// 00081 void replaceAllUsesWith(Value *V); 00082 00083 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous. 00084 // Only use when in type resolution situations! 00085 void uncheckedReplaceAllUsesWith(Value *V); 00086 00087 //---------------------------------------------------------------------- 00088 // Methods for handling the vector of uses of this Value. 00089 // 00090 typedef UseListIteratorWrapper use_iterator; 00091 typedef UseListConstIteratorWrapper use_const_iterator; 00092 00093 unsigned use_size() const { return Uses.size(); } 00094 bool use_empty() const { return Uses.empty(); } 00095 use_iterator use_begin() { return Uses.begin(); } 00096 use_const_iterator use_begin() const { return Uses.begin(); } 00097 use_iterator use_end() { return Uses.end(); } 00098 use_const_iterator use_end() const { return Uses.end(); } 00099 User *use_back() { return Uses.back().getUser(); } 00100 const User *use_back() const { return Uses.back().getUser(); } 00101 00102 /// hasOneUse - Return true if there is exactly one user of this value. This 00103 /// is specialized because it is a common request and does not require 00104 /// traversing the whole use list. 00105 /// 00106 bool hasOneUse() const { 00107 iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end(); 00108 if (I == E) return false; 00109 return ++I == E; 00110 } 00111 00112 /// addUse/killUse - These two methods should only be used by the Use class. 00113 /// 00114 void addUse(Use &U) { Uses.push_back(&U); } 00115 void killUse(Use &U) { Uses.remove(&U); } 00116 00117 /// getValueType - Return an ID for the concrete type of this object. This is 00118 /// used to implement the classof checks. This should not be used for any 00119 /// other purpose, as the values may change as LLVM evolves. Also, note that 00120 /// starting with the InstructionVal value, the value stored is actually the 00121 /// Instruction opcode, so there are more than just these values possible here 00122 /// (and Instruction must be last). 00123 /// 00124 enum ValueTy { 00125 ArgumentVal, // This is an instance of Argument 00126 BasicBlockVal, // This is an instance of BasicBlock 00127 FunctionVal, // This is an instance of Function 00128 GlobalVariableVal, // This is an instance of GlobalVariable 00129 UndefValueVal, // This is an instance of UndefValue 00130 ConstantExprVal, // This is an instance of ConstantExpr 00131 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull 00132 SimpleConstantVal, // This is some other type of Constant 00133 InstructionVal, // This is an instance of Instruction 00134 ValueListVal // This is for bcreader, a special ValTy 00135 }; 00136 unsigned getValueType() const { 00137 return SubclassID; 00138 } 00139 00140 // Methods for support type inquiry through isa, cast, and dyn_cast: 00141 static inline bool classof(const Value *V) { 00142 return true; // Values are always values. 00143 } 00144 00145 /// getRawType - This should only be used to implement the vmcore library. 00146 /// 00147 const Type *getRawType() const { return Ty.getRawType(); } 00148 00149 private: 00150 /// FIXME: this is a gross hack, needed by another gross hack. Eliminate! 00151 void setValueType(unsigned VT) { SubclassID = VT; } 00152 friend class Instruction; 00153 }; 00154 00155 inline std::ostream &operator<<(std::ostream &OS, const Value &V) { 00156 V.print(OS); 00157 return OS; 00158 } 00159 00160 00161 inline User *UseListIteratorWrapper::operator*() const { 00162 return Super::operator*().getUser(); 00163 } 00164 00165 inline const User *UseListConstIteratorWrapper::operator*() const { 00166 return Super::operator*().getUser(); 00167 } 00168 00169 00170 Use::Use(Value *v, User *user) : Val(v), U(user) { 00171 if (Val) Val->addUse(*this); 00172 } 00173 00174 Use::Use(const Use &u) : Val(u.Val), U(u.U) { 00175 if (Val) Val->addUse(*this); 00176 } 00177 00178 Use::~Use() { 00179 if (Val) Val->killUse(*this); 00180 } 00181 00182 void Use::set(Value *V) { 00183 if (Val) Val->killUse(*this); 00184 Val = V; 00185 if (V) V->addUse(*this); 00186 } 00187 00188 00189 // isa - Provide some specializations of isa so that we don't have to include 00190 // the subtype header files to test to see if the value is a subclass... 00191 // 00192 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 00193 return Val.getValueType() == Value::SimpleConstantVal || 00194 Val.getValueType() == Value::FunctionVal || 00195 Val.getValueType() == Value::GlobalVariableVal || 00196 Val.getValueType() == Value::ConstantExprVal || 00197 Val.getValueType() == Value::ConstantAggregateZeroVal || 00198 Val.getValueType() == Value::UndefValueVal; 00199 } 00200 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 00201 return Val.getValueType() == Value::ArgumentVal; 00202 } 00203 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 00204 return Val.getValueType() >= Value::InstructionVal; 00205 } 00206 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 00207 return Val.getValueType() == Value::BasicBlockVal; 00208 } 00209 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 00210 return Val.getValueType() == Value::FunctionVal; 00211 } 00212 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 00213 return Val.getValueType() == Value::GlobalVariableVal; 00214 } 00215 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 00216 return isa<GlobalVariable>(Val) || isa<Function>(Val); 00217 } 00218 00219 } // End llvm namespace 00220 00221 #endif