Value.h revision e6c42dd6d3cc1e0a8a11224a11bcfbd8c60c6fff
1//===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the Value class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_VALUE_H 15#define LLVM_VALUE_H 16 17#include "llvm/AbstractTypeUser.h" 18#include "llvm/Use.h" 19#include "llvm/ADT/StringRef.h" 20#include "llvm/Support/Casting.h" 21#include <iosfwd> 22#include <string> 23 24namespace llvm { 25 26class Constant; 27class Argument; 28class Instruction; 29class BasicBlock; 30class GlobalValue; 31class Function; 32class GlobalVariable; 33class GlobalAlias; 34class InlineAsm; 35class ValueSymbolTable; 36class TypeSymbolTable; 37template<typename ValueTy> class StringMapEntry; 38template <typename ValueTy = Value> 39class AssertingVH; 40typedef StringMapEntry<Value*> ValueName; 41class raw_ostream; 42class AssemblyAnnotationWriter; 43class ValueHandleBase; 44class LLVMContext; 45 46//===----------------------------------------------------------------------===// 47// Value Class 48//===----------------------------------------------------------------------===// 49 50/// This is a very important LLVM class. It is the base class of all values 51/// computed by a program that may be used as operands to other values. Value is 52/// the super class of other important classes such as Instruction and Function. 53/// All Values have a Type. Type is not a subclass of Value. All types can have 54/// a name and they should belong to some Module. Setting the name on the Value 55/// automatically updates the module's symbol table. 56/// 57/// Every value has a "use list" that keeps track of which other Values are 58/// using this Value. A Value can also have an arbitrary number of ValueHandle 59/// objects that watch it and listen to RAUW and Destroy events see 60/// llvm/Support/ValueHandle.h for details. 61/// 62/// @brief LLVM Value Representation 63class Value { 64 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) 65 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? 66protected: 67 /// SubclassOptionalData - This member is similar to SubclassData, however it 68 /// is for holding information which may be used to aid optimization, but 69 /// which may be cleared to zero without affecting conservative 70 /// interpretation. 71 unsigned char SubclassOptionalData : 7; 72 73 /// SubclassData - This member is defined by this class, but is not used for 74 /// anything. Subclasses can use it to hold whatever state they find useful. 75 /// This field is initialized to zero by the ctor. 76 unsigned short SubclassData; 77private: 78 PATypeHolder VTy; 79 Use *UseList; 80 81 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name. 82 friend class SymbolTable; // Allow SymbolTable to directly poke Name. 83 friend class ValueHandleBase; 84 ValueName *Name; 85 86 void operator=(const Value &); // Do not implement 87 Value(const Value &); // Do not implement 88 89public: 90 Value(const Type *Ty, unsigned scid); 91 virtual ~Value(); 92 93 /// dump - Support for debugging, callable in GDB: V->dump() 94 // 95 virtual void dump() const; 96 97 /// print - Implement operator<< on Value. 98 /// 99 void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const; 100 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const; 101 102 /// All values are typed, get the type of this value. 103 /// 104 inline const Type *getType() const { return VTy; } 105 106 /// All values hold a context through their type. 107 LLVMContext &getContext() const; 108 109 // All values can potentially be named... 110 inline bool hasName() const { return Name != 0; } 111 ValueName *getValueName() const { return Name; } 112 113 /// getNameStart - Return a pointer to a null terminated string for this name. 114 /// Note that names can have null characters within the string as well as at 115 /// their end. This always returns a non-null pointer. 116 const char *getNameStart() const; 117 /// getNameEnd - Return a pointer to the end of the name. 118 const char *getNameEnd() const { return getNameStart() + getNameLen(); } 119 120 /// isName - Return true if this value has the name specified by the provided 121 /// nul terminated string. 122 bool isName(const char *N) const; 123 124 /// getNameLen - Return the length of the string, correctly handling nul 125 /// characters embedded into them. 126 unsigned getNameLen() const; 127 128 /// getName()/getNameStr() - Return the name of the specified value, 129 /// *constructing a string* to hold it. Because these are guaranteed to 130 /// construct a string, they are very expensive and should be avoided. 131 std::string getName() const { return getNameStr(); } 132 std::string getNameStr() const; 133 StringRef getNameRef() const; 134 135 void setName(const std::string &name); 136 void setName(const char *Name, unsigned NameLen); 137 void setName(const char *Name); // Takes a null-terminated string. 138 139 140 /// takeName - transfer the name from V to this value, setting V's name to 141 /// empty. It is an error to call V->takeName(V). 142 void takeName(Value *V); 143 144 /// replaceAllUsesWith - Go through the uses list for this definition and make 145 /// each use point to "V" instead of "this". After this completes, 'this's 146 /// use list is guaranteed to be empty. 147 /// 148 void replaceAllUsesWith(Value *V); 149 150 // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous. 151 // Only use when in type resolution situations! 152 void uncheckedReplaceAllUsesWith(Value *V); 153 154 /// clearOptionalData - Clear any optional optimization data from this Value. 155 /// Transformation passes must call this method whenever changing the IR 156 /// in a way that would affect the values produced by this Value, unless 157 /// it takes special care to ensure correctness in some other way. 158 void clearOptionalData() { SubclassOptionalData = 0; } 159 160 //---------------------------------------------------------------------- 161 // Methods for handling the chain of uses of this Value. 162 // 163 typedef value_use_iterator<User> use_iterator; 164 typedef value_use_iterator<const User> use_const_iterator; 165 166 bool use_empty() const { return UseList == 0; } 167 use_iterator use_begin() { return use_iterator(UseList); } 168 use_const_iterator use_begin() const { return use_const_iterator(UseList); } 169 use_iterator use_end() { return use_iterator(0); } 170 use_const_iterator use_end() const { return use_const_iterator(0); } 171 User *use_back() { return *use_begin(); } 172 const User *use_back() const { return *use_begin(); } 173 174 /// hasOneUse - Return true if there is exactly one user of this value. This 175 /// is specialized because it is a common request and does not require 176 /// traversing the whole use list. 177 /// 178 bool hasOneUse() const { 179 use_const_iterator I = use_begin(), E = use_end(); 180 if (I == E) return false; 181 return ++I == E; 182 } 183 184 /// hasNUses - Return true if this Value has exactly N users. 185 /// 186 bool hasNUses(unsigned N) const; 187 188 /// hasNUsesOrMore - Return true if this value has N users or more. This is 189 /// logically equivalent to getNumUses() >= N. 190 /// 191 bool hasNUsesOrMore(unsigned N) const; 192 193 bool isUsedInBasicBlock(const BasicBlock *BB) const; 194 195 /// getNumUses - This method computes the number of uses of this Value. This 196 /// is a linear time operation. Use hasOneUse, hasNUses, or hasMoreThanNUses 197 /// to check for specific values. 198 unsigned getNumUses() const; 199 200 /// addUse - This method should only be used by the Use class. 201 /// 202 void addUse(Use &U) { U.addToList(&UseList); } 203 204 /// An enumeration for keeping track of the concrete subclass of Value that 205 /// is actually instantiated. Values of this enumeration are kept in the 206 /// Value classes SubclassID field. They are used for concrete type 207 /// identification. 208 enum ValueTy { 209 ArgumentVal, // This is an instance of Argument 210 BasicBlockVal, // This is an instance of BasicBlock 211 FunctionVal, // This is an instance of Function 212 GlobalAliasVal, // This is an instance of GlobalAlias 213 GlobalVariableVal, // This is an instance of GlobalVariable 214 UndefValueVal, // This is an instance of UndefValue 215 ConstantExprVal, // This is an instance of ConstantExpr 216 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull 217 ConstantIntVal, // This is an instance of ConstantInt 218 ConstantFPVal, // This is an instance of ConstantFP 219 ConstantArrayVal, // This is an instance of ConstantArray 220 ConstantStructVal, // This is an instance of ConstantStruct 221 ConstantVectorVal, // This is an instance of ConstantVector 222 ConstantPointerNullVal, // This is an instance of ConstantPointerNull 223 MDNodeVal, // This is an instance of MDNode 224 MDStringVal, // This is an instance of MDString 225 InlineAsmVal, // This is an instance of InlineAsm 226 PseudoSourceValueVal, // This is an instance of PseudoSourceValue 227 InstructionVal, // This is an instance of Instruction 228 229 // Markers: 230 ConstantFirstVal = FunctionVal, 231 ConstantLastVal = ConstantPointerNullVal 232 }; 233 234 /// getValueID - Return an ID for the concrete type of this object. This is 235 /// used to implement the classof checks. This should not be used for any 236 /// other purpose, as the values may change as LLVM evolves. Also, note that 237 /// for instructions, the Instruction's opcode is added to InstructionVal. So 238 /// this means three things: 239 /// # there is no value with code InstructionVal (no opcode==0). 240 /// # there are more possible values for the value type than in ValueTy enum. 241 /// # the InstructionVal enumerator must be the highest valued enumerator in 242 /// the ValueTy enum. 243 unsigned getValueID() const { 244 return SubclassID; 245 } 246 247 // Methods for support type inquiry through isa, cast, and dyn_cast: 248 static inline bool classof(const Value *) { 249 return true; // Values are always values. 250 } 251 252 /// getRawType - This should only be used to implement the vmcore library. 253 /// 254 const Type *getRawType() const { return VTy.getRawType(); } 255 256 /// stripPointerCasts - This method strips off any unneeded pointer 257 /// casts from the specified value, returning the original uncasted value. 258 /// Note that the returned value has pointer type if the specified value does. 259 Value *stripPointerCasts(); 260 const Value *stripPointerCasts() const { 261 return const_cast<Value*>(this)->stripPointerCasts(); 262 } 263 264 /// getUnderlyingObject - This method strips off any GEP address adjustments 265 /// and pointer casts from the specified value, returning the original object 266 /// being addressed. Note that the returned value has pointer type if the 267 /// specified value does. 268 Value *getUnderlyingObject(); 269 const Value *getUnderlyingObject() const { 270 return const_cast<Value*>(this)->getUnderlyingObject(); 271 } 272 273 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, 274 /// return the value in the PHI node corresponding to PredBB. If not, return 275 /// ourself. This is useful if you want to know the value something has in a 276 /// predecessor block. 277 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB); 278 279 const Value *DoPHITranslation(const BasicBlock *CurBB, 280 const BasicBlock *PredBB) const{ 281 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB); 282 } 283}; 284 285inline std::ostream &operator<<(std::ostream &OS, const Value &V) { 286 V.print(OS); 287 return OS; 288} 289inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { 290 V.print(OS); 291 return OS; 292} 293 294void Use::set(Value *V) { 295 if (Val) removeFromList(); 296 Val = V; 297 if (V) V->addUse(*this); 298} 299 300 301// isa - Provide some specializations of isa so that we don't have to include 302// the subtype header files to test to see if the value is a subclass... 303// 304template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 305 return Val.getValueID() >= Value::ConstantFirstVal && 306 Val.getValueID() <= Value::ConstantLastVal; 307} 308template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 309 return Val.getValueID() == Value::ArgumentVal; 310} 311template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) { 312 return Val.getValueID() == Value::InlineAsmVal; 313} 314template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 315 return Val.getValueID() >= Value::InstructionVal; 316} 317template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 318 return Val.getValueID() == Value::BasicBlockVal; 319} 320template <> inline bool isa_impl<Function, Value>(const Value &Val) { 321 return Val.getValueID() == Value::FunctionVal; 322} 323template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 324 return Val.getValueID() == Value::GlobalVariableVal; 325} 326template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) { 327 return Val.getValueID() == Value::GlobalAliasVal; 328} 329template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 330 return isa<GlobalVariable>(Val) || isa<Function>(Val) || 331 isa<GlobalAlias>(Val); 332} 333 334 335// Value* is only 4-byte aligned. 336template<> 337class PointerLikeTypeTraits<Value*> { 338 typedef Value* PT; 339public: 340 static inline void *getAsVoidPointer(PT P) { return P; } 341 static inline PT getFromVoidPointer(void *P) { 342 return static_cast<PT>(P); 343 } 344 enum { NumLowBitsAvailable = 2 }; 345}; 346 347} // End llvm namespace 348 349#endif 350