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"
33 class ValueSymbolTable;
34 template<typename ValueTy> class StringMapEntry;
35 template <typename ValueTy = Value>
37 typedef StringMapEntry<Value*> ValueName;
39 class AssemblyAnnotationWriter;
40 class ValueHandleBase;
46 //===----------------------------------------------------------------------===//
48 //===----------------------------------------------------------------------===//
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. Some values can
54 /// have a name and they belong to some Module. Setting the name on the Value
55 /// automatically updates the module's symbol table.
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.
62 /// @brief LLVM Value Representation
64 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
65 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
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
71 unsigned char SubclassOptionalData : 7;
74 /// SubclassData - This member is defined by this class, but is not used for
75 /// anything. Subclasses can use it to hold whatever state they find useful.
76 /// This field is initialized to zero by the ctor.
77 unsigned short SubclassData;
82 friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
83 friend class ValueHandleBase;
86 void operator=(const Value &); // Do not implement
87 Value(const Value &); // Do not implement
90 /// printCustom - Value subclasses can override this to implement custom
91 /// printing behavior.
92 virtual void printCustom(raw_ostream &O) const;
94 Value(Type *Ty, unsigned scid);
98 /// dump - Support for debugging, callable in GDB: V->dump()
102 /// print - Implement operator<< on Value.
104 void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
106 /// All values are typed, get the type of this value.
108 Type *getType() const { return VTy; }
110 /// All values hold a context through their type.
111 LLVMContext &getContext() const;
113 // All values can potentially be named...
114 bool hasName() const { return Name != 0; }
115 ValueName *getValueName() const { return Name; }
117 /// getName() - Return a constant reference to the value's name. This is cheap
118 /// and guaranteed to return the same reference as long as the value is not
120 StringRef getName() const;
122 /// setName() - Change the name of the value, choosing a new unique name if
123 /// the provided name is taken.
125 /// \arg Name - The new name; or "" if the value's name should be removed.
126 void setName(const Twine &Name);
129 /// takeName - transfer the name from V to this value, setting V's name to
130 /// empty. It is an error to call V->takeName(V).
131 void takeName(Value *V);
133 /// replaceAllUsesWith - Go through the uses list for this definition and make
134 /// each use point to "V" instead of "this". After this completes, 'this's
135 /// use list is guaranteed to be empty.
137 void replaceAllUsesWith(Value *V);
139 //----------------------------------------------------------------------
140 // Methods for handling the chain of uses of this Value.
142 typedef value_use_iterator<User> use_iterator;
143 typedef value_use_iterator<const User> const_use_iterator;
145 bool use_empty() const { return UseList == 0; }
146 use_iterator use_begin() { return use_iterator(UseList); }
147 const_use_iterator use_begin() const { return const_use_iterator(UseList); }
148 use_iterator use_end() { return use_iterator(0); }
149 const_use_iterator use_end() const { return const_use_iterator(0); }
150 User *use_back() { return *use_begin(); }
151 const User *use_back() const { return *use_begin(); }
153 /// hasOneUse - Return true if there is exactly one user of this value. This
154 /// is specialized because it is a common request and does not require
155 /// traversing the whole use list.
157 bool hasOneUse() const {
158 const_use_iterator I = use_begin(), E = use_end();
159 if (I == E) return false;
163 /// hasNUses - Return true if this Value has exactly N users.
165 bool hasNUses(unsigned N) const;
167 /// hasNUsesOrMore - Return true if this value has N users or more. This is
168 /// logically equivalent to getNumUses() >= N.
170 bool hasNUsesOrMore(unsigned N) const;
172 bool isUsedInBasicBlock(const BasicBlock *BB) const;
174 /// getNumUses - This method computes the number of uses of this Value. This
175 /// is a linear time operation. Use hasOneUse, hasNUses, or hasNUsesOrMore
176 /// to check for specific values.
177 unsigned getNumUses() const;
179 /// addUse - This method should only be used by the Use class.
181 void addUse(Use &U) { U.addToList(&UseList); }
183 /// An enumeration for keeping track of the concrete subclass of Value that
184 /// is actually instantiated. Values of this enumeration are kept in the
185 /// Value classes SubclassID field. They are used for concrete type
188 ArgumentVal, // This is an instance of Argument
189 BasicBlockVal, // This is an instance of BasicBlock
190 FunctionVal, // This is an instance of Function
191 GlobalAliasVal, // This is an instance of GlobalAlias
192 GlobalVariableVal, // This is an instance of GlobalVariable
193 UndefValueVal, // This is an instance of UndefValue
194 BlockAddressVal, // This is an instance of BlockAddress
195 ConstantExprVal, // This is an instance of ConstantExpr
196 ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
197 ConstantIntVal, // This is an instance of ConstantInt
198 ConstantFPVal, // This is an instance of ConstantFP
199 ConstantArrayVal, // This is an instance of ConstantArray
200 ConstantStructVal, // This is an instance of ConstantStruct
201 ConstantVectorVal, // This is an instance of ConstantVector
202 ConstantPointerNullVal, // This is an instance of ConstantPointerNull
203 MDNodeVal, // This is an instance of MDNode
204 MDStringVal, // This is an instance of MDString
205 InlineAsmVal, // This is an instance of InlineAsm
206 PseudoSourceValueVal, // This is an instance of PseudoSourceValue
207 FixedStackPseudoSourceValueVal, // This is an instance of
208 // FixedStackPseudoSourceValue
209 InstructionVal, // This is an instance of Instruction
210 // Enum values starting at InstructionVal are used for Instructions;
211 // don't add new values here!
214 ConstantFirstVal = FunctionVal,
215 ConstantLastVal = ConstantPointerNullVal
218 /// getValueID - Return an ID for the concrete type of this object. This is
219 /// used to implement the classof checks. This should not be used for any
220 /// other purpose, as the values may change as LLVM evolves. Also, note that
221 /// for instructions, the Instruction's opcode is added to InstructionVal. So
222 /// this means three things:
223 /// # there is no value with code InstructionVal (no opcode==0).
224 /// # there are more possible values for the value type than in ValueTy enum.
225 /// # the InstructionVal enumerator must be the highest valued enumerator in
226 /// the ValueTy enum.
227 unsigned getValueID() const {
231 /// getRawSubclassOptionalData - Return the raw optional flags value
232 /// contained in this value. This should only be used when testing two
233 /// Values for equivalence.
234 unsigned getRawSubclassOptionalData() const {
235 return SubclassOptionalData;
238 /// clearSubclassOptionalData - Clear the optional flags contained in
240 void clearSubclassOptionalData() {
241 SubclassOptionalData = 0;
244 /// hasSameSubclassOptionalData - Test whether the optional flags contained
245 /// in this value are equal to the optional flags in the given value.
246 bool hasSameSubclassOptionalData(const Value *V) const {
247 return SubclassOptionalData == V->SubclassOptionalData;
250 /// intersectOptionalDataWith - Clear any optional flags in this value
251 /// that are not also set in the given value.
252 void intersectOptionalDataWith(const Value *V) {
253 SubclassOptionalData &= V->SubclassOptionalData;
256 /// hasValueHandle - Return true if there is a value handle associated with
258 bool hasValueHandle() const { return HasValueHandle; }
260 // Methods for support type inquiry through isa, cast, and dyn_cast:
261 static inline bool classof(const Value *) {
262 return true; // Values are always values.
265 /// stripPointerCasts - This method strips off any unneeded pointer
266 /// casts from the specified value, returning the original uncasted value.
267 /// Note that the returned value has pointer type if the specified value does.
268 Value *stripPointerCasts();
269 const Value *stripPointerCasts() const {
270 return const_cast<Value*>(this)->stripPointerCasts();
273 /// isDereferenceablePointer - Test if this value is always a pointer to
274 /// allocated and suitably aligned memory for a simple load or store.
275 bool isDereferenceablePointer() const;
277 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
278 /// return the value in the PHI node corresponding to PredBB. If not, return
279 /// ourself. This is useful if you want to know the value something has in a
280 /// predecessor block.
281 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
283 const Value *DoPHITranslation(const BasicBlock *CurBB,
284 const BasicBlock *PredBB) const{
285 return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
288 /// MaximumAlignment - This is the greatest alignment value supported by
289 /// load, store, and alloca instructions, and global values.
290 static const unsigned MaximumAlignment = 1u << 29;
292 /// mutateType - Mutate the type of this Value to be of the specified type.
293 /// Note that this is an extremely dangerous operation which can create
294 /// completely invalid IR very easily. It is strongly recommended that you
295 /// recreate IR objects with the right types instead of mutating them in
297 void mutateType(Type *Ty) {
302 unsigned short getSubclassDataFromValue() const { return SubclassData; }
303 void setValueSubclassData(unsigned short D) { SubclassData = D; }
306 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
311 void Use::set(Value *V) {
312 if (Val) removeFromList();
314 if (V) V->addUse(*this);
318 // isa - Provide some specializations of isa so that we don't have to include
319 // the subtype header files to test to see if the value is a subclass...
321 template <> struct isa_impl<Constant, Value> {
322 static inline bool doit(const Value &Val) {
323 return Val.getValueID() >= Value::ConstantFirstVal &&
324 Val.getValueID() <= Value::ConstantLastVal;
328 template <> struct isa_impl<Argument, Value> {
329 static inline bool doit (const Value &Val) {
330 return Val.getValueID() == Value::ArgumentVal;
334 template <> struct isa_impl<InlineAsm, Value> {
335 static inline bool doit(const Value &Val) {
336 return Val.getValueID() == Value::InlineAsmVal;
340 template <> struct isa_impl<Instruction, Value> {
341 static inline bool doit(const Value &Val) {
342 return Val.getValueID() >= Value::InstructionVal;
346 template <> struct isa_impl<BasicBlock, Value> {
347 static inline bool doit(const Value &Val) {
348 return Val.getValueID() == Value::BasicBlockVal;
352 template <> struct isa_impl<Function, Value> {
353 static inline bool doit(const Value &Val) {
354 return Val.getValueID() == Value::FunctionVal;
358 template <> struct isa_impl<GlobalVariable, Value> {
359 static inline bool doit(const Value &Val) {
360 return Val.getValueID() == Value::GlobalVariableVal;
364 template <> struct isa_impl<GlobalAlias, Value> {
365 static inline bool doit(const Value &Val) {
366 return Val.getValueID() == Value::GlobalAliasVal;
370 template <> struct isa_impl<GlobalValue, Value> {
371 static inline bool doit(const Value &Val) {
372 return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
373 isa<GlobalAlias>(Val);
377 template <> struct isa_impl<MDNode, Value> {
378 static inline bool doit(const Value &Val) {
379 return Val.getValueID() == Value::MDNodeVal;
383 // Value* is only 4-byte aligned.
385 class PointerLikeTypeTraits<Value*> {
388 static inline void *getAsVoidPointer(PT P) { return P; }
389 static inline PT getFromVoidPointer(void *P) {
390 return static_cast<PT>(P);
392 enum { NumLowBitsAvailable = 2 };
395 } // End llvm namespace