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 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Casting.h"
32 class ValueSymbolTable;
33 template<typename ValueTy> class StringMapEntry;
34 template <typename ValueTy = Value>
36 typedef StringMapEntry<Value*> ValueName;
38 class AssemblyAnnotationWriter;
39 class ValueHandleBase;
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 /// This is a very important LLVM class. It is the base class of all values
50 /// computed by a program that may be used as operands to other values. Value is
51 /// the super class of other important classes such as Instruction and Function.
52 /// All Values have a Type. Type is not a subclass of Value. Some values can
53 /// have a name and they belong to some Module. Setting the name on the Value
54 /// automatically updates the module's symbol table.
56 /// Every value has a "use list" that keeps track of which other Values are
57 /// using this Value. A Value can also have an arbitrary number of ValueHandle
58 /// objects that watch it and listen to RAUW and Destroy events. See
59 /// llvm/Support/ValueHandle.h for details.
61 /// @brief LLVM Value Representation
63 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
64 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
66 /// SubclassOptionalData - This member is similar to SubclassData, however it
67 /// is for holding information which may be used to aid optimization, but
68 /// which may be cleared to zero without affecting conservative
70 unsigned char SubclassOptionalData : 7;
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;
81 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
82 friend class ValueHandleBase;
85 void operator=(const Value &); // Do not implement
86 Value(const Value &); // Do not implement
89 /// printCustom - Value subclasses can override this to implement custom
90 /// printing behavior.
91 virtual void printCustom(raw_ostream &O) const;
93 Value(Type *Ty, unsigned scid);
97 /// dump - Support for debugging, callable in GDB: V->dump()
101 /// print - Implement operator<< on Value.
103 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
105 /// All values are typed, get the type of this value.
107 Type *getType() const { return VTy; }
109 /// All values hold a context through their type.
110 LLVMContext &getContext() const;
112 // All values can potentially be named...
113 bool hasName() const { return Name != 0; }
114 ValueName *getValueName() const { return Name; }
116 /// getName() - Return a constant reference to the value's name. This is cheap
117 /// and guaranteed to return the same reference as long as the value is not
119 StringRef getName() const;
121 /// setName() - Change the name of the value, choosing a new unique name if
122 /// the provided name is taken.
124 /// \arg Name - The new name; or "" if the value's name should be removed.
125 void setName(const Twine &Name);
128 /// takeName - transfer the name from V to this value, setting V's name to
129 /// empty. It is an error to call V->takeName(V).
130 void takeName(Value *V);
132 /// replaceAllUsesWith - Go through the uses list for this definition and make
133 /// each use point to "V" instead of "this". After this completes, 'this's
134 /// use list is guaranteed to be empty.
136 void replaceAllUsesWith(Value *V);
138 //----------------------------------------------------------------------
139 // Methods for handling the chain of uses of this Value.
141 typedef value_use_iterator<User> use_iterator;
142 typedef value_use_iterator<const User> const_use_iterator;
144 bool use_empty() const { return UseList == 0; }
145 use_iterator use_begin() { return use_iterator(UseList); }
146 const_use_iterator use_begin() const { return const_use_iterator(UseList); }
147 use_iterator use_end() { return use_iterator(0); }
148 const_use_iterator use_end() const { return const_use_iterator(0); }
149 User *use_back() { return *use_begin(); }
150 const User *use_back() const { return *use_begin(); }
152 /// hasOneUse - Return true if there is exactly one user of this value. This
153 /// is specialized because it is a common request and does not require
154 /// traversing the whole use list.
156 bool hasOneUse() const {
157 const_use_iterator I = use_begin(), E = use_end();
158 if (I == E) return false;
162 /// hasNUses - Return true if this Value has exactly N users.
164 bool hasNUses(unsigned N) const;
166 /// hasNUsesOrMore - Return true if this value has N users or more. This is
167 /// logically equivalent to getNumUses() >= N.
169 bool hasNUsesOrMore(unsigned N) const;
171 bool isUsedInBasicBlock(const BasicBlock *BB) const;
173 /// getNumUses - This method computes the number of uses of this Value. This
174 /// is a linear time operation. Use hasOneUse, hasNUses, or hasNUsesOrMore
175 /// to check for specific values.
176 unsigned getNumUses() const;
178 /// addUse - This method should only be used by the Use class.
180 void addUse(Use &U) { U.addToList(&UseList); }
182 /// An enumeration for keeping track of the concrete subclass of Value that
183 /// is actually instantiated. Values of this enumeration are kept in the
184 /// Value classes SubclassID field. They are used for concrete type
187 ArgumentVal, // This is an instance of Argument
188 BasicBlockVal, // This is an instance of BasicBlock
189 FunctionVal, // This is an instance of Function
190 GlobalAliasVal, // This is an instance of GlobalAlias
191 GlobalVariableVal, // This is an instance of GlobalVariable
192 UndefValueVal, // This is an instance of UndefValue
193 BlockAddressVal, // This is an instance of BlockAddress
194 ConstantExprVal, // This is an instance of ConstantExpr
195 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
196 ConstantDataArrayVal, // This is an instance of ConstantDataArray
197 ConstantDataVectorVal, // This is an instance of ConstantDataVector
198 ConstantIntVal, // This is an instance of ConstantInt
199 ConstantFPVal, // This is an instance of ConstantFP
200 ConstantArrayVal, // This is an instance of ConstantArray
201 ConstantStructVal, // This is an instance of ConstantStruct
202 ConstantVectorVal, // This is an instance of ConstantVector
203 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
204 MDNodeVal, // This is an instance of MDNode
205 MDStringVal, // This is an instance of MDString
206 InlineAsmVal, // This is an instance of InlineAsm
207 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
208 FixedStackPseudoSourceValueVal, // This is an instance of
209 // FixedStackPseudoSourceValue
210 InstructionVal, // This is an instance of Instruction
211 // Enum values starting at InstructionVal are used for Instructions;
212 // don't add new values here!
215 ConstantFirstVal = FunctionVal,
216 ConstantLastVal = ConstantPointerNullVal
219 /// getValueID - Return an ID for the concrete type of this object. This is
220 /// used to implement the classof checks. This should not be used for any
221 /// other purpose, as the values may change as LLVM evolves. Also, note that
222 /// for instructions, the Instruction's opcode is added to InstructionVal. So
223 /// this means three things:
224 /// # there is no value with code InstructionVal (no opcode==0).
225 /// # there are more possible values for the value type than in ValueTy enum.
226 /// # the InstructionVal enumerator must be the highest valued enumerator in
227 /// the ValueTy enum.
228 unsigned getValueID() const {
232 /// getRawSubclassOptionalData - Return the raw optional flags value
233 /// contained in this value. This should only be used when testing two
234 /// Values for equivalence.
235 unsigned getRawSubclassOptionalData() const {
236 return SubclassOptionalData;
239 /// clearSubclassOptionalData - Clear the optional flags contained in
241 void clearSubclassOptionalData() {
242 SubclassOptionalData = 0;
245 /// hasSameSubclassOptionalData - Test whether the optional flags contained
246 /// in this value are equal to the optional flags in the given value.
247 bool hasSameSubclassOptionalData(const Value *V) const {
248 return SubclassOptionalData == V->SubclassOptionalData;
251 /// intersectOptionalDataWith - Clear any optional flags in this value
252 /// that are not also set in the given value.
253 void intersectOptionalDataWith(const Value *V) {
254 SubclassOptionalData &= V->SubclassOptionalData;
257 /// hasValueHandle - Return true if there is a value handle associated with
259 bool hasValueHandle() const { return HasValueHandle; }
261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const Value *) {
263 return true; // Values are always values.
266 /// stripPointerCasts - This method strips off any unneeded pointer
267 /// casts from the specified value, returning the original uncasted value.
268 /// Note that the returned value has pointer type if the specified value does.
269 Value *stripPointerCasts();
270 const Value *stripPointerCasts() const {
271 return const_cast<Value*>(this)->stripPointerCasts();
274 /// isDereferenceablePointer - Test if this value is always a pointer to
275 /// allocated and suitably aligned memory for a simple load or store.
276 bool isDereferenceablePointer() const;
278 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
279 /// return the value in the PHI node corresponding to PredBB. If not, return
280 /// ourself. This is useful if you want to know the value something has in a
281 /// predecessor block.
282 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
284 const Value *DoPHITranslation(const BasicBlock *CurBB,
285 const BasicBlock *PredBB) const{
286 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
289 /// MaximumAlignment - This is the greatest alignment value supported by
290 /// load, store, and alloca instructions, and global values.
291 static const unsigned MaximumAlignment = 1u << 29;
293 /// mutateType - Mutate the type of this Value to be of the specified type.
294 /// Note that this is an extremely dangerous operation which can create
295 /// completely invalid IR very easily. It is strongly recommended that you
296 /// recreate IR objects with the right types instead of mutating them in
298 void mutateType(Type *Ty) {
303 unsigned short getSubclassDataFromValue() const { return SubclassData; }
304 void setValueSubclassData(unsigned short D) { SubclassData = D; }
307 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
312 void Use::set(Value *V) {
313 if (Val) removeFromList();
315 if (V) V->addUse(*this);
319 // isa - Provide some specializations of isa so that we don't have to include
320 // the subtype header files to test to see if the value is a subclass...
322 template <> struct isa_impl<Constant, Value> {
323 static inline bool doit(const Value &Val) {
324 return Val.getValueID() >= Value::ConstantFirstVal &&
325 Val.getValueID() <= Value::ConstantLastVal;
329 template <> struct isa_impl<Argument, Value> {
330 static inline bool doit (const Value &Val) {
331 return Val.getValueID() == Value::ArgumentVal;
335 template <> struct isa_impl<InlineAsm, Value> {
336 static inline bool doit(const Value &Val) {
337 return Val.getValueID() == Value::InlineAsmVal;
341 template <> struct isa_impl<Instruction, Value> {
342 static inline bool doit(const Value &Val) {
343 return Val.getValueID() >= Value::InstructionVal;
347 template <> struct isa_impl<BasicBlock, Value> {
348 static inline bool doit(const Value &Val) {
349 return Val.getValueID() == Value::BasicBlockVal;
353 template <> struct isa_impl<Function, Value> {
354 static inline bool doit(const Value &Val) {
355 return Val.getValueID() == Value::FunctionVal;
359 template <> struct isa_impl<GlobalVariable, Value> {
360 static inline bool doit(const Value &Val) {
361 return Val.getValueID() == Value::GlobalVariableVal;
365 template <> struct isa_impl<GlobalAlias, Value> {
366 static inline bool doit(const Value &Val) {
367 return Val.getValueID() == Value::GlobalAliasVal;
371 template <> struct isa_impl<GlobalValue, Value> {
372 static inline bool doit(const Value &Val) {
373 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
374 isa<GlobalAlias>(Val);
378 template <> struct isa_impl<MDNode, Value> {
379 static inline bool doit(const Value &Val) {
380 return Val.getValueID() == Value::MDNodeVal;
384 // Value* is only 4-byte aligned.
386 class PointerLikeTypeTraits<Value*> {
389 static inline void *getAsVoidPointer(PT P) { return P; }
390 static inline PT getFromVoidPointer(void *P) {
391 return static_cast<PT>(P);
393 enum { NumLowBitsAvailable = 2 };
396 } // End llvm namespace