LLVM API Documentation
00001 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the Value class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_VALUE_H 00015 #define LLVM_VALUE_H 00016 00017 #include "llvm/AbstractTypeUser.h" 00018 #include "llvm/Use.h" 00019 #include "llvm/Support/Casting.h" 00020 #include <iosfwd> 00021 #include <string> 00022 00023 namespace llvm { 00024 00025 class Constant; 00026 class Argument; 00027 class Instruction; 00028 class BasicBlock; 00029 class GlobalValue; 00030 class Function; 00031 class GlobalVariable; 00032 class GlobalAlias; 00033 class InlineAsm; 00034 class ValueSymbolTable; 00035 class TypeSymbolTable; 00036 template<typename ValueTy> class StringMapEntry; 00037 typedef StringMapEntry<Value*> ValueName; 00038 class raw_ostream; 00039 class AssemblyAnnotationWriter; 00040 00041 //===----------------------------------------------------------------------===// 00042 // Value Class 00043 //===----------------------------------------------------------------------===// 00044 00045 /// This is a very important LLVM class. It is the base class of all values 00046 /// computed by a program that may be used as operands to other values. Value is 00047 /// the super class of other important classes such as Instruction and Function. 00048 /// All Values have a Type. Type is not a subclass of Value. All types can have 00049 /// a name and they should belong to some Module. Setting the name on the Value 00050 /// automatically updates the module's symbol table. 00051 /// 00052 /// Every value has a "use list" that keeps track of which other Values are 00053 /// using this Value. 00054 /// @brief LLVM Value Representation 00055 class Value { 00056 const unsigned short SubclassID; // Subclass identifier (for isa/dyn_cast) 00057 protected: 00058 /// SubclassData - This member is defined by this class, but is not used for 00059 /// anything. Subclasses can use it to hold whatever state they find useful. 00060 /// This field is initialized to zero by the ctor. 00061 unsigned short SubclassData; 00062 private: 00063 PATypeHolder VTy; 00064 Use *UseList; 00065 00066 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name. 00067 friend class SymbolTable; // Allow SymbolTable to directly poke Name. 00068 ValueName *Name; 00069 00070 void operator=(const Value &); // Do not implement 00071 Value(const Value &); // Do not implement 00072 00073 public: 00074 Value(const Type *Ty, unsigned scid); 00075 virtual ~Value(); 00076 00077 /// dump - Support for debugging, callable in GDB: V->dump() 00078 // 00079 virtual void dump() const; 00080 00081 /// print - Implement operator<< on Value. 00082 /// 00083 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const; 00084 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const; 00085 00086 /// All values are typed, get the type of this value. 00087 /// 00088 inline const Type *getType() const { return VTy; } 00089 00090 // All values can potentially be named... 00091 inline bool hasName() const { return Name != 0; } 00092 ValueName *getValueName() const { return Name; } 00093 00094 /// getNameStart - Return a pointer to a null terminated string for this name. 00095 /// Note that names can have null characters within the string as well as at 00096 /// their end. This always returns a non-null pointer. 00097 const char *getNameStart() const; 00098 /// getNameEnd - Return a pointer to the end of the name. 00099 const char *getNameEnd() const { return getNameStart() + getNameLen(); } 00100 00101 /// isName - Return true if this value has the name specified by the provided 00102 /// nul terminated string. 00103 bool isName(const char *N) const; 00104 00105 /// getNameLen - Return the length of the string, correctly handling nul 00106 /// characters embedded into them. 00107 unsigned getNameLen() const; 00108 00109 /// getName()/getNameStr() - Return the name of the specified value, 00110 /// *constructing a string* to hold it. Because these are guaranteed to 00111 /// construct a string, they are very expensive and should be avoided. 00112 std::string getName() const { return getNameStr(); } 00113 std::string getNameStr() const; 00114 00115 00116 void setName(const std::string &name); 00117 void setName(const char *Name, unsigned NameLen); 00118 void setName(const char *Name); // Takes a null-terminated string. 00119 00120 00121 /// takeName - transfer the name from V to this value, setting V's name to 00122 /// empty. It is an error to call V->takeName(V). 00123 void takeName(Value *V); 00124 00125 /// replaceAllUsesWith - Go through the uses list for this definition and make 00126 /// each use point to "V" instead of "this". After this completes, 'this's 00127 /// use list is guaranteed to be empty. 00128 /// 00129 void replaceAllUsesWith(Value *V); 00130 00131 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous. 00132 // Only use when in type resolution situations! 00133 void uncheckedReplaceAllUsesWith(Value *V); 00134 00135 //---------------------------------------------------------------------- 00136 // Methods for handling the chain of uses of this Value. 00137 // 00138 typedef value_use_iterator<User> use_iterator; 00139 typedef value_use_iterator<const User> use_const_iterator; 00140 00141 bool use_empty() const { return UseList == 0; } 00142 use_iterator use_begin() { return use_iterator(UseList); } 00143 use_const_iterator use_begin() const { return use_const_iterator(UseList); } 00144 use_iterator use_end() { return use_iterator(0); } 00145 use_const_iterator use_end() const { return use_const_iterator(0); } 00146 User *use_back() { return *use_begin(); } 00147 const User *use_back() const { return *use_begin(); } 00148 00149 /// hasOneUse - Return true if there is exactly one user of this value. This 00150 /// is specialized because it is a common request and does not require 00151 /// traversing the whole use list. 00152 /// 00153 bool hasOneUse() const { 00154 use_const_iterator I = use_begin(), E = use_end(); 00155 if (I == E) return false; 00156 return ++I == E; 00157 } 00158 00159 /// hasNUses - Return true if this Value has exactly N users. 00160 /// 00161 bool hasNUses(unsigned N) const; 00162 00163 /// hasNUsesOrMore - Return true if this value has N users or more. This is 00164 /// logically equivalent to getNumUses() >= N. 00165 /// 00166 bool hasNUsesOrMore(unsigned N) const; 00167 00168 bool isUsedInBasicBlock(const BasicBlock *BB) const; 00169 00170 /// getNumUses - This method computes the number of uses of this Value. This 00171 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses 00172 /// to check for specific values. 00173 unsigned getNumUses() const; 00174 00175 /// addUse - This method should only be used by the Use class. 00176 /// 00177 void addUse(Use &U) { U.addToList(&UseList); } 00178 00179 /// An enumeration for keeping track of the concrete subclass of Value that 00180 /// is actually instantiated. Values of this enumeration are kept in the 00181 /// Value classes SubclassID field. They are used for concrete type 00182 /// identification. 00183 enum ValueTy { 00184 ArgumentVal, // This is an instance of Argument 00185 BasicBlockVal, // This is an instance of BasicBlock 00186 FunctionVal, // This is an instance of Function 00187 GlobalAliasVal, // This is an instance of GlobalAlias 00188 GlobalVariableVal, // This is an instance of GlobalVariable 00189 UndefValueVal, // This is an instance of UndefValue 00190 ConstantExprVal, // This is an instance of ConstantExpr 00191 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull 00192 ConstantIntVal, // This is an instance of ConstantInt 00193 ConstantFPVal, // This is an instance of ConstantFP 00194 ConstantArrayVal, // This is an instance of ConstantArray 00195 ConstantStructVal, // This is an instance of ConstantStruct 00196 ConstantVectorVal, // This is an instance of ConstantVector 00197 ConstantPointerNullVal, // This is an instance of ConstantPointerNull 00198 InlineAsmVal, // This is an instance of InlineAsm 00199 PseudoSourceValueVal, // This is an instance of PseudoSourceValue 00200 InstructionVal, // This is an instance of Instruction 00201 00202 // Markers: 00203 ConstantFirstVal = FunctionVal, 00204 ConstantLastVal = ConstantPointerNullVal 00205 }; 00206 00207 /// getValueID - Return an ID for the concrete type of this object. This is 00208 /// used to implement the classof checks. This should not be used for any 00209 /// other purpose, as the values may change as LLVM evolves. Also, note that 00210 /// for instructions, the Instruction's opcode is added to InstructionVal. So 00211 /// this means three things: 00212 /// # there is no value with code InstructionVal (no opcode==0). 00213 /// # there are more possible values for the value type than in ValueTy enum. 00214 /// # the InstructionVal enumerator must be the highest valued enumerator in 00215 /// the ValueTy enum. 00216 unsigned getValueID() const { 00217 return SubclassID; 00218 } 00219 00220 // Methods for support type inquiry through isa, cast, and dyn_cast: 00221 static inline bool classof(const Value *) { 00222 return true; // Values are always values. 00223 } 00224 00225 /// getRawType - This should only be used to implement the vmcore library. 00226 /// 00227 const Type *getRawType() const { return VTy.getRawType(); } 00228 00229 /// stripPointerCasts - This method strips off any unneeded pointer 00230 /// casts from the specified value, returning the original uncasted value. 00231 /// Note that the returned value has pointer type if the specified value does. 00232 Value *stripPointerCasts(); 00233 const Value *stripPointerCasts() const { 00234 return const_cast<Value*>(this)->stripPointerCasts(); 00235 } 00236 00237 /// getUnderlyingObject - This method strips off any GEP address adjustments 00238 /// and pointer casts from the specified value, returning the original object 00239 /// being addressed. Note that the returned value has pointer type if the 00240 /// specified value does. 00241 Value *getUnderlyingObject(); 00242 const Value *getUnderlyingObject() const { 00243 return const_cast<Value*>(this)->getUnderlyingObject(); 00244 } 00245 }; 00246 00247 inline std::ostream &operator<<(std::ostream &OS, const Value &V) { 00248 V.print(OS); 00249 return OS; 00250 } 00251 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { 00252 V.print(OS); 00253 return OS; 00254 } 00255 00256 void Use::init(Value *V, User *) { 00257 Val = V; 00258 if (V) V->addUse(*this); 00259 } 00260 00261 void Use::set(Value *V) { 00262 if (Val) removeFromList(); 00263 Val = V; 00264 if (V) V->addUse(*this); 00265 } 00266 00267 00268 // isa - Provide some specializations of isa so that we don't have to include 00269 // the subtype header files to test to see if the value is a subclass... 00270 // 00271 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 00272 return Val.getValueID() >= Value::ConstantFirstVal && 00273 Val.getValueID() <= Value::ConstantLastVal; 00274 } 00275 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 00276 return Val.getValueID() == Value::ArgumentVal; 00277 } 00278 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) { 00279 return Val.getValueID() == Value::InlineAsmVal; 00280 } 00281 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 00282 return Val.getValueID() >= Value::InstructionVal; 00283 } 00284 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 00285 return Val.getValueID() == Value::BasicBlockVal; 00286 } 00287 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 00288 return Val.getValueID() == Value::FunctionVal; 00289 } 00290 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 00291 return Val.getValueID() == Value::GlobalVariableVal; 00292 } 00293 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) { 00294 return Val.getValueID() == Value::GlobalAliasVal; 00295 } 00296 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 00297 return isa<GlobalVariable>(Val) || isa<Function>(Val) || isa<GlobalAlias>(Val); 00298 } 00299 00300 } // End llvm namespace 00301 00302 #endif