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_IR_VALUE_H 15 #define LLVM_IR_VALUE_H 16 17 #include "llvm/IR/Use.h" 18 #include "llvm/Support/Casting.h" 19 #include "llvm/Support/CBindingWrapping.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm-c/Core.h" 22 23 namespace llvm { 24 25 class APInt; 26 class Argument; 27 class AssemblyAnnotationWriter; 28 class BasicBlock; 29 class Constant; 30 class DataLayout; 31 class Function; 32 class GlobalAlias; 33 class GlobalValue; 34 class GlobalVariable; 35 class InlineAsm; 36 class Instruction; 37 class LLVMContext; 38 class MDNode; 39 class StringRef; 40 class Twine; 41 class Type; 42 class ValueHandleBase; 43 class ValueSymbolTable; 44 class raw_ostream; 45 46 template<typename ValueTy> class StringMapEntry; 47 typedef StringMapEntry<Value*> ValueName; 48 49 //===----------------------------------------------------------------------===// 50 // Value Class 51 //===----------------------------------------------------------------------===// 52 53 /// This is a very important LLVM class. It is the base class of all values 54 /// computed by a program that may be used as operands to other values. Value is 55 /// the super class of other important classes such as Instruction and Function. 56 /// All Values have a Type. Type is not a subclass of Value. Some values can 57 /// have a name and they belong to some Module. Setting the name on the Value 58 /// automatically updates the module's symbol table. 59 /// 60 /// Every value has a "use list" that keeps track of which other Values are 61 /// using this Value. A Value can also have an arbitrary number of ValueHandle 62 /// objects that watch it and listen to RAUW and Destroy events. See 63 /// llvm/Support/ValueHandle.h for details. 64 /// 65 /// @brief LLVM Value Representation 66 class Value { 67 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast) 68 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this? 69 protected: 70 /// SubclassOptionalData - This member is similar to SubclassData, however it 71 /// is for holding information which may be used to aid optimization, but 72 /// which may be cleared to zero without affecting conservative 73 /// interpretation. 74 unsigned char SubclassOptionalData : 7; 75 76 private: 77 /// SubclassData - This member is defined by this class, but is not used for 78 /// anything. Subclasses can use it to hold whatever state they find useful. 79 /// This field is initialized to zero by the ctor. 80 unsigned short SubclassData; 81 82 Type *VTy; 83 Use *UseList; 84 85 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name. 86 friend class ValueHandleBase; 87 ValueName *Name; 88 89 void operator=(const Value &) LLVM_DELETED_FUNCTION; 90 Value(const Value &) LLVM_DELETED_FUNCTION; 91 92 protected: 93 /// printCustom - Value subclasses can override this to implement custom 94 /// printing behavior. 95 virtual void printCustom(raw_ostream &O) const; 96 97 Value(Type *Ty, unsigned scid); 98 public: 99 virtual ~Value(); 100 101 /// dump - Support for debugging, callable in GDB: V->dump() 102 // 103 void dump() const; 104 105 /// print - Implement operator<< on Value. 106 /// 107 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const; 108 109 /// All values are typed, get the type of this value. 110 /// 111 Type *getType() const { return VTy; } 112 113 /// All values hold a context through their type. 114 LLVMContext &getContext() const; 115 116 // All values can potentially be named. 117 bool hasName() const { return Name != 0 && SubclassID != MDStringVal; } 118 ValueName *getValueName() const { return Name; } 119 void setValueName(ValueName *VN) { Name = VN; } 120 121 /// getName() - Return a constant reference to the value's name. This is cheap 122 /// and guaranteed to return the same reference as long as the value is not 123 /// modified. 124 StringRef getName() const; 125 126 /// setName() - Change the name of the value, choosing a new unique name if 127 /// the provided name is taken. 128 /// 129 /// \param Name The new name; or "" if the value's name should be removed. 130 void setName(const Twine &Name); 131 132 133 /// takeName - transfer the name from V to this value, setting V's name to 134 /// empty. It is an error to call V->takeName(V). 135 void takeName(Value *V); 136 137 /// replaceAllUsesWith - Go through the uses list for this definition and make 138 /// each use point to "V" instead of "this". After this completes, 'this's 139 /// use list is guaranteed to be empty. 140 /// 141 void replaceAllUsesWith(Value *V); 142 143 //---------------------------------------------------------------------- 144 // Methods for handling the chain of uses of this Value. 145 // 146 typedef value_use_iterator<User> use_iterator; 147 typedef value_use_iterator<const User> const_use_iterator; 148 149 bool use_empty() const { return UseList == 0; } 150 use_iterator use_begin() { return use_iterator(UseList); } 151 const_use_iterator use_begin() const { return const_use_iterator(UseList); } 152 use_iterator use_end() { return use_iterator(0); } 153 const_use_iterator use_end() const { return const_use_iterator(0); } 154 User *use_back() { return *use_begin(); } 155 const User *use_back() const { return *use_begin(); } 156 157 /// hasOneUse - Return true if there is exactly one user of this value. This 158 /// is specialized because it is a common request and does not require 159 /// traversing the whole use list. 160 /// 161 bool hasOneUse() const { 162 const_use_iterator I = use_begin(), E = use_end(); 163 if (I == E) return false; 164 return ++I == E; 165 } 166 167 /// hasNUses - Return true if this Value has exactly N users. 168 /// 169 bool hasNUses(unsigned N) const; 170 171 /// hasNUsesOrMore - Return true if this value has N users or more. This is 172 /// logically equivalent to getNumUses() >= N. 173 /// 174 bool hasNUsesOrMore(unsigned N) const; 175 176 bool isUsedInBasicBlock(const BasicBlock *BB) const; 177 178 /// getNumUses - This method computes the number of uses of this Value. This 179 /// is a linear time operation. Use hasOneUse, hasNUses, or hasNUsesOrMore 180 /// to check for specific values. 181 unsigned getNumUses() const; 182 183 /// addUse - This method should only be used by the Use class. 184 /// 185 void addUse(Use &U) { U.addToList(&UseList); } 186 187 /// An enumeration for keeping track of the concrete subclass of Value that 188 /// is actually instantiated. Values of this enumeration are kept in the 189 /// Value classes SubclassID field. They are used for concrete type 190 /// identification. 191 enum ValueTy { 192 ArgumentVal, // This is an instance of Argument 193 BasicBlockVal, // This is an instance of BasicBlock 194 FunctionVal, // This is an instance of Function 195 GlobalAliasVal, // This is an instance of GlobalAlias 196 GlobalVariableVal, // This is an instance of GlobalVariable 197 UndefValueVal, // This is an instance of UndefValue 198 BlockAddressVal, // This is an instance of BlockAddress 199 ConstantExprVal, // This is an instance of ConstantExpr 200 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero 201 ConstantDataArrayVal, // This is an instance of ConstantDataArray 202 ConstantDataVectorVal, // This is an instance of ConstantDataVector 203 ConstantIntVal, // This is an instance of ConstantInt 204 ConstantFPVal, // This is an instance of ConstantFP 205 ConstantArrayVal, // This is an instance of ConstantArray 206 ConstantStructVal, // This is an instance of ConstantStruct 207 ConstantVectorVal, // This is an instance of ConstantVector 208 ConstantPointerNullVal, // This is an instance of ConstantPointerNull 209 MDNodeVal, // This is an instance of MDNode 210 MDStringVal, // This is an instance of MDString 211 InlineAsmVal, // This is an instance of InlineAsm 212 PseudoSourceValueVal, // This is an instance of PseudoSourceValue 213 FixedStackPseudoSourceValueVal, // This is an instance of 214 // FixedStackPseudoSourceValue 215 InstructionVal, // This is an instance of Instruction 216 // Enum values starting at InstructionVal are used for Instructions; 217 // don't add new values here! 218 219 // Markers: 220 ConstantFirstVal = FunctionVal, 221 ConstantLastVal = ConstantPointerNullVal 222 }; 223 224 /// getValueID - Return an ID for the concrete type of this object. This is 225 /// used to implement the classof checks. This should not be used for any 226 /// other purpose, as the values may change as LLVM evolves. Also, note that 227 /// for instructions, the Instruction's opcode is added to InstructionVal. So 228 /// this means three things: 229 /// # there is no value with code InstructionVal (no opcode==0). 230 /// # there are more possible values for the value type than in ValueTy enum. 231 /// # the InstructionVal enumerator must be the highest valued enumerator in 232 /// the ValueTy enum. 233 unsigned getValueID() const { 234 return SubclassID; 235 } 236 237 /// getRawSubclassOptionalData - Return the raw optional flags value 238 /// contained in this value. This should only be used when testing two 239 /// Values for equivalence. 240 unsigned getRawSubclassOptionalData() const { 241 return SubclassOptionalData; 242 } 243 244 /// clearSubclassOptionalData - Clear the optional flags contained in 245 /// this value. 246 void clearSubclassOptionalData() { 247 SubclassOptionalData = 0; 248 } 249 250 /// hasSameSubclassOptionalData - Test whether the optional flags contained 251 /// in this value are equal to the optional flags in the given value. 252 bool hasSameSubclassOptionalData(const Value *V) const { 253 return SubclassOptionalData == V->SubclassOptionalData; 254 } 255 256 /// intersectOptionalDataWith - Clear any optional flags in this value 257 /// that are not also set in the given value. 258 void intersectOptionalDataWith(const Value *V) { 259 SubclassOptionalData &= V->SubclassOptionalData; 260 } 261 262 /// hasValueHandle - Return true if there is a value handle associated with 263 /// this value. 264 bool hasValueHandle() const { return HasValueHandle; } 265 266 /// \brief Strips off any unneeded pointer casts, all-zero GEPs and aliases 267 /// from the specified value, returning the original uncasted value. 268 /// 269 /// If this is called on a non-pointer value, it returns 'this'. 270 Value *stripPointerCasts(); 271 const Value *stripPointerCasts() const { 272 return const_cast<Value*>(this)->stripPointerCasts(); 273 } 274 275 /// \brief Strips off any unneeded pointer casts and all-zero GEPs from the 276 /// specified value, returning the original uncasted value. 277 /// 278 /// If this is called on a non-pointer value, it returns 'this'. 279 Value *stripPointerCastsNoFollowAliases(); 280 const Value *stripPointerCastsNoFollowAliases() const { 281 return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases(); 282 } 283 284 /// \brief Strips off unneeded pointer casts and all-constant GEPs from the 285 /// specified value, returning the original pointer value. 286 /// 287 /// If this is called on a non-pointer value, it returns 'this'. 288 Value *stripInBoundsConstantOffsets(); 289 const Value *stripInBoundsConstantOffsets() const { 290 return const_cast<Value*>(this)->stripInBoundsConstantOffsets(); 291 } 292 293 /// \brief Strips like \c stripInBoundsConstantOffsets but also accumulates 294 /// the constant offset stripped. 295 /// 296 /// Stores the resulting constant offset stripped into the APInt provided. 297 /// The provided APInt will be extended or truncated as needed to be the 298 /// correct bitwidth for an offset of this pointer type. 299 /// 300 /// If this is called on a non-pointer value, it returns 'this'. 301 Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 302 APInt &Offset); 303 const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 304 APInt &Offset) const { 305 return const_cast<Value *>(this) 306 ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 307 } 308 309 /// \brief Strips off unneeded pointer casts and any in-bounds offsets from 310 /// the specified value, returning the original pointer value. 311 /// 312 /// If this is called on a non-pointer value, it returns 'this'. 313 Value *stripInBoundsOffsets(); 314 const Value *stripInBoundsOffsets() const { 315 return const_cast<Value*>(this)->stripInBoundsOffsets(); 316 } 317 318 /// isDereferenceablePointer - Test if this value is always a pointer to 319 /// allocated and suitably aligned memory for a simple load or store. 320 bool isDereferenceablePointer() const; 321 322 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, 323 /// return the value in the PHI node corresponding to PredBB. If not, return 324 /// ourself. This is useful if you want to know the value something has in a 325 /// predecessor block. 326 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB); 327 328 const Value *DoPHITranslation(const BasicBlock *CurBB, 329 const BasicBlock *PredBB) const{ 330 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB); 331 } 332 333 /// MaximumAlignment - This is the greatest alignment value supported by 334 /// load, store, and alloca instructions, and global values. 335 static const unsigned MaximumAlignment = 1u << 29; 336 337 /// mutateType - Mutate the type of this Value to be of the specified type. 338 /// Note that this is an extremely dangerous operation which can create 339 /// completely invalid IR very easily. It is strongly recommended that you 340 /// recreate IR objects with the right types instead of mutating them in 341 /// place. 342 void mutateType(Type *Ty) { 343 VTy = Ty; 344 } 345 346 protected: 347 unsigned short getSubclassDataFromValue() const { return SubclassData; } 348 void setValueSubclassData(unsigned short D) { SubclassData = D; } 349 }; 350 351 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { 352 V.print(OS); 353 return OS; 354 } 355 356 void Use::set(Value *V) { 357 if (Val) removeFromList(); 358 Val = V; 359 if (V) V->addUse(*this); 360 } 361 362 363 // isa - Provide some specializations of isa so that we don't have to include 364 // the subtype header files to test to see if the value is a subclass... 365 // 366 template <> struct isa_impl<Constant, Value> { 367 static inline bool doit(const Value &Val) { 368 return Val.getValueID() >= Value::ConstantFirstVal && 369 Val.getValueID() <= Value::ConstantLastVal; 370 } 371 }; 372 373 template <> struct isa_impl<Argument, Value> { 374 static inline bool doit (const Value &Val) { 375 return Val.getValueID() == Value::ArgumentVal; 376 } 377 }; 378 379 template <> struct isa_impl<InlineAsm, Value> { 380 static inline bool doit(const Value &Val) { 381 return Val.getValueID() == Value::InlineAsmVal; 382 } 383 }; 384 385 template <> struct isa_impl<Instruction, Value> { 386 static inline bool doit(const Value &Val) { 387 return Val.getValueID() >= Value::InstructionVal; 388 } 389 }; 390 391 template <> struct isa_impl<BasicBlock, Value> { 392 static inline bool doit(const Value &Val) { 393 return Val.getValueID() == Value::BasicBlockVal; 394 } 395 }; 396 397 template <> struct isa_impl<Function, Value> { 398 static inline bool doit(const Value &Val) { 399 return Val.getValueID() == Value::FunctionVal; 400 } 401 }; 402 403 template <> struct isa_impl<GlobalVariable, Value> { 404 static inline bool doit(const Value &Val) { 405 return Val.getValueID() == Value::GlobalVariableVal; 406 } 407 }; 408 409 template <> struct isa_impl<GlobalAlias, Value> { 410 static inline bool doit(const Value &Val) { 411 return Val.getValueID() == Value::GlobalAliasVal; 412 } 413 }; 414 415 template <> struct isa_impl<GlobalValue, Value> { 416 static inline bool doit(const Value &Val) { 417 return isa<GlobalVariable>(Val) || isa<Function>(Val) || 418 isa<GlobalAlias>(Val); 419 } 420 }; 421 422 template <> struct isa_impl<MDNode, Value> { 423 static inline bool doit(const Value &Val) { 424 return Val.getValueID() == Value::MDNodeVal; 425 } 426 }; 427 428 // Value* is only 4-byte aligned. 429 template<> 430 class PointerLikeTypeTraits<Value*> { 431 typedef Value* PT; 432 public: 433 static inline void *getAsVoidPointer(PT P) { return P; } 434 static inline PT getFromVoidPointer(void *P) { 435 return static_cast<PT>(P); 436 } 437 enum { NumLowBitsAvailable = 2 }; 438 }; 439 440 // Create wrappers for C Binding types (see CBindingWrapping.h). 441 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef) 442 443 /* Specialized opaque value conversions. 444 */ 445 inline Value **unwrap(LLVMValueRef *Vals) { 446 return reinterpret_cast<Value**>(Vals); 447 } 448 449 template<typename T> 450 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { 451 #ifdef DEBUG 452 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) 453 cast<T>(*I); 454 #endif 455 (void)Length; 456 return reinterpret_cast<T**>(Vals); 457 } 458 459 inline LLVMValueRef *wrap(const Value **Vals) { 460 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); 461 } 462 463 } // End llvm namespace 464 465 #endif 466