1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the Value class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_VALUE_H
15 #define LLVM_IR_VALUE_H
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"
27 class AssemblyAnnotationWriter;
42 class ValueHandleBase;
43 class ValueSymbolTable;
46 template<typename ValueTy> class StringMapEntry;
47 typedef StringMapEntry<Value*> ValueName;
49 //===----------------------------------------------------------------------===//
51 //===----------------------------------------------------------------------===//
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.
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.
65 /// @brief LLVM Value Representation
67 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
68 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
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
74 unsigned char SubclassOptionalData : 7;
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;
85 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
86 friend class ValueHandleBase;
89 void operator=(const Value &) LLVM_DELETED_FUNCTION;
90 Value(const Value &) LLVM_DELETED_FUNCTION;
93 /// printCustom - Value subclasses can override this to implement custom
94 /// printing behavior.
95 virtual void printCustom(raw_ostream &O) const;
97 Value(Type *Ty, unsigned scid);
101 /// dump - Support for debugging, callable in GDB: V->dump()
105 /// print - Implement operator<< on Value.
107 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
109 /// All values are typed, get the type of this value.
111 Type *getType() const { return VTy; }
113 /// All values hold a context through their type.
114 LLVMContext &getContext() const;
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; }
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
124 StringRef getName() const;
126 /// setName() - Change the name of the value, choosing a new unique name if
127 /// the provided name is taken.
129 /// \param Name The new name; or "" if the value's name should be removed.
130 void setName(const Twine &Name);
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);
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.
141 void replaceAllUsesWith(Value *V);
143 //----------------------------------------------------------------------
144 // Methods for handling the chain of uses of this Value.
146 typedef value_use_iterator<User> use_iterator;
147 typedef value_use_iterator<const User> const_use_iterator;
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(); }
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.
161 bool hasOneUse() const {
162 const_use_iterator I = use_begin(), E = use_end();
163 if (I == E) return false;
167 /// hasNUses - Return true if this Value has exactly N users.
169 bool hasNUses(unsigned N) const;
171 /// hasNUsesOrMore - Return true if this value has N users or more. This is
172 /// logically equivalent to getNumUses() >= N.
174 bool hasNUsesOrMore(unsigned N) const;
176 bool isUsedInBasicBlock(const BasicBlock *BB) const;
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;
183 /// addUse - This method should only be used by the Use class.
185 void addUse(Use &U) { U.addToList(&UseList); }
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
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!
220 ConstantFirstVal = FunctionVal,
221 ConstantLastVal = ConstantPointerNullVal
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 {
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;
244 /// clearSubclassOptionalData - Clear the optional flags contained in
246 void clearSubclassOptionalData() {
247 SubclassOptionalData = 0;
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;
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;
262 /// hasValueHandle - Return true if there is a value handle associated with
264 bool hasValueHandle() const { return HasValueHandle; }
266 /// \brief Strips off any unneeded pointer casts, all-zero GEPs and aliases
267 /// from the specified value, returning the original uncasted value.
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();
275 /// \brief Strips off any unneeded pointer casts and all-zero GEPs from the
276 /// specified value, returning the original uncasted value.
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();
284 /// \brief Strips off unneeded pointer casts and all-constant GEPs from the
285 /// specified value, returning the original pointer value.
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();
293 /// \brief Strips like \c stripInBoundsConstantOffsets but also accumulates
294 /// the constant offset stripped.
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.
300 /// If this is called on a non-pointer value, it returns 'this'.
301 Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
303 const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
304 APInt &Offset) const {
305 return const_cast<Value *>(this)
306 ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
309 /// \brief Strips off unneeded pointer casts and any in-bounds offsets from
310 /// the specified value, returning the original pointer value.
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();
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;
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);
328 const Value *DoPHITranslation(const BasicBlock *CurBB,
329 const BasicBlock *PredBB) const{
330 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
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;
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
342 void mutateType(Type *Ty) {
347 unsigned short getSubclassDataFromValue() const { return SubclassData; }
348 void setValueSubclassData(unsigned short D) { SubclassData = D; }
351 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
356 void Use::set(Value *V) {
357 if (Val) removeFromList();
359 if (V) V->addUse(*this);
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...
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;
373 template <> struct isa_impl<Argument, Value> {
374 static inline bool doit (const Value &Val) {
375 return Val.getValueID() == Value::ArgumentVal;
379 template <> struct isa_impl<InlineAsm, Value> {
380 static inline bool doit(const Value &Val) {
381 return Val.getValueID() == Value::InlineAsmVal;
385 template <> struct isa_impl<Instruction, Value> {
386 static inline bool doit(const Value &Val) {
387 return Val.getValueID() >= Value::InstructionVal;
391 template <> struct isa_impl<BasicBlock, Value> {
392 static inline bool doit(const Value &Val) {
393 return Val.getValueID() == Value::BasicBlockVal;
397 template <> struct isa_impl<Function, Value> {
398 static inline bool doit(const Value &Val) {
399 return Val.getValueID() == Value::FunctionVal;
403 template <> struct isa_impl<GlobalVariable, Value> {
404 static inline bool doit(const Value &Val) {
405 return Val.getValueID() == Value::GlobalVariableVal;
409 template <> struct isa_impl<GlobalAlias, Value> {
410 static inline bool doit(const Value &Val) {
411 return Val.getValueID() == Value::GlobalAliasVal;
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);
422 template <> struct isa_impl<MDNode, Value> {
423 static inline bool doit(const Value &Val) {
424 return Val.getValueID() == Value::MDNodeVal;
428 // Value* is only 4-byte aligned.
430 class PointerLikeTypeTraits<Value*> {
433 static inline void *getAsVoidPointer(PT P) { return P; }
434 static inline PT getFromVoidPointer(void *P) {
435 return static_cast<PT>(P);
437 enum { NumLowBitsAvailable = 2 };
440 // Create wrappers for C Binding types (see CBindingWrapping.h).
441 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
443 /* Specialized opaque value conversions.
445 inline Value **unwrap(LLVMValueRef *Vals) {
446 return reinterpret_cast<Value**>(Vals);
450 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
452 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
456 return reinterpret_cast<T**>(Vals);
459 inline LLVMValueRef *wrap(const Value **Vals) {
460 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
463 } // End llvm namespace