Value.h revision bddcb9427cb36ac6609fef233eaac3c9b5e5a8f4
1//===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the Value class.
11// This file also defines the Use<> template for users of value.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_VALUE_H
16#define LLVM_VALUE_H
17
18#include "llvm/AbstractTypeUser.h"
19#include "llvm/Use.h"
20#include "llvm/Support/Casting.h"
21#include <string>
22
23namespace llvm {
24
25class Constant;
26class Argument;
27class Instruction;
28class BasicBlock;
29class GlobalValue;
30class Function;
31class GlobalVariable;
32class InlineAsm;
33class SymbolTable;
34
35//===----------------------------------------------------------------------===//
36//                                 Value Class
37//===----------------------------------------------------------------------===//
38
39/// This is a very important LLVM class. It is the base class of all values
40/// computed by a program that may be used as operands to other values. Value is
41/// the super class of other important classes such as Instruction and Function.
42/// All Values have a Type. Type is not a subclass of Value. All types can have
43/// a name and they should belong to some Module. Setting the name on the Value
44/// automatically update's the module's symbol table.
45///
46/// Every value has a "use list" that keeps track of which other Values are
47/// using this Value.
48/// @brief LLVM Value Representation
49class Value {
50  unsigned short SubclassID;         // Subclass identifier (for isa/dyn_cast)
51protected:
52  /// SubclassData - This member is defined by this class, but is not used for
53  /// anything.  Subclasses can use it to hold whatever state they find useful.
54  /// This field is initialized to zero by the ctor.
55  unsigned short SubclassData;
56private:
57  PATypeHolder Ty;
58  Use *UseList;
59
60  friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
61  friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
62  std::string Name;
63
64  void operator=(const Value &);     // Do not implement
65  Value(const Value &);              // Do not implement
66
67public:
68  Value(const Type *Ty, unsigned scid, const std::string &name = "");
69  virtual ~Value();
70
71  /// dump - Support for debugging, callable in GDB: V->dump()
72  //
73  virtual void dump() const;
74
75  /// print - Implement operator<< on Value...
76  ///
77  virtual void print(std::ostream &O) const = 0;
78
79  /// All values are typed, get the type of this value.
80  ///
81  inline const Type *getType() const { return Ty; }
82
83  // All values can potentially be named...
84  inline bool               hasName() const { return !Name.empty(); }
85  inline const std::string &getName() const { return Name; }
86
87  void setName(const std::string &name);
88
89  /// replaceAllUsesWith - Go through the uses list for this definition and make
90  /// each use point to "V" instead of "this".  After this completes, 'this's
91  /// use list is guaranteed to be empty.
92  ///
93  void replaceAllUsesWith(Value *V);
94
95  // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
96  // Only use when in type resolution situations!
97  void uncheckedReplaceAllUsesWith(Value *V);
98
99  //----------------------------------------------------------------------
100  // Methods for handling the vector of uses of this Value.
101  //
102  typedef value_use_iterator<User>       use_iterator;
103  typedef value_use_iterator<const User> use_const_iterator;
104
105  bool               use_empty() const { return UseList == 0; }
106  use_iterator       use_begin()       { return use_iterator(UseList); }
107  use_const_iterator use_begin() const { return use_const_iterator(UseList); }
108  use_iterator       use_end()         { return use_iterator(0);   }
109  use_const_iterator use_end()   const { return use_const_iterator(0);   }
110  User              *use_back()        { return *use_begin(); }
111  const User        *use_back() const  { return *use_begin(); }
112
113  /// hasOneUse - Return true if there is exactly one user of this value.  This
114  /// is specialized because it is a common request and does not require
115  /// traversing the whole use list.
116  ///
117  bool hasOneUse() const {
118    use_const_iterator I = use_begin(), E = use_end();
119    if (I == E) return false;
120    return ++I == E;
121  }
122
123  /// hasNUses - Return true if this Value has exactly N users.
124  ///
125  bool hasNUses(unsigned N) const;
126
127  /// hasNUsesOrMore - Return true if this value has N users or more.  This is
128  /// logically equivalent to getNumUses() >= N.
129  ///
130  bool hasNUsesOrMore(unsigned N) const;
131
132  /// getNumUses - This method computes the number of uses of this Value.  This
133  /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
134  /// to check for specific values.
135  unsigned getNumUses() const;
136
137  /// addUse/killUse - These two methods should only be used by the Use class.
138  ///
139  void addUse(Use &U) { U.addToList(&UseList); }
140
141  /// An enumeration for keeping track of the concrete subclass of Value that
142  /// is actually instantiated. Values of this enumeration are kept in the
143  /// Value classes SubclassID field. They are used for concrete type
144  /// identification.
145  enum ValueTy {
146    ArgumentVal,              // This is an instance of Argument
147    BasicBlockVal,            // This is an instance of BasicBlock
148    FunctionVal,              // This is an instance of Function
149    GlobalVariableVal,        // This is an instance of GlobalVariable
150    UndefValueVal,            // This is an instance of UndefValue
151    ConstantExprVal,          // This is an instance of ConstantExpr
152    ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
153    ConstantBoolVal,          // This is an instance of ConstantBool
154    ConstantSIntVal,          // This is an instance of ConstantSInt
155    ConstantUIntVal,          // This is an instance of ConstantUInt
156    ConstantFPVal,            // This is an instance of ConstantFP
157    ConstantArrayVal,         // This is an instance of ConstantArray
158    ConstantStructVal,        // This is an instance of ConstantStruct
159    ConstantPackedVal,        // This is an instance of ConstantPacked
160    ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
161    InlineAsmVal,             // This is an instance of InlineAsm
162    InstructionVal,           // This is an instance of Instruction
163
164    // Markers:
165    ConstantFirstVal = FunctionVal,
166    ConstantLastVal  = ConstantPointerNullVal
167  };
168
169  /// getValueType - Return an ID for the concrete type of this object.  This is
170  /// used to implement the classof checks.  This should not be used for any
171  /// other purpose, as the values may change as LLVM evolves.  Also, note that
172  /// starting with the InstructionVal value, the value stored is actually the
173  /// Instruction opcode, so there are more than just these values possible here
174  /// (and Instruction must be last).
175  ///
176  unsigned getValueType() const {
177    return SubclassID;
178  }
179
180  // Methods for support type inquiry through isa, cast, and dyn_cast:
181  static inline bool classof(const Value *) {
182    return true; // Values are always values.
183  }
184
185  /// getRawType - This should only be used to implement the vmcore library.
186  ///
187  const Type *getRawType() const { return Ty.getRawType(); }
188
189private:
190  /// FIXME: this is a gross hack, needed by another gross hack.  Eliminate!
191  void setValueType(unsigned short VT) { SubclassID = VT; }
192  friend class Instruction;
193};
194
195inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
196  V.print(OS);
197  return OS;
198}
199
200void Use::init(Value *v, User *user) {
201  Val = v;
202  U = user;
203  if (Val) Val->addUse(*this);
204}
205
206Use::~Use() {
207  if (Val) removeFromList();
208}
209
210void Use::set(Value *V) {
211  if (Val) removeFromList();
212  Val = V;
213  if (V) V->addUse(*this);
214}
215
216
217// isa - Provide some specializations of isa so that we don't have to include
218// the subtype header files to test to see if the value is a subclass...
219//
220template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
221  return Val.getValueType() >= Value::ConstantFirstVal &&
222         Val.getValueType() <= Value::ConstantLastVal;
223}
224template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
225  return Val.getValueType() == Value::ArgumentVal;
226}
227template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
228  return Val.getValueType() == Value::InlineAsmVal;
229}
230template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
231  return Val.getValueType() >= Value::InstructionVal;
232}
233template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
234  return Val.getValueType() == Value::BasicBlockVal;
235}
236template <> inline bool isa_impl<Function, Value>(const Value &Val) {
237  return Val.getValueType() == Value::FunctionVal;
238}
239template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
240  return Val.getValueType() == Value::GlobalVariableVal;
241}
242template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
243  return isa<GlobalVariable>(Val) || isa<Function>(Val);
244}
245
246} // End llvm namespace
247
248#endif
249